Merge pull request #93 from zyp/master
[fw/stlink] / example / libs_stm / src / stm32l1xx / stm32l1xx_i2c.c
1 /**\r
2   ******************************************************************************\r
3   * @file    stm32l1xx_i2c.c\r
4   * @author  MCD Application Team\r
5   * @version V1.0.0\r
6   * @date    31-December-2010\r
7   * @brief   This file provides firmware functions to manage the following \r
8   *          functionalities of the Inter-integrated circuit (I2C)\r
9   *           - Initialization and Configuration\r
10   *           - Data transfers\r
11   *           - PEC management\r
12   *           - DMA transfers management\r
13   *           - Interrupts, events and flags management \r
14   *           \r
15   *  @verbatim\r
16   *    \r
17   *          ===================================================================\r
18   *                                 How to use this driver\r
19   *          ===================================================================\r
20   *          1. Enable peripheral clock using RCC_APB1PeriphClockCmd(RCC_APB1Periph_I2Cx, ENABLE)\r
21   *             function for I2C1 or I2C2.\r
22   *\r
23   *          2. Enable SDA, SCL  and SMBA (when used) GPIO clocks using \r
24   *             RCC_AHBPeriphClockCmd() function. \r
25   *\r
26   *          3. Peripherals alternate function: \r
27   *                 - Connect the pin to the desired peripherals' Alternate \r
28   *                   Function (AF) using GPIO_PinAFConfig() function\r
29   *                 - Configure the desired pin in alternate function by:\r
30   *                   GPIO_InitStruct->GPIO_Mode = GPIO_Mode_AF\r
31   *                 - Select the type, pull-up/pull-down and output speed via \r
32   *                   GPIO_PuPd, GPIO_OType and GPIO_Speed members\r
33   *                 - Call GPIO_Init() function\r
34   *        \r
35   *          4. Program the Mode, duty cycle , Own address, Ack, Speed and Acknowledged\r
36   *             Address using the I2C_Init() function.\r
37   *\r
38   *          5. Optionally you can enable/configure the following parameters without\r
39   *             re-initialization (i.e there is no need to call again I2C_Init() function):\r
40   *              - Enable the acknowledge feature using I2C_AcknowledgeConfig() function\r
41   *              - Enable the dual addressing mode using I2C_DualAddressCmd() function\r
42   *              - Enable the general call using the I2C_GeneralCallCmd() function\r
43   *              - Enable the clock stretching using I2C_StretchClockCmd() function\r
44   *              - Enable the fast mode duty cycle using the I2C_FastModeDutyCycleConfig()\r
45   *                function\r
46   *              - Enable the PEC Calculation using I2C_CalculatePEC() function\r
47   *              - For SMBus Mode: \r
48   *                   - Enable the Address Resolution Protocol (ARP) using I2C_ARPCmd() function\r
49   *                   - Configure the SMBusAlert pin using I2C_SMBusAlertConfig() function\r
50   *\r
51   *          6. Enable the NVIC and the corresponding interrupt using the function \r
52   *             I2C_ITConfig() if you need to use interrupt mode. \r
53   *\r
54   *          7. When using the DMA mode \r
55   *                   - Configure the DMA using DMA_Init() function\r
56   *                   - Active the needed channel Request using I2C_DMACmd() or\r
57               I2C_DMALastTransferCmd() function\r
58   * \r
59   *          8. Enable the I2C using the I2C_Cmd() function.\r
60   * \r
61   *          9. Enable the DMA using the DMA_Cmd() function when using DMA mode in the \r
62   *             transfers. \r
63   *\r
64   *  @endverbatim\r
65   *  \r
66   ******************************************************************************\r
67   * @attention\r
68   *\r
69   * THE PRESENT FIRMWARE WHICH IS FOR GUIDANCE ONLY AIMS AT PROVIDING CUSTOMERS\r
70   * WITH CODING INFORMATION REGARDING THEIR PRODUCTS IN ORDER FOR THEM TO SAVE\r
71   * TIME. AS A RESULT, STMICROELECTRONICS SHALL NOT BE HELD LIABLE FOR ANY\r
72   * DIRECT, INDIRECT OR CONSEQUENTIAL DAMAGES WITH RESPECT TO ANY CLAIMS ARISING\r
73   * FROM THE CONTENT OF SUCH FIRMWARE AND/OR THE USE MADE BY CUSTOMERS OF THE\r
74   * CODING INFORMATION CONTAINED HEREIN IN CONNECTION WITH THEIR PRODUCTS.\r
75   *\r
76   * <h2><center>&copy; COPYRIGHT 2010 STMicroelectronics</center></h2>\r
77   ******************************************************************************  \r
78   */ \r
79 \r
80 /* Includes ------------------------------------------------------------------*/\r
81 #include "stm32l1xx_i2c.h"\r
82 #include "stm32l1xx_rcc.h"\r
83 \r
84 \r
85 /** @addtogroup STM32L1xx_StdPeriph_Driver\r
86   * @{\r
87   */\r
88 \r
89 /** @defgroup I2C \r
90   * @brief I2C driver modules\r
91   * @{\r
92   */ \r
93 \r
94 /* Private typedef -----------------------------------------------------------*/\r
95 /* Private define ------------------------------------------------------------*/\r
96 \r
97 #define CR1_CLEAR_MASK          ((uint16_t)0xFBF5)      /*<! I2C registers Masks */\r
98 #define FLAG_MASK               ((uint32_t)0x00FFFFFF)  /*<! I2C FLAG mask */\r
99 #define ITEN_MASK               ((uint32_t)0x07000000)  /*<! I2C Interrupt Enable mask */\r
100 \r
101 /* Private macro -------------------------------------------------------------*/\r
102 /* Private variables ---------------------------------------------------------*/\r
103 /* Private function prototypes -----------------------------------------------*/\r
104 /* Private functions ---------------------------------------------------------*/\r
105 \r
106 /** @defgroup I2C_Private_Functions\r
107   * @{\r
108   */\r
109 \r
110 /** @defgroup I2C_Group1 Initialization and Configuration functions\r
111  *  @brief   Initialization and Configuration functions \r
112  *\r
113 @verbatim   \r
114  ===============================================================================\r
115                    Initialization and Configuration functions\r
116  ===============================================================================  \r
117 \r
118 @endverbatim\r
119   * @{\r
120   */\r
121 \r
122 /**\r
123   * @brief  Deinitializes the I2Cx peripheral registers to their default reset values.\r
124   * @param  I2Cx: where x can be 1 or 2 to select the I2C peripheral.\r
125   * @retval None\r
126   */\r
127 void I2C_DeInit(I2C_TypeDef* I2Cx)\r
128 {\r
129   /* Check the parameters */\r
130   assert_param(IS_I2C_ALL_PERIPH(I2Cx));\r
131 \r
132   if (I2Cx == I2C1)\r
133   {\r
134     /* Enable I2C1 reset state */\r
135     RCC_APB1PeriphResetCmd(RCC_APB1Periph_I2C1, ENABLE);\r
136     /* Release I2C1 from reset state */\r
137     RCC_APB1PeriphResetCmd(RCC_APB1Periph_I2C1, DISABLE);\r
138   }\r
139   else\r
140   {\r
141     /* Enable I2C2 reset state */\r
142     RCC_APB1PeriphResetCmd(RCC_APB1Periph_I2C2, ENABLE);\r
143     /* Release I2C2 from reset state */\r
144     RCC_APB1PeriphResetCmd(RCC_APB1Periph_I2C2, DISABLE);\r
145   }\r
146 }\r
147 \r
148 /**\r
149   * @brief  Initializes the I2Cx peripheral according to the specified \r
150   *         parameters in the I2C_InitStruct.\r
151   * @note   To use the I2C at 400 KHz (in fast mode), the PCLK1 frequency \r
152   *         (I2C peripheral input clock) must be a multiple of 10 MHz.  \r
153   * @param  I2Cx: where x can be 1 or 2 to select the I2C peripheral.\r
154   * @param  I2C_InitStruct: pointer to a I2C_InitTypeDef structure that\r
155   *   contains the configuration information for the specified I2C peripheral.\r
156   * @retval None\r
157   */\r
158 void I2C_Init(I2C_TypeDef* I2Cx, I2C_InitTypeDef* I2C_InitStruct)\r
159 {\r
160   uint16_t tmpreg = 0, freqrange = 0;\r
161   uint16_t result = 0x04;\r
162   uint32_t pclk1 = 8000000;\r
163   RCC_ClocksTypeDef  rcc_clocks;\r
164   /* Check the parameters */\r
165   assert_param(IS_I2C_ALL_PERIPH(I2Cx));\r
166   assert_param(IS_I2C_CLOCK_SPEED(I2C_InitStruct->I2C_ClockSpeed));\r
167   assert_param(IS_I2C_MODE(I2C_InitStruct->I2C_Mode));\r
168   assert_param(IS_I2C_DUTY_CYCLE(I2C_InitStruct->I2C_DutyCycle));\r
169   assert_param(IS_I2C_OWN_ADDRESS1(I2C_InitStruct->I2C_OwnAddress1));\r
170   assert_param(IS_I2C_ACK_STATE(I2C_InitStruct->I2C_Ack));\r
171   assert_param(IS_I2C_ACKNOWLEDGE_ADDRESS(I2C_InitStruct->I2C_AcknowledgedAddress));\r
172 \r
173 /*---------------------------- I2Cx CR2 Configuration ------------------------*/\r
174   /* Get the I2Cx CR2 value */\r
175   tmpreg = I2Cx->CR2;\r
176   /* Clear frequency FREQ[5:0] bits */\r
177   tmpreg &= (uint16_t)~((uint16_t)I2C_CR2_FREQ);\r
178   /* Get pclk1 frequency value */\r
179   RCC_GetClocksFreq(&rcc_clocks);\r
180   pclk1 = rcc_clocks.PCLK1_Frequency;\r
181   /* Set frequency bits depending on pclk1 value */\r
182   freqrange = (uint16_t)(pclk1 / 1000000);\r
183   tmpreg |= freqrange;\r
184   /* Write to I2Cx CR2 */\r
185   I2Cx->CR2 = tmpreg;\r
186 \r
187 /*---------------------------- I2Cx CCR Configuration ------------------------*/\r
188   /* Disable the selected I2C peripheral to configure TRISE */\r
189   I2Cx->CR1 &= (uint16_t)~((uint16_t)I2C_CR1_PE);\r
190   /* Reset tmpreg value */\r
191   /* Clear F/S, DUTY and CCR[11:0] bits */\r
192   tmpreg = 0;\r
193 \r
194   /* Configure speed in standard mode */\r
195   if (I2C_InitStruct->I2C_ClockSpeed <= 100000)\r
196   {\r
197     /* Standard mode speed calculate */\r
198     result = (uint16_t)(pclk1 / (I2C_InitStruct->I2C_ClockSpeed << 1));\r
199     /* Test if CCR value is under 0x4*/\r
200     if (result < 0x04)\r
201     {\r
202       /* Set minimum allowed value */\r
203       result = 0x04;  \r
204     }\r
205     /* Set speed value for standard mode */\r
206     tmpreg |= result;     \r
207     /* Set Maximum Rise Time for standard mode */\r
208     I2Cx->TRISE = freqrange + 1; \r
209   }\r
210   /* Configure speed in fast mode */\r
211   /* To use the I2C at 400 KHz (in fast mode), the PCLK1 frequency (I2C peripheral\r
212      input clock) must be a multiple of 10 MHz */\r
213   else /*(I2C_InitStruct->I2C_ClockSpeed <= 400000)*/\r
214   {\r
215     if (I2C_InitStruct->I2C_DutyCycle == I2C_DutyCycle_2)\r
216     {\r
217       /* Fast mode speed calculate: Tlow/Thigh = 2 */\r
218       result = (uint16_t)(pclk1 / (I2C_InitStruct->I2C_ClockSpeed * 3));\r
219     }\r
220     else /*I2C_InitStruct->I2C_DutyCycle == I2C_DutyCycle_16_9*/\r
221     {\r
222       /* Fast mode speed calculate: Tlow/Thigh = 16/9 */\r
223       result = (uint16_t)(pclk1 / (I2C_InitStruct->I2C_ClockSpeed * 25));\r
224       /* Set DUTY bit */\r
225       result |= I2C_DutyCycle_16_9;\r
226     }\r
227 \r
228     /* Test if CCR value is under 0x1*/\r
229     if ((result & I2C_CCR_CCR) == 0)\r
230     {\r
231       /* Set minimum allowed value */\r
232       result |= (uint16_t)0x0001;  \r
233     }\r
234     /* Set speed value and set F/S bit for fast mode */\r
235     tmpreg |= (uint16_t)(result | I2C_CCR_FS);\r
236     /* Set Maximum Rise Time for fast mode */\r
237     I2Cx->TRISE = (uint16_t)(((freqrange * (uint16_t)300) / (uint16_t)1000) + (uint16_t)1);  \r
238   }\r
239 \r
240   /* Write to I2Cx CCR */\r
241   I2Cx->CCR = tmpreg;\r
242   /* Enable the selected I2C peripheral */\r
243   I2Cx->CR1 |= I2C_CR1_PE;\r
244 \r
245 /*---------------------------- I2Cx CR1 Configuration ------------------------*/\r
246   /* Get the I2Cx CR1 value */\r
247   tmpreg = I2Cx->CR1;\r
248   /* Clear ACK, SMBTYPE and  SMBUS bits */\r
249   tmpreg &= CR1_CLEAR_MASK;\r
250   /* Configure I2Cx: mode and acknowledgement */\r
251   /* Set SMBTYPE and SMBUS bits according to I2C_Mode value */\r
252   /* Set ACK bit according to I2C_Ack value */\r
253   tmpreg |= (uint16_t)((uint32_t)I2C_InitStruct->I2C_Mode | I2C_InitStruct->I2C_Ack);\r
254   /* Write to I2Cx CR1 */\r
255   I2Cx->CR1 = tmpreg;\r
256 \r
257 /*---------------------------- I2Cx OAR1 Configuration -----------------------*/\r
258   /* Set I2Cx Own Address1 and acknowledged address */\r
259   I2Cx->OAR1 = (I2C_InitStruct->I2C_AcknowledgedAddress | I2C_InitStruct->I2C_OwnAddress1);\r
260 }\r
261 \r
262 /**\r
263   * @brief  Fills each I2C_InitStruct member with its default value.\r
264   * @param  I2C_InitStruct: pointer to an I2C_InitTypeDef structure which will be initialized.\r
265   * @retval None\r
266   */\r
267 void I2C_StructInit(I2C_InitTypeDef* I2C_InitStruct)\r
268 {\r
269 /*---------------- Reset I2C init structure parameters values ----------------*/\r
270   /* initialize the I2C_ClockSpeed member */\r
271   I2C_InitStruct->I2C_ClockSpeed = 5000;\r
272   /* Initialize the I2C_Mode member */\r
273   I2C_InitStruct->I2C_Mode = I2C_Mode_I2C;\r
274   /* Initialize the I2C_DutyCycle member */\r
275   I2C_InitStruct->I2C_DutyCycle = I2C_DutyCycle_2;\r
276   /* Initialize the I2C_OwnAddress1 member */\r
277   I2C_InitStruct->I2C_OwnAddress1 = 0;\r
278   /* Initialize the I2C_Ack member */\r
279   I2C_InitStruct->I2C_Ack = I2C_Ack_Disable;\r
280   /* Initialize the I2C_AcknowledgedAddress member */\r
281   I2C_InitStruct->I2C_AcknowledgedAddress = I2C_AcknowledgedAddress_7bit;\r
282 }\r
283 \r
284 /**\r
285   * @brief  Enables or disables the specified I2C peripheral.\r
286   * @param  I2Cx: where x can be 1 or 2 to select the I2C peripheral.\r
287   * @param  NewState: new state of the I2Cx peripheral. \r
288   *   This parameter can be: ENABLE or DISABLE.\r
289   * @retval None\r
290   */\r
291 void I2C_Cmd(I2C_TypeDef* I2Cx, FunctionalState NewState)\r
292 {\r
293   /* Check the parameters */\r
294   assert_param(IS_I2C_ALL_PERIPH(I2Cx));\r
295   assert_param(IS_FUNCTIONAL_STATE(NewState));\r
296   if (NewState != DISABLE)\r
297   {\r
298     /* Enable the selected I2C peripheral */\r
299     I2Cx->CR1 |= I2C_CR1_PE;\r
300   }\r
301   else\r
302   {\r
303     /* Disable the selected I2C peripheral */\r
304     I2Cx->CR1 &= (uint16_t)~((uint16_t)I2C_CR1_PE);\r
305   }\r
306 }\r
307 \r
308 /**\r
309   * @brief  Generates I2Cx communication START condition.\r
310   * @param  I2Cx: where x can be 1 or 2 to select the I2C peripheral.\r
311   * @param  NewState: new state of the I2C START condition generation.\r
312   *   This parameter can be: ENABLE or DISABLE.\r
313   * @retval None.\r
314   */\r
315 void I2C_GenerateSTART(I2C_TypeDef* I2Cx, FunctionalState NewState)\r
316 {\r
317   /* Check the parameters */\r
318   assert_param(IS_I2C_ALL_PERIPH(I2Cx));\r
319   assert_param(IS_FUNCTIONAL_STATE(NewState));\r
320   if (NewState != DISABLE)\r
321   {\r
322     /* Generate a START condition */\r
323     I2Cx->CR1 |= I2C_CR1_START;\r
324   }\r
325   else\r
326   {\r
327     /* Disable the START condition generation */\r
328     I2Cx->CR1 &= (uint16_t)~((uint16_t)I2C_CR1_START);\r
329   }\r
330 }\r
331 \r
332 /**\r
333   * @brief  Generates I2Cx communication STOP condition.\r
334   * @param  I2Cx: where x can be 1 or 2 to select the I2C peripheral.\r
335   * @param  NewState: new state of the I2C STOP condition generation.\r
336   *   This parameter can be: ENABLE or DISABLE.\r
337   * @retval None.\r
338   */\r
339 void I2C_GenerateSTOP(I2C_TypeDef* I2Cx, FunctionalState NewState)\r
340 {\r
341   /* Check the parameters */\r
342   assert_param(IS_I2C_ALL_PERIPH(I2Cx));\r
343   assert_param(IS_FUNCTIONAL_STATE(NewState));\r
344   if (NewState != DISABLE)\r
345   {\r
346     /* Generate a STOP condition */\r
347     I2Cx->CR1 |= I2C_CR1_STOP;\r
348   }\r
349   else\r
350   {\r
351     /* Disable the STOP condition generation */\r
352     I2Cx->CR1 &= (uint16_t)~((uint16_t)I2C_CR1_STOP);\r
353   }\r
354 }\r
355 \r
356 /**\r
357   * @brief  Enables or disables the specified I2C acknowledge feature.\r
358   * @param  I2Cx: where x can be 1 or 2 to select the I2C peripheral.\r
359   * @param  NewState: new state of the I2C Acknowledgement.\r
360   *   This parameter can be: ENABLE or DISABLE.\r
361   * @retval None.\r
362   */\r
363 void I2C_AcknowledgeConfig(I2C_TypeDef* I2Cx, FunctionalState NewState)\r
364 {\r
365   /* Check the parameters */\r
366   assert_param(IS_I2C_ALL_PERIPH(I2Cx));\r
367   assert_param(IS_FUNCTIONAL_STATE(NewState));\r
368   if (NewState != DISABLE)\r
369   {\r
370     /* Enable the acknowledgement */\r
371     I2Cx->CR1 |= I2C_CR1_ACK;\r
372   }\r
373   else\r
374   {\r
375     /* Disable the acknowledgement */\r
376     I2Cx->CR1 &= (uint16_t)~((uint16_t)I2C_CR1_ACK);\r
377   }\r
378 }\r
379 \r
380 /**\r
381   * @brief  Configures the specified I2C own address2.\r
382   * @param  I2Cx: where x can be 1 or 2 to select the I2C peripheral.\r
383   * @param  Address: specifies the 7bit I2C own address2.\r
384   * @retval None.\r
385   */\r
386 void I2C_OwnAddress2Config(I2C_TypeDef* I2Cx, uint8_t Address)\r
387 {\r
388   uint16_t tmpreg = 0;\r
389 \r
390   /* Check the parameters */\r
391   assert_param(IS_I2C_ALL_PERIPH(I2Cx));\r
392 \r
393   /* Get the old register value */\r
394   tmpreg = I2Cx->OAR2;\r
395 \r
396   /* Reset I2Cx Own address2 bit [7:1] */\r
397   tmpreg &= (uint16_t)~((uint16_t)I2C_OAR2_ADD2);\r
398 \r
399   /* Set I2Cx Own address2 */\r
400   tmpreg |= (uint16_t)((uint16_t)Address & (uint16_t)0x00FE);\r
401 \r
402   /* Store the new register value */\r
403   I2Cx->OAR2 = tmpreg;\r
404 }\r
405 \r
406 /**\r
407   * @brief  Enables or disables the specified I2C dual addressing mode.\r
408   * @param  I2Cx: where x can be 1 or 2 to select the I2C peripheral.\r
409   * @param  NewState: new state of the I2C dual addressing mode.\r
410   *   This parameter can be: ENABLE or DISABLE.\r
411   * @retval None\r
412   */\r
413 void I2C_DualAddressCmd(I2C_TypeDef* I2Cx, FunctionalState NewState)\r
414 {\r
415   /* Check the parameters */\r
416   assert_param(IS_I2C_ALL_PERIPH(I2Cx));\r
417   assert_param(IS_FUNCTIONAL_STATE(NewState));\r
418   if (NewState != DISABLE)\r
419   {\r
420     /* Enable dual addressing mode */\r
421     I2Cx->OAR2 |= I2C_OAR2_ENDUAL;\r
422   }\r
423   else\r
424   {\r
425     /* Disable dual addressing mode */\r
426     I2Cx->OAR2 &= (uint16_t)~((uint16_t)I2C_OAR2_ENDUAL);\r
427   }\r
428 }\r
429 \r
430 /**\r
431   * @brief  Enables or disables the specified I2C general call feature.\r
432   * @param  I2Cx: where x can be 1 or 2 to select the I2C peripheral.\r
433   * @param  NewState: new state of the I2C General call.\r
434   *   This parameter can be: ENABLE or DISABLE.\r
435   * @retval None\r
436   */\r
437 void I2C_GeneralCallCmd(I2C_TypeDef* I2Cx, FunctionalState NewState)\r
438 {\r
439   /* Check the parameters */\r
440   assert_param(IS_I2C_ALL_PERIPH(I2Cx));\r
441   assert_param(IS_FUNCTIONAL_STATE(NewState));\r
442   if (NewState != DISABLE)\r
443   {\r
444     /* Enable generall call */\r
445     I2Cx->CR1 |= I2C_CR1_ENGC;\r
446   }\r
447   else\r
448   {\r
449     /* Disable generall call */\r
450     I2Cx->CR1 &= (uint16_t)~((uint16_t)I2C_CR1_ENGC);\r
451   }\r
452 }\r
453 \r
454 /**\r
455   * @brief  Enables or disables the specified I2C software reset.\r
456   * @param  I2Cx: where x can be 1 or 2 to select the I2C peripheral.\r
457   * @param  NewState: new state of the I2C software reset.\r
458   *   This parameter can be: ENABLE or DISABLE.\r
459   * @retval None\r
460   */\r
461 void I2C_SoftwareResetCmd(I2C_TypeDef* I2Cx, FunctionalState NewState)\r
462 {\r
463   /* Check the parameters */\r
464   assert_param(IS_I2C_ALL_PERIPH(I2Cx));\r
465   assert_param(IS_FUNCTIONAL_STATE(NewState));\r
466   if (NewState != DISABLE)\r
467   {\r
468     /* Peripheral under reset */\r
469     I2Cx->CR1 |= I2C_CR1_SWRST;\r
470   }\r
471   else\r
472   {\r
473     /* Peripheral not under reset */\r
474     I2Cx->CR1 &= (uint16_t)~((uint16_t)I2C_CR1_SWRST);\r
475   }\r
476 }\r
477 \r
478 /**\r
479   * @brief  Drives the SMBusAlert pin high or low for the specified I2C.\r
480   * @param  I2Cx: where x can be 1 or 2 to select the I2C peripheral.\r
481   * @param  I2C_SMBusAlert: specifies SMBAlert pin level. \r
482   *   This parameter can be one of the following values:\r
483   *     @arg I2C_SMBusAlert_Low: SMBAlert pin driven low\r
484   *     @arg I2C_SMBusAlert_High: SMBAlert pin driven high\r
485   * @retval None\r
486   */\r
487 void I2C_SMBusAlertConfig(I2C_TypeDef* I2Cx, uint16_t I2C_SMBusAlert)\r
488 {\r
489   /* Check the parameters */\r
490   assert_param(IS_I2C_ALL_PERIPH(I2Cx));\r
491   assert_param(IS_I2C_SMBUS_ALERT(I2C_SMBusAlert));\r
492   if (I2C_SMBusAlert == I2C_SMBusAlert_Low)\r
493   {\r
494     /* Drive the SMBusAlert pin Low */\r
495     I2Cx->CR1 |= I2C_SMBusAlert_Low;\r
496   }\r
497   else\r
498   {\r
499     /* Drive the SMBusAlert pin High  */\r
500     I2Cx->CR1 &= I2C_SMBusAlert_High;\r
501   }\r
502 }\r
503 \r
504 /**\r
505   * @brief  Enables or disables the specified I2C ARP.\r
506   * @param  I2Cx: where x can be 1 or 2 to select the I2C peripheral.\r
507   * @param  NewState: new state of the I2Cx ARP. \r
508   *   This parameter can be: ENABLE or DISABLE.\r
509   * @retval None\r
510   */\r
511 void I2C_ARPCmd(I2C_TypeDef* I2Cx, FunctionalState NewState)\r
512 {\r
513   /* Check the parameters */\r
514   assert_param(IS_I2C_ALL_PERIPH(I2Cx));\r
515   assert_param(IS_FUNCTIONAL_STATE(NewState));\r
516   if (NewState != DISABLE)\r
517   {\r
518     /* Enable the selected I2C ARP */\r
519     I2Cx->CR1 |= I2C_CR1_ENARP;\r
520   }\r
521   else\r
522   {\r
523     /* Disable the selected I2C ARP */\r
524     I2Cx->CR1 &= (uint16_t)~((uint16_t)I2C_CR1_ENARP);\r
525   }\r
526 }\r
527 \r
528 /**\r
529   * @brief  Enables or disables the specified I2C Clock stretching.\r
530   * @param  I2Cx: where x can be 1 or 2 to select the I2C peripheral.\r
531   * @param  NewState: new state of the I2Cx Clock stretching.\r
532   *   This parameter can be: ENABLE or DISABLE.\r
533   * @retval None\r
534   */\r
535 void I2C_StretchClockCmd(I2C_TypeDef* I2Cx, FunctionalState NewState)\r
536 {\r
537   /* Check the parameters */\r
538   assert_param(IS_I2C_ALL_PERIPH(I2Cx));\r
539   assert_param(IS_FUNCTIONAL_STATE(NewState));\r
540   if (NewState == DISABLE)\r
541   {\r
542     /* Enable the selected I2C Clock stretching */\r
543     I2Cx->CR1 |= I2C_CR1_NOSTRETCH;\r
544   }\r
545   else\r
546   {\r
547     /* Disable the selected I2C Clock stretching */\r
548     I2Cx->CR1 &= (uint16_t)~((uint16_t)I2C_CR1_NOSTRETCH);\r
549   }\r
550 }\r
551 \r
552 /**\r
553   * @brief  Selects the specified I2C fast mode duty cycle.\r
554   * @param  I2Cx: where x can be 1 or 2 to select the I2C peripheral.\r
555   * @param  I2C_DutyCycle: specifies the fast mode duty cycle.\r
556   *   This parameter can be one of the following values:\r
557   *     @arg I2C_DutyCycle_2: I2C fast mode Tlow/Thigh = 2\r
558   *     @arg I2C_DutyCycle_16_9: I2C fast mode Tlow/Thigh = 16/9\r
559   * @retval None\r
560   */\r
561 void I2C_FastModeDutyCycleConfig(I2C_TypeDef* I2Cx, uint16_t I2C_DutyCycle)\r
562 {\r
563   /* Check the parameters */\r
564   assert_param(IS_I2C_ALL_PERIPH(I2Cx));\r
565   assert_param(IS_I2C_DUTY_CYCLE(I2C_DutyCycle));\r
566   if (I2C_DutyCycle != I2C_DutyCycle_16_9)\r
567   {\r
568     /* I2C fast mode Tlow/Thigh=2 */\r
569     I2Cx->CCR &= I2C_DutyCycle_2;\r
570   }\r
571   else\r
572   {\r
573     /* I2C fast mode Tlow/Thigh=16/9 */\r
574     I2Cx->CCR |= I2C_DutyCycle_16_9;\r
575   }\r
576 }\r
577 \r
578 /**\r
579   * @brief  Transmits the address byte to select the slave device.\r
580   * @param  I2Cx: where x can be 1 or 2 to select the I2C peripheral.\r
581   * @param  Address: specifies the slave address which will be transmitted\r
582   * @param  I2C_Direction: specifies whether the I2C device will be a\r
583   *   Transmitter or a Receiver. This parameter can be one of the following values\r
584   *     @arg I2C_Direction_Transmitter: Transmitter mode\r
585   *     @arg I2C_Direction_Receiver: Receiver mode\r
586   * @retval None.\r
587   */\r
588 void I2C_Send7bitAddress(I2C_TypeDef* I2Cx, uint8_t Address, uint8_t I2C_Direction)\r
589 {\r
590   /* Check the parameters */\r
591   assert_param(IS_I2C_ALL_PERIPH(I2Cx));\r
592   assert_param(IS_I2C_DIRECTION(I2C_Direction));\r
593   /* Test on the direction to set/reset the read/write bit */\r
594   if (I2C_Direction != I2C_Direction_Transmitter)\r
595   {\r
596     /* Set the address bit0 for read */\r
597     Address |= I2C_OAR1_ADD0;\r
598   }\r
599   else\r
600   {\r
601     /* Reset the address bit0 for write */\r
602     Address &= (uint8_t)~((uint8_t)I2C_OAR1_ADD0);\r
603   }\r
604   /* Send the address */\r
605   I2Cx->DR = Address;\r
606 }\r
607 \r
608 /**\r
609   * @}\r
610   */\r
611 \r
612 /** @defgroup I2C_Group2 Data transfers functions\r
613  *  @brief   Data transfers functions \r
614  *\r
615 @verbatim   \r
616  ===============================================================================\r
617                         Data transfers functions\r
618  ===============================================================================  \r
619 \r
620 @endverbatim\r
621   * @{\r
622   */\r
623 \r
624 /**\r
625   * @brief  Sends a data byte through the I2Cx peripheral.\r
626   * @param  I2Cx: where x can be 1 or 2 to select the I2C peripheral.\r
627   * @param  Data: Byte to be transmitted..\r
628   * @retval None\r
629   */\r
630 void I2C_SendData(I2C_TypeDef* I2Cx, uint8_t Data)\r
631 {\r
632   /* Check the parameters */\r
633   assert_param(IS_I2C_ALL_PERIPH(I2Cx));\r
634   /* Write in the DR register the data to be sent */\r
635   I2Cx->DR = Data;\r
636 }\r
637 \r
638 /**\r
639   * @brief  Returns the most recent received data by the I2Cx peripheral.\r
640   * @param  I2Cx: where x can be 1 or 2 to select the I2C peripheral.\r
641   * @retval The value of the received data.\r
642   */\r
643 uint8_t I2C_ReceiveData(I2C_TypeDef* I2Cx)\r
644 {\r
645   /* Check the parameters */\r
646   assert_param(IS_I2C_ALL_PERIPH(I2Cx));\r
647   /* Return the data in the DR register */\r
648   return (uint8_t)I2Cx->DR;\r
649 }\r
650 \r
651 /**\r
652   * @}\r
653   */\r
654 \r
655 /** @defgroup I2C_Group3 PEC management functions\r
656  *  @brief   PEC management functions \r
657  *\r
658 @verbatim   \r
659  ===============================================================================\r
660                          PEC management functions\r
661  ===============================================================================  \r
662 \r
663 @endverbatim\r
664   * @{\r
665   */\r
666 \r
667 /**\r
668   * @brief  Enables or disables the specified I2C PEC transfer.\r
669   * @param  I2Cx: where x can be 1 or 2 to select the I2C peripheral.\r
670   * @param  NewState: new state of the I2C PEC transmission.\r
671   *   This parameter can be: ENABLE or DISABLE.\r
672   * @retval None\r
673   */\r
674 void I2C_TransmitPEC(I2C_TypeDef* I2Cx, FunctionalState NewState)\r
675 {\r
676   /* Check the parameters */\r
677   assert_param(IS_I2C_ALL_PERIPH(I2Cx));\r
678   assert_param(IS_FUNCTIONAL_STATE(NewState));\r
679   if (NewState != DISABLE)\r
680   {\r
681     /* Enable the selected I2C PEC transmission */\r
682     I2Cx->CR1 |= I2C_CR1_PEC;\r
683   }\r
684   else\r
685   {\r
686     /* Disable the selected I2C PEC transmission */\r
687     I2Cx->CR1 &= (uint16_t)~((uint16_t)I2C_CR1_PEC);\r
688   }\r
689 }\r
690 \r
691 /**\r
692   * @brief  Selects the specified I2C PEC position.\r
693   * @param  I2Cx: where x can be 1 or 2 to select the I2C peripheral.\r
694   * @param  I2C_PECPosition: specifies the PEC position. \r
695   *   This parameter can be one of the following values:\r
696   *     @arg I2C_PECPosition_Next: indicates that the next byte is PEC\r
697   *     @arg I2C_PECPosition_Current: indicates that current byte is PEC\r
698   * @retval None\r
699   */\r
700 void I2C_PECPositionConfig(I2C_TypeDef* I2Cx, uint16_t I2C_PECPosition)\r
701 {\r
702   /* Check the parameters */\r
703   assert_param(IS_I2C_ALL_PERIPH(I2Cx));\r
704   assert_param(IS_I2C_PEC_POSITION(I2C_PECPosition));\r
705   if (I2C_PECPosition == I2C_PECPosition_Next)\r
706   {\r
707     /* Next byte in shift register is PEC */\r
708     I2Cx->CR1 |= I2C_PECPosition_Next;\r
709   }\r
710   else\r
711   {\r
712     /* Current byte in shift register is PEC */\r
713     I2Cx->CR1 &= I2C_PECPosition_Current;\r
714   }\r
715 }\r
716 \r
717 /**\r
718   * @brief  Enables or disables the PEC value calculation of the transferred bytes.\r
719   * @param  I2Cx: where x can be 1 or 2 to select the I2C peripheral.\r
720   * @param  NewState: new state of the I2Cx PEC value calculation.\r
721   *   This parameter can be: ENABLE or DISABLE.\r
722   * @retval None\r
723   */\r
724 void I2C_CalculatePEC(I2C_TypeDef* I2Cx, FunctionalState NewState)\r
725 {\r
726   /* Check the parameters */\r
727   assert_param(IS_I2C_ALL_PERIPH(I2Cx));\r
728   assert_param(IS_FUNCTIONAL_STATE(NewState));\r
729   if (NewState != DISABLE)\r
730   {\r
731     /* Enable the selected I2C PEC calculation */\r
732     I2Cx->CR1 |= I2C_CR1_ENPEC;\r
733   }\r
734   else\r
735   {\r
736     /* Disable the selected I2C PEC calculation */\r
737     I2Cx->CR1 &= (uint16_t)~((uint16_t)I2C_CR1_ENPEC);\r
738   }\r
739 }\r
740 \r
741 /**\r
742   * @brief  Returns the PEC value for the specified I2C.\r
743   * @param  I2Cx: where x can be 1 or 2 to select the I2C peripheral.\r
744   * @retval The PEC value.\r
745   */\r
746 uint8_t I2C_GetPEC(I2C_TypeDef* I2Cx)\r
747 {\r
748   /* Check the parameters */\r
749   assert_param(IS_I2C_ALL_PERIPH(I2Cx));\r
750   /* Return the selected I2C PEC value */\r
751   return ((I2Cx->SR2) >> 8);\r
752 }\r
753 \r
754 /**\r
755   * @}\r
756   */\r
757 \r
758 /** @defgroup I2C_Group4 DMA transfers management functions\r
759  *  @brief   DMA transfers management functions \r
760  *\r
761 @verbatim   \r
762  ===============================================================================\r
763                          DMA transfers management functions\r
764  ===============================================================================  \r
765   This section provides functions allowing to configure the I2C DMA channels \r
766   requests.\r
767   \r
768 @endverbatim\r
769   * @{\r
770   */\r
771 \r
772 /**\r
773   * @brief  Enables or disables the specified I2C DMA requests.\r
774   * @param  I2Cx: where x can be 1 or 2 to select the I2C peripheral.\r
775   * @param  NewState: new state of the I2C DMA transfer.\r
776   *   This parameter can be: ENABLE or DISABLE.\r
777   * @retval None\r
778   */\r
779 void I2C_DMACmd(I2C_TypeDef* I2Cx, FunctionalState NewState)\r
780 {\r
781   /* Check the parameters */\r
782   assert_param(IS_I2C_ALL_PERIPH(I2Cx));\r
783   assert_param(IS_FUNCTIONAL_STATE(NewState));\r
784   if (NewState != DISABLE)\r
785   {\r
786     /* Enable the selected I2C DMA requests */\r
787     I2Cx->CR2 |= I2C_CR2_DMAEN;\r
788   }\r
789   else\r
790   {\r
791     /* Disable the selected I2C DMA requests */\r
792     I2Cx->CR2 &= (uint16_t)~((uint16_t)I2C_CR2_DMAEN);\r
793   }\r
794 }\r
795 \r
796 /**\r
797   * @brief  Specifies that the next DMA transfer is the last one.\r
798   * @param  I2Cx: where x can be 1 or 2 to select the I2C peripheral.\r
799   * @param  NewState: new state of the I2C DMA last transfer.\r
800   *   This parameter can be: ENABLE or DISABLE.\r
801   * @retval None\r
802   */\r
803 void I2C_DMALastTransferCmd(I2C_TypeDef* I2Cx, FunctionalState NewState)\r
804 {\r
805   /* Check the parameters */\r
806   assert_param(IS_I2C_ALL_PERIPH(I2Cx));\r
807   assert_param(IS_FUNCTIONAL_STATE(NewState));\r
808   if (NewState != DISABLE)\r
809   {\r
810     /* Next DMA transfer is the last transfer */\r
811     I2Cx->CR2 |= I2C_CR2_LAST;\r
812   }\r
813   else\r
814   {\r
815     /* Next DMA transfer is not the last transfer */\r
816     I2Cx->CR2 &= (uint16_t)~((uint16_t)I2C_CR2_LAST);\r
817   }\r
818 }\r
819 \r
820 /**\r
821   * @}\r
822   */\r
823 \r
824 /** @defgroup I2C_Group5 Interrupts events and flags management functions\r
825  *  @brief   Interrupts, events and flags management functions\r
826  *\r
827 @verbatim   \r
828  ===============================================================================\r
829                 Interrupts, events and flags management functions\r
830  ===============================================================================  \r
831   This section provides functions allowing to configure the I2C Interrupts \r
832   sources and check or clear the flags or pending bits status.\r
833   The user should identify which mode will be used in his application to manage \r
834   the communication: Polling mode, Interrupt mode or DMA mode. \r
835 \r
836  ===============================================================================\r
837                           I2C State Monitoring Functions                    \r
838  ===============================================================================   \r
839   This I2C driver provides three different ways for I2C state monitoring\r
840   depending on the application requirements and constraints:\r
841          \r
842    \r
843      1. Basic state monitoring (Using I2C_CheckEvent() function)\r
844      -----------------------------------------------------------\r
845         It compares the status registers (SR1 and SR2) content to a given event\r
846         (can be the combination of one or more flags).\r
847         It returns SUCCESS if the current status includes the given flags \r
848         and returns ERROR if one or more flags are missing in the current status.\r
849 \r
850           - When to use\r
851              - This function is suitable for most applications as well as for startup \r
852                activity since the events are fully described in the product reference \r
853                manual (RM0038).\r
854              - It is also suitable for users who need to define their own events.\r
855 \r
856           - Limitations\r
857              - If an error occurs (ie. error flags are set besides to the monitored \r
858                flags), the I2C_CheckEvent() function may return SUCCESS despite \r
859                the communication hold or corrupted real state. \r
860                In this case, it is advised to use error interrupts to monitor \r
861                the error events and handle them in the interrupt IRQ handler.\r
862          \r
863      @note \r
864          For error management, it is advised to use the following functions:\r
865            - I2C_ITConfig() to configure and enable the error interrupts (I2C_IT_ERR).\r
866            - I2Cx_ER_IRQHandler() which is called when the error interrupt occurs.\r
867              Where x is the peripheral instance (I2C1, I2C2 ...)\r
868            - I2C_GetFlagStatus() or I2C_GetITStatus()  to be called into the \r
869              I2Cx_ER_IRQHandler() function in order to determine which error occurred.\r
870            - I2C_ClearFlag() or I2C_ClearITPendingBit() and/or I2C_SoftwareResetCmd() \r
871              and/or I2C_GenerateStop() in order to clear the error flag and source \r
872              and return to correct  communication status.\r
873              \r
874  \r
875      2. Advanced state monitoring (Using the function I2C_GetLastEvent())\r
876      -------------------------------------------------------------------- \r
877         Using the function I2C_GetLastEvent() which returns the image of both status \r
878         registers in a single word (uint32_t) (Status Register 2 value is shifted left \r
879         by 16 bits and concatenated to Status Register 1).\r
880 \r
881           - When to use\r
882              - This function is suitable for the same applications above but it \r
883                allows to overcome the mentioned limitation of I2C_GetFlagStatus() \r
884                function.\r
885              - The returned value could be compared to events already defined in \r
886                the library (stm32l1xx_i2c.h) or to custom values defined by user.\r
887                This function is suitable when multiple flags are monitored at the \r
888                same time.\r
889              - At the opposite of I2C_CheckEvent() function, this function allows \r
890                user to choose when an event is accepted (when all events flags are \r
891                set and no other flags are set or just when the needed flags are set \r
892                like I2C_CheckEvent() function.\r
893 \r
894           - Limitations\r
895              - User may need to define his own events.\r
896              - Same remark concerning the error management is applicable for this \r
897                function if user decides to check only regular communication flags \r
898                (and ignores error flags).\r
899       \r
900  \r
901      3. Flag-based state monitoring (Using the function I2C_GetFlagStatus())\r
902      -----------------------------------------------------------------------\r
903      \r
904       Using the function I2C_GetFlagStatus() which simply returns the status of \r
905       one single flag (ie. I2C_FLAG_RXNE ...). \r
906 \r
907           - When to use\r
908              - This function could be used for specific applications or in debug \r
909                phase.\r
910              - It is suitable when only one flag checking is needed (most I2C \r
911                events are monitored through multiple flags).\r
912           - Limitations: \r
913              - When calling this function, the Status register is accessed. \r
914                Some flags are cleared when the status register is accessed. \r
915                So checking the status of one Flag, may clear other ones.\r
916              - Function may need to be called twice or more in order to monitor \r
917                one single event.\r
918  \r
919    For detailed description of Events, please refer to section I2C_Events in \r
920    stm32l1xx_i2c.h file.\r
921        \r
922 @endverbatim\r
923   * @{\r
924   */\r
925    \r
926 /**\r
927   * @brief  Reads the specified I2C register and returns its value.\r
928   * @param  I2C_Register: specifies the register to read.\r
929   *   This parameter can be one of the following values:\r
930   *     @arg I2C_Register_CR1:  CR1 register.\r
931   *     @arg I2C_Register_CR2:   CR2 register.\r
932   *     @arg I2C_Register_OAR1:  OAR1 register.\r
933   *     @arg I2C_Register_OAR2:  OAR2 register.\r
934   *     @arg I2C_Register_DR:    DR register.\r
935   *     @arg I2C_Register_SR1:   SR1 register.\r
936   *     @arg I2C_Register_SR2:   SR2 register.\r
937   *     @arg I2C_Register_CCR:   CCR register.\r
938   *     @arg I2C_Register_TRISE: TRISE register.\r
939   * @retval The value of the read register.\r
940   */\r
941 uint16_t I2C_ReadRegister(I2C_TypeDef* I2Cx, uint8_t I2C_Register)\r
942 {\r
943   __IO uint32_t tmp = 0;\r
944 \r
945   /* Check the parameters */\r
946   assert_param(IS_I2C_ALL_PERIPH(I2Cx));\r
947   assert_param(IS_I2C_REGISTER(I2C_Register));\r
948 \r
949   tmp = (uint32_t) I2Cx;\r
950   tmp += I2C_Register;\r
951 \r
952   /* Return the selected register value */\r
953   return (*(__IO uint16_t *) tmp);\r
954 }\r
955 \r
956 /**\r
957   * @brief  Enables or disables the specified I2C interrupts.\r
958   * @param  I2Cx: where x can be 1 or 2 to select the I2C peripheral.\r
959   * @param  I2C_IT: specifies the I2C interrupts sources to be enabled or disabled. \r
960   *   This parameter can be any combination of the following values:\r
961   *     @arg I2C_IT_BUF: Buffer interrupt mask\r
962   *     @arg I2C_IT_EVT: Event interrupt mask\r
963   *     @arg I2C_IT_ERR: Error interrupt mask\r
964   * @param  NewState: new state of the specified I2C interrupts.\r
965   *   This parameter can be: ENABLE or DISABLE.\r
966   * @retval None\r
967   */\r
968 void I2C_ITConfig(I2C_TypeDef* I2Cx, uint16_t I2C_IT, FunctionalState NewState)\r
969 {\r
970   /* Check the parameters */\r
971   assert_param(IS_I2C_ALL_PERIPH(I2Cx));\r
972   assert_param(IS_FUNCTIONAL_STATE(NewState));\r
973   assert_param(IS_I2C_CONFIG_IT(I2C_IT));\r
974   \r
975   if (NewState != DISABLE)\r
976   {\r
977     /* Enable the selected I2C interrupts */\r
978     I2Cx->CR2 |= I2C_IT;\r
979   }\r
980   else\r
981   {\r
982     /* Disable the selected I2C interrupts */\r
983     I2Cx->CR2 &= (uint16_t)~I2C_IT;\r
984   }\r
985 }\r
986 \r
987 /**\r
988  ===============================================================================\r
989                           1. Basic state monitoring                    \r
990  ===============================================================================  \r
991  */\r
992 \r
993 /**\r
994   * @brief  Checks whether the last I2Cx Event is equal to the one passed\r
995   *   as parameter.\r
996   * @param  I2Cx: where x can be 1 or 2 to select the I2C peripheral.\r
997   * @param  I2C_EVENT: specifies the event to be checked. \r
998   *   This parameter can be one of the following values:\r
999   *     @arg I2C_EVENT_SLAVE_TRANSMITTER_ADDRESS_MATCHED           : EV1\r
1000   *     @arg I2C_EVENT_SLAVE_RECEIVER_ADDRESS_MATCHED              : EV1\r
1001   *     @arg I2C_EVENT_SLAVE_TRANSMITTER_SECONDADDRESS_MATCHED     : EV1\r
1002   *     @arg I2C_EVENT_SLAVE_RECEIVER_SECONDADDRESS_MATCHED        : EV1\r
1003   *     @arg I2C_EVENT_SLAVE_GENERALCALLADDRESS_MATCHED            : EV1\r
1004   *     @arg I2C_EVENT_SLAVE_BYTE_RECEIVED                         : EV2\r
1005   *     @arg (I2C_EVENT_SLAVE_BYTE_RECEIVED | I2C_FLAG_DUALF)      : EV2\r
1006   *     @arg (I2C_EVENT_SLAVE_BYTE_RECEIVED | I2C_FLAG_GENCALL)    : EV2\r
1007   *     @arg I2C_EVENT_SLAVE_BYTE_TRANSMITTED                      : EV3\r
1008   *     @arg (I2C_EVENT_SLAVE_BYTE_TRANSMITTED | I2C_FLAG_DUALF)   : EV3\r
1009   *     @arg (I2C_EVENT_SLAVE_BYTE_TRANSMITTED | I2C_FLAG_GENCALL) : EV3\r
1010   *     @arg I2C_EVENT_SLAVE_ACK_FAILURE                           : EV3_2\r
1011   *     @arg I2C_EVENT_SLAVE_STOP_DETECTED                         : EV4\r
1012   *     @arg I2C_EVENT_MASTER_MODE_SELECT                          : EV5\r
1013   *     @arg I2C_EVENT_MASTER_TRANSMITTER_MODE_SELECTED            : EV6     \r
1014   *     @arg I2C_EVENT_MASTER_RECEIVER_MODE_SELECTED               : EV6\r
1015   *     @arg I2C_EVENT_MASTER_BYTE_RECEIVED                        : EV7\r
1016   *     @arg I2C_EVENT_MASTER_BYTE_TRANSMITTING                    : EV8\r
1017   *     @arg I2C_EVENT_MASTER_BYTE_TRANSMITTED                     : EV8_2\r
1018   *     @arg I2C_EVENT_MASTER_MODE_ADDRESS10                       : EV9\r
1019   *     \r
1020   * @note: For detailed description of Events, please refer to section \r
1021   *    I2C_Events in stm32l1xx_i2c.h file.\r
1022   *    \r
1023   * @retval An ErrorStatus enumeration value:\r
1024   * - SUCCESS: Last event is equal to the I2C_EVENT\r
1025   * - ERROR: Last event is different from the I2C_EVENT\r
1026   */\r
1027 ErrorStatus I2C_CheckEvent(I2C_TypeDef* I2Cx, uint32_t I2C_EVENT)\r
1028 {\r
1029   uint32_t lastevent = 0;\r
1030   uint32_t flag1 = 0, flag2 = 0;\r
1031   ErrorStatus status = ERROR;\r
1032 \r
1033   /* Check the parameters */\r
1034   assert_param(IS_I2C_ALL_PERIPH(I2Cx));\r
1035   assert_param(IS_I2C_EVENT(I2C_EVENT));\r
1036 \r
1037   /* Read the I2Cx status register */\r
1038   flag1 = I2Cx->SR1;\r
1039   flag2 = I2Cx->SR2;\r
1040   flag2 = flag2 << 16;\r
1041 \r
1042   /* Get the last event value from I2C status register */\r
1043   lastevent = (flag1 | flag2) & FLAG_MASK;\r
1044 \r
1045   /* Check whether the last event contains the I2C_EVENT */\r
1046   if ((lastevent & I2C_EVENT) == I2C_EVENT)\r
1047   {\r
1048     /* SUCCESS: last event is equal to I2C_EVENT */\r
1049     status = SUCCESS;\r
1050   }\r
1051   else\r
1052   {\r
1053     /* ERROR: last event is different from I2C_EVENT */\r
1054     status = ERROR;\r
1055   }\r
1056   /* Return status */\r
1057   return status;\r
1058 }\r
1059 \r
1060 /**\r
1061  ===============================================================================\r
1062                           2. Advanced state monitoring                   \r
1063  ===============================================================================  \r
1064  */\r
1065 \r
1066 /**\r
1067   * @brief  Returns the last I2Cx Event.\r
1068   * @param  I2Cx: where x can be 1 or 2 to select the I2C peripheral.\r
1069   *     \r
1070   * @note: For detailed description of Events, please refer to section \r
1071   *    I2C_Events in stm32l1xx_i2c.h file.\r
1072   *    \r
1073   * @retval The last event\r
1074   */\r
1075 uint32_t I2C_GetLastEvent(I2C_TypeDef* I2Cx)\r
1076 {\r
1077   uint32_t lastevent = 0;\r
1078   uint32_t flag1 = 0, flag2 = 0;\r
1079 \r
1080   /* Check the parameters */\r
1081   assert_param(IS_I2C_ALL_PERIPH(I2Cx));\r
1082 \r
1083   /* Read the I2Cx status register */\r
1084   flag1 = I2Cx->SR1;\r
1085   flag2 = I2Cx->SR2;\r
1086   flag2 = flag2 << 16;\r
1087 \r
1088   /* Get the last event value from I2C status register */\r
1089   lastevent = (flag1 | flag2) & FLAG_MASK;\r
1090 \r
1091   /* Return status */\r
1092   return lastevent;\r
1093 }\r
1094 \r
1095 /**\r
1096  ===============================================================================\r
1097                           3. Flag-based state monitoring                   \r
1098  ===============================================================================  \r
1099  */\r
1100 \r
1101 /**\r
1102   * @brief  Checks whether the specified I2C flag is set or not.\r
1103   * @param  I2Cx: where x can be 1 or 2 to select the I2C peripheral.\r
1104   * @param  I2C_FLAG: specifies the flag to check. \r
1105   *   This parameter can be one of the following values:\r
1106   *     @arg I2C_FLAG_DUALF: Dual flag (Slave mode)\r
1107   *     @arg I2C_FLAG_SMBHOST: SMBus host header (Slave mode)\r
1108   *     @arg I2C_FLAG_SMBDEFAULT: SMBus default header (Slave mode)\r
1109   *     @arg I2C_FLAG_GENCALL: General call header flag (Slave mode)\r
1110   *     @arg I2C_FLAG_TRA: Transmitter/Receiver flag\r
1111   *     @arg I2C_FLAG_BUSY: Bus busy flag\r
1112   *     @arg I2C_FLAG_MSL: Master/Slave flag\r
1113   *     @arg I2C_FLAG_SMBALERT: SMBus Alert flag\r
1114   *     @arg I2C_FLAG_TIMEOUT: Timeout or Tlow error flag\r
1115   *     @arg I2C_FLAG_PECERR: PEC error in reception flag\r
1116   *     @arg I2C_FLAG_OVR: Overrun/Underrun flag (Slave mode)\r
1117   *     @arg I2C_FLAG_AF: Acknowledge failure flag\r
1118   *     @arg I2C_FLAG_ARLO: Arbitration lost flag (Master mode)\r
1119   *     @arg I2C_FLAG_BERR: Bus error flag\r
1120   *     @arg I2C_FLAG_TXE: Data register empty flag (Transmitter)\r
1121   *     @arg I2C_FLAG_RXNE: Data register not empty (Receiver) flag\r
1122   *     @arg I2C_FLAG_STOPF: Stop detection flag (Slave mode)\r
1123   *     @arg I2C_FLAG_ADD10: 10-bit header sent flag (Master mode)\r
1124   *     @arg I2C_FLAG_BTF: Byte transfer finished flag\r
1125   *     @arg I2C_FLAG_ADDR: Address sent flag (Master mode) \93ADSL\94\r
1126   *   Address matched flag (Slave mode)\94ENDAD\94\r
1127   *     @arg I2C_FLAG_SB: Start bit flag (Master mode)\r
1128   * @retval The new state of I2C_FLAG (SET or RESET).\r
1129   */\r
1130 FlagStatus I2C_GetFlagStatus(I2C_TypeDef* I2Cx, uint32_t I2C_FLAG)\r
1131 {\r
1132   FlagStatus bitstatus = RESET;\r
1133   __IO uint32_t i2creg = 0, i2cxbase = 0;\r
1134 \r
1135   /* Check the parameters */\r
1136   assert_param(IS_I2C_ALL_PERIPH(I2Cx));\r
1137   assert_param(IS_I2C_GET_FLAG(I2C_FLAG));\r
1138 \r
1139   /* Get the I2Cx peripheral base address */\r
1140   i2cxbase = (uint32_t)I2Cx;\r
1141   \r
1142   /* Read flag register index */\r
1143   i2creg = I2C_FLAG >> 28;\r
1144   \r
1145   /* Get bit[23:0] of the flag */\r
1146   I2C_FLAG &= FLAG_MASK;\r
1147   \r
1148   if(i2creg != 0)\r
1149   {\r
1150     /* Get the I2Cx SR1 register address */\r
1151     i2cxbase += 0x14;\r
1152   }\r
1153   else\r
1154   {\r
1155     /* Flag in I2Cx SR2 Register */\r
1156     I2C_FLAG = (uint32_t)(I2C_FLAG >> 16);\r
1157     /* Get the I2Cx SR2 register address */\r
1158     i2cxbase += 0x18;\r
1159   }\r
1160   \r
1161   if(((*(__IO uint32_t *)i2cxbase) & I2C_FLAG) != (uint32_t)RESET)\r
1162   {\r
1163     /* I2C_FLAG is set */\r
1164     bitstatus = SET;\r
1165   }\r
1166   else\r
1167   {\r
1168     /* I2C_FLAG is reset */\r
1169     bitstatus = RESET;\r
1170   }\r
1171   \r
1172   /* Return the I2C_FLAG status */\r
1173   return  bitstatus;\r
1174 }\r
1175 \r
1176 /**\r
1177   * @brief  Clears the I2Cx's pending flags.\r
1178   * @param  I2Cx: where x can be 1 or 2 to select the I2C peripheral.\r
1179   * @param  I2C_FLAG: specifies the flag to clear. \r
1180   *   This parameter can be any combination of the following values:\r
1181   *     @arg I2C_FLAG_SMBALERT: SMBus Alert flag\r
1182   *     @arg I2C_FLAG_TIMEOUT: Timeout or Tlow error flag\r
1183   *     @arg I2C_FLAG_PECERR: PEC error in reception flag\r
1184   *     @arg I2C_FLAG_OVR: Overrun/Underrun flag (Slave mode)\r
1185   *     @arg I2C_FLAG_AF: Acknowledge failure flag\r
1186   *     @arg I2C_FLAG_ARLO: Arbitration lost flag (Master mode)\r
1187   *     @arg I2C_FLAG_BERR: Bus error flag\r
1188   *   \r
1189   * @note\r
1190   *   - STOPF (STOP detection) is cleared by software sequence: a read operation \r
1191   *     to I2C_SR1 register (I2C_GetFlagStatus()) followed by a write operation \r
1192   *     to I2C_CR1 register (I2C_Cmd() to re-enable the I2C peripheral).\r
1193   *   - ADD10 (10-bit header sent) is cleared by software sequence: a read \r
1194   *     operation to I2C_SR1 (I2C_GetFlagStatus()) followed by writing the \r
1195   *     second byte of the address in DR register.\r
1196   *   - BTF (Byte Transfer Finished) is cleared by software sequence: a read \r
1197   *     operation to I2C_SR1 register (I2C_GetFlagStatus()) followed by a \r
1198   *     read/write to I2C_DR register (I2C_SendData()).\r
1199   *   - ADDR (Address sent) is cleared by software sequence: a read operation to \r
1200   *     I2C_SR1 register (I2C_GetFlagStatus()) followed by a read operation to \r
1201   *     I2C_SR2 register ((void)(I2Cx->SR2)).\r
1202   *   - SB (Start Bit) is cleared software sequence: a read operation to I2C_SR1\r
1203   *     register (I2C_GetFlagStatus()) followed by a write operation to I2C_DR\r
1204   *     register  (I2C_SendData()).\r
1205   * @retval None\r
1206   */\r
1207 void I2C_ClearFlag(I2C_TypeDef* I2Cx, uint32_t I2C_FLAG)\r
1208 {\r
1209   uint32_t flagpos = 0;\r
1210   /* Check the parameters */\r
1211   assert_param(IS_I2C_ALL_PERIPH(I2Cx));\r
1212   assert_param(IS_I2C_CLEAR_FLAG(I2C_FLAG));\r
1213   /* Get the I2C flag position */\r
1214   flagpos = I2C_FLAG & FLAG_MASK;\r
1215   /* Clear the selected I2C flag */\r
1216   I2Cx->SR1 = (uint16_t)~flagpos;\r
1217 }\r
1218 \r
1219 /**\r
1220   * @brief  Checks whether the specified I2C interrupt has occurred or not.\r
1221   * @param  I2Cx: where x can be 1 or 2 to select the I2C peripheral.\r
1222   * @param  I2C_IT: specifies the interrupt source to check. \r
1223   *   This parameter can be one of the following values:\r
1224   *     @arg I2C_IT_SMBALERT: SMBus Alert flag\r
1225   *     @arg I2C_IT_TIMEOUT: Timeout or Tlow error flag\r
1226   *     @arg I2C_IT_PECERR: PEC error in reception flag\r
1227   *     @arg I2C_IT_OVR: Overrun/Underrun flag (Slave mode)\r
1228   *     @arg I2C_IT_AF: Acknowledge failure flag\r
1229   *     @arg I2C_IT_ARLO: Arbitration lost flag (Master mode)\r
1230   *     @arg I2C_IT_BERR: Bus error flag\r
1231   *     @arg I2C_IT_TXE: Data register empty flag (Transmitter)\r
1232   *     @arg I2C_IT_RXNE: Data register not empty (Receiver) flag\r
1233   *     @arg I2C_IT_STOPF: Stop detection flag (Slave mode)\r
1234   *     @arg I2C_IT_ADD10: 10-bit header sent flag (Master mode)\r
1235   *     @arg I2C_IT_BTF: Byte transfer finished flag\r
1236   *     @arg I2C_IT_ADDR: Address sent flag (Master mode) \93ADSL\94\r
1237   *                       Address matched flag (Slave mode)\94ENDAD\94\r
1238   *     @arg I2C_IT_SB: Start bit flag (Master mode)\r
1239   * @retval The new state of I2C_IT (SET or RESET).\r
1240   */\r
1241 ITStatus I2C_GetITStatus(I2C_TypeDef* I2Cx, uint32_t I2C_IT)\r
1242 {\r
1243   ITStatus bitstatus = RESET;\r
1244   uint32_t enablestatus = 0;\r
1245 \r
1246   /* Check the parameters */\r
1247   assert_param(IS_I2C_ALL_PERIPH(I2Cx));\r
1248   assert_param(IS_I2C_GET_IT(I2C_IT));\r
1249 \r
1250   /* Check if the interrupt source is enabled or not */\r
1251   enablestatus = (uint32_t)(((I2C_IT & ITEN_MASK) >> 16) & (I2Cx->CR2)) ;\r
1252   \r
1253   /* Get bit[23:0] of the flag */\r
1254   I2C_IT &= FLAG_MASK;\r
1255 \r
1256   /* Check the status of the specified I2C flag */\r
1257   if (((I2Cx->SR1 & I2C_IT) != (uint32_t)RESET) && enablestatus)\r
1258   {\r
1259     /* I2C_IT is set */\r
1260     bitstatus = SET;\r
1261   }\r
1262   else\r
1263   {\r
1264     /* I2C_IT is reset */\r
1265     bitstatus = RESET;\r
1266   }\r
1267   /* Return the I2C_IT status */\r
1268   return  bitstatus;\r
1269 }\r
1270 \r
1271 /**\r
1272   * @brief  Clears the I2Cx\92s interrupt pending bits.\r
1273   * @param  I2Cx: where x can be 1 or 2 to select the I2C peripheral.\r
1274   * @param  I2C_IT: specifies the interrupt pending bit to clear. \r
1275   *   This parameter can be any combination of the following values:\r
1276   *     @arg I2C_IT_SMBALERT: SMBus Alert interrupt\r
1277   *     @arg I2C_IT_TIMEOUT: Timeout or Tlow error interrupt\r
1278   *     @arg I2C_IT_PECERR: PEC error in reception  interrupt\r
1279   *     @arg I2C_IT_OVR: Overrun/Underrun interrupt (Slave mode)\r
1280   *     @arg I2C_IT_AF: Acknowledge failure interrupt\r
1281   *     @arg I2C_IT_ARLO: Arbitration lost interrupt (Master mode)\r
1282   *     @arg I2C_IT_BERR: Bus error interrupt\r
1283   *   \r
1284   * @note\r
1285   *   - STOPF (STOP detection) is cleared by software sequence: a read operation \r
1286   *     to I2C_SR1 register (I2C_GetITStatus()) followed by a write operation to \r
1287   *     I2C_CR1 register (I2C_Cmd() to re-enable the I2C peripheral).\r
1288   *   - ADD10 (10-bit header sent) is cleared by software sequence: a read \r
1289   *     operation to I2C_SR1 (I2C_GetITStatus()) followed by writing the second \r
1290   *     byte of the address in I2C_DR register.\r
1291   *   - BTF (Byte Transfer Finished) is cleared by software sequence: a read \r
1292   *     operation to I2C_SR1 register (I2C_GetITStatus()) followed by a \r
1293   *     read/write to I2C_DR register (I2C_SendData()).\r
1294   *   - ADDR (Address sent) is cleared by software sequence: a read operation to \r
1295   *     I2C_SR1 register (I2C_GetITStatus()) followed by a read operation to \r
1296   *     I2C_SR2 register ((void)(I2Cx->SR2)).\r
1297   *   - SB (Start Bit) is cleared by software sequence: a read operation to \r
1298   *     I2C_SR1 register (I2C_GetITStatus()) followed by a write operation to \r
1299   *     I2C_DR register (I2C_SendData()).\r
1300   * @retval None\r
1301   */\r
1302 void I2C_ClearITPendingBit(I2C_TypeDef* I2Cx, uint32_t I2C_IT)\r
1303 {\r
1304   uint32_t flagpos = 0;\r
1305   /* Check the parameters */\r
1306   assert_param(IS_I2C_ALL_PERIPH(I2Cx));\r
1307   assert_param(IS_I2C_CLEAR_IT(I2C_IT));\r
1308   /* Get the I2C flag position */\r
1309   flagpos = I2C_IT & FLAG_MASK;\r
1310   /* Clear the selected I2C flag */\r
1311   I2Cx->SR1 = (uint16_t)~flagpos;\r
1312 }\r
1313 \r
1314 /**\r
1315   * @}\r
1316   */\r
1317 \r
1318 /**\r
1319   * @}\r
1320   */ \r
1321 \r
1322 /**\r
1323   * @}\r
1324   */ \r
1325 \r
1326 /**\r
1327   * @}\r
1328   */ \r
1329 \r
1330 /******************* (C) COPYRIGHT 2010 STMicroelectronics *****END OF FILE****/\r
1331 \r
1332 \r
1333 \r