py32f0xx_ll_i2c.h 47 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414
  1. /**
  2. ******************************************************************************
  3. * @file py32f0xx_ll_i2c.h
  4. * @author MCU Application Team
  5. * @brief Header file of I2C LL module.
  6. ******************************************************************************
  7. * @attention
  8. *
  9. * <h2><center>&copy; Copyright (c) 2023 Puya Semiconductor Co.
  10. * All rights reserved.</center></h2>
  11. *
  12. * This software component is licensed by Puya under BSD 3-Clause license,
  13. * the "License"; You may not use this file except in compliance with the
  14. * License. You may obtain a copy of the License at:
  15. * opensource.org/licenses/BSD-3-Clause
  16. *
  17. ******************************************************************************
  18. * @attention
  19. *
  20. * <h2><center>&copy; Copyright (c) 2016 STMicroelectronics.
  21. * All rights reserved.</center></h2>
  22. *
  23. * This software component is licensed by ST under BSD 3-Clause license,
  24. * the "License"; You may not use this file except in compliance with the
  25. * License. You may obtain a copy of the License at:
  26. * opensource.org/licenses/BSD-3-Clause
  27. *
  28. ******************************************************************************
  29. */
  30. /* Define to prevent recursive inclusion -------------------------------------*/
  31. #ifndef __PY32F0xx_LL_I2C_H
  32. #define __PY32F0xx_LL_I2C_H
  33. #ifdef __cplusplus
  34. extern "C" {
  35. #endif
  36. /* Includes ------------------------------------------------------------------*/
  37. #include "py32f0xx.h"
  38. /** @addtogroup PY32F0xx_LL_Driver
  39. * @{
  40. */
  41. #if defined (I2C1) || defined (I2C2)
  42. /** @defgroup I2C_LL I2C
  43. * @{
  44. */
  45. /* Private types -------------------------------------------------------------*/
  46. /* Private variables ---------------------------------------------------------*/
  47. /* Private constants ---------------------------------------------------------*/
  48. /** @defgroup I2C_LL_Private_Constants I2C Private Constants
  49. * @{
  50. */
  51. /* Defines used to perform compute and check in the macros */
  52. #define LL_I2C_MAX_SPEED_STANDARD 100000U
  53. #define LL_I2C_MAX_SPEED_FAST 400000U
  54. /**
  55. * @}
  56. */
  57. /* Private macros ------------------------------------------------------------*/
  58. #if defined(USE_FULL_LL_DRIVER)
  59. /** @defgroup I2C_LL_Private_Macros I2C Private Macros
  60. * @{
  61. */
  62. /**
  63. * @}
  64. */
  65. #endif /*USE_FULL_LL_DRIVER*/
  66. /* Exported types ------------------------------------------------------------*/
  67. #if defined(USE_FULL_LL_DRIVER)
  68. /** @defgroup I2C_LL_ES_INIT I2C Exported Init structure
  69. * @{
  70. */
  71. typedef struct
  72. {
  73. uint32_t ClockSpeed; /*!< Specifies the clock frequency.
  74. This parameter must be set to a value lower than 400kHz (in Hz)
  75. This feature can be modified afterwards using unitary function @ref LL_I2C_SetClockPeriod()
  76. or @ref LL_I2C_SetDutyCycle() or @ref LL_I2C_SetClockSpeedMode() or @ref LL_I2C_ConfigSpeed(). */
  77. uint32_t DutyCycle; /*!< Specifies the I2C fast mode duty cycle.
  78. This parameter can be a value of @ref I2C_LL_EC_DUTYCYCLE
  79. This feature can be modified afterwards using unitary function @ref LL_I2C_SetDutyCycle(). */
  80. uint32_t OwnAddress1; /*!< Specifies the device own address 1.
  81. This parameter must be a value between Min_Data = 0x00 and Max_Data = 0x3FF
  82. This feature can be modified afterwards using unitary function @ref LL_I2C_SetOwnAddress1(). */
  83. uint32_t TypeAcknowledge; /*!< Specifies the ACKnowledge or Non ACKnowledge condition after the address receive match code or next received byte.
  84. This parameter can be a value of @ref I2C_LL_EC_I2C_ACKNOWLEDGE
  85. This feature can be modified afterwards using unitary function @ref LL_I2C_AcknowledgeNextData(). */
  86. } LL_I2C_InitTypeDef;
  87. /**
  88. * @}
  89. */
  90. #endif /*USE_FULL_LL_DRIVER*/
  91. /* Exported constants --------------------------------------------------------*/
  92. /** @defgroup I2C_LL_Exported_Constants I2C Exported Constants
  93. * @{
  94. */
  95. /** @defgroup I2C_LL_EC_GET_FLAG Get Flags Defines
  96. * @brief Flags defines which can be used with LL_I2C_ReadReg function
  97. * @{
  98. */
  99. #define LL_I2C_SR1_SB I2C_SR1_SB /*!< Start Bit (master mode) */
  100. #define LL_I2C_SR1_ADDR I2C_SR1_ADDR /*!< Address sent (master mode) or
  101. Address matched flag (slave mode) */
  102. #define LL_I2C_SR1_BTF I2C_SR1_BTF /*!< Byte Transfer Finished flag */
  103. #define LL_I2C_SR1_STOPF I2C_SR1_STOPF /*!< Stop detection flag (slave mode) */
  104. #define LL_I2C_SR1_RXNE I2C_SR1_RXNE /*!< Data register not empty (receivers) */
  105. #define LL_I2C_SR1_TXE I2C_SR1_TXE /*!< Data register empty (transmitters) */
  106. #define LL_I2C_SR1_BERR I2C_SR1_BERR /*!< Bus error */
  107. #define LL_I2C_SR1_ARLO I2C_SR1_ARLO /*!< Arbitration lost */
  108. #define LL_I2C_SR1_AF I2C_SR1_AF /*!< Acknowledge failure flag */
  109. #define LL_I2C_SR1_OVR I2C_SR1_OVR /*!< Overrun/Underrun */
  110. #define LL_I2C_SR1_PECERR I2C_SR1_PECERR /*!< PEC Error in reception (SMBus mode) */
  111. #define LL_I2C_SR2_MSL I2C_SR2_MSL /*!< Master/Slave flag */
  112. #define LL_I2C_SR2_BUSY I2C_SR2_BUSY /*!< Bus busy flag */
  113. #define LL_I2C_SR2_TRA I2C_SR2_TRA /*!< Transmitter/receiver direction */
  114. #define LL_I2C_SR2_GENCALL I2C_SR2_GENCALL /*!< General call address (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_DUTYCYCLE Fast Mode Duty Cycle
  129. * @{
  130. */
  131. #define LL_I2C_DUTYCYCLE_2 0x00000000U /*!< I2C fast mode Tlow/Thigh = 2 */
  132. #define LL_I2C_DUTYCYCLE_16_9 I2C_CCR_DUTY /*!< I2C fast mode Tlow/Thigh = 16/9 */
  133. /**
  134. * @}
  135. */
  136. /** @defgroup I2C_LL_EC_CLOCK_SPEED_MODE Master Clock Speed Mode
  137. * @{
  138. */
  139. #define LL_I2C_CLOCK_SPEED_STANDARD_MODE 0x00000000U /*!< Master clock speed range is standard mode */
  140. #define LL_I2C_CLOCK_SPEED_FAST_MODE I2C_CCR_FS /*!< Master clock speed range is fast mode */
  141. /**
  142. * @}
  143. */
  144. /** @defgroup I2C_LL_EC_I2C_ACKNOWLEDGE Acknowledge Generation
  145. * @{
  146. */
  147. #define LL_I2C_ACK I2C_CR1_ACK /*!< ACK is sent after current received byte. */
  148. #define LL_I2C_NACK 0x00000000U /*!< NACK is sent after current received byte.*/
  149. /**
  150. * @}
  151. */
  152. /** @defgroup I2C_LL_EC_DIRECTION Read Write Direction
  153. * @{
  154. */
  155. #define LL_I2C_DIRECTION_WRITE I2C_SR2_TRA /*!< Bus is in write transfer */
  156. #define LL_I2C_DIRECTION_READ 0x00000000U /*!< Bus is in read transfer */
  157. /**
  158. * @}
  159. */
  160. /**
  161. * @}
  162. */
  163. /* Exported macro ------------------------------------------------------------*/
  164. /** @defgroup I2C_LL_Exported_Macros I2C Exported Macros
  165. * @{
  166. */
  167. /** @defgroup I2C_LL_EM_WRITE_READ Common Write and read registers Macros
  168. * @{
  169. */
  170. /**
  171. * @brief Write a value in I2C register
  172. * @param __INSTANCE__ I2C Instance
  173. * @param __REG__ Register to be written
  174. * @param __VALUE__ Value to be written in the register
  175. * @retval None
  176. */
  177. #define LL_I2C_WriteReg(__INSTANCE__, __REG__, __VALUE__) WRITE_REG(__INSTANCE__->__REG__, (__VALUE__))
  178. /**
  179. * @brief Read a value in I2C register
  180. * @param __INSTANCE__ I2C Instance
  181. * @param __REG__ Register to be read
  182. * @retval Register value
  183. */
  184. #define LL_I2C_ReadReg(__INSTANCE__, __REG__) READ_REG(__INSTANCE__->__REG__)
  185. /**
  186. * @}
  187. */
  188. /** @defgroup I2C_LL_EM_Exported_Macros_Helper Exported Macros Helper
  189. * @{
  190. */
  191. /**
  192. * @brief Convert Peripheral Clock Frequency in MHz.
  193. * @param __PCLK__ This parameter must be a value of peripheral clock (in Hz).
  194. * @retval Value of peripheral clock (in MHz)
  195. */
  196. #define __LL_I2C_FREQ_HZ_TO_MHZ(__PCLK__) (uint32_t)((__PCLK__)/1000000U)
  197. /**
  198. * @brief Convert Peripheral Clock Frequency in Hz.
  199. * @param __PCLK__ This parameter must be a value of peripheral clock (in MHz).
  200. * @retval Value of peripheral clock (in Hz)
  201. */
  202. #define __LL_I2C_FREQ_MHZ_TO_HZ(__PCLK__) (uint32_t)((__PCLK__)*1000000U)
  203. /**
  204. * @brief Compute I2C Clock rising time.
  205. * @param __FREQRANGE__ This parameter must be a value of peripheral clock (in MHz).
  206. * @param __SPEED__ This parameter must be a value lower than 400kHz (in Hz).
  207. * @retval Value between Min_Data=0x02 and Max_Data=0x3F
  208. */
  209. #define __LL_I2C_RISE_TIME(__FREQRANGE__, __SPEED__) (uint32_t)(((__SPEED__) <= LL_I2C_MAX_SPEED_STANDARD) ? ((__FREQRANGE__) + 1U) : ((((__FREQRANGE__) * 300U) / 1000U) + 1U))
  210. /**
  211. * @brief Compute Speed clock range to a Clock Control Register (I2C_CCR_CCR) value.
  212. * @param __PCLK__ This parameter must be a value of peripheral clock (in Hz).
  213. * @param __SPEED__ This parameter must be a value lower than 400kHz (in Hz).
  214. * @param __DUTYCYCLE__ This parameter can be one of the following values:
  215. * @arg @ref LL_I2C_DUTYCYCLE_2
  216. * @arg @ref LL_I2C_DUTYCYCLE_16_9
  217. * @retval Value between Min_Data=0x004 and Max_Data=0xFFF, except in FAST DUTY mode where Min_Data=0x001.
  218. */
  219. #define __LL_I2C_SPEED_TO_CCR(__PCLK__, __SPEED__, __DUTYCYCLE__) (uint32_t)(((__SPEED__) <= LL_I2C_MAX_SPEED_STANDARD)? \
  220. (__LL_I2C_SPEED_STANDARD_TO_CCR((__PCLK__), (__SPEED__))) : \
  221. (__LL_I2C_SPEED_FAST_TO_CCR((__PCLK__), (__SPEED__), (__DUTYCYCLE__))))
  222. /**
  223. * @brief Compute Speed Standard clock range to a Clock Control Register (I2C_CCR_CCR) value.
  224. * @param __PCLK__ This parameter must be a value of peripheral clock (in Hz).
  225. * @param __SPEED__ This parameter must be a value lower than 100kHz (in Hz).
  226. * @retval Value between Min_Data=0x004 and Max_Data=0xFFF.
  227. */
  228. #define __LL_I2C_SPEED_STANDARD_TO_CCR(__PCLK__, __SPEED__) (uint32_t)(((((__PCLK__)/((__SPEED__) << 1U)) & I2C_CCR_CCR) < 4U)? 4U:((__PCLK__) / ((__SPEED__) << 1U)))
  229. /**
  230. * @brief Compute Speed Fast clock range to a Clock Control Register (I2C_CCR_CCR) value.
  231. * @param __PCLK__ This parameter must be a value of peripheral clock (in Hz).
  232. * @param __SPEED__ This parameter must be a value between Min_Data=100Khz and Max_Data=400Khz (in Hz).
  233. * @param __DUTYCYCLE__ This parameter can be one of the following values:
  234. * @arg @ref LL_I2C_DUTYCYCLE_2
  235. * @arg @ref LL_I2C_DUTYCYCLE_16_9
  236. * @retval Value between Min_Data=0x001 and Max_Data=0xFFF
  237. */
  238. #define __LL_I2C_SPEED_FAST_TO_CCR(__PCLK__, __SPEED__, __DUTYCYCLE__) (uint32_t)(((__DUTYCYCLE__) == LL_I2C_DUTYCYCLE_2)? \
  239. (((((__PCLK__) / ((__SPEED__) * 3U)) & I2C_CCR_CCR) == 0U)? 1U:((__PCLK__) / ((__SPEED__) * 3U))) : \
  240. (((((__PCLK__) / ((__SPEED__) * 25U)) & I2C_CCR_CCR) == 0U)? 1U:((__PCLK__) / ((__SPEED__) * 25U))))
  241. /**
  242. * @}
  243. */
  244. /**
  245. * @}
  246. */
  247. /* Exported functions --------------------------------------------------------*/
  248. /** @defgroup I2C_LL_Exported_Functions I2C Exported Functions
  249. * @{
  250. */
  251. /** @defgroup I2C_LL_EF_Configuration Configuration
  252. * @{
  253. */
  254. /**
  255. * @brief Enable I2C peripheral (PE = 1).
  256. * @rmtoll CR1 PE LL_I2C_Enable
  257. * @param I2Cx I2C Instance.
  258. * @retval None
  259. */
  260. __STATIC_INLINE void LL_I2C_Enable(I2C_TypeDef *I2Cx)
  261. {
  262. SET_BIT(I2Cx->CR1, I2C_CR1_PE);
  263. }
  264. /**
  265. * @brief Disable I2C peripheral (PE = 0).
  266. * @rmtoll CR1 PE LL_I2C_Disable
  267. * @param I2Cx I2C Instance.
  268. * @retval None
  269. */
  270. __STATIC_INLINE void LL_I2C_Disable(I2C_TypeDef *I2Cx)
  271. {
  272. CLEAR_BIT(I2Cx->CR1, I2C_CR1_PE);
  273. }
  274. /**
  275. * @brief Check if the I2C peripheral is enabled or disabled.
  276. * @rmtoll CR1 PE LL_I2C_IsEnabled
  277. * @param I2Cx I2C Instance.
  278. * @retval State of bit (1 or 0).
  279. */
  280. __STATIC_INLINE uint32_t LL_I2C_IsEnabled(I2C_TypeDef *I2Cx)
  281. {
  282. return (READ_BIT(I2Cx->CR1, I2C_CR1_PE) == (I2C_CR1_PE));
  283. }
  284. #if (defined(DMA1) || defined(DMA))
  285. /**
  286. * @brief Enable DMA transmission requests.
  287. * @note Depending on devices and packages, DMA may not be available.
  288. * Refer to device datasheet for DMA availability.
  289. * @rmtoll CR2 DMAEN LL_I2C_EnableDMAReq_TX
  290. * @param I2Cx I2C Instance.
  291. * @retval None
  292. */
  293. __STATIC_INLINE void LL_I2C_EnableDMAReq_TX(I2C_TypeDef *I2Cx)
  294. {
  295. SET_BIT(I2Cx->CR2, I2C_CR2_DMAEN);
  296. }
  297. /**
  298. * @brief Disable DMA transmission requests.
  299. * @note Depending on devices and packages, DMA may not be available.
  300. * Refer to device datasheet for DMA availability.
  301. * @rmtoll CR2 DMAEN LL_I2C_DisableDMAReq_TX
  302. * @param I2Cx I2C Instance.
  303. * @retval None
  304. */
  305. __STATIC_INLINE void LL_I2C_DisableDMAReq_TX(I2C_TypeDef *I2Cx)
  306. {
  307. CLEAR_BIT(I2Cx->CR2, I2C_CR2_DMAEN);
  308. }
  309. /**
  310. * @brief Check if DMA transmission requests are enabled or disabled.
  311. * @note Depending on devices and packages, DMA may not be available.
  312. * Refer to device datasheet for DMA availability.
  313. * @rmtoll CR2 DMAEN LL_I2C_IsEnabledDMAReq_TX
  314. * @param I2Cx I2C Instance.
  315. * @retval State of bit (1 or 0).
  316. */
  317. __STATIC_INLINE uint32_t LL_I2C_IsEnabledDMAReq_TX(I2C_TypeDef *I2Cx)
  318. {
  319. return (READ_BIT(I2Cx->CR2, I2C_CR2_DMAEN) == (I2C_CR2_DMAEN));
  320. }
  321. /**
  322. * @brief Enable DMA reception requests.
  323. * @note Depending on devices and packages, DMA may not be available.
  324. * Refer to device datasheet for DMA availability.
  325. * @rmtoll CR2 DMAEN LL_I2C_EnableDMAReq_RX
  326. * @param I2Cx I2C Instance.
  327. * @retval None
  328. */
  329. __STATIC_INLINE void LL_I2C_EnableDMAReq_RX(I2C_TypeDef *I2Cx)
  330. {
  331. SET_BIT(I2Cx->CR2, I2C_CR2_DMAEN);
  332. }
  333. /**
  334. * @brief Disable DMA reception requests.
  335. * @note Depending on devices and packages, DMA may not be available.
  336. * Refer to device datasheet for DMA availability.
  337. * @rmtoll CR2 DMAEN LL_I2C_DisableDMAReq_RX
  338. * @param I2Cx I2C Instance.
  339. * @retval None
  340. */
  341. __STATIC_INLINE void LL_I2C_DisableDMAReq_RX(I2C_TypeDef *I2Cx)
  342. {
  343. CLEAR_BIT(I2Cx->CR2, I2C_CR2_DMAEN);
  344. }
  345. /**
  346. * @brief Check if DMA reception requests are enabled or disabled.
  347. * @note Depending on devices and packages, DMA may not be available.
  348. * Refer to device datasheet for DMA availability.
  349. * @rmtoll CR2 DMAEN LL_I2C_IsEnabledDMAReq_RX
  350. * @param I2Cx I2C Instance.
  351. * @retval State of bit (1 or 0).
  352. */
  353. __STATIC_INLINE uint32_t LL_I2C_IsEnabledDMAReq_RX(I2C_TypeDef *I2Cx)
  354. {
  355. return (READ_BIT(I2Cx->CR2, I2C_CR2_DMAEN) == (I2C_CR2_DMAEN));
  356. }
  357. /**
  358. * @brief Get the data register address used for DMA transfer.
  359. * @note Depending on devices and packages, DMA may not be available.
  360. * Refer to device datasheet for DMA availability.
  361. * @rmtoll DR DR LL_I2C_DMA_GetRegAddr
  362. * @param I2Cx I2C Instance.
  363. * @retval Address of data register
  364. */
  365. __STATIC_INLINE uint32_t LL_I2C_DMA_GetRegAddr(I2C_TypeDef *I2Cx)
  366. {
  367. return (uint32_t) & (I2Cx->DR);
  368. }
  369. #endif /* DMA1 or DMA */
  370. /**
  371. * @brief Enable Clock stretching.
  372. * @note This bit can only be programmed when the I2C is disabled (PE = 0).
  373. * @rmtoll CR1 NOSTRETCH LL_I2C_EnableClockStretching
  374. * @param I2Cx I2C Instance.
  375. * @retval None
  376. */
  377. __STATIC_INLINE void LL_I2C_EnableClockStretching(I2C_TypeDef *I2Cx)
  378. {
  379. CLEAR_BIT(I2Cx->CR1, I2C_CR1_NOSTRETCH);
  380. }
  381. /**
  382. * @brief Disable Clock stretching.
  383. * @note This bit can only be programmed when the I2C is disabled (PE = 0).
  384. * @rmtoll CR1 NOSTRETCH LL_I2C_DisableClockStretching
  385. * @param I2Cx I2C Instance.
  386. * @retval None
  387. */
  388. __STATIC_INLINE void LL_I2C_DisableClockStretching(I2C_TypeDef *I2Cx)
  389. {
  390. SET_BIT(I2Cx->CR1, I2C_CR1_NOSTRETCH);
  391. }
  392. /**
  393. * @brief Check if Clock stretching is enabled or disabled.
  394. * @rmtoll CR1 NOSTRETCH LL_I2C_IsEnabledClockStretching
  395. * @param I2Cx I2C Instance.
  396. * @retval State of bit (1 or 0).
  397. */
  398. __STATIC_INLINE uint32_t LL_I2C_IsEnabledClockStretching(I2C_TypeDef *I2Cx)
  399. {
  400. return (READ_BIT(I2Cx->CR1, I2C_CR1_NOSTRETCH) != (I2C_CR1_NOSTRETCH));
  401. }
  402. /**
  403. * @brief Enable General Call.
  404. * @note When enabled the Address 0x00 is ACKed.
  405. * @rmtoll CR1 ENGC LL_I2C_EnableGeneralCall
  406. * @param I2Cx I2C Instance.
  407. * @retval None
  408. */
  409. __STATIC_INLINE void LL_I2C_EnableGeneralCall(I2C_TypeDef *I2Cx)
  410. {
  411. SET_BIT(I2Cx->CR1, I2C_CR1_ENGC);
  412. }
  413. /**
  414. * @brief Disable General Call.
  415. * @note When disabled the Address 0x00 is NACKed.
  416. * @rmtoll CR1 ENGC LL_I2C_DisableGeneralCall
  417. * @param I2Cx I2C Instance.
  418. * @retval None
  419. */
  420. __STATIC_INLINE void LL_I2C_DisableGeneralCall(I2C_TypeDef *I2Cx)
  421. {
  422. CLEAR_BIT(I2Cx->CR1, I2C_CR1_ENGC);
  423. }
  424. /**
  425. * @brief Check if General Call is enabled or disabled.
  426. * @rmtoll CR1 ENGC LL_I2C_IsEnabledGeneralCall
  427. * @param I2Cx I2C Instance.
  428. * @retval State of bit (1 or 0).
  429. */
  430. __STATIC_INLINE uint32_t LL_I2C_IsEnabledGeneralCall(I2C_TypeDef *I2Cx)
  431. {
  432. return (READ_BIT(I2Cx->CR1, I2C_CR1_ENGC) == (I2C_CR1_ENGC));
  433. }
  434. /**
  435. * @brief Set the Own Address1.
  436. * @param I2Cx I2C Instance.
  437. * @param OwnAddress1 This parameter must be a value between Min_Data=0 and Max_Data=0x3FF.
  438. * @param OwnAddrSize This parameter is not used, can pass 0.
  439. * @retval None
  440. */
  441. __STATIC_INLINE void LL_I2C_SetOwnAddress1(I2C_TypeDef *I2Cx, uint32_t OwnAddress1, uint32_t OwnAddrSize)
  442. {
  443. (void)OwnAddrSize;
  444. MODIFY_REG(I2Cx->OAR1, I2C_OAR1_ADD1_7, OwnAddress1);
  445. }
  446. /**
  447. * @brief Configure the Peripheral clock frequency.
  448. * @rmtoll CR2 FREQ LL_I2C_SetPeriphClock
  449. * @param I2Cx I2C Instance.
  450. * @param PeriphClock Peripheral Clock (in Hz)
  451. * @retval None
  452. */
  453. __STATIC_INLINE void LL_I2C_SetPeriphClock(I2C_TypeDef *I2Cx, uint32_t PeriphClock)
  454. {
  455. MODIFY_REG(I2Cx->CR2, I2C_CR2_FREQ, __LL_I2C_FREQ_HZ_TO_MHZ(PeriphClock));
  456. }
  457. /**
  458. * @brief Get the Peripheral clock frequency.
  459. * @rmtoll CR2 FREQ LL_I2C_GetPeriphClock
  460. * @param I2Cx I2C Instance.
  461. * @retval Value of Peripheral Clock (in Hz)
  462. */
  463. __STATIC_INLINE uint32_t LL_I2C_GetPeriphClock(I2C_TypeDef *I2Cx)
  464. {
  465. return (uint32_t)(__LL_I2C_FREQ_MHZ_TO_HZ(READ_BIT(I2Cx->CR2, I2C_CR2_FREQ)));
  466. }
  467. /**
  468. * @brief Configure the Duty cycle (Fast mode only).
  469. * @rmtoll CCR DUTY LL_I2C_SetDutyCycle
  470. * @param I2Cx I2C Instance.
  471. * @param DutyCycle This parameter can be one of the following values:
  472. * @arg @ref LL_I2C_DUTYCYCLE_2
  473. * @arg @ref LL_I2C_DUTYCYCLE_16_9
  474. * @retval None
  475. */
  476. __STATIC_INLINE void LL_I2C_SetDutyCycle(I2C_TypeDef *I2Cx, uint32_t DutyCycle)
  477. {
  478. MODIFY_REG(I2Cx->CCR, I2C_CCR_DUTY, DutyCycle);
  479. }
  480. /**
  481. * @brief Get the Duty cycle (Fast mode only).
  482. * @rmtoll CCR DUTY LL_I2C_GetDutyCycle
  483. * @param I2Cx I2C Instance.
  484. * @retval Returned value can be one of the following values:
  485. * @arg @ref LL_I2C_DUTYCYCLE_2
  486. * @arg @ref LL_I2C_DUTYCYCLE_16_9
  487. */
  488. __STATIC_INLINE uint32_t LL_I2C_GetDutyCycle(I2C_TypeDef *I2Cx)
  489. {
  490. return (uint32_t)(READ_BIT(I2Cx->CCR, I2C_CCR_DUTY));
  491. }
  492. /**
  493. * @brief Configure the I2C master clock speed mode.
  494. * @rmtoll CCR FS LL_I2C_SetClockSpeedMode
  495. * @param I2Cx I2C Instance.
  496. * @param ClockSpeedMode This parameter can be one of the following values:
  497. * @arg @ref LL_I2C_CLOCK_SPEED_STANDARD_MODE
  498. * @arg @ref LL_I2C_CLOCK_SPEED_FAST_MODE
  499. * @retval None
  500. */
  501. __STATIC_INLINE void LL_I2C_SetClockSpeedMode(I2C_TypeDef *I2Cx, uint32_t ClockSpeedMode)
  502. {
  503. MODIFY_REG(I2Cx->CCR, I2C_CCR_FS, ClockSpeedMode);
  504. }
  505. /**
  506. * @brief Get the the I2C master speed mode.
  507. * @rmtoll CCR FS LL_I2C_GetClockSpeedMode
  508. * @param I2Cx I2C Instance.
  509. * @retval Returned value can be one of the following values:
  510. * @arg @ref LL_I2C_CLOCK_SPEED_STANDARD_MODE
  511. * @arg @ref LL_I2C_CLOCK_SPEED_FAST_MODE
  512. */
  513. __STATIC_INLINE uint32_t LL_I2C_GetClockSpeedMode(I2C_TypeDef *I2Cx)
  514. {
  515. return (uint32_t)(READ_BIT(I2Cx->CCR, I2C_CCR_FS));
  516. }
  517. /**
  518. * @brief Configure the SCL, SDA rising time.
  519. * @note This bit can only be programmed when the I2C is disabled (PE = 0).
  520. * @rmtoll TRISE TRISE LL_I2C_SetRiseTime
  521. * @param I2Cx I2C Instance.
  522. * @param RiseTime This parameter must be a value between Min_Data=0x02 and Max_Data=0x3F.
  523. * @retval None
  524. */
  525. __STATIC_INLINE void LL_I2C_SetRiseTime(I2C_TypeDef *I2Cx, uint32_t RiseTime)
  526. {
  527. MODIFY_REG(I2Cx->TRISE, I2C_TRISE_TRISE, RiseTime);
  528. }
  529. /**
  530. * @brief Get the SCL, SDA rising time.
  531. * @rmtoll TRISE TRISE LL_I2C_GetRiseTime
  532. * @param I2Cx I2C Instance.
  533. * @retval Value between Min_Data=0x02 and Max_Data=0x3F
  534. */
  535. __STATIC_INLINE uint32_t LL_I2C_GetRiseTime(I2C_TypeDef *I2Cx)
  536. {
  537. return (uint32_t)(READ_BIT(I2Cx->TRISE, I2C_TRISE_TRISE));
  538. }
  539. /**
  540. * @brief Configure the SCL high and low period.
  541. * @note This bit can only be programmed when the I2C is disabled (PE = 0).
  542. * @rmtoll CCR CCR LL_I2C_SetClockPeriod
  543. * @param I2Cx I2C Instance.
  544. * @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.
  545. * @retval None
  546. */
  547. __STATIC_INLINE void LL_I2C_SetClockPeriod(I2C_TypeDef *I2Cx, uint32_t ClockPeriod)
  548. {
  549. MODIFY_REG(I2Cx->CCR, I2C_CCR_CCR, ClockPeriod);
  550. }
  551. /**
  552. * @brief Get the SCL high and low period.
  553. * @rmtoll CCR CCR LL_I2C_GetClockPeriod
  554. * @param I2Cx I2C Instance.
  555. * @retval Value between Min_Data=0x004 and Max_Data=0xFFF, except in FAST DUTY mode where Min_Data=0x001.
  556. */
  557. __STATIC_INLINE uint32_t LL_I2C_GetClockPeriod(I2C_TypeDef *I2Cx)
  558. {
  559. return (uint32_t)(READ_BIT(I2Cx->CCR, I2C_CCR_CCR));
  560. }
  561. /**
  562. * @brief Configure the SCL speed.
  563. * @note This bit can only be programmed when the I2C is disabled (PE = 0).
  564. * @rmtoll CR2 FREQ LL_I2C_ConfigSpeed\n
  565. * TRISE TRISE LL_I2C_ConfigSpeed\n
  566. * CCR FS LL_I2C_ConfigSpeed\n
  567. * CCR DUTY LL_I2C_ConfigSpeed\n
  568. * CCR CCR LL_I2C_ConfigSpeed
  569. * @param I2Cx I2C Instance.
  570. * @param PeriphClock Peripheral Clock (in Hz)
  571. * @param ClockSpeed This parameter must be a value lower than 400kHz (in Hz).
  572. * @param DutyCycle This parameter can be one of the following values:
  573. * @arg @ref LL_I2C_DUTYCYCLE_2
  574. * @arg @ref LL_I2C_DUTYCYCLE_16_9
  575. * @retval None
  576. */
  577. __STATIC_INLINE void LL_I2C_ConfigSpeed(I2C_TypeDef *I2Cx, uint32_t PeriphClock, uint32_t ClockSpeed,
  578. uint32_t DutyCycle)
  579. {
  580. register uint32_t freqrange = 0x0U;
  581. register uint32_t clockconfig = 0x0U;
  582. /* Compute frequency range */
  583. freqrange = __LL_I2C_FREQ_HZ_TO_MHZ(PeriphClock);
  584. /* Configure I2Cx: Frequency range register */
  585. MODIFY_REG(I2Cx->CR2, I2C_CR2_FREQ, freqrange);
  586. /* Configure I2Cx: Rise Time register */
  587. MODIFY_REG(I2Cx->TRISE, I2C_TRISE_TRISE, __LL_I2C_RISE_TIME(freqrange, ClockSpeed));
  588. /* Configure Speed mode, Duty Cycle and Clock control register value */
  589. if (ClockSpeed > LL_I2C_MAX_SPEED_STANDARD)
  590. {
  591. /* Set Speed mode at fast and duty cycle for Clock Speed request in fast clock range */
  592. clockconfig = LL_I2C_CLOCK_SPEED_FAST_MODE | \
  593. __LL_I2C_SPEED_FAST_TO_CCR(PeriphClock, ClockSpeed, DutyCycle) | \
  594. DutyCycle;
  595. }
  596. else
  597. {
  598. /* Set Speed mode at standard for Clock Speed request in standard clock range */
  599. clockconfig = LL_I2C_CLOCK_SPEED_STANDARD_MODE | \
  600. __LL_I2C_SPEED_STANDARD_TO_CCR(PeriphClock, ClockSpeed);
  601. }
  602. /* Configure I2Cx: Clock control register */
  603. MODIFY_REG(I2Cx->CCR, (I2C_CCR_FS | I2C_CCR_DUTY | I2C_CCR_CCR), clockconfig);
  604. }
  605. /**
  606. * @}
  607. */
  608. /** @defgroup I2C_LL_EF_IT_Management IT Management
  609. * @{
  610. */
  611. /**
  612. * @brief Enable TXE interrupt.
  613. * @rmtoll CR2 ITEVTEN LL_I2C_EnableIT_TX\n
  614. * CR2 ITBUFEN LL_I2C_EnableIT_TX
  615. * @param I2Cx I2C Instance.
  616. * @retval None
  617. */
  618. __STATIC_INLINE void LL_I2C_EnableIT_TX(I2C_TypeDef *I2Cx)
  619. {
  620. SET_BIT(I2Cx->CR2, I2C_CR2_ITEVTEN | I2C_CR2_ITBUFEN);
  621. }
  622. /**
  623. * @brief Disable TXE interrupt.
  624. * @rmtoll CR2 ITEVTEN LL_I2C_DisableIT_TX\n
  625. * CR2 ITBUFEN LL_I2C_DisableIT_TX
  626. * @param I2Cx I2C Instance.
  627. * @retval None
  628. */
  629. __STATIC_INLINE void LL_I2C_DisableIT_TX(I2C_TypeDef *I2Cx)
  630. {
  631. CLEAR_BIT(I2Cx->CR2, I2C_CR2_ITEVTEN | I2C_CR2_ITBUFEN);
  632. }
  633. /**
  634. * @brief Check if the TXE Interrupt is enabled or disabled.
  635. * @rmtoll CR2 ITEVTEN LL_I2C_IsEnabledIT_TX\n
  636. * CR2 ITBUFEN LL_I2C_IsEnabledIT_TX
  637. * @param I2Cx I2C Instance.
  638. * @retval State of bit (1 or 0).
  639. */
  640. __STATIC_INLINE uint32_t LL_I2C_IsEnabledIT_TX(I2C_TypeDef *I2Cx)
  641. {
  642. return (READ_BIT(I2Cx->CR2, I2C_CR2_ITEVTEN | I2C_CR2_ITBUFEN) == (I2C_CR2_ITEVTEN | I2C_CR2_ITBUFEN));
  643. }
  644. /**
  645. * @brief Enable RXNE interrupt.
  646. * @rmtoll CR2 ITEVTEN LL_I2C_EnableIT_RX\n
  647. * CR2 ITBUFEN LL_I2C_EnableIT_RX
  648. * @param I2Cx I2C Instance.
  649. * @retval None
  650. */
  651. __STATIC_INLINE void LL_I2C_EnableIT_RX(I2C_TypeDef *I2Cx)
  652. {
  653. SET_BIT(I2Cx->CR2, I2C_CR2_ITEVTEN | I2C_CR2_ITBUFEN);
  654. }
  655. /**
  656. * @brief Disable RXNE interrupt.
  657. * @rmtoll CR2 ITEVTEN LL_I2C_DisableIT_RX\n
  658. * CR2 ITBUFEN LL_I2C_DisableIT_RX
  659. * @param I2Cx I2C Instance.
  660. * @retval None
  661. */
  662. __STATIC_INLINE void LL_I2C_DisableIT_RX(I2C_TypeDef *I2Cx)
  663. {
  664. CLEAR_BIT(I2Cx->CR2, I2C_CR2_ITEVTEN | I2C_CR2_ITBUFEN);
  665. }
  666. /**
  667. * @brief Check if the RXNE Interrupt is enabled or disabled.
  668. * @rmtoll CR2 ITEVTEN LL_I2C_IsEnabledIT_RX\n
  669. * CR2 ITBUFEN LL_I2C_IsEnabledIT_RX
  670. * @param I2Cx I2C Instance.
  671. * @retval State of bit (1 or 0).
  672. */
  673. __STATIC_INLINE uint32_t LL_I2C_IsEnabledIT_RX(I2C_TypeDef *I2Cx)
  674. {
  675. return (READ_BIT(I2Cx->CR2, I2C_CR2_ITEVTEN | I2C_CR2_ITBUFEN) == (I2C_CR2_ITEVTEN | I2C_CR2_ITBUFEN));
  676. }
  677. /**
  678. * @brief Enable Events interrupts.
  679. * @note Any of these events will generate interrupt :
  680. * Start Bit (SB)
  681. * Address sent, Address matched (ADDR)
  682. * Stop detection (STOPF)
  683. * Byte transfer finished (BTF)
  684. *
  685. * @note Any of these events will generate interrupt if Buffer interrupts are enabled too(using unitary function @ref LL_I2C_EnableIT_BUF()) :
  686. * Receive buffer not empty (RXNE)
  687. * Transmit buffer empty (TXE)
  688. * @rmtoll CR2 ITEVTEN LL_I2C_EnableIT_EVT
  689. * @param I2Cx I2C Instance.
  690. * @retval None
  691. */
  692. __STATIC_INLINE void LL_I2C_EnableIT_EVT(I2C_TypeDef *I2Cx)
  693. {
  694. SET_BIT(I2Cx->CR2, I2C_CR2_ITEVTEN);
  695. }
  696. /**
  697. * @brief Disable Events interrupts.
  698. * @note Any of these events will generate interrupt :
  699. * Start Bit (SB)
  700. * Address sent, Address matched (ADDR)
  701. * Stop detection (STOPF)
  702. * Byte transfer finished (BTF)
  703. * Receive buffer not empty (RXNE)
  704. * Transmit buffer empty (TXE)
  705. * @rmtoll CR2 ITEVTEN LL_I2C_DisableIT_EVT
  706. * @param I2Cx I2C Instance.
  707. * @retval None
  708. */
  709. __STATIC_INLINE void LL_I2C_DisableIT_EVT(I2C_TypeDef *I2Cx)
  710. {
  711. CLEAR_BIT(I2Cx->CR2, I2C_CR2_ITEVTEN);
  712. }
  713. /**
  714. * @brief Check if Events interrupts are enabled or disabled.
  715. * @rmtoll CR2 ITEVTEN LL_I2C_IsEnabledIT_EVT
  716. * @param I2Cx I2C Instance.
  717. * @retval State of bit (1 or 0).
  718. */
  719. __STATIC_INLINE uint32_t LL_I2C_IsEnabledIT_EVT(I2C_TypeDef *I2Cx)
  720. {
  721. return (READ_BIT(I2Cx->CR2, I2C_CR2_ITEVTEN) == (I2C_CR2_ITEVTEN));
  722. }
  723. /**
  724. * @brief Enable Buffer interrupts.
  725. * @note Any of these Buffer events will generate interrupt if Events interrupts are enabled too(using unitary function @ref LL_I2C_EnableIT_EVT()) :
  726. * Receive buffer not empty (RXNE)
  727. * Transmit buffer empty (TXE)
  728. * @rmtoll CR2 ITBUFEN LL_I2C_EnableIT_BUF
  729. * @param I2Cx I2C Instance.
  730. * @retval None
  731. */
  732. __STATIC_INLINE void LL_I2C_EnableIT_BUF(I2C_TypeDef *I2Cx)
  733. {
  734. SET_BIT(I2Cx->CR2, I2C_CR2_ITBUFEN);
  735. }
  736. /**
  737. * @brief Disable Buffer interrupts.
  738. * @note Any of these Buffer events will generate interrupt :
  739. * Receive buffer not empty (RXNE)
  740. * Transmit buffer empty (TXE)
  741. * @rmtoll CR2 ITBUFEN LL_I2C_DisableIT_BUF
  742. * @param I2Cx I2C Instance.
  743. * @retval None
  744. */
  745. __STATIC_INLINE void LL_I2C_DisableIT_BUF(I2C_TypeDef *I2Cx)
  746. {
  747. CLEAR_BIT(I2Cx->CR2, I2C_CR2_ITBUFEN);
  748. }
  749. /**
  750. * @brief Check if Buffer interrupts are enabled or disabled.
  751. * @rmtoll CR2 ITBUFEN LL_I2C_IsEnabledIT_BUF
  752. * @param I2Cx I2C Instance.
  753. * @retval State of bit (1 or 0).
  754. */
  755. __STATIC_INLINE uint32_t LL_I2C_IsEnabledIT_BUF(I2C_TypeDef *I2Cx)
  756. {
  757. return (READ_BIT(I2Cx->CR2, I2C_CR2_ITBUFEN) == (I2C_CR2_ITBUFEN));
  758. }
  759. /**
  760. * @brief Enable Error interrupts.
  761. * @note Macro @ref IS_SMBUS_ALL_INSTANCE(I2Cx) can be used to check whether or not
  762. * SMBus feature is supported by the I2Cx Instance.
  763. * @note Any of these errors will generate interrupt :
  764. * Bus Error detection (BERR)
  765. * Arbitration Loss (ARLO)
  766. * Acknowledge Failure(AF)
  767. * Overrun/Underrun (OVR)
  768. * @rmtoll CR2 ITERREN LL_I2C_EnableIT_ERR
  769. * @param I2Cx I2C Instance.
  770. * @retval None
  771. */
  772. __STATIC_INLINE void LL_I2C_EnableIT_ERR(I2C_TypeDef *I2Cx)
  773. {
  774. SET_BIT(I2Cx->CR2, I2C_CR2_ITERREN);
  775. }
  776. /**
  777. * @brief Disable Error interrupts.
  778. * @note Macro @ref IS_SMBUS_ALL_INSTANCE(I2Cx) can be used to check whether or not
  779. * SMBus feature is supported by the I2Cx Instance.
  780. * @note Any of these errors will generate interrupt :
  781. * Bus Error detection (BERR)
  782. * Arbitration Loss (ARLO)
  783. * Acknowledge Failure(AF)
  784. * Overrun/Underrun (OVR)
  785. * SMBus Timeout detection (TIMEOUT)
  786. * SMBus PEC error detection (PECERR)
  787. * SMBus Alert pin event detection (SMBALERT)
  788. * @rmtoll CR2 ITERREN LL_I2C_DisableIT_ERR
  789. * @param I2Cx I2C Instance.
  790. * @retval None
  791. */
  792. __STATIC_INLINE void LL_I2C_DisableIT_ERR(I2C_TypeDef *I2Cx)
  793. {
  794. CLEAR_BIT(I2Cx->CR2, I2C_CR2_ITERREN);
  795. }
  796. /**
  797. * @brief Check if Error interrupts are enabled or disabled.
  798. * @rmtoll CR2 ITERREN LL_I2C_IsEnabledIT_ERR
  799. * @param I2Cx I2C Instance.
  800. * @retval State of bit (1 or 0).
  801. */
  802. __STATIC_INLINE uint32_t LL_I2C_IsEnabledIT_ERR(I2C_TypeDef *I2Cx)
  803. {
  804. return (READ_BIT(I2Cx->CR2, I2C_CR2_ITERREN) == (I2C_CR2_ITERREN));
  805. }
  806. /**
  807. * @}
  808. */
  809. /** @defgroup I2C_LL_EF_FLAG_management FLAG Management
  810. * @{
  811. */
  812. /**
  813. * @brief Indicate the status of Transmit data register empty flag.
  814. * @note RESET: When next data is written in Transmit data register.
  815. * SET: When Transmit data register is empty.
  816. * @rmtoll SR1 TXE LL_I2C_IsActiveFlag_TXE
  817. * @param I2Cx I2C Instance.
  818. * @retval State of bit (1 or 0).
  819. */
  820. __STATIC_INLINE uint32_t LL_I2C_IsActiveFlag_TXE(I2C_TypeDef *I2Cx)
  821. {
  822. return (READ_BIT(I2Cx->SR1, I2C_SR1_TXE) == (I2C_SR1_TXE));
  823. }
  824. /**
  825. * @brief Indicate the status of Byte Transfer Finished flag.
  826. * RESET: When Data byte transfer not done.
  827. * SET: When Data byte transfer succeeded.
  828. * @rmtoll SR1 BTF LL_I2C_IsActiveFlag_BTF
  829. * @param I2Cx I2C Instance.
  830. * @retval State of bit (1 or 0).
  831. */
  832. __STATIC_INLINE uint32_t LL_I2C_IsActiveFlag_BTF(I2C_TypeDef *I2Cx)
  833. {
  834. return (READ_BIT(I2Cx->SR1, I2C_SR1_BTF) == (I2C_SR1_BTF));
  835. }
  836. /**
  837. * @brief Indicate the status of Receive data register not empty flag.
  838. * @note RESET: When Receive data register is read.
  839. * SET: When the received data is copied in Receive data register.
  840. * @rmtoll SR1 RXNE LL_I2C_IsActiveFlag_RXNE
  841. * @param I2Cx I2C Instance.
  842. * @retval State of bit (1 or 0).
  843. */
  844. __STATIC_INLINE uint32_t LL_I2C_IsActiveFlag_RXNE(I2C_TypeDef *I2Cx)
  845. {
  846. return (READ_BIT(I2Cx->SR1, I2C_SR1_RXNE) == (I2C_SR1_RXNE));
  847. }
  848. /**
  849. * @brief Indicate the status of Start Bit (master mode).
  850. * @note RESET: When No Start condition.
  851. * SET: When Start condition is generated.
  852. * @rmtoll SR1 SB LL_I2C_IsActiveFlag_SB
  853. * @param I2Cx I2C Instance.
  854. * @retval State of bit (1 or 0).
  855. */
  856. __STATIC_INLINE uint32_t LL_I2C_IsActiveFlag_SB(I2C_TypeDef *I2Cx)
  857. {
  858. return (READ_BIT(I2Cx->SR1, I2C_SR1_SB) == (I2C_SR1_SB));
  859. }
  860. /**
  861. * @brief Indicate the status of Address sent (master mode) or Address matched flag (slave mode).
  862. * @note RESET: Clear default value.
  863. * 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).
  864. * @rmtoll SR1 ADDR LL_I2C_IsActiveFlag_ADDR
  865. * @param I2Cx I2C Instance.
  866. * @retval State of bit (1 or 0).
  867. */
  868. __STATIC_INLINE uint32_t LL_I2C_IsActiveFlag_ADDR(I2C_TypeDef *I2Cx)
  869. {
  870. return (READ_BIT(I2Cx->SR1, I2C_SR1_ADDR) == (I2C_SR1_ADDR));
  871. }
  872. /**
  873. * @brief Indicate the status of Acknowledge failure flag.
  874. * @note RESET: No acknowledge failure.
  875. * SET: When an acknowledge failure is received after a byte transmission.
  876. * @rmtoll SR1 AF LL_I2C_IsActiveFlag_AF
  877. * @param I2Cx I2C Instance.
  878. * @retval State of bit (1 or 0).
  879. */
  880. __STATIC_INLINE uint32_t LL_I2C_IsActiveFlag_AF(I2C_TypeDef *I2Cx)
  881. {
  882. return (READ_BIT(I2Cx->SR1, I2C_SR1_AF) == (I2C_SR1_AF));
  883. }
  884. /**
  885. * @brief Indicate the status of Stop detection flag (slave mode).
  886. * @note RESET: Clear default value.
  887. * SET: When a Stop condition is detected.
  888. * @rmtoll SR1 STOPF LL_I2C_IsActiveFlag_STOP
  889. * @param I2Cx I2C Instance.
  890. * @retval State of bit (1 or 0).
  891. */
  892. __STATIC_INLINE uint32_t LL_I2C_IsActiveFlag_STOP(I2C_TypeDef *I2Cx)
  893. {
  894. return (READ_BIT(I2Cx->SR1, I2C_SR1_STOPF) == (I2C_SR1_STOPF));
  895. }
  896. /**
  897. * @brief Indicate the status of Bus error flag.
  898. * @note RESET: Clear default value.
  899. * SET: When a misplaced Start or Stop condition is detected.
  900. * @rmtoll SR1 BERR LL_I2C_IsActiveFlag_BERR
  901. * @param I2Cx I2C Instance.
  902. * @retval State of bit (1 or 0).
  903. */
  904. __STATIC_INLINE uint32_t LL_I2C_IsActiveFlag_BERR(I2C_TypeDef *I2Cx)
  905. {
  906. return (READ_BIT(I2Cx->SR1, I2C_SR1_BERR) == (I2C_SR1_BERR));
  907. }
  908. /**
  909. * @brief Indicate the status of Arbitration lost flag.
  910. * @note RESET: Clear default value.
  911. * SET: When arbitration lost.
  912. * @rmtoll SR1 ARLO LL_I2C_IsActiveFlag_ARLO
  913. * @param I2Cx I2C Instance.
  914. * @retval State of bit (1 or 0).
  915. */
  916. __STATIC_INLINE uint32_t LL_I2C_IsActiveFlag_ARLO(I2C_TypeDef *I2Cx)
  917. {
  918. return (READ_BIT(I2Cx->SR1, I2C_SR1_ARLO) == (I2C_SR1_ARLO));
  919. }
  920. /**
  921. * @brief Indicate the status of Overrun/Underrun flag.
  922. * @note RESET: Clear default value.
  923. * SET: When an overrun/underrun error occurs (Clock Stretching Disabled).
  924. * @rmtoll SR1 OVR LL_I2C_IsActiveFlag_OVR
  925. * @param I2Cx I2C Instance.
  926. * @retval State of bit (1 or 0).
  927. */
  928. __STATIC_INLINE uint32_t LL_I2C_IsActiveFlag_OVR(I2C_TypeDef *I2Cx)
  929. {
  930. return (READ_BIT(I2Cx->SR1, I2C_SR1_OVR) == (I2C_SR1_OVR));
  931. }
  932. /**
  933. * @brief Indicate the status of SMBus PEC error flag in reception.
  934. * @note Macro @ref IS_SMBUS_ALL_INSTANCE(I2Cx) can be used to check whether or not
  935. * SMBus feature is supported by the I2Cx Instance.
  936. * @rmtoll SR1 PECERR LL_I2C_IsActiveSMBusFlag_PECERR
  937. * @param I2Cx I2C Instance.
  938. * @retval State of bit (1 or 0).
  939. */
  940. __STATIC_INLINE uint32_t LL_I2C_IsActiveSMBusFlag_PECERR(I2C_TypeDef *I2Cx)
  941. {
  942. return (READ_BIT(I2Cx->SR1, I2C_SR1_PECERR) == (I2C_SR1_PECERR));
  943. }
  944. /**
  945. * @brief Indicate the status of Bus Busy flag.
  946. * @note RESET: Clear default value.
  947. * SET: When a Start condition is detected.
  948. * @rmtoll SR2 BUSY LL_I2C_IsActiveFlag_BUSY
  949. * @param I2Cx I2C Instance.
  950. * @retval State of bit (1 or 0).
  951. */
  952. __STATIC_INLINE uint32_t LL_I2C_IsActiveFlag_BUSY(I2C_TypeDef *I2Cx)
  953. {
  954. return (READ_BIT(I2Cx->SR2, I2C_SR2_BUSY) == (I2C_SR2_BUSY));
  955. }
  956. /**
  957. * @brief Indicate the status of General call address reception (Slave mode).
  958. * @note RESET: No Generall call address
  959. * SET: General call address received.
  960. * @note This status is cleared by hardware after a STOP condition or repeated START condition.
  961. * @rmtoll SR2 GENCALL LL_I2C_IsActiveFlag_GENCALL
  962. * @param I2Cx I2C Instance.
  963. * @retval State of bit (1 or 0).
  964. */
  965. __STATIC_INLINE uint32_t LL_I2C_IsActiveFlag_GENCALL(I2C_TypeDef *I2Cx)
  966. {
  967. return (READ_BIT(I2Cx->SR2, I2C_SR2_GENCALL) == (I2C_SR2_GENCALL));
  968. }
  969. /**
  970. * @brief Indicate the status of Master/Slave flag.
  971. * @note RESET: Slave Mode.
  972. * SET: Master Mode.
  973. * @rmtoll SR2 MSL LL_I2C_IsActiveFlag_MSL
  974. * @param I2Cx I2C Instance.
  975. * @retval State of bit (1 or 0).
  976. */
  977. __STATIC_INLINE uint32_t LL_I2C_IsActiveFlag_MSL(I2C_TypeDef *I2Cx)
  978. {
  979. return (READ_BIT(I2Cx->SR2, I2C_SR2_MSL) == (I2C_SR2_MSL));
  980. }
  981. /**
  982. * @brief Clear Address Matched flag.
  983. * @note Clearing this flag is done by a read access to the I2Cx_SR1
  984. * register followed by a read access to the I2Cx_SR2 register.
  985. * @rmtoll SR1 ADDR LL_I2C_ClearFlag_ADDR
  986. * @param I2Cx I2C Instance.
  987. * @retval None
  988. */
  989. __STATIC_INLINE void LL_I2C_ClearFlag_ADDR(I2C_TypeDef *I2Cx)
  990. {
  991. __IO uint32_t tmpreg;
  992. tmpreg = I2Cx->SR1;
  993. (void) tmpreg;
  994. tmpreg = I2Cx->SR2;
  995. (void) tmpreg;
  996. }
  997. /**
  998. * @brief Clear Acknowledge failure flag.
  999. * @rmtoll SR1 AF LL_I2C_ClearFlag_AF
  1000. * @param I2Cx I2C Instance.
  1001. * @retval None
  1002. */
  1003. __STATIC_INLINE void LL_I2C_ClearFlag_AF(I2C_TypeDef *I2Cx)
  1004. {
  1005. CLEAR_BIT(I2Cx->SR1, I2C_SR1_AF);
  1006. }
  1007. /**
  1008. * @brief Clear Stop detection flag.
  1009. * @note Clearing this flag is done by a read access to the I2Cx_SR1
  1010. * register followed by a write access to I2Cx_CR1 register.
  1011. * @rmtoll SR1 STOPF LL_I2C_ClearFlag_STOP\n
  1012. * CR1 PE LL_I2C_ClearFlag_STOP
  1013. * @param I2Cx I2C Instance.
  1014. * @retval None
  1015. */
  1016. __STATIC_INLINE void LL_I2C_ClearFlag_STOP(I2C_TypeDef *I2Cx)
  1017. {
  1018. __IO uint32_t tmpreg;
  1019. tmpreg = I2Cx->SR1;
  1020. (void) tmpreg;
  1021. SET_BIT(I2Cx->CR1, I2C_CR1_PE);
  1022. }
  1023. /**
  1024. * @brief Clear Bus error flag.
  1025. * @rmtoll SR1 BERR LL_I2C_ClearFlag_BERR
  1026. * @param I2Cx I2C Instance.
  1027. * @retval None
  1028. */
  1029. __STATIC_INLINE void LL_I2C_ClearFlag_BERR(I2C_TypeDef *I2Cx)
  1030. {
  1031. CLEAR_BIT(I2Cx->SR1, I2C_SR1_BERR);
  1032. }
  1033. /**
  1034. * @brief Clear Arbitration lost flag.
  1035. * @rmtoll SR1 ARLO LL_I2C_ClearFlag_ARLO
  1036. * @param I2Cx I2C Instance.
  1037. * @retval None
  1038. */
  1039. __STATIC_INLINE void LL_I2C_ClearFlag_ARLO(I2C_TypeDef *I2Cx)
  1040. {
  1041. CLEAR_BIT(I2Cx->SR1, I2C_SR1_ARLO);
  1042. }
  1043. /**
  1044. * @brief Clear Overrun/Underrun flag.
  1045. * @rmtoll SR1 OVR LL_I2C_ClearFlag_OVR
  1046. * @param I2Cx I2C Instance.
  1047. * @retval None
  1048. */
  1049. __STATIC_INLINE void LL_I2C_ClearFlag_OVR(I2C_TypeDef *I2Cx)
  1050. {
  1051. CLEAR_BIT(I2Cx->SR1, I2C_SR1_OVR);
  1052. }
  1053. /**
  1054. * @brief Clear SMBus PEC error flag.
  1055. * @rmtoll SR1 PECERR LL_I2C_ClearSMBusFlag_PECERR
  1056. * @param I2Cx I2C Instance.
  1057. * @retval None
  1058. */
  1059. __STATIC_INLINE void LL_I2C_ClearSMBusFlag_PECERR(I2C_TypeDef *I2Cx)
  1060. {
  1061. CLEAR_BIT(I2Cx->SR1, I2C_SR1_PECERR);
  1062. }
  1063. /**
  1064. * @}
  1065. */
  1066. /** @defgroup I2C_LL_EF_Data_Management Data_Management
  1067. * @{
  1068. */
  1069. /**
  1070. * @brief Enable Reset of I2C peripheral.
  1071. * @rmtoll CR1 SWRST LL_I2C_EnableReset
  1072. * @param I2Cx I2C Instance.
  1073. * @retval None
  1074. */
  1075. __STATIC_INLINE void LL_I2C_EnableReset(I2C_TypeDef *I2Cx)
  1076. {
  1077. SET_BIT(I2Cx->CR1, I2C_CR1_SWRST);
  1078. }
  1079. /**
  1080. * @brief Disable Reset of I2C peripheral.
  1081. * @rmtoll CR1 SWRST LL_I2C_DisableReset
  1082. * @param I2Cx I2C Instance.
  1083. * @retval None
  1084. */
  1085. __STATIC_INLINE void LL_I2C_DisableReset(I2C_TypeDef *I2Cx)
  1086. {
  1087. CLEAR_BIT(I2Cx->CR1, I2C_CR1_SWRST);
  1088. }
  1089. /**
  1090. * @brief Check if the I2C peripheral is under reset state or not.
  1091. * @rmtoll CR1 SWRST LL_I2C_IsResetEnabled
  1092. * @param I2Cx I2C Instance.
  1093. * @retval State of bit (1 or 0).
  1094. */
  1095. __STATIC_INLINE uint32_t LL_I2C_IsResetEnabled(I2C_TypeDef *I2Cx)
  1096. {
  1097. return (READ_BIT(I2Cx->CR1, I2C_CR1_SWRST) == (I2C_CR1_SWRST));
  1098. }
  1099. /**
  1100. * @brief Prepare the generation of a ACKnowledge or Non ACKnowledge condition after the address receive match code or next received byte.
  1101. * @note Usage in Slave or Master mode.
  1102. * @rmtoll CR1 ACK LL_I2C_AcknowledgeNextData
  1103. * @param I2Cx I2C Instance.
  1104. * @param TypeAcknowledge This parameter can be one of the following values:
  1105. * @arg @ref LL_I2C_ACK
  1106. * @arg @ref LL_I2C_NACK
  1107. * @retval None
  1108. */
  1109. __STATIC_INLINE void LL_I2C_AcknowledgeNextData(I2C_TypeDef *I2Cx, uint32_t TypeAcknowledge)
  1110. {
  1111. MODIFY_REG(I2Cx->CR1, I2C_CR1_ACK, TypeAcknowledge);
  1112. }
  1113. /**
  1114. * @brief Generate a START or RESTART condition
  1115. * @note The START bit can be set even if bus is BUSY or I2C is in slave mode.
  1116. * This action has no effect when RELOAD is set.
  1117. * @rmtoll CR1 START LL_I2C_GenerateStartCondition
  1118. * @param I2Cx I2C Instance.
  1119. * @retval None
  1120. */
  1121. __STATIC_INLINE void LL_I2C_GenerateStartCondition(I2C_TypeDef *I2Cx)
  1122. {
  1123. SET_BIT(I2Cx->CR1, I2C_CR1_START);
  1124. }
  1125. /**
  1126. * @brief Generate a STOP condition after the current byte transfer (master mode).
  1127. * @rmtoll CR1 STOP LL_I2C_GenerateStopCondition
  1128. * @param I2Cx I2C Instance.
  1129. * @retval None
  1130. */
  1131. __STATIC_INLINE void LL_I2C_GenerateStopCondition(I2C_TypeDef *I2Cx)
  1132. {
  1133. SET_BIT(I2Cx->CR1, I2C_CR1_STOP);
  1134. }
  1135. /**
  1136. * @brief Enable bit POS (master/host mode).
  1137. * @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.
  1138. * @rmtoll CR1 POS LL_I2C_EnableBitPOS
  1139. * @param I2Cx I2C Instance.
  1140. * @retval None
  1141. */
  1142. __STATIC_INLINE void LL_I2C_EnableBitPOS(I2C_TypeDef *I2Cx)
  1143. {
  1144. SET_BIT(I2Cx->CR1, I2C_CR1_POS);
  1145. }
  1146. /**
  1147. * @brief Disable bit POS (master/host mode).
  1148. * @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.
  1149. * @rmtoll CR1 POS LL_I2C_DisableBitPOS
  1150. * @param I2Cx I2C Instance.
  1151. * @retval None
  1152. */
  1153. __STATIC_INLINE void LL_I2C_DisableBitPOS(I2C_TypeDef *I2Cx)
  1154. {
  1155. CLEAR_BIT(I2Cx->CR1, I2C_CR1_POS);
  1156. }
  1157. /**
  1158. * @brief Check if bit POS is enabled or disabled.
  1159. * @rmtoll CR1 POS LL_I2C_IsEnabledBitPOS
  1160. * @param I2Cx I2C Instance.
  1161. * @retval State of bit (1 or 0).
  1162. */
  1163. __STATIC_INLINE uint32_t LL_I2C_IsEnabledBitPOS(I2C_TypeDef *I2Cx)
  1164. {
  1165. return (READ_BIT(I2Cx->CR1, I2C_CR1_POS) == (I2C_CR1_POS));
  1166. }
  1167. /**
  1168. * @brief Indicate the value of transfer direction.
  1169. * @note RESET: Bus is in read transfer (peripheral point of view).
  1170. * SET: Bus is in write transfer (peripheral point of view).
  1171. * @rmtoll SR2 TRA LL_I2C_GetTransferDirection
  1172. * @param I2Cx I2C Instance.
  1173. * @retval Returned value can be one of the following values:
  1174. * @arg @ref LL_I2C_DIRECTION_WRITE
  1175. * @arg @ref LL_I2C_DIRECTION_READ
  1176. */
  1177. __STATIC_INLINE uint32_t LL_I2C_GetTransferDirection(I2C_TypeDef *I2Cx)
  1178. {
  1179. return (uint32_t)(READ_BIT(I2Cx->SR2, I2C_SR2_TRA));
  1180. }
  1181. #if (defined(DMA1) || defined(DMA))
  1182. /**
  1183. * @brief Enable DMA last transfer.
  1184. * @note This action mean that next DMA EOT is the last transfer.
  1185. * @note Depending on devices and packages, DMA may not be available.
  1186. * Refer to device datasheet for DMA availability.
  1187. * @rmtoll CR2 LAST LL_I2C_EnableLastDMA
  1188. * @param I2Cx I2C Instance.
  1189. * @retval None
  1190. */
  1191. __STATIC_INLINE void LL_I2C_EnableLastDMA(I2C_TypeDef *I2Cx)
  1192. {
  1193. SET_BIT(I2Cx->CR2, I2C_CR2_LAST);
  1194. }
  1195. /**
  1196. * @brief Disable DMA last transfer.
  1197. * @note This action mean that next DMA EOT is not the last transfer.
  1198. * @note Depending on devices and packages, DMA may not be available.
  1199. * Refer to device datasheet for DMA availability.
  1200. * @rmtoll CR2 LAST LL_I2C_DisableLastDMA
  1201. * @param I2Cx I2C Instance.
  1202. * @retval None
  1203. */
  1204. __STATIC_INLINE void LL_I2C_DisableLastDMA(I2C_TypeDef *I2Cx)
  1205. {
  1206. CLEAR_BIT(I2Cx->CR2, I2C_CR2_LAST);
  1207. }
  1208. /**
  1209. * @brief Check if DMA last transfer is enabled or disabled.
  1210. * @note Depending on devices and packages, DMA may not be available.
  1211. * Refer to device datasheet for DMA availability.
  1212. * @rmtoll CR2 LAST LL_I2C_IsEnabledLastDMA
  1213. * @param I2Cx I2C Instance.
  1214. * @retval State of bit (1 or 0).
  1215. */
  1216. __STATIC_INLINE uint32_t LL_I2C_IsEnabledLastDMA(I2C_TypeDef *I2Cx)
  1217. {
  1218. return (READ_BIT(I2Cx->CR2, I2C_CR2_LAST) == (I2C_CR2_LAST));
  1219. }
  1220. #endif /* DMA1 or DMA */
  1221. /**
  1222. * @brief Read Receive Data register.
  1223. * @rmtoll DR DR LL_I2C_ReceiveData8
  1224. * @param I2Cx I2C Instance.
  1225. * @retval Value between Min_Data=0x0 and Max_Data=0xFF
  1226. */
  1227. __STATIC_INLINE uint8_t LL_I2C_ReceiveData8(I2C_TypeDef *I2Cx)
  1228. {
  1229. return (uint8_t)(READ_BIT(I2Cx->DR, I2C_DR_DR));
  1230. }
  1231. /**
  1232. * @brief Write in Transmit Data Register .
  1233. * @rmtoll DR DR LL_I2C_TransmitData8
  1234. * @param I2Cx I2C Instance.
  1235. * @param Data Value between Min_Data=0x0 and Max_Data=0xFF
  1236. * @retval None
  1237. */
  1238. __STATIC_INLINE void LL_I2C_TransmitData8(I2C_TypeDef *I2Cx, uint8_t Data)
  1239. {
  1240. MODIFY_REG(I2Cx->DR, I2C_DR_DR, Data);
  1241. }
  1242. /**
  1243. * @}
  1244. */
  1245. #if defined(USE_FULL_LL_DRIVER)
  1246. /** @defgroup I2C_LL_EF_Init Initialization and de-initialization functions
  1247. * @{
  1248. */
  1249. uint32_t LL_I2C_Init(I2C_TypeDef *I2Cx, LL_I2C_InitTypeDef *I2C_InitStruct);
  1250. uint32_t LL_I2C_DeInit(I2C_TypeDef *I2Cx);
  1251. void LL_I2C_StructInit(LL_I2C_InitTypeDef *I2C_InitStruct);
  1252. /**
  1253. * @}
  1254. */
  1255. #endif /* USE_FULL_LL_DRIVER */
  1256. /**
  1257. * @}
  1258. */
  1259. /**
  1260. * @}
  1261. */
  1262. #endif /* I2C1 || I2C2 */
  1263. /**
  1264. * @}
  1265. */
  1266. #ifdef __cplusplus
  1267. }
  1268. #endif
  1269. #endif /* __PY32F0xx_LL_I2C_H */
  1270. /************************ (C) COPYRIGHT Puya *****END OF FILE****/