A bundled STM32F10x Std Periph and CMSIS library
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

stm32f10x_i2c.h 27 KiB

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