Home » Source Code » MPU6050 (hardware IIC) » stm32f10x_i2c.c

stm32f10x_i2c.c ( File view )

  • By 昱晨 2015-04-27
  • View(s):8
  • Download(s):2
  • Point(s): 0
			/**
  ******************************************************************************
  * @file    stm32f10x_i2c.c
  * @author  MCD Application Team
  * @version V3.5.0
  * @date    11-March-2011
  * @brief   This file provides all the I2C firmware functions.
  ******************************************************************************
  * @attention
  *
  * THE PRESENT FIRMWARE WHICH IS FOR GUIDANCE ONLY AIMS AT PROVIDING CUSTOMERS
  * WITH CODING INFORMATION REGARDING THEIR PRODUCTS IN ORDER FOR THEM TO SAVE
  * TIME. AS A RESULT, STMICROELECTRONICS SHALL NOT BE HELD LIABLE FOR ANY
  * DIRECT, INDIRECT OR CONSEQUENTIAL DAMAGES WITH RESPECT TO ANY CLAIMS ARISING
  * FROM THE CONTENT OF SUCH FIRMWARE AND/OR THE USE MADE BY CUSTOMERS OF THE
  * CODING INFORMATION CONTAINED HEREIN IN CONNECTION WITH THEIR PRODUCTS.
  *
  * <h2><center>&copy; COPYRIGHT 2011 STMicroelectronics</center></h2>
  ******************************************************************************
  */

/* Includes ------------------------------------------------------------------*/
#include "stm32f10x_i2c.h"
#include "stm32f10x_rcc.h"


/** @addtogroup STM32F10x_StdPeriph_Driver
  * @{

  */

/** @defgroup I2C 
  * @brief I2C driver modules
  * @{

  */ 

/** @defgroup I2C_Private_TypesDefinitions
  * @{

  */

/**
  * @
}
  */

/** @defgroup I2C_Private_Defines
  * @{

  */

/* I2C SPE mask */
#define CR1_PE_Set              ((uint16_t)0x0001)
#define CR1_PE_Reset            ((uint16_t)0xFFFE)

/* I2C START mask */
#define CR1_START_Set           ((uint16_t)0x0100)
#define CR1_START_Reset         ((uint16_t)0xFEFF)

/* I2C STOP mask */
#define CR1_STOP_Set            ((uint16_t)0x0200)
#define CR1_STOP_Reset          ((uint16_t)0xFDFF)

/* I2C ACK mask */
#define CR1_ACK_Set             ((uint16_t)0x0400)
#define CR1_ACK_Reset           ((uint16_t)0xFBFF)

/* I2C ENGC mask */
#define CR1_ENGC_Set            ((uint16_t)0x0040)
#define CR1_ENGC_Reset          ((uint16_t)0xFFBF)

/* I2C SWRST mask */
#define CR1_SWRST_Set           ((uint16_t)0x8000)
#define CR1_SWRST_Reset         ((uint16_t)0x7FFF)

/* I2C PEC mask */
#define CR1_PEC_Set             ((uint16_t)0x1000)
#define CR1_PEC_Reset           ((uint16_t)0xEFFF)

/* I2C ENPEC mask */
#define CR1_ENPEC_Set           ((uint16_t)0x0020)
#define CR1_ENPEC_Reset         ((uint16_t)0xFFDF)

/* I2C ENARP mask */
#define CR1_ENARP_Set           ((uint16_t)0x0010)
#define CR1_ENARP_Reset         ((uint16_t)0xFFEF)

/* I2C NOSTRETCH mask */
#define CR1_NOSTRETCH_Set       ((uint16_t)0x0080)
#define CR1_NOSTRETCH_Reset     ((uint16_t)0xFF7F)

/* I2C registers Masks */
#define CR1_CLEAR_Mask          ((uint16_t)0xFBF5)

/* I2C DMAEN mask */
#define CR2_DMAEN_Set           ((uint16_t)0x0800)
#define CR2_DMAEN_Reset         ((uint16_t)0xF7FF)

