esp.sv 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318
  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] = adjusted memory address
  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. );
  56. reg [31:0] mem_addr = 'b0;
  57. wire [31:0] mem_addr_mask = (1'b1 << dram_bits) - 3'd4;
  58. wire [31:0] mem_addr_out = (mem_addr & mem_addr_mask)
  59. | dram_base;
  60. reg mem_valid;
  61. reg [31:0] mem_wdata;
  62. wire mem_write;
  63. reg [ 3:0] mem_wstrb;
  64. wire mem_ready;
  65. wire [31:0] mem_rdata;
  66. dram_port #(32) mem
  67. (
  68. .bus ( dram ),
  69. .prio ( 2'd2 ),
  70. .addr ( mem_addr[dram_bits-1:0] ),
  71. .valid ( mem_valid ),
  72. .wd ( mem_wdata ),
  73. .wstrb ( mem_wstrb ),
  74. .ready ( mem_ready ),
  75. .rd ( mem_rdata )
  76. );
  77. reg [1:0] spi_clk_q;
  78. reg spi_cs_n_q;
  79. reg [1:0] spi_io_q;
  80. always @(posedge sdram_clk)
  81. begin
  82. spi_clk_q <= { spi_clk_q[0], spi_clk };
  83. spi_cs_n_q <= spi_cs_n;
  84. spi_io_q <= spi_io;
  85. end
  86. typedef enum logic [1:0] {
  87. st_dead, // Do nothing until CS# deasserted
  88. st_cmd, // Reading command
  89. st_addr, // Reading address
  90. st_io // I/O (including read dummy bits)
  91. } state_t;
  92. state_t spi_state;
  93. reg [ 4:0] spi_cmd;
  94. reg [31:0] spi_shr;
  95. reg [ 3:0] spi_ctr;
  96. reg [ 3:0] cpu_irq;
  97. reg [ 3:0] cpu_set_irq; // CPU IRQs to set once idle
  98. reg cpu_send_irq; // Ready to set CPU IRQs
  99. reg [ 3:1] spi_irq;
  100. reg [ 3:1] latched_spi_irq; // SPI IRQ as of transition start
  101. reg [ 1:0] spi_out;
  102. reg spi_oe;
  103. reg [ 2:0] spi_wbe; // Partial word write byte enables
  104. reg [23:0] spi_wdata; // Partial word write data
  105. reg spi_wr_en; // SPI writes enabled by CPU
  106. reg spi_mem_en; // Read, or write enabled at start of trans
  107. assign spi_io = spi_oe ? spi_out : 2'bzz;
  108. assign mem_write = ~spi_cmd[4];
  109. wire [31:0] spi_indata = { spi_shr[29:0], spi_io_q };
  110. reg cpu_valid_q;
  111. always @(negedge rst_n or posedge sdram_clk)
  112. if (~rst_n)
  113. begin
  114. spi_state <= st_dead;
  115. spi_cmd <= 'b0;
  116. spi_ctr <= 4'd3; // 8 bits needed for this state
  117. cpu_irq <= 'b0;
  118. cpu_set_irq <= 'b0;
  119. cpu_send_irq <= 1'b0;
  120. spi_irq <= 'b0;
  121. latched_spi_irq <= 'b0;
  122. spi_oe <= 1'b0;
  123. spi_wbe <= 3'b0;
  124. mem_addr <= 'b0;
  125. mem_wstrb <= 4'b0;
  126. mem_valid <= 1'b0;
  127. spi_wr_en <= 1'b0;
  128. spi_mem_en <= 1'b0;
  129. end
  130. else
  131. begin
  132. esp_int <= ~|spi_irq;
  133. if (spi_cs_n_q)
  134. begin
  135. spi_state <= st_cmd;
  136. spi_ctr <= 4'd3;
  137. spi_oe <= 1'b0;
  138. cpu_send_irq <= |cpu_set_irq;
  139. end
  140. if (cpu_send_irq & ~mem_valid & ~|spi_wbe)
  141. begin
  142. cpu_irq <= cpu_irq | cpu_set_irq;
  143. cpu_set_irq <= 'b0;
  144. cpu_send_irq <= 1'b0;
  145. end
  146. if (~spi_cs_n_q && spi_clk_q == 2'b01)
  147. begin
  148. spi_ctr <= spi_ctr - 1'b1;
  149. spi_shr <= spi_indata;
  150. case (spi_ctr)
  151. 4'b1100:
  152. if (spi_state == st_io && mem_write)
  153. begin
  154. spi_wbe[0] <= 1'b1;
  155. spi_wdata[7:0] <= spi_indata[7:0];
  156. end
  157. 4'b1000:
  158. if (spi_state == st_io && mem_write)
  159. begin
  160. spi_wbe[1] <= 1'b1;
  161. spi_wdata[15:8] <= spi_indata[7:0];
  162. end
  163. 4'b0100:
  164. if (spi_state == st_io && mem_write)
  165. begin
  166. spi_wbe[2] <= 1'b1;
  167. spi_wdata[23:16] <= spi_indata[7:0];
  168. end
  169. 4'b0000: begin
  170. // Load spi_shr, but account for endianness here...
  171. if (spi_state == st_io)
  172. begin
  173. // Memory output
  174. spi_shr[31:24] <= mem_rdata[ 7: 0];
  175. spi_shr[23:16] <= mem_rdata[15: 8];
  176. spi_shr[15: 8] <= mem_rdata[23:16];
  177. spi_shr[ 7: 0] <= mem_rdata[31:24];
  178. end
  179. else
  180. begin
  181. // Status output
  182. spi_shr[31:28] <= { latched_spi_irq, spi_wr_en };
  183. spi_shr[27:24] <= cpu_irq;
  184. spi_shr[23:16] <= 8'b1001_0000;
  185. spi_shr[15: 8] <= mem_addr_out[23:16];
  186. spi_shr[ 7: 0] <= mem_addr_out[31:24];
  187. end // else: !if(spi_state == st_io)
  188. if (mem_valid && spi_state != st_cmd)
  189. begin
  190. cpu_irq[0] <= 1'b1; // Overrun/underrun
  191. spi_wr_en <= 1'b0; // Block further memory writes
  192. spi_mem_en <= ~mem_write;
  193. end
  194. case (spi_state)
  195. st_dead: begin
  196. // Do nothing
  197. end
  198. st_cmd: begin
  199. spi_cmd <= spi_indata[5:0];
  200. spi_state <= st_addr;
  201. latched_spi_irq <= spi_irq;
  202. spi_mem_en <= ~mem_write | spi_wr_en;
  203. for (int i = 1; i < 4; i++)
  204. begin
  205. if (spi_indata[3:2] == i)
  206. spi_irq[i] <= 1'b0;
  207. if (spi_indata[1:0] == i)
  208. cpu_set_irq[i] <= 1'b1;
  209. end
  210. end
  211. st_addr: begin
  212. mem_addr <= spi_indata & mem_addr_mask;
  213. spi_state <= st_io;
  214. mem_valid <= ~mem_write;
  215. mem_wstrb <= 4'b0;
  216. spi_wbe <= 3'b000;
  217. // If the first word is partial, skip ahead
  218. if (mem_write)
  219. spi_ctr[3:2] <= ~spi_indata[1:0];
  220. end
  221. st_io: begin
  222. if (mem_write)
  223. begin
  224. mem_wdata[23: 0] <= spi_wdata[23:0];
  225. mem_wdata[31:24] <= spi_indata[7:0];
  226. mem_wstrb <= { 1'b1, spi_wbe };
  227. end
  228. else
  229. begin
  230. mem_wstrb <= 4'b0000;
  231. end // else: !if(mem_write)
  232. mem_valid <= spi_mem_en;
  233. spi_wbe <= 3'b000;
  234. end
  235. endcase
  236. end // case: 4'b0000
  237. default: begin
  238. // Do nothing
  239. end
  240. endcase // case (spi_ctr)
  241. end // if (spi_clk_q == 2'b01)
  242. else if (~spi_cs_n_q && spi_clk_q == 2'b10)
  243. begin
  244. spi_out <= spi_shr[31:30];
  245. spi_oe <= (spi_state == st_io) & ~mem_write;
  246. end
  247. if (mem_valid & mem_ready)
  248. begin
  249. mem_addr <= mem_addr + 3'd4;
  250. mem_valid <= 1'b0;
  251. end
  252. if (spi_state != st_io & ~mem_valid & |spi_wbe)
  253. begin
  254. // Complete a partial write terminated by CS#
  255. mem_valid <= spi_mem_en;
  256. mem_wstrb <= { 1'b0, spi_wbe };
  257. mem_wdata[23:0] <= spi_wdata[23:0];
  258. mem_wdata[31:24] <= 8'hxx;
  259. spi_wbe <= 3'b000;
  260. end
  261. cpu_valid_q <= cpu_valid;
  262. if (cpu_valid & ~cpu_valid_q & cpu_wstrb[0])
  263. case (cpu_addr[1:0])
  264. 2'b00:
  265. cpu_irq <= cpu_wdata[3:0];
  266. 2'b01:
  267. for (int i = 0; i < 4; i++)
  268. if (cpu_wdata[i])
  269. cpu_irq[i] <= cpu_send_irq & cpu_set_irq[i];
  270. 2'b10:
  271. { spi_irq, spi_wr_en } <= cpu_wdata[3:0];
  272. 2'b11: begin
  273. if (cpu_wdata[0])
  274. spi_wr_en <= 1'b1;
  275. for (int i = 1; i < 4; i++)
  276. if (cpu_wdata[i])
  277. spi_irq[i] <= 1'b1;
  278. end
  279. endcase // case (cpu_addr[1:0])
  280. end // else: !if(~rst_n)
  281. always @(posedge sys_clk)
  282. irq <= |cpu_irq;
  283. always @(*)
  284. casez (cpu_addr[1:0])
  285. 2'b0?:
  286. cpu_rdata = { 28'b0, cpu_irq };
  287. 2'b1?:
  288. cpu_rdata = { 28'b0, spi_irq, spi_wr_en };
  289. endcase // casez (cpu_addr[1:0])
  290. endmodule // esp