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