1
0

nu_usci_i2c.h 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332
  1. /**************************************************************************//**
  2. * @file USCI_I2C.h
  3. * @version V3.0
  4. * @brief M480 series USCI I2C(UI2C) driver header file
  5. *
  6. * SPDX-License-Identifier: Apache-2.0
  7. * @copyright (C) 2016-2020 Nuvoton Technology Corp. All rights reserved.
  8. ******************************************************************************/
  9. #ifndef __NU_USCI_I2C_H__
  10. #define __NU_USCI_I2C_H__
  11. #ifdef __cplusplus
  12. extern "C"
  13. {
  14. #endif
  15. /** @addtogroup Standard_Driver Standard Driver
  16. @{
  17. */
  18. /** @addtogroup USCI_I2C_Driver USCI_I2C Driver
  19. @{
  20. */
  21. /** @addtogroup USCI_I2C_EXPORTED_CONSTANTS USCI_I2C Exported Constants
  22. @{
  23. */
  24. /*---------------------------------------------------------------------------------------------------------*/
  25. /* USCI_I2C master event definitions */
  26. /*---------------------------------------------------------------------------------------------------------*/
  27. enum UI2C_MASTER_EVENT
  28. {
  29. MASTER_SEND_ADDRESS = 10, /*!< Master send address to Slave */
  30. MASTER_SEND_H_WR_ADDRESS, /*!< Master send High address to Slave */
  31. MASTER_SEND_H_RD_ADDRESS, /*!< Master send address to Slave (Read ADDR) */
  32. MASTER_SEND_L_ADDRESS, /*!< Master send Low address to Slave */
  33. MASTER_SEND_DATA, /*!< Master Send Data to Slave */
  34. MASTER_SEND_REPEAT_START, /*!< Master send repeat start to Slave */
  35. MASTER_READ_DATA, /*!< Master Get Data from Slave */
  36. MASTER_STOP, /*!< Master send stop to Slave */
  37. MASTER_SEND_START /*!< Master send start to Slave */
  38. };
  39. /*---------------------------------------------------------------------------------------------------------*/
  40. /* USCI_I2C slave event definitions */
  41. /*---------------------------------------------------------------------------------------------------------*/
  42. enum UI2C_SLAVE_EVENT
  43. {
  44. SLAVE_ADDRESS_ACK = 100, /*!< Slave send address ACK */
  45. SLAVE_H_WR_ADDRESS_ACK, /*!< Slave send High address ACK */
  46. SLAVE_L_WR_ADDRESS_ACK, /*!< Slave send Low address ACK */
  47. SLAVE_GET_DATA, /*!< Slave Get Data from Master (Write CMD) */
  48. SLAVE_SEND_DATA, /*!< Slave Send Data to Master (Read CMD) */
  49. SLAVE_H_RD_ADDRESS_ACK, /*!< Slave send High address ACK */
  50. SLAVE_L_RD_ADDRESS_ACK /*!< Slave send Low address ACK */
  51. };
  52. /*---------------------------------------------------------------------------------------------------------*/
  53. /* USCI_CTL constant definitions. */
  54. /*---------------------------------------------------------------------------------------------------------*/
  55. #define UI2C_CTL_PTRG 0x20UL /*!< USCI_CTL setting for I2C control bits. It would set PTRG bit \hideinitializer */
  56. #define UI2C_CTL_STA 0x08UL /*!< USCI_CTL setting for I2C control bits. It would set STA bit \hideinitializer */
  57. #define UI2C_CTL_STO 0x04UL /*!< USCI_CTL setting for I2C control bits. It would set STO bit \hideinitializer */
  58. #define UI2C_CTL_AA 0x02UL /*!< USCI_CTL setting for I2C control bits. It would set AA bit \hideinitializer */
  59. /*---------------------------------------------------------------------------------------------------------*/
  60. /* USCI_I2C GCMode constant definitions. */
  61. /*---------------------------------------------------------------------------------------------------------*/
  62. #define UI2C_GCMODE_ENABLE (1U) /*!< Enable USCI_I2C GC Mode \hideinitializer */
  63. #define UI2C_GCMODE_DISABLE (0U) /*!< Disable USCI_I2C GC Mode \hideinitializer */
  64. /*---------------------------------------------------------------------------------------------------------*/
  65. /* USCI_I2C Wakeup Mode constant definitions. */
  66. /*---------------------------------------------------------------------------------------------------------*/
  67. #define UI2C_DATA_TOGGLE_WK (0x0U << UI2C_WKCTL_WKADDREN_Pos) /*!< Wakeup according data toggle \hideinitializer */
  68. #define UI2C_ADDR_MATCH_WK (0x1U << UI2C_WKCTL_WKADDREN_Pos) /*!< Wakeup according address match \hideinitializer */
  69. /*---------------------------------------------------------------------------------------------------------*/
  70. /* USCI_I2C interrupt mask definitions */
  71. /*---------------------------------------------------------------------------------------------------------*/
  72. #define UI2C_TO_INT_MASK (0x001U) /*!< Time-out interrupt mask \hideinitializer */
  73. #define UI2C_STAR_INT_MASK (0x002U) /*!< Start condition received interrupt mask \hideinitializer */
  74. #define UI2C_STOR_INT_MASK (0x004U) /*!< Stop condition received interrupt mask \hideinitializer */
  75. #define UI2C_NACK_INT_MASK (0x008U) /*!< Non-acknowledge interrupt mask \hideinitializer */
  76. #define UI2C_ARBLO_INT_MASK (0x010U) /*!< Arbitration lost interrupt mask \hideinitializer */
  77. #define UI2C_ERR_INT_MASK (0x020U) /*!< Error interrupt mask \hideinitializer */
  78. #define UI2C_ACK_INT_MASK (0x040U) /*!< Acknowledge interrupt mask \hideinitializer */
  79. /*@}*/ /* end of group USCI_I2C_EXPORTED_CONSTANTS */
  80. /** @addtogroup USCI_I2C_EXPORTED_FUNCTIONS USCI_I2C Exported Functions
  81. @{
  82. */
  83. /**
  84. * @brief This macro sets the USCI_I2C protocol control register at one time
  85. *
  86. * @param[in] ui2c The pointer of the specified USCI_I2C module.
  87. * @param[in] u8Ctrl Set the register value of USCI_I2C control register.
  88. *
  89. * @return None
  90. *
  91. * @details Set UI2C_PROTCTL register to control USCI_I2C bus conditions of START, STOP, SI, ACK.
  92. * \hideinitializer
  93. */
  94. #define UI2C_SET_CONTROL_REG(ui2c, u8Ctrl) ((ui2c)->PROTCTL = ((ui2c)->PROTCTL & ~0x2EU) | (u8Ctrl))
  95. /**
  96. * @brief This macro only set START bit to protocol control register of USCI_I2C module.
  97. *
  98. * @param[in] ui2c The pointer of the specified USCI_I2C module.
  99. *
  100. * @return None
  101. *
  102. * @details Set the USCI_I2C bus START condition in UI2C_PROTCTL register.
  103. * \hideinitializer
  104. */
  105. #define UI2C_START(ui2c) ((ui2c)->PROTCTL = ((ui2c)->PROTCTL & ~UI2C_PROTCTL_PTRG_Msk) | UI2C_PROTCTL_STA_Msk)
  106. /**
  107. * @brief This macro only set STOP bit to the control register of USCI_I2C module
  108. *
  109. * @param[in] ui2c The pointer of the specified USCI_I2C module.
  110. *
  111. * @return None
  112. *
  113. * @details Set the USCI_I2C bus STOP condition in UI2C_PROTCTL register.
  114. * \hideinitializer
  115. */
  116. #define UI2C_STOP(ui2c) ((ui2c)->PROTCTL = ((ui2c)->PROTCTL & ~0x2E) | (UI2C_PROTCTL_PTRG_Msk | UI2C_PROTCTL_STO_Msk))
  117. /**
  118. * @brief This macro returns the data stored in data register of USCI_I2C module
  119. *
  120. * @param[in] ui2c The pointer of the specified USCI_I2C module.
  121. *
  122. * @return Data
  123. *
  124. * @details Read a byte data value of UI2C_RXDAT register from USCI_I2C bus
  125. * \hideinitializer
  126. */
  127. #define UI2C_GET_DATA(ui2c) ((ui2c)->RXDAT)
  128. /**
  129. * @brief This macro writes the data to data register of USCI_I2C module
  130. *
  131. * @param[in] ui2c The pointer of the specified USCI_I2C module.
  132. * @param[in] u8Data The data which will be written to data register of USCI_I2C module.
  133. *
  134. * @return None
  135. *
  136. * @details Write a byte data value of UI2C_TXDAT register, then sends address or data to USCI I2C bus
  137. * \hideinitializer
  138. */
  139. #define UI2C_SET_DATA(ui2c, u8Data) ((ui2c)->TXDAT = (u8Data))
  140. /**
  141. * @brief This macro returns time-out flag
  142. *
  143. * @param[in] ui2c The pointer of the specified USCI_I2C module.
  144. *
  145. * @retval 0 USCI_I2C bus time-out is not happened
  146. * @retval 1 USCI_I2C bus time-out is happened
  147. *
  148. * @details USCI_I2C bus occurs time-out event, the time-out flag will be set. If not occurs time-out event, this bit is cleared.
  149. * \hideinitializer
  150. */
  151. #define UI2C_GET_TIMEOUT_FLAG(ui2c) (((ui2c)->PROTSTS & UI2C_PROTSTS_TOIF_Msk) == UI2C_PROTSTS_TOIF_Msk ? 1:0)
  152. /**
  153. * @brief This macro returns wake-up flag
  154. *
  155. * @param[in] ui2c The pointer of the specified USCI_I2C module.
  156. *
  157. * @retval 0 Chip is not woken-up from power-down mode
  158. * @retval 1 Chip is woken-up from power-down mode
  159. *
  160. * @details USCI_I2C controller wake-up flag will be set when USCI_I2C bus occurs wake-up from deep-sleep.
  161. * \hideinitializer
  162. */
  163. #define UI2C_GET_WAKEUP_FLAG(ui2c) (((ui2c)->WKSTS & UI2C_WKSTS_WKF_Msk) == UI2C_WKSTS_WKF_Msk ? 1:0)
  164. /**
  165. * @brief This macro is used to clear USCI_I2C wake-up flag
  166. *
  167. * @param[in] ui2c The pointer of the specified USCI_I2C module.
  168. *
  169. * @return None
  170. *
  171. * @details If USCI_I2C wake-up flag is set, use this macro to clear it.
  172. * \hideinitializer
  173. */
  174. #define UI2C_CLR_WAKEUP_FLAG(ui2c) ((ui2c)->WKSTS = UI2C_WKSTS_WKF_Msk)
  175. /**
  176. * @brief This macro disables the USCI_I2C 10-bit address mode
  177. *
  178. * @param[in] ui2c The pointer of the specified USCI_I2C module.
  179. *
  180. * @return None
  181. *
  182. * @details The UI2C_I2C is 7-bit address mode, when disable USCI_I2C 10-bit address match function.
  183. * \hideinitializer
  184. */
  185. #define UI2C_DISABLE_10BIT_ADDR_MODE(ui2c) ((ui2c)->PROTCTL &= ~(UI2C_PROTCTL_ADDR10EN_Msk))
  186. /**
  187. * @brief This macro enables the 10-bit address mode
  188. *
  189. * @param[in] ui2c The pointer of the specified USCI_I2C module.
  190. *
  191. * @return None
  192. *
  193. * @details To enable USCI_I2C 10-bit address match function.
  194. * \hideinitializer
  195. */
  196. #define UI2C_ENABLE_10BIT_ADDR_MODE(ui2c) ((ui2c)->PROTCTL |= UI2C_PROTCTL_ADDR10EN_Msk)
  197. /**
  198. * @brief This macro gets USCI_I2C protocol interrupt flag or bus status
  199. *
  200. * @param[in] ui2c The pointer of the specified USCI_I2C module.
  201. *
  202. * @return A word data of USCI_I2C_PROTSTS register
  203. *
  204. * @details Read a word data of USCI_I2C PROTSTS register to get USCI_I2C bus Interrupt flags or status.
  205. * \hideinitializer
  206. */
  207. #define UI2C_GET_PROT_STATUS(ui2c) ((ui2c)->PROTSTS)
  208. /**
  209. * @brief This macro clears specified protocol interrupt flag
  210. * @param[in] ui2c The pointer of the specified USCI_I2C module.
  211. * @param[in] u32IntTypeFlag Interrupt Type Flag, should be
  212. * - \ref UI2C_PROTSTS_ACKIF_Msk
  213. * - \ref UI2C_PROTSTS_ERRIF_Msk
  214. * - \ref UI2C_PROTSTS_ARBLOIF_Msk
  215. * - \ref UI2C_PROTSTS_NACKIF_Msk
  216. * - \ref UI2C_PROTSTS_STORIF_Msk
  217. * - \ref UI2C_PROTSTS_STARIF_Msk
  218. * - \ref UI2C_PROTSTS_TOIF_Msk
  219. * @return None
  220. *
  221. * @details To clear interrupt flag when USCI_I2C occurs interrupt and set interrupt flag.
  222. * \hideinitializer
  223. */
  224. #define UI2C_CLR_PROT_INT_FLAG(ui2c,u32IntTypeFlag) ((ui2c)->PROTSTS = (u32IntTypeFlag))
  225. /**
  226. * @brief This macro enables specified protocol interrupt
  227. * @param[in] ui2c The pointer of the specified USCI_I2C module.
  228. * @param[in] u32IntSel Interrupt Type, should be
  229. * - \ref UI2C_PROTIEN_ACKIEN_Msk
  230. * - \ref UI2C_PROTIEN_ERRIEN_Msk
  231. * - \ref UI2C_PROTIEN_ARBLOIEN_Msk
  232. * - \ref UI2C_PROTIEN_NACKIEN_Msk
  233. * - \ref UI2C_PROTIEN_STORIEN_Msk
  234. * - \ref UI2C_PROTIEN_STARIEN_Msk
  235. * - \ref UI2C_PROTIEN_TOIEN_Msk
  236. * @return None
  237. *
  238. * @details Set specified USCI_I2C protocol interrupt bits to enable interrupt function.
  239. * \hideinitializer
  240. */
  241. #define UI2C_ENABLE_PROT_INT(ui2c, u32IntSel) ((ui2c)->PROTIEN |= (u32IntSel))
  242. /**
  243. * @brief This macro disables specified protocol interrupt
  244. * @param[in] ui2c The pointer of the specified USCI_I2C module.
  245. * @param[in] u32IntSel Interrupt Type, should be
  246. * - \ref UI2C_PROTIEN_ACKIEN_Msk
  247. * - \ref UI2C_PROTIEN_ERRIEN_Msk
  248. * - \ref UI2C_PROTIEN_ARBLOIEN_Msk
  249. * - \ref UI2C_PROTIEN_NACKIEN_Msk
  250. * - \ref UI2C_PROTIEN_STORIEN_Msk
  251. * - \ref UI2C_PROTIEN_STARIEN_Msk
  252. * - \ref UI2C_PROTIEN_TOIEN_Msk
  253. * @return None
  254. *
  255. * @details Clear specified USCI_I2C protocol interrupt bits to disable interrupt function.
  256. * \hideinitializer
  257. */
  258. #define UI2C_DISABLE_PROT_INT(ui2c, u32IntSel) ((ui2c)->PROTIEN &= ~ (u32IntSel))
  259. uint32_t UI2C_Open(UI2C_T *ui2c, uint32_t u32BusClock);
  260. void UI2C_Close(UI2C_T *ui2c);
  261. void UI2C_ClearTimeoutFlag(UI2C_T *ui2c);
  262. void UI2C_Trigger(UI2C_T *ui2c, uint8_t u8Start, uint8_t u8Stop, uint8_t u8Ptrg, uint8_t u8Ack);
  263. void UI2C_DisableInt(UI2C_T *ui2c, uint32_t u32Mask);
  264. void UI2C_EnableInt(UI2C_T *ui2c, uint32_t u32Mask);
  265. uint32_t UI2C_GetBusClockFreq(UI2C_T *ui2c);
  266. uint32_t UI2C_SetBusClockFreq(UI2C_T *ui2c, uint32_t u32BusClock);
  267. uint32_t UI2C_GetIntFlag(UI2C_T *ui2c, uint32_t u32Mask);
  268. void UI2C_ClearIntFlag(UI2C_T* ui2c, uint32_t u32Mask);
  269. uint32_t UI2C_GetData(UI2C_T *ui2c);
  270. void UI2C_SetData(UI2C_T *ui2c, uint8_t u8Data);
  271. void UI2C_SetSlaveAddr(UI2C_T *ui2c, uint8_t u8SlaveNo, uint16_t u16SlaveAddr, uint8_t u8GCMode);
  272. void UI2C_SetSlaveAddrMask(UI2C_T *ui2c, uint8_t u8SlaveNo, uint16_t u16SlaveAddrMask);
  273. void UI2C_EnableTimeout(UI2C_T *ui2c, uint32_t u32TimeoutCnt);
  274. void UI2C_DisableTimeout(UI2C_T *ui2c);
  275. void UI2C_EnableWakeup(UI2C_T *ui2c, uint8_t u8WakeupMode);
  276. void UI2C_DisableWakeup(UI2C_T *ui2c);
  277. uint8_t UI2C_WriteByte(UI2C_T *ui2c, uint8_t u8SlaveAddr, uint8_t data);
  278. uint32_t UI2C_WriteMultiBytes(UI2C_T *ui2c, uint8_t u8SlaveAddr, uint8_t *data, uint32_t u32wLen);
  279. uint8_t UI2C_WriteByteOneReg(UI2C_T *ui2c, uint8_t u8SlaveAddr, uint8_t u8DataAddr, uint8_t data);
  280. uint32_t UI2C_WriteMultiBytesOneReg(UI2C_T *ui2c, uint8_t u8SlaveAddr, uint8_t u8DataAddr, uint8_t *data, uint32_t u32wLen);
  281. uint8_t UI2C_WriteByteTwoRegs(UI2C_T *ui2c, uint8_t u8SlaveAddr, uint16_t u16DataAddr, uint8_t data);
  282. uint32_t UI2C_WriteMultiBytesTwoRegs(UI2C_T *ui2c, uint8_t u8SlaveAddr, uint16_t u16DataAddr, uint8_t *data, uint32_t u32wLen);
  283. uint8_t UI2C_ReadByte(UI2C_T *ui2c, uint8_t u8SlaveAddr);
  284. uint32_t UI2C_ReadMultiBytes(UI2C_T *ui2c, uint8_t u8SlaveAddr, uint8_t *rdata, uint32_t u32rLen);
  285. uint8_t UI2C_ReadByteOneReg(UI2C_T *ui2c, uint8_t u8SlaveAddr, uint8_t u8DataAddr);
  286. uint32_t UI2C_ReadMultiBytesOneReg(UI2C_T *ui2c, uint8_t u8SlaveAddr, uint8_t u8DataAddr, uint8_t *rdata, uint32_t u32rLen);
  287. uint8_t UI2C_ReadByteTwoRegs(UI2C_T *ui2c, uint8_t u8SlaveAddr, uint16_t u16DataAddr);
  288. uint32_t UI2C_ReadMultiBytesTwoRegs(UI2C_T *ui2c, uint8_t u8SlaveAddr, uint16_t u16DataAddr, uint8_t *rdata, uint32_t u32rLen);
  289. /*@}*/ /* end of group USCI_I2C_EXPORTED_FUNCTIONS */
  290. /*@}*/ /* end of group USCI_I2C_Driver */
  291. /*@}*/ /* end of group Standard_Driver */
  292. #ifdef __cplusplus
  293. }
  294. #endif
  295. #endif
  296. /*** (C) COPYRIGHT 2016 Nuvoton Technology Corp. ***/