// +clk2------------------------------------------ // 1 ) 20mhz -> clk2 -> SCL-400khz clk2==2xSCL // 2 ) cnt = 20m/800k = 25 /2 = 12 cnt[3: 0 ] // -clk2 ------------------------------------------------ 06月begin cnt_clk <= 0 ; clk_800k <= 0 ; end else if (cnt_clk== 12 ) begin cnt_clk <= 0 ; clk_800k <= ~clk_800k; // scl@falling sda@rising end else cnt_clk <= cnt_clk + 1 ;
// +iic------------------------------------------ // 1) // -iic------------------------------------------ always @( negedge clk_800k or negedge rst_n) // negedge if (rst_n== 0 ) iic_scl <= 0 ; else iic_scl <= ~iic_scl; // 400k
// +iic------------------------------------------ // 1) // -iic------------------------------------------ always @( posedge clk_800k or negedge rst_n) if (rst_n== 0 ) begin NS_iic <= IIC_idle; iic_sda <= 1 ; finish_iic <= 0 ; end else case (NS_iic) IIC_idle: begin if (start_iic== 1 ) NS_iic <= IIC_start; else NS_iic <= IIC_idle; end IIC_start: begin if (iic_scl== 1 ) begin NS_iic <= IIC_data_setup; iic_sda <= 0 ; // start condition data_iic <= bram_dout[ 23 : 0 ]; cnt_byte <= 2 ; cnt_bit <= 0 ; // 0->7 end // else // NS_iic <= IIC_start; end IIC_data_setup: // data setup begin NS_iic <= IIC_data_hold; iic_sda <= data_iic[ 23 ]; cnt_bit <= cnt_bit - 1 ; end IIC_data_hold: // data hold begin if (cnt_bit== 0 ) NS_iic <= IIC_ack_setup; else NS_iic <= IIC_data_setup; data_iic <= {data_iic[ 22 : 0 ], 1 ' b0}; end IIC_ack_setup: // ack prepare begin NS_iic <= IIC_ack_hold; iic_sda <= 1 ' bz; end IIC_ack_hold: // ack response begin if (iic_sda== 1 ||cnt_byte== 0 ) NS_iic <= IIC_stop_setup; else NS_iic <= IIC_data_setup; cnt_byte <= cnt_byte - 1 ; end IIC_stop_setup: begin NS_iic <= IIC_stop_hold; iic_sda <= 0 ; finish_iic <= 1 ; end IIC_stop_hold: begin NS_iic <= IIC_idle; iic_sda <= 1 ; finish_iic <= 0 ; end default : begin NS_iic <= IIC_idle; end endcase