ch32v30x_i2c.h 21 KB

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