Debian Codegraph @

examples/emcu-with-apb-spi.v

raw
1 `default_nettype none
2
3 /*
4 * The apprate SPI is implemented with the following limitations:
5 * - master only,
6 * - SPI3 mode,
7 * - write only,
8 * - LSB/MSB format switching is performed by a bit in the control/status register,
9 * - the status register contains one bit - BUSY.
10 * - speed is fixed = 0.5 clk (~13MHz)
11 *
12 * Attention: flashing is performed with firmware as follows:
13 * openFPGALoader -f -b tangnano4k --mcufw=emcu-firmware/apb-spi.bin emcu-apb-spi-tangnano4k.fs
14 *
15 */
16
17 module top (
18 input wire clk_i,
19 output wire mode_led,
20 input wire RXD,
21 output wire TXD,
22 input wire rst_i,
23 // gpio
24 inout wire [1:0] gpio,
25 // SPI
26 output wire spi_OUT,
27 output reg spi_SCL,
28 output [5:0] led
29 );
30
31 // dummy sinks
32 wire dummy_uart1_txd;
33 wire dummy_uart0_baudtick;
34 wire dummy_uart1_baudtick;
35 wire dummy_intmonitor;
36 wire dummy_targexp0_hsel;
37 wire [31:0] dummy_targexp0_haddr;
38 wire [1:0] dummy_targexp0_htrans;
39 wire dummy_targexp0_hwrite;
40 wire [2:0] dummy_targexp0_hsize;
41 wire [2:0] dummy_targexp0_hburst;
42 wire [3:0] dummy_targexp0_hprot;
43 wire [1:0] dummy_targexp0_memattr;
44 wire dummy_targexp0_exreq;
45 wire [3:0] dummy_targexp0_hmaster;
46 wire [31:0] dummy_targexp0_hwdata;
47 wire dummy_targexp0_hmastlock;
48 wire dummy_targexp0_hauser;
49 wire [3:0] dummy_targexp0_hwuser;
50 wire [31:0] dummy_initexp0_hrdata;
51 wire dummy_initexp0_hready;
52 wire dummy_initexp0_hresp;
53 wire dummy_initexp0_exresp;
54 wire [2:0] dummy_initexp0_hruser;
55 wire dummy_daptdo;
56 wire dummy_dapjtagnsw;
57 wire [3:0] dummy_tpiutracedata;
58 wire dummy_targexp0_hreadymux;
59 wire dummy_dapntdoen;
60
61 // Periphery bus
62 wire apbtargexp2_psel;
63 wire [11:0] apbtargexp2_paddr;
64 wire apbtargexp2_pready;
65 wire apbtargexp2_penable;
66 wire apbtargexp2_pwrite;
67 wire [31:0] apbtargexp2_pwdata;
68 wire [31:0] apbtargexp2_prdata;
69 wire [3:0] apbtargexp2_pstrb;
70 wire [2:0] apbtargexp2_pprot;
71
72 // ROM 32k
73 wire [12:0] rom_addr;
74 wire [15:0] dummy_rom_addr; // flash is addressed by 4 bytes each, and there are also unused high bits of the address
75 wire targflash0_hsel;
76 wire [1:0] targflash0_htrans;
77 wire [2:0] dummy_targflash0_hsize;
78 wire [2:0] dummy_targflash0_hburst;
79 wire dummy_targflash0_hreadymux;
80 wire [31:0] rom_out;
81 wire targflash0_readyout;
82
83 // SRAM 8k
84 wire [10:0] sram0_addr;
85 wire [1:0] dummy_sram0_addr; // high address bits
86 wire sram0_cs;
87 wire [3:0] sram0_wren; // byte write mask
88 wire [31:0] sram0_wdata;
89 wire [31:0] sram0_rdata; // all 32bit
90 wire [23:0] dummy_sram0_rdata_0; // for unused bits
91 wire [23:0] dummy_sram0_rdata_1;
92 wire [23:0] dummy_sram0_rdata_2;
93 wire [23:0] dummy_sram0_rdata_3;
94
95 wire mtx_hreset_n;
96
97 // The processor reset inputs are not explicitly brought out - Global Set Reset is used.
98 GSR gsr (
99 .GSRI(rst_i)
100 );
101
102 wire GND = 1'b0;
103 wire VCC = 1'b1;
104
105 wire dummy_lock;
106 wire clk;
107 PLLVR #(
108 .FCLKIN("27.0"),
109 .IDIV_SEL(4), // -> PFD = 5.4 MHz (range: 3-400 MHz)
110 .FBDIV_SEL(12), // -> CLKOUT = 70.2 MHz (range: 400-600 MHz)
111 .ODIV_SEL(8) // -> VCO = 561.6 MHz (range: 600-1200 MHz)
112 )
113 pll (.CLKOUTP(), .CLKOUTD(), .CLKOUTD3(), .RESET(1'b0), .RESET_P(1'b0), .CLKFB(1'b0), .FBDSEL(6'b0), .IDSEL(6'b0), .ODSEL(6'b0), .PSDA(4'b0), .DUTYDA(4'b0), .FDLY(4'b0), .VREN(1'b1),
114 .CLKIN(clk_i), // 27.0 MHz
115 .CLKOUT(clk), // 70.2 MHz
116 .LOCK(dummy_lock)
117 );
118
119 // dummy GPIO inout ports, unused
120 wire [15:0] gpio_out;
121 wire [15:0] gpio_oen;
122 wire [15:0] gpio_in;
123
124 // GPIO inout ports, 16 total but we need only two
125 wire [15:0] gpio_out;
126 wire [15:0] gpio_oen; // active low
127 wire [15:0] gpio_in;
128
129 IOBUF gpio0(
130 .O(gpio_in[0]),
131 .IO(gpio[0]),
132 .I(gpio_out[0]),
133 .OEN(~gpio_oen[0])
134 );
135
136 IOBUF gpio1(
137 .O(gpio_in[1]),
138 .IO(gpio[1]),
139 .I(gpio_out[1]),
140 .OEN(~gpio_oen[1])
141 );
142
143
144 EMCU cpu (
145 .FCLK(clk),
146 .PORESETN(GND), // doesn't matter
147 .SYSRESETN(GND), // doesn't matter
148 .RTCSRCCLK(GND), // this is normal port but we haven't RTC in this example
149 .MTXHRESETN(mtx_hreset_n),
150
151 .IOEXPOUTPUTO(gpio_out),
152 .IOEXPOUTPUTENO(gpio_oen),
153 .IOEXPINPUTI(gpio_in),
154
155 .UART0RXDI(RXD),
156 .UART1RXDI(GND),
157 .UART0TXDO(TXD),
158 .UART1TXDO(dummy_uart1_txd),
159 .UART0BAUDTICK(dummy_uart0_baudtick),
160 .UART1BAUDTICK(dummy_uart1_baudtick),
161
162 .INTMONITOR(dummy_intmonitor),
163
164 .SRAM0ADDR({dummy_sram0_addr, sram0_addr}),
165 .SRAM0CS(sram0_cs),
166 .SRAM0WREN(sram0_wren),
167 .SRAM0WDATA(sram0_wdata),
168 .SRAM0RDATA(sram0_rdata),
169
170 .TARGFLASH0HSEL(targflash0_hsel),
171 .TARGFLASH0HADDR({dummy_rom_addr[15:2], rom_addr, dummy_rom_addr[1:0]}),
172 .TARGFLASH0HTRANS(targflash0_htrans),
173 .TARGFLASH0HSIZE(dummy_targflash0_hsize),
174 .TARGFLASH0HBURST(dummy_targflash0_hburst),
175 .TARGFLASH0HREADYMUX(dummy_targflash0_hreadymux),
176 .TARGFLASH0HRDATA(rom_out),
177 .TARGFLASH0HRUSER({GND,GND,GND}),
178 .TARGFLASH0HRESP(GND),
179 .TARGFLASH0EXRESP(GND),
180 .TARGFLASH0HREADYOUT(targflash0_readyout),
181
182 .TARGEXP0HSEL(dummy_targexp0_hsel),
183 .TARGEXP0HADDR(dummy_targexp0_haddr),
184 .TARGEXP0HTRANS(dummy_targexp0_htrans),
185 .TARGEXP0HWRITE(dummy_targexp0_hwrite),
186 .TARGEXP0HSIZE(dummy_targexp0_hsize),
187 .TARGEXP0HBURST(dummy_targexp0_hburst[2:0]),
188 .TARGEXP0HPROT(dummy_targexp0_hprot[3:0]),
189 .TARGEXP0MEMATTR(dummy_targexp0_memattr),
190 .TARGEXP0EXREQ(dummy_targexp0_exreq),
191 .TARGEXP0HMASTER(dummy_targexp0_hmaster),
192 .TARGEXP0HWDATA(dummy_targexp0_hwdata),
193 .TARGEXP0HMASTLOCK(dummy_targexp0_hmastlock),
194 .TARGEXP0HREADYMUX(dummy_targexp0_hreadymux),
195 .TARGEXP0HAUSER(dummy_targexp0_hauser),
196 .TARGEXP0HWUSER(dummy_targexp0_hwuser),
197 .INITEXP0HRDATA(dummy_initexp0_hrdata),
198 .INITEXP0HREADY(dummy_initexp0_hready),
199 .INITEXP0HRESP(dummy_initexp0_hresp),
200 .INITEXP0EXRESP(dummy_initexp0_exresp),
201 .INITEXP0HRUSER(dummy_initexp0_hruser),
202 .DAPTDO(dummy_daptdo),
203 .DAPJTAGNSW(dummy_dapjtagnsw),
204 .DAPNTDOEN(dummy_dapntdoen),
205 .TPIUTRACEDATA(dummy_tpiutracedata),
206 .TARGEXP0HRDATA({32{GND}}),
207 .TARGEXP0HREADYOUT(GND),
208 .TARGEXP0HRESP(VCC), // XXX
209 .TARGEXP0EXRESP(GND),
210 .TARGEXP0HRUSER({GND,GND,GND}),
211 .INITEXP0HSEL(GND),
212 .INITEXP0HADDR({32{GND}}),
213 .INITEXP0HTRANS({GND,GND}),
214 .INITEXP0HWRITE(GND),
215 .INITEXP0HSIZE({GND,GND,GND}),
216 .INITEXP0HBURST({GND,GND,GND}),
217 .INITEXP0HPROT({GND,GND,GND,GND}),
218 .INITEXP0MEMATTR({GND,GND}),
219 .INITEXP0EXREQ(GND),
220 .INITEXP0HMASTER({GND,GND,GND,GND}),
221 .INITEXP0HWDATA({32{GND}}),
222 .INITEXP0HMASTLOCK(GND),
223 .INITEXP0HAUSER(GND),
224 .INITEXP0HWUSER({GND,GND,GND,GND}),
225
226 .APBTARGEXP2PSEL(apbtargexp2_psel),
227 .APBTARGEXP2PADDR(apbtargexp2_paddr),
228 .APBTARGEXP2PSTRB(apbtargexp2_pstrb),
229 .APBTARGEXP2PPROT(apbtargexp2_pprot),
230 .APBTARGEXP2PENABLE(apbtargexp2_penable),
231 .APBTARGEXP2PWRITE(apbtargexp2_pwrite),
232 .APBTARGEXP2PWDATA(apbtargexp2_pwdata),
233
234 .APBTARGEXP2PRDATA(apbtargexp2_prdata),
235 .APBTARGEXP2PREADY(apbtargexp2_pready),
236 .APBTARGEXP2PSLVERR(GND),
237
238 .DAPSWDITMS(GND),
239 .FLASHERR(GND),
240 .FLASHINT(GND)
241 );
242
243 // ROM
244 // To understand what the entrances and outputs of this primitive, I followed the description of the AMBA protocol.
245 // https://developer.arm.com/documentation/ihi0011/a/ or search for IHI0011a.pdf
246 // It was from there and by the names of the ports of the primitive that it
247 // became clear that RAM is connected directly without tricks, but for Flash
248 // it would be necessary to implement a whole client of this AHB Bus.
249 localparam ROM_IDLE = 2'b00;
250 localparam ROM_READ = 2'b01;
251 localparam ROM_OKEY = 2'b10;
252 reg [1:0] rom_state;
253 reg rom_sel;
254 reg [12:0] flash_in_addr;
255
256 // AHB slave
257 always @(posedge clk) begin
258 if (!(rst_i & mtx_hreset_n)) begin
259 rom_state <= ROM_IDLE;
260 rom_sel <= 1'b0;
261 end else begin
262 case (rom_state)
263 ROM_IDLE: begin
264 if (targflash0_hsel & targflash0_htrans[1]) begin // NONSEQ/SEQ transfer
265 rom_state <= ROM_READ;
266 flash_in_addr <= rom_addr;
267 rom_sel <= 1'b1;
268 end
269 end
270 ROM_READ: begin
271 if (targflash0_hsel & targflash0_htrans[1]) begin // NONSEQ/SEQ transfer
272 rom_state <= ROM_OKEY;
273 rom_sel <= 1'b0;
274 end else begin
275 rom_state <= ROM_IDLE;
276 rom_sel <= 1'b0;
277 end
278 end
279 ROM_OKEY: begin
280 if (targflash0_hsel & targflash0_htrans[1]) begin // NONSEQ/SEQ transfer
281 rom_state <= ROM_IDLE;
282 rom_sel <= 1'b0;
283 end
284 end
285 endcase
286 end
287 end
288 assign targflash0_readyout = rom_state == ROM_IDLE;
289
290 // very simple APB bridge: decode only second APB address 0x4000250
291 wire psel2;
292 assign psel2 = apbtargexp2_psel && (apbtargexp2_paddr[11:8] == 4'h5); // 0x40002 >5< 00
293
294
295 // SPI device
296 wire spi_status;
297 reg spi_msb;
298 reg spi_start;
299 reg [7:0] spi_WDATA;
300 reg [9:0] spi_tmp_wdata;
301 assign spi_OUT = spi_msb ? spi_tmp_wdata[9] : spi_tmp_wdata[0];
302 reg spi_clk;
303 assign spi_SCL = spi_clk;
304
305 assign led[0] = spi_start;
306 assign led[1] = spi_status;
307 assign led[2] = psel2;
308
309 // spi clock
310 reg [4:0] spi_clk_cnt;
311 always @(posedge clk or negedge rst_i) begin
312 spi_clk <= 1'b1;
313 spi_status <= 1'b0;
314 if (!rst_i) begin
315 spi_clk <= 1'b1;
316 spi_clk_cnt <= 5'd0;
317 end else begin
318 if (spi_start) begin
319 spi_clk_cnt <= 5'd16;
320 spi_status <= 1'b1;
321 end else begin
322 if (spi_clk_cnt != 5'd0) begin
323 spi_status <= 1'b1;
324 spi_clk_cnt <= spi_clk_cnt - 1'b1;
325 spi_clk <= ~spi_clk;
326 end
327 end
328 end
329 end
330
331 // spi data
332 always @(posedge clk or negedge rst_i) begin
333 if (!rst_i) begin
334 spi_tmp_wdata <= 8'b0;
335 end else begin
336 if (spi_start) begin
337 spi_tmp_wdata[8:1] <= spi_WDATA;
338 end else begin
339 if (spi_status && (spi_clk)) begin
340 spi_tmp_wdata <= spi_msb ? {spi_tmp_wdata[8:0], 1'b0} : {1'b0, spi_tmp_wdata[9:1]};
341 end
342 end
343 end
344 end
345
346 // APB slave device
347 // One can use apbtargexp2_paddr[7:0] for master 1 registers.
348 localparam APB_IDLE = 2'b00;
349 localparam APB_WRITE = 2'b01;
350 localparam APB_READ = 2'b10;
351 reg [1:0] apb_slave_state;
352 always @(posedge clk or negedge rst_i) begin
353 spi_start <= 1'b0;
354 if (!rst_i) begin
355 apb_slave_state <= APB_IDLE;
356 apbtargexp2_pready <= 1'b0;
357 apbtargexp2_prdata <= {32{1'b0}};
358 end else begin
359 case (apb_slave_state)
360 APB_IDLE: begin
361 apbtargexp2_pready <= 1'b0;
362 apbtargexp2_prdata <= {32{1'b0}};
363 if (psel2) begin
364 if (apbtargexp2_pwrite) begin
365 apb_slave_state <= APB_WRITE;
366 end else begin
367 apb_slave_state <= APB_READ;
368 end
369 end
370 end
371 APB_WRITE: begin
372 if (psel2 && apbtargexp2_pwrite) begin
373 apbtargexp2_pready <= 1'b1;
374 if (|apbtargexp2_paddr[7:0]) begin // any addr != 0
375 spi_WDATA <= apbtargexp2_pwdata[7:0];
376 spi_start <= 1'b1;
377 end else begin
378 spi_msb <= apbtargexp2_pwdata[0];
379 end
380 end
381 apb_slave_state <= APB_IDLE;
382 end
383 APB_READ: begin
384 if (psel2 && !apbtargexp2_pwrite) begin // return the spi status
385 apbtargexp2_pready <= 1'b1;
386 if (!(|apbtargexp2_paddr[7:0])) begin
387 apbtargexp2_prdata[0] <= spi_status;
388 end
389 end
390 apb_slave_state <= APB_IDLE;
391 end
392 default: begin
393 apb_slave_state <= APB_IDLE;
394 end
395 endcase
396 end
397 end
398
399 assign mode_led = spi_status;
400
401 FLASH256K rom(
402 .DOUT(rom_out[31:0]),
403 .XADR(flash_in_addr[12:6]),
404 .YADR(flash_in_addr[5:0]),
405 .XE(rst_i),
406 .YE(rst_i),
407 .SE(rom_sel),
408 .ERASE(GND),
409 .PROG(GND),
410 .NVSTR(GND),
411 .DIN({32{GND}})
412 );
413
414 // RAM 4 block of 2K
415 // Inferring will probably also work, but with primitives it is somehow easier for me.
416 SDPB ram_0 (
417 .DO({dummy_sram0_rdata_0, sram0_rdata[7:0]}),
418 .DI({{24{GND}}, sram0_wdata[7:0]}),
419 .BLKSELA({GND, GND, sram0_cs}),
420 .BLKSELB({GND, GND, sram0_cs}),
421 .ADA({sram0_addr, GND,GND,GND}),
422 .ADB({sram0_addr, GND,GND,GND}),
423 .CLKA(clk),
424 .CLKB(clk),
425 .CEA(sram0_wren[0]),
426 .CEB(!sram0_wren[0]),
427 .OCE(VCC),
428 .RESETA(GND),
429 .RESETB(!rst_i)
430 );
431 defparam ram_0.BIT_WIDTH_0=8;
432 defparam ram_0.BIT_WIDTH_1=8;
433 defparam ram_0.BLK_SEL_0=3'b001;
434 defparam ram_0.BLK_SEL_1=3'b001;
435 defparam ram_0.READ_MODE=1'b0;
436 defparam ram_0.RESET_MODE="SYNC";
437
438 SDPB ram_1 (
439 .DO({dummy_sram0_rdata_1, sram0_rdata[15:8]}),
440 .DI({{24{GND}}, sram0_wdata[15:8]}),
441 .BLKSELA({GND, GND, sram0_cs}),
442 .BLKSELB({GND, GND, sram0_cs}),
443 .ADA({sram0_addr, GND,GND,GND}),
444 .ADB({sram0_addr, GND,GND,GND}),
445 .CLKA(clk),
446 .CLKB(clk),
447 .CEA(sram0_wren[1]),
448 .CEB(!sram0_wren[1]),
449 .OCE(VCC),
450 .RESETA(GND),
451 .RESETB(!rst_i)
452 );
453 defparam ram_1.BIT_WIDTH_0=8;
454 defparam ram_1.BIT_WIDTH_1=8;
455 defparam ram_1.BLK_SEL_0=3'b001;
456 defparam ram_1.BLK_SEL_1=3'b001;
457 defparam ram_1.READ_MODE=1'b0;
458 defparam ram_1.RESET_MODE="SYNC";
459
460 SDPB ram_2 (
461 .DO({dummy_sram0_rdata_2, sram0_rdata[23:16]}),
462 .DI({{24{GND}}, sram0_wdata[23:16]}),
463 .BLKSELA({GND, GND, sram0_cs}),
464 .BLKSELB({GND, GND, sram0_cs}),
465 .ADA({sram0_addr, GND,GND,GND}),
466 .ADB({sram0_addr, GND,GND,GND}),
467 .CLKA(clk),
468 .CLKB(clk),
469 .CEA(sram0_wren[2]),
470 .CEB(!sram0_wren[2]),
471 .OCE(VCC),
472 .RESETA(GND),
473 .RESETB(!rst_i)
474 );
475 defparam ram_2.BIT_WIDTH_0=8;
476 defparam ram_2.BIT_WIDTH_1=8;
477 defparam ram_2.BLK_SEL_0=3'b001;
478 defparam ram_2.BLK_SEL_1=3'b001;
479 defparam ram_2.READ_MODE=1'b0;
480 defparam ram_2.RESET_MODE="SYNC";
481
482 SDPB ram_3 (
483 .DO({dummy_sram0_rdata_3, sram0_rdata[31:24]}),
484 .DI({{24{GND}}, sram0_wdata[31:24]}),
485 .BLKSELA({GND, GND, sram0_cs}),
486 .BLKSELB({GND, GND, sram0_cs}),
487 .ADA({sram0_addr, GND,GND,GND}),
488 .ADB({sram0_addr, GND,GND,GND}),
489 .CLKA(clk),
490 .CLKB(clk),
491 .CEA(sram0_wren[3]),
492 .CEB(!sram0_wren[3]),
493 .OCE(VCC),
494 .RESETA(GND),
495 .RESETB(!rst_i)
496 );
497 defparam ram_3.BIT_WIDTH_0=8;
498 defparam ram_3.BIT_WIDTH_1=8;
499 defparam ram_3.BLK_SEL_0=3'b001;
500 defparam ram_3.BLK_SEL_1=3'b001;
501 defparam ram_3.READ_MODE=1'b0;
502 defparam ram_3.RESET_MODE="SYNC";
503
504 endmodule