1 #ifndef __SWM341_I2C_H__
2 #define __SWM341_I2C_H__
3 
4 
5 typedef struct {
6     uint8_t  Master;        //1 主机模式    0 从机模式
7     uint32_t MstClk;        //主机传输时钟频率
8 
9     uint8_t  Addr10b;       //1 10位地址模式     0 7位地址模式
10     uint16_t SlvAddr;       //从机地址
11     uint16_t SlvAddrMsk;
12 
13     uint8_t  TXEmptyIEn;    //发送寄存器空中断使能
14     uint8_t  RXNotEmptyIEn; //接收寄存器非空中断使能
15     uint8_t  SlvSTADetIEn;  //从机检测到起始中断使能
16     uint8_t  SlvSTODetIEn;  //从机检测到终止中断使能
17 } I2C_InitStructure;
18 
19 
20 /* Interrupt Type */
21 #define I2C_IT_TX_EMPTY     (1 <<  0)   //TX FIFO Empty
22 #define I2C_IT_RX_NOT_EMPTY (1 <<  1)   //RX FIFO Not Empty
23 #define I2C_IT_RX_OVF       (1 <<  2)   //RX FIFO Overflow
24 #define I2C_IT_TX_DONE      (1 <<  3)   //发送完成(接收到ACK)
25 #define I2C_IT_RX_DONE      (1 <<  4)   //接收完成(发送出ACK)
26 #define I2C_IT_SLV_DET_STA  (1 <<  8)   //从机检测到起始位
27 #define I2C_IT_SLV_DET_STP  (1 <<  9)   //从机检测到停止位
28 #define I2C_IT_ARB_LOST     (1 << 16)   //主机Arbitration lost
29 #define I2C_IT_SCL_LOW_TO   (1 << 17)   //主机SCL Low Timeout
30 
31 
32 void I2C_Init(I2C_TypeDef * I2Cx, I2C_InitStructure * initStruct);
33 
34 void I2C_Open(I2C_TypeDef * I2Cx);
35 void I2C_Close(I2C_TypeDef * I2Cx);
36 
37 uint8_t I2C_Start(I2C_TypeDef * I2Cx, uint8_t addr, uint8_t wait);
38 void I2C_Stop(I2C_TypeDef * I2Cx, uint8_t wait);
39 uint8_t I2C_Write(I2C_TypeDef * I2Cx, uint8_t data, uint8_t wait);
40 uint8_t I2C_Read(I2C_TypeDef * I2Cx, uint8_t ack, uint8_t wait);
41 
42 uint8_t I2C_StartDone(I2C_TypeDef * I2Cx);
43 uint8_t I2C_StopDone(I2C_TypeDef * I2Cx);
44 uint8_t I2C_WriteDone(I2C_TypeDef * I2Cx);
45 uint8_t I2C_ReadDone(I2C_TypeDef * I2Cx);
46 uint8_t I2C_IsAck(I2C_TypeDef * I2Cx);
47 
48 void I2C_INTEn(I2C_TypeDef * I2Cx, uint32_t it);        //中断使能
49 void I2C_INTDis(I2C_TypeDef * I2Cx, uint32_t it);       //中断禁止
50 void I2C_INTClr(I2C_TypeDef * I2Cx, uint32_t it);       //中断标志清除
51 uint32_t I2C_INTStat(I2C_TypeDef * I2Cx, uint32_t it);  //中断状态查询
52 
53 #endif //__SWM341_I2C_H__
54