You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

stm32l1xx_ll_i2c.h 62KB

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