2
0

spi_master.sv 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  1. //
  2. // spi_master.sv
  3. //
  4. // Simple byte-oriented SPI master unit with optional multiwidth support
  5. // (1, 2, 4, 8).
  6. //
  7. // The output SPI clock equals the system clock /2 unless clk_en is used
  8. // to throttle the output clock.
  9. //
  10. // spi_io[0] = DI, spi_io[1] = DO in single bit mode.
  11. //
  12. // By default the output is latched; if not, it is only valid in
  13. // the cycle that eack is asserted (useful for FIFOs etc.)
  14. //
  15. //
  16. // XXX: CHPA option (mode 1/3)
  17. //
  18. `define IO_MAX max(ilog2c(width)-1, 1)
  19. function reg [7:0] shift_in
  20. (
  21. input [7:0] prev,
  22. input [7:0] in,
  23. input [1:0] xwidth, // log2(transaction width)
  24. input [0:0] lsb
  25. );
  26. (* full_case *)
  27. case (xwidth)
  28. 2'd0:
  29. shift_in = lsb ? { in[0], prev[7:1] } : { prev[6:0], in[0] };
  30. 2'd1:
  31. shift_in = lsb ? { in[1:0], prev[7:2] } : { prev[5:0], in[1:0] };
  32. 2'd2:
  33. shift_in = lsb ? { in[3:0], prev[7:4] } : { prev[3:0], in[3:0] };
  34. 2'd3:
  35. shift_in = in;
  36. endcase // case (width)
  37. endfunction // shift_in
  38. module spi_master
  39. #(
  40. parameter width = 1, // Max width of SPI bus (1, 2, 4, or 8)
  41. parameter n_cs = 1, // Number of CS# outputs
  42. parameter cs_delay = 1, // Time from CS# low to first data
  43. parameter latch_q = 1 // Latch output
  44. )
  45. (
  46. input rst_n, // Unit reset
  47. input clk, // System clock
  48. input clk_en, // SPI clock enable
  49. input [7:0] d, // System data in
  50. output [7:0] q, // System data out
  51. input req, // Session request
  52. input dir, // Session is write (for multibit)
  53. input [1:0] iowidth, // Session width (lg2)
  54. output reg sack, // Session started
  55. output reg eack, // Session ended
  56. input idle_io, // Signal level for I/Os at idle
  57. input cpol, // Clock polarity (usually constant)
  58. input lsb, // Littleendian mode (usually constant)
  59. input [n_cs-1:0] cs, // Device select (active high)
  60. output reg spi_sck, // SPI clock
  61. inout [`IO_MAX:0] spi_io, // SPI data
  62. output [n_cs-1:0] spi_cs_n // SPI CS# lines
  63. );
  64. localparam io_max = `IO_MAX;
  65. localparam iowidth_max = ilog2c(width);
  66. localparam ctr_max = max(ilog2c(cs_delay)-1,2);
  67. reg spi_active;
  68. reg [((width > 2) ? 1 : 0):0] spi_width;
  69. reg [ctr_max:0] spi_ctr;
  70. reg [n_cs-1:0] spi_cs_q;
  71. reg [io_max:0] spi_out_q;
  72. reg [1:0] spi_oe_q;
  73. reg d_dir;
  74. reg [7:0] d_out; // Output shift register
  75. reg [7:0] d_in; // Input shift register
  76. reg [7:0] q_q; // Latched output data
  77. assign spi_cs_n = ~spi_cs_q;
  78. assign q = latch_q ? q_q : d_in;
  79. wire spi_cs_changed = |(spi_cs_q ^ cs);
  80. // Always 2'b10 for single-bit SPI
  81. wire [1:0] spi_oe = (width > 1) ? spi_oe_q : 2'b10;
  82. assign spi_io[0] = spi_oe[0] ? spi_out_q[0] : 1'bz;
  83. assign spi_io[io_max:1] = spi_oe[1] ? spi_out_q[io_max:1] : {io_max{1'bz}};
  84. // Make it clear to the compiler that iowidth is undefined if
  85. // it is too large
  86. wire [1:0] iowidth_in = (iowidth > iowidth_max) ? 2'bxx : iowidth;
  87. // Just for convenience...
  88. wire [3:0] spi_width_n = 1'b1 << spi_width;
  89. wire [io_max:0] idle_allio = {(io_max+1){idle_io}};
  90. always @(negedge rst_n or posedge clk)
  91. if (~rst_n)
  92. begin
  93. spi_sck <= 1'b0;
  94. spi_active <= 1'b0;
  95. spi_width <= 4'b0001;
  96. spi_ctr <= 1'b0;
  97. spi_cs_q <= 1'b0;
  98. spi_out_q <= idle_allio;
  99. spi_oe_q <= 2'b10;
  100. sack <= 1'b0;
  101. eack <= 1'b0;
  102. d_out <= idle_allio;
  103. d_in <= 8'hxx;
  104. q_q <= 8'hxx;
  105. end
  106. else
  107. begin
  108. // These are always single system clock pulses
  109. sack <= 1'b0;
  110. eack <= 1'b0;
  111. if (clk_en)
  112. begin
  113. spi_ctr <= spi_ctr - 1'b1;
  114. spi_sck <= (spi_ctr[0] & spi_active) ^ cpol;
  115. if (~spi_ctr[0])
  116. begin
  117. d_in <= shift_in(d_in, spi_io, spi_width, lsb);
  118. end
  119. else
  120. begin
  121. spi_out_q <= idle_allio;
  122. if (spi_active)
  123. begin
  124. if (~|spi_ctr[ctr_max:1])
  125. begin
  126. eack <= 1'b1;
  127. q_q <= d_in;
  128. spi_active <= 1'b0;
  129. end
  130. if (spi_width > iowidth_max)
  131. begin
  132. // Invalid width, let the compiler do whatever
  133. d_out <= 8'hxx;
  134. spi_out_q <= {(io_max+1){1'bx}};
  135. end
  136. else
  137. begin
  138. d_out <= shift_in(d_out, {8{idle_io}},
  139. spi_width, lsb);
  140. // Note: 1-byte SPI uses IO1 as output (DO)
  141. (* full_case *)
  142. casez ( {spi_width, lsb} )
  143. 3'b00_0: spi_out_q[1] <= d_out[7];
  144. 3'b00_1: spi_out_q[1] <= d_out[0];
  145. 3'b01_0: spi_out_q[1:0] <= d_out[7:6];
  146. 3'b01_1: spi_out_q[1:0] <= d_out[1:0];
  147. 3'b10_0: spi_out_q[3:0] <= d_out[7:4];
  148. 3'b10_1: spi_out_q[3:0] <= d_out[3:0];
  149. 3'b11_?: spi_out_q <= d_out;
  150. endcase // casez ( {spi_width, lsb} )
  151. end
  152. end // if (spi_active)
  153. else
  154. begin
  155. spi_cs_q <= cs;
  156. d_out <= d;
  157. if (cs_delay != 0 &&
  158. (spi_cs_changed | ~|spi_ctr[ctr_max:1]))
  159. begin
  160. if (spi_cs_changed)
  161. spi_ctr[ctr_max:1] <= cs_delay;
  162. spi_oe_q <= 2'b10; // As for 1-bit mode
  163. end
  164. else if (req &&
  165. (cs_delay == 0 || ~|spi_ctr[ctr_max:1]))
  166. begin
  167. if (iowidth <= iowidth_max)
  168. begin
  169. spi_width <= iowidth;
  170. spi_ctr[ctr_max:1] <= 3'd8 >> iowidth;
  171. spi_oe_q <=
  172. |iowidth ? {2{dir}} : 2'b10;
  173. spi_active <= 1'b1;
  174. sack <= 1'b1;
  175. end
  176. else
  177. begin
  178. // Invalid width, let the compiler
  179. // do whatever it wants...
  180. spi_width <= 2'bxx;
  181. spi_ctr[ctr_max:1] <= {ctr_max{1'bx}};
  182. spi_oe_q <= 2'bxx;
  183. spi_active <= 1'bx;
  184. sack <= 1'bx;
  185. end
  186. end // if (req &&...
  187. end // else: !if(spi_active)
  188. end // else: !if(~spi_ctr[0])
  189. end // if (clk_en)
  190. end // else: !if(~rst_n)
  191. endmodule // spi_master