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 // types 00032 typedef enum 00033 { 00034 I2C_IDLE = 0, I2C_BUSY = 1, 00035 I2C_MASTER_TX = 2, I2C_MASTER_RX = 3, 00036 I2C_SLAVE_TX = 4, I2C_SLAVE_RX = 5 00037 } eI2cStateType; 00038 00039 // functions 00040 00041 //! Initialize I2C (TWI) interface 00042 void i2cInit(void); 00043 00044 //! Set the I2C transaction bitrate (in KHz) 00045 void i2cSetBitrate(u16 bitrateKHz); 00046 00047 // I2C setup and configurations commands 00048 //! Set the local (AVR processor's) I2C device address 00049 void i2cSetLocalDeviceAddr(u08 deviceAddr, u08 genCallEn); 00050 00051 //! Set the user function which handles receiving (incoming) data as a slave 00052 void i2cSetSlaveReceiveHandler(void (*i2cSlaveRx_func)(u08 receiveDataLength, u08* recieveData)); 00053 //! Set the user function which handles transmitting (outgoing) data as a slave 00054 void i2cSetSlaveTransmitHandler(u08 (*i2cSlaveTx_func)(u08 transmitDataLengthMax, u08* transmitData)); 00055 00056 // Low-level I2C transaction commands 00057 //! Send an I2C start condition in Master mode 00058 void i2cSendStart(void); 00059 //! Send an I2C stop condition in Master mode 00060 void i2cSendStop(void); 00061 //! Wait for current I2C operation to complete 00062 void i2cWaitForComplete(void); 00063 //! Send an (address|R/W) combination or a data byte over I2C 00064 void i2cSendByte(u08 data); 00065 //! Receive a data byte over I2C 00066 // ackFlag = TRUE if recevied data should be ACK'ed 00067 // ackFlag = FALSE if recevied data should be NACK'ed 00068 void i2cReceiveByte(u08 ackFlag); 00069 //! Pick up the data that was received with i2cReceiveByte() 00070 u08 i2cGetReceivedByte(void); 00071 //! Get current I2c bus status from TWSR 00072 u08 i2cGetStatus(void); 00073 00074 // high-level I2C transaction commands 00075 00076 //! send I2C data to a device on the bus 00077 void i2cMasterSend(u08 deviceAddr, u08 length, u08 *data); 00078 //! receive I2C data from a device on the bus 00079 void i2cMasterReceive(u08 deviceAddr, u08 length, u08* data); 00080 00081 //! send I2C data to a device on the bus (non-interrupt based) 00082 void i2cMasterSendNI(u08 deviceAddr, u08 length, u08* data); 00083 //! receive I2C data from a device on the bus (non-interrupt based) 00084 void i2cMasterReceiveNI(u08 deviceAddr, u08 length, u08 *data); 00085 00086 #endif