📄 75x_i2c.c
字号:
/******************** (C) COPYRIGHT 2006 STMicroelectronics ********************
* File Name : 75x_i2c.c
* Author : MCD Application Team
* Date First Issued : 03/10/2006 : V0.1
* Description : This file provides all the I2C software functions.
********************************************************************************
* History:
* 03/10/2006 : V0.1
********************************************************************************
* 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 "75x_i2c.h"
#include "75x_mrcc.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
/* 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
/* Address direction bit */
#define I2C_ADD0_Set 0x01
#define I2C_ADD0_Reset 0xFE
/* 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
/* 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 : None
* Output : None
* Return : None
*******************************************************************************/
void I2C_DeInit(void)
{
/* Reset the I2C registers values*/
MRCC_PeripheralSWResetConfig(MRCC_Peripheral_I2C,ENABLE);
MRCC_PeripheralSWResetConfig(MRCC_Peripheral_I2C,DISABLE);
}
/*******************************************************************************
* Function Name : I2C_Init
* Description : Initializes the I2C peripheral according to the specified
* parameters in the I2C_Initstruct.
* Input : - I2C_InitStruct: pointer to a I2C_InitTypeDef structure that
* contains the configuration information for the specified I2C
* peripheral.
* Output : None
* Return : None
*******************************************************************************/
void I2C_Init(I2C_InitTypeDef* I2C_InitStruct)
{
u8 ITEState = 0;
u16 Result = 0x0F;
u32 APBClock = 8000000;
MRCC_ClocksTypeDef MRCC_ClocksStatus;
/* Get APBClock frequency value */
MRCC_GetClocksStatus(&MRCC_ClocksStatus);
APBClock = MRCC_ClocksStatus.PCLK_Frequency;
/* Save ITE bit state */
ITEState = I2C->CR & 0xFE;
/* Disable I2C peripheral to set FR[2:0] bits */
I2C_Cmd(DISABLE);
/* Clear frequency FR[2:0] bits */
I2C->OAR2 &= I2C_Frequency_Mask;
/* Set frequency bits depending on APBClock value */
if (APBClock < 10000000)
I2C->OAR2 &= 0x1F;
else if (APBClock < 16670000)
I2C->OAR2 |= 0x20;
else if (APBClock < 26670000)
I2C->OAR2 |= 0x40;
else if (APBClock < 40000000)
I2C->OAR2 |= 0x60;
else if (APBClock < 53330000)
I2C->OAR2 |= 0x80;
else if (APBClock < 66000000)
I2C->OAR2 |= 0xA0;
else if (APBClock < 80000000)
I2C->OAR2 |= 0xC0;
else if (APBClock < 100000000)
I2C->OAR2 |= 0xE0;
I2C_Cmd(ENABLE);
/* Restore the ITE bit state */
I2C->CR |= ITEState;
/* Configure general call */
if (I2C_InitStruct->I2C_GeneralCall == I2C_GeneralCall_Enable)
{
/* Enable general call */
I2C->CR |= I2C_GeneralCall_Enable;
}
else
{
/* Disable general call */
I2C->CR &= I2C_GeneralCall_Disable;
}
/* Configure acknowledgement */
if (I2C_InitStruct->I2C_Ack == I2C_Ack_Enable)
{
/* Enable acknowledgement */
I2C->CR |= I2C_Ack_Enable;
}
else
{
/* Disable acknowledgement */
I2C->CR &= I2C_Ack_Disable;
}
/* Configure LSB own address */
I2C->OAR1 = I2C_InitStruct->I2C_OwnAddress;
/* Clear MSB own address ADD[9:8] bits */
I2C->OAR2 &= I2C_AddressHigh_Mask;
/* Set MSB own address value */
I2C->OAR2 |= (I2C_InitStruct->I2C_OwnAddress & I2C_OwnAddress_Mask)>>7;
/* Configure speed in standard mode */
if (I2C_InitStruct->I2C_CLKSpeed <= 100000)
{
/* Standard mode speed calculate */
Result = ((APBClock/I2C_InitStruct->I2C_CLKSpeed)-7)/2;
/* Set speed value and clear FM/SM bit for standard mode in LSB clock divider */
I2C->CCR = Result & I2C_StandardMode_Mask;
}
/* Configure speed in fast mode */
else if (I2C_InitStruct->I2C_CLKSpeed <= 400000)
{
/* Fast mode speed calculate */
Result = ((APBClock/I2C_InitStruct->I2C_CLKSpeed)-9)/3;
/* Set speed value and set FM/SM bit for fast mode in LSB clock divider */
I2C->CCR = Result | I2C_FastMode_Mask;
}
/* Set speed in MSB clock divider */
I2C->ECCR = Result >>7;
}
/*******************************************************************************
* Function Name : I2C_StructInit
* Description : Fills each I2C_InitStruct member with its default value.
* 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 I2C peripheral.
* Input : - NewState: new state of the I2C peripheral. This parameter
* can be: ENABLE or DISABLE.
* Output : None
* Return : None.
*******************************************************************************/
void I2C_Cmd(FunctionalState NewState)
{
if (NewState == ENABLE)
{
/* Enable the I2C peripheral by setting twice the PE bit on the CR register */
I2C->CR |= I2C_PE_Set;
I2C->CR |= I2C_PE_Set;
}
else
{
/* Disable the I2C peripheral */
I2C->CR &= I2C_PE_Reset;
}
}
/*******************************************************************************
* Function Name : I2C_GenerateSTART
* Description : Generates I2C communication START condition.
* Input : - NewState: new state of the I2C START condition generation.
* This parameter can be: ENABLE or DISABLE.
* Output : None
* Return : None.
*******************************************************************************/
void I2C_GenerateSTART(FunctionalState NewState)
{
if (NewState == ENABLE)
{
/* Generate a START condition */
I2C->CR |= I2C_Start_Enable;
}
else
{
/* Disable the START condition generation */
I2C->CR &= I2C_Start_Disable;
}
}
/*******************************************************************************
* Function Name : I2C_GenerateSTOP
* Description : Generates I2C communication STOP condition.
* Input : - NewState: new state of the I2C STOP condition generation.
* This parameter can be: ENABLE or DISABLE.
* Output : None
* Return : None.
*******************************************************************************/
void I2C_GenerateSTOP(FunctionalState NewState)
{
if (NewState == ENABLE)
{
/* Generate a SIOP condition */
I2C->CR |= I2C_Stop_Enable;
}
else
{
/* Disable the STOP condition generation */
I2C->CR &= I2C_Stop_Disable;
}
}
/*******************************************************************************
* Function Name : I2C_AcknowledgeConfig
* Description : Enables or disables I2C acknowledge feature.
* Input : - NewState: new state of the I2C Acknowledgement.
* This parameter can be: ENABLE or DISABLE.
* Output : None
* Return : None.
*******************************************************************************/
void I2C_AcknowledgeConfig(FunctionalState NewState)
{
if (NewState == ENABLE)
{
/* Enable the acknowledgement */
I2C->CR |= I2C_Ack_Enable;
}
else
{
/* Disable the acknowledgement */
I2C->CR &= I2C_Ack_Disable;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -