软件模拟IIC的全面笔记(已调通)

2023-11-03 00:20

本文主要是介绍软件模拟IIC的全面笔记(已调通),希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

@[toc] lib_i2c_simulation

/** @Author: Haiyichen* @Date: 2023-09-21 16:16:16* @LastEditors: Haiyichen* @LastEditTime: 2023-10-31 18:01:10* @Description: Personal notes of i2c-simulation*/

i2c基础

通讯流程

协议

除了文字解释,有用wavedrom简单画了一些各种信号的电平变化过程,但需要支持MPE才能看到,虽然CSDN上的在线编辑器无法渲染出图,但还是放出来了,如果有条件可以在自己的VSCode中配置MPE(markdown preview enhenced)

  1. 开始:SCL为高电平时,SDA从高电平状态切换到低电平状态。
    {signal:[{       name:'SCL',   wave:'p.Pp'},{       name:'data',  wave:"x.=x", data:["Start" ]},{       name:'SDA',   wave:"1.0"}
    ]}
  2. 停止:SCL为高电平时,SDA从低电平状态切换到高电平状态。
    {signal:[{       name:'SCL',   wave:'p.PPp'},{       name:'data',  wave:"x.=x", data:["Stop" ]},{       name:'SDA',   wave:"0.10"}
    ]}
  3. 应答:发送侧发送完8bit数据后,接收侧需要回复一个信号,即第9个SCL时,接收侧将SDA拉低,称作ACK。
    {signal:[{       name:'SCL',   wave:'p.Pp..|Pp'},{       name:'data',  wave:"x.==..|=x", data:["Start", "SlaveAddress","Ack"]},{       name:'SDA',   wave:"0.10.1|0."}
    ]}
  4. 无应答:发送侧发送完8bit数据后,接收侧需要回复一个信号,即第9个SCL时,接收侧将SDA拉高(或叫释放SDA),称作NACK。NACK时同时会引起Master发生RESTART或STOP流程。
    {signal:[
    {       name:'SCL',   wave:'p.Pp..|Pp'},
    {       name:'data',  wave:"x.==..|=x", data:["Start", "SlaveAddress","Nack"]},
    {       name:'SDA',   wave:"0.10.0|1."}
    ]}
  5. 地址命令:i2c的地址是7bit,第8bit是方向位。1代表Read、0代表Write

写流程

  1. Master发起START
  2. Master发送Slave地址(7bit)和W(0:写动作),等待Slave发动应答ACK
  3. Slave发送应答ACK
  4. Master发送寄存器地址(8bit),等待Slave发送应答ACK
  5. Slave发送应答ACK
  6. Master发送写入寄存器的数据(8bit),等待Slave发送应答ACK
  7. Slave发送应答ACK
  8. 6~7可以循环多次,即按顺序写多个寄存器
  9. Master发起STOP

读流程

  1. Master发起START
  2. Master发送Slave地址(7bit)和W(0:写动作),等待Slave发送应答ACK
  3. Slave发送应答ACK
  4. Master发送寄存器地址(8bit),等待Slave发送应答ACK
  5. Slave发送应答ACK
  6. Master发起START
  7. Master发送Slave地址(7bit)和R(1:读动作),等待Slave发送应答ACK
  8. Slave发送应答ACK
  9. Slave发送寄存器里数据(8bit),等待Master发送ACK
  10. Master发送ACK或NACK(出现NACK,后面直接就是STOP流程)
  11. 9~10可以循环多次,即顺序读多个寄存器
  12. Master发起STOP

i2c模拟驱动函数

相关硬件配置

/* IO definition of i2c simulation */
#define I2C_SIMULATION_GPIO_PORT             GPIOA
#define I2C_SIMULATION_SCL_PIN               GPIO_PINS_0
#define I2C_SIMULATION_SDA_PIN               GPIO_PINS_1/* driver definition of i2c simulation for M1 */
#define I2C_SDA_H()        I2C_SIMULATION_GPIO_PORT->scr = I2C_SIMULATION_SDA_PIN;
#define I2C_SDA_L()        I2C_SIMULATION_GPIO_PORT->clr = I2C_SIMULATION_SDA_PIN; 
#define I2C_SCL_H()        I2C_SIMULATION_GPIO_PORT->scr = I2C_SIMULATION_SCL_PIN;
#define I2C_SCL_L()        I2C_SIMULATION_GPIO_PORT->clr = I2C_SIMULATION_SCL_PIN;

关于模拟i2c的IO配置细节

关于SDA引脚是配置成开漏还是推挽,

  • 推挽输出对应->需要切换输入输出模式;
  • 开漏输出对应->不需要切换输入输出模式.
    参考链接已验证

i2c_Delay()

//需要根据主控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++);
}

hw_i2c_START()

