⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 91x_i2c.c

📁 STR912 arm9实现的以太网通信程序
💻 C
📖 第 1 页 / 共 2 页
字号:
/******************** (C) COPYRIGHT 2007 STMicroelectronics ********************
* File Name          : 91x_i2c.c
* Author             : MCD Application Team
* Version            : V2.0
* Date               : 12/07/2007
* Description        : This file provides all the I2C firmware functions.
********************************************************************************
* THE PRESENT SOFTWARE 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 SOFTWARE AND/OR THE USE MADE BY CUSTOMERS OF THE CODING
* INFORMATION CONTAINED HEREIN IN CONNECTION WITH THEIR PRODUCTS.
*******************************************************************************/

/* Includes ------------------------------------------------------------------*/
#include "91x_i2c.h"
#include "91x_scu.h"

/* Private typedef -----------------------------------------------------------*/
/* Private define ------------------------------------------------------------*/

/* I2C IT enable */
#define  I2C_IT_Enable     0x01
#define  I2C_IT_Disable    0xFE

/* I2C Peripheral Enable/Disable */
#define  I2C_PE_Set        0x20
#define  I2C_PE_Reset      0xDF

/* Address direction bit */
#define I2C_ADD0_Set       0x01
#define I2C_ADD0_Reset     0xFE

/* I2C START Enable/Disable */
#define  I2C_Start_Enable       0x08
#define  I2C_Start_Disable      0xF7

/* I2C STOP Enable/Disable */
#define  I2C_Stop_Enable        0x02
#define  I2C_Stop_Disable       0xFD

/* I2C Masks */
#define  I2C_Frequency_Mask     0x1F
#define  I2C_AddressHigh_Mask   0xF9
#define  I2C_OwnAddress_Mask    0x0300
#define  I2C_StandardMode_Mask  0x7f
#define  I2C_FastMode_Mask      0x80
#define  I2C_Event_Mask         0x3FFF
#define  I2C_HeaderSet_Mask     0xF1
#define  I2C_HeaderReset_Mask   0xFE

/* Private macro -------------------------------------------------------------*/
/* Private variables ---------------------------------------------------------*/
/* Private function prototypes -----------------------------------------------*/
/* Private functions ---------------------------------------------------------*/
/*******************************************************************************
* Function Name  : I2C_DeInit
* Description    : Deinitializes the I2C peripheral registers to their default
*                  reset values.
* Input          :- I2Cx: I2C peripheral can be:
*                    - I2C0
*					 - I2C1  
* Output         : None
* Return         : None
*******************************************************************************/
void I2C_DeInit(I2C_TypeDef* I2Cx)
{
  if (I2Cx == I2C0)
  {
  /* Reset the I2C0 registers values */
   SCU_APBPeriphReset(__I2C0, ENABLE);
   SCU_APBPeriphReset(__I2C0, DISABLE);
  }
   if (I2Cx == I2C1)
  {
  /* Reset the I2C1 registers values */
  SCU_APBPeriphReset(__I2C1, ENABLE);
  SCU_APBPeriphReset(__I2C1, DISABLE);
  }
}

/*******************************************************************************
* Function Name  : I2C_Init
* Description    : Initializes the I2C  peripheral according to the specified
*                  parameters in the I2C_InitTypeDef structure.
* Input          :- I2Cx: I2C peripheral can be:
*                    - I2C0
*					 - I2C1  
*
*                  - I2C_InitStruct: pointer to an I2C_InitTypeDef structure that
*                  contains the configuration information for the specified I2C
*                  peripheral.
* Output         : None
* Return         : None
*******************************************************************************/
void I2C_Init(I2C_TypeDef* I2Cx, I2C_InitTypeDef* I2C_InitStruct)
{
  u16 wResult = 0x0F;
  u32 dPCLK = 25000000;

  /* Get PCLK frequency value */
 dPCLK = SCU_GetPCLKFreqValue()*1000;
  /* Disable I2C peripheral to set FR[2:0] bits */
  I2C_Cmd (I2Cx, DISABLE);
  /* Clear frequency FR[2:0] bits */
  I2Cx->OAR2 &= I2C_Frequency_Mask;
  /* Set frequency bits depending on PCLK value */
  if ((dPCLK <1667000) & (dPCLK > 10000000))
    I2Cx->OAR2 |= 0x20;
  else if (dPCLK < 26670000)
    I2Cx->OAR2 |= 0x40;
  else if (dPCLK < 40000000)
    I2Cx->OAR2 |= 0x60;
  else if (dPCLK < 53330000)
    I2Cx->OAR2 |= 0x80;
  else if (dPCLK < 66000000)
    I2Cx->OAR2 |= 0xA0;
  else if (dPCLK < 80000000)
    I2Cx->OAR2 |= 0xC0;
  else if (dPCLK < 100000000)
    I2Cx->OAR2 |= 0xE0;
  I2C_Cmd (I2Cx, ENABLE);

  /* Configure general call */
  if (I2C_InitStruct->I2C_GeneralCall == I2C_GeneralCall_Enable)
  {
    /* Enable general call */
    I2Cx->CR |= I2C_GeneralCall_Enable;
  }
  else
  {
    /* Disable general call */
    I2Cx->CR &= I2C_GeneralCall_Disable;
  }
  /* Configure acknowledgement */
  if (I2C_InitStruct->I2C_Ack == I2C_Ack_Enable)
  {
    /* Enable acknowledgement */
    I2Cx->CR |= I2C_Ack_Enable;
  }
  else
  {
    /* Disable acknowledgement */
    I2Cx->CR &= I2C_Ack_Disable;
  }

  /* Configure LSB own address */
  I2Cx->OAR1 = I2C_InitStruct->I2C_OwnAddress;
  /* Clear MSB own address ADD[9:8] bits */
  I2Cx->OAR2 &= I2C_AddressHigh_Mask;
  /* Set MSB own address value */
  I2Cx->OAR2 |= (I2C_InitStruct->I2C_OwnAddress & I2C_OwnAddress_Mask)>>7;

  /* Configure speed in standard mode */
  if (I2C_InitStruct->I2C_CLKSpeed <= 100000)
  {
    /* Standard mode speed calculate */
    wResult = ((dPCLK/I2C_InitStruct->I2C_CLKSpeed)-7)/2;
    /* Set speed value and clear FM/SM bit for standard mode in LSB clock divider */
    I2Cx->CCR = wResult & I2C_StandardMode_Mask;
  }
  /* Configure speed in fast mode */
  else if (I2C_InitStruct->I2C_CLKSpeed <= 400000)
  {
    /* Fast mode speed calculate */
    wResult = ((dPCLK/I2C_InitStruct->I2C_CLKSpeed)-9)/3;
    /* Set speed value and set FM/SM bit for fast mode in LSB clock divider */
    I2Cx->CCR = wResult | I2C_FastMode_Mask;
  }
  /* Set speed in MSB clock divider */
  I2Cx->ECCR = wResult >>7;
}

