STM32F2 Standard Peripheral bibliotheek  1.0
ST Microelectronics bibliotheek documentatie voor de STM32F2 Standard Peripheral Library
 All Data Structures Files Functions Variables Enumerations Enumerator Groups
stm32f2xx_i2c.c File Reference

This file provides firmware functions to manage the following functionalities of the Inter-integrated circuit (I2C) More...

#include "stm32f2xx_i2c.h"
#include "stm32f2xx_rcc.h"


#define CR1_CLEAR_MASK   ((uint16_t)0xFBF5) /*<! I2C registers Masks */
#define FLAG_MASK   ((uint32_t)0x00FFFFFF) /*<! I2C FLAG mask */
#define ITEN_MASK   ((uint32_t)0x07000000) /*<! I2C Interrupt Enable mask */


void I2C_DeInit (I2C_TypeDef *I2Cx)
 Deinitialize the I2Cx peripheral registers to their default reset values. More...
void I2C_Init (I2C_TypeDef *I2Cx, I2C_InitTypeDef *I2C_InitStruct)
 Initializes the I2Cx peripheral according to the specified parameters in the I2C_InitStruct. More...
void I2C_StructInit (I2C_InitTypeDef *I2C_InitStruct)
 Fills each I2C_InitStruct member with its default value. More...
void I2C_Cmd (I2C_TypeDef *I2Cx, FunctionalState NewState)
 Enables or disables the specified I2C peripheral. More...
void I2C_GenerateSTART (I2C_TypeDef *I2Cx, FunctionalState NewState)
 Generates I2Cx communication START condition. More...
void I2C_GenerateSTOP (I2C_TypeDef *I2Cx, FunctionalState NewState)
 Generates I2Cx communication STOP condition. More...
void I2C_Send7bitAddress (I2C_TypeDef *I2Cx, uint8_t Address, uint8_t I2C_Direction)
 Transmits the address byte to select the slave device. More...
void I2C_AcknowledgeConfig (I2C_TypeDef *I2Cx, FunctionalState NewState)
 Enables or disables the specified I2C acknowledge feature. More...
void I2C_OwnAddress2Config (I2C_TypeDef *I2Cx, uint8_t Address)
 Configures the specified I2C own address2. More...
void I2C_DualAddressCmd (I2C_TypeDef *I2Cx, FunctionalState NewState)
 Enables or disables the specified I2C dual addressing mode. More...
void I2C_GeneralCallCmd (I2C_TypeDef *I2Cx, FunctionalState NewState)
 Enables or disables the specified I2C general call feature. More...
void I2C_SoftwareResetCmd (I2C_TypeDef *I2Cx, FunctionalState NewState)
 Enables or disables the specified I2C software reset. More...
void I2C_StretchClockCmd (I2C_TypeDef *I2Cx, FunctionalState NewState)
 Enables or disables the specified I2C Clock stretching. More...
void I2C_FastModeDutyCycleConfig (I2C_TypeDef *I2Cx, uint16_t I2C_DutyCycle)
 Selects the specified I2C fast mode duty cycle. More...
void I2C_NACKPositionConfig (I2C_TypeDef *I2Cx, uint16_t I2C_NACKPosition)
 Selects the specified I2C NACK position in master receiver mode. More...
void I2C_SMBusAlertConfig (I2C_TypeDef *I2Cx, uint16_t I2C_SMBusAlert)
 Drives the SMBusAlert pin high or low for the specified I2C. More...
void I2C_ARPCmd (I2C_TypeDef *I2Cx, FunctionalState NewState)
 Enables or disables the specified I2C ARP. More...
void I2C_SendData (I2C_TypeDef *I2Cx, uint8_t Data)
 Sends a data byte through the I2Cx peripheral. More...
uint8_t I2C_ReceiveData (I2C_TypeDef *I2Cx)
 Returns the most recent received data by the I2Cx peripheral. More...
void I2C_TransmitPEC (I2C_TypeDef *I2Cx, FunctionalState NewState)
 Enables or disables the specified I2C PEC transfer. More...
void I2C_PECPositionConfig (I2C_TypeDef *I2Cx, uint16_t I2C_PECPosition)
 Selects the specified I2C PEC position. More...
void I2C_CalculatePEC (I2C_TypeDef *I2Cx, FunctionalState NewState)
 Enables or disables the PEC value calculation of the transferred bytes. More...
uint8_t I2C_GetPEC (I2C_TypeDef *I2Cx)
 Returns the PEC value for the specified I2C. More...
void I2C_DMACmd (I2C_TypeDef *I2Cx, FunctionalState NewState)
 Enables or disables the specified I2C DMA requests. More...
