py32f002b_ll_i2c.h 42 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271
  1. /**
  2. ******************************************************************************
  3. * @file py32f002b_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 __PY32F002B_LL_I2C_H
  32. #define __PY32F002B_LL_I2C_H
  33. #ifdef __cplusplus
  34. extern "C" {
  35. #endif
  36. /* Includes ------------------------------------------------------------------*/
  37. #include "py32f0xx.h"
  38. /** @addtogroup PY32F002B_LL_Driver
  39. * @{
  40. */
  41. #if defined (I2C1)
  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. /**
  285. * @brief Enable Clock stretching.
  286. * @note This bit can only be programmed when the I2C is disabled (PE = 0).
  287. * @rmtoll CR1 NOSTRETCH LL_I2C_EnableClockStretching
  288. * @param I2Cx I2C Instance.
  289. * @retval None
  290. */
  291. __STATIC_INLINE void LL_I2C_EnableClockStretching(I2C_TypeDef *I2Cx)
  292. {
  293. CLEAR_BIT(I2Cx->CR1, I2C_CR1_NOSTRETCH);
  294. }
  295. /**
  296. * @brief Disable Clock stretching.
  297. * @note This bit can only be programmed when the I2C is disabled (PE = 0).
  298. * @rmtoll CR1 NOSTRETCH LL_I2C_DisableClockStretching
  299. * @param I2Cx I2C Instance.
  300. * @retval None
  301. */
  302. __STATIC_INLINE void LL_I2C_DisableClockStretching(I2C_TypeDef *I2Cx)
  303. {
  304. SET_BIT(I2Cx->CR1, I2C_CR1_NOSTRETCH);
  305. }
  306. /**
  307. * @brief Check if Clock stretching is enabled or disabled.
  308. * @rmtoll CR1 NOSTRETCH LL_I2C_IsEnabledClockStretching
  309. * @param I2Cx I2C Instance.
  310. * @retval State of bit (1 or 0).
  311. */
  312. __STATIC_INLINE uint32_t LL_I2C_IsEnabledClockStretching(I2C_TypeDef *I2Cx)
  313. {
  314. return (READ_BIT(I2Cx->CR1, I2C_CR1_NOSTRETCH) != (I2C_CR1_NOSTRETCH));
  315. }
  316. /**
  317. * @brief Enable General Call.
  318. * @note When enabled the Address 0x00 is ACKed.
  319. * @rmtoll CR1 ENGC LL_I2C_EnableGeneralCall
  320. * @param I2Cx I2C Instance.
  321. * @retval None
  322. */
  323. __STATIC_INLINE void LL_I2C_EnableGeneralCall(I2C_TypeDef *I2Cx)
  324. {
  325. SET_BIT(I2Cx->CR1, I2C_CR1_ENGC);
  326. }
  327. /**
  328. * @brief Disable General Call.
  329. * @note When disabled the Address 0x00 is NACKed.
  330. * @rmtoll CR1 ENGC LL_I2C_DisableGeneralCall
  331. * @param I2Cx I2C Instance.
  332. * @retval None
  333. */
  334. __STATIC_INLINE void LL_I2C_DisableGeneralCall(I2C_TypeDef *I2Cx)
  335. {
  336. CLEAR_BIT(I2Cx->CR1, I2C_CR1_ENGC);
  337. }
  338. /**
  339. * @brief Check if General Call is enabled or disabled.
  340. * @rmtoll CR1 ENGC LL_I2C_IsEnabledGeneralCall
  341. * @param I2Cx I2C Instance.
  342. * @retval State of bit (1 or 0).
  343. */
  344. __STATIC_INLINE uint32_t LL_I2C_IsEnabledGeneralCall(I2C_TypeDef *I2Cx)
  345. {
  346. return (READ_BIT(I2Cx->CR1, I2C_CR1_ENGC) == (I2C_CR1_ENGC));
  347. }
  348. /**
  349. * @brief Set the Own Address1.
  350. * @param I2Cx I2C Instance.
  351. * @param OwnAddress1 This parameter must be a value between Min_Data=0 and Max_Data=0x3FF.
  352. * @param OwnAddrSize This parameter is not used, can pass 0.
  353. * @retval None
  354. */
  355. __STATIC_INLINE void LL_I2C_SetOwnAddress1(I2C_TypeDef *I2Cx, uint32_t OwnAddress1, uint32_t OwnAddrSize)
  356. {
  357. (void)OwnAddrSize;
  358. MODIFY_REG(I2Cx->OAR1, I2C_OAR1_ADD1_7, OwnAddress1);
  359. }
  360. /**
  361. * @brief Configure the Peripheral clock frequency.
  362. * @rmtoll CR2 FREQ LL_I2C_SetPeriphClock
  363. * @param I2Cx I2C Instance.
  364. * @param PeriphClock Peripheral Clock (in Hz)
  365. * @retval None
  366. */
  367. __STATIC_INLINE void LL_I2C_SetPeriphClock(I2C_TypeDef *I2Cx, uint32_t PeriphClock)
  368. {
  369. MODIFY_REG(I2Cx->CR2, I2C_CR2_FREQ, __LL_I2C_FREQ_HZ_TO_MHZ(PeriphClock));
  370. }
  371. /**
  372. * @brief Get the Peripheral clock frequency.
  373. * @rmtoll CR2 FREQ LL_I2C_GetPeriphClock
  374. * @param I2Cx I2C Instance.
  375. * @retval Value of Peripheral Clock (in Hz)
  376. */
  377. __STATIC_INLINE uint32_t LL_I2C_GetPeriphClock(I2C_TypeDef *I2Cx)
  378. {
  379. return (uint32_t)(__LL_I2C_FREQ_MHZ_TO_HZ(READ_BIT(I2Cx->CR2, I2C_CR2_FREQ)));
  380. }
  381. /**
  382. * @brief Configure the Duty cycle (Fast mode only).
  383. * @rmtoll CCR DUTY LL_I2C_SetDutyCycle
  384. * @param I2Cx I2C Instance.
  385. * @param DutyCycle This parameter can be one of the following values:
  386. * @arg @ref LL_I2C_DUTYCYCLE_2
  387. * @arg @ref LL_I2C_DUTYCYCLE_16_9
  388. * @retval None
  389. */
  390. __STATIC_INLINE void LL_I2C_SetDutyCycle(I2C_TypeDef *I2Cx, uint32_t DutyCycle)
  391. {
  392. MODIFY_REG(I2Cx->CCR, I2C_CCR_DUTY, DutyCycle);
  393. }
  394. /**
  395. * @brief Get the Duty cycle (Fast mode only).
  396. * @rmtoll CCR DUTY LL_I2C_GetDutyCycle
  397. * @param I2Cx I2C Instance.
  398. * @retval Returned value can be one of the following values:
  399. * @arg @ref LL_I2C_DUTYCYCLE_2
  400. * @arg @ref LL_I2C_DUTYCYCLE_16_9
  401. */
  402. __STATIC_INLINE uint32_t LL_I2C_GetDutyCycle(I2C_TypeDef *I2Cx)
  403. {
  404. return (uint32_t)(READ_BIT(I2Cx->CCR, I2C_CCR_DUTY));
  405. }
  406. /**
  407. * @brief Configure the I2C master clock speed mode.
  408. * @rmtoll CCR FS LL_I2C_SetClockSpeedMode
  409. * @param I2Cx I2C Instance.
  410. * @param ClockSpeedMode This parameter can be one of the following values:
  411. * @arg @ref LL_I2C_CLOCK_SPEED_STANDARD_MODE
  412. * @arg @ref LL_I2C_CLOCK_SPEED_FAST_MODE
  413. * @retval None
  414. */
  415. __STATIC_INLINE void LL_I2C_SetClockSpeedMode(I2C_TypeDef *I2Cx, uint32_t ClockSpeedMode)
  416. {
  417. MODIFY_REG(I2Cx->CCR, I2C_CCR_FS, ClockSpeedMode);
  418. }
  419. /**
  420. * @brief Get the the I2C master speed mode.
  421. * @rmtoll CCR FS LL_I2C_GetClockSpeedMode
  422. * @param I2Cx I2C Instance.
  423. * @retval Returned value can be one of the following values:
  424. * @arg @ref LL_I2C_CLOCK_SPEED_STANDARD_MODE
  425. * @arg @ref LL_I2C_CLOCK_SPEED_FAST_MODE
  426. */
  427. __STATIC_INLINE uint32_t LL_I2C_GetClockSpeedMode(I2C_TypeDef *I2Cx)
  428. {
  429. return (uint32_t)(READ_BIT(I2Cx->CCR, I2C_CCR_FS));
  430. }
  431. /**
  432. * @brief Configure the SCL, SDA rising time.
  433. * @note This bit can only be programmed when the I2C is disabled (PE = 0).
  434. * @rmtoll TRISE TRISE LL_I2C_SetRiseTime
  435. * @param I2Cx I2C Instance.
  436. * @param RiseTime This parameter must be a value between Min_Data=0x02 and Max_Data=0x3F.
  437. * @retval None
  438. */
  439. __STATIC_INLINE void LL_I2C_SetRiseTime(I2C_TypeDef *I2Cx, uint32_t RiseTime)
  440. {
  441. MODIFY_REG(I2Cx->TRISE, I2C_TRISE_TRISE, RiseTime);
  442. }
  443. /**
  444. * @brief Get the SCL, SDA rising time.
  445. * @rmtoll TRISE TRISE LL_I2C_GetRiseTime
  446. * @param I2Cx I2C Instance.
  447. * @retval Value between Min_Data=0x02 and Max_Data=0x3F
  448. */
  449. __STATIC_INLINE uint32_t LL_I2C_GetRiseTime(I2C_TypeDef *I2Cx)
  450. {
  451. return (uint32_t)(READ_BIT(I2Cx->TRISE, I2C_TRISE_TRISE));
  452. }
  453. /**
  454. * @brief Configure the SCL high and low period.
  455. * @note This bit can only be programmed when the I2C is disabled (PE = 0).
  456. * @rmtoll CCR CCR LL_I2C_SetClockPeriod
  457. * @param I2Cx I2C Instance.
  458. * @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.
  459. * @retval None
  460. */
  461. __STATIC_INLINE void LL_I2C_SetClockPeriod(I2C_TypeDef *I2Cx, uint32_t ClockPeriod)
  462. {
  463. MODIFY_REG(I2Cx->CCR, I2C_CCR_CCR, ClockPeriod);
  464. }
  465. /**
  466. * @brief Get the SCL high and low period.
  467. * @rmtoll CCR CCR LL_I2C_GetClockPeriod
  468. * @param I2Cx I2C Instance.
  469. * @retval Value between Min_Data=0x004 and Max_Data=0xFFF, except in FAST DUTY mode where Min_Data=0x001.
  470. */
  471. __STATIC_INLINE uint32_t LL_I2C_GetClockPeriod(I2C_TypeDef *I2Cx)
  472. {
  473. return (uint32_t)(READ_BIT(I2Cx->CCR, I2C_CCR_CCR));
  474. }
  475. /**
  476. * @brief Configure the SCL speed.
  477. * @note This bit can only be programmed when the I2C is disabled (PE = 0).
  478. * @rmtoll CR2 FREQ LL_I2C_ConfigSpeed\n
  479. * TRISE TRISE LL_I2C_ConfigSpeed\n
  480. * CCR FS LL_I2C_ConfigSpeed\n
  481. * CCR DUTY LL_I2C_ConfigSpeed\n
  482. * CCR CCR LL_I2C_ConfigSpeed
  483. * @param I2Cx I2C Instance.
  484. * @param PeriphClock Peripheral Clock (in Hz)
  485. * @param ClockSpeed This parameter must be a value lower than 400kHz (in Hz).
  486. * @param DutyCycle This parameter can be one of the following values:
  487. * @arg @ref LL_I2C_DUTYCYCLE_2
  488. * @arg @ref LL_I2C_DUTYCYCLE_16_9
  489. * @retval None
  490. */
  491. __STATIC_INLINE void LL_I2C_ConfigSpeed(I2C_TypeDef *I2Cx, uint32_t PeriphClock, uint32_t ClockSpeed,
  492. uint32_t DutyCycle)
  493. {
  494. register uint32_t freqrange = 0x0U;
  495. register uint32_t clockconfig = 0x0U;
  496. /* Compute frequency range */
  497. freqrange = __LL_I2C_FREQ_HZ_TO_MHZ(PeriphClock);
  498. /* Configure I2Cx: Frequency range register */
  499. MODIFY_REG(I2Cx->CR2, I2C_CR2_FREQ, freqrange);
  500. /* Configure I2Cx: Rise Time register */
  501. MODIFY_REG(I2Cx->TRISE, I2C_TRISE_TRISE, __LL_I2C_RISE_TIME(freqrange, ClockSpeed));
  502. /* Configure Speed mode, Duty Cycle and Clock control register value */
  503. if (ClockSpeed > LL_I2C_MAX_SPEED_STANDARD)
  504. {
  505. /* Set Speed mode at fast and duty cycle for Clock Speed request in fast clock range */
  506. clockconfig = LL_I2C_CLOCK_SPEED_FAST_MODE | \
  507. __LL_I2C_SPEED_FAST_TO_CCR(PeriphClock, ClockSpeed, DutyCycle) | \
  508. DutyCycle;
  509. }
  510. else
  511. {
  512. /* Set Speed mode at standard for Clock Speed request in standard clock range */
  513. clockconfig = LL_I2C_CLOCK_SPEED_STANDARD_MODE | \
  514. __LL_I2C_SPEED_STANDARD_TO_CCR(PeriphClock, ClockSpeed);
  515. }
  516. /* Configure I2Cx: Clock control register */
  517. MODIFY_REG(I2Cx->CCR, (I2C_CCR_FS | I2C_CCR_DUTY | I2C_CCR_CCR), clockconfig);
  518. }
  519. /**
  520. * @}
  521. */
  522. /** @defgroup I2C_LL_EF_IT_Management IT Management
  523. * @{
  524. */
  525. /**
  526. * @brief Enable TXE interrupt.
  527. * @rmtoll CR2 ITEVTEN LL_I2C_EnableIT_TX\n
  528. * CR2 ITBUFEN LL_I2C_EnableIT_TX
  529. * @param I2Cx I2C Instance.
  530. * @retval None
  531. */
  532. __STATIC_INLINE void LL_I2C_EnableIT_TX(I2C_TypeDef *I2Cx)
  533. {
  534. SET_BIT(I2Cx->CR2, I2C_CR2_ITEVTEN | I2C_CR2_ITBUFEN);
  535. }
  536. /**
  537. * @brief Disable TXE interrupt.
  538. * @rmtoll CR2 ITEVTEN LL_I2C_DisableIT_TX\n
  539. * CR2 ITBUFEN LL_I2C_DisableIT_TX
  540. * @param I2Cx I2C Instance.
  541. * @retval None
  542. */
  543. __STATIC_INLINE void LL_I2C_DisableIT_TX(I2C_TypeDef *I2Cx)
  544. {
  545. CLEAR_BIT(I2Cx->CR2, I2C_CR2_ITEVTEN | I2C_CR2_ITBUFEN);
  546. }
  547. /**
  548. * @brief Check if the TXE Interrupt is enabled or disabled.
  549. * @rmtoll CR2 ITEVTEN LL_I2C_IsEnabledIT_TX\n
  550. * CR2 ITBUFEN LL_I2C_IsEnabledIT_TX
  551. * @param I2Cx I2C Instance.
  552. * @retval State of bit (1 or 0).
  553. */
  554. __STATIC_INLINE uint32_t LL_I2C_IsEnabledIT_TX(I2C_TypeDef *I2Cx)
  555. {
  556. return (READ_BIT(I2Cx->CR2, I2C_CR2_ITEVTEN | I2C_CR2_ITBUFEN) == (I2C_CR2_ITEVTEN | I2C_CR2_ITBUFEN));
  557. }
  558. /**
  559. * @brief Enable RXNE interrupt.
  560. * @rmtoll CR2 ITEVTEN LL_I2C_EnableIT_RX\n
  561. * CR2 ITBUFEN LL_I2C_EnableIT_RX
  562. * @param I2Cx I2C Instance.
  563. * @retval None
  564. */
  565. __STATIC_INLINE void LL_I2C_EnableIT_RX(I2C_TypeDef *I2Cx)
  566. {
  567. SET_BIT(I2Cx->CR2, I2C_CR2_ITEVTEN | I2C_CR2_ITBUFEN);
  568. }
  569. /**
  570. * @brief Disable RXNE interrupt.
  571. * @rmtoll CR2 ITEVTEN LL_I2C_DisableIT_RX\n
  572. * CR2 ITBUFEN LL_I2C_DisableIT_RX
  573. * @param I2Cx I2C Instance.
  574. * @retval None
  575. */
  576. __STATIC_INLINE void LL_I2C_DisableIT_RX(I2C_TypeDef *I2Cx)
  577. {
  578. CLEAR_BIT(I2Cx->CR2, I2C_CR2_ITEVTEN | I2C_CR2_ITBUFEN);
  579. }
  580. /**
  581. * @brief Check if the RXNE Interrupt is enabled or disabled.
  582. * @rmtoll CR2 ITEVTEN LL_I2C_IsEnabledIT_RX\n
  583. * CR2 ITBUFEN LL_I2C_IsEnabledIT_RX
  584. * @param I2Cx I2C Instance.
  585. * @retval State of bit (1 or 0).
  586. */
  587. __STATIC_INLINE uint32_t LL_I2C_IsEnabledIT_RX(I2C_TypeDef *I2Cx)
  588. {
  589. return (READ_BIT(I2Cx->CR2, I2C_CR2_ITEVTEN | I2C_CR2_ITBUFEN) == (I2C_CR2_ITEVTEN | I2C_CR2_ITBUFEN));
  590. }
  591. /**
  592. * @brief Enable Events interrupts.
  593. * @note Any of these events will generate interrupt :
  594. * Start Bit (SB)
  595. * Address sent, Address matched (ADDR)
  596. * Stop detection (STOPF)
  597. * Byte transfer finished (BTF)
  598. *
  599. * @note Any of these events will generate interrupt if Buffer interrupts are enabled too(using unitary function @ref LL_I2C_EnableIT_BUF()) :
  600. * Receive buffer not empty (RXNE)
  601. * Transmit buffer empty (TXE)
  602. * @rmtoll CR2 ITEVTEN LL_I2C_EnableIT_EVT
  603. * @param I2Cx I2C Instance.
  604. * @retval None
  605. */
  606. __STATIC_INLINE void LL_I2C_EnableIT_EVT(I2C_TypeDef *I2Cx)
  607. {
  608. SET_BIT(I2Cx->CR2, I2C_CR2_ITEVTEN);
  609. }
  610. /**
  611. * @brief Disable Events interrupts.
  612. * @note Any of these events will generate interrupt :
  613. * Start Bit (SB)
  614. * Address sent, Address matched (ADDR)
  615. * Stop detection (STOPF)
  616. * Byte transfer finished (BTF)
  617. * Receive buffer not empty (RXNE)
  618. * Transmit buffer empty (TXE)
  619. * @rmtoll CR2 ITEVTEN LL_I2C_DisableIT_EVT
  620. * @param I2Cx I2C Instance.
  621. * @retval None
  622. */
  623. __STATIC_INLINE void LL_I2C_DisableIT_EVT(I2C_TypeDef *I2Cx)
  624. {
  625. CLEAR_BIT(I2Cx->CR2, I2C_CR2_ITEVTEN);
  626. }
  627. /**
  628. * @brief Check if Events interrupts are enabled or disabled.
  629. * @rmtoll CR2 ITEVTEN LL_I2C_IsEnabledIT_EVT
  630. * @param I2Cx I2C Instance.
  631. * @retval State of bit (1 or 0).
  632. */
  633. __STATIC_INLINE uint32_t LL_I2C_IsEnabledIT_EVT(I2C_TypeDef *I2Cx)
  634. {
  635. return (READ_BIT(I2Cx->CR2, I2C_CR2_ITEVTEN) == (I2C_CR2_ITEVTEN));
  636. }
  637. /**
  638. * @brief Enable Buffer interrupts.
  639. * @note Any of these Buffer events will generate interrupt if Events interrupts are enabled too(using unitary function @ref LL_I2C_EnableIT_EVT()) :
  640. * Receive buffer not empty (RXNE)
  641. * Transmit buffer empty (TXE)
  642. * @rmtoll CR2 ITBUFEN LL_I2C_EnableIT_BUF
  643. * @param I2Cx I2C Instance.
  644. * @retval None
  645. */
  646. __STATIC_INLINE void LL_I2C_EnableIT_BUF(I2C_TypeDef *I2Cx)
  647. {
  648. SET_BIT(I2Cx->CR2, I2C_CR2_ITBUFEN);
  649. }
  650. /**
  651. * @brief Disable Buffer interrupts.
  652. * @note Any of these Buffer events will generate interrupt :
  653. * Receive buffer not empty (RXNE)
  654. * Transmit buffer empty (TXE)
  655. * @rmtoll CR2 ITBUFEN LL_I2C_DisableIT_BUF
  656. * @param I2Cx I2C Instance.
  657. * @retval None
  658. */
  659. __STATIC_INLINE void LL_I2C_DisableIT_BUF(I2C_TypeDef *I2Cx)
  660. {
  661. CLEAR_BIT(I2Cx->CR2, I2C_CR2_ITBUFEN);
  662. }
  663. /**
  664. * @brief Check if Buffer interrupts are enabled or disabled.
  665. * @rmtoll CR2 ITBUFEN LL_I2C_IsEnabledIT_BUF
  666. * @param I2Cx I2C Instance.
  667. * @retval State of bit (1 or 0).
  668. */
  669. __STATIC_INLINE uint32_t LL_I2C_IsEnabledIT_BUF(I2C_TypeDef *I2Cx)
  670. {
  671. return (READ_BIT(I2Cx->CR2, I2C_CR2_ITBUFEN) == (I2C_CR2_ITBUFEN));
  672. }
  673. /**
  674. * @brief Enable Error interrupts.
  675. * @note Any of these errors will generate interrupt :
  676. * Bus Error detection (BERR)
  677. * Arbitration Loss (ARLO)
  678. * Acknowledge Failure(AF)
  679. * Overrun/Underrun (OVR)
  680. * @rmtoll CR2 ITERREN LL_I2C_EnableIT_ERR
  681. * @param I2Cx I2C Instance.
  682. * @retval None
  683. */
  684. __STATIC_INLINE void LL_I2C_EnableIT_ERR(I2C_TypeDef *I2Cx)
  685. {
  686. SET_BIT(I2Cx->CR2, I2C_CR2_ITERREN);
  687. }
  688. /**
  689. * @brief Disable Error interrupts.
  690. * @note Any of these errors will generate interrupt :
  691. * Bus Error detection (BERR)
  692. * Arbitration Loss (ARLO)
  693. * Acknowledge Failure(AF)
  694. * Overrun/Underrun (OVR)
  695. * SMBus Timeout detection (TIMEOUT)
  696. * SMBus PEC error detection (PECERR)
  697. * SMBus Alert pin event detection (SMBALERT)
  698. * @rmtoll CR2 ITERREN LL_I2C_DisableIT_ERR
  699. * @param I2Cx I2C Instance.
  700. * @retval None
  701. */
  702. __STATIC_INLINE void LL_I2C_DisableIT_ERR(I2C_TypeDef *I2Cx)
  703. {
  704. CLEAR_BIT(I2Cx->CR2, I2C_CR2_ITERREN);
  705. }
  706. /**
  707. * @brief Check if Error interrupts are enabled or disabled.
  708. * @rmtoll CR2 ITERREN LL_I2C_IsEnabledIT_ERR
  709. * @param I2Cx I2C Instance.
  710. * @retval State of bit (1 or 0).
  711. */
  712. __STATIC_INLINE uint32_t LL_I2C_IsEnabledIT_ERR(I2C_TypeDef *I2Cx)
  713. {
  714. return (READ_BIT(I2Cx->CR2, I2C_CR2_ITERREN) == (I2C_CR2_ITERREN));
  715. }
  716. /**
  717. * @}
  718. */
  719. /** @defgroup I2C_LL_EF_FLAG_management FLAG Management
  720. * @{
  721. */
  722. /**
  723. * @brief Indicate the status of Transmit data register empty flag.
  724. * @note RESET: When next data is written in Transmit data register.
  725. * SET: When Transmit data register is empty.
  726. * @rmtoll SR1 TXE LL_I2C_IsActiveFlag_TXE
  727. * @param I2Cx I2C Instance.
  728. * @retval State of bit (1 or 0).
  729. */
  730. __STATIC_INLINE uint32_t LL_I2C_IsActiveFlag_TXE(I2C_TypeDef *I2Cx)
  731. {
  732. return (READ_BIT(I2Cx->SR1, I2C_SR1_TXE) == (I2C_SR1_TXE));
  733. }
  734. /**
  735. * @brief Indicate the status of Byte Transfer Finished flag.
  736. * RESET: When Data byte transfer not done.
  737. * SET: When Data byte transfer succeeded.
  738. * @rmtoll SR1 BTF LL_I2C_IsActiveFlag_BTF
  739. * @param I2Cx I2C Instance.
  740. * @retval State of bit (1 or 0).
  741. */
  742. __STATIC_INLINE uint32_t LL_I2C_IsActiveFlag_BTF(I2C_TypeDef *I2Cx)
  743. {
  744. return (READ_BIT(I2Cx->SR1, I2C_SR1_BTF) == (I2C_SR1_BTF));
  745. }
  746. /**
  747. * @brief Indicate the status of Receive data register not empty flag.
  748. * @note RESET: When Receive data register is read.
  749. * SET: When the received data is copied in Receive data register.
  750. * @rmtoll SR1 RXNE LL_I2C_IsActiveFlag_RXNE
  751. * @param I2Cx I2C Instance.
  752. * @retval State of bit (1 or 0).
  753. */
  754. __STATIC_INLINE uint32_t LL_I2C_IsActiveFlag_RXNE(I2C_TypeDef *I2Cx)
  755. {
  756. return (READ_BIT(I2Cx->SR1, I2C_SR1_RXNE) == (I2C_SR1_RXNE));
  757. }
  758. /**
  759. * @brief Indicate the status of Start Bit (master mode).
  760. * @note RESET: When No Start condition.
  761. * SET: When Start condition is generated.
  762. * @rmtoll SR1 SB LL_I2C_IsActiveFlag_SB
  763. * @param I2Cx I2C Instance.
  764. * @retval State of bit (1 or 0).
  765. */
  766. __STATIC_INLINE uint32_t LL_I2C_IsActiveFlag_SB(I2C_TypeDef *I2Cx)
  767. {
  768. return (READ_BIT(I2Cx->SR1, I2C_SR1_SB) == (I2C_SR1_SB));
  769. }
  770. /**
  771. * @brief Indicate the status of Address sent (master mode) or Address matched flag (slave mode).
  772. * @note RESET: Clear default value.
  773. * 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).
  774. * @rmtoll SR1 ADDR LL_I2C_IsActiveFlag_ADDR
  775. * @param I2Cx I2C Instance.
  776. * @retval State of bit (1 or 0).
  777. */
  778. __STATIC_INLINE uint32_t LL_I2C_IsActiveFlag_ADDR(I2C_TypeDef *I2Cx)
  779. {
  780. return (READ_BIT(I2Cx->SR1, I2C_SR1_ADDR) == (I2C_SR1_ADDR));
  781. }
  782. /**
  783. * @brief Indicate the status of Acknowledge failure flag.
  784. * @note RESET: No acknowledge failure.
  785. * SET: When an acknowledge failure is received after a byte transmission.
  786. * @rmtoll SR1 AF LL_I2C_IsActiveFlag_AF
  787. * @param I2Cx I2C Instance.
  788. * @retval State of bit (1 or 0).
  789. */
  790. __STATIC_INLINE uint32_t LL_I2C_IsActiveFlag_AF(I2C_TypeDef *I2Cx)
  791. {
  792. return (READ_BIT(I2Cx->SR1, I2C_SR1_AF) == (I2C_SR1_AF));
  793. }
  794. /**
  795. * @brief Indicate the status of Stop detection flag (slave mode).
  796. * @note RESET: Clear default value.
  797. * SET: When a Stop condition is detected.
  798. * @rmtoll SR1 STOPF LL_I2C_IsActiveFlag_STOP
  799. * @param I2Cx I2C Instance.
  800. * @retval State of bit (1 or 0).
  801. */
  802. __STATIC_INLINE uint32_t LL_I2C_IsActiveFlag_STOP(I2C_TypeDef *I2Cx)
  803. {
  804. return (READ_BIT(I2Cx->SR1, I2C_SR1_STOPF) == (I2C_SR1_STOPF));
  805. }
  806. /**
  807. * @brief Indicate the status of Bus error flag.
  808. * @note RESET: Clear default value.
  809. * SET: When a misplaced Start or Stop condition is detected.
  810. * @rmtoll SR1 BERR LL_I2C_IsActiveFlag_BERR
  811. * @param I2Cx I2C Instance.
  812. * @retval State of bit (1 or 0).
  813. */
  814. __STATIC_INLINE uint32_t LL_I2C_IsActiveFlag_BERR(I2C_TypeDef *I2Cx)
  815. {
  816. return (READ_BIT(I2Cx->SR1, I2C_SR1_BERR) == (I2C_SR1_BERR));
  817. }
  818. /**
  819. * @brief Indicate the status of Arbitration lost flag.
  820. * @note RESET: Clear default value.
  821. * SET: When arbitration lost.
  822. * @rmtoll SR1 ARLO LL_I2C_IsActiveFlag_ARLO
  823. * @param I2Cx I2C Instance.
  824. * @retval State of bit (1 or 0).
  825. */
  826. __STATIC_INLINE uint32_t LL_I2C_IsActiveFlag_ARLO(I2C_TypeDef *I2Cx)
  827. {
  828. return (READ_BIT(I2Cx->SR1, I2C_SR1_ARLO) == (I2C_SR1_ARLO));
  829. }
  830. /**
  831. * @brief Indicate the status of Overrun/Underrun flag.
  832. * @note RESET: Clear default value.
  833. * SET: When an overrun/underrun error occurs (Clock Stretching Disabled).
  834. * @rmtoll SR1 OVR LL_I2C_IsActiveFlag_OVR
  835. * @param I2Cx I2C Instance.
  836. * @retval State of bit (1 or 0).
  837. */
  838. __STATIC_INLINE uint32_t LL_I2C_IsActiveFlag_OVR(I2C_TypeDef *I2Cx)
  839. {
  840. return (READ_BIT(I2Cx->SR1, I2C_SR1_OVR) == (I2C_SR1_OVR));
  841. }
  842. /**
  843. * @brief Indicate the status of SMBus PEC error flag in reception.
  844. * @rmtoll SR1 PECERR LL_I2C_IsActiveSMBusFlag_PECERR
  845. * @param I2Cx I2C Instance.
  846. * @retval State of bit (1 or 0).
  847. */
  848. __STATIC_INLINE uint32_t LL_I2C_IsActiveSMBusFlag_PECERR(I2C_TypeDef *I2Cx)
  849. {
  850. return (READ_BIT(I2Cx->SR1, I2C_SR1_PECERR) == (I2C_SR1_PECERR));
  851. }
  852. /**
  853. * @brief Indicate the status of Bus Busy flag.
  854. * @note RESET: Clear default value.
  855. * SET: When a Start condition is detected.
  856. * @rmtoll SR2 BUSY LL_I2C_IsActiveFlag_BUSY
  857. * @param I2Cx I2C Instance.
  858. * @retval State of bit (1 or 0).
  859. */
  860. __STATIC_INLINE uint32_t LL_I2C_IsActiveFlag_BUSY(I2C_TypeDef *I2Cx)
  861. {
  862. return (READ_BIT(I2Cx->SR2, I2C_SR2_BUSY) == (I2C_SR2_BUSY));
  863. }
  864. /**
  865. * @brief Indicate the status of General call address reception (Slave mode).
  866. * @note RESET: No Generall call address
  867. * SET: General call address received.
  868. * @note This status is cleared by hardware after a STOP condition or repeated START condition.
  869. * @rmtoll SR2 GENCALL LL_I2C_IsActiveFlag_GENCALL
  870. * @param I2Cx I2C Instance.
  871. * @retval State of bit (1 or 0).
  872. */
  873. __STATIC_INLINE uint32_t LL_I2C_IsActiveFlag_GENCALL(I2C_TypeDef *I2Cx)
  874. {
  875. return (READ_BIT(I2Cx->SR2, I2C_SR2_GENCALL) == (I2C_SR2_GENCALL));
  876. }
  877. /**
  878. * @brief Indicate the status of Master/Slave flag.
  879. * @note RESET: Slave Mode.
  880. * SET: Master Mode.
  881. * @rmtoll SR2 MSL LL_I2C_IsActiveFlag_MSL
  882. * @param I2Cx I2C Instance.
  883. * @retval State of bit (1 or 0).
  884. */
  885. __STATIC_INLINE uint32_t LL_I2C_IsActiveFlag_MSL(I2C_TypeDef *I2Cx)
  886. {
  887. return (READ_BIT(I2Cx->SR2, I2C_SR2_MSL) == (I2C_SR2_MSL));
  888. }
  889. /**
  890. * @brief Clear Address Matched flag.
  891. * @note Clearing this flag is done by a read access to the I2Cx_SR1
  892. * register followed by a read access to the I2Cx_SR2 register.
  893. * @rmtoll SR1 ADDR LL_I2C_ClearFlag_ADDR
  894. * @param I2Cx I2C Instance.
  895. * @retval None
  896. */
  897. __STATIC_INLINE void LL_I2C_ClearFlag_ADDR(I2C_TypeDef *I2Cx)
  898. {
  899. __IO uint32_t tmpreg;
  900. tmpreg = I2Cx->SR1;
  901. (void) tmpreg;
  902. tmpreg = I2Cx->SR2;
  903. (void) tmpreg;
  904. }
  905. /**
  906. * @brief Clear Acknowledge failure flag.
  907. * @rmtoll SR1 AF LL_I2C_ClearFlag_AF
  908. * @param I2Cx I2C Instance.
  909. * @retval None
  910. */
  911. __STATIC_INLINE void LL_I2C_ClearFlag_AF(I2C_TypeDef *I2Cx)
  912. {
  913. CLEAR_BIT(I2Cx->SR1, I2C_SR1_AF);
  914. }
  915. /**
  916. * @brief Clear Stop detection flag.
  917. * @note Clearing this flag is done by a read access to the I2Cx_SR1
  918. * register followed by a write access to I2Cx_CR1 register.
  919. * @rmtoll SR1 STOPF LL_I2C_ClearFlag_STOP\n
  920. * CR1 PE LL_I2C_ClearFlag_STOP
  921. * @param I2Cx I2C Instance.
  922. * @retval None
  923. */
  924. __STATIC_INLINE void LL_I2C_ClearFlag_STOP(I2C_TypeDef *I2Cx)
  925. {
  926. __IO uint32_t tmpreg;
  927. tmpreg = I2Cx->SR1;
  928. (void) tmpreg;
  929. SET_BIT(I2Cx->CR1, I2C_CR1_PE);
  930. }
  931. /**
  932. * @brief Clear Bus error flag.
  933. * @rmtoll SR1 BERR LL_I2C_ClearFlag_BERR
  934. * @param I2Cx I2C Instance.
  935. * @retval None
  936. */
  937. __STATIC_INLINE void LL_I2C_ClearFlag_BERR(I2C_TypeDef *I2Cx)
  938. {
  939. CLEAR_BIT(I2Cx->SR1, I2C_SR1_BERR);
  940. }
  941. /**
  942. * @brief Clear Arbitration lost flag.
  943. * @rmtoll SR1 ARLO LL_I2C_ClearFlag_ARLO
  944. * @param I2Cx I2C Instance.
  945. * @retval None
  946. */
  947. __STATIC_INLINE void LL_I2C_ClearFlag_ARLO(I2C_TypeDef *I2Cx)
  948. {
  949. CLEAR_BIT(I2Cx->SR1, I2C_SR1_ARLO);
  950. }
  951. /**
  952. * @brief Clear Overrun/Underrun flag.
  953. * @rmtoll SR1 OVR LL_I2C_ClearFlag_OVR
  954. * @param I2Cx I2C Instance.
  955. * @retval None
  956. */
  957. __STATIC_INLINE void LL_I2C_ClearFlag_OVR(I2C_TypeDef *I2Cx)
  958. {
  959. CLEAR_BIT(I2Cx->SR1, I2C_SR1_OVR);
  960. }
  961. /**
  962. * @brief Clear SMBus PEC error flag.
  963. * @rmtoll SR1 PECERR LL_I2C_ClearSMBusFlag_PECERR
  964. * @param I2Cx I2C Instance.
  965. * @retval None
  966. */
  967. __STATIC_INLINE void LL_I2C_ClearSMBusFlag_PECERR(I2C_TypeDef *I2Cx)
  968. {
  969. CLEAR_BIT(I2Cx->SR1, I2C_SR1_PECERR);
  970. }
  971. /**
  972. * @}
  973. */
  974. /** @defgroup I2C_LL_EF_Data_Management Data_Management
  975. * @{
  976. */
  977. /**
  978. * @brief Enable Reset of I2C peripheral.
  979. * @rmtoll CR1 SWRST LL_I2C_EnableReset
  980. * @param I2Cx I2C Instance.
  981. * @retval None
  982. */
  983. __STATIC_INLINE void LL_I2C_EnableReset(I2C_TypeDef *I2Cx)
  984. {
  985. SET_BIT(I2Cx->CR1, I2C_CR1_SWRST);
  986. }
  987. /**
  988. * @brief Disable Reset of I2C peripheral.
  989. * @rmtoll CR1 SWRST LL_I2C_DisableReset
  990. * @param I2Cx I2C Instance.
  991. * @retval None
  992. */
  993. __STATIC_INLINE void LL_I2C_DisableReset(I2C_TypeDef *I2Cx)
  994. {
  995. CLEAR_BIT(I2Cx->CR1, I2C_CR1_SWRST);
  996. }
  997. /**
  998. * @brief Check if the I2C peripheral is under reset state or not.
  999. * @rmtoll CR1 SWRST LL_I2C_IsResetEnabled
  1000. * @param I2Cx I2C Instance.
  1001. * @retval State of bit (1 or 0).
  1002. */
  1003. __STATIC_INLINE uint32_t LL_I2C_IsResetEnabled(I2C_TypeDef *I2Cx)
  1004. {
  1005. return (READ_BIT(I2Cx->CR1, I2C_CR1_SWRST) == (I2C_CR1_SWRST));
  1006. }
  1007. /**
  1008. * @brief Prepare the generation of a ACKnowledge or Non ACKnowledge condition after the address receive match code or next received byte.
  1009. * @note Usage in Slave or Master mode.
  1010. * @rmtoll CR1 ACK LL_I2C_AcknowledgeNextData
  1011. * @param I2Cx I2C Instance.
  1012. * @param TypeAcknowledge This parameter can be one of the following values:
  1013. * @arg @ref LL_I2C_ACK
  1014. * @arg @ref LL_I2C_NACK
  1015. * @retval None
  1016. */
  1017. __STATIC_INLINE void LL_I2C_AcknowledgeNextData(I2C_TypeDef *I2Cx, uint32_t TypeAcknowledge)
  1018. {
  1019. MODIFY_REG(I2Cx->CR1, I2C_CR1_ACK, TypeAcknowledge);
  1020. }
  1021. /**
  1022. * @brief Generate a START or RESTART condition
  1023. * @note The START bit can be set even if bus is BUSY or I2C is in slave mode.
  1024. * This action has no effect when RELOAD is set.
  1025. * @rmtoll CR1 START LL_I2C_GenerateStartCondition
  1026. * @param I2Cx I2C Instance.
  1027. * @retval None
  1028. */
  1029. __STATIC_INLINE void LL_I2C_GenerateStartCondition(I2C_TypeDef *I2Cx)
  1030. {
  1031. SET_BIT(I2Cx->CR1, I2C_CR1_START);
  1032. }
  1033. /**
  1034. * @brief Generate a STOP condition after the current byte transfer (master mode).
  1035. * @rmtoll CR1 STOP LL_I2C_GenerateStopCondition
  1036. * @param I2Cx I2C Instance.
  1037. * @retval None
  1038. */
  1039. __STATIC_INLINE void LL_I2C_GenerateStopCondition(I2C_TypeDef *I2Cx)
  1040. {
  1041. SET_BIT(I2Cx->CR1, I2C_CR1_STOP);
  1042. }
  1043. /**
  1044. * @brief Enable bit POS (master/host mode).
  1045. * @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.
  1046. * @rmtoll CR1 POS LL_I2C_EnableBitPOS
  1047. * @param I2Cx I2C Instance.
  1048. * @retval None
  1049. */
  1050. __STATIC_INLINE void LL_I2C_EnableBitPOS(I2C_TypeDef *I2Cx)
  1051. {
  1052. SET_BIT(I2Cx->CR1, I2C_CR1_POS);
  1053. }
  1054. /**
  1055. * @brief Disable bit POS (master/host mode).
  1056. * @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.
  1057. * @rmtoll CR1 POS LL_I2C_DisableBitPOS
  1058. * @param I2Cx I2C Instance.
  1059. * @retval None
  1060. */
  1061. __STATIC_INLINE void LL_I2C_DisableBitPOS(I2C_TypeDef *I2Cx)
  1062. {
  1063. CLEAR_BIT(I2Cx->CR1, I2C_CR1_POS);
  1064. }
  1065. /**
  1066. * @brief Check if bit POS is enabled or disabled.
  1067. * @rmtoll CR1 POS LL_I2C_IsEnabledBitPOS
  1068. * @param I2Cx I2C Instance.
  1069. * @retval State of bit (1 or 0).
  1070. */
  1071. __STATIC_INLINE uint32_t LL_I2C_IsEnabledBitPOS(I2C_TypeDef *I2Cx)
  1072. {
  1073. return (READ_BIT(I2Cx->CR1, I2C_CR1_POS) == (I2C_CR1_POS));
  1074. }
  1075. /**
  1076. * @brief Indicate the value of transfer direction.
  1077. * @note RESET: Bus is in read transfer (peripheral point of view).
  1078. * SET: Bus is in write transfer (peripheral point of view).
  1079. * @rmtoll SR2 TRA LL_I2C_GetTransferDirection
  1080. * @param I2Cx I2C Instance.
  1081. * @retval Returned value can be one of the following values:
  1082. * @arg @ref LL_I2C_DIRECTION_WRITE
  1083. * @arg @ref LL_I2C_DIRECTION_READ
  1084. */
  1085. __STATIC_INLINE uint32_t LL_I2C_GetTransferDirection(I2C_TypeDef *I2Cx)
  1086. {
  1087. return (uint32_t)(READ_BIT(I2Cx->SR2, I2C_SR2_TRA));
  1088. }
  1089. /**
  1090. * @brief Read Receive Data register.
  1091. * @rmtoll DR DR LL_I2C_ReceiveData8
  1092. * @param I2Cx I2C Instance.
  1093. * @retval Value between Min_Data=0x0 and Max_Data=0xFF
  1094. */
  1095. __STATIC_INLINE uint8_t LL_I2C_ReceiveData8(I2C_TypeDef *I2Cx)
  1096. {
  1097. return (uint8_t)(READ_BIT(I2Cx->DR, I2C_DR_DR));
  1098. }
  1099. /**
  1100. * @brief Write in Transmit Data Register .
  1101. * @rmtoll DR DR LL_I2C_TransmitData8
  1102. * @param I2Cx I2C Instance.
  1103. * @param Data Value between Min_Data=0x0 and Max_Data=0xFF
  1104. * @retval None
  1105. */
  1106. __STATIC_INLINE void LL_I2C_TransmitData8(I2C_TypeDef *I2Cx, uint8_t Data)
  1107. {
  1108. MODIFY_REG(I2Cx->DR, I2C_DR_DR, Data);
  1109. }
  1110. /**
  1111. * @}
  1112. */
  1113. #if defined(USE_FULL_LL_DRIVER)
  1114. /** @defgroup I2C_LL_EF_Init Initialization and de-initialization functions
  1115. * @{
  1116. */
  1117. uint32_t LL_I2C_Init(I2C_TypeDef *I2Cx, LL_I2C_InitTypeDef *I2C_InitStruct);
  1118. uint32_t LL_I2C_DeInit(I2C_TypeDef *I2Cx);
  1119. void LL_I2C_StructInit(LL_I2C_InitTypeDef *I2C_InitStruct);
  1120. /**
  1121. * @}
  1122. */
  1123. #endif /* USE_FULL_LL_DRIVER */
  1124. /**
  1125. * @}
  1126. */
  1127. /**
  1128. * @}
  1129. */
  1130. #endif /* I2C1 */
  1131. /**
  1132. * @}
  1133. */
  1134. #ifdef __cplusplus
  1135. }
  1136. #endif
  1137. #endif /* __PY32F002B_LL_I2C_H */
  1138. /************************ (C) COPYRIGHT Puya *****END OF FILE****/