i2c.h 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524
  1. /******************************************************************************
  2. * @brief header file for I2C module utilities (I2C).
  3. *
  4. *******************************************************************************
  5. *
  6. * provide APIs for accessing I2C module (I2C)
  7. ******************************************************************************/
  8. #ifndef _I2C_H__
  9. #define _I2C_H__
  10. #ifdef __cplusplus
  11. extern "C" {
  12. #endif
  13. /******************************************************************************
  14. * Global variables
  15. ******************************************************************************/
  16. /******************************************************************************
  17. * Constants and macros
  18. ******************************************************************************/
  19. /******************************************************************************
  20. * define I2C work read or write
  21. *
  22. *//*! @addtogroup i2c_read_write_list
  23. * @{
  24. *******************************************************************************/
  25. #define I2C_READ 0x01 /*!< I2C read */
  26. #define I2C_WRITE 0x0 /*!< I2C write */
  27. /*! @} End of i2c_read_write_list */
  28. #define I2C_SEND_ACK 0 /*!< I2C send ACK */
  29. #define I2C_SEND_NACK 1 /*!< I2C send NACK */
  30. #define I2C_WAIT_STATUS_ETMEOUT 200000
  31. /******************************************************************************
  32. * define I2C error state
  33. *
  34. *//*! @addtogroup i2c_error_state_list
  35. * @{
  36. *******************************************************************************/
  37. #define I2C_ERROR_NULL 0x00 /*!< I2C sucess*/
  38. #define I2C_ERROR_NO_WAIT_TCF_FLAG 0x01 /*!< I2C wait TCF overETMe*/
  39. #define I2C_ERROR_NO_WAIT_IICIF_FLAG 0x02 /*!< I2C wait IICIF overETMe */
  40. #define I2C_ERROR_NO_GET_ACK 0x04 /*!< I2C no get ACK */
  41. #define I2C_ERROR_START_NO_BUSY_FLAG 0x10 /*!< I2C fail to send start signals */
  42. #define I2C_ERROR_STOP_BUSY_FLAG 0x20 /*!< I2C fail to send stop signal */
  43. #define I2C_ERROR_BUS_BUSY 0x80 /*!< I2C bus busy error */
  44. /*! @} End of i2c_error_state_list */
  45. /******************************************************************************
  46. * define I2C bus state
  47. *
  48. *//*! @addtogroup i2c_bus_state_list
  49. * @{
  50. *******************************************************************************/
  51. #define I2C_BUS_NORMAL 0x00 /*!< I2C bus normal */
  52. #define I2C_BUS_SLTF 0x01 /*!< I2C bus SLTF flag */
  53. #define I2C_BUS_SHTF2 0x02 /*!< I2C bus SHTF2 flag */
  54. /*! @} End of i2c_bus_state_list */
  55. /******************************************************************************
  56. * Local types
  57. ******************************************************************************/
  58. /******************************************************************************
  59. * Local function prototypes
  60. ******************************************************************************/
  61. /******************************************************************************
  62. * Local variables
  63. ******************************************************************************/
  64. #define I2C_MODE_MASTER 1
  65. #define I2C_MODE_SLAVE 0
  66. #define I2C_ADDRESS_7BIT 0
  67. #define I2C_ADDRESS_10BIT 1
  68. #define I2C_ETMEOUT_BUS_CLOCK_DIV64 0
  69. #define I2C_ETMEOUT_BUS_CLOCK 1
  70. /******************************************************************************
  71. *
  72. *//*! @addtogroup i2c_setting_type
  73. * @{
  74. *******************************************************************************/
  75. /*!
  76. * @brief I2C setting type.
  77. *
  78. */
  79. typedef struct
  80. {
  81. uint16_t bI2CEn :1; /*!< enable I2C module */
  82. uint16_t bIntEn :1; /*!< enable I2C enable */
  83. uint16_t bWakeUpEn :1; /*!< I2C wake up enable */
  84. uint16_t bGCAEn :1; /*!< I2C General call address enable */
  85. uint16_t bAddressExt :1; /*!< I2C extertion address selection */
  86. uint16_t bRangeAddEn :1; /*!< enable range address */
  87. uint16_t bFackEn :1; /*!< enable fast ack */
  88. uint16_t bSMB_AlertEn :1; /*!< SMB Alert enable */
  89. uint16_t bSecondAddressEn:1; /*!< enable the second address */
  90. uint16_t bETMeoutCountClockSelect:1; /*!< ETMeoutCountClockSelect */
  91. uint16_t bSHTF2IntEn :1; /*!< SHTF2 interrupt enable */
  92. uint16_t Reserve :5;
  93. }I2C_SettingType;
  94. /*! @} End of i2c_setting_type */
  95. /******************************************************************************
  96. *
  97. *//*! @addtogroup i2c_config_type
  98. * @{
  99. *******************************************************************************/
  100. /*!
  101. * @brief I2C configure type.
  102. *
  103. */
  104. typedef struct
  105. {
  106. I2C_SettingType sSetting;
  107. uint16_t u16F; /*!< setting the band rate for I2C */
  108. uint16_t u16OwnA1; /*!< slave address */
  109. uint16_t u16OwnA2; /*!< the second slave address */
  110. uint16_t u16RangeA; /*!< range address */
  111. uint16_t u16Filt; /*!< Filter for I2C */
  112. uint16_t u16Slt; /*!< SCL Low ETMeout register low */
  113. }I2C_ConfigType, *I2C_ConfigPtr;
  114. /*! @} End of i2c_config_type */
  115. /******************************************************************************
  116. *
  117. *//*! @addtogroup i2c_callback
  118. * @{
  119. *******************************************************************************/
  120. typedef void (*I2C_CallbackType)(void); /*!< I2C call back function */
  121. /*! @} End of i2c_callback */
  122. /******************************************************************************
  123. * inline functions
  124. ******************************************************************************/
  125. /******************************************************************************
  126. *
  127. *//*! @addtogroup i2c_api_list
  128. * @{
  129. *******************************************************************************/
  130. /*****************************************************************************//*!
  131. *
  132. * @brief enable I2C to transmit data.
  133. *
  134. * @param[in] pI2Cx point to I2C module type.
  135. *
  136. * @return none
  137. *
  138. * @ Pass/ Fail criteria: none
  139. *****************************************************************************/
  140. __STATIC_INLINE void I2C_TxEnable(I2C_Type *pI2Cx)
  141. {
  142. pI2Cx->C1 |= I2C_C1_TX_MASK;
  143. }
  144. /*****************************************************************************//*!
  145. *
  146. * @brief enable I2C to receive data.
  147. *
  148. * @param[in] pI2Cx point to I2C module type.
  149. *
  150. * @return none.
  151. *
  152. * @ Pass/ Fail criteria: none.
  153. *****************************************************************************/
  154. __STATIC_INLINE void I2C_RxEnable(I2C_Type *pI2Cx)
  155. {
  156. pI2Cx->C1 &= ~I2C_C1_TX_MASK;
  157. }
  158. /*****************************************************************************//*!
  159. *
  160. * @brief set IIC band rate.
  161. *
  162. * @param[in] pI2Cx point to I2C module type.
  163. *
  164. * @return none
  165. *
  166. * @ Pass/ Fail criteria: none
  167. *****************************************************************************/
  168. __STATIC_INLINE void I2C_SetBaudRate(I2C_Type *pI2Cx,uint32_t u32Bps)
  169. {
  170. pI2Cx->F = (uint8_t)u32Bps;
  171. }
  172. /*****************************************************************************//*!
  173. *
  174. * @brief enable general call.
  175. *
  176. * @param[in] pI2Cx point to I2C module type.
  177. *
  178. * @return none.
  179. *
  180. * @ Pass/ Fail criteria: none.
  181. *****************************************************************************/
  182. __STATIC_INLINE void I2C_GeneralCallEnable(I2C_Type *pI2Cx)
  183. {
  184. pI2Cx->C2 |= I2C_C2_GCAEN_MASK;
  185. }
  186. /*****************************************************************************//*!
  187. *
  188. * @brief SMBus Alert Response Address Enable.
  189. *
  190. * @param[in] pI2Cx point to I2C module type.
  191. *
  192. * @return none
  193. *
  194. * @ Pass/ Fail criteria: none
  195. *****************************************************************************/
  196. __STATIC_INLINE void I2C_SMBusAlertEnable(I2C_Type *pI2Cx)
  197. {
  198. pI2Cx->SMB|= I2C_SMB_ALERTEN_MASK;
  199. }
  200. /*****************************************************************************//*!
  201. *
  202. * @brief Range Address Matching Enable.
  203. *
  204. * @param[in] pI2Cx point to I2C module type.
  205. *
  206. * @return none
  207. *
  208. * @ Pass/ Fail criteria: none
  209. *****************************************************************************/
  210. __STATIC_INLINE void I2C_RangeAddressEnable(I2C_Type *pI2Cx)
  211. {
  212. pI2Cx->C2 |= I2C_C2_RMEN_MASK;
  213. }
  214. /*****************************************************************************//*!
  215. *
  216. * @brief SHTF2 Interrupt Enable.
  217. *
  218. * @param[in] pI2Cx point to I2C module type.
  219. *
  220. * @return none
  221. *
  222. * @ Pass/ Fail criteria: none
  223. *****************************************************************************/
  224. __STATIC_INLINE void I2C_SHTF2IntEnable(I2C_Type *pI2Cx)
  225. {
  226. pI2Cx->SMB |= I2C_SMB_SHTF2IE_MASK;
  227. }
  228. /*****************************************************************************//*!
  229. *
  230. * @brief ETMeout Counter Clock Select.
  231. *
  232. * @param[in] pI2Cx point to I2C module type.
  233. *
  234. * @return none
  235. *
  236. * @ Pass/ Fail criteria: none
  237. *****************************************************************************/
  238. __STATIC_INLINE void I2C_ETMeoutCounterClockSelect(I2C_Type *pI2Cx, uint8_t u8Clock)
  239. {
  240. if( u8Clock )
  241. {
  242. pI2Cx->SMB |= I2C_SMB_TCKSEL_MASK;
  243. }
  244. else
  245. {
  246. pI2Cx->SMB &= ~I2C_SMB_TCKSEL_MASK;
  247. }
  248. }
  249. /*****************************************************************************//*!
  250. *
  251. * @brief get I2C status.
  252. *
  253. * @param[in] pI2Cx point to I2C module type.
  254. *
  255. * @return I2C status
  256. *
  257. * @ Pass/ Fail criteria: none
  258. *****************************************************************************/
  259. __STATIC_INLINE uint8_t I2C_GetStatus(I2C_Type *pI2Cx)
  260. {
  261. return pI2Cx->S;
  262. }
  263. /*****************************************************************************//*!
  264. *
  265. * @brief clear specified status.
  266. *
  267. * @param[in] pI2Cx point to I2C module type.
  268. *
  269. * @return none
  270. *
  271. * @ Pass/ Fail criteria: none
  272. *****************************************************************************/
  273. __STATIC_INLINE void I2C_ClearStatus(I2C_Type *pI2Cx, uint8_t u8ClearFlag)
  274. {
  275. pI2Cx->S |= u8ClearFlag;
  276. }
  277. /*****************************************************************************//*!
  278. *
  279. * @brief write data to data register.
  280. *
  281. * @param[in] pI2Cx point to I2C module type.
  282. *
  283. * @return none
  284. *
  285. * @ Pass/ Fail criteria: none
  286. *****************************************************************************/
  287. __STATIC_INLINE void I2C_WriteDataReg(I2C_Type *pI2Cx, uint8_t u8DataBuff)
  288. {
  289. pI2Cx->D = u8DataBuff;
  290. }
  291. /*****************************************************************************//*!
  292. *
  293. * @brief read data from data register.
  294. *
  295. * @param[in] pI2Cx point to I2C module type.
  296. *
  297. * @return I2C data register value
  298. *
  299. * @ Pass/ Fail criteria: none
  300. *****************************************************************************/
  301. __STATIC_INLINE uint8_t I2C_ReadDataReg(I2C_Type *pI2Cx )
  302. {
  303. return pI2Cx->D;
  304. }
  305. /*****************************************************************************//*!
  306. *
  307. * @brief check if is Tx mode.
  308. *
  309. * @param[in] pI2Cx point to I2C module type.
  310. *
  311. * @return result TRUE or FALSE
  312. *
  313. * @ Pass/ Fail criteria: none
  314. *****************************************************************************/
  315. __STATIC_INLINE uint8_t I2C_IsTxMode(I2C_Type *pI2Cx )
  316. {
  317. return(pI2Cx->C1 & I2C_C1_TX_MASK);
  318. }
  319. /*****************************************************************************//*!
  320. *
  321. * @brief check I2C if busy.
  322. *
  323. * @param[in] pI2Cx point to I2C module type.
  324. *
  325. * @return result TRUE or FALSE
  326. *
  327. * @ Pass/ Fail criteria: none
  328. *****************************************************************************/
  329. __STATIC_INLINE uint8_t I2C_IsBusy(I2C_Type *pI2Cx )
  330. {
  331. return (pI2Cx->S & I2C_S_BUSY_MASK);
  332. }
  333. /*****************************************************************************//*!
  334. *
  335. * @brief check I2C receive ack or nack.
  336. *
  337. * @param[in] pI2Cx point to I2C module type.
  338. *
  339. * @return result TRUE or FALSE
  340. *
  341. * @ Pass/ Fail criteria: none.
  342. *****************************************************************************/
  343. __STATIC_INLINE uint8_t I2C_IsReceivedAck(I2C_Type *pI2Cx )
  344. {
  345. return (pI2Cx->S & I2C_S_RXAK_MASK);
  346. }
  347. /*****************************************************************************//*!
  348. *
  349. * @brief check I2C if is master mode.
  350. *
  351. * @param[in] pI2Cx point to I2C module type.
  352. *
  353. * @return result TRUE or FALSE.
  354. *
  355. * @ Pass/ Fail criteria: none.
  356. *****************************************************************************/
  357. __STATIC_INLINE uint8_t I2C_IsMasterMode(I2C_Type *pI2Cx )
  358. {
  359. return(pI2Cx->C1 & I2C_C1_MST_MASK);
  360. }
  361. /*****************************************************************************//*!
  362. *
  363. * @brief check SCL Low ETMeout Flag.
  364. *
  365. * @param[in] pI2Cx point to I2C module type.
  366. *
  367. * @return result TRUE or FALSE.
  368. *
  369. * @ Pass/ Fail criteria: none
  370. *****************************************************************************/
  371. __STATIC_INLINE uint8_t I2C_IsSMB_SLTF(I2C_Type *pI2Cx )
  372. {
  373. return (pI2Cx->SMB & I2C_SMB_SLTF_MASK);
  374. }
  375. /*****************************************************************************//*!
  376. *
  377. * @brief check SCL High ETMeout Flag is set or clear.
  378. *
  379. * @param[in] pI2Cx point to I2C module type.
  380. *
  381. * @return result TRUE or FALSE.
  382. *
  383. * @ Pass/ Fail criteria: none
  384. *****************************************************************************/
  385. __STATIC_INLINE uint8_t I2C_IsSMB_SHTF2(I2C_Type *pI2Cx )
  386. {
  387. return(pI2Cx->SMB & I2C_SMB_SHTF2_MASK);
  388. }
  389. /*****************************************************************************//*!
  390. *
  391. * @brief clear SLTF flag.
  392. *
  393. * @param[in] pI2Cx point to I2C module type.
  394. *
  395. * @return none.
  396. *
  397. * @ Pass/ Fail criteria: none.
  398. *****************************************************************************/
  399. __STATIC_INLINE void I2C_ClearSLTF(I2C_Type *pI2Cx )
  400. {
  401. pI2Cx->SMB |= I2C_SMB_SLTF_MASK;
  402. }
  403. /*****************************************************************************//*!
  404. *
  405. * @brief clear SHTF2 flag.
  406. *
  407. * @param[in] pI2Cx point to I2C module type.
  408. *
  409. * @return none
  410. *
  411. * @ Pass/ Fail criteria: none
  412. *****************************************************************************/
  413. __STATIC_INLINE void I2C_ClearSHTF2(I2C_Type *pI2Cx )
  414. {
  415. pI2Cx->SMB |= I2C_SMB_SHTF2_MASK;
  416. }
  417. /*****************************************************************************//*!
  418. *
  419. * @brief send out ACK.
  420. *
  421. * @param[in] pI2Cx point to I2C module type.
  422. *
  423. * @return none.
  424. *
  425. * @ Pass/ Fail criteria: none.
  426. *****************************************************************************/
  427. __STATIC_INLINE void I2C_SendAck(I2C_Type *pI2Cx )
  428. {
  429. pI2Cx->C1 &= ~I2C_C1_TXAK_MASK;
  430. }
  431. /*****************************************************************************//*!
  432. *
  433. * @brief send out NACK.
  434. *
  435. * @param[in] pI2Cx point to I2C module type.
  436. *
  437. * @return none.
  438. *
  439. * @ Pass/ Fail criteria: none.
  440. *****************************************************************************/
  441. __STATIC_INLINE void I2C_SendNack(I2C_Type *pI2Cx )
  442. {
  443. pI2Cx->C1 |= I2C_C1_TXAK_MASK;
  444. }
  445. /*****************************************************************************//*!
  446. *
  447. * @brief Second I2C Address Enable.
  448. *
  449. * @param[in] pI2Cx point to I2C module type.
  450. *
  451. * @return none.
  452. *
  453. * @ Pass/ Fail criteria: none.
  454. *****************************************************************************/
  455. __STATIC_INLINE void I2C_SecondAddressEnable(I2C_Type *pI2Cx)
  456. {
  457. pI2Cx->SMB |= I2C_SMB_SIICAEN_MASK;
  458. }
  459. /******************************************************************************
  460. * Global functions
  461. ******************************************************************************/
  462. void I2C_Init(I2C_Type *pI2Cx,I2C_ConfigPtr pI2CConfig);
  463. uint8_t I2C_Start(I2C_Type *pI2Cx);
  464. uint8_t I2C_Stop(I2C_Type *pI2Cx);
  465. uint8_t I2C_RepeatStart(I2C_Type *pI2Cx);
  466. uint8_t I2C_IsTxMode(I2C_Type *pI2Cx );
  467. uint8_t I2C_IsBusy(I2C_Type *pI2Cx );
  468. uint8_t I2C_IsReceivedAck(I2C_Type *pI2Cx );
  469. uint8_t I2C_IsMasterMode(I2C_Type *pI2Cx );
  470. void I2C_ClearSHTF2(I2C_Type *pI2Cx );
  471. void I2C_ClearSLTF(I2C_Type *pI2Cx );
  472. uint8_t I2C_IsSMB_SHTF2(I2C_Type *pI2Cx );
  473. uint8_t I2C_IsSMB_SLTF(I2C_Type *pI2Cx );
  474. void I2C_TxEnable(I2C_Type *pI2Cx);
  475. void I2C_RxEnable(I2C_Type *pI2Cx);
  476. void I2C_IntEnable(I2C_Type *pI2Cx);
  477. void I2C_IntDisable(I2C_Type *pI2Cx);
  478. void I2C_SetBaudRate(I2C_Type *pI2Cx,uint32_t u32Bps);
  479. void I2C_SetSlaveAddress(I2C_Type *pI2Cx,uint16_t u16SlaveAddress);
  480. void I2C_GeneralCallEnable(I2C_Type *pI2Cx);
  481. void I2C_SMBusAlertEnable(I2C_Type *pI2Cx);
  482. void I2C_RangeAddressEnable(I2C_Type *pI2Cx);
  483. void I2C_SHTF2IntEnable(I2C_Type *pI2Cx);
  484. void I2C_ETMeoutCounterClockSelect(I2C_Type *pI2Cx, uint8_t u8Clock);
  485. void I2C_SetSCLLowETMeout(I2C_Type *pI2Cx, uint16_t u16ETMeout);
  486. uint8_t I2C_GetStatus(I2C_Type *pI2Cx);
  487. void I2C_ClearStatus(I2C_Type *pI2Cx, uint8_t u8ClearFlag);
  488. void I2C_SendAck(I2C_Type *pI2Cx );
  489. void I2C_SendNack(I2C_Type *pI2Cx );
  490. void I2C_SecondAddressEnable(I2C_Type *pI2Cx);
  491. void I2C_ClearStatus(I2C_Type *pI2Cx, uint8_t u8ClearFlag);
  492. void I2C_WriteDataReg(I2C_Type *pI2Cx, uint8_t u8DataBuff);
  493. uint8_t I2C_ReadDataReg(I2C_Type *pI2Cx );
  494. void I2C_Deinit(I2C_Type *pI2Cx);
  495. uint8_t I2C_WriteOneByte(I2C_Type *pI2Cx, uint8_t u8WrBuff);
  496. uint8_t I2C_ReadOneByte(I2C_Type *pI2Cx, uint8_t *pRdBuff, uint8_t u8Ack);
  497. uint8_t I2C_MasterSendWait(I2C_Type *pI2Cx,uint16_t u16SlaveAddress,uint8_t *pWrBuff,uint32_t u32Length);
  498. uint8_t I2C_MasterReadWait(I2C_Type *pI2Cx,uint16_t u16SlaveAddress,uint8_t *pRdBuff,uint32_t u32Length);
  499. void I2C0_SetCallBack( I2C_CallbackType pCallBack );
  500. void I2C1_SetCallBack( I2C_CallbackType pCallBack );
  501. /*! @} End of i2c_bus_state_list */
  502. #ifdef __cplusplus
  503. }
  504. #endif
  505. #endif //