]> git.piffa.net Git - arduino/blob - sheets/gyro/GY-52 Three-axis gyroscope sending data /Three-axis gyroscope sending data/GY-52 Test program/STM32-CODE/serial port output MPU-3050/serial/Libraries/STM32F10x_StdPeriph_Driver/src/stm32f10x_i2c.c
first commit
[arduino] / sheets / gyro / GY-52 Three-axis gyroscope sending data / Three-axis gyroscope sending data / GY-52 Test program / STM32-CODE / serial port output MPU-3050 / serial / Libraries / STM32F10x_StdPeriph_Driver / src / stm32f10x_i2c.c
1 /**\r
2   ******************************************************************************\r
3   * @file    stm32f10x_i2c.c\r
4   * @author  MCD Application Team\r
5   * @version V3.1.2\r
6   * @date    09/28/2009\r
7   * @brief   This file provides all the I2C firmware functions.\r
8   ******************************************************************************\r
9   * @copy\r
10   *\r
11   * THE PRESENT FIRMWARE WHICH IS FOR GUIDANCE ONLY AIMS AT PROVIDING CUSTOMERS\r
12   * WITH CODING INFORMATION REGARDING THEIR PRODUCTS IN ORDER FOR THEM TO SAVE\r
13   * TIME. AS A RESULT, STMICROELECTRONICS SHALL NOT BE HELD LIABLE FOR ANY\r
14   * DIRECT, INDIRECT OR CONSEQUENTIAL DAMAGES WITH RESPECT TO ANY CLAIMS ARISING\r
15   * FROM THE CONTENT OF SUCH FIRMWARE AND/OR THE USE MADE BY CUSTOMERS OF THE\r
16   * CODING INFORMATION CONTAINED HEREIN IN CONNECTION WITH THEIR PRODUCTS.\r
17   *\r
18   * <h2><center>&copy; COPYRIGHT 2009 STMicroelectronics</center></h2>\r
19   */ \r
20 \r
21 /* Includes ------------------------------------------------------------------*/\r
22 #include "stm32f10x_i2c.h"\r
23 #include "stm32f10x_rcc.h"\r
24 \r
25 \r
26 /** @addtogroup STM32F10x_StdPeriph_Driver\r
27   * @{\r
28   */\r
29 \r
30 /** @defgroup I2C \r
31   * @brief I2C driver modules\r
32   * @{\r
33   */ \r
34 \r
35 /** @defgroup I2C_Private_TypesDefinitions\r
36   * @{\r
37   */\r
38 \r
39 /**\r
40   * @}\r
41   */\r
42 \r
43 /** @defgroup I2C_Private_Defines\r
44   * @{\r
45   */\r
46 \r
47 /* I2C SPE mask */\r
48 #define CR1_PE_Set              ((uint16_t)0x0001)\r
49 #define CR1_PE_Reset            ((uint16_t)0xFFFE)\r
50 \r
51 /* I2C START mask */\r
52 #define CR1_START_Set           ((uint16_t)0x0100)\r
53 #define CR1_START_Reset         ((uint16_t)0xFEFF)\r
54 \r
55 /* I2C STOP mask */\r
56 #define CR1_STOP_Set            ((uint16_t)0x0200)\r
57 #define CR1_STOP_Reset          ((uint16_t)0xFDFF)\r
58 \r
59 /* I2C ACK mask */\r
60 #define CR1_ACK_Set             ((uint16_t)0x0400)\r
61 #define CR1_ACK_Reset           ((uint16_t)0xFBFF)\r
62 \r
63 /* I2C ENGC mask */\r
64 #define CR1_ENGC_Set            ((uint16_t)0x0040)\r
65 #define CR1_ENGC_Reset          ((uint16_t)0xFFBF)\r
66 \r
67 /* I2C SWRST mask */\r
68 #define CR1_SWRST_Set           ((uint16_t)0x8000)\r
69 #define CR1_SWRST_Reset         ((uint16_t)0x7FFF)\r
70 \r
71 /* I2C PEC mask */\r
72 #define CR1_PEC_Set             ((uint16_t)0x1000)\r
73 #define CR1_PEC_Reset           ((uint16_t)0xEFFF)\r
74 \r
75 /* I2C ENPEC mask */\r
76 #define CR1_ENPEC_Set           ((uint16_t)0x0020)\r
77 #define CR1_ENPEC_Reset         ((uint16_t)0xFFDF)\r
78 \r
79 /* I2C ENARP mask */\r
80 #define CR1_ENARP_Set           ((uint16_t)0x0010)\r
81 #define CR1_ENARP_Reset         ((uint16_t)0xFFEF)\r
82 \r
83 /* I2C NOSTRETCH mask */\r
84 #define CR1_NOSTRETCH_Set       ((uint16_t)0x0080)\r
85 #define CR1_NOSTRETCH_Reset     ((uint16_t)0xFF7F)\r
86 \r
87 /* I2C registers Masks */\r
88 #define CR1_CLEAR_Mask          ((uint16_t)0xFBF5)\r
89 \r
90 /* I2C DMAEN mask */\r
91 #define CR2_DMAEN_Set           ((uint16_t)0x0800)\r
92 #define CR2_DMAEN_Reset         ((uint16_t)0xF7FF)\r
93 \r
94 /* I2C LAST mask */\r
95 #define CR2_LAST_Set            ((uint16_t)0x1000)\r
96 #define CR2_LAST_Reset          ((uint16_t)0xEFFF)\r
97 \r
98 /* I2C FREQ mask */\r
99 #define CR2_FREQ_Reset          ((uint16_t)0xFFC0)\r
100 \r
101 /* I2C ADD0 mask */\r
102 #define OAR1_ADD0_Set           ((uint16_t)0x0001)\r
103 #define OAR1_ADD0_Reset         ((uint16_t)0xFFFE)\r
104 \r
105 /* I2C ENDUAL mask */\r
106 #define OAR2_ENDUAL_Set         ((uint16_t)0x0001)\r
107 #define OAR2_ENDUAL_Reset       ((uint16_t)0xFFFE)\r
108 \r
109 /* I2C ADD2 mask */\r
110 #define OAR2_ADD2_Reset         ((uint16_t)0xFF01)\r
111 \r
112 /* I2C F/S mask */\r
113 #define CCR_FS_Set              ((uint16_t)0x8000)\r
114 \r
115 /* I2C CCR mask */\r
116 #define CCR_CCR_Set             ((uint16_t)0x0FFF)\r
117 \r
118 /* I2C FLAG mask */\r
119 #define FLAG_Mask               ((uint32_t)0x00FFFFFF)\r
120 \r
121 /* I2C Interrupt Enable mask */\r
122 #define ITEN_Mask               ((uint32_t)0x07000000)\r
123 \r
124 /**\r
125   * @}\r
126   */\r
127 \r
128 /** @defgroup I2C_Private_Macros\r
129   * @{\r
130   */\r
131 \r
132 /**\r
133   * @}\r
134   */\r
135 \r
136 /** @defgroup I2C_Private_Variables\r
137   * @{\r
138   */\r
139 \r
140 /**\r
141   * @}\r
142   */\r
143 \r
144 /** @defgroup I2C_Private_FunctionPrototypes\r
145   * @{\r
146   */\r
147 \r
148 /**\r
149   * @}\r
150   */\r
151 \r
152 /** @defgroup I2C_Private_Functions\r
153   * @{\r
154   */\r
155 \r
156 /**\r
157   * @brief  Deinitializes the I2Cx peripheral registers to their default reset values.\r
158   * @param  I2Cx: where x can be 1 or 2 to select the I2C peripheral.\r
159   * @retval None\r
160   */\r
161 void I2C_DeInit(I2C_TypeDef* I2Cx)\r
162 {\r
163   /* Check the parameters */\r
164   assert_param(IS_I2C_ALL_PERIPH(I2Cx));\r
165 \r
166   if (I2Cx == I2C1)\r
167   {\r
168     /* Enable I2C1 reset state */\r
169     RCC_APB1PeriphResetCmd(RCC_APB1Periph_I2C1, ENABLE);\r
170     /* Release I2C1 from reset state */\r
171     RCC_APB1PeriphResetCmd(RCC_APB1Periph_I2C1, DISABLE);\r
172   }\r
173   else\r
174   {\r
175     /* Enable I2C2 reset state */\r
176     RCC_APB1PeriphResetCmd(RCC_APB1Periph_I2C2, ENABLE);\r
177     /* Release I2C2 from reset state */\r
178     RCC_APB1PeriphResetCmd(RCC_APB1Periph_I2C2, DISABLE);\r
179   }\r
180 }\r
181 \r
182 /**\r
183   * @brief  Initializes the I2Cx peripheral according to the specified \r
184   *   parameters in the I2C_InitStruct.\r
185   * @param  I2Cx: where x can be 1 or 2 to select the I2C peripheral.\r
186   * @param  I2C_InitStruct: pointer to a I2C_InitTypeDef structure that\r
187   *   contains the configuration information for the specified I2C peripheral.\r
188   * @retval None\r
189   */\r
190 void I2C_Init(I2C_TypeDef* I2Cx, I2C_InitTypeDef* I2C_InitStruct)\r
191 {\r
192   uint16_t tmpreg = 0, freqrange = 0;\r
193   uint16_t result = 0x04;\r
194   uint32_t pclk1 = 8000000;\r
195   RCC_ClocksTypeDef  rcc_clocks;\r
196   /* Check the parameters */\r
197   assert_param(IS_I2C_ALL_PERIPH(I2Cx));\r
198   assert_param(IS_I2C_CLOCK_SPEED(I2C_InitStruct->I2C_ClockSpeed));\r
199   assert_param(IS_I2C_MODE(I2C_InitStruct->I2C_Mode));\r
200   assert_param(IS_I2C_DUTY_CYCLE(I2C_InitStruct->I2C_DutyCycle));\r
201   assert_param(IS_I2C_OWN_ADDRESS1(I2C_InitStruct->I2C_OwnAddress1));\r
202   assert_param(IS_I2C_ACK_STATE(I2C_InitStruct->I2C_Ack));\r
203   assert_param(IS_I2C_ACKNOWLEDGE_ADDRESS(I2C_InitStruct->I2C_AcknowledgedAddress));\r
204 \r
205 /*---------------------------- I2Cx CR2 Configuration ------------------------*/\r
206   /* Get the I2Cx CR2 value */\r
207   tmpreg = I2Cx->CR2;\r
208   /* Clear frequency FREQ[5:0] bits */\r
209   tmpreg &= CR2_FREQ_Reset;\r
210   /* Get pclk1 frequency value */\r
211   RCC_GetClocksFreq(&rcc_clocks);\r
212   pclk1 = rcc_clocks.PCLK1_Frequency;\r
213   /* Set frequency bits depending on pclk1 value */\r
214   freqrange = (uint16_t)(pclk1 / 1000000);\r
215   tmpreg |= freqrange;\r
216   /* Write to I2Cx CR2 */\r
217   I2Cx->CR2 = tmpreg;\r
218 \r
219 /*---------------------------- I2Cx CCR Configuration ------------------------*/\r
220   /* Disable the selected I2C peripheral to configure TRISE */\r
221   I2Cx->CR1 &= CR1_PE_Reset;\r
222   /* Reset tmpreg value */\r
223   /* Clear F/S, DUTY and CCR[11:0] bits */\r
224   tmpreg = 0;\r
225 \r
226   /* Configure speed in standard mode */\r
227   if (I2C_InitStruct->I2C_ClockSpeed <= 100000)\r
228   {\r
229     /* Standard mode speed calculate */\r
230     result = (uint16_t)(pclk1 / (I2C_InitStruct->I2C_ClockSpeed << 1));\r
231     /* Test if CCR value is under 0x4*/\r
232     if (result < 0x04)\r
233     {\r
234       /* Set minimum allowed value */\r
235       result = 0x04;  \r
236     }\r
237     /* Set speed value for standard mode */\r
238     tmpreg |= result;     \r
239     /* Set Maximum Rise Time for standard mode */\r
240     I2Cx->TRISE = freqrange + 1; \r
241   }\r
242   /* Configure speed in fast mode */\r
243   else /*(I2C_InitStruct->I2C_ClockSpeed <= 400000)*/\r
244   {\r
245     if (I2C_InitStruct->I2C_DutyCycle == I2C_DutyCycle_2)\r
246     {\r
247       /* Fast mode speed calculate: Tlow/Thigh = 2 */\r
248       result = (uint16_t)(pclk1 / (I2C_InitStruct->I2C_ClockSpeed * 3));\r
249     }\r
250     else /*I2C_InitStruct->I2C_DutyCycle == I2C_DutyCycle_16_9*/\r
251     {\r
252       /* Fast mode speed calculate: Tlow/Thigh = 16/9 */\r
253       result = (uint16_t)(pclk1 / (I2C_InitStruct->I2C_ClockSpeed * 25));\r
254       /* Set DUTY bit */\r
255       result |= I2C_DutyCycle_16_9;\r
256     }\r
257 \r
258     /* Test if CCR value is under 0x1*/\r
259     if ((result & CCR_CCR_Set) == 0)\r
260     {\r
261       /* Set minimum allowed value */\r
262       result |= (uint16_t)0x0001;  \r
263     }\r
264     /* Set speed value and set F/S bit for fast mode */\r
265     tmpreg |= (uint16_t)(result | CCR_FS_Set);\r
266     /* Set Maximum Rise Time for fast mode */\r
267     I2Cx->TRISE = (uint16_t)(((freqrange * (uint16_t)300) / (uint16_t)1000) + (uint16_t)1);  \r
268   }\r
269 \r
270   /* Write to I2Cx CCR */\r
271   I2Cx->CCR = tmpreg;\r
272   /* Enable the selected I2C peripheral */\r
273   I2Cx->CR1 |= CR1_PE_Set;\r
274 \r
275 /*---------------------------- I2Cx CR1 Configuration ------------------------*/\r
276   /* Get the I2Cx CR1 value */\r
277   tmpreg = I2Cx->CR1;\r
278   /* Clear ACK, SMBTYPE and  SMBUS bits */\r
279   tmpreg &= CR1_CLEAR_Mask;\r
280   /* Configure I2Cx: mode and acknowledgement */\r
281   /* Set SMBTYPE and SMBUS bits according to I2C_Mode value */\r
282   /* Set ACK bit according to I2C_Ack value */\r
283   tmpreg |= (uint16_t)((uint32_t)I2C_InitStruct->I2C_Mode | I2C_InitStruct->I2C_Ack);\r
284   /* Write to I2Cx CR1 */\r
285   I2Cx->CR1 = tmpreg;\r
286 \r
287 /*---------------------------- I2Cx OAR1 Configuration -----------------------*/\r
288   /* Set I2Cx Own Address1 and acknowledged address */\r
289   I2Cx->OAR1 = (I2C_InitStruct->I2C_AcknowledgedAddress | I2C_InitStruct->I2C_OwnAddress1);\r
290 }\r
291 \r
292 /**\r
293   * @brief  Fills each I2C_InitStruct member with its default value.\r
294   * @param  I2C_InitStruct: pointer to an I2C_InitTypeDef structure which will be initialized.\r
295   * @retval None\r
296   */\r
297 void I2C_StructInit(I2C_InitTypeDef* I2C_InitStruct)\r
298 {\r
299 /*---------------- Reset I2C init structure parameters values ----------------*/\r
300   /* initialize the I2C_ClockSpeed member */\r
301   I2C_InitStruct->I2C_ClockSpeed = 5000;\r
302   /* Initialize the I2C_Mode member */\r
303   I2C_InitStruct->I2C_Mode = I2C_Mode_I2C;\r
304   /* Initialize the I2C_DutyCycle member */\r
305   I2C_InitStruct->I2C_DutyCycle = I2C_DutyCycle_2;\r
306   /* Initialize the I2C_OwnAddress1 member */\r
307   I2C_InitStruct->I2C_OwnAddress1 = 0;\r
308   /* Initialize the I2C_Ack member */\r
309   I2C_InitStruct->I2C_Ack = I2C_Ack_Disable;\r
310   /* Initialize the I2C_AcknowledgedAddress member */\r
311   I2C_InitStruct->I2C_AcknowledgedAddress = I2C_AcknowledgedAddress_7bit;\r
312 }\r
313 \r
314 /**\r
315   * @brief  Enables or disables the specified I2C peripheral.\r
316   * @param  I2Cx: where x can be 1 or 2 to select the I2C peripheral.\r
317   * @param  NewState: new state of the I2Cx peripheral. \r
318   *   This parameter can be: ENABLE or DISABLE.\r
319   * @retval None\r
320   */\r
321 void I2C_Cmd(I2C_TypeDef* I2Cx, FunctionalState NewState)\r
322 {\r
323   /* Check the parameters */\r
324   assert_param(IS_I2C_ALL_PERIPH(I2Cx));\r
325   assert_param(IS_FUNCTIONAL_STATE(NewState));\r
326   if (NewState != DISABLE)\r
327   {\r
328     /* Enable the selected I2C peripheral */\r
329     I2Cx->CR1 |= CR1_PE_Set;\r
330   }\r
331   else\r
332   {\r
333     /* Disable the selected I2C peripheral */\r
334     I2Cx->CR1 &= CR1_PE_Reset;\r
335   }\r
336 }\r
337 \r
338 /**\r
339   * @brief  Enables or disables the specified I2C DMA requests.\r
340   * @param  I2Cx: where x can be 1 or 2 to select the I2C peripheral.\r
341   * @param  NewState: new state of the I2C DMA transfer.\r
342   *   This parameter can be: ENABLE or DISABLE.\r
343   * @retval None\r
344   */\r
345 void I2C_DMACmd(I2C_TypeDef* I2Cx, FunctionalState NewState)\r
346 {\r
347   /* Check the parameters */\r
348   assert_param(IS_I2C_ALL_PERIPH(I2Cx));\r
349   assert_param(IS_FUNCTIONAL_STATE(NewState));\r
350   if (NewState != DISABLE)\r
351   {\r
352     /* Enable the selected I2C DMA requests */\r
353     I2Cx->CR2 |= CR2_DMAEN_Set;\r
354   }\r
355   else\r
356   {\r
357     /* Disable the selected I2C DMA requests */\r
358     I2Cx->CR2 &= CR2_DMAEN_Reset;\r
359   }\r
360 }\r
361 \r
362 /**\r
363   * @brief  Specifies that the next DMA transfer is the last one.\r
364   * @param  I2Cx: where x can be 1 or 2 to select the I2C peripheral.\r
365   * @param  NewState: new state of the I2C DMA last transfer.\r
366   *   This parameter can be: ENABLE or DISABLE.\r
367   * @retval None\r
368   */\r
369 void I2C_DMALastTransferCmd(I2C_TypeDef* I2Cx, FunctionalState NewState)\r
370 {\r
371   /* Check the parameters */\r
372   assert_param(IS_I2C_ALL_PERIPH(I2Cx));\r
373   assert_param(IS_FUNCTIONAL_STATE(NewState));\r
374   if (NewState != DISABLE)\r
375   {\r
376     /* Next DMA transfer is the last transfer */\r
377     I2Cx->CR2 |= CR2_LAST_Set;\r
378   }\r
379   else\r
380   {\r
381     /* Next DMA transfer is not the last transfer */\r
382     I2Cx->CR2 &= CR2_LAST_Reset;\r
383   }\r
384 }\r
385 \r
386 /**\r
387   * @brief  Generates I2Cx communication START condition.\r
388   * @param  I2Cx: where x can be 1 or 2 to select the I2C peripheral.\r
389   * @param  NewState: new state of the I2C START condition generation.\r
390   *   This parameter can be: ENABLE or DISABLE.\r
391   * @retval None.\r
392   */\r
393 void I2C_GenerateSTART(I2C_TypeDef* I2Cx, FunctionalState NewState)\r
394 {\r
395   /* Check the parameters */\r
396   assert_param(IS_I2C_ALL_PERIPH(I2Cx));\r
397   assert_param(IS_FUNCTIONAL_STATE(NewState));\r
398   if (NewState != DISABLE)\r
399   {\r
400     /* Generate a START condition */\r
401     I2Cx->CR1 |= CR1_START_Set;\r
402   }\r
403   else\r
404   {\r
405     /* Disable the START condition generation */\r
406     I2Cx->CR1 &= CR1_START_Reset;\r
407   }\r
408 }\r
409 \r
410 /**\r
411   * @brief  Generates I2Cx communication STOP condition.\r
412   * @param  I2Cx: where x can be 1 or 2 to select the I2C peripheral.\r
413   * @param  NewState: new state of the I2C STOP condition generation.\r
414   *   This parameter can be: ENABLE or DISABLE.\r
415   * @retval None.\r
416   */\r
417 void I2C_GenerateSTOP(I2C_TypeDef* I2Cx, FunctionalState NewState)\r
418 {\r
419   /* Check the parameters */\r
420   assert_param(IS_I2C_ALL_PERIPH(I2Cx));\r
421   assert_param(IS_FUNCTIONAL_STATE(NewState));\r
422   if (NewState != DISABLE)\r
423   {\r
424     /* Generate a STOP condition */\r
425     I2Cx->CR1 |= CR1_STOP_Set;\r
426   }\r
427   else\r
428   {\r
429     /* Disable the STOP condition generation */\r
430     I2Cx->CR1 &= CR1_STOP_Reset;\r
431   }\r
432 }\r
433 \r
434 /**\r
435   * @brief  Enables or disables the specified I2C acknowledge feature.\r
436   * @param  I2Cx: where x can be 1 or 2 to select the I2C peripheral.\r
437   * @param  NewState: new state of the I2C Acknowledgement.\r
438   *   This parameter can be: ENABLE or DISABLE.\r
439   * @retval None.\r
440   */\r
441 void I2C_AcknowledgeConfig(I2C_TypeDef* I2Cx, FunctionalState NewState)\r
442 {\r
443   /* Check the parameters */\r
444   assert_param(IS_I2C_ALL_PERIPH(I2Cx));\r
445   assert_param(IS_FUNCTIONAL_STATE(NewState));\r
446   if (NewState != DISABLE)\r
447   {\r
448     /* Enable the acknowledgement */\r
449     I2Cx->CR1 |= CR1_ACK_Set;\r
450   }\r
451   else\r
452   {\r
453     /* Disable the acknowledgement */\r
454     I2Cx->CR1 &= CR1_ACK_Reset;\r
455   }\r
456 }\r
457 \r
458 /**\r
459   * @brief  Configures the specified I2C own address2.\r
460   * @param  I2Cx: where x can be 1 or 2 to select the I2C peripheral.\r
461   * @param  Address: specifies the 7bit I2C own address2.\r
462   * @retval None.\r
463   */\r
464 void I2C_OwnAddress2Config(I2C_TypeDef* I2Cx, uint8_t Address)\r
465 {\r
466   uint16_t tmpreg = 0;\r
467 \r
468   /* Check the parameters */\r
469   assert_param(IS_I2C_ALL_PERIPH(I2Cx));\r
470 \r
471   /* Get the old register value */\r
472   tmpreg = I2Cx->OAR2;\r
473 \r
474   /* Reset I2Cx Own address2 bit [7:1] */\r
475   tmpreg &= OAR2_ADD2_Reset;\r
476 \r
477   /* Set I2Cx Own address2 */\r
478   tmpreg |= (uint16_t)((uint16_t)Address & (uint16_t)0x00FE);\r
479 \r
480   /* Store the new register value */\r
481   I2Cx->OAR2 = tmpreg;\r
482 }\r
483 \r
484 /**\r
485   * @brief  Enables or disables the specified I2C dual addressing mode.\r
486   * @param  I2Cx: where x can be 1 or 2 to select the I2C peripheral.\r
487   * @param  NewState: new state of the I2C dual addressing mode.\r
488   *   This parameter can be: ENABLE or DISABLE.\r
489   * @retval None\r
490   */\r
491 void I2C_DualAddressCmd(I2C_TypeDef* I2Cx, FunctionalState NewState)\r
492 {\r
493   /* Check the parameters */\r
494   assert_param(IS_I2C_ALL_PERIPH(I2Cx));\r
495   assert_param(IS_FUNCTIONAL_STATE(NewState));\r
496   if (NewState != DISABLE)\r
497   {\r
498     /* Enable dual addressing mode */\r
499     I2Cx->OAR2 |= OAR2_ENDUAL_Set;\r
500   }\r
501   else\r
502   {\r
503     /* Disable dual addressing mode */\r
504     I2Cx->OAR2 &= OAR2_ENDUAL_Reset;\r
505   }\r
506 }\r
507 \r
508 /**\r
509   * @brief  Enables or disables the specified I2C general call feature.\r
510   * @param  I2Cx: where x can be 1 or 2 to select the I2C peripheral.\r
511   * @param  NewState: new state of the I2C General call.\r
512   *   This parameter can be: ENABLE or DISABLE.\r
513   * @retval None\r
514   */\r
515 void I2C_GeneralCallCmd(I2C_TypeDef* I2Cx, FunctionalState NewState)\r
516 {\r
517   /* Check the parameters */\r
518   assert_param(IS_I2C_ALL_PERIPH(I2Cx));\r
519   assert_param(IS_FUNCTIONAL_STATE(NewState));\r
520   if (NewState != DISABLE)\r
521   {\r
522     /* Enable generall call */\r
523     I2Cx->CR1 |= CR1_ENGC_Set;\r
524   }\r
525   else\r
526   {\r
527     /* Disable generall call */\r
528     I2Cx->CR1 &= CR1_ENGC_Reset;\r
529   }\r
530 }\r
531 \r
532 /**\r
533   * @brief  Enables or disables the specified I2C interrupts.\r
534   * @param  I2Cx: where x can be 1 or 2 to select the I2C peripheral.\r
535   * @param  I2C_IT: specifies the I2C interrupts sources to be enabled or disabled. \r
536   *   This parameter can be any combination of the following values:\r
537   *     @arg I2C_IT_BUF: Buffer interrupt mask\r
538   *     @arg I2C_IT_EVT: Event interrupt mask\r
539   *     @arg I2C_IT_ERR: Error interrupt mask\r
540   * @param  NewState: new state of the specified I2C interrupts.\r
541   *   This parameter can be: ENABLE or DISABLE.\r
542   * @retval None\r
543   */\r
544 void I2C_ITConfig(I2C_TypeDef* I2Cx, uint16_t I2C_IT, FunctionalState NewState)\r
545 {\r
546   /* Check the parameters */\r
547   assert_param(IS_I2C_ALL_PERIPH(I2Cx));\r
548   assert_param(IS_FUNCTIONAL_STATE(NewState));\r
549   assert_param(IS_I2C_CONFIG_IT(I2C_IT));\r
550   \r
551   if (NewState != DISABLE)\r
552   {\r
553     /* Enable the selected I2C interrupts */\r
554     I2Cx->CR2 |= I2C_IT;\r
555   }\r
556   else\r
557   {\r
558     /* Disable the selected I2C interrupts */\r
559     I2Cx->CR2 &= (uint16_t)~I2C_IT;\r
560   }\r
561 }\r
562 \r
563 /**\r
564   * @brief  Sends a data byte through the I2Cx peripheral.\r
565   * @param  I2Cx: where x can be 1 or 2 to select the I2C peripheral.\r
566   * @param  Data: Byte to be transmitted..\r
567   * @retval None\r
568   */\r
569 void I2C_SendData(I2C_TypeDef* I2Cx, uint8_t Data)\r
570 {\r
571   /* Check the parameters */\r
572   assert_param(IS_I2C_ALL_PERIPH(I2Cx));\r
573   /* Write in the DR register the data to be sent */\r
574   I2Cx->DR = Data;\r
575 }\r
576 \r
577 /**\r
578   * @brief  Returns the most recent received data by the I2Cx peripheral.\r
579   * @param  I2Cx: where x can be 1 or 2 to select the I2C peripheral.\r
580   * @retval The value of the received data.\r
581   */\r
582 uint8_t I2C_ReceiveData(I2C_TypeDef* I2Cx)\r
583 {\r
584   /* Check the parameters */\r
585   assert_param(IS_I2C_ALL_PERIPH(I2Cx));\r
586   /* Return the data in the DR register */\r
587   return (uint8_t)I2Cx->DR;\r
588 }\r
589 \r
590 /**\r
591   * @brief  Transmits the address byte to select the slave device.\r
592   * @param  I2Cx: where x can be 1 or 2 to select the I2C peripheral.\r
593   * @param  Address: specifies the slave address which will be transmitted\r
594   * @param  I2C_Direction: specifies whether the I2C device will be a\r
595   *   Transmitter or a Receiver. This parameter can be one of the following values\r
596   *     @arg I2C_Direction_Transmitter: Transmitter mode\r
597   *     @arg I2C_Direction_Receiver: Receiver mode\r
598   * @retval None.\r
599   */\r
600 void I2C_Send7bitAddress(I2C_TypeDef* I2Cx, uint8_t Address, uint8_t I2C_Direction)\r
601 {\r
602   /* Check the parameters */\r
603   assert_param(IS_I2C_ALL_PERIPH(I2Cx));\r
604   assert_param(IS_I2C_DIRECTION(I2C_Direction));\r
605   /* Test on the direction to set/reset the read/write bit */\r
606   if (I2C_Direction != I2C_Direction_Transmitter)\r
607   {\r
608     /* Set the address bit0 for read */\r
609     Address |= OAR1_ADD0_Set;\r
610   }\r
611   else\r
612   {\r
613     /* Reset the address bit0 for write */\r
614     Address &= OAR1_ADD0_Reset;\r
615   }\r
616   /* Send the address */\r
617   I2Cx->DR = Address;\r
618 }\r
619 \r
620 /**\r
621   * @brief  Reads the specified I2C register and returns its value.\r
622   * @param  I2C_Register: specifies the register to read.\r
623   *   This parameter can be one of the following values:\r
624   *     @arg I2C_Register_CR1:  CR1 register.\r
625   *     @arg I2C_Register_CR2:   CR2 register.\r
626   *     @arg I2C_Register_OAR1:  OAR1 register.\r
627   *     @arg I2C_Register_OAR2:  OAR2 register.\r
628   *     @arg I2C_Register_DR:    DR register.\r
629   *     @arg I2C_Register_SR1:   SR1 register.\r
630   *     @arg I2C_Register_SR2:   SR2 register.\r
631   *     @arg I2C_Register_CCR:   CCR register.\r
632   *     @arg I2C_Register_TRISE: TRISE register.\r
633   * @retval The value of the read register.\r
634   */\r
635 uint16_t I2C_ReadRegister(I2C_TypeDef* I2Cx, uint8_t I2C_Register)\r
636 {\r
637   __IO uint32_t tmp = 0;\r
638 \r
639   /* Check the parameters */\r
640   assert_param(IS_I2C_ALL_PERIPH(I2Cx));\r
641   assert_param(IS_I2C_REGISTER(I2C_Register));\r
642 \r
643   tmp = (uint32_t) I2Cx;\r
644   tmp += I2C_Register;\r
645 \r
646   /* Return the selected register value */\r
647   return (*(__IO uint16_t *) tmp);\r
648 }\r
649 \r
650 /**\r
651   * @brief  Enables or disables the specified I2C software reset.\r
652   * @param  I2Cx: where x can be 1 or 2 to select the I2C peripheral.\r
653   * @param  NewState: new state of the I2C software reset.\r
654   *   This parameter can be: ENABLE or DISABLE.\r
655   * @retval None\r
656   */\r
657 void I2C_SoftwareResetCmd(I2C_TypeDef* I2Cx, FunctionalState NewState)\r
658 {\r
659   /* Check the parameters */\r
660   assert_param(IS_I2C_ALL_PERIPH(I2Cx));\r
661   assert_param(IS_FUNCTIONAL_STATE(NewState));\r
662   if (NewState != DISABLE)\r
663   {\r
664     /* Peripheral under reset */\r
665     I2Cx->CR1 |= CR1_SWRST_Set;\r
666   }\r
667   else\r
668   {\r
669     /* Peripheral not under reset */\r
670     I2Cx->CR1 &= CR1_SWRST_Reset;\r
671   }\r
672 }\r
673 \r
674 /**\r
675   * @brief  Drives the SMBusAlert pin high or low for the specified I2C.\r
676   * @param  I2Cx: where x can be 1 or 2 to select the I2C peripheral.\r
677   * @param  I2C_SMBusAlert: specifies SMBAlert pin level. \r
678   *   This parameter can be one of the following values:\r
679   *     @arg I2C_SMBusAlert_Low: SMBAlert pin driven low\r
680   *     @arg I2C_SMBusAlert_High: SMBAlert pin driven high\r
681   * @retval None\r
682   */\r
683 void I2C_SMBusAlertConfig(I2C_TypeDef* I2Cx, uint16_t I2C_SMBusAlert)\r
684 {\r
685   /* Check the parameters */\r
686   assert_param(IS_I2C_ALL_PERIPH(I2Cx));\r
687   assert_param(IS_I2C_SMBUS_ALERT(I2C_SMBusAlert));\r
688   if (I2C_SMBusAlert == I2C_SMBusAlert_Low)\r
689   {\r
690     /* Drive the SMBusAlert pin Low */\r
691     I2Cx->CR1 |= I2C_SMBusAlert_Low;\r
692   }\r
693   else\r
694   {\r
695     /* Drive the SMBusAlert pin High  */\r
696     I2Cx->CR1 &= I2C_SMBusAlert_High;\r
697   }\r
698 }\r
699 \r
700 /**\r
701   * @brief  Enables or disables the specified I2C PEC transfer.\r
702   * @param  I2Cx: where x can be 1 or 2 to select the I2C peripheral.\r
703   * @param  NewState: new state of the I2C PEC transmission.\r
704   *   This parameter can be: ENABLE or DISABLE.\r
705   * @retval None\r
706   */\r
707 void I2C_TransmitPEC(I2C_TypeDef* I2Cx, FunctionalState NewState)\r
708 {\r
709   /* Check the parameters */\r
710   assert_param(IS_I2C_ALL_PERIPH(I2Cx));\r
711   assert_param(IS_FUNCTIONAL_STATE(NewState));\r
712   if (NewState != DISABLE)\r
713   {\r
714     /* Enable the selected I2C PEC transmission */\r
715     I2Cx->CR1 |= CR1_PEC_Set;\r
716   }\r
717   else\r
718   {\r
719     /* Disable the selected I2C PEC transmission */\r
720     I2Cx->CR1 &= CR1_PEC_Reset;\r
721   }\r
722 }\r
723 \r
724 /**\r
725   * @brief  Selects the specified I2C PEC position.\r
726   * @param  I2Cx: where x can be 1 or 2 to select the I2C peripheral.\r
727   * @param  I2C_PECPosition: specifies the PEC position. \r
728   *   This parameter can be one of the following values:\r
729   *     @arg I2C_PECPosition_Next: indicates that the next byte is PEC\r
730   *     @arg I2C_PECPosition_Current: indicates that current byte is PEC\r
731   * @retval None\r
732   */\r
733 void I2C_PECPositionConfig(I2C_TypeDef* I2Cx, uint16_t I2C_PECPosition)\r
734 {\r
735   /* Check the parameters */\r
736   assert_param(IS_I2C_ALL_PERIPH(I2Cx));\r
737   assert_param(IS_I2C_PEC_POSITION(I2C_PECPosition));\r
738   if (I2C_PECPosition == I2C_PECPosition_Next)\r
739   {\r
740     /* Next byte in shift register is PEC */\r
741     I2Cx->CR1 |= I2C_PECPosition_Next;\r
742   }\r
743   else\r
744   {\r
745     /* Current byte in shift register is PEC */\r
746     I2Cx->CR1 &= I2C_PECPosition_Current;\r
747   }\r
748 }\r
749 \r
750 /**\r
751   * @brief  Enables or disables the PEC value calculation of the transfered bytes.\r
752   * @param  I2Cx: where x can be 1 or 2 to select the I2C peripheral.\r
753   * @param  NewState: new state of the I2Cx PEC value calculation.\r
754   *   This parameter can be: ENABLE or DISABLE.\r
755   * @retval None\r
756   */\r
757 void I2C_CalculatePEC(I2C_TypeDef* I2Cx, FunctionalState NewState)\r
758 {\r
759   /* Check the parameters */\r
760   assert_param(IS_I2C_ALL_PERIPH(I2Cx));\r
761   assert_param(IS_FUNCTIONAL_STATE(NewState));\r
762   if (NewState != DISABLE)\r
763   {\r
764     /* Enable the selected I2C PEC calculation */\r
765     I2Cx->CR1 |= CR1_ENPEC_Set;\r
766   }\r
767   else\r
768   {\r
769     /* Disable the selected I2C PEC calculation */\r
770     I2Cx->CR1 &= CR1_ENPEC_Reset;\r
771   }\r
772 }\r
773 \r
774 /**\r
775   * @brief  Returns the PEC value for the specified I2C.\r
776   * @param  I2Cx: where x can be 1 or 2 to select the I2C peripheral.\r
777   * @retval The PEC value.\r
778   */\r
779 uint8_t I2C_GetPEC(I2C_TypeDef* I2Cx)\r
780 {\r
781   /* Check the parameters */\r
782   assert_param(IS_I2C_ALL_PERIPH(I2Cx));\r
783   /* Return the selected I2C PEC value */\r
784   return ((I2Cx->SR2) >> 8);\r
785 }\r
786 \r
787 /**\r
788   * @brief  Enables or disables the specified I2C ARP.\r
789   * @param  I2Cx: where x can be 1 or 2 to select the I2C peripheral.\r
790   * @param  NewState: new state of the I2Cx ARP. \r
791   *   This parameter can be: ENABLE or DISABLE.\r
792   * @retval None\r
793   */\r
794 void I2C_ARPCmd(I2C_TypeDef* I2Cx, FunctionalState NewState)\r
795 {\r
796   /* Check the parameters */\r
797   assert_param(IS_I2C_ALL_PERIPH(I2Cx));\r
798   assert_param(IS_FUNCTIONAL_STATE(NewState));\r
799   if (NewState != DISABLE)\r
800   {\r
801     /* Enable the selected I2C ARP */\r
802     I2Cx->CR1 |= CR1_ENARP_Set;\r
803   }\r
804   else\r
805   {\r
806     /* Disable the selected I2C ARP */\r
807     I2Cx->CR1 &= CR1_ENARP_Reset;\r
808   }\r
809 }\r
810 \r
811 /**\r
812   * @brief  Enables or disables the specified I2C Clock stretching.\r
813   * @param  I2Cx: where x can be 1 or 2 to select the I2C peripheral.\r
814   * @param  NewState: new state of the I2Cx Clock stretching.\r
815   *   This parameter can be: ENABLE or DISABLE.\r
816   * @retval None\r
817   */\r
818 void I2C_StretchClockCmd(I2C_TypeDef* I2Cx, FunctionalState NewState)\r
819 {\r
820   /* Check the parameters */\r
821   assert_param(IS_I2C_ALL_PERIPH(I2Cx));\r
822   assert_param(IS_FUNCTIONAL_STATE(NewState));\r
823   if (NewState == DISABLE)\r
824   {\r
825     /* Enable the selected I2C Clock stretching */\r
826     I2Cx->CR1 |= CR1_NOSTRETCH_Set;\r
827   }\r
828   else\r
829   {\r
830     /* Disable the selected I2C Clock stretching */\r
831     I2Cx->CR1 &= CR1_NOSTRETCH_Reset;\r
832   }\r
833 }\r
834 \r
835 /**\r
836   * @brief  Selects the specified I2C fast mode duty cycle.\r
837   * @param  I2Cx: where x can be 1 or 2 to select the I2C peripheral.\r
838   * @param  I2C_DutyCycle: specifies the fast mode duty cycle.\r
839   *   This parameter can be one of the following values:\r
840   *     @arg I2C_DutyCycle_2: I2C fast mode Tlow/Thigh = 2\r
841   *     @arg I2C_DutyCycle_16_9: I2C fast mode Tlow/Thigh = 16/9\r
842   * @retval None\r
843   */\r
844 void I2C_FastModeDutyCycleConfig(I2C_TypeDef* I2Cx, uint16_t I2C_DutyCycle)\r
845 {\r
846   /* Check the parameters */\r
847   assert_param(IS_I2C_ALL_PERIPH(I2Cx));\r
848   assert_param(IS_I2C_DUTY_CYCLE(I2C_DutyCycle));\r
849   if (I2C_DutyCycle != I2C_DutyCycle_16_9)\r
850   {\r
851     /* I2C fast mode Tlow/Thigh=2 */\r
852     I2Cx->CCR &= I2C_DutyCycle_2;\r
853   }\r
854   else\r
855   {\r
856     /* I2C fast mode Tlow/Thigh=16/9 */\r
857     I2Cx->CCR |= I2C_DutyCycle_16_9;\r
858   }\r
859 }\r
860 \r
861 /**\r
862   * @brief  Returns the last I2Cx Event.\r
863   * @param  I2Cx: where x can be 1 or 2 to select the I2C peripheral.\r
864   * @retval The last event\r
865   */\r
866 uint32_t I2C_GetLastEvent(I2C_TypeDef* I2Cx)\r
867 {\r
868   uint32_t lastevent = 0;\r
869   uint32_t flag1 = 0, flag2 = 0;\r
870   /* Check the parameters */\r
871   assert_param(IS_I2C_ALL_PERIPH(I2Cx));\r
872   /* Read the I2Cx status register */\r
873   flag1 = I2Cx->SR1;\r
874   flag2 = I2Cx->SR2;\r
875   flag2 = flag2 << 16;\r
876   /* Get the last event value from I2C status register */\r
877   lastevent = (flag1 | flag2) & FLAG_Mask;\r
878   /* Return status */\r
879   return lastevent;\r
880 }\r
881 \r
882 /**\r
883   * @brief  Checks whether the last I2Cx Event is equal to the one passed\r
884   *   as parameter.\r
885   * @param  I2Cx: where x can be 1 or 2 to select the I2C peripheral.\r
886   * @param  I2C_EVENT: specifies the event to be checked. \r
887   *   This parameter can be one of the following values:\r
888   *     @arg I2C_EVENT_SLAVE_ADDRESS_MATCHED   : EV1\r
889   *     @arg I2C_EVENT_SLAVE_BYTE_RECEIVED     : EV2\r
890   *     @arg I2C_EVENT_SLAVE_BYTE_TRANSMITTED  : EV3\r
891   *     @arg I2C_EVENT_SLAVE_ACK_FAILURE       : EV3-2\r
892   *     @arg I2C_EVENT_MASTER_MODE_SELECT      : EV5\r
893   *     @arg I2C_EVENT_MASTER_MODE_SELECTED    : EV6\r
894   *     @arg I2C_EVENT_MASTER_BYTE_RECEIVED    : EV7\r
895   *     @arg I2C_EVENT_MASTER_BYTE_TRANSMITTED : EV8\r
896   *     @arg I2C_EVENT_MASTER_MODE_ADDRESS10   : EV9\r
897   *     @arg I2C_EVENT_SLAVE_STOP_DETECTED     : EV4\r
898   * @retval An ErrorStatus enumuration value:\r
899   * - SUCCESS: Last event is equal to the I2C_EVENT\r
900   * - ERROR: Last event is different from the I2C_EVENT\r
901   */\r
902 ErrorStatus I2C_CheckEvent(I2C_TypeDef* I2Cx, uint32_t I2C_EVENT)\r
903 {\r
904   uint32_t lastevent = 0;\r
905   uint32_t flag1 = 0, flag2 = 0;\r
906   ErrorStatus status = ERROR;\r
907   /* Check the parameters */\r
908   assert_param(IS_I2C_ALL_PERIPH(I2Cx));\r
909   assert_param(IS_I2C_EVENT(I2C_EVENT));\r
910   /* Read the I2Cx status register */\r
911   flag1 = I2Cx->SR1;\r
912   flag2 = I2Cx->SR2;\r
913   flag2 = flag2 << 16;\r
914   /* Get the last event value from I2C status register */\r
915   lastevent = (flag1 | flag2) & FLAG_Mask;\r
916   /* Check whether the last event is equal to I2C_EVENT */\r
917   if (lastevent == I2C_EVENT )\r
918   {\r
919     /* SUCCESS: last event is equal to I2C_EVENT */\r
920     status = SUCCESS;\r
921   }\r
922   else\r
923   {\r
924     /* ERROR: last event is different from I2C_EVENT */\r
925     status = ERROR;\r
926   }\r
927   /* Return status */\r
928   return status;\r
929 }\r
930 \r
931 /**\r
932   * @brief  Checks whether the specified I2C flag is set or not.\r
933   * @param  I2Cx: where x can be 1 or 2 to select the I2C peripheral.\r
934   * @param  I2C_FLAG: specifies the flag to check. \r
935   *   This parameter can be one of the following values:\r
936   *     @arg I2C_FLAG_DUALF: Dual flag (Slave mode)\r
937   *     @arg I2C_FLAG_SMBHOST: SMBus host header (Slave mode)\r
938   *     @arg I2C_FLAG_SMBDEFAULT: SMBus default header (Slave mode)\r
939   *     @arg I2C_FLAG_GENCALL: General call header flag (Slave mode)\r
940   *     @arg I2C_FLAG_TRA: Transmitter/Receiver flag\r
941   *     @arg I2C_FLAG_BUSY: Bus busy flag\r
942   *     @arg I2C_FLAG_MSL: Master/Slave flag\r
943   *     @arg I2C_FLAG_SMBALERT: SMBus Alert flag\r
944   *     @arg I2C_FLAG_TIMEOUT: Timeout or Tlow error flag\r
945   *     @arg I2C_FLAG_PECERR: PEC error in reception flag\r
946   *     @arg I2C_FLAG_OVR: Overrun/Underrun flag (Slave mode)\r
947   *     @arg I2C_FLAG_AF: Acknowledge failure flag\r
948   *     @arg I2C_FLAG_ARLO: Arbitration lost flag (Master mode)\r
949   *     @arg I2C_FLAG_BERR: Bus error flag\r
950   *     @arg I2C_FLAG_TXE: Data register empty flag (Transmitter)\r
951   *     @arg I2C_FLAG_RXNE: Data register not empty (Receiver) flag\r
952   *     @arg I2C_FLAG_STOPF: Stop detection flag (Slave mode)\r
953   *     @arg I2C_FLAG_ADD10: 10-bit header sent flag (Master mode)\r
954   *     @arg I2C_FLAG_BTF: Byte transfer finished flag\r
955   *     @arg I2C_FLAG_ADDR: Address sent flag (Master mode) \93ADSL\94\r
956   *   Address matched flag (Slave mode)\94ENDAD\94\r
957   *     @arg I2C_FLAG_SB: Start bit flag (Master mode)\r
958   * @retval The new state of I2C_FLAG (SET or RESET).\r
959   */\r
960 FlagStatus I2C_GetFlagStatus(I2C_TypeDef* I2Cx, uint32_t I2C_FLAG)\r
961 {\r
962   FlagStatus bitstatus = RESET;\r
963   __IO uint32_t i2creg = 0, i2cxbase = 0;\r
964 \r
965   /* Check the parameters */\r
966   assert_param(IS_I2C_ALL_PERIPH(I2Cx));\r
967   assert_param(IS_I2C_GET_FLAG(I2C_FLAG));\r
968 \r
969   /* Get the I2Cx peripheral base address */\r
970   i2cxbase = (uint32_t)I2Cx;\r
971   \r
972   /* Read flag register index */\r
973   i2creg = I2C_FLAG >> 28;\r
974   \r
975   /* Get bit[23:0] of the flag */\r
976   I2C_FLAG &= FLAG_Mask;\r
977   \r
978   if(i2creg != 0)\r
979   {\r
980     /* Get the I2Cx SR1 register address */\r
981     i2cxbase += 0x14;\r
982   }\r
983   else\r
984   {\r
985     /* Flag in I2Cx SR2 Register */\r
986     I2C_FLAG = (uint32_t)(I2C_FLAG >> 16);\r
987     /* Get the I2Cx SR2 register address */\r
988     i2cxbase += 0x18;\r
989   }\r
990   \r
991   if(((*(__IO uint32_t *)i2cxbase) & I2C_FLAG) != (uint32_t)RESET)\r
992   {\r
993     /* I2C_FLAG is set */\r
994     bitstatus = SET;\r
995   }\r
996   else\r
997   {\r
998     /* I2C_FLAG is reset */\r
999     bitstatus = RESET;\r
1000   }\r
1001   \r
1002   /* Return the I2C_FLAG status */\r
1003   return  bitstatus;\r
1004 }\r
1005 \r
1006 /**\r
1007   * @brief  Clears the I2Cx's pending flags.\r
1008   * @param  I2Cx: where x can be 1 or 2 to select the I2C peripheral.\r
1009   * @param  I2C_FLAG: specifies the flag to clear. \r
1010   *   This parameter can be any combination of the following values:\r
1011   *     @arg I2C_FLAG_SMBALERT: SMBus Alert flag\r
1012   *     @arg I2C_FLAG_TIMEOUT: Timeout or Tlow error flag\r
1013   *     @arg I2C_FLAG_PECERR: PEC error in reception flag\r
1014   *     @arg I2C_FLAG_OVR: Overrun/Underrun flag (Slave mode)\r
1015   *     @arg I2C_FLAG_AF: Acknowledge failure flag\r
1016   *     @arg I2C_FLAG_ARLO: Arbitration lost flag (Master mode)\r
1017   *     @arg I2C_FLAG_BERR: Bus error flag\r
1018   *   \r
1019   * @note\r
1020   *   - STOPF (STOP detection) is cleared by software sequence: a read operation \r
1021   *     to I2C_SR1 register (I2C_GetFlagStatus()) followed by a write operation \r
1022   *     to I2C_CR1 register (I2C_Cmd() to re-enable the I2C peripheral).\r
1023   *   - ADD10 (10-bit header sent) is cleared by software sequence: a read \r
1024   *     operation to I2C_SR1 (I2C_GetFlagStatus()) followed by writing the \r
1025   *     second byte of the address in DR register.\r
1026   *   - BTF (Byte Transfer Finished) is cleared by software sequence: a read \r
1027   *     operation to I2C_SR1 register (I2C_GetFlagStatus()) followed by a \r
1028   *     read/write to I2C_DR register (I2C_SendData()).\r
1029   *   - ADDR (Address sent) is cleared by software sequence: a read operation to \r
1030   *     I2C_SR1 register (I2C_GetFlagStatus()) followed by a read operation to \r
1031   *     I2C_SR2 register ((void)(I2Cx->SR2)).\r
1032   *   - SB (Start Bit) is cleared software sequence: a read operation to I2C_SR1\r
1033   *     register (I2C_GetFlagStatus()) followed by a write operation to I2C_DR\r
1034   *     register  (I2C_SendData()).\r
1035   * @retval None\r
1036   */\r
1037 void I2C_ClearFlag(I2C_TypeDef* I2Cx, uint32_t I2C_FLAG)\r
1038 {\r
1039   uint32_t flagpos = 0;\r
1040   /* Check the parameters */\r
1041   assert_param(IS_I2C_ALL_PERIPH(I2Cx));\r
1042   assert_param(IS_I2C_CLEAR_FLAG(I2C_FLAG));\r
1043   /* Get the I2C flag position */\r
1044   flagpos = I2C_FLAG & FLAG_Mask;\r
1045   /* Clear the selected I2C flag */\r
1046   I2Cx->SR1 = (uint16_t)~flagpos;\r
1047 }\r
1048 \r
1049 /**\r
1050   * @brief  Checks whether the specified I2C interrupt has occurred or not.\r
1051   * @param  I2Cx: where x can be 1 or 2 to select the I2C peripheral.\r
1052   * @param  I2C_IT: specifies the interrupt source to check. \r
1053   *   This parameter can be one of the following values:\r
1054   *     @arg I2C_IT_SMBALERT: SMBus Alert flag\r
1055   *     @arg I2C_IT_TIMEOUT: Timeout or Tlow error flag\r
1056   *     @arg I2C_IT_PECERR: PEC error in reception flag\r
1057   *     @arg I2C_IT_OVR: Overrun/Underrun flag (Slave mode)\r
1058   *     @arg I2C_IT_AF: Acknowledge failure flag\r
1059   *     @arg I2C_IT_ARLO: Arbitration lost flag (Master mode)\r
1060   *     @arg I2C_IT_BERR: Bus error flag\r
1061   *     @arg I2C_IT_TXE: Data register empty flag (Transmitter)\r
1062   *     @arg I2C_IT_RXNE: Data register not empty (Receiver) flag\r
1063   *     @arg I2C_IT_STOPF: Stop detection flag (Slave mode)\r
1064   *     @arg I2C_IT_ADD10: 10-bit header sent flag (Master mode)\r
1065   *     @arg I2C_IT_BTF: Byte transfer finished flag\r
1066   *     @arg I2C_IT_ADDR: Address sent flag (Master mode) \93ADSL\94\r
1067   *                       Address matched flag (Slave mode)\94ENDAD\94\r
1068   *     @arg I2C_IT_SB: Start bit flag (Master mode)\r
1069   * @retval The new state of I2C_IT (SET or RESET).\r
1070   */\r
1071 ITStatus I2C_GetITStatus(I2C_TypeDef* I2Cx, uint32_t I2C_IT)\r
1072 {\r
1073   ITStatus bitstatus = RESET;\r
1074   uint32_t enablestatus = 0;\r
1075   /* Check the parameters */\r
1076   assert_param(IS_I2C_ALL_PERIPH(I2Cx));\r
1077   assert_param(IS_I2C_GET_IT(I2C_IT));\r
1078   /* Check if the interrupt source is enabled or not */\r
1079   enablestatus = (uint32_t)(((I2C_IT & ITEN_Mask) >> 16) & (I2Cx->CR2)) ;  \r
1080   /* Get bit[23:0] of the flag */\r
1081   I2C_IT &= FLAG_Mask;\r
1082   /* Check the status of the specified I2C flag */\r
1083   if (((I2Cx->SR1 & I2C_IT) != (uint32_t)RESET) && enablestatus)\r
1084   {\r
1085     /* I2C_IT is set */\r
1086     bitstatus = SET;\r
1087   }\r
1088   else\r
1089   {\r
1090     /* I2C_IT is reset */\r
1091     bitstatus = RESET;\r
1092   }\r
1093   /* Return the I2C_IT status */\r
1094   return  bitstatus;\r
1095 }\r
1096 \r
1097 /**\r
1098   * @brief  Clears the I2Cx\92s interrupt pending bits.\r
1099   * @param  I2Cx: where x can be 1 or 2 to select the I2C peripheral.\r
1100   * @param  I2C_IT: specifies the interrupt pending bit to clear. \r
1101   *   This parameter can be any combination of the following values:\r
1102   *     @arg I2C_IT_SMBALERT: SMBus Alert interrupt\r
1103   *     @arg I2C_IT_TIMEOUT: Timeout or Tlow error interrupt\r
1104   *     @arg I2C_IT_PECERR: PEC error in reception  interrupt\r
1105   *     @arg I2C_IT_OVR: Overrun/Underrun interrupt (Slave mode)\r
1106   *     @arg I2C_IT_AF: Acknowledge failure interrupt\r
1107   *     @arg I2C_IT_ARLO: Arbitration lost interrupt (Master mode)\r
1108   *     @arg I2C_IT_BERR: Bus error interrupt\r
1109   *   \r
1110   * @note\r
1111   *   - STOPF (STOP detection) is cleared by software sequence: a read operation \r
1112   *     to I2C_SR1 register (I2C_GetITStatus()) followed by a write operation to \r
1113   *     I2C_CR1 register (I2C_Cmd() to re-enable the I2C peripheral).\r
1114   *   - ADD10 (10-bit header sent) is cleared by software sequence: a read \r
1115   *     operation to I2C_SR1 (I2C_GetITStatus()) followed by writing the second \r
1116   *     byte of the address in I2C_DR register.\r
1117   *   - BTF (Byte Transfer Finished) is cleared by software sequence: a read \r
1118   *     operation to I2C_SR1 register (I2C_GetITStatus()) followed by a \r
1119   *     read/write to I2C_DR register (I2C_SendData()).\r
1120   *   - ADDR (Address sent) is cleared by software sequence: a read operation to \r
1121   *     I2C_SR1 register (I2C_GetITStatus()) followed by a read operation to \r
1122   *     I2C_SR2 register ((void)(I2Cx->SR2)).\r
1123   *   - SB (Start Bit) is cleared by software sequence: a read operation to \r
1124   *     I2C_SR1 register (I2C_GetITStatus()) followed by a write operation to \r
1125   *     I2C_DR register (I2C_SendData()).\r
1126   * @retval None\r
1127   */\r
1128 void I2C_ClearITPendingBit(I2C_TypeDef* I2Cx, uint32_t I2C_IT)\r
1129 {\r
1130   uint32_t flagpos = 0;\r
1131   /* Check the parameters */\r
1132   assert_param(IS_I2C_ALL_PERIPH(I2Cx));\r
1133   assert_param(IS_I2C_CLEAR_IT(I2C_IT));\r
1134   /* Get the I2C flag position */\r
1135   flagpos = I2C_IT & FLAG_Mask;\r
1136   /* Clear the selected I2C flag */\r
1137   I2Cx->SR1 = (uint16_t)~flagpos;\r
1138 }\r
1139 \r
1140 /**\r
1141   * @}\r
1142   */ \r
1143 \r
1144 /**\r
1145   * @}\r
1146   */ \r
1147 \r
1148 /**\r
1149   * @}\r
1150   */ \r
1151 \r
1152 /******************* (C) COPYRIGHT 2009 STMicroelectronics *****END OF FILE****/\r