stm32f1xx_ll_i2c.h 63 KB

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