ch32v00X_i2c.h 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415
  1. /********************************** (C) COPYRIGHT *******************************
  2. * File Name : ch32v00X_i2c.h
  3. * Author : WCH
  4. * Version : V1.0.0
  5. * Date : 2024/01/01
  6. * Description : This file contains all the functions prototypes for the
  7. * I2C firmware library.
  8. *********************************************************************************
  9. * Copyright (c) 2021 Nanjing Qinheng Microelectronics Co., Ltd.
  10. * Attention: This software (modified or not) and binary are used for
  11. * microcontroller manufactured by Nanjing Qinheng Microelectronics.
  12. *******************************************************************************/
  13. #ifndef __CH32V00X_I2C_H
  14. #define __CH32V00X_I2C_H
  15. #ifdef __cplusplus
  16. extern "C" {
  17. #endif
  18. #include <ch32v00X.h>
  19. /* I2C Init structure definition */
  20. typedef struct
  21. {
  22. uint32_t I2C_ClockSpeed; /* Specifies the clock frequency.
  23. This parameter must be set to a value lower than 400kHz */
  24. uint16_t I2C_Mode; /* Specifies the I2C mode.
  25. This parameter can be a value of @ref I2C_mode */
  26. uint16_t I2C_DutyCycle; /* Specifies the I2C fast mode duty cycle.
  27. This parameter can be a value of @ref I2C_duty_cycle_in_fast_mode */
  28. uint16_t I2C_OwnAddress1; /* Specifies the first device own address.
  29. This parameter can be a 7-bit or 10-bit address. */
  30. uint16_t I2C_Ack; /* Enables or disables the acknowledgement.
  31. This parameter can be a value of @ref I2C_acknowledgement */
  32. uint16_t I2C_AcknowledgedAddress; /* Specifies if 7-bit or 10-bit address is acknowledged.
  33. This parameter can be a value of @ref I2C_acknowledged_address */
  34. } I2C_InitTypeDef;
  35. /* I2C_mode */
  36. #define I2C_Mode_I2C ((uint16_t)0x0000)
  37. /* I2C_duty_cycle_in_fast_mode */
  38. #define I2C_DutyCycle_16_9 ((uint16_t)0x4000) /* I2C fast mode Tlow/Thigh = 16/9 */
  39. #define I2C_DutyCycle_2 ((uint16_t)0xBFFF) /* I2C fast mode Tlow/Thigh = 2 */
  40. /* I2C_acknowledgement */
  41. #define I2C_Ack_Enable ((uint16_t)0x0400)
  42. #define I2C_Ack_Disable ((uint16_t)0x0000)
  43. /* I2C_transfer_direction */
  44. #define I2C_Direction_Transmitter ((uint8_t)0x00)
  45. #define I2C_Direction_Receiver ((uint8_t)0x01)
  46. /* I2C_acknowledged_address */
  47. #define I2C_AcknowledgedAddress_7bit ((uint16_t)0x4000)
  48. #define I2C_AcknowledgedAddress_10bit ((uint16_t)0xC000)
  49. /* I2C_registers */
  50. #define I2C_Register_CTLR1 ((uint8_t)0x00)
  51. #define I2C_Register_CTLR2 ((uint8_t)0x04)
  52. #define I2C_Register_OADDR1 ((uint8_t)0x08)
  53. #define I2C_Register_OADDR2 ((uint8_t)0x0C)
  54. #define I2C_Register_DATAR ((uint8_t)0x10)
  55. #define I2C_Register_STAR1 ((uint8_t)0x14)
  56. #define I2C_Register_STAR2 ((uint8_t)0x18)
  57. #define I2C_Register_CKCFGR ((uint8_t)0x1C)
  58. /* I2C_PEC_position */
  59. #define I2C_PECPosition_Next ((uint16_t)0x0800)
  60. #define I2C_PECPosition_Current ((uint16_t)0xF7FF)
  61. /* I2C_NACK_position */
  62. #define I2C_NACKPosition_Next ((uint16_t)0x0800)
  63. #define I2C_NACKPosition_Current ((uint16_t)0xF7FF)
  64. /* I2C_interrupts_definition */
  65. #define I2C_IT_BUF ((uint16_t)0x0400)
  66. #define I2C_IT_EVT ((uint16_t)0x0200)
  67. #define I2C_IT_ERR ((uint16_t)0x0100)
  68. /* I2C_interrupts_definition */
  69. #define I2C_IT_PECERR ((uint32_t)0x01001000)
  70. #define I2C_IT_OVR ((uint32_t)0x01000800)
  71. #define I2C_IT_AF ((uint32_t)0x01000400)
  72. #define I2C_IT_ARLO ((uint32_t)0x01000200)
  73. #define I2C_IT_BERR ((uint32_t)0x01000100)
  74. #define I2C_IT_TXE ((uint32_t)0x06000080)
  75. #define I2C_IT_RXNE ((uint32_t)0x06000040)
  76. #define I2C_IT_STOPF ((uint32_t)0x02000010)
  77. #define I2C_IT_ADD10 ((uint32_t)0x02000008)
  78. #define I2C_IT_BTF ((uint32_t)0x02000004)
  79. #define I2C_IT_ADDR ((uint32_t)0x02000002)
  80. #define I2C_IT_SB ((uint32_t)0x02000001)
  81. /* STAR2 register flags */
  82. #define I2C_FLAG_DUALF ((uint32_t)0x00800000)
  83. #define I2C_FLAG_GENCALL ((uint32_t)0x00100000)
  84. #define I2C_FLAG_TRA ((uint32_t)0x00040000)
  85. #define I2C_FLAG_BUSY ((uint32_t)0x00020000)
  86. #define I2C_FLAG_MSL ((uint32_t)0x00010000)
  87. /* STAR1 register flags */
  88. #define I2C_FLAG_PECERR ((uint32_t)0x10001000)
  89. #define I2C_FLAG_OVR ((uint32_t)0x10000800)
  90. #define I2C_FLAG_AF ((uint32_t)0x10000400)
  91. #define I2C_FLAG_ARLO ((uint32_t)0x10000200)
  92. #define I2C_FLAG_BERR ((uint32_t)0x10000100)
  93. #define I2C_FLAG_TXE ((uint32_t)0x10000080)
  94. #define I2C_FLAG_RXNE ((uint32_t)0x10000040)
  95. #define I2C_FLAG_STOPF ((uint32_t)0x10000010)
  96. #define I2C_FLAG_ADD10 ((uint32_t)0x10000008)
  97. #define I2C_FLAG_BTF ((uint32_t)0x10000004)
  98. #define I2C_FLAG_ADDR ((uint32_t)0x10000002)
  99. #define I2C_FLAG_SB ((uint32_t)0x10000001)
  100. /****************I2C Master Events (Events grouped in order of communication)********************/
  101. /********************************************************************************************************************
  102. * @brief Start communicate
  103. *
  104. * After master use I2C_GenerateSTART() function sending the START condition,the master
  105. * has to wait for event 5(the Start condition has been correctly
  106. * released on the I2C bus ).
  107. *
  108. */
  109. /* EVT5 */
  110. #define I2C_EVENT_MASTER_MODE_SELECT ((uint32_t)0x00030001) /* BUSY, MSL and SB flag */
  111. /********************************************************************************************************************
  112. * @brief Address Acknowledge
  113. *
  114. * When start condition correctly released on the bus(check EVT5), the
  115. * master use I2C_Send7bitAddress() function sends the address of the slave(s) with which it will communicate
  116. * it also determines master as transmitter or Receiver. Then the master has to wait that a slave acknowledges
  117. * his address. If an acknowledge is sent on the bus, one of the following events will be set:
  118. *
  119. *
  120. *
  121. * 1) In case of Master Receiver (7-bit addressing): the I2C_EVENT_MASTER_RECEIVER_MODE_SELECTED
  122. * event is set.
  123. *
  124. * 2) In case of Master Transmitter (7-bit addressing): the I2C_EVENT_MASTER_TRANSMITTER_MODE_SELECTED
  125. * is set
  126. *
  127. * 3) In case of 10-Bit addressing mode, the master (after generating the START
  128. * and checking on EVT5) use I2C_SendData() function send the header of 10-bit addressing mode.
  129. * Then master wait EVT9. EVT9 means that the 10-bit addressing header has been correctly sent
  130. * on the bus. Then master should use the function I2C_Send7bitAddress() to send the second part
  131. * of the 10-bit address (LSB) . Then master should wait for event 6.
  132. *
  133. *
  134. */
  135. /* EVT6 */
  136. #define I2C_EVENT_MASTER_TRANSMITTER_MODE_SELECTED ((uint32_t)0x00070082) /* BUSY, MSL, ADDR, TXE and TRA flags */
  137. #define I2C_EVENT_MASTER_RECEIVER_MODE_SELECTED ((uint32_t)0x00030002) /* BUSY, MSL and ADDR flags */
  138. /* EVT9 */
  139. #define I2C_EVENT_MASTER_MODE_ADDRESS10 ((uint32_t)0x00030008) /* BUSY, MSL and ADD10 flags */
  140. /********************************************************************************************************************
  141. * @brief Communication events
  142. *
  143. * If START condition has generated and slave address
  144. * been acknowledged. then the master has to check one of the following events for
  145. * communication procedures:
  146. *
  147. * 1) Master Receiver mode: The master has to wait on the event EVT7 then use
  148. * I2C_ReceiveData() function to read the data received from the slave .
  149. *
  150. * 2) Master Transmitter mode: The master use I2C_SendData() function to send data
  151. * then to wait on event EVT8 or EVT8_2.
  152. * These two events are similar:
  153. * - EVT8 means that the data has been written in the data register and is
  154. * being shifted out.
  155. * - EVT8_2 means that the data has been physically shifted out and output
  156. * on the bus.
  157. * In most cases, using EVT8 is sufficient for the application.
  158. * Using EVT8_2 will leads to a slower communication speed but will more reliable .
  159. * EVT8_2 is also more suitable than EVT8 for testing on the last data transmission
  160. *
  161. *
  162. * Note:
  163. * In case the user software does not guarantee that this event EVT7 is managed before
  164. * the current byte end of transfer, then user may check on I2C_EVENT_MASTER_BYTE_RECEIVED
  165. * and I2C_FLAG_BTF flag at the same time .But in this case the communication may be slower.
  166. *
  167. *
  168. */
  169. /* Master Receive mode */
  170. /* EVT7 */
  171. #define I2C_EVENT_MASTER_BYTE_RECEIVED ((uint32_t)0x00030040) /* BUSY, MSL and RXNE flags */
  172. /* Master Transmitter mode*/
  173. /* EVT8 */
  174. #define I2C_EVENT_MASTER_BYTE_TRANSMITTING ((uint32_t)0x00070080) /* TRA, BUSY, MSL, TXE flags */
  175. /* EVT8_2 */
  176. #define I2C_EVENT_MASTER_BYTE_TRANSMITTED ((uint32_t)0x00070084) /* TRA, BUSY, MSL, TXE and BTF flags */
  177. /******************I2C Slave Events (Events grouped in order of communication)******************/
  178. /********************************************************************************************************************
  179. * @brief Start Communicate events
  180. *
  181. * Wait on one of these events at the start of the communication. It means that
  182. * the I2C peripheral detected a start condition of master device generate on the bus.
  183. * If the acknowledge feature is enabled through function I2C_AcknowledgeConfig()),The peripheral generates an ACK condition on the bus.
  184. *
  185. *
  186. *
  187. * a) In normal case (only one address managed by the slave), when the address
  188. * sent by the master matches the own address of the peripheral (configured by
  189. * I2C_OwnAddress1 field) the I2C_EVENT_SLAVE_XXX_ADDRESS_MATCHED event is set
  190. * (where XXX could be TRANSMITTER or RECEIVER).
  191. *
  192. * b) In case the address sent by the master matches the second address of the
  193. * peripheral (configured by the function I2C_OwnAddress2Config() and enabled
  194. * by the function I2C_DualAddressCmd()) the events I2C_EVENT_SLAVE_XXX_SECONDADDRESS_MATCHED
  195. * (where XXX could be TRANSMITTER or RECEIVER) are set.
  196. *
  197. * c) In case the address sent by the master is General Call (address 0x00) and
  198. * if the General Call is enabled for the peripheral (using function I2C_GeneralCallCmd())
  199. * the following event is set I2C_EVENT_SLAVE_GENERALCALLADDRESS_MATCHED.
  200. *
  201. */
  202. /* EVT1 */
  203. /* a) Case of One Single Address managed by the slave */
  204. #define I2C_EVENT_SLAVE_RECEIVER_ADDRESS_MATCHED ((uint32_t)0x00020002) /* BUSY and ADDR flags */
  205. #define I2C_EVENT_SLAVE_TRANSMITTER_ADDRESS_MATCHED ((uint32_t)0x00060082) /* TRA, BUSY, TXE and ADDR flags */
  206. /* b) Case of Dual address managed by the slave */
  207. #define I2C_EVENT_SLAVE_RECEIVER_SECONDADDRESS_MATCHED ((uint32_t)0x00820000) /* DUALF and BUSY flags */
  208. #define I2C_EVENT_SLAVE_TRANSMITTER_SECONDADDRESS_MATCHED ((uint32_t)0x00860080) /* DUALF, TRA, BUSY and TXE flags */
  209. /* c) Case of General Call enabled for the slave */
  210. #define I2C_EVENT_SLAVE_GENERALCALLADDRESS_MATCHED ((uint32_t)0x00120000) /* GENCALL and BUSY flags */
  211. /********************************************************************************************************************
  212. * @brief Communication events
  213. *
  214. * Wait on one of these events when EVT1 has already been checked :
  215. *
  216. * - Slave Receiver mode:
  217. * - EVT2--The device is expecting to receive a data byte .
  218. * - EVT4--The device is expecting the end of the communication: master
  219. * sends a stop condition and data transmission is stopped.
  220. *
  221. * - Slave Transmitter mode:
  222. * - EVT3--When a byte has been transmitted by the slave and the Master is expecting
  223. * the end of the byte transmission. The two events I2C_EVENT_SLAVE_BYTE_TRANSMITTED and
  224. * I2C_EVENT_SLAVE_BYTE_TRANSMITTING are similar. If the user software doesn't guarantee
  225. * the EVT3 is managed before the current byte end of transfer The second one can optionally
  226. * be used.
  227. * - EVT3_2--When the master sends a NACK to tell slave device that data transmission
  228. * shall end . The slave device has to stop sending
  229. * data bytes and wait a Stop condition from bus.
  230. *
  231. * Note:
  232. * If the user software does not guarantee that the event 2 is
  233. * managed before the current byte end of transfer, User may check on I2C_EVENT_SLAVE_BYTE_RECEIVED
  234. * and I2C_FLAG_BTF flag at the same time .
  235. * In this case the communication will be slower.
  236. *
  237. */
  238. /* Slave Receiver mode*/
  239. /* EVT2 */
  240. #define I2C_EVENT_SLAVE_BYTE_RECEIVED ((uint32_t)0x00020040) /* BUSY and RXNE flags */
  241. /* EVT4 */
  242. #define I2C_EVENT_SLAVE_STOP_DETECTED ((uint32_t)0x00000010) /* STOPF flag */
  243. /* Slave Transmitter mode -----------------------*/
  244. /* EVT3 */
  245. #define I2C_EVENT_SLAVE_BYTE_TRANSMITTED ((uint32_t)0x00060084) /* TRA, BUSY, TXE and BTF flags */
  246. #define I2C_EVENT_SLAVE_BYTE_TRANSMITTING ((uint32_t)0x00060080) /* TRA, BUSY and TXE flags */
  247. /* EVT3_2 */
  248. #define I2C_EVENT_SLAVE_ACK_FAILURE ((uint32_t)0x00000400) /* AF flag */
  249. void I2C_DeInit(I2C_TypeDef *I2Cx);
  250. void I2C_Init(I2C_TypeDef *I2Cx, I2C_InitTypeDef *I2C_InitStruct);
  251. void I2C_StructInit(I2C_InitTypeDef *I2C_InitStruct);
  252. void I2C_Cmd(I2C_TypeDef *I2Cx, FunctionalState NewState);
  253. void I2C_DMACmd(I2C_TypeDef *I2Cx, FunctionalState NewState);
  254. void I2C_DMALastTransferCmd(I2C_TypeDef *I2Cx, FunctionalState NewState);
  255. void I2C_GenerateSTART(I2C_TypeDef *I2Cx, FunctionalState NewState);
  256. void I2C_GenerateSTOP(I2C_TypeDef *I2Cx, FunctionalState NewState);
  257. void I2C_AcknowledgeConfig(I2C_TypeDef *I2Cx, FunctionalState NewState);
  258. void I2C_OwnAddress2Config(I2C_TypeDef *I2Cx, uint8_t Address);
  259. void I2C_DualAddressCmd(I2C_TypeDef *I2Cx, FunctionalState NewState);
  260. void I2C_GeneralCallCmd(I2C_TypeDef *I2Cx, FunctionalState NewState);
  261. void I2C_ITConfig(I2C_TypeDef *I2Cx, uint16_t I2C_IT, FunctionalState NewState);
  262. void I2C_SendData(I2C_TypeDef *I2Cx, uint8_t Data);
  263. uint8_t I2C_ReceiveData(I2C_TypeDef *I2Cx);
  264. void I2C_Send7bitAddress(I2C_TypeDef *I2Cx, uint8_t Address, uint8_t I2C_Direction);
  265. uint16_t I2C_ReadRegister(I2C_TypeDef *I2Cx, uint8_t I2C_Register);
  266. void I2C_SoftwareResetCmd(I2C_TypeDef *I2Cx, FunctionalState NewState);
  267. void I2C_NACKPositionConfig(I2C_TypeDef *I2Cx, uint16_t I2C_NACKPosition);
  268. void I2C_TransmitPEC(I2C_TypeDef *I2Cx, FunctionalState NewState);
  269. void I2C_PECPositionConfig(I2C_TypeDef *I2Cx, uint16_t I2C_PECPosition);
  270. void I2C_CalculatePEC(I2C_TypeDef *I2Cx, FunctionalState NewState);
  271. uint8_t I2C_GetPEC(I2C_TypeDef *I2Cx);
  272. void I2C_ARPCmd(I2C_TypeDef *I2Cx, FunctionalState NewState);
  273. void I2C_StretchClockCmd(I2C_TypeDef *I2Cx, FunctionalState NewState);
  274. void I2C_FastModeDutyCycleConfig(I2C_TypeDef *I2Cx, uint16_t I2C_DutyCycle);
  275. /*****************************************************************************************
  276. *
  277. * I2C State Monitoring Functions
  278. *
  279. ****************************************************************************************
  280. * This I2C driver provides three different ways for I2C state monitoring
  281. * profit the application requirements and constraints:
  282. *
  283. *
  284. * a) First way:
  285. * Using I2C_CheckEvent() function:
  286. * It compares the status registers (STARR1 and STAR2) content to a given event
  287. * (can be the combination of more flags).
  288. * If the current status registers includes the given flags will return SUCCESS.
  289. * and if the current status registers miss flags will returns ERROR.
  290. * - When to use:
  291. * - This function is suitable for most applications as well as for startup
  292. * activity since the events are fully described in the product reference manual
  293. * (CH32V03RM).
  294. * - It is also suitable for users who need to define their own events.
  295. * - Limitations:
  296. * - If an error occurs besides to the monitored error,
  297. * the I2C_CheckEvent() function may return SUCCESS despite the communication
  298. * in corrupted state. it is suggeted to use error interrupts to monitor the error
  299. * events and handle them in IRQ handler.
  300. *
  301. *
  302. * Note:
  303. * The following functions are recommended for error management: :
  304. * - I2C_ITConfig() main function of configure and enable the error interrupts.
  305. * - I2Cx_ER_IRQHandler() will be called when the error interrupt happen.
  306. * Where x is the peripheral instance (I2C1, I2C2 ...)
  307. * - I2Cx_ER_IRQHandler() will call I2C_GetFlagStatus() or I2C_GetITStatus() functions
  308. * to determine which error occurred.
  309. * - I2C_ClearFlag() \ I2C_ClearITPendingBit() \ I2C_SoftwareResetCmd()
  310. * \ I2C_GenerateStop() will be use to clear the error flag and source,
  311. * and return to correct communication status.
  312. *
  313. *
  314. * b) Second way:
  315. * Using the function to get a single word(uint32_t) composed of status register 1 and register 2.
  316. * (Status Register 2 value is shifted left by 16 bits and concatenated to Status Register 1).
  317. * - When to use:
  318. *
  319. * - This function is suitable for the same applications above but it
  320. * don't have the limitations of I2C_GetFlagStatus() function .
  321. * The returned value could be compared to events already defined in the
  322. * library (CH32V00X_i2c.h) or to custom values defined by user.
  323. * - This function can be used to monitor the status of multiple flags simultaneously.
  324. * - Contrary to the I2C_CheckEvent () function, this function can choose the time to
  325. * accept the event according to the user's needs (when all event flags are set and
  326. * no other flags are set, or only when the required flags are set)
  327. *
  328. * - Limitations:
  329. * - User may need to define his own events.
  330. * - Same remark concerning the error management is applicable for this
  331. * function if user decides to check only regular communication flags (and
  332. * ignores error flags).
  333. *
  334. *
  335. * c) Third way:
  336. * Using the function I2C_GetFlagStatus() get the status of
  337. * one single flag .
  338. * - When to use:
  339. * - This function could be used for specific applications or in debug phase.
  340. * - It is suitable when only one flag checking is needed .
  341. *
  342. * - Limitations:
  343. * - Call this function to access the status register. Some flag bits may be cleared.
  344. * - Function may need to be called twice or more in order to monitor one single event.
  345. */
  346. /*********************************************************
  347. *
  348. * a) Basic state monitoring(First way)
  349. ********************************************************
  350. */
  351. ErrorStatus I2C_CheckEvent(I2C_TypeDef* I2Cx, uint32_t I2C_EVENT);
  352. /*********************************************************
  353. *
  354. * b) Advanced state monitoring(Second way:)
  355. ********************************************************
  356. */
  357. uint32_t I2C_GetLastEvent(I2C_TypeDef* I2Cx);
  358. /*********************************************************
  359. *
  360. * c) Flag-based state monitoring(Third way)
  361. *********************************************************
  362. */
  363. FlagStatus I2C_GetFlagStatus(I2C_TypeDef* I2Cx, uint32_t I2C_FLAG);
  364. void I2C_ClearFlag(I2C_TypeDef *I2Cx, uint32_t I2C_FLAG);
  365. ITStatus I2C_GetITStatus(I2C_TypeDef *I2Cx, uint32_t I2C_IT);
  366. void I2C_ClearITPendingBit(I2C_TypeDef *I2Cx, uint32_t I2C_IT);
  367. #ifdef __cplusplus
  368. }
  369. #endif
  370. #endif /*__CH32V00X_I2C_H */