软件模拟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

相关文章

全面解析HTML5中Checkbox标签

《全面解析HTML5中Checkbox标签》Checkbox是HTML5中非常重要的表单元素之一,通过合理使用其属性和样式自定义方法,可以为用户提供丰富多样的交互体验,这篇文章给大家介绍HTML5中C... 在html5中,Checkbox(复选框)是一种常用的表单元素,允许用户在一组选项中选择多个项目。本

一文全面详解Python变量作用域

《一文全面详解Python变量作用域》变量作用域是Python中非常重要的概念,它决定了在哪里可以访问变量,下面我将用通俗易懂的方式,结合代码示例和图表,带你全面了解Python变量作用域,需要的朋友... 目录一、什么是变量作用域?二、python的四种作用域作用域查找顺序图示三、各作用域详解1. 局部作

安装centos8设置基础软件仓库时出错的解决方案

《安装centos8设置基础软件仓库时出错的解决方案》:本文主要介绍安装centos8设置基础软件仓库时出错的解决方案,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐... 目录安装Centos8设置基础软件仓库时出错版本 8版本 8.2.200android4版本 javas

Nacos注册中心和配置中心的底层原理全面解读

《Nacos注册中心和配置中心的底层原理全面解读》:本文主要介绍Nacos注册中心和配置中心的底层原理的全面解读,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录临时实例和永久实例为什么 Nacos 要将服务实例分为临时实例和永久实例?1.x 版本和2.x版本的区别

Python数据分析与可视化的全面指南(从数据清洗到图表呈现)

《Python数据分析与可视化的全面指南(从数据清洗到图表呈现)》Python是数据分析与可视化领域中最受欢迎的编程语言之一,凭借其丰富的库和工具,Python能够帮助我们快速处理、分析数据并生成高质... 目录一、数据采集与初步探索二、数据清洗的七种武器1. 缺失值处理策略2. 异常值检测与修正3. 数据

如何确定哪些软件是Mac系统自带的? Mac系统内置应用查看技巧

《如何确定哪些软件是Mac系统自带的?Mac系统内置应用查看技巧》如何确定哪些软件是Mac系统自带的?mac系统中有很多自带的应用,想要看看哪些是系统自带,该怎么查看呢?下面我们就来看看Mac系统内... 在MAC电脑上,可以使用以下方法来确定哪些软件是系统自带的:1.应用程序文件夹打开应用程序文件夹

Python使用pynput模拟实现键盘自动输入工具

《Python使用pynput模拟实现键盘自动输入工具》在日常办公和软件开发中,我们经常需要处理大量重复的文本输入工作,所以本文就来和大家介绍一款使用Python的PyQt5库结合pynput键盘控制... 目录概述:当自动化遇上可视化功能全景图核心功能矩阵技术栈深度效果展示使用教程四步操作指南核心代码解析

Python模拟串口通信的示例详解

《Python模拟串口通信的示例详解》pySerial是Python中用于操作串口的第三方模块,它支持Windows、Linux、OSX、BSD等多个平台,下面我们就来看看Python如何使用pySe... 目录1.win 下载虚www.chinasem.cn拟串口2、确定串口号3、配置串口4、串口通信示例5

MySQL主从同步延迟问题的全面解决方案

《MySQL主从同步延迟问题的全面解决方案》MySQL主从同步延迟是分布式数据库系统中的常见问题,会导致从库读取到过期数据,影响业务一致性,下面我将深入分析延迟原因并提供多层次的解决方案,需要的朋友可... 目录一、同步延迟原因深度分析1.1 主从复制原理回顾1.2 延迟产生的关键环节二、实时监控与诊断方案

利用Python快速搭建Markdown笔记发布系统

《利用Python快速搭建Markdown笔记发布系统》这篇文章主要为大家详细介绍了使用Python生态的成熟工具,在30分钟内搭建一个支持Markdown渲染、分类标签、全文搜索的私有化知识发布系统... 目录引言:为什么要自建知识博客一、技术选型:极简主义开发栈二、系统架构设计三、核心代码实现(分步解析