DSO138_SourceCodes_v037.rar
[DSO138] / 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.3.0\r
6   * @date    04/16/2010\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 2010 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 if the next DMA transfer will be 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 \r
863 /**\r
864  * @brief\r
865  ****************************************************************************************\r
866  *\r
867  *                         I2C State Monitoring Functions\r
868  *                       \r
869  ****************************************************************************************   \r
870  * This I2C driver provides three different ways for I2C state monitoring\r
871  *  depending on the application requirements and constraints:\r
872  *        \r
873  *  \r
874  * 1) Basic state monitoring:\r
875  *    Using I2C_CheckEvent() function:\r
876  *    It compares the status registers (SR1 and SR2) content to a given event\r
877  *    (can be the combination of one or more flags).\r
878  *    It returns SUCCESS if the current status includes the given flags \r
879  *    and returns ERROR if one or more flags are missing in the current status.\r
880  *    - When to use:\r
881  *      - This function is suitable for most applciations as well as for startup \r
882  *      activity since the events are fully described in the product reference manual \r
883  *      (RM0008).\r
884  *      - It is also suitable for users who need to define their own events.\r
885  *    - Limitations:\r
886  *      - If an error occurs (ie. error flags are set besides to the monitored flags),\r
887  *        the I2C_CheckEvent() function may return SUCCESS despite the communication\r
888  *        hold or corrupted real state. \r
889  *        In this case, it is advised to use error interrupts to monitor the error\r
890  *        events and handle them in the interrupt IRQ handler.\r
891  *        \r
892  *        @note \r
893  *        For error management, it is advised to use the following functions:\r
894  *          - I2C_ITConfig() to configure and enable the error interrupts (I2C_IT_ERR).\r
895  *          - I2Cx_ER_IRQHandler() which is called when the error interurpt occurs.\r
896  *            Where x is the peripheral instance (I2C1, I2C2 ...)\r
897  *          - I2C_GetFlagStatus() or I2C_GetITStatus() to be called into I2Cx_ER_IRQHandler() \r
898  *            in order to determine which error occured.\r
899  *          - I2C_ClearFlag() or I2C_ClearITPendingBit() and/or I2C_SoftwareResetCmd()\r
900  *            and/or I2C_GenerateStop() in order to clear the error flag and source,\r
901  *            and return to correct communication status.\r
902  *            \r
903  *\r
904  *  2) Advanced state monitoring:\r
905  *     Using the function I2C_GetLastEvent() which returns the image of both status \r
906  *     registers in a single word (uint32_t) (Status Register 2 value is shifted left \r
907  *     by 16 bits and concatenated to Status Register 1).\r
908  *     - When to use:\r
909  *       - This function is suitable for the same applications above but it allows to\r
910  *         overcome the mentionned limitation of I2C_GetFlagStatus() function.\r
911  *         The returned value could be compared to events already defined in the \r
912  *         library (stm32f10x_i2c.h) or to custom values defiend by user.\r
913  *       - This function is suitable when multiple flags are monitored at the same time.\r
914  *       - At the opposite of I2C_CheckEvent() function, this function allows user to\r
915  *         choose when an event is accepted (when all events flags are set and no \r
916  *         other flags are set or just when the needed flags are set like \r
917  *         I2C_CheckEvent() function).\r
918  *     - Limitations:\r
919  *       - User may need to define his own events.\r
920  *       - Same remark concerning the error management is applicable for this \r
921  *         function if user decides to check only regular communication flags (and \r
922  *         ignores error flags).\r
923  *     \r
924  *\r
925  *  3) Flag-based state monitoring:\r
926  *     Using the function I2C_GetFlagStatus() which simply returns the status of \r
927  *     one single flag (ie. I2C_FLAG_RXNE ...). \r
928  *     - When to use:\r
929  *        - This function could be used for specific applications or in debug phase.\r
930  *        - It is suitable when only one flag checking is needed (most I2C events \r
931  *          are monitored through multiple flags).\r
932  *     - Limitations: \r
933  *        - When calling this function, the Status register is accessed. Some flags are\r
934  *          cleared when the status register is accessed. So checking the status\r
935  *          of one Flag, may clear other ones.\r
936  *        - Function may need to be called twice or more in order to monitor one \r
937  *          single event.\r
938  *\r
939  *  For detailed description of Events, please refer to section I2C_Events in \r
940  *  stm32f10x_i2c.h file.\r
941  *  \r
942  */\r
943 \r
944 /**\r
945  * \r
946  *  1) Basic state monitoring\r
947  *******************************************************************************\r
948  */\r
949 \r
950 /**\r
951   * @brief  Checks whether the last I2Cx Event is equal to the one passed\r
952   *   as parameter.\r
953   * @param  I2Cx: where x can be 1 or 2 to select the I2C peripheral.\r
954   * @param  I2C_EVENT: specifies the event to be checked. \r
955   *   This parameter can be one of the following values:\r
956   *     @arg I2C_EVENT_SLAVE_TRANSMITTER_ADDRESS_MATCHED           : EV1\r
957   *     @arg I2C_EVENT_SLAVE_RECEIVER_ADDRESS_MATCHED              : EV1\r
958   *     @arg I2C_EVENT_SLAVE_TRANSMITTER_SECONDADDRESS_MATCHED     : EV1\r
959   *     @arg I2C_EVENT_SLAVE_RECEIVER_SECONDADDRESS_MATCHED        : EV1\r
960   *     @arg I2C_EVENT_SLAVE_GENERALCALLADDRESS_MATCHED            : EV1\r
961   *     @arg I2C_EVENT_SLAVE_BYTE_RECEIVED                         : EV2\r
962   *     @arg (I2C_EVENT_SLAVE_BYTE_RECEIVED | I2C_FLAG_DUALF)      : EV2\r
963   *     @arg (I2C_EVENT_SLAVE_BYTE_RECEIVED | I2C_FLAG_GENCALL)    : EV2\r
964   *     @arg I2C_EVENT_SLAVE_BYTE_TRANSMITTED                      : EV3\r
965   *     @arg (I2C_EVENT_SLAVE_BYTE_TRANSMITTED | I2C_FLAG_DUALF)   : EV3\r
966   *     @arg (I2C_EVENT_SLAVE_BYTE_TRANSMITTED | I2C_FLAG_GENCALL) : EV3\r
967   *     @arg I2C_EVENT_SLAVE_ACK_FAILURE                           : EV3_2\r
968   *     @arg I2C_EVENT_SLAVE_STOP_DETECTED                         : EV4\r
969   *     @arg I2C_EVENT_MASTER_MODE_SELECT                          : EV5\r
970   *     @arg I2C_EVENT_MASTER_TRANSMITTER_MODE_SELECTED            : EV6     \r
971   *     @arg I2C_EVENT_MASTER_RECEIVER_MODE_SELECTED               : EV6\r
972   *     @arg I2C_EVENT_MASTER_BYTE_RECEIVED                        : EV7\r
973   *     @arg I2C_EVENT_MASTER_BYTE_TRANSMITTING                    : EV8\r
974   *     @arg I2C_EVENT_MASTER_BYTE_TRANSMITTED                     : EV8_2\r
975   *     @arg I2C_EVENT_MASTER_MODE_ADDRESS10                       : EV9\r
976   *     \r
977   * @note: For detailed description of Events, please refer to section \r
978   *    I2C_Events in stm32f10x_i2c.h file.\r
979   *    \r
980   * @retval An ErrorStatus enumuration value:\r
981   * - SUCCESS: Last event is equal to the I2C_EVENT\r
982   * - ERROR: Last event is different from the I2C_EVENT\r
983   */\r
984 ErrorStatus I2C_CheckEvent(I2C_TypeDef* I2Cx, uint32_t I2C_EVENT)\r
985 {\r
986   uint32_t lastevent = 0;\r
987   uint32_t flag1 = 0, flag2 = 0;\r
988   ErrorStatus status = ERROR;\r
989 \r
990   /* Check the parameters */\r
991   assert_param(IS_I2C_ALL_PERIPH(I2Cx));\r
992   assert_param(IS_I2C_EVENT(I2C_EVENT));\r
993 \r
994   /* Read the I2Cx status register */\r
995   flag1 = I2Cx->SR1;\r
996   flag2 = I2Cx->SR2;\r
997   flag2 = flag2 << 16;\r
998 \r
999   /* Get the last event value from I2C status register */\r
1000   lastevent = (flag1 | flag2) & FLAG_Mask;\r
1001 \r
1002   /* Check whether the last event contains the I2C_EVENT */\r
1003   if ((lastevent & I2C_EVENT) == I2C_EVENT)\r
1004   {\r
1005     /* SUCCESS: last event is equal to I2C_EVENT */\r
1006     status = SUCCESS;\r
1007   }\r
1008   else\r
1009   {\r
1010     /* ERROR: last event is different from I2C_EVENT */\r
1011     status = ERROR;\r
1012   }\r
1013   /* Return status */\r
1014   return status;\r
1015 }\r
1016 \r
1017 /**\r
1018  * \r
1019  *  2) Advanced state monitoring\r
1020  *******************************************************************************\r
1021  */\r
1022 \r
1023 /**\r
1024   * @brief  Returns the last I2Cx Event.\r
1025   * @param  I2Cx: where x can be 1 or 2 to select the I2C peripheral.\r
1026   *     \r
1027   * @note: For detailed description of Events, please refer to section \r
1028   *    I2C_Events in stm32f10x_i2c.h file.\r
1029   *    \r
1030   * @retval The last event\r
1031   */\r
1032 uint32_t I2C_GetLastEvent(I2C_TypeDef* I2Cx)\r
1033 {\r
1034   uint32_t lastevent = 0;\r
1035   uint32_t flag1 = 0, flag2 = 0;\r
1036 \r
1037   /* Check the parameters */\r
1038   assert_param(IS_I2C_ALL_PERIPH(I2Cx));\r
1039 \r
1040   /* Read the I2Cx status register */\r
1041   flag1 = I2Cx->SR1;\r
1042   flag2 = I2Cx->SR2;\r
1043   flag2 = flag2 << 16;\r
1044 \r
1045   /* Get the last event value from I2C status register */\r
1046   lastevent = (flag1 | flag2) & FLAG_Mask;\r
1047 \r
1048   /* Return status */\r
1049   return lastevent;\r
1050 }\r
1051 \r
1052 /**\r
1053  * \r
1054  *  3) Flag-based state monitoring\r
1055  *******************************************************************************\r
1056  */\r
1057 \r
1058 /**\r
1059   * @brief  Checks whether the specified I2C flag is set or not.\r
1060   * @param  I2Cx: where x can be 1 or 2 to select the I2C peripheral.\r
1061   * @param  I2C_FLAG: specifies the flag to check. \r
1062   *   This parameter can be one of the following values:\r
1063   *     @arg I2C_FLAG_DUALF: Dual flag (Slave mode)\r
1064   *     @arg I2C_FLAG_SMBHOST: SMBus host header (Slave mode)\r
1065   *     @arg I2C_FLAG_SMBDEFAULT: SMBus default header (Slave mode)\r
1066   *     @arg I2C_FLAG_GENCALL: General call header flag (Slave mode)\r
1067   *     @arg I2C_FLAG_TRA: Transmitter/Receiver flag\r
1068   *     @arg I2C_FLAG_BUSY: Bus busy flag\r
1069   *     @arg I2C_FLAG_MSL: Master/Slave flag\r
1070   *     @arg I2C_FLAG_SMBALERT: SMBus Alert flag\r
1071   *     @arg I2C_FLAG_TIMEOUT: Timeout or Tlow error flag\r
1072   *     @arg I2C_FLAG_PECERR: PEC error in reception flag\r
1073   *     @arg I2C_FLAG_OVR: Overrun/Underrun flag (Slave mode)\r
1074   *     @arg I2C_FLAG_AF: Acknowledge failure flag\r
1075   *     @arg I2C_FLAG_ARLO: Arbitration lost flag (Master mode)\r
1076   *     @arg I2C_FLAG_BERR: Bus error flag\r
1077   *     @arg I2C_FLAG_TXE: Data register empty flag (Transmitter)\r
1078   *     @arg I2C_FLAG_RXNE: Data register not empty (Receiver) flag\r
1079   *     @arg I2C_FLAG_STOPF: Stop detection flag (Slave mode)\r
1080   *     @arg I2C_FLAG_ADD10: 10-bit header sent flag (Master mode)\r
1081   *     @arg I2C_FLAG_BTF: Byte transfer finished flag\r
1082   *     @arg I2C_FLAG_ADDR: Address sent flag (Master mode) \93ADSL\94\r
1083   *   Address matched flag (Slave mode)\94ENDAD\94\r
1084   *     @arg I2C_FLAG_SB: Start bit flag (Master mode)\r
1085   * @retval The new state of I2C_FLAG (SET or RESET).\r
1086   */\r
1087 FlagStatus I2C_GetFlagStatus(I2C_TypeDef* I2Cx, uint32_t I2C_FLAG)\r
1088 {\r
1089   FlagStatus bitstatus = RESET;\r
1090   __IO uint32_t i2creg = 0, i2cxbase = 0;\r
1091 \r
1092   /* Check the parameters */\r
1093   assert_param(IS_I2C_ALL_PERIPH(I2Cx));\r
1094   assert_param(IS_I2C_GET_FLAG(I2C_FLAG));\r
1095 \r
1096   /* Get the I2Cx peripheral base address */\r
1097   i2cxbase = (uint32_t)I2Cx;\r
1098   \r
1099   /* Read flag register index */\r
1100   i2creg = I2C_FLAG >> 28;\r
1101   \r
1102   /* Get bit[23:0] of the flag */\r
1103   I2C_FLAG &= FLAG_Mask;\r
1104   \r
1105   if(i2creg != 0)\r
1106   {\r
1107     /* Get the I2Cx SR1 register address */\r
1108     i2cxbase += 0x14;\r
1109   }\r
1110   else\r
1111   {\r
1112     /* Flag in I2Cx SR2 Register */\r
1113     I2C_FLAG = (uint32_t)(I2C_FLAG >> 16);\r
1114     /* Get the I2Cx SR2 register address */\r
1115     i2cxbase += 0x18;\r
1116   }\r
1117   \r
1118   if(((*(__IO uint32_t *)i2cxbase) & I2C_FLAG) != (uint32_t)RESET)\r
1119   {\r
1120     /* I2C_FLAG is set */\r
1121     bitstatus = SET;\r
1122   }\r
1123   else\r
1124   {\r
1125     /* I2C_FLAG is reset */\r
1126     bitstatus = RESET;\r
1127   }\r
1128   \r
1129   /* Return the I2C_FLAG status */\r
1130   return  bitstatus;\r
1131 }\r
1132 \r
1133 \r
1134 \r
1135 /**\r
1136   * @brief  Clears the I2Cx's pending flags.\r
1137   * @param  I2Cx: where x can be 1 or 2 to select the I2C peripheral.\r
1138   * @param  I2C_FLAG: specifies the flag to clear. \r
1139   *   This parameter can be any combination of the following values:\r
1140   *     @arg I2C_FLAG_SMBALERT: SMBus Alert flag\r
1141   *     @arg I2C_FLAG_TIMEOUT: Timeout or Tlow error flag\r
1142   *     @arg I2C_FLAG_PECERR: PEC error in reception flag\r
1143   *     @arg I2C_FLAG_OVR: Overrun/Underrun flag (Slave mode)\r
1144   *     @arg I2C_FLAG_AF: Acknowledge failure flag\r
1145   *     @arg I2C_FLAG_ARLO: Arbitration lost flag (Master mode)\r
1146   *     @arg I2C_FLAG_BERR: Bus error flag\r
1147   *   \r
1148   * @note\r
1149   *   - STOPF (STOP detection) is cleared by software sequence: a read operation \r
1150   *     to I2C_SR1 register (I2C_GetFlagStatus()) followed by a write operation \r
1151   *     to I2C_CR1 register (I2C_Cmd() to re-enable the I2C peripheral).\r
1152   *   - ADD10 (10-bit header sent) is cleared by software sequence: a read \r
1153   *     operation to I2C_SR1 (I2C_GetFlagStatus()) followed by writing the \r
1154   *     second byte of the address in DR register.\r
1155   *   - BTF (Byte Transfer Finished) is cleared by software sequence: a read \r
1156   *     operation to I2C_SR1 register (I2C_GetFlagStatus()) followed by a \r
1157   *     read/write to I2C_DR register (I2C_SendData()).\r
1158   *   - ADDR (Address sent) is cleared by software sequence: a read operation to \r
1159   *     I2C_SR1 register (I2C_GetFlagStatus()) followed by a read operation to \r
1160   *     I2C_SR2 register ((void)(I2Cx->SR2)).\r
1161   *   - SB (Start Bit) is cleared software sequence: a read operation to I2C_SR1\r
1162   *     register (I2C_GetFlagStatus()) followed by a write operation to I2C_DR\r
1163   *     register  (I2C_SendData()).\r
1164   * @retval None\r
1165   */\r
1166 void I2C_ClearFlag(I2C_TypeDef* I2Cx, uint32_t I2C_FLAG)\r
1167 {\r
1168   uint32_t flagpos = 0;\r
1169   /* Check the parameters */\r
1170   assert_param(IS_I2C_ALL_PERIPH(I2Cx));\r
1171   assert_param(IS_I2C_CLEAR_FLAG(I2C_FLAG));\r
1172   /* Get the I2C flag position */\r
1173   flagpos = I2C_FLAG & FLAG_Mask;\r
1174   /* Clear the selected I2C flag */\r
1175   I2Cx->SR1 = (uint16_t)~flagpos;\r
1176 }\r
1177 \r
1178 /**\r
1179   * @brief  Checks whether the specified I2C interrupt has occurred or not.\r
1180   * @param  I2Cx: where x can be 1 or 2 to select the I2C peripheral.\r
1181   * @param  I2C_IT: specifies the interrupt source to check. \r
1182   *   This parameter can be one of the following values:\r
1183   *     @arg I2C_IT_SMBALERT: SMBus Alert flag\r
1184   *     @arg I2C_IT_TIMEOUT: Timeout or Tlow error flag\r
1185   *     @arg I2C_IT_PECERR: PEC error in reception flag\r
1186   *     @arg I2C_IT_OVR: Overrun/Underrun flag (Slave mode)\r
1187   *     @arg I2C_IT_AF: Acknowledge failure flag\r
1188   *     @arg I2C_IT_ARLO: Arbitration lost flag (Master mode)\r
1189   *     @arg I2C_IT_BERR: Bus error flag\r
1190   *     @arg I2C_IT_TXE: Data register empty flag (Transmitter)\r
1191   *     @arg I2C_IT_RXNE: Data register not empty (Receiver) flag\r
1192   *     @arg I2C_IT_STOPF: Stop detection flag (Slave mode)\r
1193   *     @arg I2C_IT_ADD10: 10-bit header sent flag (Master mode)\r
1194   *     @arg I2C_IT_BTF: Byte transfer finished flag\r
1195   *     @arg I2C_IT_ADDR: Address sent flag (Master mode) \93ADSL\94\r
1196   *                       Address matched flag (Slave mode)\94ENDAD\94\r
1197   *     @arg I2C_IT_SB: Start bit flag (Master mode)\r
1198   * @retval The new state of I2C_IT (SET or RESET).\r
1199   */\r
1200 ITStatus I2C_GetITStatus(I2C_TypeDef* I2Cx, uint32_t I2C_IT)\r
1201 {\r
1202   ITStatus bitstatus = RESET;\r
1203   uint32_t enablestatus = 0;\r
1204 \r
1205   /* Check the parameters */\r
1206   assert_param(IS_I2C_ALL_PERIPH(I2Cx));\r
1207   assert_param(IS_I2C_GET_IT(I2C_IT));\r
1208 \r
1209   /* Check if the interrupt source is enabled or not */\r
1210   enablestatus = (uint32_t)(((I2C_IT & ITEN_Mask) >> 16) & (I2Cx->CR2)) ;\r
1211   \r
1212   /* Get bit[23:0] of the flag */\r
1213   I2C_IT &= FLAG_Mask;\r
1214 \r
1215   /* Check the status of the specified I2C flag */\r
1216   if (((I2Cx->SR1 & I2C_IT) != (uint32_t)RESET) && enablestatus)\r
1217   {\r
1218     /* I2C_IT is set */\r
1219     bitstatus = SET;\r
1220   }\r
1221   else\r
1222   {\r
1223     /* I2C_IT is reset */\r
1224     bitstatus = RESET;\r
1225   }\r
1226   /* Return the I2C_IT status */\r
1227   return  bitstatus;\r
1228 }\r
1229 \r
1230 /**\r
1231   * @brief  Clears the I2Cx\92s interrupt pending bits.\r
1232   * @param  I2Cx: where x can be 1 or 2 to select the I2C peripheral.\r
1233   * @param  I2C_IT: specifies the interrupt pending bit to clear. \r
1234   *   This parameter can be any combination of the following values:\r
1235   *     @arg I2C_IT_SMBALERT: SMBus Alert interrupt\r
1236   *     @arg I2C_IT_TIMEOUT: Timeout or Tlow error interrupt\r
1237   *     @arg I2C_IT_PECERR: PEC error in reception  interrupt\r
1238   *     @arg I2C_IT_OVR: Overrun/Underrun interrupt (Slave mode)\r
1239   *     @arg I2C_IT_AF: Acknowledge failure interrupt\r
1240   *     @arg I2C_IT_ARLO: Arbitration lost interrupt (Master mode)\r
1241   *     @arg I2C_IT_BERR: Bus error interrupt\r
1242   *   \r
1243   * @note\r
1244   *   - STOPF (STOP detection) is cleared by software sequence: a read operation \r
1245   *     to I2C_SR1 register (I2C_GetITStatus()) followed by a write operation to \r
1246   *     I2C_CR1 register (I2C_Cmd() to re-enable the I2C peripheral).\r
1247   *   - ADD10 (10-bit header sent) is cleared by software sequence: a read \r
1248   *     operation to I2C_SR1 (I2C_GetITStatus()) followed by writing the second \r
1249   *     byte of the address in I2C_DR register.\r
1250   *   - BTF (Byte Transfer Finished) is cleared by software sequence: a read \r
1251   *     operation to I2C_SR1 register (I2C_GetITStatus()) followed by a \r
1252   *     read/write to I2C_DR register (I2C_SendData()).\r
1253   *   - ADDR (Address sent) is cleared by software sequence: a read operation to \r
1254   *     I2C_SR1 register (I2C_GetITStatus()) followed by a read operation to \r
1255   *     I2C_SR2 register ((void)(I2Cx->SR2)).\r
1256   *   - SB (Start Bit) is cleared by software sequence: a read operation to \r
1257   *     I2C_SR1 register (I2C_GetITStatus()) followed by a write operation to \r
1258   *     I2C_DR register (I2C_SendData()).\r
1259   * @retval None\r
1260   */\r
1261 void I2C_ClearITPendingBit(I2C_TypeDef* I2Cx, uint32_t I2C_IT)\r
1262 {\r
1263   uint32_t flagpos = 0;\r
1264   /* Check the parameters */\r
1265   assert_param(IS_I2C_ALL_PERIPH(I2Cx));\r
1266   assert_param(IS_I2C_CLEAR_IT(I2C_IT));\r
1267   /* Get the I2C flag position */\r
1268   flagpos = I2C_IT & FLAG_Mask;\r
1269   /* Clear the selected I2C flag */\r
1270   I2Cx->SR1 = (uint16_t)~flagpos;\r
1271 }\r
1272 \r
1273 /**\r
1274   * @}\r
1275   */ \r
1276 \r
1277 /**\r
1278   * @}\r
1279   */ \r
1280 \r
1281 /**\r
1282   * @}\r
1283   */ \r
1284 \r
1285 /******************* (C) COPYRIGHT 2010 STMicroelectronics *****END OF FILE****/\r