/*******************************************************************************
* Function Name  : I2C_StructInit		         		
* Description    : Initialize the I2C Init Structure parameters
* Input          : - I2C_InitStruct: pointer to an I2C_InitTypeDef structure
                     which will be initialized.
* Output         : None	
* Return         : None.						
*******************************************************************************/
void I2C_StructInit(I2C_InitTypeDef* I2C_InitStruct)
{
  /* Initialize the I2C_CLKSpeed member */
  I2C_InitStruct->I2C_CLKSpeed = 5000;

  /* Initialize the I2C_OwnAddress member */
  I2C_InitStruct->I2C_OwnAddress = 0x0;

  /* Initialize the I2C_GeneralCall member */
  I2C_InitStruct->I2C_GeneralCall = I2C_GeneralCall_Disable;

  /* Initialize the I2C_Ack member */
  I2C_InitStruct->I2C_Ack = I2C_Ack_Disable;
}

/*******************************************************************************
* Function Name  : I2C_Cmd
* Description    : Enables or disables the specified I2C peripheral.
* Input          :- I2Cx: I2C peripheral can be:
*                    - I2C0
*					 - I2C1  
*                 - NewState: new state of the I2C peripheral. This parameter
*                    can be: ENABLE or DISABLE.
* Output         : None
* Return         : None.
*******************************************************************************/
void I2C_Cmd(I2C_TypeDef* I2Cx, FunctionalState NewState)
{
  if (NewState == ENABLE)
  {
    /* Enable the I2C peripheral by setting twice the PE bit on the CR register */
    I2Cx->CR |= I2C_PE_Set;
	  I2Cx->CR |= I2C_PE_Set;
  }
  else
  {
    /* Disable the I2C peripheral */
    I2Cx->CR &= I2C_PE_Reset;
  }
}

/*******************************************************************************
* Function Name  : I2C_GenerateSTART
* Description    : Generates I2C communication START condition.
* Input          :- I2Cx: I2C peripheral can be:
*                    - I2C0
*					 - I2C1  
*
*                 - NewState: new state of the Start condition. This parameter
*                    can be: ENABLE or DISABLE.
* Output         : None
* Return         : None.
*******************************************************************************/
void I2C_GenerateStart(I2C_TypeDef* I2Cx, FunctionalState NewState)
{
  if (NewState == ENABLE)
  {
    /* Generate a START condition */
    I2Cx->CR |= I2C_Start_Enable;
  }
  else
  {
    /* Disable the START condition generation */
    I2Cx->CR &= I2C_Start_Disable;
  }
}

/*******************************************************************************
* Function Name  : I2C_GenerateSTOP
* Description    : Generates I2C communication STOP condition.
* Input          :- I2Cx: I2C peripheral can be:
*                    - I2C0
*					 - I2C1  
*
*                  - NewState: new state of the Stop condition. This parameter
*                    can be: ENABLE or DISABLE.
* Output         : None
* Return         : None.
*******************************************************************************/
void I2C_GenerateSTOP(I2C_TypeDef* I2Cx, FunctionalState NewState)
{
  if (NewState == ENABLE)
  {
    /* Generate a SIOP condition */
    I2Cx->CR |= I2C_Stop_Enable;
  }
  else
  {
    /* Disable the STOP condition generation */
    I2Cx->CR &= I2C_Stop_Disable;
  }
}

/*******************************************************************************
* Function Name  : I2C_AcknowledgeConfig
* Description    : Enables or disables I2C acknowledge feature.
* Input          :- I2Cx: I2C peripheral can be:
*                    - I2C0
*					 - I2C1  
*                 - NewState: new state of the Acknowledgement. This parameter
*                    can be: ENABLE or DISABLE.
* Output         : None
* Return         : None.
*******************************************************************************/
void I2C_AcknowledgeConfig(I2C_TypeDef *I2Cx, FunctionalState NewState)
{
  if (NewState == ENABLE)
  {
    /* Enable the acknowledgement */
    I2Cx->CR |= I2C_Ack_Enable;
  }
  else
  {
    /* Disable the acknowledgement */
    I2Cx->CR &= I2C_Ack_Disable;
  }
}

/*******************************************************************************
* Function Name  : I2C_ITConfig
* Description    : Enables or disables I2C interrupt feature.
* Input          :- I2Cx: I2C peripheral can be:
*                    - I2C0
*					 - I2C1  
*                  - NewState: new state of the specified I2C interrupt.
*                    This parameter can be: ENABLE or DISABLE.

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -