i2c.sv 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  1. //
  2. // Simple I2C master devic, controlled by programmed I/O.
  3. //
  4. // Registers:
  5. // 0 - write data/command
  6. // [15:8] - data bits (must be 1 for read)
  7. // [7] - ACK bit (must be 1 for write)
  8. // [2] - follow with P condition
  9. // [1] - follow with Sr condition
  10. //
  11. // 1 - read data/status
  12. // [15:8] - data bits
  13. // [7] - ack bit
  14. // [1] - bus idle (after P condition)
  15. // [0] - busy
  16. //
  17. // 2 - baud rate divisor (f = clk/(4*(divisor+1)))
  18. //
  19. // This unit handles S(r) and P conditions by considering two classes of
  20. // symbols: "normal", when SCL is asserted at the end of the
  21. module i2c (
  22. input rst_n,
  23. input clk,
  24. input valid,
  25. input [1:0] addr,
  26. input [31:0] wdata,
  27. input [3:0] wstrb,
  28. output reg [31:0] rdata,
  29. output irq,
  30. inout i2c_scl,
  31. inout i2c_sda
  32. );
  33. reg [7:0] divisor;
  34. reg [7:0] baudctr;
  35. reg [3:0] bitctr;
  36. reg [1:0] phase;
  37. reg [8:0] wreg; // Output shift register
  38. reg [8:0] rreg; // Input shift register
  39. reg do_read; // Shift in a data bit next cycle
  40. reg busy; // Data received, running
  41. reg end_s, end_p; // Trailing S(r) or P
  42. reg started; // S sent, but not P
  43. reg is_sr; // Sending an S(r)
  44. reg [1:0] outsymb; // Output symbol [abnormal, data]
  45. reg scl_out = 1'b1;
  46. reg sda_out = 1'b1;
  47. assign i2c_scl = scl_out ? 1'bz : 1'b0;
  48. assign i2c_sda = sda_out ? 1'bz : 1'b0;
  49. always @(negedge rst_n or posedge clk)
  50. if (~rst_n)
  51. begin
  52. bitctr <= 4'd14; // Idle line - wait for start condition
  53. busy <= 1'b0;
  54. outsymb <= 2'b11; // A1
  55. baudctr <= 8'd0;
  56. divisor <= 8'd209; // 84 MHz -> 100 kHz
  57. scl_out <= 1'b1;
  58. sda_out <= 1'b1;
  59. phase <= 2'b00;
  60. do_read <= 1'b0;
  61. started <= 1'b0;
  62. end
  63. else
  64. begin
  65. //
  66. // I2C state machine
  67. //
  68. if (|baudctr)
  69. begin
  70. baudctr <= baudctr - 1'b1;
  71. end
  72. else
  73. begin
  74. // I2C clock event
  75. baudctr <= divisor;
  76. phase <= phase + 1'b1;
  77. if ((phase == 2'b10) & ~i2c_scl)
  78. phase <= 2'b10; // Clock stretch
  79. // Phase 0: data is shifted out
  80. // Phase 1: SCL goes high
  81. // Phase 2: if SCL is low, hold
  82. // Phase 3: SCL does low if symbol is normal,
  83. // sample SDA on read
  84. if (phase[0])
  85. scl_out <= outsymb[1] | ~phase[1];
  86. sda_out <= outsymb[0];
  87. if (phase == 2'b11)
  88. begin
  89. // Sample input and set up for the next cycle
  90. if (do_read)
  91. rreg <= { rreg[7:0], i2c_sda };
  92. do_read <= 1'b0;
  93. // Unit idle; send A0 or A1 depending on if we are
  94. // started or not.
  95. if (~busy)
  96. begin
  97. outsymb <= { 1'b1, ~started };
  98. end
  99. else
  100. begin
  101. bitctr <= bitctr + 1'b1;
  102. started <= 1'b1;
  103. case (bitctr)
  104. 4'd0, 4'd1, 4'd2, 4'd3, 4'd4,
  105. 4'd5, 4'd6, 4'd7, 4'd8:
  106. begin
  107. outsymb <= { 1'b0, wreg[8] }; // Nx
  108. wreg <= { wreg[7:0], 1'b1 };
  109. do_read <= 1'b1;
  110. if (bitctr[3])
  111. begin
  112. bitctr <= end_p ? 4'd12 :
  113. end_s ? 4'd14 : 4'd0;
  114. busy <= end_p;
  115. // If we are to be followed by
  116. // an S(r) condition, we are not
  117. // really "started".
  118. started <= ~(end_s | end_p);
  119. end
  120. end // case: 4'd0, 4'd1, 4'd2, 4'd3, 4'd4,...
  121. // Stop condition
  122. 4'd12: begin
  123. started <= 1'b0;
  124. outsymb <= 2'b10; // A0
  125. end
  126. 4'd13: begin
  127. started <= 1'b0;
  128. outsymb <= 2'b11; // A1
  129. busy <= 1'b0;
  130. end
  131. // Start condition
  132. 4'd14: begin
  133. started <= 1'b1;
  134. outsymb <= 2'b11; // A1
  135. end
  136. 4'd15: begin
  137. started <= 1'b1;
  138. outsymb <= 2'b00; // N0
  139. end
  140. default: begin
  141. outsymb <= 2'bxx;
  142. end
  143. endcase // case (bitctr)
  144. end // else: !if(~busy)
  145. end // if (phase == 2'b11)
  146. end // else: !if(|baudctr)
  147. //
  148. // CPU write interface
  149. //
  150. if (valid)
  151. case (addr)
  152. 2'b00:
  153. if (~busy)
  154. begin
  155. if (wstrb[1])
  156. wreg[8:1] <= wdata[15:8];
  157. if (wstrb[0])
  158. begin
  159. wreg[0] <= wdata[7];
  160. end_p <= wdata[2];
  161. end_s <= wdata[1];
  162. busy <= 1'b1;
  163. end
  164. end // if (~busy)
  165. 2'b10: begin
  166. if (wstrb[0])
  167. divisor <= wdata[7:0];
  168. end
  169. default: begin
  170. // Do nothing
  171. end
  172. endcase // case (addr)
  173. end // else: !if(~rst_n)
  174. //
  175. // CPU read interface
  176. //
  177. always_comb
  178. case (addr)
  179. 2'b00: rdata = { 16'b0, wreg, 4'b0, end_p, end_s, busy | do_read };
  180. 2'b01: rdata = { 16'b0, rreg, 4'b0, end_p, end_s, busy | do_read };
  181. 2'b10: rdata = { 24'b0, divisor };
  182. default: rdata = 32'b0;
  183. endcase // casez (addr)
  184. //
  185. // IRQ (edge) when unit idle
  186. //
  187. assign irq = ~(busy | do_read);
  188. endmodule // i2c