/*** @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();}

hw_i2c_ACK()

/*** @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();}

hw_i2c_WaitAck()

/*** @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 Highi2c_Delay();I2C_SCL_H();                //MCU(master) send a new SCL signal, slave device should return an Ack signali2c_Delay();tempRe = I2C_SDA_READ();    //MCU(master) read SDA state(1 or 0)I2C_SCL_L();i2c_Delay();return tempRe;
}

hw_i2c_NACK()

/*** @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();
}

hw_i2c_STOP()

/*** @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();
}

lib_i2c_SendByte()

/*** @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();        }
}

lib_i2c_ReadByte()

/*** @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(i = 0; i < 8; i++){tempData <<=1;I2C_SCL_H();i2c_Delay();tempRe = I2C_SDA_READ();if(tempRe){tempData++;}I2C_SCL_L();i2c_Delay();}return tempData;
}

lib_i2c_ReadMutiBytes()

/*** @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 Ackbreak;}/* 4th:send target register address */lib_i2c_SendByte(reg_address);/* 5th:wait Ack from slave device */stempRe = hw_i2c_WaitAck();if (tempRe){							// the return value is 1 ,that is to say SDA is not lowwed. the target ist8310 doesn't Ackbreak;}/* 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 Ackbreak;}			//9th:read data by loopfor(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;
}

lib_i2c_WriteSingleByte()

/*** @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 Ackbreak;}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 Ackbreak;}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 Ackbreak;}hw_i2c_STOP();} while (0);return tempRe;
}

小结

因为用的芯片硬件IIC的底层官方函数一直卡死跑不通,于是干脆自己整理了一套软件模拟IIC的相关流程和函数,已经在项目中顺利调通了,项目换芯片也经历过不同芯片的移植,也很方便。

这篇关于软件模拟IIC的全面笔记(已调通)的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/334302

相关文章

Python 字符串裁切与提取全面且实用的解决方案

《Python字符串裁切与提取全面且实用的解决方案》本文梳理了Python字符串处理方法,涵盖基础切片、split/partition分割、正则匹配及结构化数据解析(如BeautifulSoup、j... 目录python 字符串裁切与提取的完整指南 基础切片方法1. 使用切片操作符[start:end]2

Python学习笔记之getattr和hasattr用法示例详解

《Python学习笔记之getattr和hasattr用法示例详解》在Python中,hasattr()、getattr()和setattr()是一组内置函数,用于对对象的属性进行操作和查询,这篇文章... 目录1.getattr用法详解1.1 基本作用1.2 示例1.3 原理2.hasattr用法详解2.

SpringBoot加载profile全面解析

《SpringBoot加载profile全面解析》SpringBoot的Profile机制通过多配置文件和注解实现环境隔离,支持开发、测试、生产等不同环境的灵活配置切换,无需修改代码,关键点包括配置文... 目录题目详细答案什么是 Profile配置 Profile使用application-{profil

Python自定义异常的全面指南(入门到实践)

《Python自定义异常的全面指南(入门到实践)》想象你正在开发一个银行系统,用户转账时余额不足,如果直接抛出ValueError,调用方很难区分是金额格式错误还是余额不足,这正是Python自定义异... 目录引言:为什么需要自定义异常一、异常基础:先搞懂python的异常体系1.1 异常是什么?1.2

全面解析Golang 中的 Gorilla CORS 中间件正确用法

《全面解析Golang中的GorillaCORS中间件正确用法》Golang中使用gorilla/mux路由器配合rs/cors中间件库可以优雅地解决这个问题,然而,很多人刚开始使用时会遇到配... 目录如何让 golang 中的 Gorilla CORS 中间件正确工作一、基础依赖二、错误用法(很多人一开

深入浅出SpringBoot WebSocket构建实时应用全面指南

《深入浅出SpringBootWebSocket构建实时应用全面指南》WebSocket是一种在单个TCP连接上进行全双工通信的协议,这篇文章主要为大家详细介绍了SpringBoot如何集成WebS... 目录前言为什么需要 WebSocketWebSocket 是什么Spring Boot 如何简化 We

python运用requests模拟浏览器发送请求过程

《python运用requests模拟浏览器发送请求过程》模拟浏览器请求可选用requests处理静态内容,selenium应对动态页面,playwright支持高级自动化,设置代理和超时参数,根据需... 目录使用requests库模拟浏览器请求使用selenium自动化浏览器操作使用playwright

Spring Boot3.0新特性全面解析与应用实战

《SpringBoot3.0新特性全面解析与应用实战》SpringBoot3.0作为Spring生态系统的一个重要里程碑,带来了众多令人兴奋的新特性和改进,本文将深入解析SpringBoot3.0的... 目录核心变化概览Java版本要求提升迁移至Jakarta EE重要新特性详解1. Native Ima

全面掌握 SQL 中的 DATEDIFF函数及用法最佳实践

《全面掌握SQL中的DATEDIFF函数及用法最佳实践》本文解析DATEDIFF在不同数据库中的差异,强调其边界计算原理,探讨应用场景及陷阱,推荐根据需求选择TIMESTAMPDIFF或inte... 目录1. 核心概念:DATEDIFF 究竟在计算什么?2. 主流数据库中的 DATEDIFF 实现2.1

Java操作Word文档的全面指南

《Java操作Word文档的全面指南》在Java开发中,操作Word文档是常见的业务需求,广泛应用于合同生成、报表输出、通知发布、法律文书生成、病历模板填写等场景,本文将全面介绍Java操作Word文... 目录简介段落页头与页脚页码表格图片批注文本框目录图表简介Word编程最重要的类是org.apach