stm32f1xx_ll_i2c.h 62 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785
  1. /**
  2. ******************************************************************************
  3. * @file stm32f1xx_ll_i2c.h
  4. * @author MCD Application Team
  5. * @brief Header file of I2C LL module.
  6. ******************************************************************************
  7. * @attention
  8. *
  9. * <h2><center>&copy; Copyright (c) 2016 STMicroelectronics.
  10. * All rights reserved.</center></h2>
  11. *
  12. * This software component is licensed by ST under BSD 3-Clause license,
  13. * the "License"; You may not use this file except in compliance with the
  14. * License. You may obtain a copy of the License at:
  15. * opensource.org/licenses/BSD-3-Clause
  16. *
  17. ******************************************************************************
  18. */
  19. /* Define to prevent recursive inclusion -------------------------------------*/
  20. #ifndef __STM32F1xx_LL_I2C_H
  21. #define __STM32F1xx_LL_I2C_H
  22. #ifdef __cplusplus
  23. extern "C" {
  24. #endif
  25. /* Includes ------------------------------------------------------------------*/
  26. #include "stm32f1xx.h"
  27. /** @addtogroup STM32F1xx_LL_Driver
  28. * @{
  29. */
  30. #if defined (I2C1) || defined (I2C2)
  31. /** @defgroup I2C_LL I2C
  32. * @{
  33. */
  34. /* Private types -------------------------------------------------------------*/
  35. /* Private variables ---------------------------------------------------------*/
  36. /* Private constants ---------------------------------------------------------*/
  37. /** @defgroup I2C_LL_Private_Constants I2C Private Constants
  38. * @{
  39. */
  40. /* Defines used to perform compute and check in the macros */
  41. #define LL_I2C_MAX_SPEED_STANDARD 100000U
  42. #define LL_I2C_MAX_SPEED_FAST 400000U
  43. /**
  44. * @}
  45. */
  46. /* Private macros ------------------------------------------------------------*/
  47. #if defined(USE_FULL_LL_DRIVER)
  48. /** @defgroup I2C_LL_Private_Macros I2C Private Macros
  49. * @{
  50. */
  51. /**
  52. * @}
  53. */
  54. #endif /*USE_FULL_LL_DRIVER*/
  55. /* Exported types ------------------------------------------------------------*/
  56. #if defined(USE_FULL_LL_DRIVER)
  57. /** @defgroup I2C_LL_ES_INIT I2C Exported Init structure
  58. * @{
  59. */
  60. typedef struct
  61. {
  62. uint32_t PeripheralMode; /*!< Specifies the peripheral mode.
  63. This parameter can be a value of @ref I2C_LL_EC_PERIPHERAL_MODE
  64. This feature can be modified afterwards using unitary function @ref LL_I2C_SetMode(). */
  65. uint32_t ClockSpeed; /*!< Specifies the clock frequency.
  66. This parameter must be set to a value lower than 400kHz (in Hz)
  67. This feature can be modified afterwards using unitary function @ref LL_I2C_SetClockPeriod()
  68. or @ref LL_I2C_SetDutyCycle() or @ref LL_I2C_SetClockSpeedMode() or @ref LL_I2C_ConfigSpeed(). */
  69. uint32_t DutyCycle; /*!< Specifies the I2C fast mode duty cycle.
  70. This parameter can be a value of @ref I2C_LL_EC_DUTYCYCLE
  71. This feature can be modified afterwards using unitary function @ref LL_I2C_SetDutyCycle(). */
  72. uint32_t OwnAddress1; /*!< Specifies the device own address 1.
  73. This parameter must be a value between Min_Data = 0x00 and Max_Data = 0x3FF
  74. This feature can be modified afterwards using unitary function @ref LL_I2C_SetOwnAddress1(). */
  75. uint32_t TypeAcknowledge; /*!< Specifies the ACKnowledge or Non ACKnowledge condition after the address receive match code or next received byte.
  76. This parameter can be a value of @ref I2C_LL_EC_I2C_ACKNOWLEDGE
  77. This feature can be modified afterwards using unitary function @ref LL_I2C_AcknowledgeNextData(). */
  78. uint32_t OwnAddrSize; /*!< Specifies the device own address 1 size (7-bit or 10-bit).
  79. This parameter can be a value of @ref I2C_LL_EC_OWNADDRESS1
  80. This feature can be modified afterwards using unitary function @ref LL_I2C_SetOwnAddress1(). */
  81. } LL_I2C_InitTypeDef;
  82. /**
  83. * @}
  84. */
  85. #endif /*USE_FULL_LL_DRIVER*/
  86. /* Exported constants --------------------------------------------------------*/
  87. /** @defgroup I2C_LL_Exported_Constants I2C Exported Constants
  88. * @{
  89. */
  90. /** @defgroup I2C_LL_EC_GET_FLAG Get Flags Defines
  91. * @brief Flags defines which can be used with LL_I2C_ReadReg function
  92. * @{
  93. */
  94. #define LL_I2C_SR1_SB I2C_SR1_SB /*!< Start Bit (master mode) */
  95. #define LL_I2C_SR1_ADDR I2C_SR1_ADDR /*!< Address sent (master mode) or
  96. Address matched flag (slave mode) */
  97. #define LL_I2C_SR1_BTF I2C_SR1_BTF /*!< Byte Transfer Finished flag */
  98. #define LL_I2C_SR1_ADD10 I2C_SR1_ADD10 /*!< 10-bit header sent (master mode) */
  99. #define LL_I2C_SR1_STOPF I2C_SR1_STOPF /*!< Stop detection flag (slave mode) */
  100. #define LL_I2C_SR1_RXNE I2C_SR1_RXNE /*!< Data register not empty (receivers) */
  101. #define LL_I2C_SR1_TXE I2C_SR1_TXE /*!< Data register empty (transmitters) */
  102. #define LL_I2C_SR1_BERR I2C_SR1_BERR /*!< Bus error */
  103. #define LL_I2C_SR1_ARLO I2C_SR1_ARLO /*!< Arbitration lost */
  104. #define LL_I2C_SR1_AF I2C_SR1_AF /*!< Acknowledge failure flag */
  105. #define LL_I2C_SR1_OVR I2C_SR1_OVR /*!< Overrun/Underrun */
  106. #define LL_I2C_SR1_PECERR I2C_ISR_PECERR /*!< PEC Error in reception (SMBus mode) */
  107. #define LL_I2C_SR1_TIMEOUT I2C_ISR_TIMEOUT /*!< Timeout detection flag (SMBus mode) */
  108. #define LL_I2C_SR1_SMALERT I2C_ISR_SMALERT /*!< SMBus alert (SMBus mode) */
  109. #define LL_I2C_SR2_MSL I2C_SR2_MSL /*!< Master/Slave flag */
  110. #define LL_I2C_SR2_BUSY I2C_SR2_BUSY /*!< Bus busy flag */
  111. #define LL_I2C_SR2_TRA I2C_SR2_TRA /*!< Transmitter/receiver direction */
  112. #define LL_I2C_SR2_GENCALL I2C_SR2_GENCALL /*!< General call address (Slave mode) */
  113. #define LL_I2C_SR2_SMBDEFAULT I2C_SR2_SMBDEFAULT /*!< SMBus Device default address (Slave mode) */
  114. #define LL_I2C_SR2_SMBHOST I2C_SR2_SMBHOST /*!< SMBus Host address (Slave mode) */
  115. #define LL_I2C_SR2_DUALF I2C_SR2_DUALF /*!< Dual flag (Slave mode) */
  116. /**
  117. * @}
  118. */
  119. /** @defgroup I2C_LL_EC_IT IT Defines
  120. * @brief IT defines which can be used with LL_I2C_ReadReg and LL_I2C_WriteReg functions
  121. * @{
  122. */
  123. #define LL_I2C_CR2_ITEVTEN I2C_CR2_ITEVTEN /*!< Events interrupts enable */
  124. #define LL_I2C_CR2_ITBUFEN I2C_CR2_ITBUFEN /*!< Buffer interrupts enable */
  125. #define LL_I2C_CR2_ITERREN I2C_CR2_ITERREN /*!< Error interrupts enable */
  126. /**
  127. * @}
  128. */
  129. /** @defgroup I2C_LL_EC_OWNADDRESS1 Own Address 1 Length
  130. * @{
  131. */
  132. #define LL_I2C_OWNADDRESS1_7BIT 0x00004000U /*!< Own address 1 is a 7-bit address. */
  133. #define LL_I2C_OWNADDRESS1_10BIT (uint32_t)(I2C_OAR1_ADDMODE | 0x00004000U) /*!< Own address 1 is a 10-bit address. */
  134. /**
  135. * @}
  136. */
  137. /** @defgroup I2C_LL_EC_DUTYCYCLE Fast Mode Duty Cycle
  138. * @{
  139. */
  140. #define LL_I2C_DUTYCYCLE_2 0x00000000U /*!< I2C fast mode Tlow/Thigh = 2 */
  141. #define LL_I2C_DUTYCYCLE_16_9 I2C_CCR_DUTY /*!< I2C fast mode Tlow/Thigh = 16/9 */
  142. /**
  143. * @}
  144. */
  145. /** @defgroup I2C_LL_EC_CLOCK_SPEED_MODE Master Clock Speed Mode
  146. * @{
  147. */
  148. #define LL_I2C_CLOCK_SPEED_STANDARD_MODE 0x00000000U /*!< Master clock speed range is standard mode */
  149. #define LL_I2C_CLOCK_SPEED_FAST_MODE I2C_CCR_FS /*!< Master clock speed range is fast mode */
  150. /**
  151. * @}
  152. */
  153. /** @defgroup I2C_LL_EC_PERIPHERAL_MODE Peripheral Mode
  154. * @{
  155. */
  156. #define LL_I2C_MODE_I2C 0x00000000U /*!< I2C Master or Slave mode */
  157. #define LL_I2C_MODE_SMBUS_HOST (uint32_t)(I2C_CR1_SMBUS | I2C_CR1_SMBTYPE | I2C_CR1_ENARP) /*!< SMBus Host address acknowledge */
  158. #define LL_I2C_MODE_SMBUS_DEVICE I2C_CR1_SMBUS /*!< SMBus Device default mode (Default address not acknowledge) */
  159. #define LL_I2C_MODE_SMBUS_DEVICE_ARP (uint32_t)(I2C_CR1_SMBUS | I2C_CR1_ENARP) /*!< SMBus Device Default address acknowledge */
  160. /**
  161. * @}
  162. */
  163. /** @defgroup I2C_LL_EC_I2C_ACKNOWLEDGE Acknowledge Generation
  164. * @{
  165. */
  166. #define LL_I2C_ACK I2C_CR1_ACK /*!< ACK is sent after current received byte. */
  167. #define LL_I2C_NACK 0x00000000U /*!< NACK is sent after current received byte.*/
  168. /**
  169. * @}
  170. */
  171. /** @defgroup I2C_LL_EC_DIRECTION Read Write Direction
  172. * @{
  173. */
  174. #define LL_I2C_DIRECTION_WRITE I2C_SR2_TRA /*!< Bus is in write transfer */
  175. #define LL_I2C_DIRECTION_READ 0x00000000U /*!< Bus is in read transfer */
  176. /**
  177. * @}
  178. */
  179. /**
  180. * @}
  181. */
  182. /* Exported macro ------------------------------------------------------------*/
  183. /** @defgroup I2C_LL_Exported_Macros I2C Exported Macros
  184. * @{
  185. */
  186. /** @defgroup I2C_LL_EM_WRITE_READ Common Write and read registers Macros
  187. * @{
  188. */
  189. /**
  190. * @brief Write a value in I2C register
  191. * @param __INSTANCE__ I2C Instance
  192. * @param __REG__ Register to be written
  193. * @param __VALUE__ Value to be written in the register
  194. * @retval None
  195. */
  196. #define LL_I2C_WriteReg(__INSTANCE__, __REG__, __VALUE__) WRITE_REG(__INSTANCE__->__REG__, (__VALUE__))
  197. /**
  198. * @brief Read a value in I2C register
  199. * @param __INSTANCE__ I2C Instance
  200. * @param __REG__ Register to be read
  201. * @retval Register value
  202. */
  203. #define LL_I2C_ReadReg(__INSTANCE__, __REG__) READ_REG(__INSTANCE__->__REG__)
  204. /**
  205. * @}
  206. */
  207. /** @defgroup I2C_LL_EM_Exported_Macros_Helper Exported_Macros_Helper
  208. * @{
  209. */
  210. /**
  211. * @brief Convert Peripheral Clock Frequency in Mhz.
  212. * @param __PCLK__ This parameter must be a value of peripheral clock (in Hz).
  213. * @retval Value of peripheral clock (in Mhz)
  214. */
  215. #define __LL_I2C_FREQ_HZ_TO_MHZ(__PCLK__) (uint32_t)((__PCLK__)/1000000U)
  216. /**
  217. * @brief Convert Peripheral Clock Frequency in Hz.
  218. * @param __PCLK__ This parameter must be a value of peripheral clock (in Mhz).
  219. * @retval Value of peripheral clock (in Hz)
  220. */
  221. #define __LL_I2C_FREQ_MHZ_TO_HZ(__PCLK__) (uint32_t)((__PCLK__)*1000000U)
  222. /**
  223. * @brief Compute I2C Clock rising time.
  224. * @param __FREQRANGE__ This parameter must be a value of peripheral clock (in Mhz).
  225. * @param __SPEED__ This parameter must be a value lower than 400kHz (in Hz).
  226. * @retval Value between Min_Data=0x02 and Max_Data=0x3F
  227. */
  228. #define __LL_I2C_RISE_TIME(__FREQRANGE__, __SPEED__) (uint32_t)(((__SPEED__) <= LL_I2C_MAX_SPEED_STANDARD) ? ((__FREQRANGE__) + 1U) : ((((__FREQRANGE__) * 300U) / 1000U) + 1U))
  229. /**
  230. * @brief Compute Speed clock range to a Clock Control Register (I2C_CCR_CCR) value.
  231. * @param __PCLK__ This parameter must be a value of peripheral clock (in Hz).
  232. * @param __SPEED__ This parameter must be a value lower than 400kHz (in Hz).
  233. * @param __DUTYCYCLE__ This parameter can be one of the following values:
  234. * @arg @ref LL_I2C_DUTYCYCLE_2
  235. * @arg @ref LL_I2C_DUTYCYCLE_16_9
  236. * @retval Value between Min_Data=0x004 and Max_Data=0xFFF, except in FAST DUTY mode where Min_Data=0x001.
  237. */
  238. #define __LL_I2C_SPEED_TO_CCR(__PCLK__, __SPEED__, __DUTYCYCLE__) (uint32_t)(((__SPEED__) <= LL_I2C_MAX_SPEED_STANDARD)? \
  239. (__LL_I2C_SPEED_STANDARD_TO_CCR((__PCLK__), (__SPEED__))) : \
  240. (__LL_I2C_SPEED_FAST_TO_CCR((__PCLK__), (__SPEED__), (__DUTYCYCLE__))))
  241. /**
  242. * @brief Compute Speed Standard clock range to a Clock Control Register (I2C_CCR_CCR) value.
  243. * @param __PCLK__ This parameter must be a value of peripheral clock (in Hz).
  244. * @param __SPEED__ This parameter must be a value lower than 100kHz (in Hz).
  245. * @retval Value between Min_Data=0x004 and Max_Data=0xFFF.
  246. */
  247. #define __LL_I2C_SPEED_STANDARD_TO_CCR(__PCLK__, __SPEED__) (uint32_t)(((((__PCLK__)/((__SPEED__) << 1U)) & I2C_CCR_CCR) < 4U)? 4U:((__PCLK__) / ((__SPEED__) << 1U)))
  248. /**
  249. * @brief Compute Speed Fast clock range to a Clock Control Register (I2C_CCR_CCR) value.
  250. * @param __PCLK__ This parameter must be a value of peripheral clock (in Hz).
  251. * @param __SPEED__ This parameter must be a value between Min_Data=100Khz and Max_Data=400Khz (in Hz).
  252. * @param __DUTYCYCLE__ This parameter can be one of the following values:
  253. * @arg @ref LL_I2C_DUTYCYCLE_2
  254. * @arg @ref LL_I2C_DUTYCYCLE_16_9
  255. * @retval Value between Min_Data=0x001 and Max_Data=0xFFF
  256. */
  257. #define __LL_I2C_SPEED_FAST_TO_CCR(__PCLK__, __SPEED__, __DUTYCYCLE__) (uint32_t)(((__DUTYCYCLE__) == LL_I2C_DUTYCYCLE_2)? \
  258. (((((__PCLK__) / ((__SPEED__) * 3U)) & I2C_CCR_CCR) == 0U)? 1U:((__PCLK__) / ((__SPEED__) * 3U))) : \
  259. (((((__PCLK__) / ((__SPEED__) * 25U)) & I2C_CCR_CCR) == 0U)? 1U:((__PCLK__) / ((__SPEED__) * 25U))))
  260. /**
  261. * @brief Get the Least significant bits of a 10-Bits address.
  262. * @param __ADDRESS__ This parameter must be a value of a 10-Bits slave address.
  263. * @retval Value between Min_Data=0x00 and Max_Data=0xFF
  264. */
  265. #define __LL_I2C_10BIT_ADDRESS(__ADDRESS__) ((uint8_t)((uint16_t)((__ADDRESS__) & (uint16_t)(0x00FF))))
  266. /**
  267. * @brief Convert a 10-Bits address to a 10-Bits header with Write direction.
  268. * @param __ADDRESS__ This parameter must be a value of a 10-Bits slave address.
  269. * @retval Value between Min_Data=0xF0 and Max_Data=0xF6
  270. */
  271. #define __LL_I2C_10BIT_HEADER_WRITE(__ADDRESS__) ((uint8_t)((uint16_t)((uint16_t)(((uint16_t)((__ADDRESS__) & (uint16_t)(0x0300))) >> 7) | (uint16_t)(0xF0))))
  272. /**
  273. * @brief Convert a 10-Bits address to a 10-Bits header with Read direction.
  274. * @param __ADDRESS__ This parameter must be a value of a 10-Bits slave address.
  275. * @retval Value between Min_Data=0xF1 and Max_Data=0xF7
  276. */
  277. #define __LL_I2C_10BIT_HEADER_READ(__ADDRESS__) ((uint8_t)((uint16_t)((uint16_t)(((uint16_t)((__ADDRESS__) & (uint16_t)(0x0300))) >> 7) | (uint16_t)(0xF1))))
  278. /**
  279. * @}
  280. */
  281. /**
  282. * @}
  283. */
  284. /* Exported functions --------------------------------------------------------*/
  285. /** @defgroup I2C_LL_Exported_Functions I2C Exported Functions
  286. * @{
  287. */
  288. /** @defgroup I2C_LL_EF_Configuration Configuration
  289. * @{
  290. */
  291. /**
  292. * @brief Enable I2C peripheral (PE = 1).
  293. * @rmtoll CR1 PE LL_I2C_Enable
  294. * @param I2Cx I2C Instance.
  295. * @retval None
  296. */
  297. __STATIC_INLINE void LL_I2C_Enable(I2C_TypeDef *I2Cx)
  298. {
  299. SET_BIT(I2Cx->CR1, I2C_CR1_PE);
  300. }
  301. /**
  302. * @brief Disable I2C peripheral (PE = 0).
  303. * @rmtoll CR1 PE LL_I2C_Disable
  304. * @param I2Cx I2C Instance.
  305. * @retval None
  306. */
  307. __STATIC_INLINE void LL_I2C_Disable(I2C_TypeDef *I2Cx)
  308. {
  309. CLEAR_BIT(I2Cx->CR1, I2C_CR1_PE);
  310. }
  311. /**
  312. * @brief Check if the I2C peripheral is enabled or disabled.
  313. * @rmtoll CR1 PE LL_I2C_IsEnabled
  314. * @param I2Cx I2C Instance.
  315. * @retval State of bit (1 or 0).
  316. */
  317. __STATIC_INLINE uint32_t LL_I2C_IsEnabled(I2C_TypeDef *I2Cx)
  318. {
  319. return (READ_BIT(I2Cx->CR1, I2C_CR1_PE) == (I2C_CR1_PE));
  320. }
  321. /**
  322. * @brief Enable DMA transmission requests.
  323. * @rmtoll CR2 DMAEN LL_I2C_EnableDMAReq_TX
  324. * @param I2Cx I2C Instance.
  325. * @retval None
  326. */
  327. __STATIC_INLINE void LL_I2C_EnableDMAReq_TX(I2C_TypeDef *I2Cx)
  328. {
  329. SET_BIT(I2Cx->CR2, I2C_CR2_DMAEN);
  330. }
  331. /**
  332. * @brief Disable DMA transmission requests.
  333. * @rmtoll CR2 DMAEN LL_I2C_DisableDMAReq_TX
  334. * @param I2Cx I2C Instance.
  335. * @retval None
  336. */
  337. __STATIC_INLINE void LL_I2C_DisableDMAReq_TX(I2C_TypeDef *I2Cx)
  338. {
  339. CLEAR_BIT(I2Cx->CR2, I2C_CR2_DMAEN);
  340. }
  341. /**
  342. * @brief Check if DMA transmission requests are enabled or disabled.
  343. * @rmtoll CR2 DMAEN LL_I2C_IsEnabledDMAReq_TX
  344. * @param I2Cx I2C Instance.
  345. * @retval State of bit (1 or 0).
  346. */
  347. __STATIC_INLINE uint32_t LL_I2C_IsEnabledDMAReq_TX(I2C_TypeDef *I2Cx)
  348. {
  349. return (READ_BIT(I2Cx->CR2, I2C_CR2_DMAEN) == (I2C_CR2_DMAEN));
  350. }
  351. /**
  352. * @brief Enable DMA reception requests.
  353. * @rmtoll CR2 DMAEN LL_I2C_EnableDMAReq_RX
  354. * @param I2Cx I2C Instance.
  355. * @retval None
  356. */
  357. __STATIC_INLINE void LL_I2C_EnableDMAReq_RX(I2C_TypeDef *I2Cx)
  358. {
  359. SET_BIT(I2Cx->CR2, I2C_CR2_DMAEN);
  360. }
  361. /**
  362. * @brief Disable DMA reception requests.
  363. * @rmtoll CR2 DMAEN LL_I2C_DisableDMAReq_RX
  364. * @param I2Cx I2C Instance.
  365. * @retval None
  366. */
  367. __STATIC_INLINE void LL_I2C_DisableDMAReq_RX(I2C_TypeDef *I2Cx)
  368. {
  369. CLEAR_BIT(I2Cx->CR2, I2C_CR2_DMAEN);
  370. }
  371. /**
  372. * @brief Check if DMA reception requests are enabled or disabled.
  373. * @rmtoll CR2 DMAEN LL_I2C_IsEnabledDMAReq_RX
  374. * @param I2Cx I2C Instance.
  375. * @retval State of bit (1 or 0).
  376. */
  377. __STATIC_INLINE uint32_t LL_I2C_IsEnabledDMAReq_RX(I2C_TypeDef *I2Cx)
  378. {
  379. return (READ_BIT(I2Cx->CR2, I2C_CR2_DMAEN) == (I2C_CR2_DMAEN));
  380. }
  381. /**
  382. * @brief Get the data register address used for DMA transfer.
  383. * @rmtoll DR DR LL_I2C_DMA_GetRegAddr
  384. * @param I2Cx I2C Instance.
  385. * @retval Address of data register
  386. */
  387. __STATIC_INLINE uint32_t LL_I2C_DMA_GetRegAddr(I2C_TypeDef *I2Cx)
  388. {
  389. return (uint32_t) & (I2Cx->DR);
  390. }
  391. /**
  392. * @brief Enable Clock stretching.
  393. * @note This bit can only be programmed when the I2C is disabled (PE = 0).
  394. * @rmtoll CR1 NOSTRETCH LL_I2C_EnableClockStretching
  395. * @param I2Cx I2C Instance.
  396. * @retval None
  397. */
  398. __STATIC_INLINE void LL_I2C_EnableClockStretching(I2C_TypeDef *I2Cx)
  399. {
  400. CLEAR_BIT(I2Cx->CR1, I2C_CR1_NOSTRETCH);
  401. }
  402. /**
  403. * @brief Disable Clock stretching.
  404. * @note This bit can only be programmed when the I2C is disabled (PE = 0).
  405. * @rmtoll CR1 NOSTRETCH LL_I2C_DisableClockStretching
  406. * @param I2Cx I2C Instance.
  407. * @retval None
  408. */
  409. __STATIC_INLINE void LL_I2C_DisableClockStretching(I2C_TypeDef *I2Cx)
  410. {
  411. SET_BIT(I2Cx->CR1, I2C_CR1_NOSTRETCH);
  412. }
  413. /**
  414. * @brief Check if Clock stretching is enabled or disabled.
  415. * @rmtoll CR1 NOSTRETCH LL_I2C_IsEnabledClockStretching
  416. * @param I2Cx I2C Instance.
  417. * @retval State of bit (1 or 0).
  418. */
  419. __STATIC_INLINE uint32_t LL_I2C_IsEnabledClockStretching(I2C_TypeDef *I2Cx)
  420. {
  421. return (READ_BIT(I2Cx->CR1, I2C_CR1_NOSTRETCH) != (I2C_CR1_NOSTRETCH));
  422. }
  423. /**
  424. * @brief Enable General Call.
  425. * @note When enabled the Address 0x00 is ACKed.
  426. * @rmtoll CR1 ENGC LL_I2C_EnableGeneralCall
  427. * @param I2Cx I2C Instance.
  428. * @retval None
  429. */
  430. __STATIC_INLINE void LL_I2C_EnableGeneralCall(I2C_TypeDef *I2Cx)
  431. {
  432. SET_BIT(I2Cx->CR1, I2C_CR1_ENGC);
  433. }
  434. /**
  435. * @brief Disable General Call.
  436. * @note When disabled the Address 0x00 is NACKed.
  437. * @rmtoll CR1 ENGC LL_I2C_DisableGeneralCall
  438. * @param I2Cx I2C Instance.
  439. * @retval None
  440. */
  441. __STATIC_INLINE void LL_I2C_DisableGeneralCall(I2C_TypeDef *I2Cx)
  442. {
  443. CLEAR_BIT(I2Cx->CR1, I2C_CR1_ENGC);
  444. }
  445. /**
  446. * @brief Check if General Call is enabled or disabled.
  447. * @rmtoll CR1 ENGC LL_I2C_IsEnabledGeneralCall
  448. * @param I2Cx I2C Instance.
  449. * @retval State of bit (1 or 0).
  450. */
  451. __STATIC_INLINE uint32_t LL_I2C_IsEnabledGeneralCall(I2C_TypeDef *I2Cx)
  452. {
  453. return (READ_BIT(I2Cx->CR1, I2C_CR1_ENGC) == (I2C_CR1_ENGC));
  454. }
  455. /**
  456. * @brief Set the Own Address1.
  457. * @rmtoll OAR1 ADD0 LL_I2C_SetOwnAddress1\n
  458. * OAR1 ADD1_7 LL_I2C_SetOwnAddress1\n
  459. * OAR1 ADD8_9 LL_I2C_SetOwnAddress1\n
  460. * OAR1 ADDMODE LL_I2C_SetOwnAddress1
  461. * @param I2Cx I2C Instance.
  462. * @param OwnAddress1 This parameter must be a value between Min_Data=0 and Max_Data=0x3FF.
  463. * @param OwnAddrSize This parameter can be one of the following values:
  464. * @arg @ref LL_I2C_OWNADDRESS1_7BIT
  465. * @arg @ref LL_I2C_OWNADDRESS1_10BIT
  466. * @retval None
  467. */
  468. __STATIC_INLINE void LL_I2C_SetOwnAddress1(I2C_TypeDef *I2Cx, uint32_t OwnAddress1, uint32_t OwnAddrSize)
  469. {
  470. MODIFY_REG(I2Cx->OAR1, I2C_OAR1_ADD0 | I2C_OAR1_ADD1_7 | I2C_OAR1_ADD8_9 | I2C_OAR1_ADDMODE, OwnAddress1 | OwnAddrSize);
  471. }
  472. /**
  473. * @brief Set the 7bits Own Address2.
  474. * @note This action has no effect if own address2 is enabled.
  475. * @rmtoll OAR2 ADD2 LL_I2C_SetOwnAddress2
  476. * @param I2Cx I2C Instance.
  477. * @param OwnAddress2 This parameter must be a value between Min_Data=0 and Max_Data=0x7F.
  478. * @retval None
  479. */
  480. __STATIC_INLINE void LL_I2C_SetOwnAddress2(I2C_TypeDef *I2Cx, uint32_t OwnAddress2)
  481. {
  482. MODIFY_REG(I2Cx->OAR2, I2C_OAR2_ADD2, OwnAddress2);
  483. }
  484. /**
  485. * @brief Enable acknowledge on Own Address2 match address.
  486. * @rmtoll OAR2 ENDUAL LL_I2C_EnableOwnAddress2
  487. * @param I2Cx I2C Instance.
  488. * @retval None
  489. */
  490. __STATIC_INLINE void LL_I2C_EnableOwnAddress2(I2C_TypeDef *I2Cx)
  491. {
  492. SET_BIT(I2Cx->OAR2, I2C_OAR2_ENDUAL);
  493. }
  494. /**
  495. * @brief Disable acknowledge on Own Address2 match address.
  496. * @rmtoll OAR2 ENDUAL LL_I2C_DisableOwnAddress2
  497. * @param I2Cx I2C Instance.
  498. * @retval None
  499. */
  500. __STATIC_INLINE void LL_I2C_DisableOwnAddress2(I2C_TypeDef *I2Cx)
  501. {
  502. CLEAR_BIT(I2Cx->OAR2, I2C_OAR2_ENDUAL);
  503. }
  504. /**
  505. * @brief Check if Own Address1 acknowledge is enabled or disabled.
  506. * @rmtoll OAR2 ENDUAL LL_I2C_IsEnabledOwnAddress2
  507. * @param I2Cx I2C Instance.
  508. * @retval State of bit (1 or 0).
  509. */
  510. __STATIC_INLINE uint32_t LL_I2C_IsEnabledOwnAddress2(I2C_TypeDef *I2Cx)
  511. {
  512. return (READ_BIT(I2Cx->OAR2, I2C_OAR2_ENDUAL) == (I2C_OAR2_ENDUAL));
  513. }
  514. /**
  515. * @brief Configure the Peripheral clock frequency.
  516. * @rmtoll CR2 FREQ LL_I2C_SetPeriphClock
  517. * @param I2Cx I2C Instance.
  518. * @param PeriphClock Peripheral Clock (in Hz)
  519. * @retval None
  520. */
  521. __STATIC_INLINE void LL_I2C_SetPeriphClock(I2C_TypeDef *I2Cx, uint32_t PeriphClock)
  522. {
  523. MODIFY_REG(I2Cx->CR2, I2C_CR2_FREQ, __LL_I2C_FREQ_HZ_TO_MHZ(PeriphClock));
  524. }
  525. /**
  526. * @brief Get the Peripheral clock frequency.
  527. * @rmtoll CR2 FREQ LL_I2C_GetPeriphClock
  528. * @param I2Cx I2C Instance.
  529. * @retval Value of Peripheral Clock (in Hz)
  530. */
  531. __STATIC_INLINE uint32_t LL_I2C_GetPeriphClock(I2C_TypeDef *I2Cx)
  532. {
  533. return (uint32_t)(__LL_I2C_FREQ_MHZ_TO_HZ(READ_BIT(I2Cx->CR2, I2C_CR2_FREQ)));
  534. }
  535. /**
  536. * @brief Configure the Duty cycle (Fast mode only).
  537. * @rmtoll CCR DUTY LL_I2C_SetDutyCycle
  538. * @param I2Cx I2C Instance.
  539. * @param DutyCycle This parameter can be one of the following values:
  540. * @arg @ref LL_I2C_DUTYCYCLE_2
  541. * @arg @ref LL_I2C_DUTYCYCLE_16_9
  542. * @retval None
  543. */
  544. __STATIC_INLINE void LL_I2C_SetDutyCycle(I2C_TypeDef *I2Cx, uint32_t DutyCycle)
  545. {
  546. MODIFY_REG(I2Cx->CCR, I2C_CCR_DUTY, DutyCycle);
  547. }
  548. /**
  549. * @brief Get the Duty cycle (Fast mode only).
  550. * @rmtoll CCR DUTY LL_I2C_GetDutyCycle
  551. * @param I2Cx I2C Instance.
  552. * @retval Returned value can be one of the following values:
  553. * @arg @ref LL_I2C_DUTYCYCLE_2
  554. * @arg @ref LL_I2C_DUTYCYCLE_16_9
  555. */
  556. __STATIC_INLINE uint32_t LL_I2C_GetDutyCycle(I2C_TypeDef *I2Cx)
  557. {
  558. return (uint32_t)(READ_BIT(I2Cx->CCR, I2C_CCR_DUTY));
  559. }
  560. /**
  561. * @brief Configure the I2C master clock speed mode.
  562. * @rmtoll CCR FS LL_I2C_SetClockSpeedMode
  563. * @param I2Cx I2C Instance.
  564. * @param ClockSpeedMode This parameter can be one of the following values:
  565. * @arg @ref LL_I2C_CLOCK_SPEED_STANDARD_MODE
  566. * @arg @ref LL_I2C_CLOCK_SPEED_FAST_MODE
  567. * @retval None
  568. */
  569. __STATIC_INLINE void LL_I2C_SetClockSpeedMode(I2C_TypeDef *I2Cx, uint32_t ClockSpeedMode)
  570. {
  571. MODIFY_REG(I2Cx->CCR, I2C_CCR_FS, ClockSpeedMode);
  572. }
  573. /**
  574. * @brief Get the the I2C master speed mode.
  575. * @rmtoll CCR FS LL_I2C_GetClockSpeedMode
  576. * @param I2Cx I2C Instance.
  577. * @retval Returned value can be one of the following values:
  578. * @arg @ref LL_I2C_CLOCK_SPEED_STANDARD_MODE
  579. * @arg @ref LL_I2C_CLOCK_SPEED_FAST_MODE
  580. */
  581. __STATIC_INLINE uint32_t LL_I2C_GetClockSpeedMode(I2C_TypeDef *I2Cx)
  582. {
  583. return (uint32_t)(READ_BIT(I2Cx->CCR, I2C_CCR_FS));
  584. }
  585. /**
  586. * @brief Configure the SCL, SDA rising time.
  587. * @note This bit can only be programmed when the I2C is disabled (PE = 0).
  588. * @rmtoll TRISE TRISE LL_I2C_SetRiseTime
  589. * @param I2Cx I2C Instance.
  590. * @param RiseTime This parameter must be a value between Min_Data=0x02 and Max_Data=0x3F.
  591. * @retval None
  592. */
  593. __STATIC_INLINE void LL_I2C_SetRiseTime(I2C_TypeDef *I2Cx, uint32_t RiseTime)
  594. {
  595. MODIFY_REG(I2Cx->TRISE, I2C_TRISE_TRISE, RiseTime);
  596. }
  597. /**
  598. * @brief Get the SCL, SDA rising time.
  599. * @rmtoll TRISE TRISE LL_I2C_GetRiseTime
  600. * @param I2Cx I2C Instance.
  601. * @retval Value between Min_Data=0x02 and Max_Data=0x3F
  602. */
  603. __STATIC_INLINE uint32_t LL_I2C_GetRiseTime(I2C_TypeDef *I2Cx)
  604. {
  605. return (uint32_t)(READ_BIT(I2Cx->TRISE, I2C_TRISE_TRISE));
  606. }
  607. /**
  608. * @brief Configure the SCL high and low period.
  609. * @note This bit can only be programmed when the I2C is disabled (PE = 0).
  610. * @rmtoll CCR CCR LL_I2C_SetClockPeriod
  611. * @param I2Cx I2C Instance.
  612. * @param ClockPeriod This parameter must be a value between Min_Data=0x004 and Max_Data=0xFFF, except in FAST DUTY mode where Min_Data=0x001.
  613. * @retval None
  614. */
  615. __STATIC_INLINE void LL_I2C_SetClockPeriod(I2C_TypeDef *I2Cx, uint32_t ClockPeriod)
  616. {
  617. MODIFY_REG(I2Cx->CCR, I2C_CCR_CCR, ClockPeriod);
  618. }
  619. /**
  620. * @brief Get the SCL high and low period.
  621. * @rmtoll CCR CCR LL_I2C_GetClockPeriod
  622. * @param I2Cx I2C Instance.
  623. * @retval Value between Min_Data=0x004 and Max_Data=0xFFF, except in FAST DUTY mode where Min_Data=0x001.
  624. */
  625. __STATIC_INLINE uint32_t LL_I2C_GetClockPeriod(I2C_TypeDef *I2Cx)
  626. {
  627. return (uint32_t)(READ_BIT(I2Cx->CCR, I2C_CCR_CCR));
  628. }
  629. /**
  630. * @brief Configure the SCL speed.
  631. * @note This bit can only be programmed when the I2C is disabled (PE = 0).
  632. * @rmtoll CR2 FREQ LL_I2C_ConfigSpeed\n
  633. * TRISE TRISE LL_I2C_ConfigSpeed\n
  634. * CCR FS LL_I2C_ConfigSpeed\n
  635. * CCR DUTY LL_I2C_ConfigSpeed\n
  636. * CCR CCR LL_I2C_ConfigSpeed
  637. * @param I2Cx I2C Instance.
  638. * @param PeriphClock Peripheral Clock (in Hz)
  639. * @param ClockSpeed This parameter must be a value lower than 400kHz (in Hz).
  640. * @param DutyCycle This parameter can be one of the following values:
  641. * @arg @ref LL_I2C_DUTYCYCLE_2
  642. * @arg @ref LL_I2C_DUTYCYCLE_16_9
  643. * @retval None
  644. */
  645. __STATIC_INLINE void LL_I2C_ConfigSpeed(I2C_TypeDef *I2Cx, uint32_t PeriphClock, uint32_t ClockSpeed,
  646. uint32_t DutyCycle)
  647. {
  648. uint32_t freqrange = 0x0U;
  649. uint32_t clockconfig = 0x0U;
  650. /* Compute frequency range */
  651. freqrange = __LL_I2C_FREQ_HZ_TO_MHZ(PeriphClock);
  652. /* Configure I2Cx: Frequency range register */
  653. MODIFY_REG(I2Cx->CR2, I2C_CR2_FREQ, freqrange);
  654. /* Configure I2Cx: Rise Time register */
  655. MODIFY_REG(I2Cx->TRISE, I2C_TRISE_TRISE, __LL_I2C_RISE_TIME(freqrange, ClockSpeed));
  656. /* Configure Speed mode, Duty Cycle and Clock control register value */
  657. if (ClockSpeed > LL_I2C_MAX_SPEED_STANDARD)
  658. {
  659. /* Set Speed mode at fast and duty cycle for Clock Speed request in fast clock range */
  660. clockconfig = LL_I2C_CLOCK_SPEED_FAST_MODE | \
  661. __LL_I2C_SPEED_FAST_TO_CCR(PeriphClock, ClockSpeed, DutyCycle) | \
  662. DutyCycle;
  663. }
  664. else
  665. {
  666. /* Set Speed mode at standard for Clock Speed request in standard clock range */
  667. clockconfig = LL_I2C_CLOCK_SPEED_STANDARD_MODE | \
  668. __LL_I2C_SPEED_STANDARD_TO_CCR(PeriphClock, ClockSpeed);
  669. }
  670. /* Configure I2Cx: Clock control register */
  671. MODIFY_REG(I2Cx->CCR, (I2C_CCR_FS | I2C_CCR_DUTY | I2C_CCR_CCR), clockconfig);
  672. }
  673. /**
  674. * @brief Configure peripheral mode.
  675. * @note Macro @ref IS_SMBUS_ALL_INSTANCE(I2Cx) can be used to check whether or not
  676. * SMBus feature is supported by the I2Cx Instance.
  677. * @rmtoll CR1 SMBUS LL_I2C_SetMode\n
  678. * CR1 SMBTYPE LL_I2C_SetMode\n
  679. * CR1 ENARP LL_I2C_SetMode
  680. * @param I2Cx I2C Instance.
  681. * @param PeripheralMode This parameter can be one of the following values:
  682. * @arg @ref LL_I2C_MODE_I2C
  683. * @arg @ref LL_I2C_MODE_SMBUS_HOST
  684. * @arg @ref LL_I2C_MODE_SMBUS_DEVICE
  685. * @arg @ref LL_I2C_MODE_SMBUS_DEVICE_ARP
  686. * @retval None
  687. */
  688. __STATIC_INLINE void LL_I2C_SetMode(I2C_TypeDef *I2Cx, uint32_t PeripheralMode)
  689. {
  690. MODIFY_REG(I2Cx->CR1, I2C_CR1_SMBUS | I2C_CR1_SMBTYPE | I2C_CR1_ENARP, PeripheralMode);
  691. }
  692. /**
  693. * @brief Get peripheral mode.
  694. * @note Macro @ref IS_SMBUS_ALL_INSTANCE(I2Cx) can be used to check whether or not
  695. * SMBus feature is supported by the I2Cx Instance.
  696. * @rmtoll CR1 SMBUS LL_I2C_GetMode\n
  697. * CR1 SMBTYPE LL_I2C_GetMode\n
  698. * CR1 ENARP LL_I2C_GetMode
  699. * @param I2Cx I2C Instance.
  700. * @retval Returned value can be one of the following values:
  701. * @arg @ref LL_I2C_MODE_I2C
  702. * @arg @ref LL_I2C_MODE_SMBUS_HOST
  703. * @arg @ref LL_I2C_MODE_SMBUS_DEVICE
  704. * @arg @ref LL_I2C_MODE_SMBUS_DEVICE_ARP
  705. */
  706. __STATIC_INLINE uint32_t LL_I2C_GetMode(I2C_TypeDef *I2Cx)
  707. {
  708. return (uint32_t)(READ_BIT(I2Cx->CR1, I2C_CR1_SMBUS | I2C_CR1_SMBTYPE | I2C_CR1_ENARP));
  709. }
  710. /**
  711. * @brief Enable SMBus alert (Host or Device mode)
  712. * @note Macro @ref IS_SMBUS_ALL_INSTANCE(I2Cx) can be used to check whether or not
  713. * SMBus feature is supported by the I2Cx Instance.
  714. * @note SMBus Device mode:
  715. * - SMBus Alert pin is drived low and
  716. * Alert Response Address Header acknowledge is enabled.
  717. * SMBus Host mode:
  718. * - SMBus Alert pin management is supported.
  719. * @rmtoll CR1 ALERT LL_I2C_EnableSMBusAlert
  720. * @param I2Cx I2C Instance.
  721. * @retval None
  722. */
  723. __STATIC_INLINE void LL_I2C_EnableSMBusAlert(I2C_TypeDef *I2Cx)
  724. {
  725. SET_BIT(I2Cx->CR1, I2C_CR1_ALERT);
  726. }
  727. /**
  728. * @brief Disable SMBus alert (Host or Device mode)
  729. * @note Macro @ref IS_SMBUS_ALL_INSTANCE(I2Cx) can be used to check whether or not
  730. * SMBus feature is supported by the I2Cx Instance.
  731. * @note SMBus Device mode:
  732. * - SMBus Alert pin is not drived (can be used as a standard GPIO) and
  733. * Alert Response Address Header acknowledge is disabled.
  734. * SMBus Host mode:
  735. * - SMBus Alert pin management is not supported.
  736. * @rmtoll CR1 ALERT LL_I2C_DisableSMBusAlert
  737. * @param I2Cx I2C Instance.
  738. * @retval None
  739. */
  740. __STATIC_INLINE void LL_I2C_DisableSMBusAlert(I2C_TypeDef *I2Cx)
  741. {
  742. CLEAR_BIT(I2Cx->CR1, I2C_CR1_ALERT);
  743. }
  744. /**
  745. * @brief Check if SMBus alert (Host or Device mode) is enabled or disabled.
  746. * @note Macro @ref IS_SMBUS_ALL_INSTANCE(I2Cx) can be used to check whether or not
  747. * SMBus feature is supported by the I2Cx Instance.
  748. * @rmtoll CR1 ALERT LL_I2C_IsEnabledSMBusAlert
  749. * @param I2Cx I2C Instance.
  750. * @retval State of bit (1 or 0).
  751. */
  752. __STATIC_INLINE uint32_t LL_I2C_IsEnabledSMBusAlert(I2C_TypeDef *I2Cx)
  753. {
  754. return (READ_BIT(I2Cx->CR1, I2C_CR1_ALERT) == (I2C_CR1_ALERT));
  755. }
  756. /**
  757. * @brief Enable SMBus Packet Error Calculation (PEC).
  758. * @note Macro @ref IS_SMBUS_ALL_INSTANCE(I2Cx) can be used to check whether or not
  759. * SMBus feature is supported by the I2Cx Instance.
  760. * @rmtoll CR1 ENPEC LL_I2C_EnableSMBusPEC
  761. * @param I2Cx I2C Instance.
  762. * @retval None
  763. */
  764. __STATIC_INLINE void LL_I2C_EnableSMBusPEC(I2C_TypeDef *I2Cx)
  765. {
  766. SET_BIT(I2Cx->CR1, I2C_CR1_ENPEC);
  767. }
  768. /**
  769. * @brief Disable SMBus Packet Error Calculation (PEC).
  770. * @note Macro @ref IS_SMBUS_ALL_INSTANCE(I2Cx) can be used to check whether or not
  771. * SMBus feature is supported by the I2Cx Instance.
  772. * @rmtoll CR1 ENPEC LL_I2C_DisableSMBusPEC
  773. * @param I2Cx I2C Instance.
  774. * @retval None
  775. */
  776. __STATIC_INLINE void LL_I2C_DisableSMBusPEC(I2C_TypeDef *I2Cx)
  777. {
  778. CLEAR_BIT(I2Cx->CR1, I2C_CR1_ENPEC);
  779. }
  780. /**
  781. * @brief Check if SMBus Packet Error Calculation (PEC) is enabled or disabled.
  782. * @note Macro @ref IS_SMBUS_ALL_INSTANCE(I2Cx) can be used to check whether or not
  783. * SMBus feature is supported by the I2Cx Instance.
  784. * @rmtoll CR1 ENPEC LL_I2C_IsEnabledSMBusPEC
  785. * @param I2Cx I2C Instance.
  786. * @retval State of bit (1 or 0).
  787. */
  788. __STATIC_INLINE uint32_t LL_I2C_IsEnabledSMBusPEC(I2C_TypeDef *I2Cx)
  789. {
  790. return (READ_BIT(I2Cx->CR1, I2C_CR1_ENPEC) == (I2C_CR1_ENPEC));
  791. }
  792. /**
  793. * @}
  794. */
  795. /** @defgroup I2C_LL_EF_IT_Management IT_Management
  796. * @{
  797. */
  798. /**
  799. * @brief Enable TXE interrupt.
  800. * @rmtoll CR2 ITEVTEN LL_I2C_EnableIT_TX\n
  801. * CR2 ITBUFEN LL_I2C_EnableIT_TX
  802. * @param I2Cx I2C Instance.
  803. * @retval None
  804. */
  805. __STATIC_INLINE void LL_I2C_EnableIT_TX(I2C_TypeDef *I2Cx)
  806. {
  807. SET_BIT(I2Cx->CR2, I2C_CR2_ITEVTEN | I2C_CR2_ITBUFEN);
  808. }
  809. /**
  810. * @brief Disable TXE interrupt.
  811. * @rmtoll CR2 ITEVTEN LL_I2C_DisableIT_TX\n
  812. * CR2 ITBUFEN LL_I2C_DisableIT_TX
  813. * @param I2Cx I2C Instance.
  814. * @retval None
  815. */
  816. __STATIC_INLINE void LL_I2C_DisableIT_TX(I2C_TypeDef *I2Cx)
  817. {
  818. CLEAR_BIT(I2Cx->CR2, I2C_CR2_ITEVTEN | I2C_CR2_ITBUFEN);
  819. }
  820. /**
  821. * @brief Check if the TXE Interrupt is enabled or disabled.
  822. * @rmtoll CR2 ITEVTEN LL_I2C_IsEnabledIT_TX\n
  823. * CR2 ITBUFEN LL_I2C_IsEnabledIT_TX
  824. * @param I2Cx I2C Instance.
  825. * @retval State of bit (1 or 0).
  826. */
  827. __STATIC_INLINE uint32_t LL_I2C_IsEnabledIT_TX(I2C_TypeDef *I2Cx)
  828. {
  829. return (READ_BIT(I2Cx->CR2, I2C_CR2_ITEVTEN | I2C_CR2_ITBUFEN) == (I2C_CR2_ITEVTEN | I2C_CR2_ITBUFEN));
  830. }
  831. /**
  832. * @brief Enable RXNE interrupt.
  833. * @rmtoll CR2 ITEVTEN LL_I2C_EnableIT_RX\n
  834. * CR2 ITBUFEN LL_I2C_EnableIT_RX
  835. * @param I2Cx I2C Instance.
  836. * @retval None
  837. */
  838. __STATIC_INLINE void LL_I2C_EnableIT_RX(I2C_TypeDef *I2Cx)
  839. {
  840. SET_BIT(I2Cx->CR2, I2C_CR2_ITEVTEN | I2C_CR2_ITBUFEN);
  841. }
  842. /**
  843. * @brief Disable RXNE interrupt.
  844. * @rmtoll CR2 ITEVTEN LL_I2C_DisableIT_RX\n
  845. * CR2 ITBUFEN LL_I2C_DisableIT_RX
  846. * @param I2Cx I2C Instance.
  847. * @retval None
  848. */
  849. __STATIC_INLINE void LL_I2C_DisableIT_RX(I2C_TypeDef *I2Cx)
  850. {
  851. CLEAR_BIT(I2Cx->CR2, I2C_CR2_ITEVTEN | I2C_CR2_ITBUFEN);
  852. }
  853. /**
  854. * @brief Check if the RXNE Interrupt is enabled or disabled.
  855. * @rmtoll CR2 ITEVTEN LL_I2C_IsEnabledIT_RX\n
  856. * CR2 ITBUFEN LL_I2C_IsEnabledIT_RX
  857. * @param I2Cx I2C Instance.
  858. * @retval State of bit (1 or 0).
  859. */
  860. __STATIC_INLINE uint32_t LL_I2C_IsEnabledIT_RX(I2C_TypeDef *I2Cx)
  861. {
  862. return (READ_BIT(I2Cx->CR2, I2C_CR2_ITEVTEN | I2C_CR2_ITBUFEN) == (I2C_CR2_ITEVTEN | I2C_CR2_ITBUFEN));
  863. }
  864. /**
  865. * @brief Enable Events interrupts.
  866. * @note Any of these events will generate interrupt :
  867. * Start Bit (SB)
  868. * Address sent, Address matched (ADDR)
  869. * 10-bit header sent (ADD10)
  870. * Stop detection (STOPF)
  871. * Byte transfer finished (BTF)
  872. *
  873. * @note Any of these events will generate interrupt if Buffer interrupts are enabled too(using unitary function @ref LL_I2C_EnableIT_BUF()) :
  874. * Receive buffer not empty (RXNE)
  875. * Transmit buffer empty (TXE)
  876. * @rmtoll CR2 ITEVTEN LL_I2C_EnableIT_EVT
  877. * @param I2Cx I2C Instance.
  878. * @retval None
  879. */
  880. __STATIC_INLINE void LL_I2C_EnableIT_EVT(I2C_TypeDef *I2Cx)
  881. {
  882. SET_BIT(I2Cx->CR2, I2C_CR2_ITEVTEN);
  883. }
  884. /**
  885. * @brief Disable Events interrupts.
  886. * @note Any of these events will generate interrupt :
  887. * Start Bit (SB)
  888. * Address sent, Address matched (ADDR)
  889. * 10-bit header sent (ADD10)
  890. * Stop detection (STOPF)
  891. * Byte transfer finished (BTF)
  892. * Receive buffer not empty (RXNE)
  893. * Transmit buffer empty (TXE)
  894. * @rmtoll CR2 ITEVTEN LL_I2C_DisableIT_EVT
  895. * @param I2Cx I2C Instance.
  896. * @retval None
  897. */
  898. __STATIC_INLINE void LL_I2C_DisableIT_EVT(I2C_TypeDef *I2Cx)
  899. {
  900. CLEAR_BIT(I2Cx->CR2, I2C_CR2_ITEVTEN);
  901. }
  902. /**
  903. * @brief Check if Events interrupts are enabled or disabled.
  904. * @rmtoll CR2 ITEVTEN LL_I2C_IsEnabledIT_EVT
  905. * @param I2Cx I2C Instance.
  906. * @retval State of bit (1 or 0).
  907. */
  908. __STATIC_INLINE uint32_t LL_I2C_IsEnabledIT_EVT(I2C_TypeDef *I2Cx)
  909. {
  910. return (READ_BIT(I2Cx->CR2, I2C_CR2_ITEVTEN) == (I2C_CR2_ITEVTEN));
  911. }
  912. /**
  913. * @brief Enable Buffer interrupts.
  914. * @note Any of these Buffer events will generate interrupt if Events interrupts are enabled too(using unitary function @ref LL_I2C_EnableIT_EVT()) :
  915. * Receive buffer not empty (RXNE)
  916. * Transmit buffer empty (TXE)
  917. * @rmtoll CR2 ITBUFEN LL_I2C_EnableIT_BUF
  918. * @param I2Cx I2C Instance.
  919. * @retval None
  920. */
  921. __STATIC_INLINE void LL_I2C_EnableIT_BUF(I2C_TypeDef *I2Cx)
  922. {
  923. SET_BIT(I2Cx->CR2, I2C_CR2_ITBUFEN);
  924. }
  925. /**
  926. * @brief Disable Buffer interrupts.
  927. * @note Any of these Buffer events will generate interrupt :
  928. * Receive buffer not empty (RXNE)
  929. * Transmit buffer empty (TXE)
  930. * @rmtoll CR2 ITBUFEN LL_I2C_DisableIT_BUF
  931. * @param I2Cx I2C Instance.
  932. * @retval None
  933. */
  934. __STATIC_INLINE void LL_I2C_DisableIT_BUF(I2C_TypeDef *I2Cx)
  935. {
  936. CLEAR_BIT(I2Cx->CR2, I2C_CR2_ITBUFEN);
  937. }
  938. /**
  939. * @brief Check if Buffer interrupts are enabled or disabled.
  940. * @rmtoll CR2 ITBUFEN LL_I2C_IsEnabledIT_BUF
  941. * @param I2Cx I2C Instance.
  942. * @retval State of bit (1 or 0).
  943. */
  944. __STATIC_INLINE uint32_t LL_I2C_IsEnabledIT_BUF(I2C_TypeDef *I2Cx)
  945. {
  946. return (READ_BIT(I2Cx->CR2, I2C_CR2_ITBUFEN) == (I2C_CR2_ITBUFEN));
  947. }
  948. /**
  949. * @brief Enable Error interrupts.
  950. * @note Macro @ref IS_SMBUS_ALL_INSTANCE(I2Cx) can be used to check whether or not
  951. * SMBus feature is supported by the I2Cx Instance.
  952. * @note Any of these errors will generate interrupt :
  953. * Bus Error detection (BERR)
  954. * Arbitration Loss (ARLO)
  955. * Acknowledge Failure(AF)
  956. * Overrun/Underrun (OVR)
  957. * SMBus Timeout detection (TIMEOUT)
  958. * SMBus PEC error detection (PECERR)
  959. * SMBus Alert pin event detection (SMBALERT)
  960. * @rmtoll CR2 ITERREN LL_I2C_EnableIT_ERR
  961. * @param I2Cx I2C Instance.
  962. * @retval None
  963. */
  964. __STATIC_INLINE void LL_I2C_EnableIT_ERR(I2C_TypeDef *I2Cx)
  965. {
  966. SET_BIT(I2Cx->CR2, I2C_CR2_ITERREN);
  967. }
  968. /**
  969. * @brief Disable Error interrupts.
  970. * @note Macro @ref IS_SMBUS_ALL_INSTANCE(I2Cx) can be used to check whether or not
  971. * SMBus feature is supported by the I2Cx Instance.
  972. * @note Any of these errors will generate interrupt :
  973. * Bus Error detection (BERR)
  974. * Arbitration Loss (ARLO)
  975. * Acknowledge Failure(AF)
  976. * Overrun/Underrun (OVR)
  977. * SMBus Timeout detection (TIMEOUT)
  978. * SMBus PEC error detection (PECERR)
  979. * SMBus Alert pin event detection (SMBALERT)
  980. * @rmtoll CR2 ITERREN LL_I2C_DisableIT_ERR
  981. * @param I2Cx I2C Instance.
  982. * @retval None
  983. */
  984. __STATIC_INLINE void LL_I2C_DisableIT_ERR(I2C_TypeDef *I2Cx)
  985. {
  986. CLEAR_BIT(I2Cx->CR2, I2C_CR2_ITERREN);
  987. }
  988. /**
  989. * @brief Check if Error interrupts are enabled or disabled.
  990. * @rmtoll CR2 ITERREN LL_I2C_IsEnabledIT_ERR
  991. * @param I2Cx I2C Instance.
  992. * @retval State of bit (1 or 0).
  993. */
  994. __STATIC_INLINE uint32_t LL_I2C_IsEnabledIT_ERR(I2C_TypeDef *I2Cx)
  995. {
  996. return (READ_BIT(I2Cx->CR2, I2C_CR2_ITERREN) == (I2C_CR2_ITERREN));
  997. }
  998. /**
  999. * @}
  1000. */
  1001. /** @defgroup I2C_LL_EF_FLAG_management FLAG_management
  1002. * @{
  1003. */
  1004. /**
  1005. * @brief Indicate the status of Transmit data register empty flag.
  1006. * @note RESET: When next data is written in Transmit data register.
  1007. * SET: When Transmit data register is empty.
  1008. * @rmtoll SR1 TXE LL_I2C_IsActiveFlag_TXE
  1009. * @param I2Cx I2C Instance.
  1010. * @retval State of bit (1 or 0).
  1011. */
  1012. __STATIC_INLINE uint32_t LL_I2C_IsActiveFlag_TXE(I2C_TypeDef *I2Cx)
  1013. {
  1014. return (READ_BIT(I2Cx->SR1, I2C_SR1_TXE) == (I2C_SR1_TXE));
  1015. }
  1016. /**
  1017. * @brief Indicate the status of Byte Transfer Finished flag.
  1018. * RESET: When Data byte transfer not done.
  1019. * SET: When Data byte transfer succeeded.
  1020. * @rmtoll SR1 BTF LL_I2C_IsActiveFlag_BTF
  1021. * @param I2Cx I2C Instance.
  1022. * @retval State of bit (1 or 0).
  1023. */
  1024. __STATIC_INLINE uint32_t LL_I2C_IsActiveFlag_BTF(I2C_TypeDef *I2Cx)
  1025. {
  1026. return (READ_BIT(I2Cx->SR1, I2C_SR1_BTF) == (I2C_SR1_BTF));
  1027. }
  1028. /**
  1029. * @brief Indicate the status of Receive data register not empty flag.
  1030. * @note RESET: When Receive data register is read.
  1031. * SET: When the received data is copied in Receive data register.
  1032. * @rmtoll SR1 RXNE LL_I2C_IsActiveFlag_RXNE
  1033. * @param I2Cx I2C Instance.
  1034. * @retval State of bit (1 or 0).
  1035. */
  1036. __STATIC_INLINE uint32_t LL_I2C_IsActiveFlag_RXNE(I2C_TypeDef *I2Cx)
  1037. {
  1038. return (READ_BIT(I2Cx->SR1, I2C_SR1_RXNE) == (I2C_SR1_RXNE));
  1039. }
  1040. /**
  1041. * @brief Indicate the status of Start Bit (master mode).
  1042. * @note RESET: When No Start condition.
  1043. * SET: When Start condition is generated.
  1044. * @rmtoll SR1 SB LL_I2C_IsActiveFlag_SB
  1045. * @param I2Cx I2C Instance.
  1046. * @retval State of bit (1 or 0).
  1047. */
  1048. __STATIC_INLINE uint32_t LL_I2C_IsActiveFlag_SB(I2C_TypeDef *I2Cx)
  1049. {
  1050. return (READ_BIT(I2Cx->SR1, I2C_SR1_SB) == (I2C_SR1_SB));
  1051. }
  1052. /**
  1053. * @brief Indicate the status of Address sent (master mode) or Address matched flag (slave mode).
  1054. * @note RESET: Clear default value.
  1055. * SET: When the address is fully sent (master mode) or when the received slave address matched with one of the enabled slave address (slave mode).
  1056. * @rmtoll SR1 ADDR LL_I2C_IsActiveFlag_ADDR
  1057. * @param I2Cx I2C Instance.
  1058. * @retval State of bit (1 or 0).
  1059. */
  1060. __STATIC_INLINE uint32_t LL_I2C_IsActiveFlag_ADDR(I2C_TypeDef *I2Cx)
  1061. {
  1062. return (READ_BIT(I2Cx->SR1, I2C_SR1_ADDR) == (I2C_SR1_ADDR));
  1063. }
  1064. /**
  1065. * @brief Indicate the status of 10-bit header sent (master mode).
  1066. * @note RESET: When no ADD10 event occurred.
  1067. * SET: When the master has sent the first address byte (header).
  1068. * @rmtoll SR1 ADD10 LL_I2C_IsActiveFlag_ADD10
  1069. * @param I2Cx I2C Instance.
  1070. * @retval State of bit (1 or 0).
  1071. */
  1072. __STATIC_INLINE uint32_t LL_I2C_IsActiveFlag_ADD10(I2C_TypeDef *I2Cx)
  1073. {
  1074. return (READ_BIT(I2Cx->SR1, I2C_SR1_ADD10) == (I2C_SR1_ADD10));
  1075. }
  1076. /**
  1077. * @brief Indicate the status of Acknowledge failure flag.
  1078. * @note RESET: No acknowledge failure.
  1079. * SET: When an acknowledge failure is received after a byte transmission.
  1080. * @rmtoll SR1 AF LL_I2C_IsActiveFlag_AF
  1081. * @param I2Cx I2C Instance.
  1082. * @retval State of bit (1 or 0).
  1083. */
  1084. __STATIC_INLINE uint32_t LL_I2C_IsActiveFlag_AF(I2C_TypeDef *I2Cx)
  1085. {
  1086. return (READ_BIT(I2Cx->SR1, I2C_SR1_AF) == (I2C_SR1_AF));
  1087. }
  1088. /**
  1089. * @brief Indicate the status of Stop detection flag (slave mode).
  1090. * @note RESET: Clear default value.
  1091. * SET: When a Stop condition is detected.
  1092. * @rmtoll SR1 STOPF LL_I2C_IsActiveFlag_STOP
  1093. * @param I2Cx I2C Instance.
  1094. * @retval State of bit (1 or 0).
  1095. */
  1096. __STATIC_INLINE uint32_t LL_I2C_IsActiveFlag_STOP(I2C_TypeDef *I2Cx)
  1097. {
  1098. return (READ_BIT(I2Cx->SR1, I2C_SR1_STOPF) == (I2C_SR1_STOPF));
  1099. }
  1100. /**
  1101. * @brief Indicate the status of Bus error flag.
  1102. * @note RESET: Clear default value.
  1103. * SET: When a misplaced Start or Stop condition is detected.
  1104. * @rmtoll SR1 BERR LL_I2C_IsActiveFlag_BERR
  1105. * @param I2Cx I2C Instance.
  1106. * @retval State of bit (1 or 0).
  1107. */
  1108. __STATIC_INLINE uint32_t LL_I2C_IsActiveFlag_BERR(I2C_TypeDef *I2Cx)
  1109. {
  1110. return (READ_BIT(I2Cx->SR1, I2C_SR1_BERR) == (I2C_SR1_BERR));
  1111. }
  1112. /**
  1113. * @brief Indicate the status of Arbitration lost flag.
  1114. * @note RESET: Clear default value.
  1115. * SET: When arbitration lost.
  1116. * @rmtoll SR1 ARLO LL_I2C_IsActiveFlag_ARLO
  1117. * @param I2Cx I2C Instance.
  1118. * @retval State of bit (1 or 0).
  1119. */
  1120. __STATIC_INLINE uint32_t LL_I2C_IsActiveFlag_ARLO(I2C_TypeDef *I2Cx)
  1121. {
  1122. return (READ_BIT(I2Cx->SR1, I2C_SR1_ARLO) == (I2C_SR1_ARLO));
  1123. }
  1124. /**
  1125. * @brief Indicate the status of Overrun/Underrun flag.
  1126. * @note RESET: Clear default value.
  1127. * SET: When an overrun/underrun error occurs (Clock Stretching Disabled).
  1128. * @rmtoll SR1 OVR LL_I2C_IsActiveFlag_OVR
  1129. * @param I2Cx I2C Instance.
  1130. * @retval State of bit (1 or 0).
  1131. */
  1132. __STATIC_INLINE uint32_t LL_I2C_IsActiveFlag_OVR(I2C_TypeDef *I2Cx)
  1133. {
  1134. return (READ_BIT(I2Cx->SR1, I2C_SR1_OVR) == (I2C_SR1_OVR));
  1135. }
  1136. /**
  1137. * @brief Indicate the status of SMBus PEC error flag in reception.
  1138. * @note Macro @ref IS_SMBUS_ALL_INSTANCE(I2Cx) can be used to check whether or not
  1139. * SMBus feature is supported by the I2Cx Instance.
  1140. * @rmtoll SR1 PECERR LL_I2C_IsActiveSMBusFlag_PECERR
  1141. * @param I2Cx I2C Instance.
  1142. * @retval State of bit (1 or 0).
  1143. */
  1144. __STATIC_INLINE uint32_t LL_I2C_IsActiveSMBusFlag_PECERR(I2C_TypeDef *I2Cx)
  1145. {
  1146. return (READ_BIT(I2Cx->SR1, I2C_SR1_PECERR) == (I2C_SR1_PECERR));
  1147. }
  1148. /**
  1149. * @brief Indicate the status of SMBus Timeout detection flag.
  1150. * @note Macro @ref IS_SMBUS_ALL_INSTANCE(I2Cx) can be used to check whether or not
  1151. * SMBus feature is supported by the I2Cx Instance.
  1152. * @rmtoll SR1 TIMEOUT LL_I2C_IsActiveSMBusFlag_TIMEOUT
  1153. * @param I2Cx I2C Instance.
  1154. * @retval State of bit (1 or 0).
  1155. */
  1156. __STATIC_INLINE uint32_t LL_I2C_IsActiveSMBusFlag_TIMEOUT(I2C_TypeDef *I2Cx)
  1157. {
  1158. return (READ_BIT(I2Cx->SR1, I2C_SR1_TIMEOUT) == (I2C_SR1_TIMEOUT));
  1159. }
  1160. /**
  1161. * @brief Indicate the status of SMBus alert flag.
  1162. * @note Macro @ref IS_SMBUS_ALL_INSTANCE(I2Cx) can be used to check whether or not
  1163. * SMBus feature is supported by the I2Cx Instance.
  1164. * @rmtoll SR1 SMBALERT LL_I2C_IsActiveSMBusFlag_ALERT
  1165. * @param I2Cx I2C Instance.
  1166. * @retval State of bit (1 or 0).
  1167. */
  1168. __STATIC_INLINE uint32_t LL_I2C_IsActiveSMBusFlag_ALERT(I2C_TypeDef *I2Cx)
  1169. {
  1170. return (READ_BIT(I2Cx->SR1, I2C_SR1_SMBALERT) == (I2C_SR1_SMBALERT));
  1171. }
  1172. /**
  1173. * @brief Indicate the status of Bus Busy flag.
  1174. * @note RESET: Clear default value.
  1175. * SET: When a Start condition is detected.
  1176. * @rmtoll SR2 BUSY LL_I2C_IsActiveFlag_BUSY
  1177. * @param I2Cx I2C Instance.
  1178. * @retval State of bit (1 or 0).
  1179. */
  1180. __STATIC_INLINE uint32_t LL_I2C_IsActiveFlag_BUSY(I2C_TypeDef *I2Cx)
  1181. {
  1182. return (READ_BIT(I2Cx->SR2, I2C_SR2_BUSY) == (I2C_SR2_BUSY));
  1183. }
  1184. /**
  1185. * @brief Indicate the status of Dual flag.
  1186. * @note RESET: Received address matched with OAR1.
  1187. * SET: Received address matched with OAR2.
  1188. * @rmtoll SR2 DUALF LL_I2C_IsActiveFlag_DUAL
  1189. * @param I2Cx I2C Instance.
  1190. * @retval State of bit (1 or 0).
  1191. */
  1192. __STATIC_INLINE uint32_t LL_I2C_IsActiveFlag_DUAL(I2C_TypeDef *I2Cx)
  1193. {
  1194. return (READ_BIT(I2Cx->SR2, I2C_SR2_DUALF) == (I2C_SR2_DUALF));
  1195. }
  1196. /**
  1197. * @brief Indicate the status of SMBus Host address reception (Slave mode).
  1198. * @note Macro @ref IS_SMBUS_ALL_INSTANCE(I2Cx) can be used to check whether or not
  1199. * SMBus feature is supported by the I2Cx Instance.
  1200. * @note RESET: No SMBus Host address
  1201. * SET: SMBus Host address received.
  1202. * @note This status is cleared by hardware after a STOP condition or repeated START condition.
  1203. * @rmtoll SR2 SMBHOST LL_I2C_IsActiveSMBusFlag_SMBHOST
  1204. * @param I2Cx I2C Instance.
  1205. * @retval State of bit (1 or 0).
  1206. */
  1207. __STATIC_INLINE uint32_t LL_I2C_IsActiveSMBusFlag_SMBHOST(I2C_TypeDef *I2Cx)
  1208. {
  1209. return (READ_BIT(I2Cx->SR2, I2C_SR2_SMBHOST) == (I2C_SR2_SMBHOST));
  1210. }
  1211. /**
  1212. * @brief Indicate the status of SMBus Device default address reception (Slave mode).
  1213. * @note Macro @ref IS_SMBUS_ALL_INSTANCE(I2Cx) can be used to check whether or not
  1214. * SMBus feature is supported by the I2Cx Instance.
  1215. * @note RESET: No SMBus Device default address
  1216. * SET: SMBus Device default address received.
  1217. * @note This status is cleared by hardware after a STOP condition or repeated START condition.
  1218. * @rmtoll SR2 SMBDEFAULT LL_I2C_IsActiveSMBusFlag_SMBDEFAULT
  1219. * @param I2Cx I2C Instance.
  1220. * @retval State of bit (1 or 0).
  1221. */
  1222. __STATIC_INLINE uint32_t LL_I2C_IsActiveSMBusFlag_SMBDEFAULT(I2C_TypeDef *I2Cx)
  1223. {
  1224. return (READ_BIT(I2Cx->SR2, I2C_SR2_SMBDEFAULT) == (I2C_SR2_SMBDEFAULT));
  1225. }
  1226. /**
  1227. * @brief Indicate the status of General call address reception (Slave mode).
  1228. * @note RESET: No Generall call address
  1229. * SET: General call address received.
  1230. * @note This status is cleared by hardware after a STOP condition or repeated START condition.
  1231. * @rmtoll SR2 GENCALL LL_I2C_IsActiveFlag_GENCALL
  1232. * @param I2Cx I2C Instance.
  1233. * @retval State of bit (1 or 0).
  1234. */
  1235. __STATIC_INLINE uint32_t LL_I2C_IsActiveFlag_GENCALL(I2C_TypeDef *I2Cx)
  1236. {
  1237. return (READ_BIT(I2Cx->SR2, I2C_SR2_GENCALL) == (I2C_SR2_GENCALL));
  1238. }
  1239. /**
  1240. * @brief Indicate the status of Master/Slave flag.
  1241. * @note RESET: Slave Mode.
  1242. * SET: Master Mode.
  1243. * @rmtoll SR2 MSL LL_I2C_IsActiveFlag_MSL
  1244. * @param I2Cx I2C Instance.
  1245. * @retval State of bit (1 or 0).
  1246. */
  1247. __STATIC_INLINE uint32_t LL_I2C_IsActiveFlag_MSL(I2C_TypeDef *I2Cx)
  1248. {
  1249. return (READ_BIT(I2Cx->SR2, I2C_SR2_MSL) == (I2C_SR2_MSL));
  1250. }
  1251. /**
  1252. * @brief Clear Address Matched flag.
  1253. * @note Clearing this flag is done by a read access to the I2Cx_SR1
  1254. * register followed by a read access to the I2Cx_SR2 register.
  1255. * @rmtoll SR1 ADDR LL_I2C_ClearFlag_ADDR
  1256. * @param I2Cx I2C Instance.
  1257. * @retval None
  1258. */
  1259. __STATIC_INLINE void LL_I2C_ClearFlag_ADDR(I2C_TypeDef *I2Cx)
  1260. {
  1261. __IO uint32_t tmpreg;
  1262. tmpreg = I2Cx->SR1;
  1263. (void) tmpreg;
  1264. tmpreg = I2Cx->SR2;
  1265. (void) tmpreg;
  1266. }
  1267. /**
  1268. * @brief Clear Acknowledge failure flag.
  1269. * @rmtoll SR1 AF LL_I2C_ClearFlag_AF
  1270. * @param I2Cx I2C Instance.
  1271. * @retval None
  1272. */
  1273. __STATIC_INLINE void LL_I2C_ClearFlag_AF(I2C_TypeDef *I2Cx)
  1274. {
  1275. CLEAR_BIT(I2Cx->SR1, I2C_SR1_AF);
  1276. }
  1277. /**
  1278. * @brief Clear Stop detection flag.
  1279. * @note Clearing this flag is done by a read access to the I2Cx_SR1
  1280. * register followed by a write access to I2Cx_CR1 register.
  1281. * @rmtoll SR1 STOPF LL_I2C_ClearFlag_STOP\n
  1282. * CR1 PE LL_I2C_ClearFlag_STOP
  1283. * @param I2Cx I2C Instance.
  1284. * @retval None
  1285. */
  1286. __STATIC_INLINE void LL_I2C_ClearFlag_STOP(I2C_TypeDef *I2Cx)
  1287. {
  1288. __IO uint32_t tmpreg;
  1289. tmpreg = I2Cx->SR1;
  1290. (void) tmpreg;
  1291. SET_BIT(I2Cx->CR1, I2C_CR1_PE);
  1292. }
  1293. /**
  1294. * @brief Clear Bus error flag.
  1295. * @rmtoll SR1 BERR LL_I2C_ClearFlag_BERR
  1296. * @param I2Cx I2C Instance.
  1297. * @retval None
  1298. */
  1299. __STATIC_INLINE void LL_I2C_ClearFlag_BERR(I2C_TypeDef *I2Cx)
  1300. {
  1301. CLEAR_BIT(I2Cx->SR1, I2C_SR1_BERR);
  1302. }
  1303. /**
  1304. * @brief Clear Arbitration lost flag.
  1305. * @rmtoll SR1 ARLO LL_I2C_ClearFlag_ARLO
  1306. * @param I2Cx I2C Instance.
  1307. * @retval None
  1308. */
  1309. __STATIC_INLINE void LL_I2C_ClearFlag_ARLO(I2C_TypeDef *I2Cx)
  1310. {
  1311. CLEAR_BIT(I2Cx->SR1, I2C_SR1_ARLO);
  1312. }
  1313. /**
  1314. * @brief Clear Overrun/Underrun flag.
  1315. * @rmtoll SR1 OVR LL_I2C_ClearFlag_OVR
  1316. * @param I2Cx I2C Instance.
  1317. * @retval None
  1318. */
  1319. __STATIC_INLINE void LL_I2C_ClearFlag_OVR(I2C_TypeDef *I2Cx)
  1320. {
  1321. CLEAR_BIT(I2Cx->SR1, I2C_SR1_OVR);
  1322. }
  1323. /**
  1324. * @brief Clear SMBus PEC error flag.
  1325. * @rmtoll SR1 PECERR LL_I2C_ClearSMBusFlag_PECERR
  1326. * @param I2Cx I2C Instance.
  1327. * @retval None
  1328. */
  1329. __STATIC_INLINE void LL_I2C_ClearSMBusFlag_PECERR(I2C_TypeDef *I2Cx)
  1330. {
  1331. CLEAR_BIT(I2Cx->SR1, I2C_SR1_PECERR);
  1332. }
  1333. /**
  1334. * @brief Clear SMBus Timeout detection flag.
  1335. * @note Macro @ref IS_SMBUS_ALL_INSTANCE(I2Cx) can be used to check whether or not
  1336. * SMBus feature is supported by the I2Cx Instance.
  1337. * @rmtoll SR1 TIMEOUT LL_I2C_ClearSMBusFlag_TIMEOUT
  1338. * @param I2Cx I2C Instance.
  1339. * @retval None
  1340. */
  1341. __STATIC_INLINE void LL_I2C_ClearSMBusFlag_TIMEOUT(I2C_TypeDef *I2Cx)
  1342. {
  1343. CLEAR_BIT(I2Cx->SR1, I2C_SR1_TIMEOUT);
  1344. }
  1345. /**
  1346. * @brief Clear SMBus Alert flag.
  1347. * @note Macro @ref IS_SMBUS_ALL_INSTANCE(I2Cx) can be used to check whether or not
  1348. * SMBus feature is supported by the I2Cx Instance.
  1349. * @rmtoll SR1 SMBALERT LL_I2C_ClearSMBusFlag_ALERT
  1350. * @param I2Cx I2C Instance.
  1351. * @retval None
  1352. */
  1353. __STATIC_INLINE void LL_I2C_ClearSMBusFlag_ALERT(I2C_TypeDef *I2Cx)
  1354. {
  1355. CLEAR_BIT(I2Cx->SR1, I2C_SR1_SMBALERT);
  1356. }
  1357. /**
  1358. * @}
  1359. */
  1360. /** @defgroup I2C_LL_EF_Data_Management Data_Management
  1361. * @{
  1362. */
  1363. /**
  1364. * @brief Enable Reset of I2C peripheral.
  1365. * @rmtoll CR1 SWRST LL_I2C_EnableReset
  1366. * @param I2Cx I2C Instance.
  1367. * @retval None
  1368. */
  1369. __STATIC_INLINE void LL_I2C_EnableReset(I2C_TypeDef *I2Cx)
  1370. {
  1371. SET_BIT(I2Cx->CR1, I2C_CR1_SWRST);
  1372. }
  1373. /**
  1374. * @brief Disable Reset of I2C peripheral.
  1375. * @rmtoll CR1 SWRST LL_I2C_DisableReset
  1376. * @param I2Cx I2C Instance.
  1377. * @retval None
  1378. */
  1379. __STATIC_INLINE void LL_I2C_DisableReset(I2C_TypeDef *I2Cx)
  1380. {
  1381. CLEAR_BIT(I2Cx->CR1, I2C_CR1_SWRST);
  1382. }
  1383. /**
  1384. * @brief Check if the I2C peripheral is under reset state or not.
  1385. * @rmtoll CR1 SWRST LL_I2C_IsResetEnabled
  1386. * @param I2Cx I2C Instance.
  1387. * @retval State of bit (1 or 0).
  1388. */
  1389. __STATIC_INLINE uint32_t LL_I2C_IsResetEnabled(I2C_TypeDef *I2Cx)
  1390. {
  1391. return (READ_BIT(I2Cx->CR1, I2C_CR1_SWRST) == (I2C_CR1_SWRST));
  1392. }
  1393. /**
  1394. * @brief Prepare the generation of a ACKnowledge or Non ACKnowledge condition after the address receive match code or next received byte.
  1395. * @note Usage in Slave or Master mode.
  1396. * @rmtoll CR1 ACK LL_I2C_AcknowledgeNextData
  1397. * @param I2Cx I2C Instance.
  1398. * @param TypeAcknowledge This parameter can be one of the following values:
  1399. * @arg @ref LL_I2C_ACK
  1400. * @arg @ref LL_I2C_NACK
  1401. * @retval None
  1402. */
  1403. __STATIC_INLINE void LL_I2C_AcknowledgeNextData(I2C_TypeDef *I2Cx, uint32_t TypeAcknowledge)
  1404. {
  1405. MODIFY_REG(I2Cx->CR1, I2C_CR1_ACK, TypeAcknowledge);
  1406. }
  1407. /**
  1408. * @brief Generate a START or RESTART condition
  1409. * @note The START bit can be set even if bus is BUSY or I2C is in slave mode.
  1410. * This action has no effect when RELOAD is set.
  1411. * @rmtoll CR1 START LL_I2C_GenerateStartCondition
  1412. * @param I2Cx I2C Instance.
  1413. * @retval None
  1414. */
  1415. __STATIC_INLINE void LL_I2C_GenerateStartCondition(I2C_TypeDef *I2Cx)
  1416. {
  1417. SET_BIT(I2Cx->CR1, I2C_CR1_START);
  1418. }
  1419. /**
  1420. * @brief Generate a STOP condition after the current byte transfer (master mode).
  1421. * @rmtoll CR1 STOP LL_I2C_GenerateStopCondition
  1422. * @param I2Cx I2C Instance.
  1423. * @retval None
  1424. */
  1425. __STATIC_INLINE void LL_I2C_GenerateStopCondition(I2C_TypeDef *I2Cx)
  1426. {
  1427. SET_BIT(I2Cx->CR1, I2C_CR1_STOP);
  1428. }
  1429. /**
  1430. * @brief Enable bit POS (master/host mode).
  1431. * @note In that case, the ACK bit controls the (N)ACK of the next byte received or the PEC bit indicates that the next byte in shift register is a PEC.
  1432. * @rmtoll CR1 POS LL_I2C_EnableBitPOS
  1433. * @param I2Cx I2C Instance.
  1434. * @retval None
  1435. */
  1436. __STATIC_INLINE void LL_I2C_EnableBitPOS(I2C_TypeDef *I2Cx)
  1437. {
  1438. SET_BIT(I2Cx->CR1, I2C_CR1_POS);
  1439. }
  1440. /**
  1441. * @brief Disable bit POS (master/host mode).
  1442. * @note In that case, the ACK bit controls the (N)ACK of the current byte received or the PEC bit indicates that the current byte in shift register is a PEC.
  1443. * @rmtoll CR1 POS LL_I2C_DisableBitPOS
  1444. * @param I2Cx I2C Instance.
  1445. * @retval None
  1446. */
  1447. __STATIC_INLINE void LL_I2C_DisableBitPOS(I2C_TypeDef *I2Cx)
  1448. {
  1449. CLEAR_BIT(I2Cx->CR1, I2C_CR1_POS);
  1450. }
  1451. /**
  1452. * @brief Check if bit POS is enabled or disabled.
  1453. * @rmtoll CR1 POS LL_I2C_IsEnabledBitPOS
  1454. * @param I2Cx I2C Instance.
  1455. * @retval State of bit (1 or 0).
  1456. */
  1457. __STATIC_INLINE uint32_t LL_I2C_IsEnabledBitPOS(I2C_TypeDef *I2Cx)
  1458. {
  1459. return (READ_BIT(I2Cx->CR1, I2C_CR1_POS) == (I2C_CR1_POS));
  1460. }
  1461. /**
  1462. * @brief Indicate the value of transfer direction.
  1463. * @note RESET: Bus is in read transfer (peripheral point of view).
  1464. * SET: Bus is in write transfer (peripheral point of view).
  1465. * @rmtoll SR2 TRA LL_I2C_GetTransferDirection
  1466. * @param I2Cx I2C Instance.
  1467. * @retval Returned value can be one of the following values:
  1468. * @arg @ref LL_I2C_DIRECTION_WRITE
  1469. * @arg @ref LL_I2C_DIRECTION_READ
  1470. */
  1471. __STATIC_INLINE uint32_t LL_I2C_GetTransferDirection(I2C_TypeDef *I2Cx)
  1472. {
  1473. return (uint32_t)(READ_BIT(I2Cx->SR2, I2C_SR2_TRA));
  1474. }
  1475. /**
  1476. * @brief Enable DMA last transfer.
  1477. * @note This action mean that next DMA EOT is the last transfer.
  1478. * @rmtoll CR2 LAST LL_I2C_EnableLastDMA
  1479. * @param I2Cx I2C Instance.
  1480. * @retval None
  1481. */
  1482. __STATIC_INLINE void LL_I2C_EnableLastDMA(I2C_TypeDef *I2Cx)
  1483. {
  1484. SET_BIT(I2Cx->CR2, I2C_CR2_LAST);
  1485. }
  1486. /**
  1487. * @brief Disable DMA last transfer.
  1488. * @note This action mean that next DMA EOT is not the last transfer.
  1489. * @rmtoll CR2 LAST LL_I2C_DisableLastDMA
  1490. * @param I2Cx I2C Instance.
  1491. * @retval None
  1492. */
  1493. __STATIC_INLINE void LL_I2C_DisableLastDMA(I2C_TypeDef *I2Cx)
  1494. {
  1495. CLEAR_BIT(I2Cx->CR2, I2C_CR2_LAST);
  1496. }
  1497. /**
  1498. * @brief Check if DMA last transfer is enabled or disabled.
  1499. * @rmtoll CR2 LAST LL_I2C_IsEnabledLastDMA
  1500. * @param I2Cx I2C Instance.
  1501. * @retval State of bit (1 or 0).
  1502. */
  1503. __STATIC_INLINE uint32_t LL_I2C_IsEnabledLastDMA(I2C_TypeDef *I2Cx)
  1504. {
  1505. return (READ_BIT(I2Cx->CR2, I2C_CR2_LAST) == (I2C_CR2_LAST));
  1506. }
  1507. /**
  1508. * @brief Enable transfer or internal comparison of the SMBus Packet Error byte (transmission or reception mode).
  1509. * @note Macro @ref IS_SMBUS_ALL_INSTANCE(I2Cx) can be used to check whether or not
  1510. * SMBus feature is supported by the I2Cx Instance.
  1511. * @note This feature is cleared by hardware when the PEC byte is transferred or compared,
  1512. * or by a START or STOP condition, it is also cleared by software.
  1513. * @rmtoll CR1 PEC LL_I2C_EnableSMBusPECCompare
  1514. * @param I2Cx I2C Instance.
  1515. * @retval None
  1516. */
  1517. __STATIC_INLINE void LL_I2C_EnableSMBusPECCompare(I2C_TypeDef *I2Cx)
  1518. {
  1519. SET_BIT(I2Cx->CR1, I2C_CR1_PEC);
  1520. }
  1521. /**
  1522. * @brief Disable transfer or internal comparison of the SMBus Packet Error byte (transmission or reception mode).
  1523. * @note Macro @ref IS_SMBUS_ALL_INSTANCE(I2Cx) can be used to check whether or not
  1524. * SMBus feature is supported by the I2Cx Instance.
  1525. * @rmtoll CR1 PEC LL_I2C_DisableSMBusPECCompare
  1526. * @param I2Cx I2C Instance.
  1527. * @retval None
  1528. */
  1529. __STATIC_INLINE void LL_I2C_DisableSMBusPECCompare(I2C_TypeDef *I2Cx)
  1530. {
  1531. CLEAR_BIT(I2Cx->CR1, I2C_CR1_PEC);
  1532. }
  1533. /**
  1534. * @brief Check if the SMBus Packet Error byte transfer or internal comparison is requested or not.
  1535. * @note Macro @ref IS_SMBUS_ALL_INSTANCE(I2Cx) can be used to check whether or not
  1536. * SMBus feature is supported by the I2Cx Instance.
  1537. * @rmtoll CR1 PEC LL_I2C_IsEnabledSMBusPECCompare
  1538. * @param I2Cx I2C Instance.
  1539. * @retval State of bit (1 or 0).
  1540. */
  1541. __STATIC_INLINE uint32_t LL_I2C_IsEnabledSMBusPECCompare(I2C_TypeDef *I2Cx)
  1542. {
  1543. return (READ_BIT(I2Cx->CR1, I2C_CR1_PEC) == (I2C_CR1_PEC));
  1544. }
  1545. /**
  1546. * @brief Get the SMBus Packet Error byte calculated.
  1547. * @note Macro @ref IS_SMBUS_ALL_INSTANCE(I2Cx) can be used to check whether or not
  1548. * SMBus feature is supported by the I2Cx Instance.
  1549. * @rmtoll SR2 PEC LL_I2C_GetSMBusPEC
  1550. * @param I2Cx I2C Instance.
  1551. * @retval Value between Min_Data=0x00 and Max_Data=0xFF
  1552. */
  1553. __STATIC_INLINE uint32_t LL_I2C_GetSMBusPEC(I2C_TypeDef *I2Cx)
  1554. {
  1555. return (uint32_t)(READ_BIT(I2Cx->SR2, I2C_SR2_PEC) >> I2C_SR2_PEC_Pos);
  1556. }
  1557. /**
  1558. * @brief Read Receive Data register.
  1559. * @rmtoll DR DR LL_I2C_ReceiveData8
  1560. * @param I2Cx I2C Instance.
  1561. * @retval Value between Min_Data=0x0 and Max_Data=0xFF
  1562. */
  1563. __STATIC_INLINE uint8_t LL_I2C_ReceiveData8(I2C_TypeDef *I2Cx)
  1564. {
  1565. return (uint8_t)(READ_BIT(I2Cx->DR, I2C_DR_DR));
  1566. }
  1567. /**
  1568. * @brief Write in Transmit Data Register .
  1569. * @rmtoll DR DR LL_I2C_TransmitData8
  1570. * @param I2Cx I2C Instance.
  1571. * @param Data Value between Min_Data=0x0 and Max_Data=0xFF
  1572. * @retval None
  1573. */
  1574. __STATIC_INLINE void LL_I2C_TransmitData8(I2C_TypeDef *I2Cx, uint8_t Data)
  1575. {
  1576. MODIFY_REG(I2Cx->DR, I2C_DR_DR, Data);
  1577. }
  1578. /**
  1579. * @}
  1580. */
  1581. #if defined(USE_FULL_LL_DRIVER)
  1582. /** @defgroup I2C_LL_EF_Init Initialization and de-initialization functions
  1583. * @{
  1584. */
  1585. uint32_t LL_I2C_Init(I2C_TypeDef *I2Cx, LL_I2C_InitTypeDef *I2C_InitStruct);
  1586. uint32_t LL_I2C_DeInit(I2C_TypeDef *I2Cx);
  1587. void LL_I2C_StructInit(LL_I2C_InitTypeDef *I2C_InitStruct);
  1588. /**
  1589. * @}
  1590. */
  1591. #endif /* USE_FULL_LL_DRIVER */
  1592. /**
  1593. * @}
  1594. */
  1595. /**
  1596. * @}
  1597. */
  1598. #endif /* I2C1 || I2C2 */
  1599. /**
  1600. * @}
  1601. */
  1602. #ifdef __cplusplus
  1603. }
  1604. #endif
  1605. #endif /* __STM32F1xx_LL_I2C_H */
  1606. /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/