2
0

i2c.sv 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  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 [1:0] outsymb; // Output symbol [abnormal, data]
  43. reg scl_out = 1'b1;
  44. reg sda_out = 1'b1;
  45. assign i2c_scl = scl_out ? 1'bz : 1'b0;
  46. assign i2c_sda = sda_out ? 1'bz : 1'b0;
  47. always @(negedge rst_n or posedge clk)
  48. if (~rst_n)
  49. begin
  50. bitctr <= 4'd14; // Idle line - wait for start condition
  51. busy <= 1'b0;
  52. outsymb <= 2'b11; // A1
  53. baudctr <= 8'd0;
  54. divisor <= 8'd209; // 84 MHz -> 100 kHz
  55. scl_out <= 1'b1;
  56. sda_out <= 1'b1;
  57. phase <= 2'b00;
  58. do_read <= 1'b0;
  59. end
  60. else
  61. begin
  62. //
  63. // I2C state machine
  64. //
  65. if (|baudctr)
  66. begin
  67. baudctr <= baudctr - 1'b1;
  68. end
  69. else
  70. begin
  71. // I2C clock event
  72. baudctr <= divisor;
  73. phase <= phase + 1'b1;
  74. if ((phase == 2'b10) & ~i2c_scl)
  75. phase <= 2'b10; // Clock stretch
  76. // Phase 0: data is shifted out
  77. // Phase 1: SCL goes high
  78. // Phase 2: if SCL is low, hold
  79. // Phase 3: SCL does low if symbol is normal,
  80. // sample SDA on read
  81. if (phase[0])
  82. scl_out <= outsymb[1] | ~phase[1];
  83. sda_out <= outsymb[0];
  84. if (phase == 2'b11)
  85. begin
  86. // Sample input and set up for the next cycle
  87. if (do_read)
  88. rreg <= { rreg[7:0], i2c_sda };
  89. do_read <= 1'b0;
  90. // Unit idle; send A0 or A1 depending on if we are
  91. // started or not.
  92. if (~busy)
  93. begin
  94. outsymb[1] <= 1'b1;
  95. end
  96. else
  97. begin
  98. bitctr <= bitctr + 1'b1;
  99. case (bitctr)
  100. 4'd0, 4'd1, 4'd2, 4'd3, 4'd4,
  101. 4'd5, 4'd6, 4'd7, 4'd8:
  102. begin
  103. outsymb <= { 1'b0, wreg[8] }; // Nx
  104. wreg <= { wreg[7:0], 1'b1 };
  105. do_read <= 1'b1;
  106. if (bitctr[3])
  107. begin
  108. bitctr <= end_p ? 4'd12 :
  109. end_s ? 4'd14 : 4'd0;
  110. busy <= end_p;
  111. end
  112. end // case: 4'd0, 4'd1, 4'd2, 4'd3, 4'd4,...
  113. // Stop condition
  114. 4'd12: begin
  115. outsymb <= 2'b10; // A0
  116. end
  117. 4'd13: begin
  118. outsymb <= 2'b11; // A1
  119. busy <= 1'b0;
  120. end
  121. // Start condition
  122. 4'd14: begin
  123. outsymb <= 2'b11; // A1
  124. end
  125. 4'd15: begin
  126. outsymb <= 2'b00; // N0
  127. end
  128. default: begin
  129. outsymb <= 2'bxx;
  130. end
  131. endcase // case (bitctr)
  132. end // else: !if(~busy)
  133. end // if (phase == 2'b11)
  134. end // else: !if(|baudctr)
  135. //
  136. // CPU write interface
  137. //
  138. if (valid)
  139. case (addr)
  140. 2'b00:
  141. if (~busy)
  142. begin
  143. if (wstrb[1])
  144. wreg[8:1] <= wdata[15:8];
  145. if (wstrb[0])
  146. begin
  147. wreg[0] <= wdata[7];
  148. end_p <= wdata[2];
  149. end_s <= wdata[1];
  150. busy <= 1'b1;
  151. end
  152. end // if (~busy)
  153. 2'b10: begin
  154. if (wstrb[0])
  155. divisor <= wdata[7:0];
  156. end
  157. default: begin
  158. // Do nothing
  159. end
  160. endcase // case (addr)
  161. end // else: !if(~rst_n)
  162. //
  163. // CPU read interface
  164. //
  165. always_comb
  166. case (addr)
  167. 2'b00: rdata = { 16'b0, wreg, 4'b0, end_p, end_s, busy | do_read };
  168. 2'b01: rdata = { 16'b0, rreg, 4'b0, end_p, end_s, busy | do_read };
  169. 2'b10: rdata = { 24'b0, divisor };
  170. default: rdata = 32'b0;
  171. endcase // casez (addr)
  172. //
  173. // IRQ (edge) when unit idle
  174. //
  175. assign irq = ~(busy | do_read);
  176. endmodule // i2c