apm32f0xx_i2c.h 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405
  1. /*!
  2. * @file apm32f0xx_i2c.h
  3. *
  4. * @brief This file contains all the functions prototypes for the I2C firmware library
  5. *
  6. * @version V1.0.3
  7. *
  8. * @date 2022-09-20
  9. *
  10. * @attention
  11. *
  12. * Copyright (C) 2020-2022 Geehy Semiconductor
  13. *
  14. * You may not use this file except in compliance with the
  15. * GEEHY COPYRIGHT NOTICE (GEEHY SOFTWARE PACKAGE LICENSE).
  16. *
  17. * The program is only for reference, which is distributed in the hope
  18. * that it will be useful and instructional for customers to develop
  19. * their software. Unless required by applicable law or agreed to in
  20. * writing, the program is distributed on an "AS IS" BASIS, WITHOUT
  21. * ANY WARRANTY OR CONDITIONS OF ANY KIND, either express or implied.
  22. * See the GEEHY SOFTWARE PACKAGE LICENSE for the governing permissions
  23. * and limitations under the License.
  24. */
  25. /* Define to prevent recursive inclusion */
  26. #ifndef __APM32F0XX_I2C_H
  27. #define __APM32F0XX_I2C_H
  28. #ifdef __cplusplus
  29. extern "C" {
  30. #endif
  31. /* Includes */
  32. #include "apm32f0xx.h"
  33. /** @addtogroup APM32F0xx_StdPeriphDriver
  34. @{
  35. */
  36. /** @addtogroup I2C_Driver
  37. @{
  38. */
  39. /** @defgroup I2C_Macros Macros
  40. @{
  41. */
  42. /* Slave address (master mode) */
  43. #define I2C_CTRL2_SADD ((uint32_t)0x000003FF)
  44. /* Number of bytes */
  45. #define I2C_CTRL2_NUMBYT ((uint32_t)0x00FF0000)
  46. /* ENDCFG,RELOADEN,STOP,START,TXDIR clear CTRL2 register Mask */
  47. #define I2C_CTRL2_MASK ((uint32_t)0x03006400)
  48. /* I2C Register reset value */
  49. #define I2C_CTRL1_RESET_VALUE ((uint32_t)0x00)
  50. #define I2C_CTRL2_RESET_VALUE ((uint32_t)0x00)
  51. #define I2C_ADDR1_RESET_VALUE ((uint32_t)0x00)
  52. #define I2C_ADDR2_RESET_VALUE ((uint32_t)0x00)
  53. #define I2C_TIMING_RESET_VALUE ((uint32_t)0x00)
  54. #define I2C_TIMEOUT_RESET_VALUE ((uint32_t)0x00)
  55. #define I2C_STS_RESET_VALUE ((uint32_t)0x01)
  56. #define I2C_INTFCLR_RESET_VALUE ((uint32_t)0x00)
  57. #define I2C_PEC_RESET_VALUE ((uint32_t)0x00)
  58. #define I2C_TXDATA_RESET_VALUE ((uint32_t)0x00)
  59. #define I2C_RXDATA_RESET_VALUE ((uint32_t)0x00)
  60. /* I2C_channels */
  61. #define I2C_Channel_TempSensor ((uint32_t)I2C_CHANNEL_16)
  62. #define I2C_Channel_Vrefint ((uint32_t)I2C_CHANNEL_17)
  63. /**@} end of group I2C_Macros */
  64. /** @defgroup I2C_Enumerations Enumerations
  65. @{
  66. */
  67. /**
  68. * @brief I2C Analog noise filter
  69. */
  70. typedef enum
  71. {
  72. I2C_ANALOG_FILTER_ENABLE = ((uint8_t)0), /*!< Analog noise filter enabled */
  73. I2C_ANALOG_FILTER_DISABLE = ((uint8_t)1), /*!< Analog noise filter disabled */
  74. } I2C_ANALOG_FILTER_T;
  75. /**
  76. * @brief I2C Digital noise filter
  77. */
  78. typedef enum
  79. {
  80. I2C_DIGITAL_FILTER_0 = ((uint8_t)0x00), /*!< Digital filter disabled */
  81. I2C_DIGITAL_FILTER_1 = ((uint8_t)0x01), /*!< Digital filter enabled and filtering capability is 1 T_i2cclk */
  82. I2C_DIGITAL_FILTER_2 = ((uint8_t)0x02), /*!< Digital filter enabled and filtering capability is 3 T_i2cclk */
  83. I2C_DIGITAL_FILTER_3 = ((uint8_t)0x03), /*!< Digital filter enabled and filtering capability is 3 T_i2cclk */
  84. I2C_DIGITAL_FILTER_4 = ((uint8_t)0x04), /*!< Digital filter enabled and filtering capability is 4 T_i2cclk */
  85. I2C_DIGITAL_FILTER_5 = ((uint8_t)0x05), /*!< Digital filter enabled and filtering capability is 5 T_i2cclk */
  86. I2C_DIGITAL_FILTER_6 = ((uint8_t)0x06), /*!< Digital filter enabled and filtering capability is 6 T_i2cclk */
  87. I2C_DIGITAL_FILTER_7 = ((uint8_t)0x07), /*!< Digital filter enabled and filtering capability is 7 T_i2cclk */
  88. I2C_DIGITAL_FILTER_8 = ((uint8_t)0x08), /*!< Digital filter enabled and filtering capability is 8 T_i2cclk */
  89. I2C_DIGITAL_FILTER_9 = ((uint8_t)0x09), /*!< Digital filter enabled and filtering capability is 9 T_i2cclk */
  90. I2C_DIGITAL_FILTER_10 = ((uint8_t)0x0A), /*!< Digital filter enabled and filtering capability is 10 T_i2cclk */
  91. I2C_DIGITAL_FILTER_11 = ((uint8_t)0x0B), /*!< Digital filter enabled and filtering capability is 11 T_i2cclk */
  92. I2C_DIGITAL_FILTER_12 = ((uint8_t)0x0C), /*!< Digital filter enabled and filtering capability is 12 T_i2cclk */
  93. I2C_DIGITAL_FILTER_13 = ((uint8_t)0x0D), /*!< Digital filter enabled and filtering capability is 13 T_i2cclk */
  94. I2C_DIGITAL_FILTER_14 = ((uint8_t)0x0E), /*!< Digital filter enabled and filtering capability is 14 T_i2cclk */
  95. I2C_DIGITAL_FILTER_15 = ((uint8_t)0x0F), /*!< Digital filter enabled and filtering capability is 15 T_i2cclk */
  96. } I2C_DIGITAL_FILTER_T;
  97. /**
  98. * @brief I2C mode
  99. */
  100. typedef enum
  101. {
  102. I2C_MODE_I2C = ((uint32_t)0x00000000), /*!< I2C mode */
  103. I2C_MODE_SMBUSDEVICE = ((uint32_t)0x00100000), /*!< SMBus device mode */
  104. I2C_MODE_SMBUSHOST = ((uint32_t)0x00200000), /*!< SMBus host mode */
  105. } I2C_MODE_T;
  106. /**
  107. * @brief I2C acknowledge
  108. */
  109. typedef enum
  110. {
  111. I2C_ACK_ENABLE = ((uint8_t)0), /*!< ACK generation (slave mode) */
  112. I2C_ACK_DISABLE = ((uint8_t)1), /*!< NACK generation (slave mode) */
  113. } I2C_ACK_T;
  114. /**
  115. * @brief I2C acknowledge address
  116. */
  117. typedef enum
  118. {
  119. I2C_ACK_ADDRESS_7BIT = ((uint8_t)0), /*!< ACK generation (slave mode) */
  120. I2C_ACK_ADDRESS_10BIT = ((uint8_t)1), /*!< NACK generation (slave mode) */
  121. } I2C_ACK_ADDRESS_T;
  122. /**
  123. * @brief I2C transfer direction
  124. */
  125. typedef enum
  126. {
  127. I2C_DIRECTION_TX = ((uint8_t)0), /*!< Transmission direction */
  128. I2C_DIRECTION_RX = ((uint8_t)1), /*!< Reception direction */
  129. } I2C_DIRECTION_T;
  130. /**
  131. * @brief I2C DMA requests
  132. */
  133. typedef enum
  134. {
  135. I2C_DMA_REQ_TX = ((uint8_t)0), /*!< DMA transmission requests enable */
  136. I2C_DMA_REQ_RX = ((uint8_t)1), /*!< DMA reception requests enable */
  137. } I2C_DMA_REQ_T;
  138. /**
  139. * @brief I2C own address2 mask
  140. */
  141. typedef enum
  142. {
  143. I2C_ADDR2MSK_NOMASK = ((uint8_t)0x00), /*!< No masked */
  144. I2C_ADDR2MSK_MASK01 = ((uint8_t)0x01), /*!< Don't care masked ADDR2[1:0] */
  145. I2C_ADDR2MSK_MASK02 = ((uint8_t)0x02), /*!< Don't care masked ADDR2[2:1] */
  146. I2C_ADDR2MSK_MASK03 = ((uint8_t)0x03), /*!< Don't care masked ADDR2[3:1] */
  147. I2C_ADDR2MSK_MASK04 = ((uint8_t)0x04), /*!< Don't care masked ADDR2[4:1] */
  148. I2C_ADDR2MSK_MASK05 = ((uint8_t)0x05), /*!< Don't care masked ADDR2[5:1] */
  149. I2C_ADDR2MSK_MASK06 = ((uint8_t)0x06), /*!< Don't care masked ADDR2[6:1] */
  150. I2C_ADDR2MSK_MASK07 = ((uint8_t)0x07), /*!< Don't care masked ADDR2[7:1] */
  151. } I2C_ADDR2MSK_T;
  152. /**
  153. * @brief I2C registers
  154. */
  155. typedef enum
  156. {
  157. I2C_REGISTER_CTRL1 = ((uint8_t)0x00), /*!< CTRL1 register */
  158. I2C_REGISTER_CTRL2 = ((uint8_t)0x04), /*!< CTRL2 register */
  159. I2C_REGISTER_ADDR1 = ((uint8_t)0x08), /*!< ADDR1 register */
  160. I2C_REGISTER_ADDR2 = ((uint8_t)0x0C), /*!< ADDR2 register */
  161. I2C_REGISTER_TIMING = ((uint8_t)0x10), /*!< TIMING register */
  162. I2C_REGISTER_TIMEOUT = ((uint8_t)0x14), /*!< TIMEOUT register */
  163. I2C_REGISTER_STS = ((uint8_t)0x18), /*!< STS register */
  164. I2C_REGISTER_INTFCLR = ((uint8_t)0x1C), /*!< INTFCLR register */
  165. I2C_REGISTER_PEC = ((uint8_t)0x20), /*!< PEC register */
  166. I2C_REGISTER_RXDATA = ((uint8_t)0x24), /*!< RXDATA register */
  167. I2C_REGISTER_TXDATA = ((uint8_t)0x28), /*!< TXDATA register */
  168. } I2C_REGISTER_T;
  169. /**
  170. * @brief I2C interrupts source
  171. */
  172. typedef enum
  173. {
  174. I2C_INT_TXIE = ((uint8_t)0x02), /*!< TX Interrupt enable */
  175. I2C_INT_RXIE = ((uint8_t)0x04), /*!< RX Interrupt enable */
  176. I2C_INT_ADDRIE = ((uint8_t)0x08), /*!< Address match interrupt enable (slave only) */
  177. I2C_INT_NACKIE = ((uint8_t)0x10), /*!< Not acknowledge received interrupt enable */
  178. I2C_INT_STOPIE = ((uint8_t)0x20), /*!< STOP detection Interrupt enable */
  179. I2C_INT_TXCIE = ((uint8_t)0x40), /*!< Transfer complete interrupt enable */
  180. I2C_INT_ERRIE = ((uint8_t)0x80), /*!< Error interrupts enable */
  181. } I2C_INT_T;
  182. /**
  183. * @brief I2C Flags
  184. */
  185. typedef enum
  186. {
  187. /* STS1 register flag */
  188. I2C_FLAG_TXBE = ((uint32_t)0x0001), /*!< Transmit buffer data register empty flag */
  189. I2C_FLAG_TXINT = ((uint32_t)0x0002), /*!< Transmit interrupt flag */
  190. I2C_FLAG_RXBNE = ((uint32_t)0x0004), /*!< Read buffer data register not empty flag */
  191. I2C_FLAG_ADDR = ((uint32_t)0x0008), /*!< Address Sent/Matched (master/slave) flag */
  192. I2C_FLAG_NACK = ((uint32_t)0x0010), /*!< Not acknowledge received flag */
  193. I2C_FLAG_STOP = ((uint32_t)0x0020), /*!< Stop detected flag */
  194. I2C_FLAG_TXCF = ((uint32_t)0x0040), /*!< Transfer complete flag */
  195. I2C_FLAG_TCRF = ((uint32_t)0x0080), /*!< Transfer complete reload flag */
  196. I2C_FLAG_BUSERR = ((uint32_t)0x0100), /*!< Bus error flag */
  197. I2C_FLAG_ALF = ((uint32_t)0x0200), /*!< Arbitration Loss flag */
  198. I2C_FLAG_OVR = ((uint32_t)0x0400), /*!< Overrun/Underrun flag */
  199. I2C_FLAG_PECERR = ((uint32_t)0x0800), /*!< PEC error flag */
  200. I2C_FLAG_TIMEOUT = ((uint32_t)0x1000), /*!< Timeout or t_low detection flag */
  201. I2C_FLAG_ALERT = ((uint32_t)0x2000), /*!< SMBus alert flag */
  202. I2C_FLAG_BUSY = ((uint32_t)0x8000), /*!< Bus Busy Flag */
  203. } I2C_FLAG_T;
  204. /**
  205. * @brief I2C Interrupt flag
  206. */
  207. typedef enum
  208. {
  209. I2C_INT_FLAG_TXINT = ((uint32_t)0x0002), /*!< Transmit interrupt flag */
  210. I2C_INT_FLAG_RXBNE = ((uint32_t)0x0004), /*!< Read Buffer Data Register Not Empty interrupt flag */
  211. I2C_INT_FLAG_ADDR = ((uint32_t)0x0008), /*!< Address Sent/Matched (master/slave) interrupt flag */
  212. I2C_INT_FLAG_NACK = ((uint32_t)0x0010), /*!< Not acknowledge received interrupt flag */
  213. I2C_INT_FLAG_STOP = ((uint32_t)0x0020), /*!< Stop detected interrupt flag */
  214. I2C_INT_FLAG_TXCF = ((uint32_t)0x0040), /*!< Transfer complete interrupt flag */
  215. I2C_INT_FLAG_TCRF = ((uint32_t)0x0080), /*!< Transfer Complete Reloadinterrupt flag */
  216. I2C_INT_FLAG_BUSERR = ((uint32_t)0x0100), /*!< Bus error interrupt flag */
  217. I2C_INT_FLAG_ALF = ((uint32_t)0x0200), /*!< Arbitration Loss interrupt flag */
  218. I2C_INT_FLAG_OVR = ((uint32_t)0x0400), /*!< Overrun/Underrun interrupt flag */
  219. I2C_INT_FLAG_PECERR = ((uint32_t)0x0800), /*!< PEC error interrupt flag */
  220. I2C_INT_FLAG_TIMEOUT = ((uint32_t)0x1000), /*!< Timeout or t_low detection interrupt flag */
  221. I2C_INT_FLAG_ALERT = ((uint32_t)0x2000), /*!< SMBus alert interrupt flag */
  222. } I2C_INT_FLAG_T;
  223. /**
  224. * @brief I2C Reload End Mode
  225. */
  226. typedef enum
  227. {
  228. I2C_RELOAD_MODE_RELOAD = ((uint32_t)0x01000000), /*!< Enable Reload mode */
  229. I2C_RELOAD_MODE_AUTOEND = ((uint32_t)0x02000000), /*!< Enable Automatic end mode */
  230. I2C_RELOAD_MODE_SOFTEND = ((uint32_t)0x00000000), /*!< Enable Software end mode */
  231. } I2C_RELOAD_MODE_T;
  232. /**
  233. * @brief I2C Start/Stop Mode
  234. */
  235. typedef enum
  236. {
  237. I2C_GENERATE_NO_STARTSTOP = ((uint32_t)0x00000000), /*!< Don't Generate stop and start condition */
  238. I2C_GENERATE_START_WRITE = ((uint32_t)0x00002000), /*!< Generate Restart for write request */
  239. I2C_GENERATE_STOP = ((uint32_t)0x00004000), /*!< Generate stop condition */
  240. I2C_GENERATE_START_READ = ((uint32_t)0x00002400), /*!< Generate Restart for read request */
  241. } I2C_GENERATE_T;
  242. /**@} end of group I2C_Enumerations */
  243. /** @defgroup I2C_Structures Structures
  244. @{
  245. */
  246. /**
  247. * @brief I2C Config struct definition
  248. */
  249. typedef struct
  250. {
  251. uint32_t timing; /*!< Specifies the I2C_TIMINGR_register value */
  252. uint32_t address1; /*!< Specifies the device own address1 */
  253. I2C_ACK_T ack; /*!< Enables or disables the acknowledgement */
  254. I2C_MODE_T mode; /*!< Specifies the I2C mode*/
  255. I2C_ACK_ADDRESS_T ackaddress; /*!< Specifies if 7-bit or 10-bit address is acknowledged */
  256. I2C_ANALOG_FILTER_T analogfilter; /*!< Enables or disables analog noise filter */
  257. I2C_DIGITAL_FILTER_T digitalfilter; /*!< Configures the digital noise filter */
  258. } I2C_Config_T;
  259. /**@} end of group I2C_Structures */
  260. /** @defgroup I2C_Variables Variables
  261. @{
  262. */
  263. /**@} end of group I2C_Variables*/
  264. /** @defgroup I2C_Functions Functions
  265. @{
  266. */
  267. /* I2C reset and configuration */
  268. void I2C_Reset(I2C_T* i2c);
  269. void I2C_Config(I2C_T* i2c, I2C_Config_T* i2cConfig);
  270. void I2C_ConfigStructInit(I2C_Config_T* i2cConfig);
  271. void I2C_Enable(I2C_T* i2c);
  272. void I2C_Disable(I2C_T* i2c);
  273. void I2C_SoftwareReset(I2C_T* i2c);
  274. void I2C_EnableAutoEnd(I2C_T* i2c);
  275. void I2C_DisableAutoEnd(I2C_T* i2c);
  276. void I2C_EnableReload(I2C_T* i2c);
  277. void I2C_DisableReload(I2C_T* i2c);
  278. void I2C_ConfigNumberOfBytes(I2C_T* i2c, uint8_t number);
  279. void I2C_ConfigMasterRequest(I2C_T* i2c, I2C_DIRECTION_T direction);
  280. void I2C_EnableGenerateStart(I2C_T* i2c);
  281. void I2C_DisableGenerateStart(I2C_T* i2c);
  282. void I2C_EnableGenerateStop(I2C_T* i2c);
  283. void I2C_DisableGenerateStop(I2C_T* i2c);
  284. void I2C_EnableAcknowledge(I2C_T* i2c);
  285. void I2C_DisableAcknowledge(I2C_T* i2c);
  286. uint8_t I2C_ReadAddressMatched(I2C_T* i2c);
  287. uint16_t I2C_ReadTransferDirection(I2C_T* i2c);
  288. void I2C_HandlingTransfer(I2C_T* i2c, uint16_t address, uint8_t number, I2C_RELOAD_MODE_T reloadend, I2C_GENERATE_T generates);
  289. void I2C_ConfigTimeoutA(I2C_T* i2c, uint16_t timeout);
  290. void I2C_ConfigTimeoutB(I2C_T* i2c, uint16_t timeout);
  291. uint32_t I2C_ReadRegister(I2C_T* i2c, uint8_t registers);
  292. /* Interrupt */
  293. void I2C_EnableInterrupt(I2C_T* i2c, uint8_t interrupt);
  294. void I2C_DisableInterrupt(I2C_T* i2c, uint8_t interrupt);
  295. /* Clock Stretch */
  296. void I2C_EnableStretchClock(I2C_T* i2c);
  297. void I2C_DisableStretchClock(I2C_T* i2c);
  298. /* Stop Mode */
  299. void I2C_EnableStopMode(I2C_T* i2c); /*!< Not for APM32F030 devices */
  300. void I2C_DisableStopMode(I2C_T* i2c); /*!< Not for APM32F030 devices */
  301. /* Own address 2*/
  302. void I2C_EnableOwnAddress2(I2C_T* i2c);
  303. void I2C_DisableOwnAddress2(I2C_T* i2c);
  304. void I2C_OwnAddress2Mask(I2C_T* i2c, uint16_t address, I2C_ADDR2MSK_T mask);
  305. /* Broadcast */
  306. void I2C_EnableBroadcastCall(I2C_T* i2c);
  307. void I2C_DisableBroadcastCall(I2C_T* i2c);
  308. /* slave */
  309. void I2C_EnableSlaveByteControl(I2C_T* i2c);
  310. void I2C_DisableSlaveByteControl(I2C_T* i2c);
  311. void I2C_SlaveAddress(I2C_T* i2c, uint16_t address);
  312. /* master */
  313. void I2C_Enable10BitAddressingMode(I2C_T* i2c);
  314. void I2C_Disable10BitAddressingMode(I2C_T* i2c);
  315. void I2C_Enable10BitAddressHeader(I2C_T* i2c);
  316. void I2C_Disable10BitAddressHeader(I2C_T* i2c);
  317. /* Clock */
  318. void I2C_EnableClockTimeout(I2C_T* i2c);
  319. void I2C_DisableClockTimeout(I2C_T* i2c);
  320. void I2C_EnableExtendClockTimeout(I2C_T* i2c);
  321. void I2C_DisableExtendClockTimeout(I2C_T* i2c);
  322. void I2C_EnableIdleClockTimeout(I2C_T* i2c);
  323. void I2C_DisableIdleClockTimeout(I2C_T* i2c);
  324. /* Transmit and receive */
  325. void I2C_TxData(I2C_T* i2c, uint8_t data);
  326. uint8_t I2C_RxData(I2C_T* i2c);
  327. /* DMA request */
  328. void I2C_EnableDMA(I2C_T* i2c, I2C_DMA_REQ_T request);
  329. void I2C_DisableDMA(I2C_T* i2c, I2C_DMA_REQ_T request);
  330. /* flag */
  331. uint8_t I2C_ReadStatusFlag(I2C_T* i2c, I2C_FLAG_T flag);
  332. void I2C_ClearStatusFlag(I2C_T* i2c, uint32_t flag);
  333. uint8_t I2C_ReadIntFlag(I2C_T* i2c, I2C_INT_FLAG_T flag);
  334. void I2C_ClearIntFlag(I2C_T* i2c, uint32_t flag);
  335. /* SMBus */
  336. void I2C_EnableSMBusAlert(I2C_T* i2c);
  337. void I2C_DisableSMBusAlert(I2C_T* i2c);
  338. void I2C_EnableSMBusHAEN(I2C_T* i2c);
  339. void I2C_DisableSMBusHAEN(I2C_T* i2c);
  340. void I2C_EnableSMBusDAEN(I2C_T* i2c);
  341. void I2C_DisableSMBusDAEN(I2C_T* i2c);
  342. void I2C_EnablePEC(I2C_T* i2c);
  343. void I2C_DisablePEC(I2C_T* i2c);
  344. void I2C_EnablePECRequest(I2C_T* i2c);
  345. void I2C_DisablePECRequest(I2C_T* i2c);
  346. uint8_t I2C_ReadPEC(I2C_T* i2c);
  347. #ifdef __cplusplus
  348. }
  349. #endif
  350. #endif /* __APM32F0XX_I2C_H */
  351. /**@} end of group I2C_Functions */
  352. /**@} end of group I2C_Driver */
  353. /**@} end of group APM32F0xx_StdPeriphDriver */