1 /**
2   ******************************************************************************
3   * @file    stm32f37x_i2c.h
4   * @author  MCD Application Team
5   * @version V1.0.0
6   * @date    20-September-2012
7   * @brief   This file contains all the functions prototypes for the I2C firmware
8   *          library.
9   ******************************************************************************
10   * @attention
11   *
12   * <h2><center>&copy; COPYRIGHT 2012 STMicroelectronics</center></h2>
13   *
14   * Licensed under MCD-ST Liberty SW License Agreement V2, (the "License");
15   * You may not use this file except in compliance with the License.
16   * You may obtain a copy of the License at:
17   *
18   *        http://www.st.com/software_license_agreement_liberty_v2
19   *
20   * Unless required by applicable law or agreed to in writing, software
21   * distributed under the License is distributed on an "AS IS" BASIS,
22   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
23   * See the License for the specific language governing permissions and
24   * limitations under the License.
25   *
26   ******************************************************************************
27   */
28 
29 /* Define to prevent recursive inclusion -------------------------------------*/
30 #ifndef __STM32F37X_I2C_H
31 #define __STM32F37X_I2C_H
32 
33 #ifdef __cplusplus
34  extern "C" {
35 #endif
36 
37 /* Includes ------------------------------------------------------------------*/
38 #include "stm32f37x.h"
39 
40 /** @addtogroup STM32F37x_StdPeriph_Driver
41   * @{
42   */
43 
44 /** @addtogroup I2C
45   * @{
46   */
47 
48 /* Exported types ------------------------------------------------------------*/
49 
50 /**
51   * @brief  I2C Init structure definition
52   */
53 
54 typedef struct
55 {
56   uint32_t I2C_Timing;              /*!< Specifies the I2C_TIMINGR_register value.
57                                          This parameter must be set by referring to I2C_Timing_Config_Tool*/
58 
59   uint32_t I2C_AnalogFilter;        /*!< Enables or disables analog noise filter.
60                                          This parameter can be a value of @ref I2C_Analog_Filter*/
61 
62   uint32_t I2C_DigitalFilter;       /*!< Configures the digital noise filter.
63                                          This parameter can be a number between 0x00 and 0x0F*/
64 
65   uint32_t I2C_Mode;                /*!< Specifies the I2C mode.
66                                          This parameter can be a value of @ref I2C_mode*/
67 
68   uint32_t I2C_OwnAddress1;         /*!< Specifies the device own address 1.
69                                          This parameter can be a 7-bit or 10-bit address*/
70 
71   uint32_t I2C_Ack;                 /*!< Enables or disables the acknowledgement.
72                                          This parameter can be a value of @ref I2C_acknowledgement*/
73 
74   uint32_t I2C_AcknowledgedAddress; /*!< Specifies if 7-bit or 10-bit address is acknowledged.
75                                          This parameter can be a value of @ref I2C_acknowledged_address*/
76 }I2C_InitTypeDef;
77 
78 /* Exported constants --------------------------------------------------------*/
79 
80 
81 /** @defgroup I2C_Exported_Constants
82   * @{
83   */
84 
85 #define IS_I2C_ALL_PERIPH(PERIPH)       (((PERIPH) == I2C1) || \
86                                          ((PERIPH) == I2C2))
87 
88 /** @defgroup I2C_Analog_Filter
89   * @{
90   */
91 
92 #define I2C_AnalogFilter_Enable         ((uint32_t)0x00000000)
93 #define I2C_AnalogFilter_Disable        I2C_CR1_ANFOFF
94 
95 #define IS_I2C_ANALOG_FILTER(FILTER)    (((FILTER) == I2C_AnalogFilter_Enable) || \
96                                          ((FILTER) == I2C_AnalogFilter_Disable))
97 /**
98   * @}
99   */
100 
101 /** @defgroup I2C_Digital_Filter
102   * @{
103   */
104 
105 #define IS_I2C_DIGITAL_FILTER(FILTER)   ((FILTER) <= 0x0000000F)
106 /**
107   * @}
108   */
109 
110 /** @defgroup I2C_mode
111   * @{
112   */
113 
114 #define I2C_Mode_I2C                    ((uint32_t)0x00000000)
115 #define I2C_Mode_SMBusDevice            I2C_CR1_SMBDEN
116 #define I2C_Mode_SMBusHost              I2C_CR1_SMBHEN
117 
118 #define IS_I2C_MODE(MODE)               (((MODE) == I2C_Mode_I2C) || \
119                                          ((MODE) == I2C_Mode_SMBusDevice) || \
120                                          ((MODE) == I2C_Mode_SMBusHost))
121 /**
122   * @}
123   */
124 
125 /** @defgroup I2C_acknowledgement
126   * @{
127   */
128 
129 #define I2C_Ack_Enable                  ((uint32_t)0x00000000)
130 #define I2C_Ack_Disable                 I2C_CR2_NACK
131 
132 #define IS_I2C_ACK(ACK)                 (((ACK) == I2C_Ack_Enable) || \
133                                          ((ACK) == I2C_Ack_Disable))
134 /**
135   * @}
136   */
137 
138 /** @defgroup I2C_acknowledged_address
139   * @{
140   */
141 
142 #define I2C_AcknowledgedAddress_7bit    ((uint32_t)0x00000000)
143 #define I2C_AcknowledgedAddress_10bit   I2C_OAR1_OA1MODE
144 
145 #define IS_I2C_ACKNOWLEDGE_ADDRESS(ADDRESS) (((ADDRESS) == I2C_AcknowledgedAddress_7bit) || \
146                                              ((ADDRESS) == I2C_AcknowledgedAddress_10bit))
147 /**
148   * @}
149   */
150 
151 /** @defgroup I2C_own_address1
152   * @{
153   */
154 
155 #define IS_I2C_OWN_ADDRESS1(ADDRESS1)   ((ADDRESS1) <= (uint32_t)0x000003FF)
156 /**
157   * @}
158   */
159 
160 /** @defgroup I2C_transfer_direction
161   * @{
162   */
163 
164 #define I2C_Direction_Transmitter       ((uint16_t)0x0000)
165 #define I2C_Direction_Receiver          ((uint16_t)0x0400)
166 
167 #define IS_I2C_DIRECTION(DIRECTION)     (((DIRECTION) == I2C_Direction_Transmitter) || \
168                                          ((DIRECTION) == I2C_Direction_Receiver))
169 /**
170   * @}
171   */
172 
173 /** @defgroup I2C_DMA_transfer_requests
174   * @{
175   */
176 
177 #define I2C_DMAReq_Tx                   I2C_CR1_TXDMAEN
178 #define I2C_DMAReq_Rx                   I2C_CR1_RXDMAEN
179 
180 #define IS_I2C_DMA_REQ(REQ)             ((((REQ) & (uint32_t)0xFFFF3FFF) == 0x00) && ((REQ) != 0x00))
181 /**
182   * @}
183   */
184 
185 /** @defgroup I2C_slave_address
186   * @{
187   */
188 
189 #define IS_I2C_SLAVE_ADDRESS(ADDRESS)   ((ADDRESS) <= (uint16_t)0x03FF)
190 /**
191   * @}
192   */
193 
194 
195 /** @defgroup I2C_own_address2
196   * @{
197   */
198 
199 #define IS_I2C_OWN_ADDRESS2(ADDRESS2)   ((ADDRESS2) <= (uint16_t)0x00FF)
200 
201 /**
202   * @}
203   */
204 
205 /** @defgroup I2C_own_address2_mask
206   * @{
207   */
208 
209 #define I2C_OA2_NoMask                  ((uint8_t)0x00)
210 #define I2C_OA2_Mask01                  ((uint8_t)0x01)
211 #define I2C_OA2_Mask02                  ((uint8_t)0x02)
212 #define I2C_OA2_Mask03                  ((uint8_t)0x03)
213 #define I2C_OA2_Mask04                  ((uint8_t)0x04)
214 #define I2C_OA2_Mask05                  ((uint8_t)0x05)
215 #define I2C_OA2_Mask06                  ((uint8_t)0x06)
216 #define I2C_OA2_Mask07                  ((uint8_t)0x07)
217 
218 #define IS_I2C_OWN_ADDRESS2_MASK(MASK)  (((MASK) == I2C_OA2_NoMask) || \
219                                          ((MASK) == I2C_OA2_Mask01) || \
220                                          ((MASK) == I2C_OA2_Mask02) || \
221                                          ((MASK) == I2C_OA2_Mask03) || \
222                                          ((MASK) == I2C_OA2_Mask04) || \
223                                          ((MASK) == I2C_OA2_Mask05) || \
224                                          ((MASK) == I2C_OA2_Mask06) || \
225                                          ((MASK) == I2C_OA2_Mask07))
226 
227 /**
228   * @}
229   */
230 
231 /** @defgroup I2C_timeout
232   * @{
233   */
234 
235 #define IS_I2C_TIMEOUT(TIMEOUT)   ((TIMEOUT) <= (uint16_t)0x0FFF)
236 
237 /**
238   * @}
239   */
240 
241 /** @defgroup I2C_registers
242   * @{
243   */
244 
245 #define I2C_Register_CR1                ((uint8_t)0x00)
246 #define I2C_Register_CR2                ((uint8_t)0x04)
247 #define I2C_Register_OAR1               ((uint8_t)0x08)
248 #define I2C_Register_OAR2               ((uint8_t)0x0C)
249 #define I2C_Register_TIMINGR            ((uint8_t)0x10)
250 #define I2C_Register_TIMEOUTR           ((uint8_t)0x14)
251 #define I2C_Register_ISR                ((uint8_t)0x18)
252 #define I2C_Register_ICR                ((uint8_t)0x1C)
253 #define I2C_Register_PECR               ((uint8_t)0x20)
254 #define I2C_Register_RXDR               ((uint8_t)0x24)
255 #define I2C_Register_TXDR               ((uint8_t)0x28)
256 
257 #define IS_I2C_REGISTER(REGISTER)       (((REGISTER) == I2C_Register_CR1) || \
258                                          ((REGISTER) == I2C_Register_CR2) || \
259                                          ((REGISTER) == I2C_Register_OAR1) || \
260                                          ((REGISTER) == I2C_Register_OAR2) || \
261                                          ((REGISTER) == I2C_Register_TIMINGR) || \
262                                          ((REGISTER) == I2C_Register_TIMEOUTR) || \
263                                          ((REGISTER) == I2C_Register_ISR) || \
264                                          ((REGISTER) == I2C_Register_ICR) || \
265                                          ((REGISTER) == I2C_Register_PECR) || \
266                                          ((REGISTER) == I2C_Register_RXDR) || \
267                                          ((REGISTER) == I2C_Register_TXDR))
268 /**
269   * @}
270   */
271 
272 /** @defgroup I2C_interrupts_definition
273   * @{
274   */
275 
276 #define I2C_IT_ERRI                     I2C_CR1_ERRIE
277 #define I2C_IT_TCI                      I2C_CR1_TCIE
278 #define I2C_IT_STOPI                    I2C_CR1_STOPIE
279 #define I2C_IT_NACKI                    I2C_CR1_NACKIE
280 #define I2C_IT_ADDRI                    I2C_CR1_ADDRIE
281 #define I2C_IT_RXI                      I2C_CR1_RXIE
282 #define I2C_IT_TXI                      I2C_CR1_TXIE
283 
284 #define IS_I2C_CONFIG_IT(IT)            ((((IT) & (uint32_t)0xFFFFFF01) == 0x00) && ((IT) != 0x00))
285 
286 /**
287   * @}
288   */
289 
290 /** @defgroup I2C_flags_definition
291   * @{
292   */
293 
294 #define  I2C_FLAG_TXE                   I2C_ISR_TXE
295 #define  I2C_FLAG_TXIS                  I2C_ISR_TXIS
296 #define  I2C_FLAG_RXNE                  I2C_ISR_RXNE
297 #define  I2C_FLAG_ADDR                  I2C_ISR_ADDR
298 #define  I2C_FLAG_NACKF                 I2C_ISR_NACKF
299 #define  I2C_FLAG_STOPF                 I2C_ISR_STOPF
300 #define  I2C_FLAG_TC                    I2C_ISR_TC
301 #define  I2C_FLAG_TCR                   I2C_ISR_TCR
302 #define  I2C_FLAG_BERR                  I2C_ISR_BERR
303 #define  I2C_FLAG_ARLO                  I2C_ISR_ARLO
304 #define  I2C_FLAG_OVR                   I2C_ISR_OVR
305 #define  I2C_FLAG_PECERR                I2C_ISR_PECERR
306 #define  I2C_FLAG_TIMEOUT               I2C_ISR_TIMEOUT
307 #define  I2C_FLAG_ALERT                 I2C_ISR_ALERT
308 #define  I2C_FLAG_BUSY                  I2C_ISR_BUSY
309 
310 #define IS_I2C_CLEAR_FLAG(FLAG)         ((((FLAG) & (uint32_t)0xFFFF4000) == 0x00) && ((FLAG) != 0x00))
311 
312 #define IS_I2C_GET_FLAG(FLAG)           (((FLAG) == I2C_FLAG_TXE) || ((FLAG) == I2C_FLAG_TXIS) || \
313                                          ((FLAG) == I2C_FLAG_RXNE) || ((FLAG) == I2C_FLAG_ADDR) || \
314                                          ((FLAG) == I2C_FLAG_NACKF) || ((FLAG) == I2C_FLAG_STOPF) || \
315                                          ((FLAG) == I2C_FLAG_TC) || ((FLAG) == I2C_FLAG_TCR) || \
316                                          ((FLAG) == I2C_FLAG_BERR) || ((FLAG) == I2C_FLAG_ARLO) || \
317                                          ((FLAG) == I2C_FLAG_OVR) || ((FLAG) == I2C_FLAG_PECERR) || \
318                                          ((FLAG) == I2C_FLAG_TIMEOUT) || ((FLAG) == I2C_FLAG_ALERT) || \
319                                          ((FLAG) == I2C_FLAG_BUSY))
320 
321 /**
322   * @}
323   */
324 
325 
326 /** @defgroup I2C_interrupts_definition
327   * @{
328   */
329 
330 #define  I2C_IT_TXIS                    I2C_ISR_TXIS
331 #define  I2C_IT_RXNE                    I2C_ISR_RXNE
332 #define  I2C_IT_ADDR                    I2C_ISR_ADDR
333 #define  I2C_IT_NACKF                   I2C_ISR_NACKF
334 #define  I2C_IT_STOPF                   I2C_ISR_STOPF
335 #define  I2C_IT_TC                      I2C_ISR_TC
336 #define  I2C_IT_TCR                     I2C_ISR_TCR
337 #define  I2C_IT_BERR                    I2C_ISR_BERR
338 #define  I2C_IT_ARLO                    I2C_ISR_ARLO
339 #define  I2C_IT_OVR                     I2C_ISR_OVR
340 #define  I2C_IT_PECERR                  I2C_ISR_PECERR
341 #define  I2C_IT_TIMEOUT                 I2C_ISR_TIMEOUT
342 #define  I2C_IT_ALERT                   I2C_ISR_ALERT
343 
344 #define IS_I2C_CLEAR_IT(IT)             ((((IT) & (uint32_t)0xFFFFC001) == 0x00) && ((IT) != 0x00))
345 
346 #define IS_I2C_GET_IT(IT)               (((IT) == I2C_IT_TXIS) || ((IT) == I2C_IT_RXNE) || \
347                                          ((IT) == I2C_IT_ADDR) || ((IT) == I2C_IT_NACKF) || \
348                                          ((IT) == I2C_IT_STOPF) || ((IT) == I2C_IT_TC) || \
349                                          ((IT) == I2C_IT_TCR) || ((IT) == I2C_IT_BERR) || \
350                                          ((IT) == I2C_IT_ARLO) || ((IT) == I2C_IT_OVR) || \
351                                          ((IT) == I2C_IT_PECERR) || ((IT) == I2C_IT_TIMEOUT) || \
352                                          ((IT) == I2C_IT_ALERT))
353 
354 
355 /**
356   * @}
357   */
358 
359 /** @defgroup I2C_ReloadEndMode_definition
360   * @{
361   */
362 
363 #define  I2C_Reload_Mode                I2C_CR2_RELOAD
364 #define  I2C_AutoEnd_Mode               I2C_CR2_AUTOEND
365 #define  I2C_SoftEnd_Mode               ((uint32_t)0x00000000)
366 
367 
368 #define IS_RELOAD_END_MODE(MODE)        (((MODE) == I2C_Reload_Mode) || \
369                                          ((MODE) == I2C_AutoEnd_Mode) || \
370                                          ((MODE) == I2C_SoftEnd_Mode))
371 
372 
373 /**
374   * @}
375   */
376 
377 /** @defgroup I2C_StartStopMode_definition
378   * @{
379   */
380 
381 #define  I2C_No_StartStop                 ((uint32_t)0x00000000)
382 #define  I2C_Generate_Stop                I2C_CR2_STOP
383 #define  I2C_Generate_Start_Read          (uint32_t)(I2C_CR2_START | I2C_CR2_RD_WRN)
384 #define  I2C_Generate_Start_Write         I2C_CR2_START
385 
386 
387 #define IS_START_STOP_MODE(MODE)        (((MODE) == I2C_Generate_Stop) || \
388                                          ((MODE) == I2C_Generate_Start_Read) || \
389                                          ((MODE) == I2C_Generate_Start_Write) || \
390                                          ((MODE) == I2C_No_StartStop))
391 
392 
393 /**
394   * @}
395   */
396 
397 /**
398   * @}
399   */
400 
401 /* Exported macro ------------------------------------------------------------*/
402 /* Exported functions ------------------------------------------------------- */
403 
404 
405 /* Initialization and Configuration functions *********************************/
406 void I2C_DeInit(I2C_TypeDef* I2Cx);
407 void I2C_Init(I2C_TypeDef* I2Cx, I2C_InitTypeDef* I2C_InitStruct);
408 void I2C_StructInit(I2C_InitTypeDef* I2C_InitStruct);
409 void I2C_Cmd(I2C_TypeDef* I2Cx, FunctionalState NewState);
410 void I2C_SoftwareResetCmd(I2C_TypeDef* I2Cx);
411 void I2C_ITConfig(I2C_TypeDef* I2Cx, uint32_t I2C_IT, FunctionalState NewState);
412 void I2C_StretchClockCmd(I2C_TypeDef* I2Cx, FunctionalState NewState);
413 void I2C_StopModeCmd(I2C_TypeDef* I2Cx, FunctionalState NewState);
414 void I2C_DualAddressCmd(I2C_TypeDef* I2Cx, FunctionalState NewState);
415 void I2C_OwnAddress2Config(I2C_TypeDef* I2Cx, uint16_t Address, uint8_t Mask);
416 void I2C_GeneralCallCmd(I2C_TypeDef* I2Cx, FunctionalState NewState);
417 void I2C_SlaveByteControlCmd(I2C_TypeDef* I2Cx, FunctionalState NewState);
418 void I2C_SlaveAddressConfig(I2C_TypeDef* I2Cx, uint16_t Address);
419 void I2C_10BitAddressingModeCmd(I2C_TypeDef* I2Cx, FunctionalState NewState);
420 
421 /* Communications handling functions ******************************************/
422 void I2C_AutoEndCmd(I2C_TypeDef* I2Cx, FunctionalState NewState);
423 void I2C_ReloadCmd(I2C_TypeDef* I2Cx, FunctionalState NewState);
424 void I2C_NumberOfBytesConfig(I2C_TypeDef* I2Cx, uint8_t Number_Bytes);
425 void I2C_MasterRequestConfig(I2C_TypeDef* I2Cx, uint16_t I2C_Direction);
426 void I2C_GenerateSTART(I2C_TypeDef* I2Cx, FunctionalState NewState);
427 void I2C_GenerateSTOP(I2C_TypeDef* I2Cx, FunctionalState NewState);
428 void I2C_10BitAddressHeaderCmd(I2C_TypeDef* I2Cx, FunctionalState NewState);
429 void I2C_AcknowledgeConfig(I2C_TypeDef* I2Cx, FunctionalState NewState);
430 uint8_t I2C_GetAddressMatched(I2C_TypeDef* I2Cx);
431 uint16_t I2C_GetTransferDirection(I2C_TypeDef* I2Cx);
432 void I2C_TransferHandling(I2C_TypeDef* I2Cx, uint16_t Address, uint8_t Number_Bytes, uint32_t ReloadEndMode, uint32_t StartStopMode);
433 
434 /*  SMBUS management functions ************************************************/
435 void I2C_SMBusAlertCmd(I2C_TypeDef* I2Cx, FunctionalState NewState);
436 void I2C_ClockTimeoutCmd(I2C_TypeDef* I2Cx, FunctionalState NewState);
437 void I2C_ExtendedClockTimeoutCmd(I2C_TypeDef* I2Cx, FunctionalState NewState);
438 void I2C_IdleClockTimeoutCmd(I2C_TypeDef* I2Cx, FunctionalState NewState);
439 void I2C_TimeoutAConfig(I2C_TypeDef* I2Cx, uint16_t Timeout);
440 void I2C_TimeoutBConfig(I2C_TypeDef* I2Cx, uint16_t Timeout);
441 void I2C_CalculatePEC(I2C_TypeDef* I2Cx, FunctionalState NewState);
442 void I2C_PECRequestCmd(I2C_TypeDef* I2Cx, FunctionalState NewState);
443 uint8_t I2C_GetPEC(I2C_TypeDef* I2Cx);
444 
445 /* I2C registers management functions *****************************************/
446 uint32_t I2C_ReadRegister(I2C_TypeDef* I2Cx, uint8_t I2C_Register);
447 
448 /* Data transfers management functions ****************************************/
449 void I2C_SendData(I2C_TypeDef* I2Cx, uint8_t Data);
450 uint8_t I2C_ReceiveData(I2C_TypeDef* I2Cx);
451 
452 /* DMA transfers management functions *****************************************/
453 void I2C_DMACmd(I2C_TypeDef* I2Cx, uint32_t I2C_DMAReq, FunctionalState NewState);
454 
455 /* Interrupts and flags management functions **********************************/
456 FlagStatus I2C_GetFlagStatus(I2C_TypeDef* I2Cx, uint32_t I2C_FLAG);
457 void I2C_ClearFlag(I2C_TypeDef* I2Cx, uint32_t I2C_FLAG);
458 ITStatus I2C_GetITStatus(I2C_TypeDef* I2Cx, uint32_t I2C_IT);
459 void I2C_ClearITPendingBit(I2C_TypeDef* I2Cx, uint32_t I2C_IT);
460 
461 
462 #ifdef __cplusplus
463 }
464 #endif
465 
466 #endif /*__STM32F37X_I2C_H */
467 
468 /**
469   * @}
470   */
471 
472 /**
473   * @}
474   */
475 
476 /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/
477