i2c.sv 5.5 KB

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