/* I2C LAST mask */
#define CR2_LAST_Set            ((uint16_t)0x1000)
#define CR2_LAST_Reset          ((uint16_t)0xEFFF)

/* I2C FREQ mask */
#define CR2_FREQ_Reset          ((uint16_t)0xFFC0)

/* I2C ADD0 mask */
#define OAR1_ADD0_Set           ((uint16_t)0x0001)
#define OAR1_ADD0_Reset         ((uint16_t)0xFFFE)

/* I2C ENDUAL mask */
#define OAR2_ENDUAL_Set         ((uint16_t)0x0001)
#define OAR2_ENDUAL_Reset       ((uint16_t)0xFFFE)

/* I2C ADD2 mask */
#define OAR2_ADD2_Reset         ((uint16_t)0xFF01)

/* I2C F/S mask */
#define CCR_FS_Set              ((uint16_t)0x8000)

/* I2C CCR mask */
#define CCR_CCR_Set             ((uint16_t)0x0FFF)

/* I2C FLAG mask */
#define FLAG_Mask               ((uint32_t)0x00FFFFFF)

/* I2C Interrupt Enable mask */
#define ITEN_Mask               ((uint32_t)0x07000000)

/**
  * @
}
  */

/** @defgroup I2C_Private_Macros
  * @{

  */

/**
  * @
}
  */

/** @defgroup I2C_Private_Variables
  * @{

  */

/**
  * @
}
  */

/** @defgroup I2C_Private_FunctionPrototypes
  * @{

  */

/**
  * @
}
  */

/** @defgroup I2C_Private_Functions
  * @{

  */

/**
  * @brief  Deinitializes the I2Cx peripheral registers to their default reset values.
  * @param  I2Cx: where x can be 1 or 2 to select the I2C peripheral.
  * @retval None
  */
void I2C_DeInit(I2C_TypeDef* I2Cx)
{

  /* Check the parameters */
  assert_param(IS_I2C_ALL_PERIPH(I2Cx));

  if (I2Cx == I2C1)
  {

    /* Enable I2C1 reset state */
    RCC_APB1PeriphResetCmd(RCC_APB1Periph_I2C1, ENABLE);
    /* Release I2C1 from reset state */
    RCC_APB1PeriphResetCmd(RCC_APB1Periph_I2C1, DISABLE);
  
}
  else
  {

    /* Enable I2C2 reset state */
    RCC_APB1PeriphResetCmd(RCC_APB1Periph_I2C2, ENABLE);
    /* Release I2C2 from reset state */
    RCC_APB1PeriphResetCmd(RCC_APB1Periph_I2C2, DISABLE);
  
}

}

/**
  * @brief  Initializes the I2Cx peripheral according to the specified 
  *   parameters in the I2C_InitStruct.
  * @param  I2Cx: where x can be 1 or 2 to select the I2C peripheral.
  * @param  I2C_InitStruct: pointer to a I2C_InitTypeDef structure that
  *   contains the configuration information for the specified I2C peripheral.
  * @retval None
  */
