I2C通信协议
时序及数据帧
信号条件
- START 起始信号 信号条件为在SCK高电平时SDA产生下降沿。
- ACK 应答信号 信号条件为在数据第九位为高电平时为非应答信号,低电平是为应答信号。
- STOP 停止信号 信号条件为与起始信号相反,SCK高电平时SDA产生上升沿。
- DATA 数据信号 在SCK为高电平时,SDA的持续电平为数据。
数据帧条件
如上图所示,完成一帧数据以起始信号开始,首先发送从机地址->等待应答->发送寄存器地址->等待应答->发送数据->等待应答->结束
以上是主机对从机的操作,图中灰色为主机发送的内容白色从机发送的内容,如果需要读取数据在发送从机地址数据的第八位写1,然后在写入寄存器地址后读取SDA电平状态进行接收从机数据。
软件I2C 应用代码
参考链接:https://blog.csdn.net/Dawn_Burning/article/details/134181024
//需要根据主控MCU频率和i的取值,调整i2c_Delay的时长,进而调整SCL的脉宽。(也受编译器“优化等级”影响)
/**
* @name i2c_Delay
* @brief soft delay for i2c clock
* @param none
* @retval none
*/
static void i2c_Delay(void)
{
uint8_t i;
/*
*AT32F425F6P7,
*i = 100,SCL = 163.4KHZ,6.1us
*i = 75, SCL = 243.9KHZ,4.1us
*i = 50, SCL = 312.5kHZ,3.2us
*/
for(i=0;i<100;i++);
}
/**
* @name hw_i2c_START
* @brief start signal for i2c simulation
* @param none
* @retval none
*/
void hw_i2c_START(void)
{
I2C_SDA_H();
I2C_SCL_H();
i2c_Delay();
I2C_SDA_L();
i2c_Delay();
I2C_SCL_L();
i2c_Delay();
}
/**
* @name hw_i2c_ACK
* @brief ACK signal for i2c simulation
* @param none
* @retval none
*/
void hw_i2c_ACK(void)
{
I2C_SDA_L();
i2c_Delay();
I2C_SCL_H();
i2c_Delay();
I2C_SCL_L();
i2c_Delay();
I2C_SDA_H();
}
/**
* @name hw_i2c_WaitAck
* @brief Wait ACK signal for i2c simulation
* @param none
* @retval uint8_t tempRe:Get slave ack signal or not
*/
uint8_t hw_i2c_WaitAck(void)
{
uint8_t tempRe;
I2C_SDA_H(); //MCU(master) set SDA High
i2c_Delay();
I2C_SCL_H(); //MCU(master) send a new SCL signal, slave device should return an Ack signal
i2c_Delay();
tempRe = I2C_SDA_READ(); //MCU(master) read SDA state(1 or 0)
I2C_SCL_L();
i2c_Delay();
return tempRe;
}
/**
* @name hw_i2c_NACK
* @brief Send NACK signal to slave for i2c simulation
* @param none
* @retval none
*/
void hw_i2c_NACK(void)
{
I2C_SDA_L();
I2C_SCL_H();
i2c_Delay();
I2C_SDA_H();
}
/**
* @name hw_i2c_STOP
* @brief Send STOP signal to slave for i2c simulation
* @param none
* @retval none
*/
void hw_i2c_STOP(void)
{
I2C_SDA_L();
I2C_SCL_H();
i2c_Delay();
I2C_SDA_H();
i2c_Delay();
}
/**
* @name lib_i2c_SendByte
* @brief Send Byte from master by simulation of i2c
* @param DataByte:data
* @retval none
*/
void lib_i2c_SendByte(uint8_t DataByte)
{
uint8_t i;
for ( i = 0; i < 8; i++)
{
if (DataByte & 0x80)
{
I2C_SDA_H();
}
else
{
I2C_SDA_L();
}
i2c_Delay();
I2C_SCL_H();
i2c_Delay();
I2C_SCL_L();
if (i == 7)
{
I2C_SDA_H(); //MCU(master) set SDA high
}
DataByte <<= 1;
i2c_Delay();
}
}
/**
* @name lib_i2c_ReadByte
* @brief Read Byte from slave device by simulation of i2c
* @param none
* @retval tempData:data
*/
uint8_t lib_i2c_ReadByte(void)
{
uint8_t i;
uint8_t tempData = 0;
uint8_t tempRe = 0;
for (int i = 0; i < 8; i++)
{
tempData <<= 1; // 将tempData左移一位
I2C_SCL_H(); // 拉高时钟信号
i2c_Delay(); // 等待一段时间
tempRe = I2C_SDA_READ(); // 读取SDA线上的数据
if (tempRe) {
tempData |= 0x01; // 如果读取到的数据为1,则在tempData的最低位设置1
}
I2C_SCL_L(); // 释放时钟信号
i2c_Delay(); // 等待一段时间
}
// for(i = 0; i < 8; i++)
// {
// tempData <<=1;
// I2C_SCL_H();
// i2c_Delay();
//
// tempRe = I2C_SDA_READ();
// {
// tempData++;
// }
// I2C_SCL_L();
// i2c_Delay();
// }
return tempData;
}
/**
* @name lib_i2c_ReadMutiBytes
* @brief Read Muti Bytes data from slave device
* @param slave_address
* @param reg_address
* @param pdatabuf
* @param len
* @retval tempRe:whether read data successfully or not
*/
uint8_t lib_i2c_ReadMutiBytes(uint8_t slave_address, uint8_t reg_address, uint8_t* pdatabuf, uint8_t len)
{
uint8_t tempData;
uint8_t tempRe = 0;
uint8_t cnt = 0;
uint8_t tempaddr_W = slave_address<<1;
uint8_t tempaddr_R = tempaddr_W + 1;
do
{
/* 1st:i2c start signal */
hw_i2c_START();
/* 2nd:write slave device address, bit0 is a read-write control bit, 0 for write, and 1 for read */
lib_i2c_SendByte(tempaddr_W);
/* 3rd:wait ack from slave device */
tempRe = hw_i2c_WaitAck();
if (tempRe){ // the return value is 1 ,that is to say SDA is not lowwed. the target ist8310 doesn't Ack
break;
}
/* 4th:send target register address */
lib_i2c_SendByte(reg_address);
/* 5th:wait Ack from slave device */s
tempRe = hw_i2c_WaitAck();
if (tempRe){ // the return value is 1 ,that is to say SDA is not lowwed. the target ist8310 doesn't Ack
break;
}
/* 6th:send a start signal to reset i2c bus, and then start to read data from slave device */
hw_i2c_START();
/* 7th:Send a read command(bit0) for the slave address */
lib_i2c_SendByte(tempaddr_R);
/* 8th:wait Ack from slave device */
tempRe = hw_i2c_WaitAck();
if (tempRe){ // the return value is 1 ,that is to say SDA is not lowwed. the target ist8310 doesn't Ack
break;
}
//9th:read data by loop
for(cnt = 0; cnt < len; cnt++)
{
pdatabuf[cnt] = lib_i2c_ReadByte(); //read 1 byte
/* After reading each byte, an Ack needs to be sent, except for the last byte, which requires a Nack */
if(cnt != len - 1)
{
/* After the middle byte is read, the CPU generates the ACK signal (Drive SDA = 0) */
hw_i2c_ACK();
}
else
{
/* After reading the last byte, the CPU generates the NACK signal (drive SDA = 1) */
hw_i2c_NACK();
}
}
}while(0);
/* Send I2C bus stop signal */
hw_i2c_STOP();
if(tempRe){
tempRe = 0;
}else{
tempRe = 1;
}
return tempRe;
}
/**
* @brief write single byte through the i2c1(For M1) by sofyware simulation.
* @param slave_address: address of tagert slave device
* @param reg_address: address of tagert register
* @param pdata: pointer to the data
* @retval tempRe: Write data successfully or not
*/
uint8_t lib_i2c_WriteSingleByte(uint16_t slave_address, uint16_t reg_address, uint8_t pdata)
{
uint8_t tempRe = 0;
uint8_t tempData = pdata;
uint16_t tempaddr = slave_address<<1;
do
{
hw_i2c_START();
lib_i2c_SendByte(tempaddr);
tempRe = hw_i2c_WaitAck();
if (tempRe){ // the return value is 1 ,that is to say SDA is not lowwed. the target ist8310 doesn't Ack
break;
}
lib_i2c_SendByte(reg_address);
tempRe = hw_i2c_WaitAck();
if (tempRe){ // the return value is 1 ,that is to say SDA is not lowwed. the target ist8310 doesn't Ack
break;
}
lib_i2c_SendByte(tempData);
tempRe = hw_i2c_WaitAck();
if (tempRe){ // the return value is 1 ,that is to say SDA is not lowwed. the target ist8310 doesn't Ack
break;
}
hw_i2c_STOP();
} while (0);
return tempRe;
}