stm32f1xx_ll_i2c.h 61 KB

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