void I2C_Init(I2C_TypeDef* I2Cx, I2C_InitTypeDef* I2C_InitStruct)
{

  uint16_t tmpreg = 0, freqrange = 0;
  uint16_t result = 0x04;
  uint32_t pclk1 = 8000000;
  RCC_ClocksTypeDef  rcc_clocks;
  /* Check the parameters */
  assert_param(IS_I2C_ALL_PERIPH(I2Cx));
  assert_param(IS_I2C_CLOCK_SPEED(I2C_InitStruct->I2C_ClockSpeed));
  assert_param(IS_I2C_MODE(I2C_InitStruct->I2C_Mode));
  assert_param(IS_I2C_DUTY_CYCLE(I2C_InitStruct->I2C_DutyCycle));
  assert_param(IS_I2C_OWN_ADDRESS1(I2C_InitStruct->I2C_OwnAddress1));
  assert_param(IS_I2C_ACK_STATE(I2C_InitStruct->I2C_Ack));
  assert_param(IS_I2C_ACKNOWLEDGE_ADDRESS(I2C_InitStruct->I2C_AcknowledgedAddress));

/*---------------------------- I2Cx CR2 Configuration ------------------------*/
  /* Get the I2Cx CR2 value */
  tmpreg = I2Cx->CR2;
  /* Clear frequency FREQ[5:0] bits */
  tmpreg &= CR2_FREQ_Reset;
  /* Get pclk1 frequency value */
  RCC_GetClocksFreq(&rcc_clocks);
  pclk1 = rcc_clocks.PCLK1_Frequency;
  /* Set frequency bits depending on pclk1 value */
  freqrange = (uint16_t)(pclk1 / 1000000);
  tmpreg |= freqrange;
  /* Write to I2Cx CR2 */
  I2Cx->CR2 = tmpreg;

/*---------------------------- I2Cx CCR Configuration ------------------------*/
  /* Disable the selected I2C peripheral to configure TRISE */
  I2Cx->CR1 &= CR1_PE_Reset;
  /* Reset tmpreg value */
  /* Clear F/S, DUTY and CCR[11:0] bits */
  tmpreg = 0;

  /* Configure speed in standard mode */
  if (I2C_InitStruct->I2C_ClockSpeed <= 100000)
  {

    /* Standard mode speed calculate */
    result = (uint16_t)(pclk1 / (I2C_InitStruct->I2C_ClockSpeed << 1));
    /* Test if CCR value is under 0x4*/
    if (result < 0x04)
    {

      /* Set minimum allowed value */
      result = 0x04;  
    
}
    /* Set speed value for standard mode */
    tmpreg |= result;	  
    /* Set Maximum Rise Time for standard mode */
    I2Cx->TRISE = freqrange + 1; 
  
}
  /* Configure speed in fast mode */
  else /*(I2C_InitStruct->I2C_ClockSpeed <= 400000)*/
  {

    if (I2C_InitStruct->I2C_DutyCycle == I2C_DutyCycle_2)
    {

      /* Fast mode speed calculate: Tlow/Thigh = 2 */
      result = (uint16_t)(pclk1 / (I2C_InitStruct->I2C_ClockSpeed * 3));
    
}
    else /*I2C_InitStruct->I2C_DutyCycle == I2C_DutyCycle_16_9*/
    {

      /* Fast mode speed calculate: Tlow/Thigh = 16/9 */
      result = (uint16_t)(pclk1 / (I2C_InitStruct->I2C_ClockSpeed * 25));
      /* Set DUTY bit */
      result |= I2C_DutyCycle_16_9;
    
}

    /* Test if CCR value is under 0x1*/
    if ((result & CCR_CCR_Set) == 0)
    {

      /* Set minimum allowed value */
      result |= (uint16_t)0x0001;  
    
}
    /* Set speed value and set F/S bit for fast mode */
    tmpreg |= (uint16_t)(result | CCR_FS_Set);
    /* Set Maximum Rise Time for fast mode */
    I2Cx->TRISE = (uint16_t)(((freqrange * (uint16_t)300) / (uint16_t)1000) + (uint16_t)1);  
  
}

  /* Write to I2Cx CCR */
  I2Cx->CCR = tmpreg;
  /* Enable the selected I2C peripheral */
  I2Cx->CR1 |= CR1_PE_Set;

/*---------------------------- I2Cx CR1 Configuration ------------------------*/
  /* Get the I2Cx CR1 value */
  tmpreg = I2Cx->CR1;
  /* Clear ACK, SMBTYPE and  SMBUS bits */
  tmpreg &= CR1_CLEAR_Mask;
  /* Configure I2Cx: mode and acknowledgement */
  /* Set SMBTYPE and SMBUS bits according to I2C_Mode value */
  /* Set ACK bit according to I2C_Ack value */
  tmpreg |= (uint16_t)((uint32_t)I2C_InitStruct->I2C_Mode | I2C_InitStruct->I2C_Ack);
  /* Write to I2Cx CR1 */
  I2Cx->CR1 = tmpreg;

/*---------------------------- I2Cx OAR1 Configuration -----------------------*/
  /* Set I2Cx Own Address1 and acknowledged address */
  I2Cx->OAR1 = (I2C_InitStruct->I2C_AcknowledgedAddress | I2C_InitStruct->I2C_OwnAddress1);

}

