1
0

apm32f10x_i2c.h 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348
  1. /*!
  2. * @file apm32f10x_i2c.h
  3. *
  4. * @brief This file contains all the functions prototypes for the I2C firmware library
  5. *
  6. * @version V1.0.2
  7. *
  8. * @date 2022-01-05
  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 usefull 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. #ifndef __APM32F10X_I2C_H
  26. #define __APM32F10X_I2C_H
  27. #ifdef __cplusplus
  28. extern "C" {
  29. #endif
  30. #include "apm32f10x.h"
  31. /** @addtogroup Peripherals_Library Standard Peripheral Library
  32. @{
  33. */
  34. /** @addtogroup I2C_Driver I2C Driver
  35. @{
  36. */
  37. /** @addtogroup I2C_Enumerations Enumerations
  38. @{
  39. */
  40. /**
  41. * @brief I2C Mode
  42. */
  43. typedef enum
  44. {
  45. I2C_MODE_I2C = 0x0000,
  46. I2C_MODE_SMBUUSDEVICE = 0x0002,
  47. I2C_MODE_SMBUSHOST = 0x000A
  48. } I2C_MODE_T;
  49. /**
  50. * @brief I2C duty cycle in fast mode
  51. */
  52. typedef enum
  53. {
  54. I2C_DUTYCYCLE_16_9 = 0x4000,
  55. I2C_DUTYCYCLE_2 = 0xBFFF
  56. } I2C_DUTYCYCLE_T;
  57. /**
  58. * @brief I2C acknowledgement
  59. */
  60. typedef enum
  61. {
  62. I2C_ACK_DISABLE,
  63. I2C_ACK_ENABLE
  64. } I2C_ACK_T;
  65. /**
  66. * @brief I2C acknowledged address
  67. */
  68. typedef enum
  69. {
  70. I2C_ACK_ADDRESS_7BIT = 0x4000,
  71. I2C_ACK_ADDRESS_10BIT = 0xC000
  72. } I2C_ACK_ADDRESS_T;
  73. /**
  74. * @brief I2C interrupts definition
  75. */
  76. typedef enum
  77. {
  78. I2C_INT_BUF = 0x0400,
  79. I2C_INT_EVT = 0x0200,
  80. I2C_INT_ERR = 0x0100
  81. } I2C_INT_T;
  82. /**
  83. * @brief I2C transfer direction
  84. */
  85. typedef enum
  86. {
  87. I2C_DIRECTION_TX,
  88. I2C_DIRECTION_RX
  89. } I2C_DIRECTION_T;
  90. /**
  91. * @brief I2C Register
  92. */
  93. typedef enum
  94. {
  95. I2C_REGISTER_CTRL1,
  96. I2C_REGISTER_CTRL2,
  97. I2C_REGISTER_SADDR1,
  98. I2C_REGISTER_SADDR2,
  99. I2C_REGISTER_DATA,
  100. I2C_REGISTER_STS1,
  101. I2C_REGISTER_STS2,
  102. I2C_REGISTER_CLKCTRL,
  103. I2C_REGISTER_RISETMAX,
  104. I2C_REGISTER_SWITCH
  105. } I2C_REGISTER_T;
  106. /**
  107. * @brief I2C NCAK position
  108. */
  109. typedef enum
  110. {
  111. I2C_NACK_POSITION_NEXT,
  112. I2C_NACK_POSITION_CURRENT
  113. } I2C_NACK_POSITION_T;
  114. /**
  115. * @brief I2C SMBus alert pin level
  116. */
  117. typedef enum
  118. {
  119. I2C_SMBUSALER_LOW,
  120. I2C_SMBUSALER_HIGH
  121. } I2C_SMBUSALER_T;
  122. /**
  123. * @brief I2C PEC position
  124. */
  125. typedef enum
  126. {
  127. I2C_PEC_POSITION_NEXT,
  128. I2C_PEC_POSITION_CURRENT
  129. } I2C_PEC_POSITION_T;
  130. /**
  131. * @brief I2C Events
  132. */
  133. typedef enum
  134. {
  135. /** I2C Master Events */
  136. /** Event 5: Communication start event */
  137. I2C_EVENT_MASTER_MODE_SELECT = 0x00030001, //!< BUSBSYFLG, MSFLG and STARTFLG flag
  138. /**
  139. * Event 6: 7-bit Address Acknowledge
  140. * in case of master receiver
  141. */
  142. I2C_EVENT_MASTER_TRANSMITTER_MODE_SELECTED = 0x00070082, //!< BUSBSYFLG, MSFLG, ADDRFLG, TXBEFLG and TRFLG flags */
  143. I2C_EVENT_MASTER_RECEIVER_MODE_SELECTED = 0x00030002, //!< BUSBSYFLG, MSFLG and ADDRFLG flags */
  144. /**
  145. * Event 9: Master has sent the first byte
  146. * in 10-bit address mode
  147. */
  148. I2C_EVENT_MASTER_MODE_ADDRESS10 = 0x00030008, //!< BUSBSYFLG, MSFLG and ADDR10FLG flags */
  149. /** Master RECEIVER mode */
  150. /** Event 7 */
  151. I2C_EVENT_MASTER_BYTE_RECEIVED = 0x00030040, //!< BUSBSYFLG, MSFLG and RXBNEFLG flags */
  152. /** Master TRANSMITTER mode */
  153. /** Event 8 */
  154. I2C_EVENT_MASTER_BYTE_TRANSMITTING = 0x00070080, //!< TRFLG, BUSBSYFLG, MSFLG, TXBEFLG flags */
  155. /** Event 8_2 */
  156. I2C_EVENT_MASTER_BYTE_TRANSMITTED = 0x00070084, //!< TRFLG, BUSBSYFLG, MSFLG, TXBEFLG and BTCFLG flags */
  157. /** EV1 (all the events below are variants of EV1) */
  158. /** 1, Case of One Single Address managed by the slave */
  159. I2C_EVENT_SLAVE_RECEIVER_ADDRESS_MATCHED = 0x00020002, //!< BUSBSYFLG and ADDRFLG flags */
  160. I2C_EVENT_SLAVE_TRANSMITTER_ADDRESS_MATCHED = 0x00060082, //!< TRFLG, BUSBSYFLG, TXBEFLG and ADDRFLG flags */
  161. /** 2, Case of Dual address managed by the slave */
  162. I2C_EVENT_SLAVE_RECEIVER_SECONDADDRESS_MATCHED = 0x00820000, //!< DUALF and BUSBSYFLG flags */
  163. I2C_EVENT_SLAVE_TRANSMITTER_SECONDADDRESS_MATCHED = 0x00860080, //!< DUALF, TRFLG, BUSBSYFLG and TXBEFLG flags */
  164. /** 3, Case of General Call enabled for the slave */
  165. I2C_EVENT_SLAVE_GENERALCALLADDRESS_MATCHED = 0x00120000, //!< GENCALL and BUSBSYFLG flags */
  166. /** Slave RECEIVER mode */
  167. /** EV2 */
  168. I2C_EVENT_SLAVE_BYTE_RECEIVED = 0x00020040, //!< BUSBSYFLG and RXBNEFLG flags */
  169. /** EV4 */
  170. I2C_EVENT_SLAVE_STOP_DETECTED = 0x00000010, //!< STOPFLG flag */
  171. /** Slave TRANSMITTER mode */
  172. /** EV3 */
  173. I2C_EVENT_SLAVE_BYTE_TRANSMITTED = 0x00060084, //!< TRFLG, BUSBSYFLG, TXBEFLG and BTCFLG flags */
  174. I2C_EVENT_SLAVE_BYTE_TRANSMITTING = 0x00060080, //!< TRFLG, BUSBSYFLG and TXBEFLG flags */
  175. /** EV3_2 */
  176. I2C_EVENT_SLAVE_ACK_FAILURE = 0x00000400, //!< AEFLG flag */
  177. } I2C_EVENT_T;
  178. /**
  179. * @brief I2C flags
  180. */
  181. typedef enum
  182. {
  183. /** STS2 register flags */
  184. I2C_FLAG_DUALADDR,
  185. I2C_FLAG_SMMHADDR,
  186. I2C_FLAG_SMBDADDR,
  187. I2C_FLAG_GENCALL,
  188. I2C_FLAG_TR,
  189. I2C_FLAG_BUSBSY,
  190. I2C_FLAG_MS,
  191. /** STS1 register flags */
  192. I2C_FLAG_SMBALT,
  193. I2C_FLAG_TTE,
  194. I2C_FLAG_PECE,
  195. I2C_FLAG_OVRUR,
  196. I2C_FLAG_AE,
  197. I2C_FLAG_AL,
  198. I2C_FLAG_BERR,
  199. I2C_FLAG_TXBE,
  200. I2C_FLAG_RXBNE,
  201. I2C_FLAG_STOP,
  202. I2C_FLAG_ADDR10,
  203. I2C_FLAG_BTC,
  204. I2C_FLAG_ADDR,
  205. I2C_FLAG_START,
  206. } I2C_FLAG_T;
  207. /**
  208. * @brief I2C interrupt
  209. */
  210. typedef enum
  211. {
  212. I2C_INT_FLAG_SMBALT = 0x01008000,
  213. I2C_INT_FLAG_TTE = 0x01004000,
  214. I2C_INT_FLAG_PECE = 0x01001000,
  215. I2C_INT_FLAG_OVRUR = 0x01000800,
  216. I2C_INT_FLAG_AE = 0x01000400,
  217. I2C_INT_FLAG_AL = 0x01000200,
  218. I2C_INT_FLAG_BERR = 0x01000100,
  219. I2C_INT_FLAG_TXBE = 0x06000080,
  220. I2C_INT_FLAG_RXBNE = 0x06000040,
  221. I2C_INT_FLAG_STOP = 0x02000010,
  222. I2C_INT_FLAG_ADDR10 = 0x02000008,
  223. I2C_INT_FLAG_BTC = 0x02000004,
  224. I2C_INT_FLAG_ADDR = 0x02000002,
  225. I2C_INT_FLAG_START = 0x02000001,
  226. } I2C_INT_FLAG_T;
  227. /**@} end of group I2C_Enumerations*/
  228. /** @addtogroup I2C_Structure Data Structure
  229. @{
  230. */
  231. /**
  232. * @brief I2C Config structure definition
  233. */
  234. typedef struct
  235. {
  236. uint32_t clockSpeed;
  237. I2C_MODE_T mode;
  238. I2C_DUTYCYCLE_T dutyCycle;
  239. uint16_t ownAddress1;
  240. I2C_ACK_T ack;
  241. I2C_ACK_ADDRESS_T ackAddress;
  242. } I2C_Config_T;
  243. /**@} end of group I2C_Structure*/
  244. /** @addtogroup I2C_Fuctions Fuctions
  245. @{
  246. */
  247. /** I2C reset and configuration */
  248. void I2C_Reset(I2C_T* i2c);
  249. void I2C_Config(I2C_T* i2c, I2C_Config_T* i2cConfig);
  250. void I2C_ConfigStructInit(I2C_Config_T* i2cConfig);
  251. void I2C_Enable(I2C_T* i2c);
  252. void I2C_Disable(I2C_T* i2c);
  253. void I2C_EnableGenerateStart(I2C_T* i2c);
  254. void I2C_DisableGenerateStart(I2C_T* i2c);
  255. void I2C_EnableGenerateStop(I2C_T* i2c);
  256. void I2C_DisableGenerateStop(I2C_T* i2c);
  257. void I2C_EnableAcknowledge(I2C_T* i2c);
  258. void I2C_DisableAcknowledge(I2C_T* i2c);
  259. void I2C_ConfigOwnAddress2(I2C_T* i2c, uint8_t address);
  260. void I2C_EnableDualAddress(I2C_T* i2c);
  261. void I2C_DisableDualAddress(I2C_T* i2c);
  262. void I2C_EnableGeneralCall(I2C_T* i2c);
  263. void I2C_DisableGeneralCall(I2C_T* i2c);
  264. /** Transmit Configuration */
  265. void I2C_TxData(I2C_T* i2c, uint8_t data);
  266. uint8_t I2C_RxData(I2C_T* i2c);
  267. void I2C_Tx7BitAddress(I2C_T* i2c, uint8_t address, I2C_DIRECTION_T direction);
  268. uint16_t I2C_ReadRegister(I2C_T* i2c, I2C_REGISTER_T i2cRegister);
  269. void I2C_EnableSoftwareReset(I2C_T* i2c);
  270. void I2C_DisableSoftwareReset(I2C_T* i2c);
  271. void I2C_ConfigNACKPosition(I2C_T* i2c, I2C_NACK_POSITION_T NACKPosition);
  272. void I2C_ConfigSMBusAlert(I2C_T* i2c, I2C_SMBUSALER_T SMBusState);
  273. void I2C_EnablePECTransmit(I2C_T* i2c);
  274. void I2C_DisablePECTransmit(I2C_T* i2c);
  275. void I2C_ConfigPECPosition(I2C_T* i2c, I2C_PEC_POSITION_T PECPosition);
  276. void I2C_EnablePEC(I2C_T* i2c);
  277. void I2C_DisablePEC(I2C_T* i2c);
  278. uint8_t I2C_ReadPEC(I2C_T* i2c);
  279. void I2C_EnableARP(I2C_T* i2c);
  280. void I2C_DisableARP(I2C_T* i2c);
  281. void I2C_EnableStretchClock(I2C_T* i2c);
  282. void I2C_DisableStretchClock(I2C_T* i2c);
  283. void I2C_ConfigFastModeDutyCycle(I2C_T* i2c, I2C_DUTYCYCLE_T dutyCycle);
  284. /** DMA */
  285. void I2C_EnableDMA(I2C_T* i2c);
  286. void I2C_DisableDMA(I2C_T* i2c);
  287. void I2C_EnableDMALastTransfer(I2C_T* i2c);
  288. void I2C_DisableDMALastTransfer(I2C_T* i2c);
  289. /** Interrupts and flags */
  290. void I2C_EnableInterrupt(I2C_T* i2c, uint16_t interrupt);
  291. void I2C_DisableInterrupt(I2C_T* i2c, uint16_t interrupt);
  292. uint8_t I2C_ReadEventStatus(I2C_T* i2c, I2C_EVENT_T i2cEvent);
  293. uint32_t I2C_ReadLastEvent(I2C_T* i2c);
  294. uint8_t I2C_ReadStatusFlag(I2C_T* i2c, I2C_FLAG_T flag);
  295. void I2C_ClearStatusFlag(I2C_T* i2c, I2C_FLAG_T flag);
  296. uint8_t I2C_ReadIntFlag(I2C_T* i2c, I2C_INT_FLAG_T flag);
  297. void I2C_ClearIntFlag(I2C_T* i2c, uint32_t flag);
  298. /**@} end of group I2C_Fuctions*/
  299. /**@} end of group I2C_Driver*/
  300. /**@} end of group Peripherals_Library*/
  301. #ifdef __cplusplus
  302. }
  303. #endif
  304. #endif /* __APM32F10X_I2C_H */