void I2C_DMALastTransferCmd (I2C_TypeDef *I2Cx, FunctionalState NewState)
 Specifies that the next DMA transfer is the last one. More...
uint16_t I2C_ReadRegister (I2C_TypeDef *I2Cx, uint8_t I2C_Register)
 Reads the specified I2C register and returns its value. More...
void I2C_ITConfig (I2C_TypeDef *I2Cx, uint16_t I2C_IT, FunctionalState NewState)
 Enables or disables the specified I2C interrupts. More...
ErrorStatus I2C_CheckEvent (I2C_TypeDef *I2Cx, uint32_t I2C_EVENT)
 Checks whether the last I2Cx Event is equal to the one passed as parameter. More...
uint32_t I2C_GetLastEvent (I2C_TypeDef *I2Cx)
 Returns the last I2Cx Event. More...
FlagStatus I2C_GetFlagStatus (I2C_TypeDef *I2Cx, uint32_t I2C_FLAG)
 Checks whether the specified I2C flag is set or not. More...
void I2C_ClearFlag (I2C_TypeDef *I2Cx, uint32_t I2C_FLAG)
 Clears the I2Cx's pending flags. More...
ITStatus I2C_GetITStatus (I2C_TypeDef *I2Cx, uint32_t I2C_IT)
 Checks whether the specified I2C interrupt has occurred or not. More...
void I2C_ClearITPendingBit (I2C_TypeDef *I2Cx, uint32_t I2C_IT)
 Clears the I2Cx's interrupt pending bits. More...

Detailed Description

This file provides firmware functions to manage the following functionalities of the Inter-integrated circuit (I2C)

MCD Application Team
  • Initialization and Configuration
  • Data transfers
  • PEC management
  • DMA transfers management
  • Interrupts, events and flags management
*          ===================================================================
*                                 How to use this driver
*          ===================================================================
*          1. Enable peripheral clock using RCC_APB1PeriphClockCmd(RCC_APB1Periph_I2Cx, ENABLE)
*             function for I2C1, I2C2 or I2C3.
*          2. Enable SDA, SCL  and SMBA (when used) GPIO clocks using 
*             RCC_AHBPeriphClockCmd() function. 
*          3. Peripherals alternate function: 
*                 - Connect the pin to the desired peripherals' Alternate 
*                   Function (AF) using GPIO_PinAFConfig() function
*                 - Configure the desired pin in alternate function by:
*                   GPIO_InitStruct->GPIO_Mode = GPIO_Mode_AF
*                 - Select the type, pull-up/pull-down and output speed via 
*                   GPIO_PuPd, GPIO_OType and GPIO_Speed members
*                 - Call GPIO_Init() function
*                 Recommended configuration is Push-Pull, Pull-up, Open-Drain.
*                 Add an external pull up if necessary (typically 4.7 KOhm).      
*          4. Program the Mode, duty cycle , Own address, Ack, Speed and Acknowledged
*             Address using the I2C_Init() function.
*          5. Optionally you can enable/configure the following parameters without
*             re-initialization (i.e there is no need to call again I2C_Init() function):
*              - Enable the acknowledge feature using I2C_AcknowledgeConfig() function
*              - Enable the dual addressing mode using I2C_DualAddressCmd() function
*              - Enable the general call using the I2C_GeneralCallCmd() function
*              - Enable the clock stretching using I2C_StretchClockCmd() function
*              - Enable the fast mode duty cycle using the I2C_FastModeDutyCycleConfig()
*                function.
*              - Configure the NACK position for Master Receiver mode in case of 
*                2 bytes reception using the function I2C_NACKPositionConfig().  
*              - Enable the PEC Calculation using I2C_CalculatePEC() function
*              - For SMBus Mode: 
*                   - Enable the Address Resolution Protocol (ARP) using I2C_ARPCmd() function
*                   - Configure the SMBusAlert pin using I2C_SMBusAlertConfig() function
*          6. Enable the NVIC and the corresponding interrupt using the function 
*             I2C_ITConfig() if you need to use interrupt mode. 
*          7. When using the DMA mode 
*                   - Configure the DMA using DMA_Init() function
*                   - Active the needed channel Request using I2C_DMACmd() or
*                     I2C_DMALastTransferCmd() function.
*              @note When using DMA mode, I2C interrupts may be used at the same time to
*                    control the communication flow (Start/Stop/Ack... events and errors).
*          8. Enable the I2C using the I2C_Cmd() function.
*          9. Enable the DMA using the DMA_Cmd() function when using DMA mode in the 
*             transfers. 

© COPYRIGHT 2012 STMicroelectronics

Licensed under MCD-ST Liberty SW License Agreement V2, (the "License"); You may not use this file except in compliance with the License. You may obtain a copy of the License at:

Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.