spi_master.sv 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  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. module spi_master
  20. #(
  21. parameter width = 1, // Max width of SPI bus (1, 2, 4, or 8)
  22. parameter n_cs = 1, // Number of CS# outputs
  23. parameter cs_delay = 1, // Time from CS# low to first data
  24. parameter latch_q = 1 // Latch output
  25. )
  26. (
  27. input rst_n, // Unit reset
  28. input clk, // System clock
  29. input clk_en, // SPI clock enable
  30. input [7:0] d, // System data in
  31. output [7:0] q, // System data out
  32. input req, // Session request
  33. input dir, // Session is write (for multibit)
  34. input [1:0] iowidth, // Session width (lg2)
  35. output sack, // Session started
  36. output eack, // Session ended
  37. input idle_io, // Signal level for I/Os at idle
  38. input cpol, // Clock polarity (usually constant)
  39. input lsb, // Littleendian mode (usually constant)
  40. input [n_cs-1:0] cs, // Device select (active high)
  41. output spi_sck, // SPI clock
  42. inout [`IO_MAX:0] spi_io, // SPI data
  43. output [n_cs-1:0] spi_cs_n // SPI CS# lines
  44. );
  45. localparam io_max = `IO_MAX;
  46. localparam iowidth_max = ilog2c(width);
  47. localparam ctr_max = max(ilog2c(cs_delay)-1,2);
  48. reg spi_sck_q;
  49. reg spi_active;
  50. reg [((width > 2) ? 1 : 0):0] spi_width;
  51. reg [ctr_max:0] spi_ctr;
  52. reg [n_cs-1:0] spi_cs_q;
  53. reg [io_max:0] spi_out_q;
  54. reg [1:0] spi_oe_q;
  55. reg d_dir;
  56. reg [7:0] d_out; // Output shift register
  57. reg [7:0] d_in; // Input shift register
  58. reg [7:0] q_q; // Latched output data
  59. reg sack_q;
  60. reg eack_q;
  61. assign spi_cs_n = ~spi_cs_q;
  62. assign spi_sck = spi_sck_q;
  63. assign q = latch_q ? q_q : d_in;
  64. wire spi_cs_changed = |(spi_cs_q ^ cs);
  65. // Always 2'b10 for single-bit SPI
  66. wire [1:0] spi_oe = (width > 1) ? spi_oe_q : 2'b10;
  67. assign spi_io[0] = spi_oe[0] ? spi_out_q[0] : 1'bz;
  68. assign spi_io[io_max:1] = spi_oe[1] ? spi_out_q[io_max:1] : {io_max{1'bz}};
  69. // Make it clear to the compiler that iowidth is undefined if
  70. // it is too large
  71. wire [1:0] iowidth_in = (iowidth > iowidth_max) ? 2'bxx : iowidth;
  72. // Just for convenience...
  73. wire [3:0] spi_width_n = 1'b1 << spi_width;
  74. wire [io_max:0] idle_allio = {(io_max+1){idle_io}};
  75. always @(negedge rst_n or posedge clk)
  76. if (~rst_n)
  77. begin
  78. spi_clk <= 1'b0;
  79. spi_active <= 1'b0;
  80. spi_width <= 4'b0001;
  81. spi_ctr <= 1'b0;
  82. spi_cs_q <= 1'b0;
  83. spi_out_q <= idle_allio;
  84. spi_oe_q <= 2'b10;
  85. sack_q <= 1'b0;
  86. eack_q <= 1'b0;
  87. d_out <= idle_allio;
  88. d_in <= 8'hxx;
  89. q_q <= 8'hxx;
  90. end
  91. else
  92. begin
  93. // These are always single system clock pulses
  94. sack_q <= 1'b0;
  95. eack_q <= 1'b0;
  96. if (clk_en)
  97. begin
  98. spi_ctr <= spi_ctr - 1'b1;
  99. spi_clk <= (spi_ctr[0] & spi_active) ^ cpol;
  100. if (~spi_ctr[0])
  101. if (lsb)
  102. d_in <= ( spi_io[spi_width_n-1:0] << (8-spi_width_n)) |
  103. (d_in >> spi_width_n);
  104. else
  105. d_in <= (d_in << spi_width_n) | spi_io[spi_width_n-1:0];
  106. else
  107. begin
  108. spi_out_q <= idle_allio;
  109. if (spi_active)
  110. begin
  111. if (~|spi_ctr[ctr_max:1])
  112. begin
  113. eack_q <= 1'b1;
  114. q_q <= d_in;
  115. spi_active <= 1'b0;
  116. end
  117. if (spi_width > iowidth_max)
  118. begin
  119. // Invalid width, let the compiler do whatever
  120. d_out <= 8'hxx;
  121. spi_out_q <= {(io_max+1){1'bx}};
  122. end
  123. else
  124. begin
  125. if (lsb)
  126. d_out <= {spi_width_n{io_idle}} |
  127. (d_out >> spi_width_n);
  128. else
  129. d_out <= (d_out << spi_width_n) |
  130. {spi_width_n{io_idle}};
  131. // 1-byte SPI uses IO1 as output (DO)
  132. if (spi_width == 2'd0)
  133. spi_out_q[1] <= d_out[lsb ? 0 : 7];
  134. else
  135. spi_out_q[spi_width_n-1:0]
  136. <= d_out >> (lsb ? 0 : 8-spi_width_n);
  137. end
  138. end // if (spi_active)
  139. else
  140. begin
  141. spi_cs_q <= cs;
  142. d_out <= d;
  143. if (cs_delay != 0 &&
  144. (spi_cs_changed | ~|spi_ctr[ctr_max:1]))
  145. begin
  146. if (spi_cs_changed)
  147. spi_ctr[ctr_max:1] <= cs_delay;
  148. spi_oe_q <= 2'b10; // As for 1-bit mode
  149. end
  150. else if (req &&
  151. (cs_delay == 0 || ~|spi_ctr[ctr_max:1]))
  152. begin
  153. if (iowidth <= iowidth_max)
  154. begin
  155. spi_width <= iowidth;
  156. spi_ctr[ctr_max:1] <= 3'd8 >> iowidth;
  157. spi_oe_q <=
  158. |iowidth ? {2{dir}} : 2'b10;
  159. spi_active <= 1'b1;
  160. sack_q <= 1'b1;
  161. end
  162. else
  163. begin
  164. // Invalid width, let the compiler
  165. // do whatever it wants...
  166. spi_width <= 2'bxx;
  167. spi_ctr[ctr_max:1] <= {ctr_max{1'bx}};
  168. spi_oe_q <= 2'bxx;
  169. spi_active <= 1'bx;
  170. sack_q <= 1'bx;
  171. end
  172. end // if (req &&...
  173. end // else: !if(spi_active)
  174. end // else: !if(~spi_ctr[0])
  175. end // if (clk_en)
  176. end // else: !if(~rst_n)
  177. endmodule // spi_master