stm8l15x_i2c.h 34 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809
  1. /**
  2. ******************************************************************************
  3. * @file stm8l15x_i2c.h
  4. * @author MCD Application Team
  5. * @version V1.6.1
  6. * @date 30-September-2014
  7. * @brief This file contains all the functions prototypes for the I2C firmware
  8. * library.
  9. ******************************************************************************
  10. * @attention
  11. *
  12. * <h2><center>&copy; COPYRIGHT 2014 STMicroelectronics</center></h2>
  13. *
  14. * Licensed under MCD-ST Liberty SW License Agreement V2, (the "License");
  15. * You may not use this file except in compliance with the License.
  16. * You may obtain a copy of the License at:
  17. *
  18. * http://www.st.com/software_license_agreement_liberty_v2
  19. *
  20. * Unless required by applicable law or agreed to in writing, software
  21. * distributed under the License is distributed on an "AS IS" BASIS,
  22. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  23. * See the License for the specific language governing permissions and
  24. * limitations under the License.
  25. *
  26. ******************************************************************************
  27. */
  28. /* Define to prevent recursive inclusion -------------------------------------*/
  29. #ifndef __STM8L15x_I2C_H
  30. #define __STM8L15x_I2C_H
  31. /* Includes ------------------------------------------------------------------*/
  32. #include "stm8l15x.h"
  33. /** @addtogroup STM8L15x_StdPeriph_Driver
  34. * @{
  35. */
  36. /** @addtogroup I2C
  37. * @{
  38. */
  39. /* Exported types ------------------------------------------------------------*/
  40. /** @defgroup I2C_Exported_Types
  41. * @{
  42. */
  43. /** @defgroup I2C_mode
  44. * @{
  45. */
  46. typedef enum
  47. {
  48. I2C_Mode_I2C = (uint8_t)0x00, /*!< I2C mode */
  49. I2C_Mode_SMBusDevice = (uint8_t)0x02, /*!< SMBus Device mode */
  50. I2C_Mode_SMBusHost = (uint8_t)0x0A /*!< SMBus Host mode */
  51. } I2C_Mode_TypeDef;
  52. /**
  53. * @}
  54. */
  55. /** @defgroup I2C_duty_cycle_in_fast_mode
  56. * @{
  57. */
  58. typedef enum
  59. {
  60. I2C_DutyCycle_2 = (uint8_t)0x00, /*!< Fast mode Tlow/THigh = 2 */
  61. I2C_DutyCycle_16_9 = (uint8_t)0x40 /*!< Fast mode Tlow/Thigh = 16/9 */
  62. } I2C_DutyCycle_TypeDef;
  63. /**
  64. * @}
  65. */
  66. /** @defgroup I2C_acknowledgement
  67. * @{
  68. */
  69. typedef enum
  70. {
  71. I2C_Ack_Disable = (uint8_t)0x00, /*!< No acknowledge */
  72. I2C_Ack_Enable = (uint8_t)0x04 /*!< Acknowledge Enabled */
  73. } I2C_Ack_TypeDef;
  74. /**
  75. * @}
  76. */
  77. /** @defgroup I2C_Position_Acknowledgement
  78. * @{
  79. */
  80. typedef enum
  81. {
  82. I2C_AckPosition_Current = (uint8_t)0x00, /*!< Acknowledge on the current byte */
  83. I2C_AckPosition_Next = (uint8_t)0x08 /*!< Acknowledge on the next byte */
  84. } I2C_AckPosition_TypeDef;
  85. /**
  86. * @}
  87. */
  88. /** @defgroup I2C_acknowledged_address
  89. * @{
  90. */
  91. typedef enum
  92. {
  93. I2C_AcknowledgedAddress_7bit = (uint8_t)0x00, /*!< 7-bit slave address (10-bit address not acknowledged) */
  94. I2C_AcknowledgedAddress_10bit = (uint8_t)0x80 /*!< 10-bit slave address (7-bit address not acknowledged) */
  95. } I2C_AcknowledgedAddress_TypeDef;
  96. /**
  97. * @}
  98. */
  99. /** @defgroup I2C_transfer_direction
  100. * @{
  101. */
  102. /**
  103. * Warning: the values correspond to the ADD0 bit position in the OARL register
  104. */
  105. typedef enum
  106. {
  107. I2C_Direction_Transmitter = (uint8_t)0x00, /*!< Transmission direction */
  108. I2C_Direction_Receiver = (uint8_t)0x01 /*!< Reception direction */
  109. } I2C_Direction_TypeDef;
  110. /**
  111. * @}
  112. */
  113. /** @defgroup I2C_SMBus_alert_pin_level
  114. * @{
  115. */
  116. typedef enum
  117. {
  118. I2C_SMBusAlert_High = (uint8_t)0x00, /*!< SMBAlert pin high */
  119. I2C_SMBusAlert_Low = (uint8_t)0x01 /*!< SMBAlert pin Low */
  120. } I2C_SMBusAlert_TypeDef;
  121. /**
  122. * @}
  123. */
  124. /** @defgroup I2C_PEC_position
  125. * @{
  126. */
  127. typedef enum
  128. {
  129. I2C_PECPosition_Current = (uint8_t)0x00, /*!< Current byte in shift register is PEC */
  130. I2C_PECPosition_Next = (uint8_t)0x08 /*!< Next byte in shift register is PEC */
  131. } I2C_PECPosition_TypeDef;
  132. /**
  133. * @}
  134. */
  135. /** @defgroup I2C_flags_definition
  136. * @{
  137. */
  138. /**
  139. * @brief Elements values convention: 0xXXYY
  140. * X = SRx registers index
  141. * X = 1 : SR1
  142. * X = 2 : SR2
  143. * X = 3 : SR3
  144. * Y = Flag mask in the register
  145. */
  146. typedef enum
  147. {
  148. /* SR1 register flags */
  149. I2C_FLAG_TXE = (uint16_t)0x0180, /*!< Transmit Data Register Empty flag */
  150. I2C_FLAG_RXNE = (uint16_t)0x0140, /*!< Read Data Register Not Empty flag */
  151. I2C_FLAG_STOPF = (uint16_t)0x0110, /*!< Stop detected flag */
  152. I2C_FLAG_ADD10 = (uint16_t)0x0108, /*!< 10-bit Header sent flag */
  153. I2C_FLAG_BTF = (uint16_t)0x0104, /*!< Data Byte Transfer Finished flag */
  154. I2C_FLAG_ADDR = (uint16_t)0x0102, /*!< Address Sent/Matched (master/slave) flag */
  155. I2C_FLAG_SB = (uint16_t)0x0101, /*!< Start bit sent flag */
  156. /* SR2 register flags */
  157. I2C_FLAG_SMBALERT = (uint16_t)0x0280, /*!< SMBUS Alert flag */
  158. I2C_FLAG_TIMEOUT = (uint16_t)0x0240, /*!< Time out flag */
  159. I2C_FLAG_WUFH = (uint16_t)0x0220, /*!< Wake Up From Halt flag */
  160. I2C_FLAG_PECERR = (uint16_t)0x0210, /*!< PEC error flag */
  161. I2C_FLAG_OVR = (uint16_t)0x0208, /*!< Overrun/Underrun flag */
  162. I2C_FLAG_AF = (uint16_t)0x0204, /*!< Acknowledge Failure flag */
  163. I2C_FLAG_ARLO = (uint16_t)0x0202, /*!< Arbitration Loss flag */
  164. I2C_FLAG_BERR = (uint16_t)0x0201, /*!< Misplaced Start or Stop condition */
  165. /* SR3 register flags */
  166. I2C_FLAG_DUALF = (uint16_t)0x0380, /*!< DUAL Flag */
  167. I2C_FLAG_SMBHOST = (uint16_t)0x0340, /*!< SMBUS host Flag */
  168. I2C_FLAG_SMBDEFAULT = (uint16_t)0x0320, /*!< SMBUS default flag */
  169. I2C_FLAG_GENCALL = (uint16_t)0x0310, /*!< General Call header received Flag */
  170. I2C_FLAG_TRA = (uint16_t)0x0304, /*!< Transmitter Receiver flag */
  171. I2C_FLAG_BUSY = (uint16_t)0x0302, /*!< Bus Busy flag */
  172. I2C_FLAG_MSL = (uint16_t)0x0301 /*!< Master Slave flag */
  173. } I2C_FLAG_TypeDef;
  174. /**
  175. * @}
  176. */
  177. /** @defgroup I2C_interrupts_definition
  178. * @{
  179. */
  180. /**
  181. * @brief I2C Pending bits
  182. * Elements values convention: 0xXYZZ
  183. * X = SRx registers index
  184. * X = 0 : ITR
  185. * X = 1 : SR1
  186. * X = 2 : SR2
  187. * Y = Position of the corresponding Interrupt
  188. * ZZ = flag mask in the dedicated register(X register)
  189. */
  190. typedef enum
  191. {
  192. I2C_IT_ERR = (uint16_t)0x0001, /*!< Error Interruption */
  193. I2C_IT_EVT = (uint16_t)0x0002, /*!< Event Interruption */
  194. I2C_IT_BUF = (uint16_t)0x0004, /*!< Buffer Interruption */
  195. /* SR1 register*/
  196. I2C_IT_TXE = (uint16_t)0x1680, /*!< Transmit Data Register Empty */
  197. I2C_IT_RXNE = (uint16_t)0x1640, /*!< Read Data Register Not Empty */
  198. I2C_IT_STOPF = (uint16_t)0x1210, /*!< Stop detected */
  199. I2C_IT_ADD10 = (uint16_t)0x1208, /*!< 10-bit Header sent */
  200. I2C_IT_BTF = (uint16_t)0x1204, /*!< Data Byte Transfer Finished */
  201. I2C_IT_ADDR = (uint16_t)0x1202, /*!< Address Sent/Matched (master/slave) */
  202. I2C_IT_SB = (uint16_t)0x1201, /*!< Start bit sent */
  203. /* SR2 register*/
  204. I2C_IT_SMBALERT = (uint16_t)0x2180, /*!< SMBUS alert */
  205. I2C_IT_TIMEOUT = (uint16_t)0x2140, /*!< Time out */
  206. I2C_IT_WUFH = (uint16_t)0x2220, /*!< PEC error */
  207. I2C_IT_PECERR = (uint16_t)0x2110, /*!< Wake Up From Halt */
  208. I2C_IT_OVR = (uint16_t)0x2108, /*!< Overrun/Underrun */
  209. I2C_IT_AF = (uint16_t)0x2104, /*!< Acknowledge Failure */
  210. I2C_IT_ARLO = (uint16_t)0x2102, /*!< Arbitration Loss */
  211. I2C_IT_BERR = (uint16_t)0x2101 /*!< Misplaced Start or Stop condition */
  212. } I2C_IT_TypeDef;
  213. /**
  214. * @}
  215. */
  216. /** @defgroup I2C_Events
  217. * @{
  218. */
  219. /**
  220. * @brief I2C possible events
  221. * Values convention: 0xXXYY
  222. * XX = Event SR3 corresponding value
  223. * YY = Event SR1 corresponding value
  224. * @note if Event = EV3_2 the rule above does not apply
  225. * YY = Event SR2 corresponding value
  226. */
  227. typedef enum
  228. {
  229. /**
  230. ===============================================================================
  231. I2C Master Events (Events grouped in order of communication)
  232. ===============================================================================
  233. */
  234. /**
  235. * @brief Communication start
  236. *
  237. * After sending the START condition (I2C_GenerateSTART() function) the master
  238. * has to wait for this event. It means that the Start condition has been correctly
  239. * released on the I2C bus (the bus is free, no other devices is communicating).
  240. *
  241. */
  242. /* --EV5 */
  243. I2C_EVENT_MASTER_MODE_SELECT = (uint16_t)0x0301, /*!< BUSY, MSL and SB flag */
  244. /**
  245. * @brief Address Acknowledge
  246. *
  247. * After checking on EV5 (start condition correctly released on the bus), the
  248. * master sends the address of the slave(s) with which it will communicate
  249. * (I2C_Send7bitAddress() function, it also determines the direction of the communication:
  250. * Master transmitter or Receiver).
  251. * Then the master has to wait that a slave acknowledges his address.
  252. * If an acknowledge is sent on the bus, one of the following events will
  253. * be set:
  254. *
  255. * 1) In case of Master Receiver (7-bit addressing):
  256. * the I2C_EVENT_MASTER_RECEIVER_MODE_SELECTED event is set.
  257. *
  258. * 2) In case of Master Transmitter (7-bit addressing):
  259. * the I2C_EVENT_MASTER_TRANSMITTER_MODE_SELECTED is set
  260. *
  261. * 3) In case of 10-Bit addressing mode, the master (just after generating the START
  262. * and checking on EV5) has to send the header of 10-bit addressing mode (I2C_SendData()
  263. * function).
  264. * Then master should wait on EV9. It means that the 10-bit addressing
  265. * header has been correctly sent on the bus.
  266. * Then master should send the second part of the 10-bit address (LSB) using
  267. * the function I2C_Send7bitAddress(). Then master should wait for event EV6.
  268. *
  269. */
  270. /* --EV6 */
  271. I2C_EVENT_MASTER_TRANSMITTER_MODE_SELECTED = (uint16_t)0x0782, /*!< BUSY, MSL, ADDR, TXE and TRA flags */
  272. I2C_EVENT_MASTER_RECEIVER_MODE_SELECTED = (uint16_t)0x0302, /*!< BUSY, MSL and ADDR flags */
  273. /* --EV9 */
  274. I2C_EVENT_MASTER_MODE_ADDRESS10 = (uint16_t)0x0308, /*!< BUSY, MSL and ADD10 flags */
  275. /**
  276. * @brief Communication events
  277. *
  278. * If a communication is established (START condition generated and slave address
  279. * acknowledged) then the master has to check on one of the following events for
  280. * communication procedures:
  281. *
  282. * 1) Master Receiver mode: The master has to wait on the event EV7 then to read
  283. * the data received from the slave (I2C_ReceiveData() function).
  284. *
  285. * 2) Master Transmitter mode: The master has to send data (I2C_SendData()
  286. * function) then to wait on event EV8 or EV8_2.
  287. * These two events are similar:
  288. * - EV8 means that the data has been written in the data register and is
  289. * being shifted out.
  290. * - EV8_2 means that the data has been physically shifted out and output
  291. * on the bus.
  292. * In most cases, using EV8 is sufficient for the application.
  293. * Using EV8_2 leads to a slower communication but ensure more reliable test.
  294. * EV8_2 is also more suitable than EV8 for testing on the last data transmission
  295. * (before Stop condition generation).
  296. *
  297. * @note In case the user software does not guarantee that this event EV7 is
  298. * managed before the current byte end of transfer, then user may check on EV7
  299. * and BTF flag at the same time (ie. (I2C_EVENT_MASTER_BYTE_RECEIVED | I2C_FLAG_BTF)).
  300. * In this case the communication may be slower.
  301. *
  302. */
  303. /* Master RECEIVER mode -----------------------------*/
  304. /* --EV7 */
  305. I2C_EVENT_MASTER_BYTE_RECEIVED = (uint16_t)0x0340, /*!< BUSY, MSL and RXNE flags */
  306. /* Master TRANSMITTER mode --------------------------*/
  307. /* --EV8 */
  308. I2C_EVENT_MASTER_BYTE_TRANSMITTING = (uint16_t)0x0780, /*!< TRA, BUSY, MSL, TXE flags */
  309. /* --EV8_2 */
  310. I2C_EVENT_MASTER_BYTE_TRANSMITTED = (uint16_t)0x0784, /*!< EV8_2: TRA, BUSY, MSL, TXE and BTF flags */
  311. /**
  312. ===============================================================================
  313. I2C Slave Events (Events grouped in order of communication)
  314. ===============================================================================
  315. */
  316. /**
  317. * @brief Communication start events
  318. *
  319. * Wait on one of these events at the start of the communication. It means that
  320. * the I2C peripheral detected a Start condition on the bus (generated by master
  321. * device) followed by the peripheral address.
  322. * The peripheral generates an ACK condition on the bus (if the acknowledge
  323. * feature is enabled through function I2C_AcknowledgeConfig()) and the events
  324. * listed above are set :
  325. *
  326. * 1) In normal case (only one address managed by the slave), when the address
  327. * sent by the master matches the own address of the peripheral (configured by
  328. * I2C_OwnAddress1 field) the I2C_EVENT_SLAVE_XXX_ADDRESS_MATCHED event is set
  329. * (where XXX could be TRANSMITTER or RECEIVER).
  330. *
  331. * 2) In case the address sent by the master matches the second address of the
  332. * peripheral (configured by the function I2C_OwnAddress2Config() and enabled
  333. * by the function I2C_DualAddressCmd()) the events I2C_EVENT_SLAVE_XXX_SECONDADDRESS_MATCHED
  334. * (where XXX could be TRANSMITTER or RECEIVER) are set.
  335. *
  336. * 3) In case the address sent by the master is General Call (address 0x00) and
  337. * if the General Call is enabled for the peripheral (using function I2C_GeneralCallCmd())
  338. * the following event is set I2C_EVENT_SLAVE_GENERALCALLADDRESS_MATCHED.
  339. *
  340. */
  341. /* --EV1 (all the events below are variants of EV1) */
  342. /* 1) Case of One Single Address managed by the slave */
  343. I2C_EVENT_SLAVE_RECEIVER_ADDRESS_MATCHED = (uint16_t)0x0202, /*!< BUSY and ADDR flags */
  344. I2C_EVENT_SLAVE_TRANSMITTER_ADDRESS_MATCHED = (uint16_t)0x0682, /*!< TRA, BUSY, TXE and ADDR flags */
  345. /* 2) Case of Dual address managed by the slave */
  346. I2C_EVENT_SLAVE_RECEIVER_SECONDADDRESS_MATCHED = (uint16_t)0x8200, /*! DUALF and BUSY flags */
  347. I2C_EVENT_SLAVE_TRANSMITTER_SECONDADDRESS_MATCHED = (uint16_t)0x8680, /*! DUALF, TRA, BUSY and TXE flags */
  348. /* 3) Case of General Call enabled for the slave */
  349. I2C_EVENT_SLAVE_GENERALCALLADDRESS_MATCHED = (uint16_t)0x1200, /*!< EV2: GENCALL and BUSY flags */
  350. /**
  351. * @brief Communication events
  352. *
  353. * Wait on one of these events when EV1 has already been checked :
  354. *
  355. * - Slave RECEIVER mode:
  356. * - EV2: When the application is expecting a data byte to be received.
  357. * - EV4: When the application is expecting the end of the communication:
  358. * master sends a stop condition and data transmission is stopped.
  359. *
  360. * - Slave Transmitter mode:
  361. * - EV3: When a byte has been transmitted by the slave and the application
  362. * is expecting the end of the byte transmission.
  363. * The two events I2C_EVENT_SLAVE_BYTE_TRANSMITTED and I2C_EVENT_SLAVE_BYTE_TRANSMITTING
  364. * are similar. The second one can optionally be used when the user software
  365. * doesn't guarantee the EV3 is managed before the current byte end of transfer.
  366. * - EV3_2: When the master sends a NACK in order to tell slave that data transmission
  367. * shall end (before sending the STOP condition).
  368. * In this case slave has to stop sending data bytes and expect a Stop
  369. * condition on the bus.
  370. *
  371. * @note In case the user software does not guarantee that the event EV2 is
  372. * managed before the current byte end of transfer, then user may check on EV2
  373. * and BTF flag at the same time (ie. (I2C_EVENT_SLAVE_BYTE_RECEIVED | I2C_FLAG_BTF)).
  374. * In this case the communication may be slower.
  375. *
  376. */
  377. /* Slave RECEIVER mode --------------------------*/
  378. /* --EV2 */
  379. I2C_EVENT_SLAVE_BYTE_RECEIVED = (uint16_t)0x0240, /*!< BUSY and RXNE flags */
  380. /* --EV4 */
  381. I2C_EVENT_SLAVE_STOP_DETECTED = (uint16_t)0x0010, /*!< STOPF flag */
  382. /* Slave TRANSMITTER mode -----------------------*/
  383. /* --EV3 */
  384. I2C_EVENT_SLAVE_BYTE_TRANSMITTED = (uint16_t)0x0684, /*!< TRA, BUSY, TXE and BTF flags */
  385. I2C_EVENT_SLAVE_BYTE_TRANSMITTING = (uint16_t)0x0680, /*!< TRA, BUSY and TXE flags */
  386. /* --EV3_2 */
  387. I2C_EVENT_SLAVE_ACK_FAILURE = (uint16_t)0x0004 /*!< AF flag */
  388. } I2C_Event_TypeDef;
  389. /**
  390. * @}
  391. */
  392. /** @defgroup I2C_Registers
  393. * @{
  394. */
  395. typedef enum
  396. {
  397. I2C_Register_CR1 = (uint8_t)0x00, /*!< Control register 1 */
  398. I2C_Register_CR2 = (uint8_t)0x01, /*!< Control register 2 */
  399. I2C_Register_FREQR = (uint8_t)0x02, /*!< Frequency register */
  400. I2C_Register_OARL = (uint8_t)0x03, /*!< Own address register LSB */
  401. I2C_Register_OARH = (uint8_t)0x04, /*!< Own address register MSB */
  402. I2C_Register_DR = (uint8_t)0x06, /*!< Data register */
  403. I2C_Register_SR1 = (uint8_t)0x07, /*!< Status register 1 */
  404. I2C_Register_SR2 = (uint8_t)0x08, /*!< Status register 2 */
  405. I2C_Register_SR3 = (uint8_t)0x09, /*!< Status register 3 */
  406. I2C_Register_ITR = (uint8_t)0x0A, /*!< Interrupt and DMA register */
  407. I2C_Register_CCRL = (uint8_t)0x0B, /*!< Clock control register low */
  408. I2C_Register_CCRH = (uint8_t)0x0C, /*!< Clock control register high */
  409. I2C_Register_TRISER = (uint8_t)0x0D, /*!< TRISE register */
  410. I2C_Register_PECR = (uint8_t)0x0E /*!< PEC register */
  411. } I2C_Register_TypeDef;
  412. /**
  413. * @}
  414. */
  415. /**
  416. * @}
  417. */
  418. /* Exported constants --------------------------------------------------------*/
  419. /** @defgroup I2C_Exported_Constants
  420. * @{
  421. */
  422. #define I2C_MAX_STANDARD_FREQ ((uint32_t)100000)
  423. #define I2C_MAX_FAST_FREQ ((uint32_t)400000)
  424. /**
  425. *@}
  426. */
  427. /* Exported macro -----------------------------------------------------------*/
  428. /** @defgroup I2C_Exported_Macros
  429. * @{
  430. */
  431. /**
  432. * @brief Macro used by the assert function to check the different functions parameters.
  433. */
  434. /**
  435. * @brief Macro used by the assert function to check the different I2C modes.
  436. */
  437. #define IS_I2C_MODE(MODE)(((MODE) == I2C_Mode_I2C) || \
  438. ((MODE) == I2C_Mode_SMBusDevice) || \
  439. ((MODE) == I2C_Mode_SMBusHost))
  440. /**
  441. * @brief Macro used by the assert function to check the different I2C duty cycles.
  442. */
  443. #define IS_I2C_DUTY_CYCLE(CYCLE)(((CYCLE) == I2C_DutyCycle_2) || \
  444. ((CYCLE) == I2C_DutyCycle_16_9))
  445. /**
  446. * @brief Macro used by the assert function to check the different acknowledgement configuration
  447. */
  448. #define IS_I2C_ACK_STATE(STATE) (((STATE) == I2C_Ack_Disable) || \
  449. ((STATE) == I2C_Ack_Enable))
  450. /**
  451. * @brief Macro used by the assert function to check the different acknowledgement position
  452. */
  453. #define IS_I2C_ACK_POSITION(POSITION) (((POSITION) == I2C_AckPosition_Next) || \
  454. ((POSITION) == I2C_AckPosition_Current))
  455. /**
  456. * @brief Macro used by the assert function to check the different I2C PEC positions.
  457. */
  458. #define IS_I2C_PEC_POSITION(POSITION) (((POSITION) == I2C_PECPosition_Current) || \
  459. ((POSITION) == I2C_PECPosition_Next))
  460. /**
  461. * @brief Macro used by the assert function to check the different I2C addressing modes.
  462. */
  463. #define IS_I2C_ACKNOWLEDGE_ADDRESS(ADDMODE) (((ADDMODE) == I2C_AcknowledgedAddress_7bit) || \
  464. ((ADDMODE) == I2C_AcknowledgedAddress_10bit))
  465. /**
  466. * @brief Macro used by the assert function to check the different I2C SMBus Alert pin configuration.
  467. */
  468. #define IS_I2C_SMBUS_ALERT(ALERT) (((ALERT) == I2C_SMBusAlert_High) || \
  469. ((ALERT) == I2C_SMBusAlert_Low))
  470. /**
  471. * @brief Macro used by the assert function to check the different I2C communication direction.
  472. */
  473. #define IS_I2C_DIRECTION(DIR)(((DIR) == I2C_Direction_Transmitter) || \
  474. ((DIR) == I2C_Direction_Receiver ))
  475. /**
  476. * @brief Macro used by the assert function to check the different I2C flags.
  477. */
  478. #define IS_I2C_GET_FLAG(FLAG) (((FLAG) == I2C_FLAG_TXE) || \
  479. ((FLAG) == I2C_FLAG_RXNE) || \
  480. ((FLAG) == I2C_FLAG_STOPF) || \
  481. ((FLAG) == I2C_FLAG_ADD10) || \
  482. ((FLAG) == I2C_FLAG_BTF) || \
  483. ((FLAG) == I2C_FLAG_ADDR) || \
  484. ((FLAG) == I2C_FLAG_SB) || \
  485. ((FLAG) == I2C_FLAG_SMBALERT) || \
  486. ((FLAG) == I2C_FLAG_TIMEOUT) || \
  487. ((FLAG) == I2C_FLAG_WUFH) || \
  488. ((FLAG) == I2C_FLAG_PECERR) || \
  489. ((FLAG) == I2C_FLAG_OVR) || \
  490. ((FLAG) == I2C_FLAG_AF) || \
  491. ((FLAG) == I2C_FLAG_ARLO) || \
  492. ((FLAG) == I2C_FLAG_BERR) || \
  493. ((FLAG) == I2C_FLAG_DUALF) || \
  494. ((FLAG) == I2C_FLAG_SMBHOST) || \
  495. ((FLAG) == I2C_FLAG_SMBDEFAULT) || \
  496. ((FLAG) == I2C_FLAG_GENCALL) || \
  497. ((FLAG) == I2C_FLAG_TRA) || \
  498. ((FLAG) == I2C_FLAG_BUSY) || \
  499. ((FLAG) == I2C_FLAG_MSL))
  500. /**
  501. * @brief Macro used by the assert function to check the I2C flags to clear.
  502. */
  503. #define IS_I2C_CLEAR_FLAG(FLAG) ((((uint16_t)(FLAG) & (uint16_t)0xFD00) == 0x00) && ((uint16_t)(FLAG) != 0x00))
  504. /**
  505. * @brief Macro used by the assert_param function in order to check the different
  506. * sensitivity values for the Interrupts
  507. */
  508. #define IS_I2C_CONFIG_IT(IT) ((((uint16_t)(IT) & (uint16_t)0xFFF8) == 0x00) && ((uint16_t)(IT) != 0x00))
  509. /**
  510. * @brief Macro used by the assert function to check the different I2C possible
  511. * pending bits to clear by writing 0.
  512. */
  513. #define IS_I2C_CLEAR_IT(IT) ((((uint16_t)(IT) & (uint16_t)0xDC00) == 0x00) && ((uint16_t)(IT) != 0x00))
  514. /**
  515. * @brief Macro used by the assert function to check the different I2C possible pending bits.
  516. */
  517. #define IS_I2C_GET_IT(IT) (((IT) == I2C_IT_OVR) ||\
  518. ((IT) == I2C_IT_AF) ||\
  519. ((IT) == I2C_IT_ARLO) ||\
  520. ((IT) == I2C_IT_BERR) ||\
  521. ((IT) == I2C_IT_TXE) ||\
  522. ((IT) == I2C_IT_RXNE) ||\
  523. ((IT) == I2C_IT_STOPF) ||\
  524. ((IT) == I2C_IT_ADD10) ||\
  525. ((IT) == I2C_IT_BTF) ||\
  526. ((IT) == I2C_IT_ADDR) ||\
  527. ((IT) == I2C_IT_PECERR) ||\
  528. ((IT) == I2C_IT_TIMEOUT) ||\
  529. ((IT) == I2C_IT_SMBALERT) ||\
  530. ((IT) == I2C_IT_WUFH) ||\
  531. ((IT) == I2C_IT_SB))
  532. /**
  533. * @brief Macro used by the assert function to check the different I2C possible events.
  534. */
  535. #define IS_I2C_EVENT(EVENT) (((EVENT) == I2C_EVENT_SLAVE_TRANSMITTER_ADDRESS_MATCHED) || \
  536. ((EVENT) == I2C_EVENT_SLAVE_RECEIVER_ADDRESS_MATCHED) || \
  537. ((EVENT) == I2C_EVENT_SLAVE_TRANSMITTER_SECONDADDRESS_MATCHED) || \
  538. ((EVENT) == I2C_EVENT_SLAVE_RECEIVER_SECONDADDRESS_MATCHED) || \
  539. ((EVENT) == I2C_EVENT_SLAVE_GENERALCALLADDRESS_MATCHED) || \
  540. ((EVENT) == I2C_EVENT_SLAVE_BYTE_RECEIVED) || \
  541. ((EVENT) == (I2C_EVENT_SLAVE_BYTE_RECEIVED | (uint16_t)I2C_FLAG_DUALF)) || \
  542. ((EVENT) == (I2C_EVENT_SLAVE_BYTE_RECEIVED | (uint16_t)I2C_FLAG_GENCALL)) || \
  543. ((EVENT) == I2C_EVENT_SLAVE_BYTE_TRANSMITTED) || \
  544. ((EVENT) == (I2C_EVENT_SLAVE_BYTE_TRANSMITTED | (uint16_t)I2C_FLAG_DUALF)) || \
  545. ((EVENT) == (I2C_EVENT_SLAVE_BYTE_TRANSMITTED | (uint16_t)I2C_FLAG_GENCALL)) || \
  546. ((EVENT) == I2C_EVENT_SLAVE_ACK_FAILURE) || \
  547. ((EVENT) == I2C_EVENT_SLAVE_STOP_DETECTED) || \
  548. ((EVENT) == I2C_EVENT_MASTER_MODE_SELECT) || \
  549. ((EVENT) == I2C_EVENT_MASTER_TRANSMITTER_MODE_SELECTED) || \
  550. ((EVENT) == I2C_EVENT_MASTER_RECEIVER_MODE_SELECTED) || \
  551. ((EVENT) == I2C_EVENT_MASTER_BYTE_RECEIVED) || \
  552. ((EVENT) == I2C_EVENT_MASTER_BYTE_TRANSMITTED) || \
  553. ((EVENT) == I2C_EVENT_MASTER_BYTE_TRANSMITTING) || \
  554. ((EVENT) == I2C_EVENT_MASTER_MODE_ADDRESS10))
  555. /**
  556. * @brief Macro used by the assert function to check the different I2C registers.
  557. */
  558. #define IS_I2C_REGISTER(REGISTER) (((REGISTER) == I2C_Register_CR1) || \
  559. ((REGISTER) == I2C_Register_CR2) || \
  560. ((REGISTER) == I2C_Register_FREQR) || \
  561. ((REGISTER) == I2C_Register_OARL) || \
  562. ((REGISTER) == I2C_Register_OARH) || \
  563. ((REGISTER) == I2C_Register_DR) || \
  564. ((REGISTER) == I2C_Register_SR1) || \
  565. ((REGISTER) == I2C_Register_SR2) || \
  566. ((REGISTER) == I2C_Register_SR3) || \
  567. ((REGISTER) == I2C_Register_ITR) || \
  568. ((REGISTER) == I2C_Register_CCRL) || \
  569. ((REGISTER) == I2C_Register_CCRH) || \
  570. ((REGISTER) == I2C_Register_TRISER) || \
  571. ((REGISTER) == I2C_Register_PECR))
  572. /**
  573. * @brief Macro used by the assert function to check the different I2C possible own address.
  574. */
  575. #define IS_I2C_OWN_ADDRESS(ADDRESS) ((ADDRESS) <= (uint16_t)0x03FF)
  576. /**
  577. * @brief Macro used by the assert function to check the different I2C address
  578. * The address must be even
  579. */
  580. #define IS_I2C_ADDRESS(ADD) (((ADD) & (uint8_t)0x01) == (uint8_t)0x00)
  581. /**
  582. * @brief Macro used by the assert function to check that I2C Output clock frequency must be between 1Hz and 400kHz.
  583. */
  584. #define IS_I2C_OUTPUT_CLOCK_FREQ(FREQ) (((FREQ) >= (uint8_t)1) && ((FREQ) <= I2C_MAX_FAST_FREQ))
  585. /**
  586. * @}
  587. */
  588. /* Exported functions ------------------------------------------------------- */
  589. /* Function used to set the I2C configuration to the default reset state *****/
  590. void I2C_DeInit(I2C_TypeDef* I2Cx);
  591. /* Initialization and Configuration functions *********************************/
  592. void I2C_Init(I2C_TypeDef* I2Cx, uint32_t OutputClockFrequency, uint16_t OwnAddress,
  593. I2C_Mode_TypeDef I2C_Mode, I2C_DutyCycle_TypeDef I2C_DutyCycle,
  594. I2C_Ack_TypeDef I2C_Ack, I2C_AcknowledgedAddress_TypeDef I2C_AcknowledgedAddress);
  595. void I2C_Cmd(I2C_TypeDef* I2Cx, FunctionalState NewState);
  596. void I2C_GeneralCallCmd(I2C_TypeDef* I2Cx, FunctionalState NewState);
  597. void I2C_SoftwareResetCmd(I2C_TypeDef* I2Cx, FunctionalState NewState);
  598. void I2C_StretchClockCmd(I2C_TypeDef* I2Cx, FunctionalState NewState);
  599. void I2C_ARPCmd(I2C_TypeDef* I2Cx, FunctionalState NewState);
  600. void I2C_GenerateSTART(I2C_TypeDef* I2Cx, FunctionalState NewState);
  601. void I2C_GenerateSTOP(I2C_TypeDef* I2Cx, FunctionalState NewState);
  602. void I2C_AcknowledgeConfig(I2C_TypeDef* I2Cx, FunctionalState NewState);
  603. void I2C_OwnAddress2Config(I2C_TypeDef* I2Cx, uint8_t Address);
  604. void I2C_DualAddressCmd(I2C_TypeDef* I2Cx, FunctionalState NewState);
  605. void I2C_AckPositionConfig(I2C_TypeDef* I2Cx, I2C_AckPosition_TypeDef I2C_AckPosition);
  606. void I2C_FastModeDutyCycleConfig(I2C_TypeDef* I2Cx, I2C_DutyCycle_TypeDef I2C_DutyCycle);
  607. void I2C_SMBusAlertConfig(I2C_TypeDef* I2Cx, I2C_SMBusAlert_TypeDef I2C_SMBusAlert);
  608. void I2C_Send7bitAddress(I2C_TypeDef* I2Cx, uint8_t Address, I2C_Direction_TypeDef I2C_Direction);
  609. /* Data transfers functions ***************************************************/
  610. void I2C_SendData(I2C_TypeDef* I2Cx, uint8_t Data);
  611. uint8_t I2C_ReceiveData(I2C_TypeDef* I2Cx);
  612. /* PEC management functions ***************************************************/
  613. void I2C_PECPositionConfig(I2C_TypeDef* I2Cx, I2C_PECPosition_TypeDef I2C_PECPosition);
  614. uint8_t I2C_GetPEC(I2C_TypeDef* I2Cx);
  615. void I2C_TransmitPEC(I2C_TypeDef* I2Cx, FunctionalState NewState);
  616. void I2C_CalculatePEC(I2C_TypeDef* I2Cx, FunctionalState NewState);
  617. /* DMA transfers management functions *****************************************/
  618. void I2C_DMACmd(I2C_TypeDef* I2Cx, FunctionalState NewState);
  619. void I2C_DMALastTransferCmd(I2C_TypeDef* I2Cx, FunctionalState NewState);
  620. /* Interrupts, events and flags management functions **************************/
  621. void I2C_ITConfig(I2C_TypeDef* I2Cx, I2C_IT_TypeDef I2C_IT, FunctionalState NewState);
  622. uint8_t I2C_ReadRegister(I2C_TypeDef* I2Cx, I2C_Register_TypeDef I2C_Register);
  623. /**
  624. * @brief
  625. *
  626. @verbatim
  627. ================================================================================
  628. I2C State Monitoring Functions
  629. ================================================================================
  630. This I2C driver provides three different ways for I2C state monitoring
  631. depending on the application requirements and constraints:
  632. 1) Basic state monitoring:
  633. Using I2C_CheckEvent() function:
  634. It compares the status registers (SR1, SR2 and SR3) content to a given event
  635. (can be the combination of one or more flags).
  636. It returns SUCCESS if the current status includes the given flags
  637. and returns ERROR if one or more flags are missing in the current status.
  638. - When to use:
  639. - This function is suitable for most applications as well as for startup
  640. activity since the events are fully described in the product reference manual
  641. (RM0031).
  642. - It is also suitable for users who need to define their own events.
  643. - Limitations:
  644. - If an error occurs (ie. error flags are set besides to the monitored flags),
  645. the I2C_CheckEvent() function may return SUCCESS despite the communication
  646. hold or corrupted real state.
  647. In this case, it is advised to use error interrupts to monitor the error
  648. events and handle them in the interrupt IRQ handler.
  649. @note
  650. For error management, it is advised to use the following functions:
  651. - I2C_ITConfig() to configure and enable the error interrupts (I2C_IT_ERR).
  652. - I2Cx_IRQHandler() which is called when the I2C interrupts occur.
  653. Where x is the peripheral instance (I2C1,...)
  654. - I2C_GetFlagStatus() or I2C_GetITStatus() to be called into the
  655. I2Cx_IRQHandler() function in order to determine which error occurred.
  656. - I2C_ClearFlag() or I2C_ClearITPendingBit() and/or I2C_SoftwareResetCmd()
  657. and/or I2C_GenerateStop() in order to clear the error flag and
  658. source and return to correct communication status.
  659. 2) Advanced state monitoring:
  660. Using the function I2C_GetLastEvent() which returns the image of both SR1
  661. & SR3 status registers in a single word (uint16_t) (Status Register 3 value
  662. is shifted left by 8 bits and concatenated to Status Register 1).
  663. - When to use:
  664. - This function is suitable for the same applications above but it allows to
  665. overcome the limitations of I2C_GetFlagStatus() function (see below).
  666. The returned value could be compared to events already defined in the
  667. library (stm8l15x_i2c.h) or to custom values defined by user.
  668. - This function is suitable when multiple flags are monitored at the same time.
  669. - At the opposite of I2C_CheckEvent() function, this function allows user to
  670. choose when an event is accepted (when all events flags are set and no
  671. other flags are set or just when the needed flags are set like
  672. I2C_CheckEvent() function).
  673. - Limitations:
  674. - User may need to define his own events.
  675. - Same remark concerning the error management is applicable for this
  676. function if user decides to check only regular communication flags (and
  677. ignores error flags).
  678. 3) Flag-based state monitoring:
  679. Using the function I2C_GetFlagStatus() which simply returns the status of
  680. one single flag (ie. I2C_FLAG_RXNE ...).
  681. - When to use:
  682. - This function could be used for specific applications or in debug phase.
  683. - It is suitable when only one flag checking is needed (most I2C events
  684. are monitored through multiple flags).
  685. - Limitations:
  686. - When calling this function, the Status register is accessed. Some flags are
  687. cleared when the status register is accessed. So checking the status
  688. of one Flag, may clear other ones.
  689. - Function may need to be called twice or more in order to monitor one
  690. single event.
  691. @endverbatim
  692. *
  693. */
  694. /**
  695. ===============================================================================
  696. 1. Basic state monitoring
  697. ===============================================================================
  698. */
  699. ErrorStatus I2C_CheckEvent(I2C_TypeDef* I2Cx, I2C_Event_TypeDef I2C_Event);
  700. /**
  701. ===============================================================================
  702. 2. Advanced state monitoring
  703. ===============================================================================
  704. */
  705. I2C_Event_TypeDef I2C_GetLastEvent(I2C_TypeDef* I2Cx);
  706. /**
  707. ===============================================================================
  708. 3. Flag-based state monitoring
  709. ===============================================================================
  710. */
  711. FlagStatus I2C_GetFlagStatus(I2C_TypeDef* I2Cx, I2C_FLAG_TypeDef I2C_FLAG);
  712. void I2C_ClearFlag(I2C_TypeDef* I2Cx, I2C_FLAG_TypeDef I2C_FLAG);
  713. ITStatus I2C_GetITStatus(I2C_TypeDef* I2Cx, I2C_IT_TypeDef I2C_IT);
  714. void I2C_ClearITPendingBit(I2C_TypeDef* I2Cx, I2C_IT_TypeDef I2C_IT);
  715. #endif /* __STM8L15x_I2C_H */
  716. /**
  717. * @}
  718. */
  719. /**
  720. * @}
  721. */
  722. /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/