#include "IIC.h"
static uint8_t I2C1Status = 0,I2C2Status = 0,I2C3Status = 0;
void I2CxPinDeInit(I2C_TypeDef* I2Cx)
{
GPIO_InitTypeDef GPIO_InitStruct;
GPIO_InitStruct.GPIO_Mode = GPIO_Mode_OUT;
GPIO_InitStruct.GPIO_OType = GPIO_OType_PP;
GPIO_InitStruct.GPIO_PuPd = GPIO_PuPd_NOPULL;
GPIO_InitStruct.GPIO_Speed = GPIO_Speed_2MHz;
if(I2Cx == I2C1)
{
}
else
if(I2Cx == I2C2)
{
}
else
if(I2Cx == I2C3)
{
GPIO_InitStruct.GPIO_Pin = GPIO_Pin_8;
RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOA,ENABLE);
GPIO_Init(GPIOA,&GPIO_InitStruct);
GPIO_SetBits(GPIOA,GPIO_Pin_8);
GPIO_InitStruct.GPIO_Pin = GPIO_Pin_9;
RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOC,ENABLE);
GPIO_Init(GPIOC,&GPIO_InitStruct);
GPIO_SetBits(GPIOC,GPIO_Pin_8);
}
else
return;
}
void I2CxPinInit(I2C_TypeDef* I2Cx)
{
GPIO_InitTypeDef GPIO_InitStruct;
GPIO_InitStruct.GPIO_Mode = GPIO_Mode_AF;
GPIO_InitStruct.GPIO_OType = GPIO_OType_OD;
GPIO_InitStruct.GPIO_PuPd = GPIO_PuPd_NOPULL;
GPIO_InitStruct.GPIO_Speed = GPIO_Speed_2MHz;
if(I2Cx == I2C1)
{
}
else
if(I2Cx == I2C2)
{
}
else
if(I2Cx == I2C3)
{
GPIO_InitStruct.GPIO_Pin = GPIO_Pin_8;
RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOA,ENABLE);
GPIO_Init(GPIOA,&GPIO_InitStruct);
GPIO_PinAFConfig(GPIOA,GPIO_PinSource8,GPIO_AF_I2C3);
GPIO_InitStruct.GPIO_Pin = GPIO_Pin_9;
RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOC,ENABLE);
GPIO_Init(GPIOC,&GPIO_InitStruct);
GPIO_PinAFConfig(GPIOC,GPIO_PinSource9,GPIO_AF_I2C3);
}
else
return;
}
void I2CxInit(I2C_TypeDef* I2Cx)
{
I2C_InitTypeDef I2C_InitStruct;
I2C_InitStruct.I2C_Mode = I2C_Mode_I2C;
I2C_InitStruct.I2C_ClockSpeed = 400000;
I2C_InitStruct.I2C_DutyCycle = I2C_DutyCycle_16_9;
I2C_InitStruct.I2C_Ack = I2C_Ack_Enable;
I2C_InitStruct.I2C_AcknowledgedAddress = I2C_AcknowledgedAddress_7bit;
I2C_InitStruct.I2C_OwnAddress1 = 0x00;
if(I2Cx == I2C1)
RCC_APB1PeriphClockCmd(RCC_APB1Periph_I2C1,ENABLE);
else if(I2Cx == I2C2)
RCC_APB1PeriphClockCmd(RCC_APB1Periph_I2C2,ENABLE);
else if(I2Cx == I2C3)
RCC_APB1PeriphClockCmd(RCC_APB1Periph_I2C3,ENABLE);
I2CxPinDeInit(I2Cx);
I2C_SoftwareResetCmd(I2Cx,ENABLE);
I2C_SoftwareResetCmd(I2Cx,DISABLE);
I2C_Init(I2Cx,&I2C_InitStruct);
I2CxPinInit(I2Cx);
I2C_Cmd(I2Cx,ENABLE);
}
uint8_t I2CxGetRunStatus(I2C_TypeDef* I2Cx)
{
if(I2Cx == I2C1)
return I2C1Status;
else if(I2Cx == I2C2)
return I2C2Status;
else if(I2Cx == I2C3)
return I2C3Status;
else
return 0xFF;
}
uint8_t I2CxWriteBuf(I2C_TypeDef* I2Cx,uint8_t SlaveAddr,uint16_t RegAddr,uint8_t RegAddrSize,uint8_t *pBuf,uint16_t Lenth)
{
uint16_t I2CTimeOut = I2CTimeOutValue;
uint8_t ErrorCode = 0;
if(I2Cx == I2C1)
I2C1Status = 1;
else if(I2Cx == I2C2)
I2C2Status = 1;
else if(I2Cx == I2C3)
I2C3Status = 1;
else
return 0xFF;
while(I2C_GetFlagStatus(I2Cx,I2C_FLAG_BUSY))
I2CxInit(I2Cx);
I2C_GenerateSTART(I2Cx,ENABLE);
while(I2C_CheckEvent(I2Cx,I2C_EVENT_MASTER_MODE_SELECT) == RESET)
{
if((I2CTimeOut --) == 0)
{
ErrorCode = 1;
goto Error;
}
}
I2CTimeOut = I2CTimeOutValue;
I2C_Send7bitAddress(I2Cx,SlaveAddr,I2C_Direction_Transmitter);
while(I2C_CheckEvent(I2Cx,I2C_EVENT_MASTER_TRANSMITTER_MODE_SELECTED) == RESET)
{
if((I2CTimeOut --) == 0)
{
ErrorCode = 2;
goto Error;
}
}
if(RegAddrSize)
{
I2C_SendData(I2Cx,RegAddr >> 8);
I2CTimeOut = I2CTimeOutValue;
while(I2C_CheckEvent(I2Cx,I2C_EVENT_MASTER_BYTE_TRANSMITTED) == RESET)
{
if((I2CTimeOut --) == 0)
{
ErrorCode = 3;
goto Error;
}
}
}
I2C_SendData(I2Cx,RegAddr & 0xFF);
I2CTimeOut = I2CTimeOutValue;
while(I2C_CheckEvent(I2Cx,I2C_EVENT_MASTER_BYTE_TRANSMITTED) == RESET)
{
if((I2CTimeOut --) == 0)
{
ErrorCode = 3;
goto Error;
}
}
for(uint32_t i = 0;i < Lenth;i ++)
{
I2C_SendData(I2Cx,pBuf[i]);
I2CTimeOut = I2CTimeOutValue;
while(I2C_CheckEvent(I2Cx,I2C_EVENT_MASTER_BYTE_TRANSMITTED) == RESET)
{
if((I2CTimeOut --) == 0)
{
ErrorCode = 4;
goto Error;
}
}
}
Error:
I2C_GenerateSTOP(I2Cx,ENABLE);
if(I2Cx == I2C1)
I2C1Status = 0;
else if(I2Cx == I2C2)
I2C2Status = 0;
else if(I2Cx == I2C3)
I2C3Status = 0;
else
return 0xFF;
return ErrorCode;
}
uint8_t I2CxReadBuf(I2C_TypeDef* I2Cx,uint8_t SlaveAddr,uint16_t RegAddr,uint8_t RegAddrSize,uint8_t *pBuf,uint16_t Lenth)
{
uint16_t I2CTimeOut = I2CTimeOutValue;
uint8_t ErrorCode = 0;
if(I2Cx == I2C1)
I2C1Status = 1;
else if(I2Cx == I2C2)
I2C2Status = 1;
else if(I2Cx == I2C3)
I2C3Status = 1;
else
return 0xFF;
while(I2C_GetFlagStatus(I2Cx,I2C_FLAG_BUSY))
I2CxInit(I2Cx);
I2C_GenerateSTART(I2Cx,ENABLE);
while(I2C_CheckEvent(I2Cx,I2C_EVENT_MASTER_MODE_SELECT) == RESET)
{
if((I2CTimeOut --) == 0)
{
ErrorCode = 1;
goto Error;
}
}
I2CTimeOut = I2CTimeOutValue;
I2C_Send7bitAddress(I2Cx,SlaveAddr,I2C_Direction_Transmitter);
while(I2C_CheckEvent(I2Cx,I2C_EVENT_MASTER_TRANSMITTER_MODE_SELECTED) == RESET)
{
if((I2CTimeOut --) == 0)
{
ErrorCode = 2;
goto Error;
}
}
if(RegAddrSize)
{
I2C_SendData(I2Cx,RegAddr >> 8);
I2CTimeOut = I2CTimeOutValue;
while(I2C_CheckEvent(I2Cx,I2C_EVENT_MASTER_BYTE_TRANSMITTED) == RESET)
{
if((I2CTimeOut --) == 0)
{
ErrorCode = 3;
goto Error;
}
}
}
I2C_SendData(I2Cx,RegAddr & 0xFF);
I2CTimeOut = I2CTimeOutValue;
while(I2C_CheckEvent(I2Cx,I2C_EVENT_MASTER_BYTE_TRANSMITTED) == RESET)
{
if((I2CTimeOut --) == 0)
{
ErrorCode = 3;
goto Error;
}
}
I2CTimeOut = I2CTimeOutValue;
I2C_GenerateSTART(I2Cx,ENABLE);
while(I2C_CheckEvent(I2Cx,I2C_EVENT_MASTER_MODE_SELECT) == RESET)
{
if((I2CTimeOut --) == 0)
{
ErrorCode = 4;
goto Error;
}
}
I2CTimeOut = I2CTimeOutValue;
I2C_Send7bitAddress(I2Cx,SlaveAddr,I2C_Direction_Receiver);
while(I2C_CheckEvent(I2Cx,I2C_EVENT_MASTER_RECEIVER_MODE_SELECTED) == RESET)
{
if((I2CTimeOut --) == 0)
{
ErrorCode = 5;
goto Error;
}
}
for(uint32_t i = 0;i < Lenth;i ++)
{
if((Lenth - i) == 1)
{
I2C_AcknowledgeConfig(I2Cx,DISABLE);
// I2C_GenerateSTOP(I2Cx,ENABLE);
}
I2CTimeOut = I2CTimeOutValue;
while(I2C_CheckEvent(I2Cx,I2C_EVENT_MASTER_BYTE_RECEIVED) == RESET)
{
if((I2CTimeOut --) == 0)
{
ErrorCode = 6;
goto Error;
}
}
pBuf[i] = I2C_ReceiveData(I2Cx);
}
Error:
I2C_GenerateSTOP(I2Cx,ENABLE);
I2C_AcknowledgeConfig(I2Cx,ENABLE);
if(I2Cx == I2C1)
I2C1Status = 0;
else if(I2Cx == I2C2)
I2C2Status = 0;
else if(I2Cx == I2C3)
I2C3Status = 0;
else
return 0xFF;
return ErrorCode;
}