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