/**
  * @brief  Fills each I2C_InitStruct member with its default value.
  * @param  I2C_InitStruct: pointer to an I2C_InitTypeDef structure which will be initialized.
  * @retval None
  */
void I2C_StructInit(I2C_InitTypeDef* I2C_InitStruct)
{

/*---------------- Reset I2C init structure parameters values ----------------*/
  /* initialize the I2C_ClockSpeed member */
  I2C_InitStruct->I2C_ClockSpeed = 5000;
  /* Initialize the I2C_Mode member */
  I2C_InitStruct->I2C_Mode = I2C_Mode_I2C;
  /* Initialize the I2C_DutyCycle member */
  I2C_InitStruct->I2C_DutyCycle = I2C_DutyCycle_2;
  /* Initialize the I2C_OwnAddress1 member */
  I2C_InitStruct->I2C_OwnAddress1 = 0;
  /* Initialize the I2C_Ack member */
  I2C_InitStruct->I2C_Ack = I2C_Ack_Disable;
  /* Initialize the I2C_AcknowledgedAddress member */
  I2C_InitStruct->I2C_AcknowledgedAddress = I2C_AcknowledgedAddress_7bit;

}

/**
  * @brief  Enables or disables the specified I2C peripheral.
  * @param  I2Cx: where x can be 1 or 2 to select the I2C peripheral.
  * @param  NewState: new state of the I2Cx peripheral. 
  *   This parameter can be: ENABLE or DISABLE.
  * @retval None
  */
