i2c.sv 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226
  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" (no S will be issued 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] - send dummy clocks on SCL when 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'bx;
  68. end_p <= 1'bx;
  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. outsymb <= { 1'b1, ~started };
  106. end
  107. else
  108. begin
  109. bitctr <= bitctr + 1'b1;
  110. started <= 1'b1;
  111. case (bitctr)
  112. 4'd0, 4'd1, 4'd2, 4'd3, 4'd4,
  113. 4'd5, 4'd6, 4'd7, 4'd8:
  114. begin
  115. outsymb <= { 1'b0, wreg[8] }; // Nx
  116. wreg <= { wreg[7:0], 1'b1 };
  117. do_read <= 1'b1;
  118. if (bitctr[3])
  119. begin
  120. bitctr <= end_s ? 4'd14 :
  121. end_p ? 4'd12 : 4'b0;
  122. busy <= end_p & ~end_s;
  123. // If we are to be followed by
  124. // an S(r) condition, we are not
  125. // really "started".
  126. started <= ~(end_s | end_p);
  127. end
  128. end // case: 4'd0, 4'd1, 4'd2, 4'd3, 4'd4,...
  129. // Stop condition
  130. 4'd12: begin
  131. started <= 1'b0;
  132. outsymb <= 2'b10; // A0
  133. end
  134. 4'd13: begin
  135. started <= 1'b0;
  136. outsymb <= 2'b11; // A1
  137. busy <= 1'b0;
  138. end
  139. // Start condition
  140. 4'd14: begin
  141. started <= ~(end_s & end_p);
  142. outsymb <= 2'b11; // A1
  143. end
  144. 4'd15: begin
  145. // N0, unless dummy in which case N1
  146. outsymb <= { 1'b0, ~started };
  147. end
  148. default: begin
  149. outsymb <= 2'bxx;
  150. end
  151. endcase // case (bitctr)
  152. end // else: !if(~busy)
  153. end // if (phase == 2'b11)
  154. end // else: !if(|baudctr)
  155. //
  156. // CPU write interface
  157. //
  158. if (valid)
  159. case (addr)
  160. 2'b00:
  161. if (~busy)
  162. begin
  163. if (wstrb[1])
  164. wreg[8:1] <= wdata[15:8];
  165. if (wstrb[0])
  166. begin
  167. wreg[0] <= wdata[7];
  168. end_p <= wdata[2];
  169. end_s <= wdata[1];
  170. busy <= 1'b1;
  171. end
  172. end // if (~busy)
  173. 2'b10: begin
  174. if (wstrb[0])
  175. divisor <= wdata[7:0];
  176. end
  177. default: begin
  178. // Do nothing
  179. end
  180. endcase // case (addr)
  181. end // else: !if(~rst_n)
  182. //
  183. // CPU read interface
  184. //
  185. always_comb
  186. case (addr)
  187. 2'b00: rdata = { 16'b0, wreg, i2c_sda, i2c_scl, started, 1'b0,
  188. end_p, end_s, busy | do_read };
  189. 2'b01: rdata = { 16'b0, rreg, i2c_sda, i2c_scl, started, 1'b0,
  190. end_p, end_s, busy | do_read };
  191. 2'b10: rdata = { 24'b0, divisor };
  192. default: rdata = 32'bx;
  193. endcase // casez (addr)
  194. //
  195. // IRQ (level) when unit idle
  196. //
  197. assign irq = ~(busy | do_read);
  198. endmodule // i2c