I²C协议基本概念和数据传输

今天给大侠带来基于fpga的 模拟 i²c 协议设计,由于篇幅较长,分三篇。今天带来第三篇,下篇,程序的仿真与测试。话不多说,上货。
之前也有相关文章介绍,这里超链接一下,仅供各位大侠参考。
源码系列:基于fpga的 iic 设计(附源工程)
这里也给出前两篇的超链接:
基于 fpga 的模拟 i²c协议设计(上)
基于 fpga 的模拟 i²c协议设计(中)
导读
i²c(inter-integrated circuit),其实是 i²c bus 简称,中文就是集成电路总线,它是一种串行通信总线,使用多主从架构,由飞利浦公司在1980年代为了让主板、嵌入式系统或手机用以连接低速周边设备而发展。i²c的正确读法为“i平方c”(i-squared-c),而“i二c”(i-two-c)则是另一种错误但被广泛使用的读法。自2006年10月1日起,使用 i²c 协议已经不需要支付专利费,但制造商仍然需要付费以获取 i²c 从属设备地址。
i²c 简单来说,就是一种串行通信协议,i²c的通信协议和通信接口在很多工程中有广泛的应用,如数据采集领域的串行 ad,图像处理领域的摄像头配置,工业控制领域的 x 射线管配置等等。除此之外,由于 i²c 协议占用的 io 资源特别少,连接方便,所以工程中也常选用 i²c 接口做为不同芯片间的通信协议。i²c 串行总线一般有两根信号线,一根是双向的数据线sda,另一根是时钟线scl。所有接到 i²c 总线设备上的串行数据sda都接到总线的sda上,各设备的时钟线scl接到总线的scl上。
在现代电子系统中,有为数众多的 ic 需要进行相互之间以及与外界的通信。为了简化电路的设计,philips 公司开发了一种用于内部 ic 控制的简单的双向两线串行总线 i²c(intel-integrated circuit bus)。1998 年当推出 i²c 总线协议 2.0 版本时,i²c 协议实际上已经成为一个国际标准。
在进行 fpga 设计时,经常需要和外围提供 i²c 接口的芯片通信。例如低功耗的 cmos 实时时钟/日历芯片 pcf8563、lcd 驱动芯片 pcf8562、并行口扩展芯片 pcf8574、键盘/led 驱动器 zlg7290 等都提供 i²c 接口。因此在 fpga 中模拟 i²c 接口已成为 fpga 开发必要的步骤。
本篇将详细讲解在 fpga 芯片中使用 vhdl/verilog hdl 模拟 i²c 协议,以及编写 testbench仿真和测试程序的方法。
第三篇内容摘要:本篇会介绍程序的仿真与测试,包括主节点的仿真、从节点的仿真、仿真主程序、仿真结果以及总结等相关内容。
四、程序的仿真与测试
i²c 协议的模拟程序完成后,还需要通过仿真程序对程序的功能进行测试。对本程序的仿真包括 3 个部分:第一部分是主节点的仿真,模拟数据读/写;第二部分是从节点的仿真,模拟数据的接收和应答;第三部分是仿真主程序,负责整个仿真过程的控制。
4.1 主节点的仿真
主节点仿真的内容包括读数据、写数据和比较数据 3 部分,代码如下:
`include timescale.v//模块定义module wb_master_model(clk, rst, adr, din, dout, cyc, stb, we, sel, ack, err, rty); //参数 parameter dwidth = 32; parameter awidth = 32; //输入、输出 input clk, rst; output [awidth -1:0] adr; input [dwidth -1:0] din; output [dwidth -1:0] dout; output cyc, stb; output we; output [dwidth/8 -1:0] sel; input ack, err, rty; //wire 定义 reg [awidth -1:0] adr; reg [dwidth -1:0] dout; reg cyc, stb; reg we; reg [dwidth/8 -1:0] sel; reg [dwidth -1:0] q; // 存储逻辑 //初始化 initial begin adr = {awidth{1'bx}}; dout = {dwidth{1'bx}}; cyc = 1'b0; stb = 1'bx; we = 1'hx; sel = {dwidth/8{1'bx}}; #1; end // 写数据周期 task wb_write; input delay; integer delay; input [awidth -1:0] a; input [dwidth -1:0] d; begin // 延迟 repeat(delay) @(posedge clk); // 设置信号值 #1; adr = a; dout = d; cyc = 1'b1; stb = 1'b1; we = 1'b1; sel = {dwidth/8{1'b1}}; @(posedge clk); // 等待从节点的应答信号 while(~ack) @(posedge clk); #1; cyc = 1'b0; stb = 1'bx; adr = {awidth{1'bx}}; dout = {dwidth{1'bx}}; we = 1'hx; sel = {dwidth/8{1'bx}}; end endtask // 读数据周期 task wb_read; input delay; integer delay; input [awidth -1:0]a; output [dwidth -1:0] d; begin // 延迟 repeat(delay) @(posedge clk); // 设置信号值 #1; adr = a; dout = {dwidth{1'bx}}; cyc = 1'b1; stb = 1'b1; we = 1'b0; sel = {dwidth/8{1'b1}}; @(posedge clk); // 等待从节点应答信号 while(~ack) @(posedge clk); #1; cyc = 1'b0; stb = 1'bx; adr = {awidth{1'bx}}; dout = {dwidth{1'bx}}; we = 1'hx; sel = {dwidth/8{1'bx}}; d = din; end endtask // 比较数据 task wb_cmp; input delay; integer delay; input [awidth -1:0] a; input [dwidth -1:0] d_exp; begin wb_read (delay, a, q); if (d_exp !== q)            $display(data compare error. received %h, expected %h at time %t, q, d_exp,$time); end endtaskendmodule
4.2 从节点的仿真
从节点仿真程序需要模拟从主节点接收数据,并发出应答信号,代码如下:
`include timescale.v//模块定义module i2c_slave_model (scl, sda); // 参数 // 地址 parameter i2c_adr = 7'b001_0000; // 输入、输出 input scl; inout sda; // 变量申明 wire debug = 1'b1; reg [7:0] mem [3:0]; // 初始化内存 reg [7:0] mem_adr; // 内存地址 reg [7:0] mem_do; // 内存数据输出 reg sta, d_sta; reg sto, d_sto; reg [7:0] sr; // 8 位移位寄存器 reg rw; // 读写方向 wire my_adr; // 地址 wire i2c_reset; // reset 信号 reg [2:0] bit_cnt; wire acc_done; // 传输完成 reg ld; reg sda_o; wire sda_dly; // 状态机的状态定义 parameter idle = 3'b000; parameter slave_ack = 3'b001; parameter get_mem_adr = 3'b010; parameter gma_ack = 3'b011; parameter data = 3'b100; parameter data_ack = 3'b101; reg [2:0] state; // 模块主体 //初始化 initial begin sda_o = 1'b1; state = idle; end // 产生移位寄存器 always @(posedge scl) sr <= #1 {sr[6:0],sda}; //检测到访问地址与从节点一致 assign my_adr = (sr[7:1] == i2c_adr); //产生位寄存器 always @(posedge scl) if(ld) bit_cnt <= #1 3'b111; else bit_cnt <= #1 bit_cnt - 3'h1; //产生访问结束标志 assign acc_done = !(|bit_cnt); // sda 延迟 assign #1 sda_dly = sda; //检测到开始状态 always @(negedge sda) if(scl) begin sta <= #1 1'b1; if(debug) $display(debug i2c_slave; start condition detected at %t, $time); end else sta <= #1 1'b0; always @(posedge scl) d_sta <= #1 sta; // 检测到停止状态信号 always @(posedge sda) if(scl) begin sto <= #1 1'b1; if(debug) $display(debug i2c_slave; stop condition detected at %t, $time); end else sto <= #1 1'b0; //产生 i2c 的 reset 信号 assign i2c_reset = sta || sto; // 状态机 always @(negedge scl or posedge sto) if (sto || (sta && !d_sta) ) begin state <= #1 idle; // reset 状态机 sda_o <= #1 1'b1; ld <= #1 1'b1; end else begin // 初始化 sda_o <= #1 1'b1; ld <= #1 1'b0; case(state) idle: // idle 状态 if (acc_done && my_adr) begin state <= #1 slave_ack; rw <= #1 sr[0]; sda_o <= #1 1'b0; // 产生应答信号 #2; if(debug && rw) $display(debug i2c_slave; command byte received (read) at %t,$time); if(debug && !rw) $display(debug i2c_slave; command byte received (write) at %t,$time); if(rw) begin mem_do <= #1 mem[mem_adr]; if(debug) begin #2 $display(debug i2c_slave; data block read %x from address %x (1), mem_do, mem_adr); #2 $display(debug i2c_slave; memcheck [0]=%x, [1]=%x, [2]=%x, mem[4'h0], mem[4'h1], mem[4'h2]); end end end slave_ack: begin if(rw) begin state <= #1 data; sda_o <= #1 mem_do[7]; end else state <= #1 get_mem_adr; ld <= #1 1'b1;                         end                              get_mem_adr: // 等待内存地址 if(acc_done) begin state <= #1 gma_ack; mem_adr <= #1 sr; // 保存内存地址 sda_o <= #1 !(sr <= 15); // 收到合法地址信号后发出应答信号 if(debug) #1 $display(debug i2c_slave; address received. adr=%x, ack=%b,sr, sda_o); end gma_ack: begin state <= #1 data; ld <= #1 1'b1; end data: // 接收数据 begin if(rw) sda_o <= #1 mem_do[7]; if(acc_done) begin state <= #1 data_ack; mem_adr <= #2 mem_adr + 8'h1; sda_o <= #1 (rw && (mem_adr <= 15) ); if(rw) begin #3 mem_do <= mem[mem_adr]; if(debug) #5 $display(debug i2c_slave; data block read %x from address %x (2), mem_do, mem_adr);                                     end if(!rw) begin mem[ mem_adr[3:0] ] <= #1 sr; // store data in memory if(debug) #2 $display(debug i2c_slave; data block write %x to address %x, sr, mem_adr); end end end data_ack: begin ld <= #1 1'b1; if(rw) if(sda) // begin state <= #1 idle; sda_o <= #1 1'b1; end else begin state <= #1 data; sda_o <= #1 mem_do[7]; end else begin state <= #1 data; sda_o <= #1 1'b1; end end endcase end // 从内存读数据 always @(posedge scl) if(!acc_done && rw) mem_do <= #1 {mem_do[6:0], 1'b1}; // 产生三态 assign sda = sda_o ? 1'bz : 1'b0; // 检查时序 wire tst_sto = sto; wire tst_sta = sta; wire tst_scl = scl; //指定各个信号的上升沿和下降沿 specify specparam normal_scl_low = 4700, normal_scl_high = 4000, normal_tsu_sta = 4700, normal_tsu_sto = 4000, normal_sta_sto = 4700, fast_scl_low = 1300, fast_scl_high = 600, fast_tsu_sta = 1300, fast_tsu_sto = 600, fast_sta_sto = 1300; $width(negedge scl, normal_scl_low); $width(posedge scl, normal_scl_high); $setup(negedge sda &&& scl, negedge scl, normal_tsu_sta); // 开始状态信号 $setup(posedge scl, posedge sda &&& scl, normal_tsu_sto); // 停止状态信号 $setup(posedge tst_sta, posedge tst_scl, normal_sta_sto); endspecify endmodule
4.3 仿真主程序
仿真主程序完成主节点数据到从节点的控制,代码如下:
`include timescale.v//模块定义module tst_bench_top(); //连线和寄存器 reg clk; reg rstn; wire [31:0] adr; wire [ 7:0] dat_i, dat_o; wire we; wire stb; wire cyc; wire ack; wire inta; //q 保存状态寄存器内容 reg [7:0] q, qq; wire scl, scl_o, scl_oen; wire sda, sda_o, sda_oen; //寄存器地址 parameter prer_lo = 3'b000; //分频寄存器低位地址 parameter prer_hi = 3'b001; //高位地址 parameter ctr = 3'b010; //控制寄存器地址,(7)使能位|6 中断使能位|5-0其余保留位 parameter rxr = 3'b011; //接收寄存器地址,(7)接收到的最后一个字节的数据 parameter txr = 3'b011; //传输寄存器地址,(7)传输地址时最后一位为读写位,1 为读 parameter cr = 3'b100; //命令寄存器地址,       //(7)开始|6 结束|5 读|4 写|3 应答(作为接收方时,发送应答信号,“0”为应答,“1”为不应答)|2 保留位|1 保留位|0 中断应答位,这八位自动清除    parameter sr = 3'b100; //状态寄存器地址,(7)接收应答位(“0”为接收到应答)|6 忙位(产生开始信号后变为 1,结束信号后变为 0)|5 仲裁位|4-2 保留位|1 传输中位(1 表示正在传输数据,0 表示传输结束)|中断标志位 parameter txr_r = 3'b101; parameter cr_r = 3'b110; // 产生时钟信号,一个时间单位为 1ns,周期为 10ns,频率为 100mhz。 always #5 clk = ~clk; //连接 master 模拟模块 wb_master_model #(8, 32) u0 ( .clk(clk), //时钟 .rst(rstn), //重起 .adr(adr), //地址 .din(dat_i), //输入的数据 .dout(dat_o), //输出的数据 .cyc(cyc), .stb(stb), .we(we), .sel(), .ack(ack), //应答 .err(1'b0), .rty(1'b0) ); //连接 i2c 接口 i2c_master_top i2c_top ( //连接到 master 模拟模块部分 .wb_clk_i(clk), //时钟 .wb_rst_i(1'b0), //同步重起位 .arst_i(rstn), //异步重起 .wb_adr_i(adr[2:0]), //地址输入 .wb_dat_i(dat_o), //数据输入接口 .wb_dat_o(dat_i), //数据从接口输出 .wb_we_i(we), //写使能信号 .wb_stb_i(stb), //片选信号,应该一直为高 .wb_cyc_i(cyc), .wb_ack_o(ack), //应答信号输出到 master 模拟模块 .wb_inta_o(inta), //中断信号输出到 master 模拟模块 //输出的 i2c 信号,连接到 slave 模拟模块 .scl_pad_i(scl), .scl_pad_o(scl_o), .scl_padoen_o(scl_oen), .sda_pad_i(sda), .sda_pad_o(sda_o), .sda_padoen_o(sda_oen) ); //连接到 slave 模拟模块 i2c_slave_model #(7'b1010_000) i2c_slave ( .scl(scl), .sda(sda) ); //为 master 模拟模块产生 scl 和 sda 的三态缓冲 assign scl = scl_oen ? 1'bz : scl_o; // create tri-state buffer for i2c_master scl line assign sda = sda_oen ? 1'bz : sda_o; // create tri-state buffer for i2c_master sda line //上拉 pullup p1(scl); // pullup scl line pullup p2(sda); // pullup sda line //初始化 initial begin $display( 状态: %t i2c 接口测试开始!, $time); // 初始值 clk = 0; //重起系统 rstn = 1'b1; // negate reset #2; rstn = 1'b0; // assert reset repeat(20) @(posedge clk); rstn = 1'b1; // negate reset $display(状态: %t 完成系统重起!, $time); @(posedge clk); // 对接口编程 // 写内部寄存器 // 分频 100m/100k*5=o'200=h'c8 u0.wb_write(1, prer_lo, 8'hc7); u0.wb_write(1, prer_hi, 8'h00); $display(状态: %t 完成分频寄存器操作!, $time); //读分频寄存器内容 u0.wb_cmp(0, prer_lo, 8'hc8); u0.wb_cmp(0, prer_hi, 8'h00); $display(状态: %t 完成分频寄存器确认操作!, $time); //接口使能 u0.wb_write(1, ctr, 8'h80); $display(状态: %t 完成接口使能!, $time); // 驱动 slave 地址 // h'a0=b'1010_0000,地址+写状态,写入的地址为 h'50 u0.wb_write(1, txr, 8'ha0); //命令内容为 b'1001_0000,产生开始位,并设置写状态 u0.wb_write(0, cr, 8'h90); $display(状态: %t 产生开始位, 然后写命令 a0(地址+写),命令开始!, $time); // 检查状态位信息 // 检查传输是否结束 u0.wb_read(1, sr, q); while(q[1]) u0.wb_read(0, sr, q); $display(状态: %t 地址驱动写操作完成!, $time); // 待写的地址为 h'01 u0.wb_write(1, txr, 8'h01); // 产生写命令 b'0001_0000 u0.wb_write(0, cr, 8'h10); $display(状态: %t 待写地址为 01,命令开始!, $time); // 检查状态位 u0.wb_read(1, sr, q); while(q[1]) u0.wb_read(0, sr, q); $display(状态: %t 写操作完成!, $time); // 写入内容 u0.wb_write(1, txr, 8'ha5); u0.wb_write(0, cr, 8'h10); $display(状态: %t 写入内容为 a5,开始写入过程!, $time); u0.wb_read(1, sr, q); while(q[1]) u0.wb_read(1, sr, q); $display(状态: %t 写 a5 到地址 h'01 中完成!, $time); // 写入下一个地址 5a u0.wb_write(1, txr, 8'h5a); // present data // 写入并停止 u0.wb_write(0, cr, 8'h50); // set command (stop, write) $display(状态: %t 写 5a 到下一个地址,产生停止位!, $time); u0.wb_read(1, sr, q); while(q[1]) u0.wb_read(1, sr, q); // poll it until it is zero $display(状态: %t 写第二个地址结束!, $time); // 读 // 驱动 slave 地址 u0.wb_write(1, txr, 8'ha0); u0.wb_write(0, cr, 8'h90); $display(状态: %t 产生开始位,写命令 a0 (slave 地址+write), $time); u0.wb_read(1, sr, q); while(q[1]) u0.wb_read(1, sr, q); // poll it until it is zero $display(状态: %t slave 地址驱动完成!, $time); // 发送地址 u0.wb_write(1, txr, 8'h01); u0.wb_write(0, cr, 8'h10); $display(状态: %t 发送地址 01!, $time); u0.wb_read(1, sr, q); while(q[1]) u0.wb_read(1, sr, q); $display(状态: %t 地址发送完成!, $time); // 驱动 slave 地址,1010_0001,h'50+read u0.wb_write(1, txr, 8'ha1); u0.wb_write(0, cr, 8'h90); $display(状态: %t 产生重复开始位, 读地址+开始位, $time); u0.wb_read(1, sr, q); while(q[1]) u0.wb_read(1, sr, q); $display(状态: %t 命令结束!, $time); // 读数据 u0.wb_write(1, cr, 8'h20); $display(状态: %t 读+应答命令, $time); u0.wb_read(1, sr, q); while(q[1]) u0.wb_read(1, sr, q); $display(状态: %t 读结束!, $time); // 检查读的内容 u0.wb_read(1, rxr, qq); if(qq !== 8'ha5) $display( 错误: 需要的是 a5, received %x at time %t, qq, $time); // 读下一个地址内容 u0.wb_write(1, cr, 8'h20); $display(状态: %t 读+ 应答, $time); u0.wb_read(1, sr, q); while(q[1]) u0.wb_read(1, sr, q); $display(状态: %t 第二个地址读结束!, $time); u0.wb_read(1, rxr, qq); if(qq !== 8'h5a) $display( 错误: 需要的是 5a, received %x at time %t, qq, $time); // 读 u0.wb_write(1, cr, 8'h20); $display(状态: %t 读 + 应答, $time); u0.wb_read(1, sr, q); while(q[1]) u0.wb_read(1, sr, q); $display(状态: %t 第三个地址读完成!, $time); u0.wb_read(1, rxr, qq); $display(状态: %t 第三个地址内容是 %x !, $time, qq); // 读 u0.wb_write(1, cr, 8'h28); $display(状态: %t 读 + 不应答!, $time); u0.wb_read(1, sr, q); while(q[1]) u0.wb_read(1, sr, q); $display(状态: %t 第四个地址读完成!, $time); u0.wb_read(1, rxr, qq); $display(状态: %t 第四个地址内容为 %x !, $time, qq); // 检查不存在的 slave 地址 // drive slave address u0.wb_write(1, txr, 8'ha0); u0.wb_write(0, cr, 8'h90);            $display(状态: %t 产生开始位, 发送命令 a0 (slave 地址+写). 检查非法地址!,$time); u0.wb_read(1, sr, q); while(q[1]) u0.wb_read(1, sr, q); // poll it until it is zero $display(状态: %t 命令结束!, $time); // 发送内存地址 u0.wb_write(1, txr, 8'h10); u0.wb_write(0, cr, 8'h10); $display(状态: %t 发送 slave 内存地址 10!, $time); u0.wb_read(1, sr, q); while(q[1]) u0.wb_read(1, sr, q); $display(状态: %t 地址发送完毕!, $time); // slave 发送不应答 $display(状态: %t 检查不应答位!, $time); if(!q[7]) $display( 错误: 需要 nack, 接收到 ack); // 从 slave 读数据 u0.wb_write(1, cr, 8'h40); $display(状态: %t 产生'stop'位, $time); u0.wb_read(1, sr, q); while(q[1]) u0.wb_read(1, sr, q); // poll it until it is zero $display(状态: %t 结束!, $time); #25000; // wait 25us $display( 状态: %t 测试结束!, $time); $finish; endendmodule
4.4 仿真结果
在 modelsim 中可以看到仿真的结果。如图 7 所示是发送开始状态并写地址“a0”时的图形,此时在图上表示为 scl 处于高时 sda 的一个下降沿,然后是数据“1010,0000”。
图 7 发送开始信号并写地址 a0   如图 8 所示为发送数据“01”和“a5”时的图形,在图上表示为:数据“0000,0001”和“1010,0101”。
图 8 发送数据“01”和“a5”   如图 9 所示的是发送停止状态信号和数据“5a”时的图形,在图上表示为 scl 处于高时sda 的一个上升沿,然后是数据“0101,1010”。
图 9 发送停止状态信号和数据“5a”   仿真程序说明 i²c 程序符合 i²c 协议的时序和数据格式,可以实现模拟 i²c 协议的任务。
五、总结
本篇首先说明了 i²c 协议相关的内容,介绍协议基本概念和数据传输各个命令的具体含义以及协议对时序的要求。接下来介绍模拟 i²c 协议程序的框架,详细讲解框架中各个模块的功能并介绍详细代码。最后通过一个完成的仿真程序完成对程序的测试。i²c 在应用中有着广泛的用途,本篇希望通过这个例子为各位大侠提供一个可行的解决方案。


新一代安卓旗舰芯片接连问世,终于有叫板苹果的实力了?
全志R128应用开发案例—点亮一颗LED灯
PyTorch教程-6.1. 层和模块
小米6现,魅族Pro7要出,三星S8不远,荣耀9快了,选谁?
基于上海华力28纳米低功耗工艺平台处理芯片成功量产
I²C协议基本概念和数据传输
Lantronix推出只有芯片大小,具以太网连接能力网络模块xPico
GPS DTU是什么,它的应用领域都有哪些
四维图新旗下世纪高通与江苏移动达成合作
索尼发布Walkman Z PMP:4.3 寸屏幕 Tegra 2处理器
友尚推出 INTEL、TI和MPS 的平板电脑设计参考方案
全球Q2可穿戴设备出货量同比增长5.5%,小米华为挺进前5
中美日韩芬齐发力 6G真正到来预计将在2030年
市场上普遍存在的行程开关型号
***是什么东西 ***的难度在哪里
电子假负载电路原理
伺服系统工作原理示意图 机器人用伺服系统技术壁垒
以SPCE061A单片机为控制核心的红外泵液器改进设计
全国第一张 5G 政务专网正式运行,实现 B 端和 C 端应用融合
伺服电机坏了怎么修