mos/dev/include/avr-i2c.h

Go to the documentation of this file.
00001 //  This file is part of MOS, the MANTIS Operating System
00002 //  See http://mantis.cs.colorado.edu/
00003 //
00004 //  Copyright (c) 2002 - 2007 University of Colorado, Boulder
00005 //
00006 //   All rights reserved.
00007 //
00008 //   Redistribution and use in source and binary forms, with or without
00009 //   modification, are permitted provided that the following conditions are
00010 //   met:
00011 //
00012 //       * Redistributions of source code must retain the above copyright
00013 //         notice, this list of conditions and the following disclaimer.
00014 //       * Redistributions in binary form must reproduce the above
00015 //         copyright notice, this list of conditions and the following
00016 //         disclaimer in the documentation and/or other materials provided
00017 //         with the distribution. 
00018 //       * Neither the name of the MANTIS Project nor the names of its
00019 //         contributors may be used to endorse or promote products derived
00020 //         from this software without specific prior written permission.
00021 //
00022 //   THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
00023 //   "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
00024 //   LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
00025 //   A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
00026 //   OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
00027 //   INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
00028 //   BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
00029 //   LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
00030 //   CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
00031 //   LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
00032 //   ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
00033 //   POSSIBILITY OF SUCH DAMAGE.
00034 
00042 #ifndef _AVR_I2C_H
00043 #define _AVR_I2C_H
00044 
00045 #define avr_i2c_set_brr( new_twbr )     TWBR = new_twbr
00046 #define avr_i2c_start_condition()       TWCR |= (1 << TWSTA)
00047 #define avr_i2c_stop_condition()        TWCR |= (1 << TWSTO)
00048 #define avr_i2c_interrupt_enable()      TWCR |= (1 << TWIE)
00049 #define avr_i2c_interrupt_disable()     TWCR &= ~(1 << TWIE)
00050 #define avr_i2c_enable()                TWCR |= (1 << TWEN)
00051 #define avr_i2c_disable()               TWCR &= ~(1 << TWEN)
00052 #define avr_i2c_get_status()            (TWSR & ~3)
00053 #define avr_i2c_put_byte( new_byte )    TWDR = new_byte
00054 #define avr_i2c_get_byte()              TWDR
00055 #define avr_i2c_set_slave_addr( new_slave )  TWAR = (new_slave << 1)
00056 #define avr_i2c_clear_int_flag()        TWCR |= (1 << TWINT)
00057 #define avr_i2c_check_int_flag()        TWCR & (1 << TWINT)
00058 #define avr_i2c_enable_general_call()   TWAR |= (1 << TWGCE)
00059 #define avr_i2c_disable_general_call()  TWAR &= ~(1 << TWGCE)
00060 
00061 
00062 
00063 #define I2C_ENABLE_ACK 0
00064 #define I2C_DISABLE_ACK 1
00065 #define I2C_DEST_ADDR 3
00066 #define I2C_SLAVE_ADDR 4
00067 #define I2C_SET_BRR 5
00068 
00069 #define I2C_IDLE 0
00070 #define I2C_READING 1
00071 #define I2C_WRITING 2
00072 
00073 
00074 //Status Codes for Master Transmitter Mode
00075 #define START 0x08 //A START condition has been transmitted.
00076 #define START_REPEAT  0x10 //A repeated START condition has been transmitted.
00077 #define MT_SLA_ACK 0x18 //SLA+W has been transmitted; ACK has been received
00078 #define MT_SLA_NACK 0x20 //SLA+W has been transmitted; NOT ACK received
00079 #define MT_DATA_ACK 0x28 //DATA has been transmitted, ACK has been received
00080 #define MT_DATA_NACK 0x30 //DATA has been transmitted, NOT ACK received
00081 #define MT_ARB_LOST 0x38 //Arbitration lost in SLA+W or data bytes
00082 #define MT_DATA_DONE 0x01
00083 
00084 //Status Codes for Master Receiver Mode
00085 #define MR_SLA_ACK 0x40 //SLA+R has been transmitted, ACK has been received
00086 #define MR_SLA_NACK 0x48 //SLAR+R has been transmitted, NOT ACK received
00087 #define MR_DATA_ACK 0x50 //DATA byte has been received, ACK returned
00088 #define MR_DATA_NACK 0x58 //DATA has been received, NOT ACK returned
00089 
00090 //Status Codes for Slave Receiver Mode
00091 #define OWN_SLAW_RECEIVED_ACK 0x60 //Own SLA+W has been received; ACK returned
00092 #define ARBITRATION_LOST_SLARW 0x68 //Arbitration lost in SLA+R/W as master; own SLA+W has been received; ACK has been returned
00093 #define GENERAL_ADDR_RECEIVED 0x70 //General call address has been received; ACK has been returned
00094 #define ARBITRATION_LOST_SLARW_GEN 0x78 //Arbitration lost in SLA+R/W as master; General call address has been received; ACK has been returned
00095 #define PREV_ADDR_DATA_RECEIVED_ACK 0x80 //Previously addressed with own SLA+W; data has been received ACK has been returned
00096 #define PREV_ADDR_DATA_RECEIVED_NACK 0x88 //Previously addressed with own SLA+W; data has been received; NOT ACK has been returned
00097 #define PREV_GENERAL_ADDR_DATA_RECEIVED_ACK 0x90 //Previously addressed with general call; data has been received; ACK has been returned
00098 #define PREV_GENERAL_ADDR_DATA_RECEIVED_NACK 0x98 //Previously addressed with general call; data has been received; NOT ACK has been returned
00099 #define STOP_REPEATED_START 0xA0 //A STOP condition or repeated START condition has been received while still addressed as slave
00100 
00101 //Status Codes for Slave Transmitter Mode
00102 #define SLAR_RECEIVED_ACK 0xA8 //Own SLA+R has been received; ACK has been returned.
00103 #define ARBITRATION_LOST_SLARW_ACK 0xB0 //Arbitration lost in SLA+R/W as master; own SLA+R has been received; ACK has been returned
00104 #define SLAVE_DATA_XMITTED_ACK 0xB8 //Data byte in TWDR has been transmitted; ACK hs been received
00105 #define SLAVE_DATA_XMITTED_NACK 0xC0 //Data byte in TWDR has been transmitted; NOT ACK has been received
00106 #define LAST_BYTE_XMITTED 0xC8 //Last data byte in TWDR has been transmitted (TWEA = "0"); ACK has been received
00107 
00108 //Miscellaneous States
00109 #define NO_RELEVANT_STATE 0xF8 //No relevant state information available; TWINT="0"
00110 #define BUS_ERROR 0x00 //Bus error due to an illegal START or STOP condition
00111 
00112 #define I2C_MT 0  //master transmitter
00113 #define I2C_MR 1  //master receiver
00114 #define I2C_ST 2  //slave transmitter
00115 #define I2C_SR 3  //slave receiver
00116 
00119 void avr_i2c_init();
00120 
00121 #endif

Generated on Mon Nov 23 06:25:59 2009 for MANTIS by  doxygen 1.4.6