51register

main
lincaigui 7 months ago
parent 41d463d7be
commit d299e2cb77

Binary file not shown.

Binary file not shown.

@ -0,0 +1,40 @@
module ges_recognize(
input wire sys_clk ,
input wire sys_rst_n ,
output wire scl ,
inout wire sda
);
wire [2: 0] step ;
wire [5: 0] cfg_num ;
wire [15: 0] cfg_data ;
wire cfg_start ;
wire i2c_clk ;
wire i2c_start ;
paj7620_cfg paj7620_cfg_inst(
.i2c_clk (i2c_clk ),
.sys_rst_n (sys_rst_n ),
.cfg_start (cfg_start ),
.step (step ),
.cfg_num (cfg_num ),
.cfg_data (cfg_data ),
.i2c_start (i2c_start )
);
i2c_ctrl i2c_ctrl_inst(
.sys_clk (sys_clk ),
.sys_rst_n (sys_rst_n ),
.i2c_start (i2c_start ),
.cfg_num (cfg_num ),
.cfg_data (cfg_data ),
.cfg_start (cfg_start ),
.step (step ),
.i2c_clk (i2c_clk ),
.scl (scl ),
.sda (sda )
);
endmodule

@ -1,10 +1,17 @@
module i2c_ctrl( module i2c_ctrl(
input wire sys_clk , input wire sys_clk ,
input wire sys_rst_n , input wire sys_rst_n ,
input wire i2c_start ,
input wire [5: 0] cfg_num ,
input wire [15: 0] cfg_data ,
output reg cfg_start ,
output reg [2: 0] step ,
output reg i2c_clk ,
output wire scl , output wire scl ,
inout wire sda inout wire sda
); );
parameter I2C_CLK_DIV = 5'd24, parameter I2C_CLK_DIV = 5'd24,
MAX = 10'd1000, MAX = 10'd1000,
@ -27,7 +34,6 @@ reg [3: 0] state_n;
//i2c //i2c
reg [4: 0] cnt_clk; reg [4: 0] cnt_clk;
reg i2c_clk;
///// /////
// //
@ -36,7 +42,7 @@ reg skip_en_1 ;//步骤1跳转信号
reg skip_en_2 ;//2 reg skip_en_2 ;//2
reg skip_en_3 ;//3 reg skip_en_3 ;//3
reg skip_en_4 ;//4 reg skip_en_4 ;//4
reg [2: 0] step ;// reg skip_en_5 ;//5
reg [1: 0] cnt_i2c_clk ;//i2c reg [1: 0] cnt_i2c_clk ;//i2c
reg [2: 0] cnt_bit ;//bit reg [2: 0] cnt_bit ;//bit
reg i2c_end ;//i2c reg i2c_end ;//i2c
@ -54,6 +60,15 @@ reg err_en ;//接受到0x20有误
assign sda_en = ((state_c == ACK_1) || (state_c == ACK_2) || (state_c == ACK_3) || (state_c == DATA && step == 3'd3)) ? 1'b0: 1'b1;// assign sda_en = ((state_c == ACK_1) || (state_c == ACK_2) || (state_c == ACK_3) || (state_c == DATA && step == 3'd3)) ? 1'b0: 1'b1;//
assign sda_in = sda; assign sda_in = sda;
assign sda = (sda_en == 1'b1)? i2c_sda : 1'bz; assign sda = (sda_en == 1'b1)? i2c_sda : 1'bz;
//cfg_start
always @(posedge i2c_clk or negedge sys_rst_n)begin
if(!sys_rst_n)begin
cfg_start <= 1'b0;
end
else begin
cfg_start <= i2c_end;//
end
end
//i2c //i2c
always @(posedge sys_clk or negedge sys_rst_n) begin always @(posedge sys_clk or negedge sys_rst_n) begin
if(!sys_rst_n)begin if(!sys_rst_n)begin
@ -91,13 +106,13 @@ end
// //
always @(*)begin always @(*)begin
case(state_c) case(state_c)
IDLE: if((skip_en_1 == 1'b1) || (skip_en_2 == 1'b1) || (skip_en_3 == 1'b1) || (skip_en_4 == 1'b1))begin IDLE: if((skip_en_1 == 1'b1) || (skip_en_2 == 1'b1) || (skip_en_3 == 1'b1) || (skip_en_4 == 1'b1) || (skip_en_5 == 1'b1))begin
state_n = START; state_n = START;
end end
else begin else begin
state_n = IDLE; state_n = IDLE;
end end
START: if((skip_en_1 == 1'b1) || (skip_en_2 == 1'b1) || (skip_en_3 == 1'b1) || (skip_en_4 == 1'b1))begin START: if((skip_en_1 == 1'b1) || (skip_en_2 == 1'b1) || (skip_en_3 == 1'b1) || (skip_en_4 == 1'b1) || (skip_en_5 == 1'b1))begin
state_n = SLAVE_ADDR; state_n = SLAVE_ADDR;
end end
else begin else begin
@ -106,13 +121,13 @@ always @(*)begin
SLAVE_ADDR: if(skip_en_1 == 1'b1)begin SLAVE_ADDR: if(skip_en_1 == 1'b1)begin
state_n = WAIT; state_n = WAIT;
end end
else if((skip_en_2 == 1'b1) || (skip_en_3 == 1'b1) || (skip_en_4 == 1'b1))begin else if((skip_en_2 == 1'b1) || (skip_en_3 == 1'b1) || (skip_en_4 == 1'b1) || (skip_en_5 == 1'b1))begin
state_n = ACK_1; state_n = ACK_1;
end end
else begin else begin
state_n = SLAVE_ADDR; state_n = SLAVE_ADDR;
end end
ACK_1: if((skip_en_2 == 1'b1) || (skip_en_3 == 1'b1))begin ACK_1: if((skip_en_2 == 1'b1) || (skip_en_3 == 1'b1) || (skip_en_5 == 1'b1))begin
state_n = DEVICE_ADDR; state_n = DEVICE_ADDR;
end end
else if(skip_en_4 == 1'b1)begin else if(skip_en_4 == 1'b1)begin
@ -121,13 +136,13 @@ always @(*)begin
else begin else begin
state_n = ACK_1; state_n = ACK_1;
end end
DEVICE_ADDR:if((skip_en_2 == 1'b1) || (skip_en_3 == 1'b1))begin DEVICE_ADDR:if((skip_en_2 == 1'b1) || (skip_en_3 == 1'b1) || (skip_en_5 == 1'b1))begin
state_n = ACK_2; state_n = ACK_2;
end end
else begin else begin
state_n = DEVICE_ADDR; state_n = DEVICE_ADDR;
end end
ACK_2: if(skip_en_2 == 1'b1)begin ACK_2: if((skip_en_2 == 1'b1) || (skip_en_5 == 1'b1))begin
state_n = DATA; state_n = DATA;
end end
else if(skip_en_3 == 1'b1)begin else if(skip_en_3 == 1'b1)begin
@ -136,7 +151,7 @@ always @(*)begin
else begin else begin
state_n = ACK_2; state_n = ACK_2;
end end
DATA: if(skip_en_2 == 1'b1)begin DATA: if((skip_en_2 == 1'b1) || (skip_en_5 == 1'b1))begin
state_n = ACK_3; state_n = ACK_3;
end end
else if(skip_en_4 == 1'b1)begin else if(skip_en_4 == 1'b1)begin
@ -154,7 +169,7 @@ always @(*)begin
else begin else begin
state_n = NACK; state_n = NACK;
end end
ACK_3: if(skip_en_2 == 1'b1)begin ACK_3: if((skip_en_2 == 1'b1) || (skip_en_5 == 1'b1))begin
state_n = STOP; state_n = STOP;
end end
else begin else begin
@ -166,7 +181,7 @@ always @(*)begin
else begin else begin
state_n = WAIT; state_n = WAIT;
end end
STOP: if((skip_en_1 == 1'b1) || (skip_en_2 == 1'b1) || (skip_en_3 == 1'b1) || (skip_en_4 == 1'b1))begin STOP: if((skip_en_1 == 1'b1) || (skip_en_2 == 1'b1) || (skip_en_3 == 1'b1) || (skip_en_4 == 1'b1) || (skip_en_5 == 1'b1))begin
state_n = IDLE; state_n = IDLE;
end end
else begin else begin
@ -185,6 +200,7 @@ always @(posedge i2c_clk or negedge sys_rst_n)begin
skip_en_2 <= 1'b0;//2 skip_en_2 <= 1'b0;//2
skip_en_3 <= 1'b0;//3 skip_en_3 <= 1'b0;//3
skip_en_4 <= 1'b0;//4 skip_en_4 <= 1'b0;//4
skip_en_5 <= 1'b0;//5
step <= 3'd0; step <= 3'd0;
err_en <= 1'b0; err_en <= 1'b0;
cnt_i2c_clk <= 2'd0; cnt_i2c_clk <= 2'd0;
@ -224,6 +240,12 @@ always @(posedge i2c_clk or negedge sys_rst_n)begin
else begin else begin
skip_en_4 <= 1'b0; skip_en_4 <= 1'b0;
end end
if((cnt_wait == MAX - 2'd2) && (step == 3'd4))begin
skip_en_5 <= 1'b1;
end
else begin
skip_en_5 <= 1'b0;
end
end end
START: begin START: begin
cnt_i2c_clk <= cnt_i2c_clk + 1'd1; cnt_i2c_clk <= cnt_i2c_clk + 1'd1;
@ -251,6 +273,12 @@ always @(posedge i2c_clk or negedge sys_rst_n)begin
else begin else begin
skip_en_4 <= 1'b0; skip_en_4 <= 1'b0;
end end
if((cnt_i2c_clk == 2'd2) && (step == 3'd4))begin
skip_en_5 <= 1'b1;
end
else begin
skip_en_5 <= 1'b0;
end
end end
SLAVE_ADDR: begin SLAVE_ADDR: begin
cnt_i2c_clk <= cnt_i2c_clk + 1'd1; cnt_i2c_clk <= cnt_i2c_clk + 1'd1;
@ -278,6 +306,12 @@ always @(posedge i2c_clk or negedge sys_rst_n)begin
else begin else begin
skip_en_4 <= 1'b0; skip_en_4 <= 1'b0;
end end
if((cnt_i2c_clk == 2'd2) && (step == 3'd4) && (cnt_bit == 3'd7))begin
skip_en_5 <= 1'b1;
end
else begin
skip_en_5 <= 1'b0;
end
if((cnt_bit == 3'd7) && (cnt_i2c_clk == 2'd3))begin if((cnt_bit == 3'd7) && (cnt_i2c_clk == 2'd3))begin
cnt_bit <= 3'd0; cnt_bit <= 3'd0;
end end
@ -308,6 +342,12 @@ always @(posedge i2c_clk or negedge sys_rst_n)begin
else begin else begin
skip_en_4 <= 1'b0; skip_en_4 <= 1'b0;
end end
if((ack == 1'b1) && (cnt_i2c_clk == 2'd2) && (step == 3'd4))begin
skip_en_5 <= 1'b1;
end
else begin
skip_en_5 <= 1'b0;
end
end end
DEVICE_ADDR:begin DEVICE_ADDR:begin
cnt_i2c_clk <= cnt_i2c_clk + 1'b1; cnt_i2c_clk <= cnt_i2c_clk + 1'b1;
@ -332,6 +372,12 @@ always @(posedge i2c_clk or negedge sys_rst_n)begin
else begin else begin
skip_en_3 <= 1'b0; skip_en_3 <= 1'b0;
end end
if((cnt_i2c_clk == 2'd2) && (cnt_bit == 3'd7) && (step == 3'd4))begin
skip_en_5 <= 1'b1;
end
else begin
skip_en_5 <= 1'b0;
end
end end
ACK_2: begin ACK_2: begin
cnt_i2c_clk <= cnt_i2c_clk + 1; cnt_i2c_clk <= cnt_i2c_clk + 1;
@ -347,6 +393,12 @@ always @(posedge i2c_clk or negedge sys_rst_n)begin
else begin else begin
skip_en_3 <= 1'b0; skip_en_3 <= 1'b0;
end end
if((ack == 1'b1) && (cnt_i2c_clk == 2'd2) && (step == 3'd4))begin
skip_en_5 <= 1'b1;
end
else begin
skip_en_5 <= 1'b0;
end
end end
DATA: begin DATA: begin
cnt_i2c_clk <= cnt_i2c_clk + 1'b1; cnt_i2c_clk <= cnt_i2c_clk + 1'b1;
@ -371,6 +423,12 @@ always @(posedge i2c_clk or negedge sys_rst_n)begin
else begin else begin
skip_en_4 <= 1'b0; skip_en_4 <= 1'b0;
end end
if((cnt_i2c_clk == 2'd2) && (cnt_bit == 3'd7) && (step == 3'd4))begin
skip_en_5 <= 1'b1;
end
else begin
skip_en_5 <= 1'b0;
end
if((cnt_i2c_clk == 2'd2) && (cnt_bit == 3'd7) && (step == 3'd3) && (recv_data != 8'h20))begin if((cnt_i2c_clk == 2'd2) && (cnt_bit == 3'd7) && (step == 3'd3) && (recv_data != 8'h20))begin
begin begin
err_en <= 1'b1; err_en <= 1'b1;
@ -401,6 +459,12 @@ always @(posedge i2c_clk or negedge sys_rst_n)begin
else begin else begin
skip_en_2 <= 1'b0; skip_en_2 <= 1'b0;
end end
if((ack == 1'b1) && (cnt_i2c_clk == 2'd2) && (step == 3'd4))begin
skip_en_5 <= 1'b1;
end
else begin
skip_en_5 <= 1'b0;
end
end end
WAIT: begin WAIT: begin
if(cnt_wait == MAX - 1'd1)begin if(cnt_wait == MAX - 1'd1)begin
@ -442,13 +506,22 @@ always @(posedge i2c_clk or negedge sys_rst_n)begin
else begin else begin
skip_en_4 <= 1'b0; skip_en_4 <= 1'b0;
end end
if((cnt_i2c_clk == 2'd2) && (step == 3'd4))begin
skip_en_5 <= 1'b1;
end
else begin
skip_en_5 <= 1'b0;
end
if(cnt_i2c_clk == 2'd2)begin if(cnt_i2c_clk == 2'd2)begin
i2c_end <= 1'b1; i2c_end <= 1'b1;
end end
else begin else begin
i2c_end <= 1'b0; i2c_end <= 1'b0;
end end
if(i2c_end == 1'b1)begin if((i2c_end == 1'b1) && (step <= 3'd3))begin
step <= step + 1'd1;
end
else if((i2c_end == 1'b1) && (step == 3'd4) && (cfg_num == 6'd51))begin
step <= step + 1'd1; step <= step + 1'd1;
end end
else begin else begin
@ -461,6 +534,7 @@ always @(posedge i2c_clk or negedge sys_rst_n)begin
skip_en_2 <= 1'b0; skip_en_2 <= 1'b0;
skip_en_3 <= 1'b0; skip_en_3 <= 1'b0;
skip_en_4 <= 1'b0; skip_en_4 <= 1'b0;
skip_en_5 <= 1'b0;
err_en <= 1'b0; err_en <= 1'b0;
step <= step; step <= step;
cnt_i2c_clk <= 2'd0; cnt_i2c_clk <= 2'd0;
@ -520,6 +594,11 @@ always @(*)begin
device_addr= {8'h00}; device_addr= {8'h00};
wr_data = {8'h00}; wr_data = {8'h00};
end end
3'd4: begin
slave_addr = {SLAVE_ID, 1'b0};
device_addr= cfg_data[15: 8];
wr_data = cfg_data[7: 0];
end
default:begin default:begin
slave_addr = 8'h0; slave_addr = 8'h0;
device_addr = 8'h0; device_addr = 8'h0;

@ -0,0 +1,86 @@
module paj7620_cfg(
input wire i2c_clk ,
input wire sys_rst_n ,
input wire cfg_start ,
input wire [2: 0] step ,
output reg [5: 0] cfg_num ,
output wire [15: 0] cfg_data ,
output reg i2c_start
);
wire [15: 0] cfg_data_reg[50: 0];
always @(posedge i2c_clk or negedge sys_rst_n)begin
if(!sys_rst_n)begin
cfg_num <= 6'd0;
end
else if((cfg_start == 1'b1) && (step == 3'd4))begin
cfg_num <= cfg_num + 1'd1;
end
else begin
cfg_num <= cfg_num;
end
end
always @(posedge i2c_clk or negedge sys_rst_n)begin
if(!sys_rst_n)begin
i2c_start <= 1'b0;
end
else if((cfg_data == 1'b1) && (step == 3'd4))begin
i2c_start <= 1'b1;
end
else begin
i2c_start <= 1'b0;
end
end
assign cfg_data = (step == 3'd4) ? cfg_data_reg[cfg_num - 1]:16'h0;
assign cfg_data_reg[00] = {8'hEF,8'h00};
assign cfg_data_reg[01] = {8'h37,8'h07};
assign cfg_data_reg[02] = {8'h38,8'h17};
assign cfg_data_reg[03] = {8'h39,8'h06};
assign cfg_data_reg[04] = {8'h42,8'h01};
assign cfg_data_reg[05] = {8'h46,8'h2D};
assign cfg_data_reg[06] = {8'h47,8'h0F};
assign cfg_data_reg[07] = {8'h48,8'h3C};
assign cfg_data_reg[08] = {8'h49,8'h00};
assign cfg_data_reg[09] = {8'h4A,8'h1E};
assign cfg_data_reg[10] = {8'h4C,8'h20};
assign cfg_data_reg[11] = {8'h51,8'h10};
assign cfg_data_reg[12] = {8'h5E,8'h10};
assign cfg_data_reg[13] = {8'h60,8'h27};
assign cfg_data_reg[14] = {8'h80,8'h42};
assign cfg_data_reg[15] = {8'h81,8'h44};
assign cfg_data_reg[16] = {8'h82,8'h04};
assign cfg_data_reg[17] = {8'h8B,8'h01};
assign cfg_data_reg[18] = {8'h90,8'h06};
assign cfg_data_reg[19] = {8'h95,8'h0A};
assign cfg_data_reg[20] = {8'h96,8'h0C};
assign cfg_data_reg[21] = {8'h97,8'h05};
assign cfg_data_reg[22] = {8'h9A,8'h14};
assign cfg_data_reg[23] = {8'h9C,8'h3F};
assign cfg_data_reg[24] = {8'hA5,8'h19};
assign cfg_data_reg[25] = {8'hCC,8'h19};
assign cfg_data_reg[26] = {8'hCD,8'h0B};
assign cfg_data_reg[27] = {8'hCE,8'h13};
assign cfg_data_reg[28] = {8'hCF,8'h64};
assign cfg_data_reg[29] = {8'hD0,8'h21};
assign cfg_data_reg[30] = {8'hEF,8'h01};
assign cfg_data_reg[31] = {8'h02,8'h0F};
assign cfg_data_reg[32] = {8'h03,8'h10};
assign cfg_data_reg[33] = {8'h04,8'h02};
assign cfg_data_reg[34] = {8'h25,8'h01};
assign cfg_data_reg[35] = {8'h27,8'h39};
assign cfg_data_reg[36] = {8'h28,8'h7F};
assign cfg_data_reg[37] = {8'h29,8'h08};
assign cfg_data_reg[38] = {8'h3E,8'hFF};
assign cfg_data_reg[39] = {8'h5E,8'h3D};
assign cfg_data_reg[40] = {8'h65,8'h96};
assign cfg_data_reg[41] = {8'h67,8'h97};
assign cfg_data_reg[42] = {8'h69,8'hCD};
assign cfg_data_reg[43] = {8'h6A,8'h01};
assign cfg_data_reg[44] = {8'h6D,8'h2C};
assign cfg_data_reg[45] = {8'h6E,8'h01};
assign cfg_data_reg[46] = {8'h72,8'h01};
assign cfg_data_reg[47] = {8'h73,8'h35};
assign cfg_data_reg[48] = {8'h74,8'h00};
assign cfg_data_reg[49] = {8'h77,8'h01};
assign cfg_data_reg[50] = {8'hEF,8'h00};
endmodule
Loading…
Cancel
Save