#include "IIC_Master.h"
void IIC_MasterInit()
{
GPIO_InitType GPIO_InitStuctrue;
I2C_InitType I2C_InitStruct;
MASTER_I2C_GPIO_APBxClkCmd(MASTER_I2C_SCL_GPIO_CLK|MASTER_I2C_SDA_GPIO_CLK,ENABLE);
MASTER_I2C_APBxClkCmd(MASTER_I2C_CLK,ENABLE);
GPIO_InitStuctrue.GPIO_Mode=GPIO_Mode_AF_OD;
GPIO_InitStuctrue.Pin=MASTER_I2C_SCL_GPIO_PIN;
GPIO_InitStuctrue.GPIO_Speed=GPIO_Speed_50MHz;
GPIO_InitPeripheral(MASTER_I2C_SCL_GPIO_PORT,&GPIO_InitStuctrue);
GPIO_InitStuctrue.GPIO_Mode = GPIO_Mode_AF_OD;
GPIO_InitStuctrue.Pin = MASTER_I2C_SDA_GPIO_PIN;
GPIO_InitStuctrue.GPIO_Speed=GPIO_Speed_50MHz;
GPIO_InitPeripheral(MASTER_I2C_SDA_GPIO_PORT,&GPIO_InitStuctrue);
I2C_DeInit(MASTER_I2C);
I2C_InitStruct.BusMode = I2C_BUSMODE_I2C;
I2C_InitStruct.AddrMode = I2C_ADDR_MODE_7BIT;
I2C_InitStruct.AckEnable = I2C_ACKEN;
I2C_InitStruct.FmDutyCycle = I2C_FMDUTYCYCLE_2;
I2C_InitStruct.ClkSpeed = MASTER_I2C_BAUDRATE;
I2C_InitStruct.OwnAddr1 = MASTER_ADDR;
I2C_Init(MASTER_I2C, &I2C_InitStruct);
}
//set delay time
static __IO uint32_t I2CTimeout = I2CT_LONG_TIMEOUT;
//timeout callback
static uint32_t I2C_TIMEOUT_UserCallback(uint8_t errorCode);
static uint32_t I2C_TIMEOUT_UserCallback(uint8_t errorCode)
{
/* Block communication and all processes */
IIC_MasterInit();
return 0;
}
//read n byte data other device
uint32_t IIC_ReadNData(I2C_Module* I2Cx, uint8_t dev_addr, uint8_t *data, uint16_t Num_ByteToRead)
{
I2CTimeout = I2CT_LONG_TIMEOUT;
//check IIC bus is busy
while(I2C_GetFlag(I2Cx, I2C_FLAG_BUSY))
{
if((I2CTimeout--) == 0) return I2C_TIMEOUT_UserCallback(1);
}
I2CTimeout = I2CT_FLAG_TIMEOUT;
//send start signal
I2C_GenerateStart(I2Cx,ENABLE);
//check EV5 Event
while( I2C_CheckEvent(I2Cx,I2C_EVT_MASTER_MODE_FLAG )==ERROR)
{
if((I2CTimeout--) == 0) return I2C_TIMEOUT_UserCallback(7);
}
I2CTimeout = I2CT_FLAG_TIMEOUT;
//send slave device addr
I2C_SendAddr7bit(I2Cx,dev_addr,I2C_DIRECTION_RECV);
//check EV6
while(I2C_CheckEvent(I2Cx,I2C_EVT_MASTER_RXMODE_FLAG) == ERROR)
{
if((I2CTimeout--) == 0) return I2C_TIMEOUT_UserCallback(10);
}
while(Num_ByteToRead--)
{
//this byte is end? is yes? send NACK and stop signal
if( Num_ByteToRead==0)
{
//send NACK
I2C_ConfigAck(I2Cx,DISABLE);
//send stop signal
I2C_GenerateStop(I2Cx,ENABLE);
}
I2CTimeout = I2CT_FLAG_TIMEOUT;
//check EV7
while( I2C_CheckEvent(I2Cx,I2C_EVT_MASTER_DATA_RECVD_FLAG) == ERROR)
{
if((I2CTimeout--) == 0) return I2C_TIMEOUT_UserCallback(10);
}
*data=I2C_RecvData(I2Cx);
data++;
}
//restart ACK siganl
I2C_ConfigAck(I2Cx,ENABLE);
return 1;
}
/*
write nBytes to other device
enStop; is send end byte or need send stop signal
*/
uint32_t IIC_WriteNByte(I2C_Module* I2Cx, uint8_t dev_addr, uint8_t *data, uint16_t Num_ByteToWrite, uint8_t enStop)
{
I2CTimeout = I2CT_LONG_TIMEOUT;
//check IIC bus is busy ?
while(I2C_GetFlag(I2Cx, I2C_FLAG_BUSY))
{
if((I2CTimeout--) == 0) return I2C_TIMEOUT_UserCallback(1);
}
I2CTimeout = I2CT_FLAG_TIMEOUT;
//send start signal
I2C_GenerateStart(I2Cx, ENABLE);
//check EV5
while( I2C_CheckEvent(I2Cx,I2C_EVT_MASTER_MODE_FLAG)==ERROR)
{
if((I2CTimeout--) == 0) return I2C_TIMEOUT_UserCallback(2);
}
I2CTimeout = I2CT_FLAG_TIMEOUT;
//send slave address and direction is write
I2C_SendAddr7bit(I2Cx,dev_addr,I2C_DIRECTION_SEND);
//check EV6
while( I2C_CheckEvent(I2Cx,I2C_EVT_MASTER_TXMODE_FLAG)==ERROR)
{
if((I2CTimeout--) == 0) return I2C_TIMEOUT_UserCallback(3);
}
while(Num_ByteToWrite)
{
I2C_SendData(I2Cx,*data);
I2CTimeout = I2CT_FLAG_TIMEOUT;
//check EV8
while( I2C_CheckEvent(I2Cx,I2C_EVT_MASTER_DATA_SENDING )==ERROR)
{
if((I2CTimeout--) == 0) return I2C_TIMEOUT_UserCallback(5);
}
Num_ByteToWrite--;
data++;
}
I2CTimeout = I2CT_FLAG_TIMEOUT;
//check EV8_2
while( I2C_CheckEvent(I2Cx,I2C_EVT_MASTER_DATA_SENDED )==ERROR)
{
if((I2CTimeout--) == 0) return I2C_TIMEOUT_UserCallback(6);
}
if(enStop == 1)
{
//send stop signal
I2C_GenerateStop(I2Cx, ENABLE);
}
return 1;
}