stm32f0xx_i2c.c 55 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586
  1. /**
  2. ******************************************************************************
  3. * @file stm32f0xx_i2c.c
  4. * @author MCD Application Team
  5. * @version V1.5.0
  6. * @date 05-December-2014
  7. * @brief This file provides firmware functions to manage the following
  8. * functionalities of the Inter-Integrated circuit (I2C):
  9. * + Initialization and Configuration
  10. * + Communications handling
  11. * + SMBUS management
  12. * + I2C registers management
  13. * + Data transfers management
  14. * + DMA transfers management
  15. * + Interrupts and flags management
  16. *
  17. * @verbatim
  18. ============================================================================
  19. ##### How to use this driver #####
  20. ============================================================================
  21. [..]
  22. (#) Enable peripheral clock using RCC_APB1PeriphClockCmd(RCC_APB1Periph_I2Cx, ENABLE)
  23. function for I2C1 or I2C2.
  24. (#) Enable SDA, SCL and SMBA (when used) GPIO clocks using
  25. RCC_AHBPeriphClockCmd() function.
  26. (#) Peripherals alternate function:
  27. (++) Connect the pin to the desired peripherals' Alternate
  28. Function (AF) using GPIO_PinAFConfig() function.
  29. (++) Configure the desired pin in alternate function by:
  30. GPIO_InitStruct->GPIO_Mode = GPIO_Mode_AF
  31. (++) Select the type, OpenDrain and speed via
  32. GPIO_PuPd, GPIO_OType and GPIO_Speed members
  33. (++) Call GPIO_Init() function.
  34. (#) Program the Mode, Timing , Own address, Ack and Acknowledged Address
  35. using the I2C_Init() function.
  36. (#) Optionally you can enable/configure the following parameters without
  37. re-initialization (i.e there is no need to call again I2C_Init() function):
  38. (++) Enable the acknowledge feature using I2C_AcknowledgeConfig() function.
  39. (++) Enable the dual addressing mode using I2C_DualAddressCmd() function.
  40. (++) Enable the general call using the I2C_GeneralCallCmd() function.
  41. (++) Enable the clock stretching using I2C_StretchClockCmd() function.
  42. (++) Enable the PEC Calculation using I2C_CalculatePEC() function.
  43. (++) For SMBus Mode:
  44. (+++) Enable the SMBusAlert pin using I2C_SMBusAlertCmd() function.
  45. (#) Enable the NVIC and the corresponding interrupt using the function
  46. I2C_ITConfig() if you need to use interrupt mode.
  47. (#) When using the DMA mode
  48. (++) Configure the DMA using DMA_Init() function.
  49. (++) Active the needed channel Request using I2C_DMACmd() function.
  50. (#) Enable the I2C using the I2C_Cmd() function.
  51. (#) Enable the DMA using the DMA_Cmd() function when using DMA mode in the
  52. transfers.
  53. [..]
  54. (@) When using I2C in Fast Mode Plus, SCL and SDA pin 20mA current drive capability
  55. must be enabled by setting the driving capability control bit in SYSCFG.
  56. @endverbatim
  57. ******************************************************************************
  58. * @attention
  59. *
  60. * <h2><center>&copy; COPYRIGHT 2014 STMicroelectronics</center></h2>
  61. *
  62. * Licensed under MCD-ST Liberty SW License Agreement V2, (the "License");
  63. * You may not use this file except in compliance with the License.
  64. * You may obtain a copy of the License at:
  65. *
  66. * http://www.st.com/software_license_agreement_liberty_v2
  67. *
  68. * Unless required by applicable law or agreed to in writing, software
  69. * distributed under the License is distributed on an "AS IS" BASIS,
  70. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  71. * See the License for the specific language governing permissions and
  72. * limitations under the License.
  73. *
  74. ******************************************************************************
  75. */
  76. /* Includes ------------------------------------------------------------------*/
  77. #include "stm32f0xx_i2c.h"
  78. #include "stm32f0xx_rcc.h"
  79. /** @addtogroup STM32F0xx_StdPeriph_Driver
  80. * @{
  81. */
  82. /** @defgroup I2C
  83. * @brief I2C driver modules
  84. * @{
  85. */
  86. /* Private typedef -----------------------------------------------------------*/
  87. /* Private define ------------------------------------------------------------*/
  88. #define CR1_CLEAR_MASK ((uint32_t)0x00CFE0FF) /*<! I2C CR1 clear register Mask */
  89. #define CR2_CLEAR_MASK ((uint32_t)0x07FF7FFF) /*<! I2C CR2 clear register Mask */
  90. #define TIMING_CLEAR_MASK ((uint32_t)0xF0FFFFFF) /*<! I2C TIMING clear register Mask */
  91. #define ERROR_IT_MASK ((uint32_t)0x00003F00) /*<! I2C Error interrupt register Mask */
  92. #define TC_IT_MASK ((uint32_t)0x000000C0) /*<! I2C TC interrupt register Mask */
  93. /* Private macro -------------------------------------------------------------*/
  94. /* Private variables ---------------------------------------------------------*/
  95. /* Private function prototypes -----------------------------------------------*/
  96. /* Private functions ---------------------------------------------------------*/
  97. /** @defgroup I2C_Private_Functions
  98. * @{
  99. */
  100. /** @defgroup I2C_Group1 Initialization and Configuration functions
  101. * @brief Initialization and Configuration functions
  102. *
  103. @verbatim
  104. ===============================================================================
  105. ##### Initialization and Configuration functions #####
  106. ===============================================================================
  107. [..] This section provides a set of functions allowing to initialize the I2C Mode,
  108. I2C Timing, I2C filters, I2C Addressing mode, I2C OwnAddress1.
  109. [..] The I2C_Init() function follows the I2C configuration procedures (these procedures
  110. are available in reference manual).
  111. [..] When the Software Reset is performed using I2C_SoftwareResetCmd() function, the internal
  112. states machines are reset and communication control bits, as well as status bits come
  113. back to their reset value.
  114. [..] Before enabling Stop mode using I2C_StopModeCmd() I2C Clock source must be set to
  115. HSI and Digital filters must be disabled.
  116. [..] Before enabling Own Address 2 via I2C_DualAddressCmd() function, OA2 and mask should be
  117. configured using I2C_OwnAddress2Config() function.
  118. [..] I2C_SlaveByteControlCmd() enable Slave byte control that allow user to get control of
  119. each byte in slave mode when NBYTES is set to 0x01.
  120. @endverbatim
  121. * @{
  122. */
  123. /**
  124. * @brief Deinitializes the I2Cx peripheral registers to their default reset values.
  125. * @param I2Cx: where x can be 1 or 2 to select the I2C peripheral.
  126. * @retval None
  127. */
  128. void I2C_DeInit(I2C_TypeDef* I2Cx)
  129. {
  130. /* Check the parameters */
  131. assert_param(IS_I2C_ALL_PERIPH(I2Cx));
  132. if (I2Cx == I2C1)
  133. {
  134. /* Enable I2C1 reset state */
  135. RCC_APB1PeriphResetCmd(RCC_APB1Periph_I2C1, ENABLE);
  136. /* Release I2C1 from reset state */
  137. RCC_APB1PeriphResetCmd(RCC_APB1Periph_I2C1, DISABLE);
  138. }
  139. else
  140. {
  141. /* Enable I2C2 reset state */
  142. RCC_APB1PeriphResetCmd(RCC_APB1Periph_I2C2, ENABLE);
  143. /* Release I2C2 from reset state */
  144. RCC_APB1PeriphResetCmd(RCC_APB1Periph_I2C2, DISABLE);
  145. }
  146. }
  147. /**
  148. * @brief Initializes the I2Cx peripheral according to the specified
  149. * parameters in the I2C_InitStruct.
  150. * @param I2Cx: where x can be 1 or 2 to select the I2C peripheral.
  151. * @param I2C_InitStruct: pointer to a I2C_InitTypeDef structure that
  152. * contains the configuration information for the specified I2C peripheral.
  153. * @retval None
  154. */
  155. void I2C_Init(I2C_TypeDef* I2Cx, I2C_InitTypeDef* I2C_InitStruct)
  156. {
  157. uint32_t tmpreg = 0;
  158. /* Check the parameters */
  159. assert_param(IS_I2C_ALL_PERIPH(I2Cx));
  160. assert_param(IS_I2C_ANALOG_FILTER(I2C_InitStruct->I2C_AnalogFilter));
  161. assert_param(IS_I2C_DIGITAL_FILTER(I2C_InitStruct->I2C_DigitalFilter));
  162. assert_param(IS_I2C_MODE(I2C_InitStruct->I2C_Mode));
  163. assert_param(IS_I2C_OWN_ADDRESS1(I2C_InitStruct->I2C_OwnAddress1));
  164. assert_param(IS_I2C_ACK(I2C_InitStruct->I2C_Ack));
  165. assert_param(IS_I2C_ACKNOWLEDGE_ADDRESS(I2C_InitStruct->I2C_AcknowledgedAddress));
  166. /* Disable I2Cx Peripheral */
  167. I2Cx->CR1 &= (uint32_t)~((uint32_t)I2C_CR1_PE);
  168. /*---------------------------- I2Cx FILTERS Configuration ------------------*/
  169. /* Get the I2Cx CR1 value */
  170. tmpreg = I2Cx->CR1;
  171. /* Clear I2Cx CR1 register */
  172. tmpreg &= CR1_CLEAR_MASK;
  173. /* Configure I2Cx: analog and digital filter */
  174. /* Set ANFOFF bit according to I2C_AnalogFilter value */
  175. /* Set DFN bits according to I2C_DigitalFilter value */
  176. tmpreg |= (uint32_t)I2C_InitStruct->I2C_AnalogFilter |(I2C_InitStruct->I2C_DigitalFilter << 8);
  177. /* Write to I2Cx CR1 */
  178. I2Cx->CR1 = tmpreg;
  179. /*---------------------------- I2Cx TIMING Configuration -------------------*/
  180. /* Configure I2Cx: Timing */
  181. /* Set TIMINGR bits according to I2C_Timing */
  182. /* Write to I2Cx TIMING */
  183. I2Cx->TIMINGR = I2C_InitStruct->I2C_Timing & TIMING_CLEAR_MASK;
  184. /* Enable I2Cx Peripheral */
  185. I2Cx->CR1 |= I2C_CR1_PE;
  186. /*---------------------------- I2Cx OAR1 Configuration ---------------------*/
  187. /* Clear tmpreg local variable */
  188. tmpreg = 0;
  189. /* Clear OAR1 register */
  190. I2Cx->OAR1 = (uint32_t)tmpreg;
  191. /* Clear OAR2 register */
  192. I2Cx->OAR2 = (uint32_t)tmpreg;
  193. /* Configure I2Cx: Own Address1 and acknowledged address */
  194. /* Set OA1MODE bit according to I2C_AcknowledgedAddress value */
  195. /* Set OA1 bits according to I2C_OwnAddress1 value */
  196. tmpreg = (uint32_t)((uint32_t)I2C_InitStruct->I2C_AcknowledgedAddress | \
  197. (uint32_t)I2C_InitStruct->I2C_OwnAddress1);
  198. /* Write to I2Cx OAR1 */
  199. I2Cx->OAR1 = tmpreg;
  200. /* Enable Own Address1 acknowledgement */
  201. I2Cx->OAR1 |= I2C_OAR1_OA1EN;
  202. /*---------------------------- I2Cx MODE Configuration ---------------------*/
  203. /* Configure I2Cx: mode */
  204. /* Set SMBDEN and SMBHEN bits according to I2C_Mode value */
  205. tmpreg = I2C_InitStruct->I2C_Mode;
  206. /* Write to I2Cx CR1 */
  207. I2Cx->CR1 |= tmpreg;
  208. /*---------------------------- I2Cx ACK Configuration ----------------------*/
  209. /* Get the I2Cx CR2 value */
  210. tmpreg = I2Cx->CR2;
  211. /* Clear I2Cx CR2 register */
  212. tmpreg &= CR2_CLEAR_MASK;
  213. /* Configure I2Cx: acknowledgement */
  214. /* Set NACK bit according to I2C_Ack value */
  215. tmpreg |= I2C_InitStruct->I2C_Ack;
  216. /* Write to I2Cx CR2 */
  217. I2Cx->CR2 = tmpreg;
  218. }
  219. /**
  220. * @brief Fills each I2C_InitStruct member with its default value.
  221. * @param I2C_InitStruct: pointer to an I2C_InitTypeDef structure which will be initialized.
  222. * @retval None
  223. */
  224. void I2C_StructInit(I2C_InitTypeDef* I2C_InitStruct)
  225. {
  226. /*---------------- Reset I2C init structure parameters values --------------*/
  227. /* Initialize the I2C_Timing member */
  228. I2C_InitStruct->I2C_Timing = 0;
  229. /* Initialize the I2C_AnalogFilter member */
  230. I2C_InitStruct->I2C_AnalogFilter = I2C_AnalogFilter_Enable;
  231. /* Initialize the I2C_DigitalFilter member */
  232. I2C_InitStruct->I2C_DigitalFilter = 0;
  233. /* Initialize the I2C_Mode member */
  234. I2C_InitStruct->I2C_Mode = I2C_Mode_I2C;
  235. /* Initialize the I2C_OwnAddress1 member */
  236. I2C_InitStruct->I2C_OwnAddress1 = 0;
  237. /* Initialize the I2C_Ack member */
  238. I2C_InitStruct->I2C_Ack = I2C_Ack_Disable;
  239. /* Initialize the I2C_AcknowledgedAddress member */
  240. I2C_InitStruct->I2C_AcknowledgedAddress = I2C_AcknowledgedAddress_7bit;
  241. }
  242. /**
  243. * @brief Enables or disables the specified I2C peripheral.
  244. * @param I2Cx: where x can be 1 or 2 to select the I2C peripheral.
  245. * @param NewState: new state of the I2Cx peripheral.
  246. * This parameter can be: ENABLE or DISABLE.
  247. * @retval None
  248. */
  249. void I2C_Cmd(I2C_TypeDef* I2Cx, FunctionalState NewState)
  250. {
  251. /* Check the parameters */
  252. assert_param(IS_I2C_ALL_PERIPH(I2Cx));
  253. assert_param(IS_FUNCTIONAL_STATE(NewState));
  254. if (NewState != DISABLE)
  255. {
  256. /* Enable the selected I2C peripheral */
  257. I2Cx->CR1 |= I2C_CR1_PE;
  258. }
  259. else
  260. {
  261. /* Disable the selected I2C peripheral */
  262. I2Cx->CR1 &= (uint32_t)~((uint32_t)I2C_CR1_PE);
  263. }
  264. }
  265. /**
  266. * @brief Enables or disables the specified I2C software reset.
  267. * @param I2Cx: where x can be 1 or 2 to select the I2C peripheral.
  268. * @retval None
  269. */
  270. void I2C_SoftwareResetCmd(I2C_TypeDef* I2Cx)
  271. {
  272. /* Check the parameters */
  273. assert_param(IS_I2C_ALL_PERIPH(I2Cx));
  274. /* Disable peripheral */
  275. I2Cx->CR1 &= (uint32_t)~((uint32_t)I2C_CR1_PE);
  276. /* Perform a dummy read to delay the disable of peripheral for minimum
  277. 3 APB clock cycles to perform the software reset functionality */
  278. *(__IO uint32_t *)(uint32_t)I2Cx;
  279. /* Enable peripheral */
  280. I2Cx->CR1 |= I2C_CR1_PE;
  281. }
  282. /**
  283. * @brief Enables or disables the specified I2C interrupts.
  284. * @param I2Cx: where x can be 1 or 2 to select the I2C peripheral.
  285. * @param I2C_IT: specifies the I2C interrupts sources to be enabled or disabled.
  286. * This parameter can be any combination of the following values:
  287. * @arg I2C_IT_ERRI: Error interrupt mask
  288. * @arg I2C_IT_TCI: Transfer Complete interrupt mask
  289. * @arg I2C_IT_STOPI: Stop Detection interrupt mask
  290. * @arg I2C_IT_NACKI: Not Acknowledge received interrupt mask
  291. * @arg I2C_IT_ADDRI: Address Match interrupt mask
  292. * @arg I2C_IT_RXI: RX interrupt mask
  293. * @arg I2C_IT_TXI: TX interrupt mask
  294. * @param NewState: new state of the specified I2C interrupts.
  295. * This parameter can be: ENABLE or DISABLE.
  296. * @retval None
  297. */
  298. void I2C_ITConfig(I2C_TypeDef* I2Cx, uint32_t I2C_IT, FunctionalState NewState)
  299. {
  300. /* Check the parameters */
  301. assert_param(IS_I2C_ALL_PERIPH(I2Cx));
  302. assert_param(IS_FUNCTIONAL_STATE(NewState));
  303. assert_param(IS_I2C_CONFIG_IT(I2C_IT));
  304. if (NewState != DISABLE)
  305. {
  306. /* Enable the selected I2C interrupts */
  307. I2Cx->CR1 |= I2C_IT;
  308. }
  309. else
  310. {
  311. /* Disable the selected I2C interrupts */
  312. I2Cx->CR1 &= (uint32_t)~((uint32_t)I2C_IT);
  313. }
  314. }
  315. /**
  316. * @brief Enables or disables the I2C Clock stretching.
  317. * @param I2Cx: where x can be 1 or 2 to select the I2C peripheral.
  318. * @param NewState: new state of the I2Cx Clock stretching.
  319. * This parameter can be: ENABLE or DISABLE.
  320. * @retval None
  321. */
  322. void I2C_StretchClockCmd(I2C_TypeDef* I2Cx, FunctionalState NewState)
  323. {
  324. /* Check the parameters */
  325. assert_param(IS_I2C_ALL_PERIPH(I2Cx));
  326. assert_param(IS_FUNCTIONAL_STATE(NewState));
  327. if (NewState != DISABLE)
  328. {
  329. /* Enable clock stretching */
  330. I2Cx->CR1 &= (uint32_t)~((uint32_t)I2C_CR1_NOSTRETCH);
  331. }
  332. else
  333. {
  334. /* Disable clock stretching */
  335. I2Cx->CR1 |= I2C_CR1_NOSTRETCH;
  336. }
  337. }
  338. /**
  339. * @brief Enables or disables I2C wakeup from stop mode.
  340. * This function is not applicable for STM32F030 devices.
  341. * @param I2Cx: where x can be 1 to select the I2C peripheral.
  342. * @param NewState: new state of the I2Cx stop mode.
  343. * This parameter can be: ENABLE or DISABLE.
  344. * @retval None
  345. */
  346. void I2C_StopModeCmd(I2C_TypeDef* I2Cx, FunctionalState NewState)
  347. {
  348. /* Check the parameters */
  349. assert_param(IS_I2C_1_PERIPH(I2Cx));
  350. assert_param(IS_FUNCTIONAL_STATE(NewState));
  351. if (NewState != DISABLE)
  352. {
  353. /* Enable wakeup from stop mode */
  354. I2Cx->CR1 |= I2C_CR1_WUPEN;
  355. }
  356. else
  357. {
  358. /* Disable wakeup from stop mode */
  359. I2Cx->CR1 &= (uint32_t)~((uint32_t)I2C_CR1_WUPEN);
  360. }
  361. }
  362. /**
  363. * @brief Enables or disables the I2C own address 2.
  364. * @param I2Cx: where x can be 1 or 2 to select the I2C peripheral.
  365. * @param NewState: new state of the I2C own address 2.
  366. * This parameter can be: ENABLE or DISABLE.
  367. * @retval None
  368. */
  369. void I2C_DualAddressCmd(I2C_TypeDef* I2Cx, FunctionalState NewState)
  370. {
  371. /* Check the parameters */
  372. assert_param(IS_I2C_ALL_PERIPH(I2Cx));
  373. assert_param(IS_FUNCTIONAL_STATE(NewState));
  374. if (NewState != DISABLE)
  375. {
  376. /* Enable own address 2 */
  377. I2Cx->OAR2 |= I2C_OAR2_OA2EN;
  378. }
  379. else
  380. {
  381. /* Disable own address 2 */
  382. I2Cx->OAR2 &= (uint32_t)~((uint32_t)I2C_OAR2_OA2EN);
  383. }
  384. }
  385. /**
  386. * @brief Configures the I2C slave own address 2 and mask.
  387. * @param I2Cx: where x can be 1 or 2 to select the I2C peripheral.
  388. * @param Address: specifies the slave address to be programmed.
  389. * @param Mask: specifies own address 2 mask to be programmed.
  390. * This parameter can be one of the following values:
  391. * @arg I2C_OA2_NoMask: no mask.
  392. * @arg I2C_OA2_Mask01: OA2[1] is masked and don't care.
  393. * @arg I2C_OA2_Mask02: OA2[2:1] are masked and don't care.
  394. * @arg I2C_OA2_Mask03: OA2[3:1] are masked and don't care.
  395. * @arg I2C_OA2_Mask04: OA2[4:1] are masked and don't care.
  396. * @arg I2C_OA2_Mask05: OA2[5:1] are masked and don't care.
  397. * @arg I2C_OA2_Mask06: OA2[6:1] are masked and don't care.
  398. * @arg I2C_OA2_Mask07: OA2[7:1] are masked and don't care.
  399. * @retval None
  400. */
  401. void I2C_OwnAddress2Config(I2C_TypeDef* I2Cx, uint16_t Address, uint8_t Mask)
  402. {
  403. uint32_t tmpreg = 0;
  404. /* Check the parameters */
  405. assert_param(IS_I2C_ALL_PERIPH(I2Cx));
  406. assert_param(IS_I2C_OWN_ADDRESS2(Address));
  407. assert_param(IS_I2C_OWN_ADDRESS2_MASK(Mask));
  408. /* Get the old register value */
  409. tmpreg = I2Cx->OAR2;
  410. /* Reset I2Cx OA2 bit [7:1] and OA2MSK bit [1:0] */
  411. tmpreg &= (uint32_t)~((uint32_t)(I2C_OAR2_OA2 | I2C_OAR2_OA2MSK));
  412. /* Set I2Cx SADD */
  413. tmpreg |= (uint32_t)(((uint32_t)Address & I2C_OAR2_OA2) | \
  414. (((uint32_t)Mask << 8) & I2C_OAR2_OA2MSK)) ;
  415. /* Store the new register value */
  416. I2Cx->OAR2 = tmpreg;
  417. }
  418. /**
  419. * @brief Enables or disables the I2C general call mode.
  420. * @param I2Cx: where x can be 1 or 2 to select the I2C peripheral.
  421. * @param NewState: new state of the I2C general call mode.
  422. * This parameter can be: ENABLE or DISABLE.
  423. * @retval None
  424. */
  425. void I2C_GeneralCallCmd(I2C_TypeDef* I2Cx, FunctionalState NewState)
  426. {
  427. /* Check the parameters */
  428. assert_param(IS_I2C_ALL_PERIPH(I2Cx));
  429. assert_param(IS_FUNCTIONAL_STATE(NewState));
  430. if (NewState != DISABLE)
  431. {
  432. /* Enable general call mode */
  433. I2Cx->CR1 |= I2C_CR1_GCEN;
  434. }
  435. else
  436. {
  437. /* Disable general call mode */
  438. I2Cx->CR1 &= (uint32_t)~((uint32_t)I2C_CR1_GCEN);
  439. }
  440. }
  441. /**
  442. * @brief Enables or disables the I2C slave byte control.
  443. * @param I2Cx: where x can be 1 or 2 to select the I2C peripheral.
  444. * @param NewState: new state of the I2C slave byte control.
  445. * This parameter can be: ENABLE or DISABLE.
  446. * @retval None
  447. */
  448. void I2C_SlaveByteControlCmd(I2C_TypeDef* I2Cx, FunctionalState NewState)
  449. {
  450. /* Check the parameters */
  451. assert_param(IS_I2C_ALL_PERIPH(I2Cx));
  452. assert_param(IS_FUNCTIONAL_STATE(NewState));
  453. if (NewState != DISABLE)
  454. {
  455. /* Enable slave byte control */
  456. I2Cx->CR1 |= I2C_CR1_SBC;
  457. }
  458. else
  459. {
  460. /* Disable slave byte control */
  461. I2Cx->CR1 &= (uint32_t)~((uint32_t)I2C_CR1_SBC);
  462. }
  463. }
  464. /**
  465. * @brief Configures the slave address to be transmitted after start generation.
  466. * @param I2Cx: where x can be 1 or 2 to select the I2C peripheral.
  467. * @param Address: specifies the slave address to be programmed.
  468. * @note This function should be called before generating start condition.
  469. * @retval None
  470. */
  471. void I2C_SlaveAddressConfig(I2C_TypeDef* I2Cx, uint16_t Address)
  472. {
  473. uint32_t tmpreg = 0;
  474. /* Check the parameters */
  475. assert_param(IS_I2C_ALL_PERIPH(I2Cx));
  476. assert_param(IS_I2C_SLAVE_ADDRESS(Address));
  477. /* Get the old register value */
  478. tmpreg = I2Cx->CR2;
  479. /* Reset I2Cx SADD bit [9:0] */
  480. tmpreg &= (uint32_t)~((uint32_t)I2C_CR2_SADD);
  481. /* Set I2Cx SADD */
  482. tmpreg |= (uint32_t)((uint32_t)Address & I2C_CR2_SADD);
  483. /* Store the new register value */
  484. I2Cx->CR2 = tmpreg;
  485. }
  486. /**
  487. * @brief Enables or disables the I2C 10-bit addressing mode for the master.
  488. * @param I2Cx: where x can be 1 or 2 to select the I2C peripheral.
  489. * @param NewState: new state of the I2C 10-bit addressing mode.
  490. * This parameter can be: ENABLE or DISABLE.
  491. * @note This function should be called before generating start condition.
  492. * @retval None
  493. */
  494. void I2C_10BitAddressingModeCmd(I2C_TypeDef* I2Cx, FunctionalState NewState)
  495. {
  496. /* Check the parameters */
  497. assert_param(IS_I2C_ALL_PERIPH(I2Cx));
  498. assert_param(IS_FUNCTIONAL_STATE(NewState));
  499. if (NewState != DISABLE)
  500. {
  501. /* Enable 10-bit addressing mode */
  502. I2Cx->CR2 |= I2C_CR2_ADD10;
  503. }
  504. else
  505. {
  506. /* Disable 10-bit addressing mode */
  507. I2Cx->CR2 &= (uint32_t)~((uint32_t)I2C_CR2_ADD10);
  508. }
  509. }
  510. /**
  511. * @}
  512. */
  513. /** @defgroup I2C_Group2 Communications handling functions
  514. * @brief Communications handling functions
  515. *
  516. @verbatim
  517. ===============================================================================
  518. ##### Communications handling functions #####
  519. ===============================================================================
  520. [..] This section provides a set of functions that handles I2C communication.
  521. [..] Automatic End mode is enabled using I2C_AutoEndCmd() function. When Reload
  522. mode is enabled via I2C_ReloadCmd() AutoEnd bit has no effect.
  523. [..] I2C_NumberOfBytesConfig() function set the number of bytes to be transferred,
  524. this configuration should be done before generating start condition in master
  525. mode.
  526. [..] When switching from master write operation to read operation in 10Bit addressing
  527. mode, master can only sends the 1st 7 bits of the 10 bit address, followed by
  528. Read direction by enabling HEADR bit using I2C_10BitAddressHeader() function.
  529. [..] In master mode, when transferring more than 255 bytes Reload mode should be used
  530. to handle communication. In the first phase of transfer, Nbytes should be set to
  531. 255. After transferring these bytes TCR flag is set and I2C_TransferHandling()
  532. function should be called to handle remaining communication.
  533. [..] In master mode, when software end mode is selected when all data is transferred
  534. TC flag is set I2C_TransferHandling() function should be called to generate STOP
  535. or generate ReStart.
  536. @endverbatim
  537. * @{
  538. */
  539. /**
  540. * @brief Enables or disables the I2C automatic end mode (stop condition is
  541. * automatically sent when nbytes data are transferred).
  542. * @param I2Cx: where x can be 1 or 2 to select the I2C peripheral.
  543. * @param NewState: new state of the I2C automatic end mode.
  544. * This parameter can be: ENABLE or DISABLE.
  545. * @note This function has effect if Reload mode is disabled.
  546. * @retval None
  547. */
  548. void I2C_AutoEndCmd(I2C_TypeDef* I2Cx, FunctionalState NewState)
  549. {
  550. /* Check the parameters */
  551. assert_param(IS_I2C_ALL_PERIPH(I2Cx));
  552. assert_param(IS_FUNCTIONAL_STATE(NewState));
  553. if (NewState != DISABLE)
  554. {
  555. /* Enable Auto end mode */
  556. I2Cx->CR2 |= I2C_CR2_AUTOEND;
  557. }
  558. else
  559. {
  560. /* Disable Auto end mode */
  561. I2Cx->CR2 &= (uint32_t)~((uint32_t)I2C_CR2_AUTOEND);
  562. }
  563. }
  564. /**
  565. * @brief Enables or disables the I2C nbytes reload mode.
  566. * @param I2Cx: where x can be 1 or 2 to select the I2C peripheral.
  567. * @param NewState: new state of the nbytes reload mode.
  568. * This parameter can be: ENABLE or DISABLE.
  569. * @retval None
  570. */
  571. void I2C_ReloadCmd(I2C_TypeDef* I2Cx, FunctionalState NewState)
  572. {
  573. /* Check the parameters */
  574. assert_param(IS_I2C_ALL_PERIPH(I2Cx));
  575. assert_param(IS_FUNCTIONAL_STATE(NewState));
  576. if (NewState != DISABLE)
  577. {
  578. /* Enable Auto Reload mode */
  579. I2Cx->CR2 |= I2C_CR2_RELOAD;
  580. }
  581. else
  582. {
  583. /* Disable Auto Reload mode */
  584. I2Cx->CR2 &= (uint32_t)~((uint32_t)I2C_CR2_RELOAD);
  585. }
  586. }
  587. /**
  588. * @brief Configures the number of bytes to be transmitted/received.
  589. * @param I2Cx: where x can be 1 or 2 to select the I2C peripheral.
  590. * @param Number_Bytes: specifies the number of bytes to be programmed.
  591. * @retval None
  592. */
  593. void I2C_NumberOfBytesConfig(I2C_TypeDef* I2Cx, uint8_t Number_Bytes)
  594. {
  595. uint32_t tmpreg = 0;
  596. /* Check the parameters */
  597. assert_param(IS_I2C_ALL_PERIPH(I2Cx));
  598. /* Get the old register value */
  599. tmpreg = I2Cx->CR2;
  600. /* Reset I2Cx Nbytes bit [7:0] */
  601. tmpreg &= (uint32_t)~((uint32_t)I2C_CR2_NBYTES);
  602. /* Set I2Cx Nbytes */
  603. tmpreg |= (uint32_t)(((uint32_t)Number_Bytes << 16 ) & I2C_CR2_NBYTES);
  604. /* Store the new register value */
  605. I2Cx->CR2 = tmpreg;
  606. }
  607. /**
  608. * @brief Configures the type of transfer request for the master.
  609. * @param I2Cx: where x can be 1 or 2 to select the I2C peripheral.
  610. * @param I2C_Direction: specifies the transfer request direction to be programmed.
  611. * This parameter can be one of the following values:
  612. * @arg I2C_Direction_Transmitter: Master request a write transfer
  613. * @arg I2C_Direction_Receiver: Master request a read transfer
  614. * @retval None
  615. */
  616. void I2C_MasterRequestConfig(I2C_TypeDef* I2Cx, uint16_t I2C_Direction)
  617. {
  618. /* Check the parameters */
  619. assert_param(IS_I2C_ALL_PERIPH(I2Cx));
  620. assert_param(IS_I2C_DIRECTION(I2C_Direction));
  621. /* Test on the direction to set/reset the read/write bit */
  622. if (I2C_Direction == I2C_Direction_Transmitter)
  623. {
  624. /* Request a write Transfer */
  625. I2Cx->CR2 &= (uint32_t)~((uint32_t)I2C_CR2_RD_WRN);
  626. }
  627. else
  628. {
  629. /* Request a read Transfer */
  630. I2Cx->CR2 |= I2C_CR2_RD_WRN;
  631. }
  632. }
  633. /**
  634. * @brief Generates I2Cx communication START condition.
  635. * @param I2Cx: where x can be 1 or 2 to select the I2C peripheral.
  636. * @param NewState: new state of the I2C START condition generation.
  637. * This parameter can be: ENABLE or DISABLE.
  638. * @retval None
  639. */
  640. void I2C_GenerateSTART(I2C_TypeDef* I2Cx, FunctionalState NewState)
  641. {
  642. /* Check the parameters */
  643. assert_param(IS_I2C_ALL_PERIPH(I2Cx));
  644. assert_param(IS_FUNCTIONAL_STATE(NewState));
  645. if (NewState != DISABLE)
  646. {
  647. /* Generate a START condition */
  648. I2Cx->CR2 |= I2C_CR2_START;
  649. }
  650. else
  651. {
  652. /* Disable the START condition generation */
  653. I2Cx->CR2 &= (uint32_t)~((uint32_t)I2C_CR2_START);
  654. }
  655. }
  656. /**
  657. * @brief Generates I2Cx communication STOP condition.
  658. * @param I2Cx: where x can be 1 or 2 to select the I2C peripheral.
  659. * @param NewState: new state of the I2C STOP condition generation.
  660. * This parameter can be: ENABLE or DISABLE.
  661. * @retval None
  662. */
  663. void I2C_GenerateSTOP(I2C_TypeDef* I2Cx, FunctionalState NewState)
  664. {
  665. /* Check the parameters */
  666. assert_param(IS_I2C_ALL_PERIPH(I2Cx));
  667. assert_param(IS_FUNCTIONAL_STATE(NewState));
  668. if (NewState != DISABLE)
  669. {
  670. /* Generate a STOP condition */
  671. I2Cx->CR2 |= I2C_CR2_STOP;
  672. }
  673. else
  674. {
  675. /* Disable the STOP condition generation */
  676. I2Cx->CR2 &= (uint32_t)~((uint32_t)I2C_CR2_STOP);
  677. }
  678. }
  679. /**
  680. * @brief Enables or disables the I2C 10-bit header only mode with read direction.
  681. * @param I2Cx: where x can be 1 or 2 to select the I2C peripheral.
  682. * @param NewState: new state of the I2C 10-bit header only mode.
  683. * This parameter can be: ENABLE or DISABLE.
  684. * @note This mode can be used only when switching from master transmitter mode
  685. * to master receiver mode.
  686. * @retval None
  687. */
  688. void I2C_10BitAddressHeaderCmd(I2C_TypeDef* I2Cx, FunctionalState NewState)
  689. {
  690. /* Check the parameters */
  691. assert_param(IS_I2C_ALL_PERIPH(I2Cx));
  692. assert_param(IS_FUNCTIONAL_STATE(NewState));
  693. if (NewState != DISABLE)
  694. {
  695. /* Enable 10-bit header only mode */
  696. I2Cx->CR2 |= I2C_CR2_HEAD10R;
  697. }
  698. else
  699. {
  700. /* Disable 10-bit header only mode */
  701. I2Cx->CR2 &= (uint32_t)~((uint32_t)I2C_CR2_HEAD10R);
  702. }
  703. }
  704. /**
  705. * @brief Generates I2C communication Acknowledge.
  706. * @param I2Cx: where x can be 1 or 2 to select the I2C peripheral.
  707. * @param NewState: new state of the Acknowledge.
  708. * This parameter can be: ENABLE or DISABLE.
  709. * @retval None
  710. */
  711. void I2C_AcknowledgeConfig(I2C_TypeDef* I2Cx, FunctionalState NewState)
  712. {
  713. /* Check the parameters */
  714. assert_param(IS_I2C_ALL_PERIPH(I2Cx));
  715. assert_param(IS_FUNCTIONAL_STATE(NewState));
  716. if (NewState != DISABLE)
  717. {
  718. /* Enable ACK generation */
  719. I2Cx->CR2 &= (uint32_t)~((uint32_t)I2C_CR2_NACK);
  720. }
  721. else
  722. {
  723. /* Enable NACK generation */
  724. I2Cx->CR2 |= I2C_CR2_NACK;
  725. }
  726. }
  727. /**
  728. * @brief Returns the I2C slave matched address .
  729. * @param I2Cx: where x can be 1 or 2 to select the I2C peripheral.
  730. * @retval The value of the slave matched address .
  731. */
  732. uint8_t I2C_GetAddressMatched(I2C_TypeDef* I2Cx)
  733. {
  734. /* Check the parameters */
  735. assert_param(IS_I2C_ALL_PERIPH(I2Cx));
  736. /* Return the slave matched address in the SR1 register */
  737. return (uint8_t)(((uint32_t)I2Cx->ISR & I2C_ISR_ADDCODE) >> 16) ;
  738. }
  739. /**
  740. * @brief Returns the I2C slave received request.
  741. * @param I2Cx: where x can be 1 or 2 to select the I2C peripheral.
  742. * @retval The value of the received request.
  743. */
  744. uint16_t I2C_GetTransferDirection(I2C_TypeDef* I2Cx)
  745. {
  746. uint32_t tmpreg = 0;
  747. uint16_t direction = 0;
  748. /* Check the parameters */
  749. assert_param(IS_I2C_ALL_PERIPH(I2Cx));
  750. /* Return the slave matched address in the SR1 register */
  751. tmpreg = (uint32_t)(I2Cx->ISR & I2C_ISR_DIR);
  752. /* If write transfer is requested */
  753. if (tmpreg == 0)
  754. {
  755. /* write transfer is requested */
  756. direction = I2C_Direction_Transmitter;
  757. }
  758. else
  759. {
  760. /* Read transfer is requested */
  761. direction = I2C_Direction_Receiver;
  762. }
  763. return direction;
  764. }
  765. /**
  766. * @brief Handles I2Cx communication when starting transfer or during transfer (TC or TCR flag are set).
  767. * @param I2Cx: where x can be 1 or 2 to select the I2C peripheral.
  768. * @param Address: specifies the slave address to be programmed.
  769. * @param Number_Bytes: specifies the number of bytes to be programmed.
  770. * This parameter must be a value between 0 and 255.
  771. * @param ReloadEndMode: new state of the I2C START condition generation.
  772. * This parameter can be one of the following values:
  773. * @arg I2C_Reload_Mode: Enable Reload mode .
  774. * @arg I2C_AutoEnd_Mode: Enable Automatic end mode.
  775. * @arg I2C_SoftEnd_Mode: Enable Software end mode.
  776. * @param StartStopMode: new state of the I2C START condition generation.
  777. * This parameter can be one of the following values:
  778. * @arg I2C_No_StartStop: Don't Generate stop and start condition.
  779. * @arg I2C_Generate_Stop: Generate stop condition (Number_Bytes should be set to 0).
  780. * @arg I2C_Generate_Start_Read: Generate Restart for read request.
  781. * @arg I2C_Generate_Start_Write: Generate Restart for write request.
  782. * @retval None
  783. */
  784. void I2C_TransferHandling(I2C_TypeDef* I2Cx, uint16_t Address, uint8_t Number_Bytes, uint32_t ReloadEndMode, uint32_t StartStopMode)
  785. {
  786. uint32_t tmpreg = 0;
  787. /* Check the parameters */
  788. assert_param(IS_I2C_ALL_PERIPH(I2Cx));
  789. assert_param(IS_I2C_SLAVE_ADDRESS(Address));
  790. assert_param(IS_RELOAD_END_MODE(ReloadEndMode));
  791. assert_param(IS_START_STOP_MODE(StartStopMode));
  792. /* Get the CR2 register value */
  793. tmpreg = I2Cx->CR2;
  794. /* clear tmpreg specific bits */
  795. tmpreg &= (uint32_t)~((uint32_t)(I2C_CR2_SADD | I2C_CR2_NBYTES | I2C_CR2_RELOAD | I2C_CR2_AUTOEND | I2C_CR2_RD_WRN | I2C_CR2_START | I2C_CR2_STOP));
  796. /* update tmpreg */
  797. tmpreg |= (uint32_t)(((uint32_t)Address & I2C_CR2_SADD) | (((uint32_t)Number_Bytes << 16 ) & I2C_CR2_NBYTES) | \
  798. (uint32_t)ReloadEndMode | (uint32_t)StartStopMode);
  799. /* update CR2 register */
  800. I2Cx->CR2 = tmpreg;
  801. }
  802. /**
  803. * @}
  804. */
  805. /** @defgroup I2C_Group3 SMBUS management functions
  806. * @brief SMBUS management functions
  807. *
  808. @verbatim
  809. ===============================================================================
  810. ##### SMBUS management functions #####
  811. ===============================================================================
  812. [..] This section provides a set of functions that handles SMBus communication
  813. and timeouts detection.
  814. [..] The SMBus Device default address (0b1100 001) is enabled by calling I2C_Init()
  815. function and setting I2C_Mode member of I2C_InitTypeDef() structure to
  816. I2C_Mode_SMBusDevice.
  817. [..] The SMBus Host address (0b0001 000) is enabled by calling I2C_Init()
  818. function and setting I2C_Mode member of I2C_InitTypeDef() structure to
  819. I2C_Mode_SMBusHost.
  820. [..] The Alert Response Address (0b0001 100) is enabled using I2C_SMBusAlertCmd()
  821. function.
  822. [..] To detect cumulative SCL stretch in master and slave mode, TIMEOUTB should be
  823. configured (in accordance to SMBus specification) using I2C_TimeoutBConfig()
  824. function then I2C_ExtendedClockTimeoutCmd() function should be called to enable
  825. the detection.
  826. [..] SCL low timeout is detected by configuring TIMEOUTB using I2C_TimeoutBConfig()
  827. function followed by the call of I2C_ClockTimeoutCmd(). When adding to this
  828. procedure the call of I2C_IdleClockTimeoutCmd() function, Bus Idle condition
  829. (both SCL and SDA high) is detected also.
  830. @endverbatim
  831. * @{
  832. */
  833. /**
  834. * @brief Enables or disables I2C SMBus alert.
  835. * @param I2Cx: where x can be 1 to select the I2C peripheral.
  836. * @param NewState: new state of the I2Cx SMBus alert.
  837. * This parameter can be: ENABLE or DISABLE.
  838. * @retval None
  839. */
  840. void I2C_SMBusAlertCmd(I2C_TypeDef* I2Cx, FunctionalState NewState)
  841. {
  842. /* Check the parameters */
  843. assert_param(IS_I2C_1_PERIPH(I2Cx));
  844. assert_param(IS_FUNCTIONAL_STATE(NewState));
  845. if (NewState != DISABLE)
  846. {
  847. /* Enable SMBus alert */
  848. I2Cx->CR1 |= I2C_CR1_ALERTEN;
  849. }
  850. else
  851. {
  852. /* Disable SMBus alert */
  853. I2Cx->CR1 &= (uint32_t)~((uint32_t)I2C_CR1_ALERTEN);
  854. }
  855. }
  856. /**
  857. * @brief Enables or disables I2C Clock Timeout (SCL Timeout detection).
  858. * @param I2Cx: where x can be 1 to select the I2C peripheral.
  859. * @param NewState: new state of the I2Cx clock Timeout.
  860. * This parameter can be: ENABLE or DISABLE.
  861. * @retval None
  862. */
  863. void I2C_ClockTimeoutCmd(I2C_TypeDef* I2Cx, FunctionalState NewState)
  864. {
  865. /* Check the parameters */
  866. assert_param(IS_I2C_1_PERIPH(I2Cx));
  867. assert_param(IS_FUNCTIONAL_STATE(NewState));
  868. if (NewState != DISABLE)
  869. {
  870. /* Enable Clock Timeout */
  871. I2Cx->TIMEOUTR |= I2C_TIMEOUTR_TIMOUTEN;
  872. }
  873. else
  874. {
  875. /* Disable Clock Timeout */
  876. I2Cx->TIMEOUTR &= (uint32_t)~((uint32_t)I2C_TIMEOUTR_TIMOUTEN);
  877. }
  878. }
  879. /**
  880. * @brief Enables or disables I2C Extended Clock Timeout (SCL cumulative Timeout detection).
  881. * @param I2Cx: where x can be 1 to select the I2C peripheral.
  882. * @param NewState: new state of the I2Cx Extended clock Timeout.
  883. * This parameter can be: ENABLE or DISABLE.
  884. * @retval None
  885. */
  886. void I2C_ExtendedClockTimeoutCmd(I2C_TypeDef* I2Cx, FunctionalState NewState)
  887. {
  888. /* Check the parameters */
  889. assert_param(IS_I2C_1_PERIPH(I2Cx));
  890. assert_param(IS_FUNCTIONAL_STATE(NewState));
  891. if (NewState != DISABLE)
  892. {
  893. /* Enable Clock Timeout */
  894. I2Cx->TIMEOUTR |= I2C_TIMEOUTR_TEXTEN;
  895. }
  896. else
  897. {
  898. /* Disable Clock Timeout */
  899. I2Cx->TIMEOUTR &= (uint32_t)~((uint32_t)I2C_TIMEOUTR_TEXTEN);
  900. }
  901. }
  902. /**
  903. * @brief Enables or disables I2C Idle Clock Timeout (Bus idle SCL and SDA
  904. * high detection).
  905. * @param I2Cx: where x can be 1 to select the I2C peripheral.
  906. * @param NewState: new state of the I2Cx Idle clock Timeout.
  907. * This parameter can be: ENABLE or DISABLE.
  908. * @retval None
  909. */
  910. void I2C_IdleClockTimeoutCmd(I2C_TypeDef* I2Cx, FunctionalState NewState)
  911. {
  912. /* Check the parameters */
  913. assert_param(IS_I2C_1_PERIPH(I2Cx));
  914. assert_param(IS_FUNCTIONAL_STATE(NewState));
  915. if (NewState != DISABLE)
  916. {
  917. /* Enable Clock Timeout */
  918. I2Cx->TIMEOUTR |= I2C_TIMEOUTR_TIDLE;
  919. }
  920. else
  921. {
  922. /* Disable Clock Timeout */
  923. I2Cx->TIMEOUTR &= (uint32_t)~((uint32_t)I2C_TIMEOUTR_TIDLE);
  924. }
  925. }
  926. /**
  927. * @brief Configures the I2C Bus Timeout A (SCL Timeout when TIDLE = 0 or Bus
  928. * idle SCL and SDA high when TIDLE = 1).
  929. * @param I2Cx: where x can be 1 to select the I2C peripheral.
  930. * @param Timeout: specifies the TimeoutA to be programmed.
  931. * @retval None
  932. */
  933. void I2C_TimeoutAConfig(I2C_TypeDef* I2Cx, uint16_t Timeout)
  934. {
  935. uint32_t tmpreg = 0;
  936. /* Check the parameters */
  937. assert_param(IS_I2C_1_PERIPH(I2Cx));
  938. assert_param(IS_I2C_TIMEOUT(Timeout));
  939. /* Get the old register value */
  940. tmpreg = I2Cx->TIMEOUTR;
  941. /* Reset I2Cx TIMEOUTA bit [11:0] */
  942. tmpreg &= (uint32_t)~((uint32_t)I2C_TIMEOUTR_TIMEOUTA);
  943. /* Set I2Cx TIMEOUTA */
  944. tmpreg |= (uint32_t)((uint32_t)Timeout & I2C_TIMEOUTR_TIMEOUTA) ;
  945. /* Store the new register value */
  946. I2Cx->TIMEOUTR = tmpreg;
  947. }
  948. /**
  949. * @brief Configures the I2C Bus Timeout B (SCL cumulative Timeout).
  950. * @param I2Cx: where x can be 1 to select the I2C peripheral.
  951. * @param Timeout: specifies the TimeoutB to be programmed.
  952. * @retval None
  953. */
  954. void I2C_TimeoutBConfig(I2C_TypeDef* I2Cx, uint16_t Timeout)
  955. {
  956. uint32_t tmpreg = 0;
  957. /* Check the parameters */
  958. assert_param(IS_I2C_1_PERIPH(I2Cx));
  959. assert_param(IS_I2C_TIMEOUT(Timeout));
  960. /* Get the old register value */
  961. tmpreg = I2Cx->TIMEOUTR;
  962. /* Reset I2Cx TIMEOUTB bit [11:0] */
  963. tmpreg &= (uint32_t)~((uint32_t)I2C_TIMEOUTR_TIMEOUTB);
  964. /* Set I2Cx TIMEOUTB */
  965. tmpreg |= (uint32_t)(((uint32_t)Timeout << 16) & I2C_TIMEOUTR_TIMEOUTB) ;
  966. /* Store the new register value */
  967. I2Cx->TIMEOUTR = tmpreg;
  968. }
  969. /**
  970. * @brief Enables or disables I2C PEC calculation.
  971. * @param I2Cx: where x can be 1 to select the I2C peripheral.
  972. * @param NewState: new state of the I2Cx PEC calculation.
  973. * This parameter can be: ENABLE or DISABLE.
  974. * @retval None
  975. */
  976. void I2C_CalculatePEC(I2C_TypeDef* I2Cx, FunctionalState NewState)
  977. {
  978. /* Check the parameters */
  979. assert_param(IS_I2C_1_PERIPH(I2Cx));
  980. assert_param(IS_FUNCTIONAL_STATE(NewState));
  981. if (NewState != DISABLE)
  982. {
  983. /* Enable PEC calculation */
  984. I2Cx->CR1 |= I2C_CR1_PECEN;
  985. }
  986. else
  987. {
  988. /* Disable PEC calculation */
  989. I2Cx->CR1 &= (uint32_t)~((uint32_t)I2C_CR1_PECEN);
  990. }
  991. }
  992. /**
  993. * @brief Enables or disables I2C PEC transmission/reception request.
  994. * @param I2Cx: where x can be 1 to select the I2C peripheral.
  995. * @param NewState: new state of the I2Cx PEC request.
  996. * This parameter can be: ENABLE or DISABLE.
  997. * @retval None
  998. */
  999. void I2C_PECRequestCmd(I2C_TypeDef* I2Cx, FunctionalState NewState)
  1000. {
  1001. /* Check the parameters */
  1002. assert_param(IS_I2C_1_PERIPH(I2Cx));
  1003. assert_param(IS_FUNCTIONAL_STATE(NewState));
  1004. if (NewState != DISABLE)
  1005. {
  1006. /* Enable PEC transmission/reception request */
  1007. I2Cx->CR2 |= I2C_CR2_PECBYTE;
  1008. }
  1009. else
  1010. {
  1011. /* Disable PEC transmission/reception request */
  1012. I2Cx->CR2 &= (uint32_t)~((uint32_t)I2C_CR2_PECBYTE);
  1013. }
  1014. }
  1015. /**
  1016. * @brief Returns the I2C PEC.
  1017. * @param I2Cx: where x can be 1 to select the I2C peripheral.
  1018. * @retval The value of the PEC .
  1019. */
  1020. uint8_t I2C_GetPEC(I2C_TypeDef* I2Cx)
  1021. {
  1022. /* Check the parameters */
  1023. assert_param(IS_I2C_1_PERIPH(I2Cx));
  1024. /* Return the slave matched address in the SR1 register */
  1025. return (uint8_t)((uint32_t)I2Cx->PECR & I2C_PECR_PEC);
  1026. }
  1027. /**
  1028. * @}
  1029. */
  1030. /** @defgroup I2C_Group4 I2C registers management functions
  1031. * @brief I2C registers management functions
  1032. *
  1033. @verbatim
  1034. ===============================================================================
  1035. ##### I2C registers management functions #####
  1036. ===============================================================================
  1037. [..] This section provides a functions that allow user the management of
  1038. I2C registers.
  1039. @endverbatim
  1040. * @{
  1041. */
  1042. /**
  1043. * @brief Reads the specified I2C register and returns its value.
  1044. * @param I2Cx: where x can be 1 or 2 to select the I2C peripheral.
  1045. * @param I2C_Register: specifies the register to read.
  1046. * This parameter can be one of the following values:
  1047. * @arg I2C_Register_CR1: CR1 register.
  1048. * @arg I2C_Register_CR2: CR2 register.
  1049. * @arg I2C_Register_OAR1: OAR1 register.
  1050. * @arg I2C_Register_OAR2: OAR2 register.
  1051. * @arg I2C_Register_TIMINGR: TIMING register.
  1052. * @arg I2C_Register_TIMEOUTR: TIMEOUTR register.
  1053. * @arg I2C_Register_ISR: ISR register.
  1054. * @arg I2C_Register_ICR: ICR register.
  1055. * @arg I2C_Register_PECR: PECR register.
  1056. * @arg I2C_Register_RXDR: RXDR register.
  1057. * @arg I2C_Register_TXDR: TXDR register.
  1058. * @retval The value of the read register.
  1059. */
  1060. uint32_t I2C_ReadRegister(I2C_TypeDef* I2Cx, uint8_t I2C_Register)
  1061. {
  1062. __IO uint32_t tmp = 0;
  1063. /* Check the parameters */
  1064. assert_param(IS_I2C_ALL_PERIPH(I2Cx));
  1065. assert_param(IS_I2C_REGISTER(I2C_Register));
  1066. tmp = (uint32_t)I2Cx;
  1067. tmp += I2C_Register;
  1068. /* Return the selected register value */
  1069. return (*(__IO uint32_t *) tmp);
  1070. }
  1071. /**
  1072. * @}
  1073. */
  1074. /** @defgroup I2C_Group5 Data transfers management functions
  1075. * @brief Data transfers management functions
  1076. *
  1077. @verbatim
  1078. ===============================================================================
  1079. ##### Data transfers management functions #####
  1080. ===============================================================================
  1081. [..] This subsection provides a set of functions allowing to manage
  1082. the I2C data transfers.
  1083. [..] The read access of the I2C_RXDR register can be done using
  1084. the I2C_ReceiveData() function and returns the received value.
  1085. Whereas a write access to the I2C_TXDR can be done using I2C_SendData()
  1086. function and stores the written data into TXDR.
  1087. @endverbatim
  1088. * @{
  1089. */
  1090. /**
  1091. * @brief Sends a data byte through the I2Cx peripheral.
  1092. * @param I2Cx: where x can be 1 or 2 to select the I2C peripheral.
  1093. * @param Data: Byte to be transmitted..
  1094. * @retval None
  1095. */
  1096. void I2C_SendData(I2C_TypeDef* I2Cx, uint8_t Data)
  1097. {
  1098. /* Check the parameters */
  1099. assert_param(IS_I2C_ALL_PERIPH(I2Cx));
  1100. /* Write in the DR register the data to be sent */
  1101. I2Cx->TXDR = (uint8_t)Data;
  1102. }
  1103. /**
  1104. * @brief Returns the most recent received data by the I2Cx peripheral.
  1105. * @param I2Cx: where x can be 1 or 2 to select the I2C peripheral.
  1106. * @retval The value of the received data.
  1107. */
  1108. uint8_t I2C_ReceiveData(I2C_TypeDef* I2Cx)
  1109. {
  1110. /* Check the parameters */
  1111. assert_param(IS_I2C_ALL_PERIPH(I2Cx));
  1112. /* Return the data in the DR register */
  1113. return (uint8_t)I2Cx->RXDR;
  1114. }
  1115. /**
  1116. * @}
  1117. */
  1118. /** @defgroup I2C_Group6 DMA transfers management functions
  1119. * @brief DMA transfers management functions
  1120. *
  1121. @verbatim
  1122. ===============================================================================
  1123. ##### DMA transfers management functions #####
  1124. ===============================================================================
  1125. [..] This section provides two functions that can be used only in DMA mode.
  1126. [..] In DMA Mode, the I2C communication can be managed by 2 DMA Channel
  1127. requests:
  1128. (#) I2C_DMAReq_Tx: specifies the Tx buffer DMA transfer request.
  1129. (#) I2C_DMAReq_Rx: specifies the Rx buffer DMA transfer request.
  1130. [..] In this Mode it is advised to use the following function:
  1131. (+) I2C_DMACmd(I2C_TypeDef* I2Cx, uint32_t I2C_DMAReq, FunctionalState NewState);
  1132. @endverbatim
  1133. * @{
  1134. */
  1135. /**
  1136. * @brief Enables or disables the I2C DMA interface.
  1137. * @param I2Cx: where x can be 1 or 2 to select the I2C peripheral.
  1138. * @param I2C_DMAReq: specifies the I2C DMA transfer request to be enabled or disabled.
  1139. * This parameter can be any combination of the following values:
  1140. * @arg I2C_DMAReq_Tx: Tx DMA transfer request
  1141. * @arg I2C_DMAReq_Rx: Rx DMA transfer request
  1142. * @param NewState: new state of the selected I2C DMA transfer request.
  1143. * This parameter can be: ENABLE or DISABLE.
  1144. * @retval None
  1145. */
  1146. void I2C_DMACmd(I2C_TypeDef* I2Cx, uint32_t I2C_DMAReq, FunctionalState NewState)
  1147. {
  1148. /* Check the parameters */
  1149. assert_param(IS_I2C_ALL_PERIPH(I2Cx));
  1150. assert_param(IS_FUNCTIONAL_STATE(NewState));
  1151. assert_param(IS_I2C_DMA_REQ(I2C_DMAReq));
  1152. if (NewState != DISABLE)
  1153. {
  1154. /* Enable the selected I2C DMA requests */
  1155. I2Cx->CR1 |= I2C_DMAReq;
  1156. }
  1157. else
  1158. {
  1159. /* Disable the selected I2C DMA requests */
  1160. I2Cx->CR1 &= (uint32_t)~I2C_DMAReq;
  1161. }
  1162. }
  1163. /**
  1164. * @}
  1165. */
  1166. /** @defgroup I2C_Group7 Interrupts and flags management functions
  1167. * @brief Interrupts and flags management functions
  1168. *
  1169. @verbatim
  1170. ===============================================================================
  1171. ##### Interrupts and flags management functions #####
  1172. ===============================================================================
  1173. [..] This section provides functions allowing to configure the I2C Interrupts
  1174. sources and check or clear the flags or pending bits status.
  1175. The user should identify which mode will be used in his application to manage
  1176. the communication: Polling mode, Interrupt mode or DMA mode(refer I2C_Group6).
  1177. *** Polling Mode ***
  1178. ====================
  1179. [..] In Polling Mode, the I2C communication can be managed by 15 flags:
  1180. (#) I2C_FLAG_TXE: to indicate the status of Transmit data register empty flag.
  1181. (#) I2C_FLAG_TXIS: to indicate the status of Transmit interrupt status flag .
  1182. (#) I2C_FLAG_RXNE: to indicate the status of Receive data register not empty flag.
  1183. (#) I2C_FLAG_ADDR: to indicate the status of Address matched flag (slave mode).
  1184. (#) I2C_FLAG_NACKF: to indicate the status of NACK received flag.
  1185. (#) I2C_FLAG_STOPF: to indicate the status of STOP detection flag.
  1186. (#) I2C_FLAG_TC: to indicate the status of Transfer complete flag(master mode).
  1187. (#) I2C_FLAG_TCR: to indicate the status of Transfer complete reload flag.
  1188. (#) I2C_FLAG_BERR: to indicate the status of Bus error flag.
  1189. (#) I2C_FLAG_ARLO: to indicate the status of Arbitration lost flag.
  1190. (#) I2C_FLAG_OVR: to indicate the status of Overrun/Underrun flag.
  1191. (#) I2C_FLAG_PECERR: to indicate the status of PEC error in reception flag.
  1192. (#) I2C_FLAG_TIMEOUT: to indicate the status of Timeout or Tlow detection flag.
  1193. (#) I2C_FLAG_ALERT: to indicate the status of SMBus Alert flag.
  1194. (#) I2C_FLAG_BUSY: to indicate the status of Bus busy flag.
  1195. [..] In this Mode it is advised to use the following functions:
  1196. (+) FlagStatus I2C_GetFlagStatus(I2C_TypeDef* I2Cx, uint32_t I2C_FLAG);
  1197. (+) void I2C_ClearFlag(I2C_TypeDef* I2Cx, uint32_t I2C_FLAG);
  1198. [..]
  1199. (@)Do not use the BUSY flag to handle each data transmission or reception.It is
  1200. better to use the TXIS and RXNE flags instead.
  1201. *** Interrupt Mode ***
  1202. ======================
  1203. [..] In Interrupt Mode, the I2C communication can be managed by 7 interrupt sources
  1204. and 15 pending bits:
  1205. [..] Interrupt Source:
  1206. (#) I2C_IT_ERRI: specifies the interrupt source for the Error interrupt.
  1207. (#) I2C_IT_TCI: specifies the interrupt source for the Transfer Complete interrupt.
  1208. (#) I2C_IT_STOPI: specifies the interrupt source for the Stop Detection interrupt.
  1209. (#) I2C_IT_NACKI: specifies the interrupt source for the Not Acknowledge received interrupt.
  1210. (#) I2C_IT_ADDRI: specifies the interrupt source for the Address Match interrupt.
  1211. (#) I2C_IT_RXI: specifies the interrupt source for the RX interrupt.
  1212. (#) I2C_IT_TXI: specifies the interrupt source for the TX interrupt.
  1213. [..] Pending Bits:
  1214. (#) I2C_IT_TXIS: to indicate the status of Transmit interrupt status flag.
  1215. (#) I2C_IT_RXNE: to indicate the status of Receive data register not empty flag.
  1216. (#) I2C_IT_ADDR: to indicate the status of Address matched flag (slave mode).
  1217. (#) I2C_IT_NACKF: to indicate the status of NACK received flag.
  1218. (#) I2C_IT_STOPF: to indicate the status of STOP detection flag.
  1219. (#) I2C_IT_TC: to indicate the status of Transfer complete flag (master mode).
  1220. (#) I2C_IT_TCR: to indicate the status of Transfer complete reload flag.
  1221. (#) I2C_IT_BERR: to indicate the status of Bus error flag.
  1222. (#) I2C_IT_ARLO: to indicate the status of Arbitration lost flag.
  1223. (#) I2C_IT_OVR: to indicate the status of Overrun/Underrun flag.
  1224. (#) I2C_IT_PECERR: to indicate the status of PEC error in reception flag.
  1225. (#) I2C_IT_TIMEOUT: to indicate the status of Timeout or Tlow detection flag.
  1226. (#) I2C_IT_ALERT: to indicate the status of SMBus Alert flag.
  1227. [..] In this Mode it is advised to use the following functions:
  1228. (+) void I2C_ClearITPendingBit(I2C_TypeDef* I2Cx, uint32_t I2C_IT);
  1229. (+) ITStatus I2C_GetITStatus(I2C_TypeDef* I2Cx, uint32_t I2C_IT);
  1230. @endverbatim
  1231. * @{
  1232. */
  1233. /**
  1234. * @brief Checks whether the specified I2C flag is set or not.
  1235. * @param I2Cx: where x can be 1 or 2 to select the I2C peripheral.
  1236. * @param I2C_FLAG: specifies the flag to check.
  1237. * This parameter can be one of the following values:
  1238. * @arg I2C_FLAG_TXE: Transmit data register empty
  1239. * @arg I2C_FLAG_TXIS: Transmit interrupt status
  1240. * @arg I2C_FLAG_RXNE: Receive data register not empty
  1241. * @arg I2C_FLAG_ADDR: Address matched (slave mode)
  1242. * @arg I2C_FLAG_NACKF: NACK received flag
  1243. * @arg I2C_FLAG_STOPF: STOP detection flag
  1244. * @arg I2C_FLAG_TC: Transfer complete (master mode)
  1245. * @arg I2C_FLAG_TCR: Transfer complete reload
  1246. * @arg I2C_FLAG_BERR: Bus error
  1247. * @arg I2C_FLAG_ARLO: Arbitration lost
  1248. * @arg I2C_FLAG_OVR: Overrun/Underrun
  1249. * @arg I2C_FLAG_PECERR: PEC error in reception
  1250. * @arg I2C_FLAG_TIMEOUT: Timeout or Tlow detection flag
  1251. * @arg I2C_FLAG_ALERT: SMBus Alert
  1252. * @arg I2C_FLAG_BUSY: Bus busy
  1253. * @retval The new state of I2C_FLAG (SET or RESET).
  1254. */
  1255. FlagStatus I2C_GetFlagStatus(I2C_TypeDef* I2Cx, uint32_t I2C_FLAG)
  1256. {
  1257. uint32_t tmpreg = 0;
  1258. FlagStatus bitstatus = RESET;
  1259. /* Check the parameters */
  1260. assert_param(IS_I2C_ALL_PERIPH(I2Cx));
  1261. assert_param(IS_I2C_GET_FLAG(I2C_FLAG));
  1262. /* Get the ISR register value */
  1263. tmpreg = I2Cx->ISR;
  1264. /* Get flag status */
  1265. tmpreg &= I2C_FLAG;
  1266. if(tmpreg != 0)
  1267. {
  1268. /* I2C_FLAG is set */
  1269. bitstatus = SET;
  1270. }
  1271. else
  1272. {
  1273. /* I2C_FLAG is reset */
  1274. bitstatus = RESET;
  1275. }
  1276. return bitstatus;
  1277. }
  1278. /**
  1279. * @brief Clears the I2Cx's pending flags.
  1280. * @param I2Cx: where x can be 1 or 2 to select the I2C peripheral.
  1281. * @param I2C_FLAG: specifies the flag to clear.
  1282. * This parameter can be any combination of the following values:
  1283. * @arg I2C_FLAG_ADDR: Address matched (slave mode)
  1284. * @arg I2C_FLAG_NACKF: NACK received flag
  1285. * @arg I2C_FLAG_STOPF: STOP detection flag
  1286. * @arg I2C_FLAG_BERR: Bus error
  1287. * @arg I2C_FLAG_ARLO: Arbitration lost
  1288. * @arg I2C_FLAG_OVR: Overrun/Underrun
  1289. * @arg I2C_FLAG_PECERR: PEC error in reception
  1290. * @arg I2C_FLAG_TIMEOUT: Timeout or Tlow detection flag
  1291. * @arg I2C_FLAG_ALERT: SMBus Alert
  1292. * @retval The new state of I2C_FLAG (SET or RESET).
  1293. */
  1294. void I2C_ClearFlag(I2C_TypeDef* I2Cx, uint32_t I2C_FLAG)
  1295. {
  1296. /* Check the parameters */
  1297. assert_param(IS_I2C_ALL_PERIPH(I2Cx));
  1298. assert_param(IS_I2C_CLEAR_FLAG(I2C_FLAG));
  1299. /* Clear the selected flag */
  1300. I2Cx->ICR = I2C_FLAG;
  1301. }
  1302. /**
  1303. * @brief Checks whether the specified I2C interrupt has occurred or not.
  1304. * @param I2Cx: where x can be 1 or 2 to select the I2C peripheral.
  1305. * @param I2C_IT: specifies the interrupt source to check.
  1306. * This parameter can be one of the following values:
  1307. * @arg I2C_IT_TXIS: Transmit interrupt status
  1308. * @arg I2C_IT_RXNE: Receive data register not empty
  1309. * @arg I2C_IT_ADDR: Address matched (slave mode)
  1310. * @arg I2C_IT_NACKF: NACK received flag
  1311. * @arg I2C_IT_STOPF: STOP detection flag
  1312. * @arg I2C_IT_TC: Transfer complete (master mode)
  1313. * @arg I2C_IT_TCR: Transfer complete reload
  1314. * @arg I2C_IT_BERR: Bus error
  1315. * @arg I2C_IT_ARLO: Arbitration lost
  1316. * @arg I2C_IT_OVR: Overrun/Underrun
  1317. * @arg I2C_IT_PECERR: PEC error in reception
  1318. * @arg I2C_IT_TIMEOUT: Timeout or Tlow detection flag
  1319. * @arg I2C_IT_ALERT: SMBus Alert
  1320. * @retval The new state of I2C_IT (SET or RESET).
  1321. */
  1322. ITStatus I2C_GetITStatus(I2C_TypeDef* I2Cx, uint32_t I2C_IT)
  1323. {
  1324. uint32_t tmpreg = 0;
  1325. ITStatus bitstatus = RESET;
  1326. uint32_t enablestatus = 0;
  1327. /* Check the parameters */
  1328. assert_param(IS_I2C_ALL_PERIPH(I2Cx));
  1329. assert_param(IS_I2C_GET_IT(I2C_IT));
  1330. /* Check if the interrupt source is enabled or not */
  1331. /* If Error interrupt */
  1332. if ((uint32_t)(I2C_IT & ERROR_IT_MASK))
  1333. {
  1334. enablestatus = (uint32_t)((I2C_CR1_ERRIE) & (I2Cx->CR1));
  1335. }
  1336. /* If TC interrupt */
  1337. else if ((uint32_t)(I2C_IT & TC_IT_MASK))
  1338. {
  1339. enablestatus = (uint32_t)((I2C_CR1_TCIE) & (I2Cx->CR1));
  1340. }
  1341. else
  1342. {
  1343. enablestatus = (uint32_t)((I2C_IT) & (I2Cx->CR1));
  1344. }
  1345. /* Get the ISR register value */
  1346. tmpreg = I2Cx->ISR;
  1347. /* Get flag status */
  1348. tmpreg &= I2C_IT;
  1349. /* Check the status of the specified I2C flag */
  1350. if((tmpreg != RESET) && enablestatus)
  1351. {
  1352. /* I2C_IT is set */
  1353. bitstatus = SET;
  1354. }
  1355. else
  1356. {
  1357. /* I2C_IT is reset */
  1358. bitstatus = RESET;
  1359. }
  1360. /* Return the I2C_IT status */
  1361. return bitstatus;
  1362. }
  1363. /**
  1364. * @brief Clears the I2Cx's interrupt pending bits.
  1365. * @param I2Cx: where x can be 1 or 2 to select the I2C peripheral.
  1366. * @param I2C_IT: specifies the interrupt pending bit to clear.
  1367. * This parameter can be any combination of the following values:
  1368. * @arg I2C_IT_ADDR: Address matched (slave mode)
  1369. * @arg I2C_IT_NACKF: NACK received flag
  1370. * @arg I2C_IT_STOPF: STOP detection flag
  1371. * @arg I2C_IT_BERR: Bus error
  1372. * @arg I2C_IT_ARLO: Arbitration lost
  1373. * @arg I2C_IT_OVR: Overrun/Underrun
  1374. * @arg I2C_IT_PECERR: PEC error in reception
  1375. * @arg I2C_IT_TIMEOUT: Timeout or Tlow detection flag
  1376. * @arg I2C_IT_ALERT: SMBus Alert
  1377. * @retval The new state of I2C_IT (SET or RESET).
  1378. */
  1379. void I2C_ClearITPendingBit(I2C_TypeDef* I2Cx, uint32_t I2C_IT)
  1380. {
  1381. /* Check the parameters */
  1382. assert_param(IS_I2C_ALL_PERIPH(I2Cx));
  1383. assert_param(IS_I2C_CLEAR_IT(I2C_IT));
  1384. /* Clear the selected flag */
  1385. I2Cx->ICR = I2C_IT;
  1386. }
  1387. /**
  1388. * @}
  1389. */
  1390. /**
  1391. * @}
  1392. */
  1393. /**
  1394. * @}
  1395. */
  1396. /**
  1397. * @}
  1398. */
  1399. /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/