stm32f1xx_ll_i2c.h 63 KB

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