2
0

esp.sv 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320
  1. //
  2. // Communication interface with ESP32-S2
  3. //
  4. // This is a DIO (2-bit, including command) SPI slave interface which
  5. // allows direct access to content in SDRAM. Additionally, each
  6. // direction has three interrupt flags (3-1); the FPGA CPU additionally
  7. // has a fourth interrupt condition (0) which indicates DRAM timing
  8. // overrun/underrun.
  9. //
  10. // The SPI command byte is:
  11. // Bit [7:5] - reserved, must be 0
  12. // Bit 4 - read/write#
  13. // Bit [3:2] - clear upstream (FPGA->ESP) interrupt flag if nonzero
  14. // Bit [1:0] - set downstream (ESP->FPGA) interrupt flag if nonzero
  15. //
  16. // CPU downstream interrupts are set after the transaction completes
  17. // (CS# goes high.)
  18. //
  19. // A 32-bit address follows; for a read, the following 16 cycles
  20. // contains dummy/status data:
  21. //
  22. // Bit [31:16] = 16 LSB of 32 kHz RTC counter
  23. // Bit [15:12] = 4'b1001
  24. // Bit [11: 8] = 0 reserved
  25. // Bit [ 7: 5] = upstream interrupts pending
  26. // Bit 4 = downstream writes enabled
  27. // Bit [ 3: 1] = downstream interrupts pending
  28. // Bit 0 = underrun error
  29. //
  30. // The following CPU registers are defined:
  31. //
  32. // 0 = status bits [3:0] (downstream)
  33. // 1 = write-1-clear of status bits [3:0]
  34. // 2 = status bits [7:4] (upstream)
  35. // 3 = write-1-set of status bits [7:4]
  36. //
  37. module esp #(
  38. parameter dram_bits = 25,
  39. parameter [31:0] dram_base = 32'h40000000
  40. ) (
  41. input rst_n,
  42. input sys_clk,
  43. input sdram_clk,
  44. input cpu_valid,
  45. input [4:0] cpu_addr,
  46. input [3:0] cpu_wstrb,
  47. input [31:0] cpu_wdata,
  48. output [31:0] cpu_rdata,
  49. output reg irq,
  50. dram_bus.dstr dram,
  51. output reg esp_int,
  52. input spi_clk,
  53. inout [1:0] spi_io,
  54. input spi_cs_n,
  55. input [15:0] rtc_ctr
  56. );
  57. reg [31:0] mem_addr = 'b0;
  58. wire [31:0] mem_addr_mask = (1'b1 << dram_bits) - 3'd4;
  59. wire [31:0] mem_addr_out = (mem_addr & mem_addr_mask)
  60. | dram_base;
  61. reg mem_valid;
  62. reg [31:0] mem_wdata;
  63. wire mem_write;
  64. reg [ 3:0] mem_wstrb;
  65. wire mem_ready;
  66. wire [31:0] mem_rdata;
  67. dram_port #(32) mem
  68. (
  69. .bus ( dram ),
  70. .prio ( 2'd2 ),
  71. .addr ( mem_addr[dram_bits-1:0] ),
  72. .valid ( mem_valid ),
  73. .wd ( mem_wdata ),
  74. .wstrb ( mem_wstrb ),
  75. .ready ( mem_ready ),
  76. .rd ( mem_rdata )
  77. );
  78. reg [1:0] spi_clk_q;
  79. reg spi_cs_n_q;
  80. reg [1:0] spi_io_q;
  81. always @(posedge sdram_clk)
  82. begin
  83. spi_clk_q <= { spi_clk_q[0], spi_clk };
  84. spi_cs_n_q <= spi_cs_n;
  85. spi_io_q <= spi_io;
  86. end
  87. typedef enum logic [1:0] {
  88. st_dead, // Do nothing until CS# deasserted
  89. st_cmd, // Reading command
  90. st_addr, // Reading address
  91. st_io // I/O (including read dummy bits)
  92. } state_t;
  93. state_t spi_state;
  94. reg [ 4:0] spi_cmd;
  95. reg [31:0] spi_shr;
  96. reg [ 3:0] spi_ctr;
  97. reg [ 3:0] cpu_irq;
  98. reg [ 3:0] cpu_set_irq; // CPU IRQs to set once idle
  99. reg cpu_send_irq; // Ready to set CPU IRQs
  100. reg [ 3:1] spi_irq;
  101. reg [ 3:1] latched_spi_irq; // SPI IRQ as of transition start
  102. reg [ 1:0] spi_out;
  103. reg spi_oe;
  104. reg [ 2:0] spi_wbe; // Partial word write byte enables
  105. reg [23:0] spi_wdata; // Partial word write data
  106. reg spi_wr_en; // SPI writes enabled by CPU
  107. reg spi_mem_en; // Read, or write enabled at start of trans
  108. assign spi_io = spi_oe ? spi_out : 2'bzz;
  109. assign mem_write = ~spi_cmd[4];
  110. wire [31:0] spi_indata = { spi_shr[29:0], spi_io_q };
  111. reg cpu_valid_q;
  112. always @(negedge rst_n or posedge sdram_clk)
  113. if (~rst_n)
  114. begin
  115. spi_state <= st_dead;
  116. spi_cmd <= 'b0;
  117. spi_ctr <= 4'd3; // 8 bits needed for this state
  118. cpu_irq <= 'b0;
  119. cpu_set_irq <= 'b0;
  120. cpu_send_irq <= 1'b0;
  121. spi_irq <= 'b0;
  122. latched_spi_irq <= 'b0;
  123. spi_oe <= 1'b0;
  124. spi_wbe <= 3'b0;
  125. mem_addr <= 'b0;
  126. mem_wstrb <= 4'b0;
  127. mem_valid <= 1'b0;
  128. spi_wr_en <= 1'b0;
  129. spi_mem_en <= 1'b0;
  130. end
  131. else
  132. begin
  133. esp_int <= ~|spi_irq;
  134. if (spi_cs_n_q)
  135. begin
  136. spi_state <= st_cmd;
  137. spi_ctr <= 4'd3;
  138. spi_oe <= 1'b0;
  139. cpu_send_irq <= |cpu_set_irq;
  140. end
  141. if (cpu_send_irq & ~mem_valid & ~|spi_wbe)
  142. begin
  143. cpu_irq <= cpu_irq | cpu_set_irq;
  144. cpu_set_irq <= 'b0;
  145. cpu_send_irq <= 1'b0;
  146. end
  147. if (~spi_cs_n_q && spi_clk_q == 2'b01)
  148. begin
  149. spi_ctr <= spi_ctr - 1'b1;
  150. spi_shr <= spi_indata;
  151. case (spi_ctr)
  152. 4'b1100:
  153. if (spi_state == st_io && mem_write)
  154. begin
  155. spi_wbe[0] <= 1'b1;
  156. spi_wdata[7:0] <= spi_indata[7:0];
  157. end
  158. 4'b1000:
  159. if (spi_state == st_io && mem_write)
  160. begin
  161. spi_wbe[1] <= 1'b1;
  162. spi_wdata[15:8] <= spi_indata[7:0];
  163. end
  164. 4'b0100:
  165. if (spi_state == st_io && mem_write)
  166. begin
  167. spi_wbe[2] <= 1'b1;
  168. spi_wdata[23:16] <= spi_indata[7:0];
  169. end
  170. 4'b0000: begin
  171. // Load spi_shr, but account for endianness here...
  172. if (spi_state == st_io)
  173. begin
  174. // Memory output
  175. spi_shr[31:24] <= mem_rdata[ 7: 0];
  176. spi_shr[23:16] <= mem_rdata[15: 8];
  177. spi_shr[15: 8] <= mem_rdata[23:16];
  178. spi_shr[ 7: 0] <= mem_rdata[31:24];
  179. end
  180. else
  181. begin
  182. // Status output
  183. spi_shr[31:28] <= { latched_spi_irq, spi_wr_en };
  184. spi_shr[27:24] <= cpu_irq;
  185. spi_shr[23:16] <= 8'b1001_0000;
  186. spi_shr[15: 8] <= rtc_ctr[ 7: 0];
  187. spi_shr[ 7: 0] <= rtc_ctr[15: 8];
  188. end // else: !if(spi_state == st_io)
  189. if (mem_valid && spi_state != st_cmd)
  190. begin
  191. cpu_irq[0] <= 1'b1; // Overrun/underrun
  192. spi_wr_en <= 1'b0; // Block further memory writes
  193. spi_mem_en <= ~mem_write;
  194. end
  195. case (spi_state)
  196. st_dead: begin
  197. // Do nothing
  198. end
  199. st_cmd: begin
  200. spi_cmd <= spi_indata[5:0];
  201. spi_state <= st_addr;
  202. latched_spi_irq <= spi_irq;
  203. spi_mem_en <= ~mem_write | spi_wr_en;
  204. for (int i = 1; i < 4; i++)
  205. begin
  206. if (spi_indata[3:2] == i)
  207. spi_irq[i] <= 1'b0;
  208. if (spi_indata[1:0] == i)
  209. cpu_set_irq[i] <= 1'b1;
  210. end
  211. end
  212. st_addr: begin
  213. mem_addr <= spi_indata & mem_addr_mask;
  214. spi_state <= st_io;
  215. mem_valid <= ~mem_write;
  216. mem_wstrb <= 4'b0;
  217. spi_wbe <= 3'b000;
  218. // If the first word is partial, skip ahead
  219. if (mem_write)
  220. spi_ctr[3:2] <= ~spi_indata[1:0];
  221. end
  222. st_io: begin
  223. if (mem_write)
  224. begin
  225. mem_wdata[23: 0] <= spi_wdata[23:0];
  226. mem_wdata[31:24] <= spi_indata[7:0];
  227. mem_wstrb <= { 1'b1, spi_wbe };
  228. end
  229. else
  230. begin
  231. mem_wstrb <= 4'b0000;
  232. end // else: !if(mem_write)
  233. mem_valid <= spi_mem_en;
  234. spi_wbe <= 3'b000;
  235. end
  236. endcase
  237. end // case: 4'b0000
  238. default: begin
  239. // Do nothing
  240. end
  241. endcase // case (spi_ctr)
  242. end // if (spi_clk_q == 2'b01)
  243. else if (~spi_cs_n_q && spi_clk_q == 2'b10)
  244. begin
  245. spi_out <= spi_shr[31:30];
  246. spi_oe <= (spi_state == st_io) & ~mem_write;
  247. end
  248. if (mem_valid & mem_ready)
  249. begin
  250. mem_addr <= mem_addr + 3'd4;
  251. mem_valid <= 1'b0;
  252. end
  253. if (spi_state != st_io & ~mem_valid & |spi_wbe)
  254. begin
  255. // Complete a partial write terminated by CS#
  256. mem_valid <= spi_mem_en;
  257. mem_wstrb <= { 1'b0, spi_wbe };
  258. mem_wdata[23:0] <= spi_wdata[23:0];
  259. mem_wdata[31:24] <= 8'hxx;
  260. spi_wbe <= 3'b000;
  261. end
  262. cpu_valid_q <= cpu_valid;
  263. if (cpu_valid & ~cpu_valid_q & cpu_wstrb[0])
  264. case (cpu_addr[1:0])
  265. 2'b00:
  266. cpu_irq <= cpu_wdata[3:0];
  267. 2'b01:
  268. for (int i = 0; i < 4; i++)
  269. if (cpu_wdata[i])
  270. cpu_irq[i] <= cpu_send_irq & cpu_set_irq[i];
  271. 2'b10:
  272. { spi_irq, spi_wr_en } <= cpu_wdata[3:0];
  273. 2'b11: begin
  274. if (cpu_wdata[0])
  275. spi_wr_en <= 1'b1;
  276. for (int i = 1; i < 4; i++)
  277. if (cpu_wdata[i])
  278. spi_irq[i] <= 1'b1;
  279. end
  280. endcase // case (cpu_addr[1:0])
  281. end // else: !if(~rst_n)
  282. always @(posedge sys_clk)
  283. irq <= |cpu_irq;
  284. always @(*)
  285. casez (cpu_addr[1:0])
  286. 2'b0?:
  287. cpu_rdata = { 28'b0, cpu_irq };
  288. 2'b1?:
  289. cpu_rdata = { 28'b0, spi_irq, spi_wr_en };
  290. endcase // casez (cpu_addr[1:0])
  291. endmodule // esp