stm32f2xx_i2c.h 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690
  1. /**
  2. ******************************************************************************
  3. * @file stm32f2xx_i2c.h
  4. * @author MCD Application Team
  5. * @version V1.1.3
  6. * @date 31-December-2021
  7. * @brief This file contains all the functions prototypes for the I2C firmware
  8. * library.
  9. ******************************************************************************
  10. * @attention
  11. *
  12. * Copyright (c) 2012 STMicroelectronics.
  13. * All rights reserved.
  14. *
  15. * This software is licensed under terms that can be found in the LICENSE file
  16. * in the root directory of this software component.
  17. * If no LICENSE file comes with this software, it is provided AS-IS.
  18. *
  19. ******************************************************************************
  20. */
  21. /* Define to prevent recursive inclusion -------------------------------------*/
  22. #ifndef __STM32F2xx_I2C_H
  23. #define __STM32F2xx_I2C_H
  24. #ifdef __cplusplus
  25. extern "C" {
  26. #endif
  27. /* Includes ------------------------------------------------------------------*/
  28. #include "stm32f2xx.h"
  29. /** @addtogroup STM32F2xx_StdPeriph_Driver
  30. * @{
  31. */
  32. /** @addtogroup I2C
  33. * @{
  34. */
  35. /* Exported types ------------------------------------------------------------*/
  36. /**
  37. * @brief I2C Init structure definition
  38. */
  39. typedef struct
  40. {
  41. uint32_t I2C_ClockSpeed; /*!< Specifies the clock frequency.
  42. This parameter must be set to a value lower than 400kHz */
  43. uint16_t I2C_Mode; /*!< Specifies the I2C mode.
  44. This parameter can be a value of @ref I2C_mode */
  45. uint16_t I2C_DutyCycle; /*!< Specifies the I2C fast mode duty cycle.
  46. This parameter can be a value of @ref I2C_duty_cycle_in_fast_mode */
  47. uint16_t I2C_OwnAddress1; /*!< Specifies the first device own address.
  48. This parameter can be a 7-bit or 10-bit address. */
  49. uint16_t I2C_Ack; /*!< Enables or disables the acknowledgement.
  50. This parameter can be a value of @ref I2C_acknowledgement */
  51. uint16_t I2C_AcknowledgedAddress; /*!< Specifies if 7-bit or 10-bit address is acknowledged.
  52. This parameter can be a value of @ref I2C_acknowledged_address */
  53. }I2C_InitTypeDef;
  54. /* Exported constants --------------------------------------------------------*/
  55. /** @defgroup I2C_Exported_Constants
  56. * @{
  57. */
  58. #define IS_I2C_ALL_PERIPH(PERIPH) (((PERIPH) == I2C1) || \
  59. ((PERIPH) == I2C2) || \
  60. ((PERIPH) == I2C3))
  61. /** @defgroup I2C_mode
  62. * @{
  63. */
  64. #define I2C_Mode_I2C ((uint16_t)0x0000)
  65. #define I2C_Mode_SMBusDevice ((uint16_t)0x0002)
  66. #define I2C_Mode_SMBusHost ((uint16_t)0x000A)
  67. #define IS_I2C_MODE(MODE) (((MODE) == I2C_Mode_I2C) || \
  68. ((MODE) == I2C_Mode_SMBusDevice) || \
  69. ((MODE) == I2C_Mode_SMBusHost))
  70. /**
  71. * @}
  72. */
  73. /** @defgroup I2C_duty_cycle_in_fast_mode
  74. * @{
  75. */
  76. #define I2C_DutyCycle_16_9 ((uint16_t)0x4000) /*!< I2C fast mode Tlow/Thigh = 16/9 */
  77. #define I2C_DutyCycle_2 ((uint16_t)0xBFFF) /*!< I2C fast mode Tlow/Thigh = 2 */
  78. #define IS_I2C_DUTY_CYCLE(CYCLE) (((CYCLE) == I2C_DutyCycle_16_9) || \
  79. ((CYCLE) == I2C_DutyCycle_2))
  80. /**
  81. * @}
  82. */
  83. /** @defgroup I2C_acknowledgement
  84. * @{
  85. */
  86. #define I2C_Ack_Enable ((uint16_t)0x0400)
  87. #define I2C_Ack_Disable ((uint16_t)0x0000)
  88. #define IS_I2C_ACK_STATE(STATE) (((STATE) == I2C_Ack_Enable) || \
  89. ((STATE) == I2C_Ack_Disable))
  90. /**
  91. * @}
  92. */
  93. /** @defgroup I2C_transfer_direction
  94. * @{
  95. */
  96. #define I2C_Direction_Transmitter ((uint8_t)0x00)
  97. #define I2C_Direction_Receiver ((uint8_t)0x01)
  98. #define IS_I2C_DIRECTION(DIRECTION) (((DIRECTION) == I2C_Direction_Transmitter) || \
  99. ((DIRECTION) == I2C_Direction_Receiver))
  100. /**
  101. * @}
  102. */
  103. /** @defgroup I2C_acknowledged_address
  104. * @{
  105. */
  106. #define I2C_AcknowledgedAddress_7bit ((uint16_t)0x4000)
  107. #define I2C_AcknowledgedAddress_10bit ((uint16_t)0xC000)
  108. #define IS_I2C_ACKNOWLEDGE_ADDRESS(ADDRESS) (((ADDRESS) == I2C_AcknowledgedAddress_7bit) || \
  109. ((ADDRESS) == I2C_AcknowledgedAddress_10bit))
  110. /**
  111. * @}
  112. */
  113. /** @defgroup I2C_registers
  114. * @{
  115. */
  116. #define I2C_Register_CR1 ((uint8_t)0x00)
  117. #define I2C_Register_CR2 ((uint8_t)0x04)
  118. #define I2C_Register_OAR1 ((uint8_t)0x08)
  119. #define I2C_Register_OAR2 ((uint8_t)0x0C)
  120. #define I2C_Register_DR ((uint8_t)0x10)
  121. #define I2C_Register_SR1 ((uint8_t)0x14)
  122. #define I2C_Register_SR2 ((uint8_t)0x18)
  123. #define I2C_Register_CCR ((uint8_t)0x1C)
  124. #define I2C_Register_TRISE ((uint8_t)0x20)
  125. #define IS_I2C_REGISTER(REGISTER) (((REGISTER) == I2C_Register_CR1) || \
  126. ((REGISTER) == I2C_Register_CR2) || \
  127. ((REGISTER) == I2C_Register_OAR1) || \
  128. ((REGISTER) == I2C_Register_OAR2) || \
  129. ((REGISTER) == I2C_Register_DR) || \
  130. ((REGISTER) == I2C_Register_SR1) || \
  131. ((REGISTER) == I2C_Register_SR2) || \
  132. ((REGISTER) == I2C_Register_CCR) || \
  133. ((REGISTER) == I2C_Register_TRISE))
  134. /**
  135. * @}
  136. */
  137. /** @defgroup I2C_NACK_position
  138. * @{
  139. */
  140. #define I2C_NACKPosition_Next ((uint16_t)0x0800)
  141. #define I2C_NACKPosition_Current ((uint16_t)0xF7FF)
  142. #define IS_I2C_NACK_POSITION(POSITION) (((POSITION) == I2C_NACKPosition_Next) || \
  143. ((POSITION) == I2C_NACKPosition_Current))
  144. /**
  145. * @}
  146. */
  147. /** @defgroup I2C_SMBus_alert_pin_level
  148. * @{
  149. */
  150. #define I2C_SMBusAlert_Low ((uint16_t)0x2000)
  151. #define I2C_SMBusAlert_High ((uint16_t)0xDFFF)
  152. #define IS_I2C_SMBUS_ALERT(ALERT) (((ALERT) == I2C_SMBusAlert_Low) || \
  153. ((ALERT) == I2C_SMBusAlert_High))
  154. /**
  155. * @}
  156. */
  157. /** @defgroup I2C_PEC_position
  158. * @{
  159. */
  160. #define I2C_PECPosition_Next ((uint16_t)0x0800)
  161. #define I2C_PECPosition_Current ((uint16_t)0xF7FF)
  162. #define IS_I2C_PEC_POSITION(POSITION) (((POSITION) == I2C_PECPosition_Next) || \
  163. ((POSITION) == I2C_PECPosition_Current))
  164. /**
  165. * @}
  166. */
  167. /** @defgroup I2C_interrupts_definition
  168. * @{
  169. */
  170. #define I2C_IT_BUF ((uint16_t)0x0400)
  171. #define I2C_IT_EVT ((uint16_t)0x0200)
  172. #define I2C_IT_ERR ((uint16_t)0x0100)
  173. #define IS_I2C_CONFIG_IT(IT) ((((IT) & (uint16_t)0xF8FF) == 0x00) && ((IT) != 0x00))
  174. /**
  175. * @}
  176. */
  177. /** @defgroup I2C_interrupts_definition
  178. * @{
  179. */
  180. #define I2C_IT_SMBALERT ((uint32_t)0x01008000)
  181. #define I2C_IT_TIMEOUT ((uint32_t)0x01004000)
  182. #define I2C_IT_PECERR ((uint32_t)0x01001000)
  183. #define I2C_IT_OVR ((uint32_t)0x01000800)
  184. #define I2C_IT_AF ((uint32_t)0x01000400)
  185. #define I2C_IT_ARLO ((uint32_t)0x01000200)
  186. #define I2C_IT_BERR ((uint32_t)0x01000100)
  187. #define I2C_IT_TXE ((uint32_t)0x06000080)
  188. #define I2C_IT_RXNE ((uint32_t)0x06000040)
  189. #define I2C_IT_STOPF ((uint32_t)0x02000010)
  190. #define I2C_IT_ADD10 ((uint32_t)0x02000008)
  191. #define I2C_IT_BTF ((uint32_t)0x02000004)
  192. #define I2C_IT_ADDR ((uint32_t)0x02000002)
  193. #define I2C_IT_SB ((uint32_t)0x02000001)
  194. #define IS_I2C_CLEAR_IT(IT) ((((IT) & (uint16_t)0x20FF) == 0x00) && ((IT) != (uint16_t)0x00))
  195. #define IS_I2C_GET_IT(IT) (((IT) == I2C_IT_SMBALERT) || ((IT) == I2C_IT_TIMEOUT) || \
  196. ((IT) == I2C_IT_PECERR) || ((IT) == I2C_IT_OVR) || \
  197. ((IT) == I2C_IT_AF) || ((IT) == I2C_IT_ARLO) || \
  198. ((IT) == I2C_IT_BERR) || ((IT) == I2C_IT_TXE) || \
  199. ((IT) == I2C_IT_RXNE) || ((IT) == I2C_IT_STOPF) || \
  200. ((IT) == I2C_IT_ADD10) || ((IT) == I2C_IT_BTF) || \
  201. ((IT) == I2C_IT_ADDR) || ((IT) == I2C_IT_SB))
  202. /**
  203. * @}
  204. */
  205. /** @defgroup I2C_flags_definition
  206. * @{
  207. */
  208. /**
  209. * @brief SR2 register flags
  210. */
  211. #define I2C_FLAG_DUALF ((uint32_t)0x00800000)
  212. #define I2C_FLAG_SMBHOST ((uint32_t)0x00400000)
  213. #define I2C_FLAG_SMBDEFAULT ((uint32_t)0x00200000)
  214. #define I2C_FLAG_GENCALL ((uint32_t)0x00100000)
  215. #define I2C_FLAG_TRA ((uint32_t)0x00040000)
  216. #define I2C_FLAG_BUSY ((uint32_t)0x00020000)
  217. #define I2C_FLAG_MSL ((uint32_t)0x00010000)
  218. /**
  219. * @brief SR1 register flags
  220. */
  221. #define I2C_FLAG_SMBALERT ((uint32_t)0x10008000)
  222. #define I2C_FLAG_TIMEOUT ((uint32_t)0x10004000)
  223. #define I2C_FLAG_PECERR ((uint32_t)0x10001000)
  224. #define I2C_FLAG_OVR ((uint32_t)0x10000800)
  225. #define I2C_FLAG_AF ((uint32_t)0x10000400)
  226. #define I2C_FLAG_ARLO ((uint32_t)0x10000200)
  227. #define I2C_FLAG_BERR ((uint32_t)0x10000100)
  228. #define I2C_FLAG_TXE ((uint32_t)0x10000080)
  229. #define I2C_FLAG_RXNE ((uint32_t)0x10000040)
  230. #define I2C_FLAG_STOPF ((uint32_t)0x10000010)
  231. #define I2C_FLAG_ADD10 ((uint32_t)0x10000008)
  232. #define I2C_FLAG_BTF ((uint32_t)0x10000004)
  233. #define I2C_FLAG_ADDR ((uint32_t)0x10000002)
  234. #define I2C_FLAG_SB ((uint32_t)0x10000001)
  235. #define IS_I2C_CLEAR_FLAG(FLAG) ((((FLAG) & (uint16_t)0x20FF) == 0x00) && ((FLAG) != (uint16_t)0x00))
  236. #define IS_I2C_GET_FLAG(FLAG) (((FLAG) == I2C_FLAG_DUALF) || ((FLAG) == I2C_FLAG_SMBHOST) || \
  237. ((FLAG) == I2C_FLAG_SMBDEFAULT) || ((FLAG) == I2C_FLAG_GENCALL) || \
  238. ((FLAG) == I2C_FLAG_TRA) || ((FLAG) == I2C_FLAG_BUSY) || \
  239. ((FLAG) == I2C_FLAG_MSL) || ((FLAG) == I2C_FLAG_SMBALERT) || \
  240. ((FLAG) == I2C_FLAG_TIMEOUT) || ((FLAG) == I2C_FLAG_PECERR) || \
  241. ((FLAG) == I2C_FLAG_OVR) || ((FLAG) == I2C_FLAG_AF) || \
  242. ((FLAG) == I2C_FLAG_ARLO) || ((FLAG) == I2C_FLAG_BERR) || \
  243. ((FLAG) == I2C_FLAG_TXE) || ((FLAG) == I2C_FLAG_RXNE) || \
  244. ((FLAG) == I2C_FLAG_STOPF) || ((FLAG) == I2C_FLAG_ADD10) || \
  245. ((FLAG) == I2C_FLAG_BTF) || ((FLAG) == I2C_FLAG_ADDR) || \
  246. ((FLAG) == I2C_FLAG_SB))
  247. /**
  248. * @}
  249. */
  250. /** @defgroup I2C_Events
  251. * @{
  252. */
  253. /**
  254. ===============================================================================
  255. I2C Master Events (Events grouped in order of communication)
  256. ===============================================================================
  257. */
  258. /**
  259. * @brief Communication start
  260. *
  261. * After sending the START condition (I2C_GenerateSTART() function) the master
  262. * has to wait for this event. It means that the Start condition has been correctly
  263. * released on the I2C bus (the bus is free, no other devices is communicating).
  264. *
  265. */
  266. /* --EV5 */
  267. #define I2C_EVENT_MASTER_MODE_SELECT ((uint32_t)0x00030001) /* BUSY, MSL and SB flag */
  268. /**
  269. * @brief Address Acknowledge
  270. *
  271. * After checking on EV5 (start condition correctly released on the bus), the
  272. * master sends the address of the slave(s) with which it will communicate
  273. * (I2C_Send7bitAddress() function, it also determines the direction of the communication:
  274. * Master transmitter or Receiver). Then the master has to wait that a slave acknowledges
  275. * his address. If an acknowledge is sent on the bus, one of the following events will
  276. * be set:
  277. *
  278. * 1) In case of Master Receiver (7-bit addressing): the I2C_EVENT_MASTER_RECEIVER_MODE_SELECTED
  279. * event is set.
  280. *
  281. * 2) In case of Master Transmitter (7-bit addressing): the I2C_EVENT_MASTER_TRANSMITTER_MODE_SELECTED
  282. * is set
  283. *
  284. * 3) In case of 10-Bit addressing mode, the master (just after generating the START
  285. * and checking on EV5) has to send the header of 10-bit addressing mode (I2C_SendData()
  286. * function). Then master should wait on EV9. It means that the 10-bit addressing
  287. * header has been correctly sent on the bus. Then master should send the second part of
  288. * the 10-bit address (LSB) using the function I2C_Send7bitAddress(). Then master
  289. * should wait for event EV6.
  290. *
  291. */
  292. /* --EV6 */
  293. #define I2C_EVENT_MASTER_TRANSMITTER_MODE_SELECTED ((uint32_t)0x00070082) /* BUSY, MSL, ADDR, TXE and TRA flags */
  294. #define I2C_EVENT_MASTER_RECEIVER_MODE_SELECTED ((uint32_t)0x00030002) /* BUSY, MSL and ADDR flags */
  295. /* --EV9 */
  296. #define I2C_EVENT_MASTER_MODE_ADDRESS10 ((uint32_t)0x00030008) /* BUSY, MSL and ADD10 flags */
  297. /**
  298. * @brief Communication events
  299. *
  300. * If a communication is established (START condition generated and slave address
  301. * acknowledged) then the master has to check on one of the following events for
  302. * communication procedures:
  303. *
  304. * 1) Master Receiver mode: The master has to wait on the event EV7 then to read
  305. * the data received from the slave (I2C_ReceiveData() function).
  306. *
  307. * 2) Master Transmitter mode: The master has to send data (I2C_SendData()
  308. * function) then to wait on event EV8 or EV8_2.
  309. * These two events are similar:
  310. * - EV8 means that the data has been written in the data register and is
  311. * being shifted out.
  312. * - EV8_2 means that the data has been physically shifted out and output
  313. * on the bus.
  314. * In most cases, using EV8 is sufficient for the application.
  315. * Using EV8_2 leads to a slower communication but ensure more reliable test.
  316. * EV8_2 is also more suitable than EV8 for testing on the last data transmission
  317. * (before Stop condition generation).
  318. *
  319. * @note In case the user software does not guarantee that this event EV7 is
  320. * managed before the current byte end of transfer, then user may check on EV7
  321. * and BTF flag at the same time (ie. (I2C_EVENT_MASTER_BYTE_RECEIVED | I2C_FLAG_BTF)).
  322. * In this case the communication may be slower.
  323. *
  324. */
  325. /* Master RECEIVER mode -----------------------------*/
  326. /* --EV7 */
  327. #define I2C_EVENT_MASTER_BYTE_RECEIVED ((uint32_t)0x00030040) /* BUSY, MSL and RXNE flags */
  328. /* Master TRANSMITTER mode --------------------------*/
  329. /* --EV8 */
  330. #define I2C_EVENT_MASTER_BYTE_TRANSMITTING ((uint32_t)0x00070080) /* TRA, BUSY, MSL, TXE flags */
  331. /* --EV8_2 */
  332. #define I2C_EVENT_MASTER_BYTE_TRANSMITTED ((uint32_t)0x00070084) /* TRA, BUSY, MSL, TXE and BTF flags */
  333. /**
  334. ===============================================================================
  335. I2C Slave Events (Events grouped in order of communication)
  336. ===============================================================================
  337. */
  338. /**
  339. * @brief Communication start events
  340. *
  341. * Wait on one of these events at the start of the communication. It means that
  342. * the I2C peripheral detected a Start condition on the bus (generated by master
  343. * device) followed by the peripheral address. The peripheral generates an ACK
  344. * condition on the bus (if the acknowledge feature is enabled through function
  345. * I2C_AcknowledgeConfig()) and the events listed above are set :
  346. *
  347. * 1) In normal case (only one address managed by the slave), when the address
  348. * sent by the master matches the own address of the peripheral (configured by
  349. * I2C_OwnAddress1 field) the I2C_EVENT_SLAVE_XXX_ADDRESS_MATCHED event is set
  350. * (where XXX could be TRANSMITTER or RECEIVER).
  351. *
  352. * 2) In case the address sent by the master matches the second address of the
  353. * peripheral (configured by the function I2C_OwnAddress2Config() and enabled
  354. * by the function I2C_DualAddressCmd()) the events I2C_EVENT_SLAVE_XXX_SECONDADDRESS_MATCHED
  355. * (where XXX could be TRANSMITTER or RECEIVER) are set.
  356. *
  357. * 3) In case the address sent by the master is General Call (address 0x00) and
  358. * if the General Call is enabled for the peripheral (using function I2C_GeneralCallCmd())
  359. * the following event is set I2C_EVENT_SLAVE_GENERALCALLADDRESS_MATCHED.
  360. *
  361. */
  362. /* --EV1 (all the events below are variants of EV1) */
  363. /* 1) Case of One Single Address managed by the slave */
  364. #define I2C_EVENT_SLAVE_RECEIVER_ADDRESS_MATCHED ((uint32_t)0x00020002) /* BUSY and ADDR flags */
  365. #define I2C_EVENT_SLAVE_TRANSMITTER_ADDRESS_MATCHED ((uint32_t)0x00060082) /* TRA, BUSY, TXE and ADDR flags */
  366. /* 2) Case of Dual address managed by the slave */
  367. #define I2C_EVENT_SLAVE_RECEIVER_SECONDADDRESS_MATCHED ((uint32_t)0x00820000) /* DUALF and BUSY flags */
  368. #define I2C_EVENT_SLAVE_TRANSMITTER_SECONDADDRESS_MATCHED ((uint32_t)0x00860080) /* DUALF, TRA, BUSY and TXE flags */
  369. /* 3) Case of General Call enabled for the slave */
  370. #define I2C_EVENT_SLAVE_GENERALCALLADDRESS_MATCHED ((uint32_t)0x00120000) /* GENCALL and BUSY flags */
  371. /**
  372. * @brief Communication events
  373. *
  374. * Wait on one of these events when EV1 has already been checked and:
  375. *
  376. * - Slave RECEIVER mode:
  377. * - EV2: When the application is expecting a data byte to be received.
  378. * - EV4: When the application is expecting the end of the communication: master
  379. * sends a stop condition and data transmission is stopped.
  380. *
  381. * - Slave Transmitter mode:
  382. * - EV3: When a byte has been transmitted by the slave and the application is expecting
  383. * the end of the byte transmission. The two events I2C_EVENT_SLAVE_BYTE_TRANSMITTED and
  384. * I2C_EVENT_SLAVE_BYTE_TRANSMITTING are similar. The second one can optionally be
  385. * used when the user software doesn't guarantee the EV3 is managed before the
  386. * current byte end of transfer.
  387. * - EV3_2: When the master sends a NACK in order to tell slave that data transmission
  388. * shall end (before sending the STOP condition). In this case slave has to stop sending
  389. * data bytes and expect a Stop condition on the bus.
  390. *
  391. * @note In case the user software does not guarantee that the event EV2 is
  392. * managed before the current byte end of transfer, then user may check on EV2
  393. * and BTF flag at the same time (ie. (I2C_EVENT_SLAVE_BYTE_RECEIVED | I2C_FLAG_BTF)).
  394. * In this case the communication may be slower.
  395. *
  396. */
  397. /* Slave RECEIVER mode --------------------------*/
  398. /* --EV2 */
  399. #define I2C_EVENT_SLAVE_BYTE_RECEIVED ((uint32_t)0x00020040) /* BUSY and RXNE flags */
  400. /* --EV4 */
  401. #define I2C_EVENT_SLAVE_STOP_DETECTED ((uint32_t)0x00000010) /* STOPF flag */
  402. /* Slave TRANSMITTER mode -----------------------*/
  403. /* --EV3 */
  404. #define I2C_EVENT_SLAVE_BYTE_TRANSMITTED ((uint32_t)0x00060084) /* TRA, BUSY, TXE and BTF flags */
  405. #define I2C_EVENT_SLAVE_BYTE_TRANSMITTING ((uint32_t)0x00060080) /* TRA, BUSY and TXE flags */
  406. /* --EV3_2 */
  407. #define I2C_EVENT_SLAVE_ACK_FAILURE ((uint32_t)0x00000400) /* AF flag */
  408. /*
  409. ===============================================================================
  410. End of Events Description
  411. ===============================================================================
  412. */
  413. #define IS_I2C_EVENT(EVENT) (((EVENT) == I2C_EVENT_SLAVE_TRANSMITTER_ADDRESS_MATCHED) || \
  414. ((EVENT) == I2C_EVENT_SLAVE_RECEIVER_ADDRESS_MATCHED) || \
  415. ((EVENT) == I2C_EVENT_SLAVE_TRANSMITTER_SECONDADDRESS_MATCHED) || \
  416. ((EVENT) == I2C_EVENT_SLAVE_RECEIVER_SECONDADDRESS_MATCHED) || \
  417. ((EVENT) == I2C_EVENT_SLAVE_GENERALCALLADDRESS_MATCHED) || \
  418. ((EVENT) == I2C_EVENT_SLAVE_BYTE_RECEIVED) || \
  419. ((EVENT) == (I2C_EVENT_SLAVE_BYTE_RECEIVED | I2C_FLAG_DUALF)) || \
  420. ((EVENT) == (I2C_EVENT_SLAVE_BYTE_RECEIVED | I2C_FLAG_GENCALL)) || \
  421. ((EVENT) == I2C_EVENT_SLAVE_BYTE_TRANSMITTED) || \
  422. ((EVENT) == (I2C_EVENT_SLAVE_BYTE_TRANSMITTED | I2C_FLAG_DUALF)) || \
  423. ((EVENT) == (I2C_EVENT_SLAVE_BYTE_TRANSMITTED | I2C_FLAG_GENCALL)) || \
  424. ((EVENT) == I2C_EVENT_SLAVE_STOP_DETECTED) || \
  425. ((EVENT) == I2C_EVENT_MASTER_MODE_SELECT) || \
  426. ((EVENT) == I2C_EVENT_MASTER_TRANSMITTER_MODE_SELECTED) || \
  427. ((EVENT) == I2C_EVENT_MASTER_RECEIVER_MODE_SELECTED) || \
  428. ((EVENT) == I2C_EVENT_MASTER_BYTE_RECEIVED) || \
  429. ((EVENT) == I2C_EVENT_MASTER_BYTE_TRANSMITTED) || \
  430. ((EVENT) == I2C_EVENT_MASTER_BYTE_TRANSMITTING) || \
  431. ((EVENT) == I2C_EVENT_MASTER_MODE_ADDRESS10) || \
  432. ((EVENT) == I2C_EVENT_SLAVE_ACK_FAILURE))
  433. /**
  434. * @}
  435. */
  436. /** @defgroup I2C_own_address1
  437. * @{
  438. */
  439. #define IS_I2C_OWN_ADDRESS1(ADDRESS1) ((ADDRESS1) <= 0x3FF)
  440. /**
  441. * @}
  442. */
  443. /** @defgroup I2C_clock_speed
  444. * @{
  445. */
  446. #define IS_I2C_CLOCK_SPEED(SPEED) (((SPEED) >= 0x1) && ((SPEED) <= 400000))
  447. /**
  448. * @}
  449. */
  450. /**
  451. * @}
  452. */
  453. /* Exported macro ------------------------------------------------------------*/
  454. /* Exported functions --------------------------------------------------------*/
  455. /* Function used to set the I2C configuration to the default reset state *****/
  456. void I2C_DeInit(I2C_TypeDef* I2Cx);
  457. /* Initialization and Configuration functions *********************************/
  458. void I2C_Init(I2C_TypeDef* I2Cx, I2C_InitTypeDef* I2C_InitStruct);
  459. void I2C_StructInit(I2C_InitTypeDef* I2C_InitStruct);
  460. void I2C_Cmd(I2C_TypeDef* I2Cx, FunctionalState NewState);
  461. void I2C_GenerateSTART(I2C_TypeDef* I2Cx, FunctionalState NewState);
  462. void I2C_GenerateSTOP(I2C_TypeDef* I2Cx, FunctionalState NewState);
  463. void I2C_Send7bitAddress(I2C_TypeDef* I2Cx, uint8_t Address, uint8_t I2C_Direction);
  464. void I2C_AcknowledgeConfig(I2C_TypeDef* I2Cx, FunctionalState NewState);
  465. void I2C_OwnAddress2Config(I2C_TypeDef* I2Cx, uint8_t Address);
  466. void I2C_DualAddressCmd(I2C_TypeDef* I2Cx, FunctionalState NewState);
  467. void I2C_GeneralCallCmd(I2C_TypeDef* I2Cx, FunctionalState NewState);
  468. void I2C_SoftwareResetCmd(I2C_TypeDef* I2Cx, FunctionalState NewState);
  469. void I2C_StretchClockCmd(I2C_TypeDef* I2Cx, FunctionalState NewState);
  470. void I2C_FastModeDutyCycleConfig(I2C_TypeDef* I2Cx, uint16_t I2C_DutyCycle);
  471. void I2C_NACKPositionConfig(I2C_TypeDef* I2Cx, uint16_t I2C_NACKPosition);
  472. void I2C_SMBusAlertConfig(I2C_TypeDef* I2Cx, uint16_t I2C_SMBusAlert);
  473. void I2C_ARPCmd(I2C_TypeDef* I2Cx, FunctionalState NewState);
  474. /* Data transfers functions ***************************************************/
  475. void I2C_SendData(I2C_TypeDef* I2Cx, uint8_t Data);
  476. uint8_t I2C_ReceiveData(I2C_TypeDef* I2Cx);
  477. /* PEC management functions ***************************************************/
  478. void I2C_TransmitPEC(I2C_TypeDef* I2Cx, FunctionalState NewState);
  479. void I2C_PECPositionConfig(I2C_TypeDef* I2Cx, uint16_t I2C_PECPosition);
  480. void I2C_CalculatePEC(I2C_TypeDef* I2Cx, FunctionalState NewState);
  481. uint8_t I2C_GetPEC(I2C_TypeDef* I2Cx);
  482. /* DMA transfers management functions *****************************************/
  483. void I2C_DMACmd(I2C_TypeDef* I2Cx, FunctionalState NewState);
  484. void I2C_DMALastTransferCmd(I2C_TypeDef* I2Cx, FunctionalState NewState);
  485. /* Interrupts, events and flags management functions **************************/
  486. uint16_t I2C_ReadRegister(I2C_TypeDef* I2Cx, uint8_t I2C_Register);
  487. void I2C_ITConfig(I2C_TypeDef* I2Cx, uint16_t I2C_IT, FunctionalState NewState);
  488. /*
  489. ===============================================================================
  490. I2C State Monitoring Functions
  491. ===============================================================================
  492. This I2C driver provides three different ways for I2C state monitoring
  493. depending on the application requirements and constraints:
  494. 1. Basic state monitoring (Using I2C_CheckEvent() function)
  495. -----------------------------------------------------------
  496. It compares the status registers (SR1 and SR2) content to a given event
  497. (can be the combination of one or more flags).
  498. It returns SUCCESS if the current status includes the given flags
  499. and returns ERROR if one or more flags are missing in the current status.
  500. - When to use
  501. - This function is suitable for most applications as well as for startup
  502. activity since the events are fully described in the product reference
  503. manual (RM0033).
  504. - It is also suitable for users who need to define their own events.
  505. - Limitations
  506. - If an error occurs (ie. error flags are set besides to the monitored
  507. flags), the I2C_CheckEvent() function may return SUCCESS despite
  508. the communication hold or corrupted real state.
  509. In this case, it is advised to use error interrupts to monitor
  510. the error events and handle them in the interrupt IRQ handler.
  511. Note
  512. For error management, it is advised to use the following functions:
  513. - I2C_ITConfig() to configure and enable the error interrupts (I2C_IT_ERR).
  514. - I2Cx_ER_IRQHandler() which is called when the error interrupt occurs.
  515. Where x is the peripheral instance (I2C1, I2C2 ...)
  516. - I2C_GetFlagStatus() or I2C_GetITStatus() to be called into the
  517. I2Cx_ER_IRQHandler() function in order to determine which error occurred.
  518. - I2C_ClearFlag() or I2C_ClearITPendingBit() and/or I2C_SoftwareResetCmd()
  519. and/or I2C_GenerateStop() in order to clear the error flag and source
  520. and return to correct communication status.
  521. 2. Advanced state monitoring (Using the function I2C_GetLastEvent())
  522. --------------------------------------------------------------------
  523. Using the function I2C_GetLastEvent() which returns the image of both status
  524. registers in a single word (uint32_t) (Status Register 2 value is shifted left
  525. by 16 bits and concatenated to Status Register 1).
  526. - When to use
  527. - This function is suitable for the same applications above but it
  528. allows to overcome the mentioned limitation of I2C_GetFlagStatus()
  529. function.
  530. - The returned value could be compared to events already defined in
  531. this file or to custom values defined by user.
  532. This function is suitable when multiple flags are monitored at the
  533. same time.
  534. - At the opposite of I2C_CheckEvent() function, this function allows
  535. user to choose when an event is accepted (when all events flags are
  536. set and no other flags are set or just when the needed flags are set
  537. like I2C_CheckEvent() function.
  538. - Limitations
  539. - User may need to define his own events.
  540. - Same remark concerning the error management is applicable for this
  541. function if user decides to check only regular communication flags
  542. (and ignores error flags).
  543. 3. Flag-based state monitoring (Using the function I2C_GetFlagStatus())
  544. -----------------------------------------------------------------------
  545. Using the function I2C_GetFlagStatus() which simply returns the status of
  546. one single flag (ie. I2C_FLAG_RXNE ...).
  547. - When to use
  548. - This function could be used for specific applications or in debug
  549. phase.
  550. - It is suitable when only one flag checking is needed (most I2C
  551. events are monitored through multiple flags).
  552. - Limitations:
  553. - When calling this function, the Status register is accessed.
  554. Some flags are cleared when the status register is accessed.
  555. So checking the status of one Flag, may clear other ones.
  556. - Function may need to be called twice or more in order to monitor
  557. one single event.
  558. */
  559. /*
  560. ===============================================================================
  561. 1. Basic state monitoring
  562. ===============================================================================
  563. */
  564. ErrorStatus I2C_CheckEvent(I2C_TypeDef* I2Cx, uint32_t I2C_EVENT);
  565. /*
  566. ===============================================================================
  567. 2. Advanced state monitoring
  568. ===============================================================================
  569. */
  570. uint32_t I2C_GetLastEvent(I2C_TypeDef* I2Cx);
  571. /*
  572. ===============================================================================
  573. 3. Flag-based state monitoring
  574. ===============================================================================
  575. */
  576. FlagStatus I2C_GetFlagStatus(I2C_TypeDef* I2Cx, uint32_t I2C_FLAG);
  577. void I2C_ClearFlag(I2C_TypeDef* I2Cx, uint32_t I2C_FLAG);
  578. ITStatus I2C_GetITStatus(I2C_TypeDef* I2Cx, uint32_t I2C_IT);
  579. void I2C_ClearITPendingBit(I2C_TypeDef* I2Cx, uint32_t I2C_IT);
  580. #ifdef __cplusplus
  581. }
  582. #endif
  583. #endif /*__STM32F2xx_I2C_H */
  584. /**
  585. * @}
  586. */
  587. /**
  588. * @}
  589. */