ft32f0xx_i2c.h 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458
  1. /**
  2. ******************************************************************************
  3. * @file ft32f0xx_i2c.h
  4. * @author FMD AE
  5. * @brief This file contains all the functions prototypes for the I2C firmware
  6. * library
  7. * @version V1.0.0
  8. * @data 2021-07-01
  9. ******************************************************************************
  10. */
  11. /* Define to prevent recursive inclusion -------------------------------------*/
  12. #ifndef __FT32F0XX_I2C_H
  13. #define __FT32F0XX_I2C_H
  14. #ifdef __cplusplus
  15. extern "C" {
  16. #endif
  17. /* Includes ------------------------------------------------------------------*/
  18. #include "ft32f0xx.h"
  19. /** @addtogroup I2C
  20. * @{
  21. */
  22. /* Exported types ------------------------------------------------------------*/
  23. /**
  24. * @brief I2C Init structure definition
  25. */
  26. typedef struct
  27. {
  28. uint32_t I2C_Timing; /*!< Specifies the I2C_TIMINGR_register value.
  29. This parameter must be set by referring to I2C_Timing_Config_Tool*/
  30. uint32_t I2C_AnalogFilter; /*!< Enables or disables analog noise filter.
  31. This parameter can be a value of @ref I2C_Analog_Filter*/
  32. uint32_t I2C_DigitalFilter; /*!< Configures the digital noise filter.
  33. This parameter can be a number between 0x00 and 0x0F*/
  34. uint32_t I2C_Mode; /*!< Specifies the I2C mode.
  35. This parameter can be a value of @ref I2C_mode*/
  36. uint32_t I2C_OwnAddress1; /*!< Specifies the device own address 1.
  37. This parameter can be a 7-bit or 10-bit address*/
  38. uint32_t I2C_Ack; /*!< Enables or disables the acknowledgement.
  39. This parameter can be a value of @ref I2C_acknowledgement*/
  40. uint32_t I2C_AcknowledgedAddress; /*!< Specifies if 7-bit or 10-bit address is acknowledged.
  41. This parameter can be a value of @ref I2C_acknowledged_address*/
  42. }I2C_InitTypeDef;
  43. /* Exported constants --------------------------------------------------------*/
  44. /** @defgroup I2C_Exported_Constants
  45. * @{
  46. */
  47. #define IS_I2C_ALL_PERIPH(PERIPH) (((PERIPH) == I2C1) || \
  48. ((PERIPH) == I2C2))
  49. #define IS_I2C_1_PERIPH(PERIPH) ((PERIPH) == I2C1)
  50. /** @defgroup I2C_Analog_Filter
  51. * @{
  52. */
  53. #define I2C_AnalogFilter_Enable ((uint32_t)0x00000000)
  54. #define I2C_AnalogFilter_Disable I2C_CR1_ANFOFF
  55. #define IS_I2C_ANALOG_FILTER(FILTER) (((FILTER) == I2C_AnalogFilter_Enable) || \
  56. ((FILTER) == I2C_AnalogFilter_Disable))
  57. /**
  58. * @}
  59. */
  60. /** @defgroup I2C_Digital_Filter
  61. * @{
  62. */
  63. #define IS_I2C_DIGITAL_FILTER(FILTER) ((FILTER) <= 0x0000000F)
  64. /**
  65. * @}
  66. */
  67. /** @defgroup I2C_mode
  68. * @{
  69. */
  70. #define I2C_Mode_I2C ((uint32_t)0x00000000)
  71. #define I2C_Mode_SMBusDevice I2C_CR1_SMBDEN
  72. #define I2C_Mode_SMBusHost I2C_CR1_SMBHEN
  73. #define IS_I2C_MODE(MODE) (((MODE) == I2C_Mode_I2C) || \
  74. ((MODE) == I2C_Mode_SMBusDevice) || \
  75. ((MODE) == I2C_Mode_SMBusHost))
  76. /**
  77. * @}
  78. */
  79. /** @defgroup I2C_acknowledgement
  80. * @{
  81. */
  82. #define I2C_Ack_Enable ((uint32_t)0x00000000)
  83. #define I2C_Ack_Disable I2C_CR2_NACK
  84. #define IS_I2C_ACK(ACK) (((ACK) == I2C_Ack_Enable) || \
  85. ((ACK) == I2C_Ack_Disable))
  86. /**
  87. * @}
  88. */
  89. /** @defgroup I2C_acknowledged_address
  90. * @{
  91. */
  92. #define I2C_AcknowledgedAddress_7bit ((uint32_t)0x00000000)
  93. #define I2C_AcknowledgedAddress_10bit I2C_OAR1_OA1MODE
  94. #define IS_I2C_ACKNOWLEDGE_ADDRESS(ADDRESS) (((ADDRESS) == I2C_AcknowledgedAddress_7bit) || \
  95. ((ADDRESS) == I2C_AcknowledgedAddress_10bit))
  96. /**
  97. * @}
  98. */
  99. /** @defgroup I2C_own_address1
  100. * @{
  101. */
  102. #define IS_I2C_OWN_ADDRESS1(ADDRESS1) ((ADDRESS1) <= (uint32_t)0x000003FF)
  103. /**
  104. * @}
  105. */
  106. /** @defgroup I2C_transfer_direction
  107. * @{
  108. */
  109. #define I2C_Direction_Transmitter ((uint16_t)0x0000)
  110. #define I2C_Direction_Receiver ((uint16_t)0x0400)
  111. #define IS_I2C_DIRECTION(DIRECTION) (((DIRECTION) == I2C_Direction_Transmitter) || \
  112. ((DIRECTION) == I2C_Direction_Receiver))
  113. /**
  114. * @}
  115. */
  116. /** @defgroup I2C_DMA_transfer_requests
  117. * @{
  118. */
  119. #define I2C_DMAReq_Tx I2C_CR1_TXDMAEN
  120. #define I2C_DMAReq_Rx I2C_CR1_RXDMAEN
  121. #define IS_I2C_DMA_REQ(REQ) ((((REQ) & (uint32_t)0xFFFF3FFF) == 0x00) && ((REQ) != 0x00))
  122. /**
  123. * @}
  124. */
  125. /** @defgroup I2C_slave_address
  126. * @{
  127. */
  128. #define IS_I2C_SLAVE_ADDRESS(ADDRESS) ((ADDRESS) <= (uint16_t)0x03FF)
  129. /**
  130. * @}
  131. */
  132. /** @defgroup I2C_own_address2
  133. * @{
  134. */
  135. #define IS_I2C_OWN_ADDRESS2(ADDRESS2) ((ADDRESS2) <= (uint16_t)0x00FF)
  136. /**
  137. * @}
  138. */
  139. /** @defgroup I2C_own_address2_mask
  140. * @{
  141. */
  142. #define I2C_OA2_NoMask ((uint8_t)0x00)
  143. #define I2C_OA2_Mask01 ((uint8_t)0x01)
  144. #define I2C_OA2_Mask02 ((uint8_t)0x02)
  145. #define I2C_OA2_Mask03 ((uint8_t)0x03)
  146. #define I2C_OA2_Mask04 ((uint8_t)0x04)
  147. #define I2C_OA2_Mask05 ((uint8_t)0x05)
  148. #define I2C_OA2_Mask06 ((uint8_t)0x06)
  149. #define I2C_OA2_Mask07 ((uint8_t)0x07)
  150. #define IS_I2C_OWN_ADDRESS2_MASK(MASK) (((MASK) == I2C_OA2_NoMask) || \
  151. ((MASK) == I2C_OA2_Mask01) || \
  152. ((MASK) == I2C_OA2_Mask02) || \
  153. ((MASK) == I2C_OA2_Mask03) || \
  154. ((MASK) == I2C_OA2_Mask04) || \
  155. ((MASK) == I2C_OA2_Mask05) || \
  156. ((MASK) == I2C_OA2_Mask06) || \
  157. ((MASK) == I2C_OA2_Mask07))
  158. /**
  159. * @}
  160. */
  161. /** @defgroup I2C_timeout
  162. * @{
  163. */
  164. #define IS_I2C_TIMEOUT(TIMEOUT) ((TIMEOUT) <= (uint16_t)0x0FFF)
  165. /**
  166. * @}
  167. */
  168. /** @defgroup I2C_registers
  169. * @{
  170. */
  171. #define I2C_Register_CR1 ((uint8_t)0x00)
  172. #define I2C_Register_CR2 ((uint8_t)0x04)
  173. #define I2C_Register_OAR1 ((uint8_t)0x08)
  174. #define I2C_Register_OAR2 ((uint8_t)0x0C)
  175. #define I2C_Register_TIMINGR ((uint8_t)0x10)
  176. #define I2C_Register_TIMEOUTR ((uint8_t)0x14)
  177. #define I2C_Register_ISR ((uint8_t)0x18)
  178. #define I2C_Register_ICR ((uint8_t)0x1C)
  179. #define I2C_Register_PECR ((uint8_t)0x20)
  180. #define I2C_Register_RXDR ((uint8_t)0x24)
  181. #define I2C_Register_TXDR ((uint8_t)0x28)
  182. #define IS_I2C_REGISTER(REGISTER) (((REGISTER) == I2C_Register_CR1) || \
  183. ((REGISTER) == I2C_Register_CR2) || \
  184. ((REGISTER) == I2C_Register_OAR1) || \
  185. ((REGISTER) == I2C_Register_OAR2) || \
  186. ((REGISTER) == I2C_Register_TIMINGR) || \
  187. ((REGISTER) == I2C_Register_TIMEOUTR) || \
  188. ((REGISTER) == I2C_Register_ISR) || \
  189. ((REGISTER) == I2C_Register_ICR) || \
  190. ((REGISTER) == I2C_Register_PECR) || \
  191. ((REGISTER) == I2C_Register_RXDR) || \
  192. ((REGISTER) == I2C_Register_TXDR))
  193. /**
  194. * @}
  195. */
  196. /** @defgroup I2C_interrupts_definition
  197. * @{
  198. */
  199. #define I2C_IT_ERRI I2C_CR1_ERRIE
  200. #define I2C_IT_TCI I2C_CR1_TCIE
  201. #define I2C_IT_STOPI I2C_CR1_STOPIE
  202. #define I2C_IT_NACKI I2C_CR1_NACKIE
  203. #define I2C_IT_ADDRI I2C_CR1_ADDRIE
  204. #define I2C_IT_RXI I2C_CR1_RXIE
  205. #define I2C_IT_TXI I2C_CR1_TXIE
  206. #define IS_I2C_CONFIG_IT(IT) ((((IT) & (uint32_t)0xFFFFFF01) == 0x00) && ((IT) != 0x00))
  207. /**
  208. * @}
  209. */
  210. /** @defgroup I2C_flags_definition
  211. * @{
  212. */
  213. #define I2C_FLAG_TXE I2C_ISR_TXE
  214. #define I2C_FLAG_TXIS I2C_ISR_TXIS
  215. #define I2C_FLAG_RXNE I2C_ISR_RXNE
  216. #define I2C_FLAG_ADDR I2C_ISR_ADDR
  217. #define I2C_FLAG_NACKF I2C_ISR_NACKF
  218. #define I2C_FLAG_STOPF I2C_ISR_STOPF
  219. #define I2C_FLAG_TC I2C_ISR_TC
  220. #define I2C_FLAG_TCR I2C_ISR_TCR
  221. #define I2C_FLAG_BERR I2C_ISR_BERR
  222. #define I2C_FLAG_ARLO I2C_ISR_ARLO
  223. #define I2C_FLAG_OVR I2C_ISR_OVR
  224. #define I2C_FLAG_PECERR I2C_ISR_PECERR
  225. #define I2C_FLAG_TIMEOUT I2C_ISR_TIMEOUT
  226. #define I2C_FLAG_ALERT I2C_ISR_ALERT
  227. #define I2C_FLAG_BUSY I2C_ISR_BUSY
  228. #define IS_I2C_CLEAR_FLAG(FLAG) ((((FLAG) & (uint32_t)0xFFFF4000) == 0x00) && ((FLAG) != 0x00))
  229. #define IS_I2C_GET_FLAG(FLAG) (((FLAG) == I2C_FLAG_TXE) || ((FLAG) == I2C_FLAG_TXIS) || \
  230. ((FLAG) == I2C_FLAG_RXNE) || ((FLAG) == I2C_FLAG_ADDR) || \
  231. ((FLAG) == I2C_FLAG_NACKF) || ((FLAG) == I2C_FLAG_STOPF) || \
  232. ((FLAG) == I2C_FLAG_TC) || ((FLAG) == I2C_FLAG_TCR) || \
  233. ((FLAG) == I2C_FLAG_BERR) || ((FLAG) == I2C_FLAG_ARLO) || \
  234. ((FLAG) == I2C_FLAG_OVR) || ((FLAG) == I2C_FLAG_PECERR) || \
  235. ((FLAG) == I2C_FLAG_TIMEOUT) || ((FLAG) == I2C_FLAG_ALERT) || \
  236. ((FLAG) == I2C_FLAG_BUSY))
  237. /**
  238. * @}
  239. */
  240. /** @defgroup I2C_interrupts_definition
  241. * @{
  242. */
  243. #define I2C_IT_TXIS I2C_ISR_TXIS
  244. #define I2C_IT_RXNE I2C_ISR_RXNE
  245. #define I2C_IT_ADDR I2C_ISR_ADDR
  246. #define I2C_IT_NACKF I2C_ISR_NACKF
  247. #define I2C_IT_STOPF I2C_ISR_STOPF
  248. #define I2C_IT_TC I2C_ISR_TC
  249. #define I2C_IT_TCR I2C_ISR_TCR
  250. #define I2C_IT_BERR I2C_ISR_BERR
  251. #define I2C_IT_ARLO I2C_ISR_ARLO
  252. #define I2C_IT_OVR I2C_ISR_OVR
  253. #define I2C_IT_PECERR I2C_ISR_PECERR
  254. #define I2C_IT_TIMEOUT I2C_ISR_TIMEOUT
  255. #define I2C_IT_ALERT I2C_ISR_ALERT
  256. #define IS_I2C_CLEAR_IT(IT) ((((IT) & (uint32_t)0xFFFFC001) == 0x00) && ((IT) != 0x00))
  257. #define IS_I2C_GET_IT(IT) (((IT) == I2C_IT_TXIS) || ((IT) == I2C_IT_RXNE) || \
  258. ((IT) == I2C_IT_ADDR) || ((IT) == I2C_IT_NACKF) || \
  259. ((IT) == I2C_IT_STOPF) || ((IT) == I2C_IT_TC) || \
  260. ((IT) == I2C_IT_TCR) || ((IT) == I2C_IT_BERR) || \
  261. ((IT) == I2C_IT_ARLO) || ((IT) == I2C_IT_OVR) || \
  262. ((IT) == I2C_IT_PECERR) || ((IT) == I2C_IT_TIMEOUT) || \
  263. ((IT) == I2C_IT_ALERT))
  264. /**
  265. * @}
  266. */
  267. /** @defgroup I2C_ReloadEndMode_definition
  268. * @{
  269. */
  270. #define I2C_Reload_Mode I2C_CR2_RELOAD
  271. #define I2C_AutoEnd_Mode I2C_CR2_AUTOEND
  272. #define I2C_SoftEnd_Mode ((uint32_t)0x00000000)
  273. #define IS_RELOAD_END_MODE(MODE) (((MODE) == I2C_Reload_Mode) || \
  274. ((MODE) == I2C_AutoEnd_Mode) || \
  275. ((MODE) == I2C_SoftEnd_Mode))
  276. /**
  277. * @}
  278. */
  279. /** @defgroup I2C_StartStopMode_definition
  280. * @{
  281. */
  282. #define I2C_No_StartStop ((uint32_t)0x00000000)
  283. #define I2C_Generate_Stop I2C_CR2_STOP
  284. #define I2C_Generate_Start_Read (uint32_t)(I2C_CR2_START | I2C_CR2_RD_WRN)
  285. #define I2C_Generate_Start_Write I2C_CR2_START
  286. #define IS_START_STOP_MODE(MODE) (((MODE) == I2C_Generate_Stop) || \
  287. ((MODE) == I2C_Generate_Start_Read) || \
  288. ((MODE) == I2C_Generate_Start_Write) || \
  289. ((MODE) == I2C_No_StartStop))
  290. /**
  291. * @}
  292. */
  293. /**
  294. * @}
  295. */
  296. /* Exported macro ------------------------------------------------------------*/
  297. /* Exported functions ------------------------------------------------------- */
  298. /* Initialization and Configuration functions *********************************/
  299. void I2C_DeInit(I2C_TypeDef* I2Cx);
  300. void I2C_Init(I2C_TypeDef* I2Cx, I2C_InitTypeDef* I2C_InitStruct);
  301. void I2C_StructInit(I2C_InitTypeDef* I2C_InitStruct);
  302. void I2C_Cmd(I2C_TypeDef* I2Cx, FunctionalState NewState);
  303. void I2C_SoftwareResetCmd(I2C_TypeDef* I2Cx);
  304. void I2C_ITConfig(I2C_TypeDef* I2Cx, uint32_t I2C_IT, FunctionalState NewState);
  305. void I2C_StretchClockCmd(I2C_TypeDef* I2Cx, FunctionalState NewState);
  306. void I2C_DualAddressCmd(I2C_TypeDef* I2Cx, FunctionalState NewState);
  307. void I2C_OwnAddress2Config(I2C_TypeDef* I2Cx, uint16_t Address, uint8_t Mask);
  308. void I2C_GeneralCallCmd(I2C_TypeDef* I2Cx, FunctionalState NewState);
  309. void I2C_SlaveByteControlCmd(I2C_TypeDef* I2Cx, FunctionalState NewState);
  310. void I2C_SlaveAddressConfig(I2C_TypeDef* I2Cx, uint16_t Address);
  311. void I2C_10BitAddressingModeCmd(I2C_TypeDef* I2Cx, FunctionalState NewState);
  312. /* Communications handling functions ******************************************/
  313. void I2C_AutoEndCmd(I2C_TypeDef* I2Cx, FunctionalState NewState);
  314. void I2C_ReloadCmd(I2C_TypeDef* I2Cx, FunctionalState NewState);
  315. void I2C_NumberOfBytesConfig(I2C_TypeDef* I2Cx, uint8_t Number_Bytes);
  316. void I2C_MasterRequestConfig(I2C_TypeDef* I2Cx, uint16_t I2C_Direction);
  317. void I2C_GenerateSTART(I2C_TypeDef* I2Cx, FunctionalState NewState);
  318. void I2C_GenerateSTOP(I2C_TypeDef* I2Cx, FunctionalState NewState);
  319. void I2C_10BitAddressHeaderCmd(I2C_TypeDef* I2Cx, FunctionalState NewState);
  320. void I2C_AcknowledgeConfig(I2C_TypeDef* I2Cx, FunctionalState NewState);
  321. uint8_t I2C_GetAddressMatched(I2C_TypeDef* I2Cx);
  322. uint16_t I2C_GetTransferDirection(I2C_TypeDef* I2Cx);
  323. void I2C_TransferHandling(I2C_TypeDef* I2Cx, uint16_t Address, uint8_t Number_Bytes, uint32_t ReloadEndMode, uint32_t StartStopMode);
  324. /* SMBUS management functions ************************************************/
  325. void I2C_SMBusAlertCmd(I2C_TypeDef* I2Cx, FunctionalState NewState);
  326. void I2C_ClockTimeoutCmd(I2C_TypeDef* I2Cx, FunctionalState NewState);
  327. void I2C_ExtendedClockTimeoutCmd(I2C_TypeDef* I2Cx, FunctionalState NewState);
  328. void I2C_IdleClockTimeoutCmd(I2C_TypeDef* I2Cx, FunctionalState NewState);
  329. void I2C_TimeoutAConfig(I2C_TypeDef* I2Cx, uint16_t Timeout);
  330. void I2C_TimeoutBConfig(I2C_TypeDef* I2Cx, uint16_t Timeout);
  331. void I2C_CalculatePEC(I2C_TypeDef* I2Cx, FunctionalState NewState);
  332. void I2C_PECRequestCmd(I2C_TypeDef* I2Cx, FunctionalState NewState);
  333. uint8_t I2C_GetPEC(I2C_TypeDef* I2Cx);
  334. /* I2C registers management functions *****************************************/
  335. uint32_t I2C_ReadRegister(I2C_TypeDef* I2Cx, uint8_t I2C_Register);
  336. /* Data transfers management functions ****************************************/
  337. void I2C_SendData(I2C_TypeDef* I2Cx, uint8_t Data);
  338. uint8_t I2C_ReceiveData(I2C_TypeDef* I2Cx);
  339. /* DMA transfers management functions *****************************************/
  340. void I2C_DMACmd(I2C_TypeDef* I2Cx, uint32_t I2C_DMAReq, FunctionalState NewState);
  341. /* Interrupts and flags management functions **********************************/
  342. FlagStatus I2C_GetFlagStatus(I2C_TypeDef* I2Cx, uint32_t I2C_FLAG);
  343. void I2C_ClearFlag(I2C_TypeDef* I2Cx, uint32_t I2C_FLAG);
  344. ITStatus I2C_GetITStatus(I2C_TypeDef* I2Cx, uint32_t I2C_IT);
  345. void I2C_ClearITPendingBit(I2C_TypeDef* I2Cx, uint32_t I2C_IT);
  346. #ifdef __cplusplus
  347. }
  348. #endif
  349. #endif /*__FT32F0XX_I2C_H */
  350. /**
  351. * @}
  352. */
  353. /**
  354. * @}
  355. */
  356. /************************ (C) COPYRIGHT FMD *****END OF FILE****/