00001 /*! \file i2c.h \brief I2C interface using AVR Two-Wire Interface (TWI) hardware. */ 00002 //***************************************************************************** 00003 // 00004 // File Name : 'i2c.h' 00005 // Title : I2C interface using AVR Two-Wire Interface (TWI) hardware 00006 // Author : Pascal Stang - Copyright (C) 2002-2003 00007 // Created : 2002.06.25 00008 // Revised : 2003.03.03 00009 // Version : 0.9 00010 // Target MCU : Atmel AVR series 00011 // Editor Tabs : 4 00012 // 00013 // This code is distributed under the GNU Public License 00014 // which can be found at http://www.gnu.org/licenses/gpl.txt 00015 // 00016 //***************************************************************************** 00017 00018 #ifndef I2C_H 00019 #define I2C_H 00020 00021 #include <avr/twi.h> 00022 #include "global.h" 00023 00024 // include project-specific configuration 00025 #include "i2cconf.h" 00026 00027 // defines and constants 00028 #define TWCR_CMD_MASK 0x0F 00029 #define TWSR_STATUS_MASK 0xF8 00030 00031 // return values 00032 #define I2C_OK 0x00 00033 #define I2C_ERROR_NODEV 0x01 00034 00035 // types 00036 typedef enum 00037 { 00038 I2C_IDLE = 0, I2C_BUSY = 1, 00039 I2C_MASTER_TX = 2, I2C_MASTER_RX = 3, 00040 I2C_SLAVE_TX = 4, I2C_SLAVE_RX = 5 00041 } eI2cStateType; 00042 00043 // functions 00044 00045 //! Initialize I2C (TWI) interface 00046 void i2cInit(void); 00047 00048 //! Set the I2C transaction bitrate (in KHz) 00049 void i2cSetBitrate(u16 bitrateKHz); 00050 00051 // I2C setup and configurations commands 00052 //! Set the local (AVR processor's) I2C device address 00053 void i2cSetLocalDeviceAddr(u08 deviceAddr, u08 genCallEn); 00054 00055 //! Set the user function which handles receiving (incoming) data as a slave 00056 void i2cSetSlaveReceiveHandler(void (*i2cSlaveRx_func)(u08 receiveDataLength, u08* recieveData)); 00057 //! Set the user function which handles transmitting (outgoing) data as a slave 00058 void i2cSetSlaveTransmitHandler(u08 (*i2cSlaveTx_func)(u08 transmitDataLengthMax, u08* transmitData)); 00059 00060 // Low-level I2C transaction commands 00061 //! Send an I2C start condition in Master mode 00062 void i2cSendStart(void); 00063 //! Send an I2C stop condition in Master mode 00064 void i2cSendStop(void); 00065 //! Wait for current I2C operation to complete 00066 void i2cWaitForComplete(void); 00067 //! Send an (address|R/W) combination or a data byte over I2C 00068 void i2cSendByte(u08 data); 00069 //! Receive a data byte over I2C 00070 // ackFlag = TRUE if recevied data should be ACK'ed 00071 // ackFlag = FALSE if recevied data should be NACK'ed 00072 void i2cReceiveByte(u08 ackFlag); 00073 //! Pick up the data that was received with i2cReceiveByte() 00074 u08 i2cGetReceivedByte(void); 00075 //! Get current I2c bus status from TWSR 00076 u08 i2cGetStatus(void); 00077 00078 // high-level I2C transaction commands 00079 00080 //! send I2C data to a device on the bus 00081 void i2cMasterSend(u08 deviceAddr, u08 length, u08 *data); 00082 //! receive I2C data from a device on the bus 00083 void i2cMasterReceive(u08 deviceAddr, u08 length, u08* data); 00084 00085 //! send I2C data to a device on the bus (non-interrupt based) 00086 u08 i2cMasterSendNI(u08 deviceAddr, u08 length, u08* data); 00087 //! receive I2C data from a device on the bus (non-interrupt based) 00088 u08 i2cMasterReceiveNI(u08 deviceAddr, u08 length, u08 *data); 00089 00090 //! Get the current high-level state of the I2C interface 00091 eI2cStateType i2cGetState(void); 00092 00093 #endif
1.3-rc2