]> git.sur5r.net Git - freertos/blob - Demo/CORTEX_STM32L152_IAR/system_and_ST_code/STM32L1xx_StdPeriph_Driver/inc/stm32l1xx_i2c.h
Start to re-arrange files to include FreeRTOS+ in main download.
[freertos] / Demo / CORTEX_STM32L152_IAR / system_and_ST_code / STM32L1xx_StdPeriph_Driver / inc / stm32l1xx_i2c.h
1 /**\r
2   ******************************************************************************\r
3   * @file    stm32l1xx_i2c.h\r
4   * @author  MCD Application Team\r
5   * @version V1.0.0RC1\r
6   * @date    07/02/2010\r
7   * @brief   This file contains all the functions prototypes for the I2C firmware \r
8   *          library.\r
9   ******************************************************************************\r
10   * @copy\r
11   *\r
12   * THE PRESENT FIRMWARE WHICH IS FOR GUIDANCE ONLY AIMS AT PROVIDING CUSTOMERS\r
13   * WITH CODING INFORMATION REGARDING THEIR PRODUCTS IN ORDER FOR THEM TO SAVE\r
14   * TIME. AS A RESULT, STMICROELECTRONICS SHALL NOT BE HELD LIABLE FOR ANY\r
15   * DIRECT, INDIRECT OR CONSEQUENTIAL DAMAGES WITH RESPECT TO ANY CLAIMS ARISING\r
16   * FROM THE CONTENT OF SUCH FIRMWARE AND/OR THE USE MADE BY CUSTOMERS OF THE\r
17   * CODING INFORMATION CONTAINED HEREIN IN CONNECTION WITH THEIR PRODUCTS.\r
18   *\r
19   * <h2><center>&copy; COPYRIGHT 2010 STMicroelectronics</center></h2>\r
20   */ \r
21 \r
22 /* Define to prevent recursive inclusion -------------------------------------*/\r
23 #ifndef __STM32L1xx_I2C_H\r
24 #define __STM32L1xx_I2C_H\r
25 \r
26 #ifdef __cplusplus\r
27  extern "C" {\r
28 #endif\r
29 \r
30 /* Includes ------------------------------------------------------------------*/\r
31 #include "stm32l1xx.h"\r
32 \r
33 /** @addtogroup STM32L1xx_StdPeriph_Driver\r
34   * @{\r
35   */\r
36 \r
37 /** @addtogroup I2C\r
38   * @{\r
39   */\r
40 \r
41 /** @defgroup I2C_Exported_Types\r
42   * @{\r
43   */\r
44 \r
45 /** \r
46   * @brief  I2C Init structure definition  \r
47   */\r
48 \r
49 typedef struct\r
50 {\r
51   uint32_t I2C_ClockSpeed;          /*!< Specifies the clock frequency.\r
52                                          This parameter must be set to a value lower than 400kHz */\r
53 \r
54   uint16_t I2C_Mode;                /*!< Specifies the I2C mode.\r
55                                          This parameter can be a value of @ref I2C_mode */\r
56 \r
57   uint16_t I2C_DutyCycle;           /*!< Specifies the I2C fast mode duty cycle.\r
58                                          This parameter can be a value of @ref I2C_duty_cycle_in_fast_mode */\r
59 \r
60   uint16_t I2C_OwnAddress1;         /*!< Specifies the first device own address.\r
61                                          This parameter can be a 7-bit or 10-bit address. */\r
62 \r
63   uint16_t I2C_Ack;                 /*!< Enables or disables the acknowledgement.\r
64                                          This parameter can be a value of @ref I2C_acknowledgement */\r
65 \r
66   uint16_t I2C_AcknowledgedAddress; /*!< Specifies if 7-bit or 10-bit address is acknowledged.\r
67                                          This parameter can be a value of @ref I2C_acknowledged_address */\r
68 }I2C_InitTypeDef;\r
69 \r
70 /**\r
71   * @}\r
72   */ \r
73 \r
74 \r
75 /** @defgroup I2C_Exported_Constants\r
76   * @{\r
77   */\r
78 \r
79 #define IS_I2C_ALL_PERIPH(PERIPH) (((PERIPH) == I2C1) || \\r
80                                    ((PERIPH) == I2C2))\r
81 /** @defgroup I2C_mode \r
82   * @{\r
83   */\r
84 \r
85 #define I2C_Mode_I2C                    ((uint16_t)0x0000)\r
86 #define I2C_Mode_SMBusDevice            ((uint16_t)0x0002)  \r
87 #define I2C_Mode_SMBusHost              ((uint16_t)0x000A)\r
88 #define IS_I2C_MODE(MODE) (((MODE) == I2C_Mode_I2C) || \\r
89                            ((MODE) == I2C_Mode_SMBusDevice) || \\r
90                            ((MODE) == I2C_Mode_SMBusHost))\r
91 /**\r
92   * @}\r
93   */\r
94 \r
95 /** @defgroup I2C_duty_cycle_in_fast_mode \r
96   * @{\r
97   */\r
98 \r
99 #define I2C_DutyCycle_16_9              ((uint16_t)0x4000) /*!< I2C fast mode Tlow/Thigh = 16/9 */\r
100 #define I2C_DutyCycle_2                 ((uint16_t)0xBFFF) /*!< I2C fast mode Tlow/Thigh = 2 */\r
101 #define IS_I2C_DUTY_CYCLE(CYCLE) (((CYCLE) == I2C_DutyCycle_16_9) || \\r
102                                   ((CYCLE) == I2C_DutyCycle_2))\r
103 /**\r
104   * @}\r
105   */ \r
106 \r
107 /** @defgroup I2C_acknowledgement\r
108   * @{\r
109   */\r
110 \r
111 #define I2C_Ack_Enable                  ((uint16_t)0x0400)\r
112 #define I2C_Ack_Disable                 ((uint16_t)0x0000)\r
113 #define IS_I2C_ACK_STATE(STATE) (((STATE) == I2C_Ack_Enable) || \\r
114                                  ((STATE) == I2C_Ack_Disable))\r
115 /**\r
116   * @}\r
117   */\r
118 \r
119 /** @defgroup I2C_transfer_direction \r
120   * @{\r
121   */\r
122 \r
123 #define  I2C_Direction_Transmitter      ((uint8_t)0x00)\r
124 #define  I2C_Direction_Receiver         ((uint8_t)0x01)\r
125 #define IS_I2C_DIRECTION(DIRECTION) (((DIRECTION) == I2C_Direction_Transmitter) || \\r
126                                      ((DIRECTION) == I2C_Direction_Receiver))\r
127 /**\r
128   * @}\r
129   */\r
130 \r
131 /** @defgroup I2C_acknowledged_address \r
132   * @{\r
133   */\r
134 \r
135 #define I2C_AcknowledgedAddress_7bit    ((uint16_t)0x4000)\r
136 #define I2C_AcknowledgedAddress_10bit   ((uint16_t)0xC000)\r
137 #define IS_I2C_ACKNOWLEDGE_ADDRESS(ADDRESS) (((ADDRESS) == I2C_AcknowledgedAddress_7bit) || \\r
138                                              ((ADDRESS) == I2C_AcknowledgedAddress_10bit))\r
139 /**\r
140   * @}\r
141   */ \r
142 \r
143 /** @defgroup I2C_registers \r
144   * @{\r
145   */\r
146 \r
147 #define I2C_Register_CR1                ((uint8_t)0x00)\r
148 #define I2C_Register_CR2                ((uint8_t)0x04)\r
149 #define I2C_Register_OAR1               ((uint8_t)0x08)\r
150 #define I2C_Register_OAR2               ((uint8_t)0x0C)\r
151 #define I2C_Register_DR                 ((uint8_t)0x10)\r
152 #define I2C_Register_SR1                ((uint8_t)0x14)\r
153 #define I2C_Register_SR2                ((uint8_t)0x18)\r
154 #define I2C_Register_CCR                ((uint8_t)0x1C)\r
155 #define I2C_Register_TRISE              ((uint8_t)0x20)\r
156 #define IS_I2C_REGISTER(REGISTER) (((REGISTER) == I2C_Register_CR1) || \\r
157                                    ((REGISTER) == I2C_Register_CR2) || \\r
158                                    ((REGISTER) == I2C_Register_OAR1) || \\r
159                                    ((REGISTER) == I2C_Register_OAR2) || \\r
160                                    ((REGISTER) == I2C_Register_DR) || \\r
161                                    ((REGISTER) == I2C_Register_SR1) || \\r
162                                    ((REGISTER) == I2C_Register_SR2) || \\r
163                                    ((REGISTER) == I2C_Register_CCR) || \\r
164                                    ((REGISTER) == I2C_Register_TRISE))\r
165 /**\r
166   * @}\r
167   */\r
168 \r
169 /** @defgroup I2C_SMBus_alert_pin_level \r
170   * @{\r
171   */\r
172 \r
173 #define I2C_SMBusAlert_Low              ((uint16_t)0x2000)\r
174 #define I2C_SMBusAlert_High             ((uint16_t)0xDFFF)\r
175 #define IS_I2C_SMBUS_ALERT(ALERT) (((ALERT) == I2C_SMBusAlert_Low) || \\r
176                                    ((ALERT) == I2C_SMBusAlert_High))\r
177 /**\r
178   * @}\r
179   */\r
180 \r
181 /** @defgroup I2C_PEC_position \r
182   * @{\r
183   */\r
184 \r
185 #define I2C_PECPosition_Next            ((uint16_t)0x0800)\r
186 #define I2C_PECPosition_Current         ((uint16_t)0xF7FF)\r
187 #define IS_I2C_PEC_POSITION(POSITION) (((POSITION) == I2C_PECPosition_Next) || \\r
188                                        ((POSITION) == I2C_PECPosition_Current))\r
189 /**\r
190   * @}\r
191   */ \r
192 \r
193 /** @defgroup I2C_interrupts_definition \r
194   * @{\r
195   */\r
196 \r
197 #define I2C_IT_BUF                      ((uint16_t)0x0400)\r
198 #define I2C_IT_EVT                      ((uint16_t)0x0200)\r
199 #define I2C_IT_ERR                      ((uint16_t)0x0100)\r
200 #define IS_I2C_CONFIG_IT(IT) ((((IT) & (uint16_t)0xF8FF) == 0x00) && ((IT) != 0x00))\r
201 /**\r
202   * @}\r
203   */ \r
204 \r
205 /** @defgroup I2C_interrupts_definition \r
206   * @{\r
207   */\r
208 \r
209 #define I2C_IT_SMBALERT                 ((uint32_t)0x01008000)\r
210 #define I2C_IT_TIMEOUT                  ((uint32_t)0x01004000)\r
211 #define I2C_IT_PECERR                   ((uint32_t)0x01001000)\r
212 #define I2C_IT_OVR                      ((uint32_t)0x01000800)\r
213 #define I2C_IT_AF                       ((uint32_t)0x01000400)\r
214 #define I2C_IT_ARLO                     ((uint32_t)0x01000200)\r
215 #define I2C_IT_BERR                     ((uint32_t)0x01000100)\r
216 #define I2C_IT_TXE                      ((uint32_t)0x06000080)\r
217 #define I2C_IT_RXNE                     ((uint32_t)0x06000040)\r
218 #define I2C_IT_STOPF                    ((uint32_t)0x02000010)\r
219 #define I2C_IT_ADD10                    ((uint32_t)0x02000008)\r
220 #define I2C_IT_BTF                      ((uint32_t)0x02000004)\r
221 #define I2C_IT_ADDR                     ((uint32_t)0x02000002)\r
222 #define I2C_IT_SB                       ((uint32_t)0x02000001)\r
223 \r
224 #define IS_I2C_CLEAR_IT(IT) ((((IT) & (uint16_t)0x20FF) == 0x00) && ((IT) != (uint16_t)0x00))\r
225 \r
226 #define IS_I2C_GET_IT(IT) (((IT) == I2C_IT_SMBALERT) || ((IT) == I2C_IT_TIMEOUT) || \\r
227                            ((IT) == I2C_IT_PECERR) || ((IT) == I2C_IT_OVR) || \\r
228                            ((IT) == I2C_IT_AF) || ((IT) == I2C_IT_ARLO) || \\r
229                            ((IT) == I2C_IT_BERR) || ((IT) == I2C_IT_TXE) || \\r
230                            ((IT) == I2C_IT_RXNE) || ((IT) == I2C_IT_STOPF) || \\r
231                            ((IT) == I2C_IT_ADD10) || ((IT) == I2C_IT_BTF) || \\r
232                            ((IT) == I2C_IT_ADDR) || ((IT) == I2C_IT_SB))\r
233 /**\r
234   * @}\r
235   */\r
236 \r
237 /** @defgroup I2C_flags_definition \r
238   * @{\r
239   */\r
240 \r
241 /** \r
242   * @brief  SR2 register flags  \r
243   */\r
244 \r
245 #define I2C_FLAG_DUALF                  ((uint32_t)0x00800000)\r
246 #define I2C_FLAG_SMBHOST                ((uint32_t)0x00400000)\r
247 #define I2C_FLAG_SMBDEFAULT             ((uint32_t)0x00200000)\r
248 #define I2C_FLAG_GENCALL                ((uint32_t)0x00100000)\r
249 #define I2C_FLAG_TRA                    ((uint32_t)0x00040000)\r
250 #define I2C_FLAG_BUSY                   ((uint32_t)0x00020000)\r
251 #define I2C_FLAG_MSL                    ((uint32_t)0x00010000)\r
252 \r
253 /** \r
254   * @brief  SR1 register flags  \r
255   */\r
256 \r
257 #define I2C_FLAG_SMBALERT               ((uint32_t)0x10008000)\r
258 #define I2C_FLAG_TIMEOUT                ((uint32_t)0x10004000)\r
259 #define I2C_FLAG_PECERR                 ((uint32_t)0x10001000)\r
260 #define I2C_FLAG_OVR                    ((uint32_t)0x10000800)\r
261 #define I2C_FLAG_AF                     ((uint32_t)0x10000400)\r
262 #define I2C_FLAG_ARLO                   ((uint32_t)0x10000200)\r
263 #define I2C_FLAG_BERR                   ((uint32_t)0x10000100)\r
264 #define I2C_FLAG_TXE                    ((uint32_t)0x10000080)\r
265 #define I2C_FLAG_RXNE                   ((uint32_t)0x10000040)\r
266 #define I2C_FLAG_STOPF                  ((uint32_t)0x10000010)\r
267 #define I2C_FLAG_ADD10                  ((uint32_t)0x10000008)\r
268 #define I2C_FLAG_BTF                    ((uint32_t)0x10000004)\r
269 #define I2C_FLAG_ADDR                   ((uint32_t)0x10000002)\r
270 #define I2C_FLAG_SB                     ((uint32_t)0x10000001)\r
271 \r
272 #define IS_I2C_CLEAR_FLAG(FLAG) ((((FLAG) & (uint16_t)0x20FF) == 0x00) && ((FLAG) != (uint16_t)0x00))\r
273 \r
274 #define IS_I2C_GET_FLAG(FLAG) (((FLAG) == I2C_FLAG_DUALF) || ((FLAG) == I2C_FLAG_SMBHOST) || \\r
275                                ((FLAG) == I2C_FLAG_SMBDEFAULT) || ((FLAG) == I2C_FLAG_GENCALL) || \\r
276                                ((FLAG) == I2C_FLAG_TRA) || ((FLAG) == I2C_FLAG_BUSY) || \\r
277                                ((FLAG) == I2C_FLAG_MSL) || ((FLAG) == I2C_FLAG_SMBALERT) || \\r
278                                ((FLAG) == I2C_FLAG_TIMEOUT) || ((FLAG) == I2C_FLAG_PECERR) || \\r
279                                ((FLAG) == I2C_FLAG_OVR) || ((FLAG) == I2C_FLAG_AF) || \\r
280                                ((FLAG) == I2C_FLAG_ARLO) || ((FLAG) == I2C_FLAG_BERR) || \\r
281                                ((FLAG) == I2C_FLAG_TXE) || ((FLAG) == I2C_FLAG_RXNE) || \\r
282                                ((FLAG) == I2C_FLAG_STOPF) || ((FLAG) == I2C_FLAG_ADD10) || \\r
283                                ((FLAG) == I2C_FLAG_BTF) || ((FLAG) == I2C_FLAG_ADDR) || \\r
284                                ((FLAG) == I2C_FLAG_SB))\r
285 /**\r
286   * @}\r
287   */\r
288 \r
289 /** @defgroup I2C_Events \r
290   * @{\r
291   */\r
292 \r
293 /*========================================\r
294      \r
295                      I2C Master Events (Events grouped in order of communication)\r
296                                                         ==========================================*/\r
297 /** \r
298   * @brief  Communication start\r
299   * \r
300   * After sending the START condition (I2C_GenerateSTART() function) the master \r
301   * has to wait for this event. It means that the Start condition has been correctly \r
302   * released on the I2C bus (the bus is free, no other devices is communicating).\r
303   * \r
304   */\r
305 /* --EV5 */\r
306 #define  I2C_EVENT_MASTER_MODE_SELECT                      ((uint32_t)0x00030001)  /* BUSY, MSL and SB flag */\r
307 \r
308 /** \r
309   * @brief  Address Acknowledge\r
310   * \r
311   * After checking on EV5 (start condition correctly released on the bus), the \r
312   * master sends the address of the slave(s) with which it will communicate \r
313   * (I2C_Send7bitAddress() function, it also determines the direction of the communication: \r
314   * Master transmitter or Receiver). Then the master has to wait that a slave acknowledges \r
315   * his address. If an acknowledge is sent on the bus, one of the following events will \r
316   * be set:\r
317   * \r
318   *  1) In case of Master Receiver (7-bit addressing): the I2C_EVENT_MASTER_RECEIVER_MODE_SELECTED \r
319   *     event is set.\r
320   *  \r
321   *  2) In case of Master Transmitter (7-bit addressing): the I2C_EVENT_MASTER_TRANSMITTER_MODE_SELECTED \r
322   *     is set\r
323   *  \r
324   *  3) In case of 10-Bit addressing mode, the master (just after generating the START \r
325   *  and checking on EV5) has to send the header of 10-bit addressing mode (I2C_SendData() \r
326   *  function). Then master should wait on EV9. It means that the 10-bit addressing \r
327   *  header has been correctly sent on the bus. Then master should send the second part of \r
328   *  the 10-bit address (LSB) using the function I2C_Send7bitAddress(). Then master \r
329   *  should wait for event EV6. \r
330   *     \r
331   */\r
332 \r
333 /* --EV6 */\r
334 #define  I2C_EVENT_MASTER_TRANSMITTER_MODE_SELECTED        ((uint32_t)0x00070082)  /* BUSY, MSL, ADDR, TXE and TRA flags */\r
335 #define  I2C_EVENT_MASTER_RECEIVER_MODE_SELECTED           ((uint32_t)0x00030002)  /* BUSY, MSL and ADDR flags */\r
336 /* --EV9 */\r
337 #define  I2C_EVENT_MASTER_MODE_ADDRESS10                   ((uint32_t)0x00030008)  /* BUSY, MSL and ADD10 flags */\r
338 \r
339 /** \r
340   * @brief Communication events\r
341   * \r
342   * If a communication is established (START condition generated and slave address \r
343   * acknowledged) then the master has to check on one of the following events for \r
344   * communication procedures:\r
345   *  \r
346   * 1) Master Receiver mode: The master has to wait on the event EV7 then to read \r
347   *    the data received from the slave (I2C_ReceiveData() function).\r
348   * \r
349   * 2) Master Transmitter mode: The master has to send data (I2C_SendData() \r
350   *    function) then to wait on event EV8 or EV8_2.\r
351   *    These two events are similar: \r
352   *     - EV8 means that the data has been written in the data register and is \r
353   *       being shifted out.\r
354   *     - EV8_2 means that the data has been physically shifted out and output \r
355   *       on the bus.\r
356   *     In most cases, using EV8 is sufficient for the application.\r
357   *     Using EV8_2 leads to a slower communication but ensure more reliable test.\r
358   *     EV8_2 is also more suitable than EV8 for testing on the last data transmission \r
359   *     (before Stop condition generation).\r
360   *     \r
361   *  @note In case the  user software does not guarantee that this event EV7 is \r
362   *  managed before the current byte end of transfer, then user may check on EV7 \r
363   *  and BTF flag at the same time (ie. (I2C_EVENT_MASTER_BYTE_RECEIVED | I2C_FLAG_BTF)).\r
364   *  In this case the communication may be slower.\r
365   * \r
366   */\r
367 \r
368 /* Master RECEIVER mode -----------------------------*/ \r
369 /* --EV7 */\r
370 #define  I2C_EVENT_MASTER_BYTE_RECEIVED                    ((uint32_t)0x00030040)  /* BUSY, MSL and RXNE flags */\r
371 \r
372 /* Master TRANSMITTER mode --------------------------*/\r
373 /* --EV8 */\r
374 #define I2C_EVENT_MASTER_BYTE_TRANSMITTING                 ((uint32_t)0x00070080) /* TRA, BUSY, MSL, TXE flags */\r
375 /* --EV8_2 */\r
376 #define  I2C_EVENT_MASTER_BYTE_TRANSMITTED                 ((uint32_t)0x00070084)  /* TRA, BUSY, MSL, TXE and BTF flags */\r
377 \r
378 \r
379 /*========================================\r
380      \r
381                      I2C Slave Events (Events grouped in order of communication)\r
382                                                         ==========================================*/\r
383 \r
384 /** \r
385   * @brief  Communication start events\r
386   * \r
387   * Wait on one of these events at the start of the communication. It means that \r
388   * the I2C peripheral detected a Start condition on the bus (generated by master \r
389   * device) followed by the peripheral address. The peripheral generates an ACK \r
390   * condition on the bus (if the acknowledge feature is enabled through function \r
391   * I2C_AcknowledgeConfig()) and the events listed above are set :\r
392   *  \r
393   * 1) In normal case (only one address managed by the slave), when the address \r
394   *   sent by the master matches the own address of the peripheral (configured by \r
395   *   I2C_OwnAddress1 field) the I2C_EVENT_SLAVE_XXX_ADDRESS_MATCHED event is set \r
396   *   (where XXX could be TRANSMITTER or RECEIVER).\r
397   *    \r
398   * 2) In case the address sent by the master matches the second address of the \r
399   *   peripheral (configured by the function I2C_OwnAddress2Config() and enabled \r
400   *   by the function I2C_DualAddressCmd()) the events I2C_EVENT_SLAVE_XXX_SECONDADDRESS_MATCHED \r
401   *   (where XXX could be TRANSMITTER or RECEIVER) are set.\r
402   *   \r
403   * 3) In case the address sent by the master is General Call (address 0x00) and \r
404   *   if the General Call is enabled for the peripheral (using function I2C_GeneralCallCmd()) \r
405   *   the following event is set I2C_EVENT_SLAVE_GENERALCALLADDRESS_MATCHED.   \r
406   * \r
407   */\r
408 \r
409 /* --EV1  (all the events below are variants of EV1) */   \r
410 /* 1) Case of One Single Address managed by the slave */\r
411 #define  I2C_EVENT_SLAVE_RECEIVER_ADDRESS_MATCHED          ((uint32_t)0x00020002) /* BUSY and ADDR flags */\r
412 #define  I2C_EVENT_SLAVE_TRANSMITTER_ADDRESS_MATCHED       ((uint32_t)0x00060082) /* TRA, BUSY, TXE and ADDR flags */\r
413 \r
414 /* 2) Case of Dual address managed by the slave */\r
415 #define  I2C_EVENT_SLAVE_RECEIVER_SECONDADDRESS_MATCHED    ((uint32_t)0x00820000)  /* DUALF and BUSY flags */\r
416 #define  I2C_EVENT_SLAVE_TRANSMITTER_SECONDADDRESS_MATCHED ((uint32_t)0x00860080)  /* DUALF, TRA, BUSY and TXE flags */\r
417 \r
418 /* 3) Case of General Call enabled for the slave */\r
419 #define  I2C_EVENT_SLAVE_GENERALCALLADDRESS_MATCHED        ((uint32_t)0x00120000)  /* GENCALL and BUSY flags */\r
420 \r
421 /** \r
422   * @brief  Communication events\r
423   * \r
424   * Wait on one of these events when EV1 has already been checked and: \r
425   * \r
426   * - Slave RECEIVER mode:\r
427   *     - EV2: When the application is expecting a data byte to be received. \r
428   *     - EV4: When the application is expecting the end of the communication: master \r
429   *       sends a stop condition and data transmission is stopped.\r
430   *    \r
431   * - Slave Transmitter mode:\r
432   *    - EV3: When a byte has been transmitted by the slave and the application is expecting \r
433   *      the end of the byte transmission. The two events I2C_EVENT_SLAVE_BYTE_TRANSMITTED and\r
434   *      I2C_EVENT_SLAVE_BYTE_TRANSMITTING are similar. The second one can optionally be \r
435   *      used when the user software doesn't guarantee the EV3 is managed before the\r
436   *      current byte end of tranfer.\r
437   *    - EV3_2: When the master sends a NACK in order to tell slave that data transmission \r
438   *      shall end (before sending the STOP condition). In this case slave has to stop sending \r
439   *      data bytes and expect a Stop condition on the bus.\r
440   *      \r
441   *  @note In case the  user software does not guarantee that the event EV2 is \r
442   *  managed before the current byte end of transfer, then user may check on EV2 \r
443   *  and BTF flag at the same time (ie. (I2C_EVENT_SLAVE_BYTE_RECEIVED | I2C_FLAG_BTF)).\r
444   *  In this case the communication may be slower.\r
445   *\r
446   */\r
447 \r
448 /* Slave RECEIVER mode --------------------------*/ \r
449 /* --EV2 */\r
450 #define  I2C_EVENT_SLAVE_BYTE_RECEIVED                     ((uint32_t)0x00020040)  /* BUSY and RXNE flags */\r
451 /* --EV4  */\r
452 #define  I2C_EVENT_SLAVE_STOP_DETECTED                     ((uint32_t)0x00000010)  /* STOPF flag */\r
453 \r
454 /* Slave TRANSMITTER mode -----------------------*/\r
455 /* --EV3 */\r
456 #define  I2C_EVENT_SLAVE_BYTE_TRANSMITTED                  ((uint32_t)0x00060084)  /* TRA, BUSY, TXE and BTF flags */\r
457 #define  I2C_EVENT_SLAVE_BYTE_TRANSMITTING                 ((uint32_t)0x00060080)  /* TRA, BUSY and TXE flags */\r
458 /* --EV3_2 */\r
459 #define  I2C_EVENT_SLAVE_ACK_FAILURE                       ((uint32_t)0x00000400)  /* AF flag */\r
460 \r
461 /*===========================      End of Events Description           ==========================================*/\r
462 \r
463 #define IS_I2C_EVENT(EVENT) (((EVENT) == I2C_EVENT_SLAVE_TRANSMITTER_ADDRESS_MATCHED) || \\r
464                              ((EVENT) == I2C_EVENT_SLAVE_RECEIVER_ADDRESS_MATCHED) || \\r
465                              ((EVENT) == I2C_EVENT_SLAVE_TRANSMITTER_SECONDADDRESS_MATCHED) || \\r
466                              ((EVENT) == I2C_EVENT_SLAVE_RECEIVER_SECONDADDRESS_MATCHED) || \\r
467                              ((EVENT) == I2C_EVENT_SLAVE_GENERALCALLADDRESS_MATCHED) || \\r
468                              ((EVENT) == I2C_EVENT_SLAVE_BYTE_RECEIVED) || \\r
469                              ((EVENT) == (I2C_EVENT_SLAVE_BYTE_RECEIVED | I2C_FLAG_DUALF)) || \\r
470                              ((EVENT) == (I2C_EVENT_SLAVE_BYTE_RECEIVED | I2C_FLAG_GENCALL)) || \\r
471                              ((EVENT) == I2C_EVENT_SLAVE_BYTE_TRANSMITTED) || \\r
472                              ((EVENT) == (I2C_EVENT_SLAVE_BYTE_TRANSMITTED | I2C_FLAG_DUALF)) || \\r
473                              ((EVENT) == (I2C_EVENT_SLAVE_BYTE_TRANSMITTED | I2C_FLAG_GENCALL)) || \\r
474                              ((EVENT) == I2C_EVENT_SLAVE_STOP_DETECTED) || \\r
475                              ((EVENT) == I2C_EVENT_MASTER_MODE_SELECT) || \\r
476                              ((EVENT) == I2C_EVENT_MASTER_TRANSMITTER_MODE_SELECTED) || \\r
477                              ((EVENT) == I2C_EVENT_MASTER_RECEIVER_MODE_SELECTED) || \\r
478                              ((EVENT) == I2C_EVENT_MASTER_BYTE_RECEIVED) || \\r
479                              ((EVENT) == I2C_EVENT_MASTER_BYTE_TRANSMITTED) || \\r
480                              ((EVENT) == I2C_EVENT_MASTER_BYTE_TRANSMITTING) || \\r
481                              ((EVENT) == I2C_EVENT_MASTER_MODE_ADDRESS10) || \\r
482                              ((EVENT) == I2C_EVENT_SLAVE_ACK_FAILURE))\r
483 /**\r
484   * @}\r
485   */\r
486 \r
487 /** @defgroup I2C_own_address1 \r
488   * @{\r
489   */\r
490 \r
491 #define IS_I2C_OWN_ADDRESS1(ADDRESS1) ((ADDRESS1) <= 0x3FF)\r
492 /**\r
493   * @}\r
494   */\r
495 \r
496 /** @defgroup I2C_clock_speed \r
497   * @{\r
498   */\r
499 \r
500 #define IS_I2C_CLOCK_SPEED(SPEED) (((SPEED) >= 0x1) && ((SPEED) <= 400000))\r
501 /**\r
502   * @}\r
503   */\r
504 \r
505 /**\r
506   * @}\r
507   */\r
508 \r
509 /** @defgroup I2C_Exported_Macros\r
510   * @{\r
511   */\r
512 \r
513 /**\r
514   * @}\r
515   */\r
516 \r
517 /** @defgroup I2C_Exported_Functions\r
518   * @{\r
519   */\r
520 \r
521 void I2C_DeInit(I2C_TypeDef* I2Cx);\r
522 void I2C_Init(I2C_TypeDef* I2Cx, I2C_InitTypeDef* I2C_InitStruct);\r
523 void I2C_StructInit(I2C_InitTypeDef* I2C_InitStruct);\r
524 void I2C_Cmd(I2C_TypeDef* I2Cx, FunctionalState NewState);\r
525 void I2C_DMACmd(I2C_TypeDef* I2Cx, FunctionalState NewState);\r
526 void I2C_DMALastTransferCmd(I2C_TypeDef* I2Cx, FunctionalState NewState);\r
527 void I2C_GenerateSTART(I2C_TypeDef* I2Cx, FunctionalState NewState);\r
528 void I2C_GenerateSTOP(I2C_TypeDef* I2Cx, FunctionalState NewState);\r
529 void I2C_AcknowledgeConfig(I2C_TypeDef* I2Cx, FunctionalState NewState);\r
530 void I2C_OwnAddress2Config(I2C_TypeDef* I2Cx, uint8_t Address);\r
531 void I2C_DualAddressCmd(I2C_TypeDef* I2Cx, FunctionalState NewState);\r
532 void I2C_GeneralCallCmd(I2C_TypeDef* I2Cx, FunctionalState NewState);\r
533 void I2C_ITConfig(I2C_TypeDef* I2Cx, uint16_t I2C_IT, FunctionalState NewState);\r
534 void I2C_SendData(I2C_TypeDef* I2Cx, uint8_t Data);\r
535 uint8_t I2C_ReceiveData(I2C_TypeDef* I2Cx);\r
536 void I2C_Send7bitAddress(I2C_TypeDef* I2Cx, uint8_t Address, uint8_t I2C_Direction);\r
537 uint16_t I2C_ReadRegister(I2C_TypeDef* I2Cx, uint8_t I2C_Register);\r
538 void I2C_SoftwareResetCmd(I2C_TypeDef* I2Cx, FunctionalState NewState);\r
539 void I2C_SMBusAlertConfig(I2C_TypeDef* I2Cx, uint16_t I2C_SMBusAlert);\r
540 void I2C_TransmitPEC(I2C_TypeDef* I2Cx, FunctionalState NewState);\r
541 void I2C_PECPositionConfig(I2C_TypeDef* I2Cx, uint16_t I2C_PECPosition);\r
542 void I2C_CalculatePEC(I2C_TypeDef* I2Cx, FunctionalState NewState);\r
543 uint8_t I2C_GetPEC(I2C_TypeDef* I2Cx);\r
544 void I2C_ARPCmd(I2C_TypeDef* I2Cx, FunctionalState NewState);\r
545 void I2C_StretchClockCmd(I2C_TypeDef* I2Cx, FunctionalState NewState);\r
546 void I2C_FastModeDutyCycleConfig(I2C_TypeDef* I2Cx, uint16_t I2C_DutyCycle);\r
547 \r
548 /**\r
549  * @brief\r
550  ****************************************************************************************\r
551  *\r
552  *                         I2C State Monitoring Functions\r
553  *                       \r
554  ****************************************************************************************   \r
555  * This I2C driver provides three different ways for I2C state monitoring\r
556  *  depending on the application requirements and constraints:\r
557  *        \r
558  *  \r
559  * 1) Basic state monitoring:\r
560  *    Using I2C_CheckEvent() function:\r
561  *    It compares the status registers (SR1 and SR2) content to a given event\r
562  *    (can be the combination of one or more flags).\r
563  *    It returns SUCCESS if the current status includes the given flags \r
564  *    and returns ERROR if one or more flags are missing in the current status.\r
565  *    - When to use:\r
566  *      - This function is suitable for most applications as well as for startup \r
567  *      activity since the events are fully described in the product reference manual \r
568  *      (RM0008).\r
569  *      - It is also suitable for users who need to define their own events.\r
570  *    - Limitations:\r
571  *      - If an error occurs (ie. error flags are set besides to the monitored flags),\r
572  *        the I2C_CheckEvent() function may return SUCCESS despite the communication\r
573  *        hold or corrupted real state. \r
574  *        In this case, it is advised to use error interrupts to monitor the error\r
575  *        events and handle them in the interrupt IRQ handler.\r
576  *        \r
577  *        @note \r
578  *        For error management, it is advised to use the following functions:\r
579  *          - I2C_ITConfig() to configure and enable the error interrupts (I2C_IT_ERR).\r
580  *          - I2Cx_ER_IRQHandler() which is called when the error interurpt occurs.\r
581  *            Where x is the peripheral instance (I2C1, I2C2 ...)\r
582  *          - I2C_GetFlagStatus() or I2C_GetITStatus() to be called into the I2Cx_ER_IRQHandler() function \r
583  *            in order to determine which error occured.\r
584  *          - I2C_ClearFlag() or I2C_ClearITPendingBit() and/or I2C_SoftwareResetCmd() and/or I2C_GenerateStop() \r
585  *            in order to clear the error flag and source and return to correct \r
586  *            communication status.\r
587  *            \r
588  *\r
589  *  2) Advanced state monitoring:\r
590  *     Using the function I2C_GetLastEvent() which returns the image of both status \r
591  *     registers in a single word (uint32_t) (Status Register 2 value is shifted left \r
592  *     by 16 bits and concatenated to Status Register 1).\r
593  *     - When to use:\r
594  *       - This function is suitable for the same applications above but it allows to\r
595  *         overcome the limitations of I2C_GetFlagStatus() function (see below).\r
596  *         The returned value could be compared to events already defined in the \r
597  *         library (stm32f10x_i2c.h) or to custom values defined by user.\r
598  *       - This function is suitable when multiple flags are monitored at the same time.\r
599  *       - At the opposite of I2C_CheckEvent() function, this function allows user to\r
600  *         choose when an event is accepted (when all events flags are set and no \r
601  *         other flags are set or just when the needed flags are set like \r
602  *         I2C_CheckEvent() function).\r
603  *     - Limitations:\r
604  *       - User may need to define his own events.\r
605  *       - Same remark concerning the error management is applicable for this \r
606  *         function if user decides to check only regular communication flags (and \r
607  *         ignores error flags).\r
608  *     \r
609  *\r
610  *  3) Flag-based state monitoring:\r
611  *     Using the function I2C_GetFlagStatus() which simply returns the status of \r
612  *     one single flag (ie. I2C_FLAG_RXNE ...). \r
613  *     - When to use:\r
614  *        - This function could be used for specific applications or in debug phase.\r
615  *        - It is suitable when only one flag checking is needed (most I2C events \r
616  *          are monitored through multiple flags).\r
617  *     - Limitations: \r
618  *        - When calling this function, the Status register is accessed. Some flags are\r
619  *          cleared when the status register is accessed. So checking the status\r
620  *          of one Flag, may clear other ones.\r
621  *        - Function may need to be called twice or more in order to monitor one \r
622  *          single event.\r
623  *            \r
624  */\r
625 \r
626 /**\r
627  * \r
628  *  1) Basic state monitoring\r
629  *******************************************************************************\r
630  */\r
631 ErrorStatus I2C_CheckEvent(I2C_TypeDef* I2Cx, uint32_t I2C_EVENT);\r
632 /**\r
633  * \r
634  *  2) Advanced state monitoring\r
635  *******************************************************************************\r
636  */\r
637 uint32_t I2C_GetLastEvent(I2C_TypeDef* I2Cx);\r
638 /**\r
639  * \r
640  *  3) Flag-based state monitoring\r
641  *******************************************************************************\r
642  */\r
643 FlagStatus I2C_GetFlagStatus(I2C_TypeDef* I2Cx, uint32_t I2C_FLAG);\r
644 /**\r
645  *\r
646  *******************************************************************************\r
647  */\r
648 \r
649 void I2C_ClearFlag(I2C_TypeDef* I2Cx, uint32_t I2C_FLAG);\r
650 ITStatus I2C_GetITStatus(I2C_TypeDef* I2Cx, uint32_t I2C_IT);\r
651 void I2C_ClearITPendingBit(I2C_TypeDef* I2Cx, uint32_t I2C_IT);\r
652 \r
653 #ifdef __cplusplus\r
654 }\r
655 #endif\r
656 \r
657 #endif /*__STM32L1xx_I2C_H */\r
658 /**\r
659   * @}\r
660   */ \r
661 \r
662 /**\r
663   * @}\r
664   */ \r
665 \r
666 /**\r
667   * @}\r
668   */ \r
669 \r
670 /******************* (C) COPYRIGHT 2010 STMicroelectronics *****END OF FILE****/\r