📄 73x_i2c.c
字号:
/******************** (C) COPYRIGHT 2005 STMicroelectronics ********************
* File Name : 73x_i2c.c
* Author : MCD Application Team
* Date First Issued : 09/27/2005 : V1.0
* Description : This file provides all the I2C software functions.
**********************************************************************************
* History:
* 09/27/2005 : V1.0
**********************************************************************************
* 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.
*********************************************************************************/
/* Standard includes -------------------------------------------------------- */
#include "73x_i2c.h"
#include "73x_cfg.h"
/* Include of other module interface headers ---------------------------------*/
/* Local includes ------------------------------------------------------------*/
/* Private typedef -----------------------------------------------------------*/
/* Private define ------------------------------------------------------------*/
/* Private macro -------------------------------------------------------------*/
/* Private variables ---------------------------------------------------------*/
/* Private function prototypes -----------------------------------------------*/
/* Interface functions -------------------------------------------------------*/
/* Private functions ---------------------------------------------------------*/
/******************************************************************************
* Function Name : I2C_DeInit
* Description : Deinitializes the I2C peripheral registers to their default
* reset values.
* Input : - I2Cx: where x can be 0 or 1 to select the I2C peripheral.
* Output : None
* Return : None
******************************************************************************/
void I2C_DeInit (I2C_TypeDef* I2Cx)
{
/* Initialize all the registers of the specified I2C */
if( I2Cx == I2C0)
{
CFG_PeripheralClockConfig(CFG_CLK_I2C0,DISABLE);
CFG_PeripheralClockConfig(CFG_CLK_I2C0,ENABLE);
}
else if ( I2Cx == I2C1)
{
CFG_PeripheralClockConfig(CFG_CLK_I2C1,DISABLE);
CFG_PeripheralClockConfig(CFG_CLK_I2C1,ENABLE);
}
}
/******************************************************************************
* Function Name : I2C_Init
* Description : Initializes the I2Cx peripheral according to the specified
* parameters in the I2C_InitTypeDef structure.
* Input : - I2Cx: where x can be 0 or 1 to select the I2C peripheral.
* - 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 Result=0x0F;
u32 MCLK=8000000;
/* Initialize all the register of the specified I2C passed as parameter */
I2Cx->CR &= I2C_CR_Mask;
I2Cx->CCR=0x0;
I2Cx->ECCR=0x0;
I2Cx->OAR1=0x0;
I2Cx->OAR2=0xE0;
(void)I2Cx->SR1;
(void)I2Cx->SR2;
I2Cx->DR=0x0;
/* FR2-FR0 frequency bits Config referring to MCLK */
/* read the MCLK frequency */
MCLK = PRCCU_GetFrequencyValue(PRCCU_CLOCK_MCLK);
I2C_Cmd (I2Cx, DISABLE);
if (MCLK <10000000)
I2Cx->OAR2 = (I2Cx->OAR2 & 0x1F);
else if (MCLK <16670000)
I2Cx->OAR2 = (I2Cx->OAR2 & 0x1F) | 0x20;
else if (MCLK < 26670000)
I2Cx->OAR2 = (I2Cx->OAR2 & 0x1F) | 0x40;
else if (MCLK <40000000)
I2Cx->OAR2 = (I2Cx->OAR2 & 0x1F) | 0x60;
else if (MCLK < 53330000)
I2Cx->OAR2 = (I2Cx->OAR2 & 0x1F) | 0x80;
else if (MCLK < 66000000)
I2Cx->OAR2 = (I2Cx->OAR2 & 0x1F) | 0xA0;
else if (MCLK <80000000)
I2Cx->OAR2 = (I2Cx->OAR2 & 0x1F) | 0xC0;
else if (MCLK <100000000)
I2Cx->OAR2 = (I2Cx->OAR2 & 0x1F) | 0xE0;
I2C_Cmd (I2Cx, ENABLE);
/* General Call */
I2Cx->CR |= I2C_InitStruct->I2C_GeneralCall;
/* Acknowledgement */
I2Cx->CR |= I2C_InitStruct->I2C_Ack;
/* Own Address */
I2Cx->OAR1 = I2C_InitStruct->I2C_OwnAddress;
I2Cx->OAR2 |= (I2C_InitStruct->I2C_OwnAddress & 0x0300)>>7;
/* Speed and Fast/Standard Mode */
if (I2C_InitStruct->I2C_CLKSpeed <=100000)
{
/* Standard mode selected */
Result = ((MCLK/I2C_InitStruct->I2C_CLKSpeed)-7)/2;
/* Clear FM/SM bit */
I2Cx->CCR = Result &0x7f;
}
else if (I2C_InitStruct->I2C_CLKSpeed <=400000)
{
/* Fast mode selected */
Result = ((MCLK/I2C_InitStruct->I2C_CLKSpeed)-9)/3;
/* Set FM/SM bit */
I2Cx->CCR = Result |0x80;
}
I2Cx->ECCR = Result >>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)
{
/* Reset I2C init structure parameters values */
I2C_InitStruct->I2C_GeneralCall = I2C_GeneralCallDisable;
I2C_InitStruct->I2C_OwnAddress = 0x0;
I2C_InitStruct->I2C_CLKSpeed = 0x0;
I2C_InitStruct->I2C_Ack = 0x0;
}
/******************************************************************************
* Function Name : I2C_Cmd
* Description : Enables or disables the specified I2C peripheral.
* Input : - I2Cx: where x can be 0 or 1 to select the I2C peripheral.
* - NewState: new state of the I2Cx 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 selected by setting twice the PE bit on the CR register */
I2Cx->CR |= I2C_PESET_Mask;
I2Cx->CR |= I2C_PESET_Mask;
}
else
{
/* Disable the selected I2C */
I2Cx->CR &= ~I2C_PESET_Mask;
}
}
/*******************************************************************************
* Function Name : I2C_STARTGenerate
* Description : Generates I2C communication START condition.
* Input : - I2Cx: where x can be 0 or 1 to select the I2C peripheral.
* - NewState: new state of the Start condition. This parameter
* can be: ENABLE or DISABLE.
* Output : None
* Return : None.
*******************************************************************************/
void I2C_STARTGenerate (I2C_TypeDef* I2Cx, FunctionalState NewState)
{
if (NewState == ENABLE)
{
I2Cx->CR |= I2C_START_Mask;
}
else
{
I2Cx->CR &= ~I2C_START_Mask;
}
}
/******************************************************************************
* Function Name : I2C_STOPGenerate
* Description : Generates I2C communication STOP condition.
* Input : - I2Cx: where x can be 0 or 1 to select the I2C peripheral.
* - NewState: new state of the Stop condition. This parameter
* can be: ENABLE or DISABLE.
* Output : None
* Return : None.
*******************************************************************************/
void I2C_STOPGenerate (I2C_TypeDef* I2Cx, FunctionalState NewState)
{
if (NewState == ENABLE)
{
I2Cx->CR |= I2C_STOP_Mask;
}
else
{
I2Cx->CR &= ~I2C_STOP_Mask;
}
}
/******************************************************************************
* Function Name : I2C_AcknowledgeConfig
* Description : Enables or disables I2C acknowledge feature.
* Input : - I2Cx: where x can be 0 or 1 to select the I2C peripheral.
* - 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)
{
I2Cx->CR |= I2C_ACK_Mask;
}
else
{
I2Cx->CR &= ~I2C_ACK_Mask;
}
}
/*******************************************************************************
* Function Name : I2C_ITConfig
* Description : Enables or disables I2C interrupt feature.
* Input : - I2Cx: where x can be 0 or 1 to select the I2C peripheral.
* - NewState: new state of the specified I2C interrupt.
* This parameter can be: ENABLE or DISABLE.
* Output : None
* Return : None.
*******************************************************************************/
void I2C_ITConfig (I2C_TypeDef* I2Cx, FunctionalState NewState)
{
if (NewState == ENABLE)
{
I2Cx->CR |= I2C_ITEnable;
}
else
{
I2Cx->CR &= I2C_ITDisable;
}
}
/******************************************************************************************
* Function Name : I2C_GetStatus
* Description : Get the flags status in a 16 bit register.
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -