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