stm32f10x_i2c.h 29 KB

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