i2c.h 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413
  1. /**************************************************************************//**
  2. * @file I2C.h
  3. * @version V3.0
  4. * $Revision: 19 $
  5. * $Date: 15/08/11 10:26a $
  6. * @brief M451 Series I2C Driver Header File
  7. *
  8. * @note
  9. * Copyright (C) 2014~2015 Nuvoton Technology Corp. All rights reserved.
  10. *
  11. ******************************************************************************/
  12. #ifndef __I2C_H__
  13. #define __I2C_H__
  14. #include "M451Series.h"
  15. #ifdef __cplusplus
  16. extern "C"
  17. {
  18. #endif
  19. /** @addtogroup Standard_Driver Standard Driver
  20. @{
  21. */
  22. /** @addtogroup I2C_Driver I2C Driver
  23. @{
  24. */
  25. /** @addtogroup I2C_EXPORTED_CONSTANTS I2C Exported Constants
  26. @{
  27. */
  28. /*---------------------------------------------------------------------------------------------------------*/
  29. /* I2C_CTL constant definitions. */
  30. /*---------------------------------------------------------------------------------------------------------*/
  31. #define I2C_CTL_STA_SI 0x28UL /*!< I2C_CTL setting for I2C control bits. It would set STA and SI bits */
  32. #define I2C_CTL_STA_SI_AA 0x2CUL /*!< I2C_CTL setting for I2C control bits. It would set STA, SI and AA bits */
  33. #define I2C_CTL_STO_SI 0x18UL /*!< I2C_CTL setting for I2C control bits. It would set STO and SI bits */
  34. #define I2C_CTL_STO_SI_AA 0x1CUL /*!< I2C_CTL setting for I2C control bits. It would set STO, SI and AA bits */
  35. #define I2C_CTL_SI 0x08UL /*!< I2C_CTL setting for I2C control bits. It would set SI bit */
  36. #define I2C_CTL_SI_AA 0x0CUL /*!< I2C_CTL setting for I2C control bits. It would set SI and AA bits */
  37. #define I2C_CTL_STA 0x20UL /*!< I2C_CTL setting for I2C control bits. It would set STA bit */
  38. #define I2C_CTL_STO 0x10UL /*!< I2C_CTL setting for I2C control bits. It would set STO bit */
  39. #define I2C_CTL_AA 0x04UL /*!< I2C_CTL setting for I2C control bits. It would set AA bit */
  40. /*---------------------------------------------------------------------------------------------------------*/
  41. /* I2C GCMode constant definitions. */
  42. /*---------------------------------------------------------------------------------------------------------*/
  43. #define I2C_GCMODE_ENABLE 1 /*!< Enable I2C GC Mode */
  44. #define I2C_GCMODE_DISABLE 0 /*!< Disable I2C GC Mode */
  45. /*---------------------------------------------------------------------------------------------------------*/
  46. /* I2C SMBUS constant definitions. */
  47. /*---------------------------------------------------------------------------------------------------------*/
  48. #define I2C_SMBH_ENABLE 1 /*!< Enable SMBus Host Mode enable */
  49. #define I2C_SMBD_ENABLE 0 /*!< Enable SMBus Device Mode enable */
  50. #define I2C_PECTX_ENABLE 1 /*!< Enable SMBus Packet Error Check Transmit function */
  51. #define I2C_PECTX_DISABLE 0 /*!< Disable SMBus Packet Error Check Transmit function */
  52. /*@}*/ /* end of group I2C_EXPORTED_CONSTANTS */
  53. /** @addtogroup I2C_EXPORTED_FUNCTIONS I2C Exported Functions
  54. @{
  55. */
  56. /**
  57. * @brief The macro is used to set I2C bus condition at One Time
  58. *
  59. * @param[in] i2c Specify I2C port
  60. * @param[in] u8Ctrl A byte writes to I2C control register
  61. *
  62. * @return None
  63. *
  64. * @details Set I2C_CTL register to control I2C bus conditions of START, STOP, SI, ACK.
  65. */
  66. #define I2C_SET_CONTROL_REG(i2c, u8Ctrl) ((i2c)->CTL = ((i2c)->CTL & ~0x3c) | (u8Ctrl))
  67. /**
  68. * @brief The macro is used to set START condition of I2C Bus
  69. *
  70. * @param[in] i2c Specify I2C port
  71. *
  72. * @return None
  73. *
  74. * @details Set the I2C bus START condition in I2C_CTL register.
  75. */
  76. #define I2C_START(i2c) ((i2c)->CTL = ((i2c)->CTL & ~I2C_CTL_SI_Msk) | I2C_CTL_STA_Msk)
  77. /**
  78. * @brief The macro is used to wait I2C bus status get ready
  79. *
  80. * @param[in] i2c Specify I2C port
  81. *
  82. * @return None
  83. *
  84. * @details When a new status is presented of I2C bus, the SI flag will be set in I2C_CTL register.
  85. */
  86. #define I2C_WAIT_READY(i2c) while(!((i2c)->CTL & I2C_CTL_SI_Msk))
  87. /**
  88. * @brief The macro is used to Read I2C Bus Data Register
  89. *
  90. * @param[in] i2c Specify I2C port
  91. *
  92. * @return A byte of I2C data register
  93. *
  94. * @details I2C controller read data from bus and save it in I2CDAT register.
  95. */
  96. #define I2C_GET_DATA(i2c) ((i2c)->DAT)
  97. /**
  98. * @brief Write a Data to I2C Data Register
  99. *
  100. * @param[in] i2c Specify I2C port
  101. * @param[in] u8Data A byte that writes to data register
  102. *
  103. * @return None
  104. *
  105. * @details When write a data to I2C_DAT register, the I2C controller will shift it to I2C bus.
  106. */
  107. #define I2C_SET_DATA(i2c, u8Data) ((i2c)->DAT = (u8Data))
  108. /**
  109. * @brief Get I2C Bus status code
  110. *
  111. * @param[in] i2c Specify I2C port
  112. *
  113. * @return I2C status code
  114. *
  115. * @details To get this status code to monitor I2C bus event.
  116. */
  117. #define I2C_GET_STATUS(i2c) ((i2c)->STATUS)
  118. /**
  119. * @brief Get Time-out flag from I2C Bus
  120. *
  121. * @param[in] i2c Specify I2C port
  122. *
  123. * @retval 0 I2C Bus time-out is not happened
  124. * @retval 1 I2C Bus time-out is happened
  125. *
  126. * @details When I2C bus occurs time-out event, the time-out flag will be set.
  127. */
  128. #define I2C_GET_TIMEOUT_FLAG(i2c) ( ((i2c)->TOCTL & I2C_TOCTL_TOIF_Msk) == I2C_TOCTL_TOIF_Msk ? 1:0 )
  129. /**
  130. * @brief To get wake-up flag from I2C Bus
  131. *
  132. * @param[in] i2c Specify I2C port
  133. *
  134. * @retval 0 Chip is not woken-up from power-down mode
  135. * @retval 1 Chip is woken-up from power-down mode
  136. *
  137. * @details I2C bus occurs wake-up event, wake-up flag will be set.
  138. */
  139. #define I2C_GET_WAKEUP_FLAG(i2c) ( ((i2c)->WKSTS & I2C_WKSTS_WKIF_Msk) == I2C_WKSTS_WKIF_Msk ? 1:0 )
  140. /**
  141. * @brief To clear wake-up flag
  142. *
  143. * @param[in] i2c Specify I2C port
  144. *
  145. * @return None
  146. *
  147. * @details If wake-up flag is set, use this macro to clear it.
  148. */
  149. #define I2C_CLEAR_WAKEUP_FLAG(i2c) ((i2c)->WKSTS = I2C_WKSTS_WKIF_Msk)
  150. /**
  151. * @brief To get SMBus Status
  152. *
  153. * @param[in] i2c Specify I2C port
  154. *
  155. * @return SMBus status
  156. *
  157. * @details To get the Bus Management status of I2C_BUSSTS register
  158. *
  159. */
  160. #define I2C_SMBUS_GET_STATUS(i2c) ((i2c)->BUSSTS)
  161. /**
  162. * @brief Get SMBus CRC value
  163. *
  164. * @param[in] i2c Specify I2C port
  165. *
  166. * @return Packet error check byte value
  167. *
  168. * @details The CRC check value after a transmission or a reception by count by using CRC8
  169. *
  170. */
  171. #define I2C_SMBUS_GET_PEC_VALUE(i2c) ((i2c)->PKTCRC)
  172. /**
  173. * @brief Set SMBus Bytes number of Transmission or reception
  174. *
  175. * @param[in] i2c Specify I2C port
  176. * @param[in] u32PktSize Transmit / Receive bytes
  177. *
  178. * @return None
  179. *
  180. * @details The transmission or receive byte number in one transaction when PECEN is set. The maximum is 255 bytes.
  181. *
  182. */
  183. #define I2C_SMBUS_SET_PACKET_BYTE_COUNT(i2c, u32PktSize) ((i2c)->PKTSIZE = (u32PktSize))
  184. /**
  185. * @brief Enable SMBus Alert function
  186. *
  187. * @param[in] i2c Specify I2C port
  188. *
  189. * @return None
  190. *
  191. * @details Device Mode(BMHEN=0): If ALERTEN(I2C_BUSCTL[4]) is set, the Alert pin will pull lo, and reply ACK when get ARP from host
  192. * Host Mode(BMHEN=1): If ALERTEN(I2C_BUSCTL[4]) is set, the Alert pin is supported to receive alert state(Lo trigger)
  193. *
  194. */
  195. #define I2C_SMBUS_ENABLE_ALERT(i2c) ((i2c)->BUSCTL |= I2C_BUSCTL_ALERTEN_Msk)
  196. /**
  197. * @brief Disable SMBus Alert pin function
  198. *
  199. * @param[in] i2c Specify I2C port
  200. *
  201. * @return None
  202. *
  203. * @details Device Mode(BMHEN=0): If ALERTEN(I2C_BUSCTL[4]) is clear, the Alert pin will pull hi, and reply NACK when get ARP from host
  204. * Host Mode(BMHEN=1): If ALERTEN(I2C_BUSCTL[4]) is clear, the Alert pin is not supported to receive alert state(Lo trigger)
  205. *
  206. */
  207. #define I2C_SMBUS_DISABLE_ALERT(i2c) ((i2c)->BUSCTL &= ~I2C_BUSCTL_ALERTEN_Msk)
  208. /**
  209. * @brief Set SMBus SUSCON pin is output mode
  210. *
  211. * @param[in] i2c Specify I2C port
  212. *
  213. * @return None
  214. *
  215. * @details This function to set SUSCON(I2C_BUSCTL[6]) pin is output mode.
  216. *
  217. *
  218. */
  219. #define I2C_SMBUS_SET_SUSCON_OUT(i2c) ((i2c)->BUSCTL |= I2C_BUSCTL_SCTLOEN_Msk)
  220. /**
  221. * @brief Set SMBus SUSCON pin is input mode
  222. *
  223. * @param[in] i2c Specify I2C port
  224. *
  225. * @return None
  226. *
  227. * @details This function to set SUSCON(I2C_BUSCTL[6]) pin is input mode.
  228. *
  229. *
  230. */
  231. #define I2C_SMBUS_SET_SUSCON_IN(i2c) ((i2c)->BUSCTL &= ~I2C_BUSCTL_SCTLOEN_Msk)
  232. /**
  233. * @brief Set SMBus SUSCON pin output high state
  234. *
  235. * @param[in] i2c Specify I2C port
  236. *
  237. * @return None
  238. *
  239. * @details This function to set SUSCON(I2C_BUSCTL[6]) pin is output hi state.
  240. *
  241. */
  242. #define I2C_SMBUS_SET_SUSCON_HIGH(i2c) ((i2c)->BUSCTL |= I2C_BUSCTL_SCTLOSTS_Msk)
  243. /**
  244. * @brief Set SMBus SUSCON pin output low state
  245. *
  246. * @param[in] i2c Specify I2C port
  247. *
  248. * @return None
  249. *
  250. * @details This function to set SUSCON(I2C_BUSCTL[6]) pin is output lo state.
  251. *
  252. */
  253. #define I2C_SMBUS_SET_SUSCON_LOW(i2c) ((i2c)->BUSCTL &= ~I2C_BUSCTL_SCTLOSTS_Msk)
  254. /**
  255. * @brief Enable SMBus Acknowledge control by manual
  256. *
  257. * @param[in] i2c Specify I2C port
  258. *
  259. * @return None
  260. *
  261. * @details The 9th bit can response the ACK or NACK according the received data by user. When the byte is received, SCLK line stretching to low between the 8th and 9th SCLK pulse.
  262. *
  263. */
  264. #define I2C_SMBUS_ACK_MANUAL(i2c) ((i2c)->BUSCTL |= I2C_BUSCTL_ACKMEN_Msk)
  265. /**
  266. * @brief Disable SMBus Acknowledge control by manual
  267. *
  268. * @param[in] i2c Specify I2C port
  269. *
  270. * @return None
  271. *
  272. * @details Disable acknowledge response control by user.
  273. *
  274. */
  275. #define I2C_SMBUS_ACK_AUTO(i2c) ((i2c)->BUSCTL &= ~I2C_BUSCTL_ACKMEN_Msk)
  276. /**
  277. * @brief Enable SMBus Acknowledge manual interrupt
  278. *
  279. * @param[in] i2c Specify I2C port
  280. *
  281. * @return None
  282. *
  283. * @details This function is used to enable SMBUS acknowledge manual interrupt on the 9th clock cycle when SMBUS=1 and ACKMEN=1
  284. *
  285. */
  286. #define I2C_SMBUS_9THBIT_INT_ENABLE(i2c) ((i2c)->BUSCTL |= I2C_BUSCTL_ACKM9SI_Msk)
  287. /**
  288. * @brief Disable SMBus Acknowledge manual interrupt
  289. *
  290. * @param[in] i2c Specify I2C port
  291. *
  292. * @return None
  293. *
  294. * @details This function is used to disable SMBUS acknowledge manual interrupt on the 9th clock cycle when SMBUS=1 and ACKMEN=1
  295. *
  296. */
  297. #define I2C_SMBUS_9THBIT_INT_DISABLE(i2c) ((i2c)->BUSCTL &= ~I2C_BUSCTL_ACKM9SI_Msk)
  298. /**
  299. * @brief Enable SMBus PEC clear at REPEAT START
  300. *
  301. * @param[in] i2c Specify I2C port
  302. *
  303. * @return None
  304. *
  305. * @details This function is used to enable the condition of REAEAT START can clear the PEC calculation.
  306. *
  307. */
  308. #define I2C_SMBUS_RST_PEC_AT_START_ENABLE(i2c) ((i2c)->BUSCTL |= I2C_BUSCTL_PECCLR_Msk)
  309. /**
  310. * @brief Disable SMBus PEC clear at Repeat START
  311. *
  312. * @param[in] i2c Specify I2C port
  313. *
  314. * @return None
  315. *
  316. * @details This function is used to disable the condition of Repeat START can clear the PEC calculation.
  317. *
  318. */
  319. #define I2C_SMBUS_RST_PEC_AT_START_DISABLE(i2c) ((i2c)->BUSCTL &= ~I2C_BUSCTL_PECCLR_Msk)
  320. /*---------------------------------------------------------------------------------------------------------*/
  321. /* inline functions */
  322. /*---------------------------------------------------------------------------------------------------------*/
  323. /**
  324. * @brief The macro is used to set STOP condition of I2C Bus
  325. *
  326. * @param[in] i2c Specify I2C port
  327. *
  328. * @return None
  329. *
  330. * @details Set the I2C bus STOP condition in I2C_CTL register.
  331. */
  332. static __INLINE void I2C_STOP(I2C_T *i2c)
  333. {
  334. (i2c)->CTL |= (I2C_CTL_SI_Msk | I2C_CTL_STO_Msk);
  335. while(i2c->CTL & I2C_CTL_STO_Msk);
  336. }
  337. void I2C_ClearTimeoutFlag(I2C_T *i2c);
  338. void I2C_Close(I2C_T *i2c);
  339. void I2C_Trigger(I2C_T *i2c, uint8_t u8Start, uint8_t u8Stop, uint8_t u8Si, uint8_t u8Ack);
  340. void I2C_DisableInt(I2C_T *i2c);
  341. void I2C_EnableInt(I2C_T *i2c);
  342. uint32_t I2C_GetBusClockFreq(I2C_T *i2c);
  343. uint32_t I2C_GetIntFlag(I2C_T *i2c);
  344. uint32_t I2C_GetStatus(I2C_T *i2c);
  345. uint32_t I2C_Open(I2C_T *i2c, uint32_t u32BusClock);
  346. uint8_t I2C_GetData(I2C_T *i2c);
  347. void I2C_SetSlaveAddr(I2C_T *i2c, uint8_t u8SlaveNo, uint8_t u8SlaveAddr, uint8_t u8GCMode);
  348. void I2C_SetSlaveAddrMask(I2C_T *i2c, uint8_t u8SlaveNo, uint8_t u8SlaveAddrMask);
  349. uint32_t I2C_SetBusClockFreq(I2C_T *i2c, uint32_t u32BusClock);
  350. void I2C_EnableTimeout(I2C_T *i2c, uint8_t u8LongTimeout);
  351. void I2C_DisableTimeout(I2C_T *i2c);
  352. void I2C_EnableWakeup(I2C_T *i2c);
  353. void I2C_DisableWakeup(I2C_T *i2c);
  354. void I2C_SetData(I2C_T *i2c, uint8_t u8Data);
  355. uint32_t I2C_SMBusGetStatus(I2C_T *i2c);
  356. void I2C_SMBusClearInterruptFlag(I2C_T *i2c, uint8_t u8ClrSMBusIntFlag);
  357. void I2C_SMBusSetPacketByteCount(I2C_T *i2c, uint32_t u32PktSize);
  358. void I2C_SMBusOpen(I2C_T *i2c, uint8_t u8HostDevice);
  359. void I2C_SMBusClose(I2C_T *i2c);
  360. void I2C_SMBusPECTxEnable(I2C_T *i2c, uint8_t u8PECTxEn);
  361. uint8_t I2C_SMBusGetPECValue(I2C_T *i2c);
  362. void I2C_SMBusIdleTimeout(I2C_T *i2c, uint32_t us, uint32_t u32Hclk);
  363. void I2C_SMBusTimeout(I2C_T *i2c, uint32_t ms, uint32_t u32Pclk);
  364. void I2C_SMBusClockLoTimeout(I2C_T *i2c, uint32_t ms, uint32_t u32Pclk);
  365. /*@}*/ /* end of group I2C_EXPORTED_FUNCTIONS */
  366. /*@}*/ /* end of group I2C_Driver */
  367. /*@}*/ /* end of group Standard_Driver */
  368. #ifdef __cplusplus
  369. }
  370. #endif
  371. #endif //__I2C_H__