void I2C_Cmd(I2C_TypeDef* I2Cx, FunctionalState NewState)
{

  /* Check the para
...
...
(Not finished, please download and read the complete file)
			
...
Expand> <Close

Want complete source code? Download it here

Point(s): 0

Download
0 lines left, continue to read
Sponsored links

File list

Tips: You can preview the content of files by clicking file names^_^
Name Size Date
01.97 kB
01.97 kB
core_cm3.c16.87 kB2010-06-07 10:25
core_cm3.h83.71 kB2011-02-09 14:59
startup_stm32f10x_hd.s15.14 kB2011-03-10 10:52
stm32f10x.h619.08 kB2011-03-10 10:51
system_stm32f10x.c35.70 kB2011-03-10 10:51
system_stm32f10x.h2.04 kB2011-03-10 10:51
01.97 kB
01.97 kB
misc.h8.77 kB2011-03-10 10:47
stm32f10x_adc.h21.18 kB2011-03-10 10:47
stm32f10x_bkp.h7.38 kB2011-03-10 10:47
stm32f10x_can.h26.91 kB2011-03-10 10:47
stm32f10x_cec.h6.42 kB2011-03-10 10:47
stm32f10x_crc.h2.11 kB2011-03-10 10:47
stm32f10x_dac.h14.88 kB2011-03-10 10:47
stm32f10x_dbgmcu.h3.73 kB2011-03-10 10:47
stm32f10x_dma.h20.27 kB2011-03-10 10:47
stm32f10x_exti.h6.66 kB2011-03-10 10:47
stm32f10x_flash.h24.85 kB2011-03-10 10:47
stm32f10x_fsmc.h26.38 kB2011-03-10 10:47
stm32f10x_gpio.h19.70 kB2011-03-10 10:47
stm32f10x_i2c.h29.33 kB2011-03-10 10:47
stm32f10x_iwdg.h3.74 kB2011-03-10 10:47
stm32f10x_pwr.h4.28 kB2011-03-10 10:47
stm32f10x_rcc.h29.74 kB2011-03-10 10:47
stm32f10x_rtc.h3.77 kB2011-03-10 10:47
stm32f10x_sdio.h21.35 kB2011-03-10 10:47
stm32f10x_spi.h17.31 kB2011-03-10 10:47
stm32f10x_tim.h51.20 kB2011-03-10 10:47
stm32f10x_usart.h16.16 kB2011-03-10 10:47
stm32f10x_wwdg.h2.90 kB2011-03-10 10:47
01.97 kB
misc.c6.88 kB2011-03-10 10:47
stm32f10x_adc.c46.09 kB2011-03-10 10:47
stm32f10x_bkp.c8.26 kB2011-03-10 10:47
stm32f10x_can.c44.05 kB2011-03-10 10:47
stm32f10x_cec.c11.38 kB2011-03-10 10:47
stm32f10x_crc.c3.27 kB2011-03-10 10:47
stm32f10x_dac.c18.64 kB2011-03-10 10:47
stm32f10x_dbgmcu.c5.03 kB2011-03-10 10:47
stm32f10x_dma.c28.91 kB2011-03-10 10:47
stm32f10x_exti.c6.80 kB2011-03-10 10:47
stm32f10x_flash.c61.08 kB2011-03-10 10:47
stm32f10x_fsmc.c34.65 kB2011-03-10 10:47
stm32f10x_gpio.c22.68 kB2011-03-11 17:43
stm32f10x_i2c.c44.71 kB2011-03-10 10:47
stm32f10x_iwdg.c4.80 kB2011-03-10 10:47
stm32f10x_pwr.c8.55 kB2011-03-10 10:47
stm32f10x_rcc.c50.07 kB2011-03-10 10:47
stm32f10x_rtc.c8.40 kB2011-03-10 10:47
stm32f10x_sdio.c28.25 kB2011-03-10 10:47
stm32f10x_spi.c29.52 kB2011-03-10 10:47
stm32f10x_tim.c106.60 kB2011-03-10 10:47
stm32f10x_usart.c37.41 kB2011-03-10 10:47
stm32f10x_wwdg.c5.60 kB2011-03-10 10:47
01.97 kB
01.97 kB
systick.hex18.40 kB2013-08-15 19:23
01.97 kB
I2C_MPU6050.c4.69 kB2013-08-15 19:23
I2C_MPU6050.h1.13 kB2013-08-15 19:10
I2C_MPU6050.uvgui.Orange131.20 kB2013-08-15 19:25
I2C_MPU6050.uvgui.flyleaf66.57 kB2012-06-25 20:54
I2C_MPU6050.uvopt10.45 kB2013-08-15 19:25
I2C_MPU6050.uvproj16.56 kB2013-08-15 19:00
JLink497.00 B2012-06-25 20:12
JLinkSettings.ini375.00 B2012-06-24 14:45
delay.c177.00 B2011-08-10 16:44
delay.h98.00 B2011-08-08 21:03
main.c1.48 kB2013-08-15 19:11
stm32f10x_conf.h3.22 kB2012-06-24 22:12
stm32f10x_it.c4.30 kB2011-04-04 19:03
stm32f10x_it.h2.04 kB2011-04-04 19:03
usart1.c5.33 kB2013-08-15 14:40
usart1.h234.00 B2011-08-05 21:11
keilkilll.bat399.00 B2011-04-23 10:24
...
Sponsored links

stm32f10x_i2c.c (294.78 kB)

Need 0 point
Your Point(s)
Download Download

Download failed? Click here to download one by one.

Tip: this source code project contains 2 packages, please click the allow button on the browser pop-up dialog,after you click the download button.

▪ Click to download this source code directly

LOGIN

Don't have an account? Register now
Need any help?
Mail to: support@codeforge.com

切换到中文版?

CodeForge Chinese Version
CodeForge English Version

Where are you going?

^_^"Oops ...

Sorry!This guy is mysterious, its blog hasn't been opened, try another, please!
OK

Warm tip!

CodeForge to FavoriteFavorite by Ctrl+D