00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018 #ifndef I2C_H
00019 #define I2C_H
00020
00021 #include "global.h"
00022
00023
00024
00025 #define TW_START 0x08
00026 #define TW_REP_START 0x10
00027
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
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
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
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
00056 #define TW_NO_INFO 0xF8
00057 #define TW_BUS_ERROR 0x00
00058
00059
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
00067 #define TWCR_CMD_MASK 0x0F
00068 #define TWSR_STATUS_MASK 0xF8
00069
00070
00071 #define I2C_OK 0x00
00072 #define I2C_ERROR_NODEV 0x01
00073
00074
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
00083
00084 #define i2cMasterSend i2cMasterSendNI
00085 #define i2cMasterReceive i2cMasterReceiveNI
00086
00087
00088 void i2cInit(void);
00089
00090
00091 void i2cSetBitrate(u16 bitrateKHz);
00092
00093
00094
00095 void i2cSetLocalDeviceAddr(u08 deviceAddr, u08 genCallEn);
00096
00097
00098
00099 void i2cSendStart(void);
00100
00101 void i2cSendStop(void);
00102
00103 void i2cWaitForComplete(void);
00104
00105 void i2cSendByte(u08 data);
00106
00107
00108
00109 void i2cReceiveByte(u08 ackFlag);
00110
00111 u08 i2cGetReceivedByte(void);
00112
00113 u08 i2cGetStatus(void);
00114
00115
00116
00117
00118 u08 i2cMasterSendNI(u08 deviceAddr, u08 length, u08* data);
00119
00120 u08 i2cMasterReceiveNI(u08 deviceAddr, u08 length, u08 *data);
00121
00122 #endif