nu_i2c.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296
  1. /****************************************************************************//**
  2. * @file nu_i2c.h
  3. * @brief I2C driver header file
  4. *
  5. * SPDX-License-Identifier: Apache-2.0
  6. * @copyright (C) 2020 Nuvoton Technology Corp. All rights reserved.
  7. *****************************************************************************/
  8. #ifndef __NU_I2C_H__
  9. #define __NU_I2C_H__
  10. #ifdef __cplusplus
  11. extern "C"
  12. {
  13. #endif
  14. /** @addtogroup Standard_Driver Standard Driver
  15. @{
  16. */
  17. /** @addtogroup I2C_Driver I2C Driver
  18. @{
  19. */
  20. /** @addtogroup I2C_EXPORTED_CONSTANTS I2C Exported Constants
  21. @{
  22. */
  23. /*---------------------------------------------------------------------------------------------------------*/
  24. /* I2C_CTL constant definitions. */
  25. /*---------------------------------------------------------------------------------------------------------*/
  26. #define I2C_CTL_STA_SI 0x28UL /*!< I2C_CTL setting for I2C control bits. It would set STA and SI bits \hideinitializer */
  27. #define I2C_CTL_STA_SI_AA 0x2CUL /*!< I2C_CTL setting for I2C control bits. It would set STA, SI and AA bits \hideinitializer */
  28. #define I2C_CTL_STO_SI 0x18UL /*!< I2C_CTL setting for I2C control bits. It would set STO and SI bits \hideinitializer */
  29. #define I2C_CTL_STO_SI_AA 0x1CUL /*!< I2C_CTL setting for I2C control bits. It would set STO, SI and AA bits \hideinitializer */
  30. #define I2C_CTL_SI 0x08UL /*!< I2C_CTL setting for I2C control bits. It would set SI bit \hideinitializer */
  31. #define I2C_CTL_SI_AA 0x0CUL /*!< I2C_CTL setting for I2C control bits. It would set SI and AA bits \hideinitializer */
  32. #define I2C_CTL_STA 0x20UL /*!< I2C_CTL setting for I2C control bits. It would set STA bit \hideinitializer */
  33. #define I2C_CTL_STO 0x10UL /*!< I2C_CTL setting for I2C control bits. It would set STO bit \hideinitializer */
  34. #define I2C_CTL_AA 0x04UL /*!< I2C_CTL setting for I2C control bits. It would set AA bit \hideinitializer */
  35. /*---------------------------------------------------------------------------------------------------------*/
  36. /* I2C GCMode constant definitions. */
  37. /*---------------------------------------------------------------------------------------------------------*/
  38. #define I2C_GCMODE_ENABLE 1 /*!< Enable I2C GC Mode \hideinitializer */
  39. #define I2C_GCMODE_DISABLE 0 /*!< Disable I2C GC Mode \hideinitializer */
  40. /*@}*/ /* end of group I2C_EXPORTED_CONSTANTS */
  41. /** @addtogroup I2C_EXPORTED_FUNCTIONS I2C Exported Functions
  42. @{
  43. */
  44. /**
  45. * @brief The macro is used to set I2C bus condition at One Time
  46. *
  47. * @param[in] i2c Specify I2C port
  48. * @param[in] u8Ctrl A byte writes to I2C control register
  49. *
  50. * @return None
  51. *
  52. * @details Set I2C_CTL register to control I2C bus conditions of START, STOP, SI, ACK.
  53. * \hideinitializer
  54. */
  55. #define I2C_SET_CONTROL_REG(i2c, u8Ctrl) ((i2c)->CTL0 = ((i2c)->CTL0 & ~0x3c) | (u8Ctrl))
  56. /**
  57. * @brief The macro is used to set START condition of I2C Bus
  58. *
  59. * @param[in] i2c Specify I2C port
  60. *
  61. * @return None
  62. *
  63. * @details Set the I2C bus START condition in I2C_CTL register.
  64. * \hideinitializer
  65. */
  66. #define I2C_START(i2c) ((i2c)->CTL0 = ((i2c)->CTL0 & ~I2C_CTL0_SI_Msk) | I2C_CTL0_STA_Msk)
  67. /**
  68. * @brief The macro is used to wait I2C bus status get ready
  69. *
  70. * @param[in] i2c Specify I2C port
  71. *
  72. * @return None
  73. *
  74. * @details When a new status is presented of I2C bus, the SI flag will be set in I2C_CTL register.
  75. * \hideinitializer
  76. */
  77. #define I2C_WAIT_READY(i2c) while(!((i2c)->CTL0 & I2C_CTL0_SI_Msk))
  78. /**
  79. * @brief The macro is used to Read I2C Bus Data Register
  80. *
  81. * @param[in] i2c Specify I2C port
  82. *
  83. * @return A byte of I2C data register
  84. *
  85. * @details I2C controller read data from bus and save it in I2CDAT register.
  86. * \hideinitializer
  87. */
  88. #define I2C_GET_DATA(i2c) ((i2c)->DAT)
  89. /**
  90. * @brief Write a Data to I2C Data Register
  91. *
  92. * @param[in] i2c Specify I2C port
  93. * @param[in] u8Data A byte that writes to data register
  94. *
  95. * @return None
  96. *
  97. * @details When write a data to I2C_DAT register, the I2C controller will shift it to I2C bus.
  98. * \hideinitializer
  99. */
  100. #define I2C_SET_DATA(i2c, u8Data) ((i2c)->DAT = (u8Data))
  101. /**
  102. * @brief Get I2C Bus status code
  103. *
  104. * @param[in] i2c Specify I2C port
  105. *
  106. * @return I2C status code
  107. *
  108. * @details To get this status code to monitor I2C bus event.
  109. * \hideinitializer
  110. */
  111. #define I2C_GET_STATUS(i2c) ((i2c)->STATUS0)
  112. /**
  113. * @brief Get Time-out flag from I2C Bus
  114. *
  115. * @param[in] i2c Specify I2C port
  116. *
  117. * @retval 0 I2C Bus time-out is not happened
  118. * @retval 1 I2C Bus time-out is happened
  119. *
  120. * @details When I2C bus occurs time-out event, the time-out flag will be set.
  121. * \hideinitializer
  122. */
  123. #define I2C_GET_TIMEOUT_FLAG(i2c) ( ((i2c)->TOCTL & I2C_TOCTL_TOIF_Msk) == I2C_TOCTL_TOIF_Msk ? 1:0 )
  124. /**
  125. * @brief To get wake-up flag from I2C Bus
  126. *
  127. * @param[in] i2c Specify I2C port
  128. *
  129. * @retval 0 Chip is not woken-up from power-down mode
  130. * @retval 1 Chip is woken-up from power-down mode
  131. *
  132. * @details I2C bus occurs wake-up event, wake-up flag will be set.
  133. * \hideinitializer
  134. */
  135. #define I2C_GET_WAKEUP_FLAG(i2c) ( ((i2c)->WKSTS & I2C_WKSTS_WKIF_Msk) == I2C_WKSTS_WKIF_Msk ? 1:0 )
  136. /**
  137. * @brief To clear wake-up flag
  138. *
  139. * @param[in] i2c Specify I2C port
  140. *
  141. * @return None
  142. *
  143. * @details If wake-up flag is set, use this macro to clear it.
  144. * \hideinitializer
  145. */
  146. #define I2C_CLEAR_WAKEUP_FLAG(i2c) ((i2c)->WKSTS = I2C_WKSTS_WKIF_Msk)
  147. /**
  148. * @brief Enable RX PDMA function.
  149. * @param[in] i2c The pointer of the specified I2C module.
  150. * @return None.
  151. * @details Set RXPDMAEN bit of I2C_CTL1 register to enable RX PDMA transfer function.
  152. * \hideinitializer
  153. */
  154. #define I2C_ENABLE_RX_PDMA(i2c) ((i2c)->CTL1 |= I2C_CTL1_RXPDMAEN_Msk)
  155. /**
  156. * @brief Enable TX PDMA function.
  157. * @param[in] i2c The pointer of the specified I2C module.
  158. * @return None.
  159. * @details Set TXPDMAEN bit of I2C_CTL1 register to enable TX PDMA transfer function.
  160. * \hideinitializer
  161. */
  162. #define I2C_ENABLE_TX_PDMA(i2c) ((i2c)->CTL1 |= I2C_CTL1_TXPDMAEN_Msk)
  163. /**
  164. * @brief Disable RX PDMA transfer.
  165. * @param[in] i2c The pointer of the specified I2C module.
  166. * @return None.
  167. * @details Clear RXPDMAEN bit of I2C_CTL1 register to disable RX PDMA transfer function.
  168. * \hideinitializer
  169. */
  170. #define I2C_DISABLE_RX_PDMA(i2c) ((i2c)->CTL1 &= ~I2C_CTL1_RXPDMAEN_Msk)
  171. /**
  172. * @brief Disable TX PDMA transfer.
  173. * @param[in] i2c The pointer of the specified I2C module.
  174. * @return None.
  175. * @details Clear TXPDMAEN bit of I2C_CTL1 register to disable TX PDMA transfer function.
  176. * \hideinitializer
  177. */
  178. #define I2C_DISABLE_TX_PDMA(i2c) ((i2c)->CTL1 &= ~I2C_CTL1_TXPDMAEN_Msk)
  179. /**
  180. * @brief Enable PDMA stretch function.
  181. * @param[in] i2c The pointer of the specified I2C module.
  182. * @return None.
  183. * @details Enable this function is to stretch bus by hardware after PDMA transfer is done if SI is not cleared.
  184. * \hideinitializer
  185. */
  186. #define I2C_ENABLE_PDMA_STRETCH(i2c) ((i2c)->CTL1 |= I2C_CTL1_PDMASTR_Msk)
  187. /**
  188. * @brief Disable PDMA stretch function.
  189. * @param[in] i2c The pointer of the specified I2C module.
  190. * @return None.
  191. * @details I2C will send STOP after PDMA transfers done automatically.
  192. * \hideinitializer
  193. */
  194. #define I2C_DISABLE_PDMA_STRETCH(i2c) ((i2c)->CTL1 &= ~I2C_CTL1_PDMASTR_Msk)
  195. /**
  196. * @brief Reset PDMA function.
  197. * @param[in] i2c The pointer of the specified I2C module.
  198. * @return None.
  199. * @details I2C PDMA engine will be reset after this function is called.
  200. * \hideinitializer
  201. */
  202. #define I2C_DISABLE_RST_PDMA(i2c) ((i2c)->CTL1 |= I2C_CTL1_PDMARST_Msk)
  203. /*---------------------------------------------------------------------------------------------------------*/
  204. /* inline functions */
  205. /*---------------------------------------------------------------------------------------------------------*/
  206. /* Declare these inline functions here to avoid MISRA C 2004 rule 8.1 error */
  207. __STATIC_INLINE void I2C_STOP(I2C_T *i2c);
  208. /**
  209. * @brief The macro is used to set STOP condition of I2C Bus
  210. *
  211. * @param[in] i2c Specify I2C port
  212. *
  213. * @return None
  214. *
  215. * @details Set the I2C bus STOP condition in I2C_CTL register.
  216. */
  217. __STATIC_INLINE void I2C_STOP(I2C_T *i2c)
  218. {
  219. (i2c)->CTL0 |= (I2C_CTL0_SI_Msk | I2C_CTL0_STO_Msk);
  220. while (i2c->CTL0 & I2C_CTL0_STO_Msk)
  221. {
  222. }
  223. }
  224. void I2C_ClearTimeoutFlag(I2C_T *i2c);
  225. void I2C_Close(I2C_T *i2c);
  226. void I2C_Trigger(I2C_T *i2c, uint8_t u8Start, uint8_t u8Stop, uint8_t u8Si, uint8_t u8Ack);
  227. void I2C_DisableInt(I2C_T *i2c);
  228. void I2C_EnableInt(I2C_T *i2c);
  229. uint32_t I2C_GetBusClockFreq(I2C_T *i2c);
  230. uint32_t I2C_GetIntFlag(I2C_T *i2c);
  231. uint32_t I2C_GetStatus(I2C_T *i2c);
  232. uint32_t I2C_Open(I2C_T *i2c, uint32_t u32BusClock);
  233. uint8_t I2C_GetData(I2C_T *i2c);
  234. void I2C_SetSlaveAddr(I2C_T *i2c, uint8_t u8SlaveNo, uint8_t u8SlaveAddr, uint8_t u8GCMode);
  235. void I2C_SetSlaveAddrMask(I2C_T *i2c, uint8_t u8SlaveNo, uint8_t u8SlaveAddrMask);
  236. uint32_t I2C_SetBusClockFreq(I2C_T *i2c, uint32_t u32BusClock);
  237. void I2C_EnableTimeout(I2C_T *i2c, uint8_t u8LongTimeout);
  238. void I2C_DisableTimeout(I2C_T *i2c);
  239. void I2C_EnableWakeup(I2C_T *i2c);
  240. void I2C_DisableWakeup(I2C_T *i2c);
  241. void I2C_SetData(I2C_T *i2c, uint8_t u8Data);
  242. uint8_t I2C_WriteByte(I2C_T *i2c, uint8_t u8SlaveAddr, uint8_t data);
  243. uint32_t I2C_WriteMultiBytes(I2C_T *i2c, uint8_t u8SlaveAddr, uint8_t data[], uint32_t u32wLen);
  244. uint8_t I2C_WriteByteOneReg(I2C_T *i2c, uint8_t u8SlaveAddr, uint8_t u8DataAddr, uint8_t data);
  245. uint32_t I2C_WriteMultiBytesOneReg(I2C_T *i2c, uint8_t u8SlaveAddr, uint8_t u8DataAddr, uint8_t data[], uint32_t u32wLen);
  246. uint8_t I2C_WriteByteTwoRegs(I2C_T *i2c, uint8_t u8SlaveAddr, uint16_t u16DataAddr, uint8_t data);
  247. uint32_t I2C_WriteMultiBytesTwoRegs(I2C_T *i2c, uint8_t u8SlaveAddr, uint16_t u16DataAddr, uint8_t data[], uint32_t u32wLen);
  248. uint8_t I2C_ReadByte(I2C_T *i2c, uint8_t u8SlaveAddr);
  249. uint32_t I2C_ReadMultiBytes(I2C_T *i2c, uint8_t u8SlaveAddr, uint8_t rdata[], uint32_t u32rLen);
  250. uint8_t I2C_ReadByteOneReg(I2C_T *i2c, uint8_t u8SlaveAddr, uint8_t u8DataAddr);
  251. uint32_t I2C_ReadMultiBytesOneReg(I2C_T *i2c, uint8_t u8SlaveAddr, uint8_t u8DataAddr, uint8_t rdata[], uint32_t u32rLen);
  252. uint8_t I2C_ReadByteTwoRegs(I2C_T *i2c, uint8_t u8SlaveAddr, uint16_t u16DataAddr);
  253. uint32_t I2C_ReadMultiBytesTwoRegs(I2C_T *i2c, uint8_t u8SlaveAddr, uint16_t u16DataAddr, uint8_t rdata[], uint32_t u32rLen);
  254. /*@}*/ /* end of group I2C_EXPORTED_FUNCTIONS */
  255. /*@}*/ /* end of group I2C_Driver */
  256. /*@}*/ /* end of group Standard_Driver */
  257. #ifdef __cplusplus
  258. }
  259. #endif
  260. #endif