Main Page | Modules | Data Structures | Directories | File List | Data Fields | Globals | Related Pages

# i2c.h

    00001 /*! \file i2c.h \brief I2C interface using Philips LPC internal I2C hardware. */
    00002 //*****************************************************************************
    00003 //
    00004 // File Name    : 'i2c.h'
    00005 // Title        : I2C interface using Philips LPC internal I2C hardware
    00006 // Author       : Pascal Stang - Copyright (C) 2004
    00007 // Created      : 2004.05.05
    00008 // Revised      : 2004.07.12
    00009 // Version      : 0.9
    00010 // Target MCU   : ARM, Philips LPC2100-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 "global.h"
    00022
    00023 /* TWSR values (not bits) */
    00024 /* Master */
    00025 #define TW_START                    0x08
    00026 #define TW_REP_START                0x10
    00027 /* Master Transmitter */
    00028 #define TW_MT_SLA_ACK               0x18
    00029 #define TW_MT_SLA_NACK              0x20
    00030 #define TW_MT_DATA_ACK              0x28
    00031 #define TW_MT_DATA_NACK             0x30
    00032 #define TW_MT_ARB_LOST              0x38
    00033 /* Master Receiver */
    00034 #define TW_MR_ARB_LOST              0x38
    00035 #define TW_MR_SLA_ACK               0x40
    00036 #define TW_MR_SLA_NACK              0x48
    00037 #define TW_MR_DATA_ACK              0x50
    00038 #define TW_MR_DATA_NACK             0x58
    00039 /* Slave Transmitter */
    00040 #define TW_ST_SLA_ACK               0xA8
    00041 #define TW_ST_ARB_LOST_SLA_ACK      0xB0
    00042 #define TW_ST_DATA_ACK              0xB8
    00043 #define TW_ST_DATA_NACK             0xC0
    00044 #define TW_ST_LAST_DATA             0xC8
    00045 /* Slave Receiver */
    00046 #define TW_SR_SLA_ACK               0x60
    00047 #define TW_SR_ARB_LOST_SLA_ACK      0x68
    00048 #define TW_SR_GCALL_ACK             0x70
    00049 #define TW_SR_ARB_LOST_GCALL_ACK    0x78
    00050 #define TW_SR_DATA_ACK              0x80
    00051 #define TW_SR_DATA_NACK             0x88
    00052 #define TW_SR_GCALL_DATA_ACK        0x90
    00053 #define TW_SR_GCALL_DATA_NACK       0x98
    00054 #define TW_SR_STOP                  0xA0
    00055 /* Misc */
    00056 #define TW_NO_INFO                  0xF8
    00057 #define TW_BUS_ERROR                0x00
    00058
    00059 // bit defines
    00060 #define I2CON_AA            2
    00061 #define I2CON_SI            3
    00062 #define I2CON_STO           4
    00063 #define I2CON_STA           5
    00064 #define I2CON_I2EN          6
    00065
    00066 // defines and constants
    00067 #define TWCR_CMD_MASK       0x0F
    00068 #define TWSR_STATUS_MASK    0xF8
    00069
    00070 // return values
    00071 #define I2C_OK              0x00
    00072 #define I2C_ERROR_NODEV     0x01
    00073
    00074 // types
    00075 typedef enum
    00076 {
    00077     I2C_IDLE = 0, I2C_BUSY = 1,
    00078     I2C_MASTER_TX = 2, I2C_MASTER_RX = 3,
    00079     I2C_SLAVE_TX = 4, I2C_SLAVE_RX = 5
    00080 } eI2cStateType;
    00081
    00082 // functions
    00083
    00084 #define i2cMasterSend       i2cMasterSendNI
    00085 #define i2cMasterReceive    i2cMasterReceiveNI
    00086
    00087 //! Initialize I2C (TWI) interface
    00088 void i2cInit(void);
    00089
    00090 //! Set the I2C transaction bitrate (in KHz)
    00091 void i2cSetBitrate(u16 bitrateKHz);
    00092
    00093 // I2C setup and configurations commands
    00094 //! Set the local (AVR processor's) I2C device address
    00095 void i2cSetLocalDeviceAddr(u08 deviceAddr, u08 genCallEn);
    00096
    00097 // Low-level I2C transaction commands
    00098 //! Send an I2C start condition in Master mode
    00099 void i2cSendStart(void);
    00100 //! Send an I2C stop condition in Master mode
    00101 void i2cSendStop(void);
    00102 //! Wait for current I2C operation to complete
    00103 void i2cWaitForComplete(void);
    00104 //! Send an (address|R/W) combination or a data byte over I2C
    00105 void i2cSendByte(u08 data);
    00106 //! Receive a data byte over I2C
    00107 // ackFlag = TRUE if recevied data should be ACK'ed
    00108 // ackFlag = FALSE if recevied data should be NACK'ed
    00109 void i2cReceiveByte(u08 ackFlag);
    00110 //! Pick up the data that was received with i2cReceiveByte()
    00111 u08 i2cGetReceivedByte(void);
    00112 //! Get current I2c bus status from TWSR
    00113 u08 i2cGetStatus(void);
    00114
    00115 // high-level I2C transaction commands
    00116
    00117 //! send I2C data to a device on the bus (non-interrupt based)
    00118 u08 i2cMasterSendNI(u08 deviceAddr, u08 length, u08* data);
    00119 //! receive I2C data from a device on the bus (non-interrupt based)
    00120 u08 i2cMasterReceiveNI(u08 deviceAddr, u08 length, u08 *data);
    00121
    00122 #endif