i2c_control.v 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221
  1. module i2c_control(
  2. Clk,
  3. Rst_n,
  4. wrreg_req,
  5. rdreg_req,
  6. addr,
  7. addr_mode,
  8. wrdata,
  9. rddata,
  10. device_id,
  11. RW_Done,
  12. ack,
  13. i2c_sclk,
  14. i2c_sdat
  15. );
  16. input Clk;
  17. input Rst_n;
  18. input wrreg_req;
  19. input rdreg_req;
  20. input [15:0]addr;
  21. input addr_mode;
  22. input [7:0]wrdata;
  23. output reg[7:0]rddata;
  24. input [7:0]device_id;
  25. output reg RW_Done;
  26. output reg ack;
  27. output i2c_sclk;
  28. inout i2c_sdat;
  29. reg [5:0]Cmd;
  30. reg [7:0]Tx_DATA;
  31. wire Trans_Done;
  32. wire ack_o;
  33. reg Go;
  34. wire [15:0] reg_addr;
  35. assign reg_addr = addr_mode?addr:{addr[7:0],addr[15:8]};
  36. wire [7:0]Rx_DATA;
  37. localparam
  38. WR = 6'b000001, //写请求
  39. STA = 6'b000010, //起始位请求
  40. RD = 6'b000100, //读请求
  41. STO = 6'b001000, //停止位请求
  42. ACK = 6'b010000, //应答位请求
  43. NACK = 6'b100000; //无应答请求
  44. i2c_bit_shift i2c_bit_shift(
  45. .Clk(Clk),
  46. .Rst_n(Rst_n),
  47. .Cmd(Cmd),
  48. .Go(Go),
  49. .Rx_DATA(Rx_DATA),
  50. .Tx_DATA(Tx_DATA),
  51. .Trans_Done(Trans_Done),
  52. .ack_o(ack_o),
  53. .i2c_sclk(i2c_sclk),
  54. .i2c_sdat(i2c_sdat)
  55. );
  56. reg [6:0]state;
  57. reg [7:0]cnt;
  58. localparam
  59. IDLE = 7'b0000001, //空闲状态
  60. WR_REG = 7'b0000010, //写寄存器状态
  61. WAIT_WR_DONE = 7'b0000100, //等待写寄存器完成状态
  62. WR_REG_DONE = 7'b0001000, //写寄存器完成状态
  63. RD_REG = 7'b0010000, //读寄存器状态
  64. WAIT_RD_DONE = 7'b0100000, //等待读寄存器完成状态
  65. RD_REG_DONE = 7'b1000000; //读寄存器完成状态
  66. always@(posedge Clk or negedge Rst_n)
  67. if(!Rst_n)begin
  68. Cmd <= 6'd0;
  69. Tx_DATA <= 8'd0;
  70. Go <= 1'b0;
  71. rddata <= 0;
  72. state <= IDLE;
  73. ack <= 0;
  74. end
  75. else begin
  76. case(state)
  77. IDLE:
  78. begin
  79. cnt <= 0;
  80. ack <= 0;
  81. RW_Done <= 1'b0;
  82. if(wrreg_req)
  83. state <= WR_REG;
  84. else if(rdreg_req)
  85. state <= RD_REG;
  86. else
  87. state <= IDLE;
  88. end
  89. WR_REG:
  90. begin
  91. state <= WAIT_WR_DONE;
  92. case(cnt)
  93. 0:write_byte(WR | STA, device_id);
  94. 1:write_byte(WR, reg_addr[15:8]);
  95. 2:write_byte(WR, reg_addr[7:0]);
  96. 3:write_byte(WR | STO, wrdata);
  97. default:;
  98. endcase
  99. end
  100. WAIT_WR_DONE:
  101. begin
  102. Go <= 1'b0;
  103. if(Trans_Done)begin
  104. ack <= ack | ack_o;
  105. case(cnt)
  106. 0: begin cnt <= 1; state <= WR_REG;end
  107. 1:
  108. begin
  109. state <= WR_REG;
  110. if(addr_mode)
  111. cnt <= 2;
  112. else
  113. cnt <= 3;
  114. end
  115. 2: begin
  116. cnt <= 3;
  117. state <= WR_REG;
  118. end
  119. 3:state <= WR_REG_DONE;
  120. default:state <= IDLE;
  121. endcase
  122. end
  123. end
  124. WR_REG_DONE:
  125. begin
  126. RW_Done <= 1'b1;
  127. state <= IDLE;
  128. end
  129. RD_REG:
  130. begin
  131. state <= WAIT_RD_DONE;
  132. case(cnt)
  133. 0:write_byte(WR | STA, device_id);
  134. 1:write_byte(WR, reg_addr[15:8]);
  135. 2:write_byte(WR, reg_addr[7:0]);
  136. 3:write_byte(WR | STA, device_id | 8'd1);
  137. 4:read_byte(RD | NACK | STO);
  138. default:;
  139. endcase
  140. end
  141. WAIT_RD_DONE:
  142. begin
  143. Go <= 1'b0;
  144. if(Trans_Done)begin
  145. if(cnt <= 3)
  146. ack <= ack | ack_o;
  147. case(cnt)
  148. 0: begin cnt <= 1; state <= RD_REG;end
  149. 1:
  150. begin
  151. state <= RD_REG;
  152. if(addr_mode)
  153. cnt <= 2;
  154. else
  155. cnt <= 3;
  156. end
  157. 2: begin
  158. cnt <= 3;
  159. state <= RD_REG;
  160. end
  161. 3:begin
  162. cnt <= 4;
  163. state <= RD_REG;
  164. end
  165. 4:state <= RD_REG_DONE;
  166. default:state <= IDLE;
  167. endcase
  168. end
  169. end
  170. RD_REG_DONE:
  171. begin
  172. RW_Done <= 1'b1;
  173. rddata <= Rx_DATA;
  174. state <= IDLE;
  175. end
  176. default:state <= IDLE;
  177. endcase
  178. end
  179. task read_byte;
  180. input [5:0]Ctrl_Cmd;
  181. begin
  182. Cmd <= Ctrl_Cmd;
  183. Go <= 1'b1;
  184. end
  185. endtask
  186. task write_byte;
  187. input [5:0]Ctrl_Cmd;
  188. input [7:0]Wr_Byte_Data;
  189. begin
  190. Cmd <= Ctrl_Cmd;
  191. Tx_DATA <= Wr_Byte_Data;
  192. Go <= 1'b1;
  193. end
  194. endtask
  195. endmodule