i178cslavecontroller(编辑修改稿)内容摘要:

synchronous reset,active low //INPUTS input rst_n。 input i2c_sclk。 input clk_ slave。 input[7:0] reg_dat_from_ slave。 input i2c_sdin。 //OUTPUTS output i2c_sdin_out_zero。 output ack。 output start_t。 output stop_t。 24 output[7:0]reg_addr。 output reg_dat_wr。 output reg_dat_rd。 output[7:0]reg_dat_to_ slave。 //INOUTS //SIGNAL DECLARATIONS reg [7:0] reg_addr。 reg [7:0] reg_dat_to_slave。 reg reg_dat_wr。 reg reg_dat_rd。 reg [3:0] bit_t。 //Counter the posedge jump of i2c_sclk reg rw。 //Read/write direction 1: read。 0: write reg [7:0] sr。 //8bit parallel data reg reg_dat_rd_dly。 reg [7:0] shift_rd_dat。 reg i2c_sclk_dly0。 reg i2c_sclk_dly1。 reg i2c_sclk_dly2。 reg i2c_sdin_dly0。 reg i2c_sdin_dly1。 reg i2c_sdin_dly2。 reg sta_condition。 //Start conditon reg sto_condition。 //Stop condition reg [3:0] cur_state。 reg [3:0] nxt_state。 wire pos_jp_sclk, wire neg_jp_sclk。 wire pos_jp_sdin。 wire neg_jp_sdin。 wire ld。 //Load downcounter wire my_addr。 //My address is called? 25 wire acc_done。 // 8bits transfered wire start_t。 wire stop_t。 wire i2c_sdin_out_zero。 wire i2c_sdin_out_en。 wire i2c_sdin_out。 //PARAMETERS // I2C slave address parameter I2C_SLAVE_ADDR= 739。 h40。 //statemachine Declaration parameter ST_IDLE = 439。 b0000。 parameter SLAVE_ADDR = 439。 b0001。 parameter SLAVE_ADDR_ACK= 439。 b0010。 parameter REG_ADDR = 439。 b0011。 parameter REG_ADDR_ACK = 439。 b0100。 parameter REG_DAT = 439。 b0101。 parameter REG_DAT_ACK = 439。 b0110。 parameter RD_REG_DAT = 439。 b0111。 parameter RD_REG_DAT_ACK= 439。 b1000。 //ASSIGN STATEMENTS //generate start and stop signal assign start_t = sta_condition。 assign stop_t = sto_condition。 //generate sequential signal assign pos_jp_sclk = i2c_sclk_dly1 amp。 amp。 !i2c_sclk_dly2。 assign neg_jp_sclk = i2c_sclk_dly2 amp。 amp。 !i2c_sclk_dly1。 assign pos_jp_sdin = i2c_sdin_dly1 amp。 amp。 !i2c_sdin_dly2。 assign neg_jp_sdin = i2c_sdin_dly2 amp。 amp。 !i2c_sdin_dly1。 //generate access done signal 26 assign acc_done= !(|bit_t)。 //generate shift register assign my_addr = (sr[7:1] == I2C_SLAVE_ADDR) amp。 amp。 (acc_done) amp。 amp。 (cur_state == SLAVE_ADDR)。 //generate load downcounter signal assign ld = (cur_state == ST_IDLE) || (cur_state == SLAVE_ADDR_ACK) || (cur_state == REG_ADDR_ACK) || (cur_state == REG_DAT_ACK) || sta_condition || sto_condition|| (cur_state == RD_REG_DAT_ACK)。 // generate acknowledge signal assign ack = (cur_state == SLAVE_ADDR_ACK) || (cur_state == REG_ADDR_ACK) || (cur_state == REG_DAT_ACK)。 assign i2c_sdin_out = shift_rd_dat[7]。 assign i2c_sdin_out_en = (cur_state == RD_REG_DAT) amp。 amp。 rw。 assign i2c_sdin_out_zero = ack ? 139。 b0 : (i2c_sdin_out_en ? i2c_sdin_out : 139。 b1)。 //MAIN CODE //sync i2c_sdin and i2c_sclk always @(posedge clk_ slave or negedge rst_n) if (!rst_n) begin i2c_sclk_dly0 = `DEL 139。 b1。 i2c_sclk_dly1 = `DEL 139。 b1。 i2c_sclk_dly2 = `DEL 139。 b1。 i2c_sdin_dly0 = `DEL 139。 b1。 i2c_sdin_dly1 = `DEL 139。 b1。 i2c_sdin_dly2 = `DEL 139。 b1。 end else begin i2c_sclk_dly0 = `DEL i2c_sclk。 27 i2c_sclk_dly1 = `DEL i2c_sclk_dly0。 i2c_sclk_dly2 = `DEL i2c_sclk_dly1。 i2c_sdin_dly0 = `DEL i2c_sdin。 i2c_sdin_dly1 = `DEL i2c_sdin_dly0。 i2c_sdin_dly2 = `DEL i2c_sdin_dly1。 end always @(posedge clk_ slave or negedge rst_n) if (!rst_n) sr = `DEL 839。 b0。 else if (pos_jp_sclk) sr = `DEL {sr[6:0], i2c_sdin_dly1}。 always @(posedge clk_ slave or negedge rst_n) if (!rst_n) cur_state = `DEL ST_IDLE。 else cur_state = `DEL nxt_state。 //find out the START condition always @(posedge clk_slave or negedge rst_n) if (!rst_n) sta_condition = `DEL 139。 b0。 else if (i2c_sclk_dly1 amp。 amp。 neg_jp_sdin) sta_condition = `DEL 139。 b1。 else sta_condition = `DEL 139。 b0。 //find out the STOP condition always @(posedge clk_slave or negedge rst_n) if (!rst_n) sto_condition = `DEL 139。 b0。 else if (pos_jp_sdin amp。 amp。 i2c_sclk_dly1) sto_condition = `DEL 139。 b1。 28 else sto_condition = `DEL 139。 b0。 always @(cur_state or sta_condition or sto_condition or acc_done or my_addr or neg_jp_sclk or rw ) if (sto_condition) nxt_state = ST_IDLE。 else if (sta_condition) nxt_state = SLAVE_ADDR。 else begin case (cur_state) SLAVE_ADDR: nxt_state = (acc_done amp。 amp。 neg_jp_sclk) ? (my_addr ? SLAVE_ADDR_ACK : ST_IDLE) :(SLAVE_ADDR)。 SLAVE_ADDR_ACK: nxt_state = neg_jp_sclk ? (rw ? RD_REG_DAT: REG_ADDR) : SLAVE_ADDR_ACK。 REG_ADDR: nxt_state = (acc_done amp。 amp。 neg_jp_sclk) ? REG_ADDR_ACK : REG_ADDR。 REG_ADDR_ACK: nxt_state = neg_jp_sclk ? REG_DAT : REG_ADDR_ACK。 REG_DAT: nxt_state = (acc_done amp。 amp。 neg_jp_sclk) ? REG_DAT_ACK : REG_DAT。 REG_DAT_ACK: nxt_state = neg_jp_sclk ? REG_ADDR : REG_DAT_ACK。 RD_REG_DAT: nxt_state = (acc_done amp。 amp。 neg_jp_sclk) ? RD_REG_DAT_ACK : RD_REG_DAT。 RD_REG_DAT_ACK: nxt_state = neg_jp_sclk ? ST_IDLE : RD_REG_DAT_ACK。 default: nxt_state = ST_IDLE。 endcase end always @(posedge clk_slave or negedge rst_n) if (!rst_n) 29 reg_addr = `DEL 839。 b0。 else if ( (cur_state == REG_ADDR) amp。 amp。 (acc_done amp。 amp。 neg_jp_sclk) ) reg_addr = `DEL sr。 always @(posedge clk_slave or negedge rst_n) if (!rst_n) reg_dat_to_slave = `DEL 839。 h0。 else if( (cur_state == REG_DAT) amp。 amp。 (acc_done amp。 amp。 neg_jp_sclk) ) reg_dat_to_slave = `DEL sr。 always @(posedge clk_slave or negedge rst_n) if (!rst_n) rw = `DEL 139。 b0。 else if ( (cur_state ==。
阅读剩余 0%
本站所有文章资讯、展示的图片素材等内容均为注册用户上传(部分报媒/平媒内容转载自网络合作媒体),仅供学习参考。 用户通过本站上传、发布的任何内容的知识产权归属用户或原始著作权人所有。如有侵犯您的版权,请联系我们反馈本站将在三个工作日内改正。