Change spiflash pin interfaces to support quad SPI

This commit is contained in:
Clifford Wolf 2017-08-04 21:05:05 +02:00
parent cb87f93cf8
commit 82a51bc8a4
5 changed files with 156 additions and 51 deletions

View File

@ -3,10 +3,12 @@
set_io clk J3
set_io spi_cs R12
set_io spi_sclk R11
set_io spi_miso P11
set_io spi_mosi P12
set_io flash_csb R12
set_io flash_clk R11
set_io flash_io0 P12
set_io flash_io1 P11
set_io flash_io2 T9 # center on J3
set_io flash_io3 P8 # center on J3
# All the GPIO pins below are connected to pins that are floating
# on the iCE40-HX8K Breakout Board, expect the marked LED pins.

View File

@ -1,8 +1,29 @@
/*
* A simple simulation model for an SPI flash
*
* Copyright (C) 2017 Clifford Wolf <clifford@clifford.at>
*
* Permission to use, copy, modify, and/or distribute this software for any
* purpose with or without fee is hereby granted, provided that the above
* copyright notice and this permission notice appear in all copies.
*
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*
*/
module spiflash (
input spi_cs,
output reg spi_miso,
input spi_mosi,
input spi_sclk
input csb,
input clk,
inout io0, // MOSI
inout io1, // MISO
inout io2,
inout io3
);
localparam verbose = 0;
@ -17,9 +38,15 @@ module spiflash (
reg [7:0] spi_out;
reg spi_io_vld;
reg qspi_active = 0;
reg powered_up = 0;
reg in_xfer = 0;
reg spi_miso;
wire spi_mosi = io0;
assign io1 = spi_miso;
// 16 MB (128Mb) Flash
reg [7:0] memory [0:16*1024*1024-1];
@ -37,6 +64,8 @@ module spiflash (
powered_up = 1;
if (spi_cmd == 8'hB9)
powered_up = 0;
if (spi_cmd == 8'hFF)
qspi_active = 0;
end
if (powered_up && spi_cmd == 'h03) begin
@ -67,8 +96,8 @@ module spiflash (
end
endtask
always @(spi_cs) begin
if (spi_cs) begin
always @(csb) begin
if (csb) begin
if (verbose && in_xfer) begin
$display("");
$fflush;
@ -81,15 +110,15 @@ module spiflash (
end
end
always @(spi_cs, spi_sclk) begin
always @(csb, clk) begin
spi_io_vld = 0;
if (!spi_cs && !spi_sclk) begin
if (!csb && !clk) begin
spi_miso = buffer[7];
end
end
always @(posedge spi_sclk) begin
if (!spi_cs) begin
always @(posedge clk) begin
if (!csb) begin
buffer = {buffer, spi_mosi};
bitcount = bitcount + 1;
if (bitcount == 8) begin

View File

@ -1,3 +1,22 @@
/*
* Interface module for SPI flash and PicoRV32 native memory interface
*
* Copyright (C) 2017 Clifford Wolf <clifford@clifford.at>
*
* Permission to use, copy, modify, and/or distribute this software for any
* purpose with or without fee is hereby granted, provided that the above
* copyright notice and this permission notice appear in all copies.
*
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*
*/
module spimemio (
input clk, resetn,
@ -6,10 +25,12 @@ module spimemio (
input [23:0] addr,
output reg [31:0] rdata,
output reg spi_cs,
output reg spi_sclk,
output reg spi_mosi,
input spi_miso
output reg flash_csb,
output reg flash_clk,
output flash_io0,
input flash_io1,
input flash_io2,
input flash_io3
);
parameter ENABLE_PREFETCH = 1;
@ -21,11 +42,17 @@ module spimemio (
reg xfer_wait;
reg prefetch;
reg spi_mosi;
wire spi_miso;
assign flash_io0 = spi_mosi;
assign spi_miso = flash_io1;
always @(posedge clk) begin
ready <= 0;
if (!resetn) begin
spi_cs <= 1;
spi_sclk <= 1;
flash_csb <= 1;
flash_clk <= 1;
xfer_cnt <= 8;
buffer <= 8'hAB << 24;
addr_q_vld <= 0;
@ -33,14 +60,14 @@ module spimemio (
prefetch <= 0;
end else
if (xfer_cnt) begin
if (spi_cs) begin
spi_cs <= 0;
if (flash_csb) begin
flash_csb <= 0;
end else
if (spi_sclk) begin
spi_sclk <= 0;
if (flash_clk) begin
flash_clk <= 0;
spi_mosi <= buffer[31];
end else begin
spi_sclk <= 1;
flash_clk <= 1;
buffer <= {buffer, spi_miso};
xfer_cnt <= xfer_cnt - 1;
end
@ -59,7 +86,7 @@ module spimemio (
xfer_wait <= 1;
prefetch <= 0;
end else begin
spi_cs <= 1;
flash_csb <= 1;
buffer <= {8'h 03, addr};
addr_q <= addr + 4;
addr_q_vld <= 1;
@ -76,7 +103,7 @@ module spimemio (
prefetch <= 0;
xfer_cnt <= 0;
xfer_wait <= 0;
spi_sclk <= 1;
flash_clk <= 1;
end
end
endmodule

View File

@ -1,3 +1,22 @@
/*
* Test bench for the "spiflash" SoC
*
* Copyright (C) 2017 Clifford Wolf <clifford@clifford.at>
*
* Permission to use, copy, modify, and/or distribute this software for any
* purpose with or without fee is hereby granted, provided that the above
* copyright notice and this permission notice appear in all copies.
*
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*
*/
module testbench;
reg clk = 1;
always #5 clk = ~clk;
@ -14,10 +33,12 @@ module testbench;
wire [31:0] gpio_i = 0;
wire [31:0] gpio_o;
wire spi_cs;
wire spi_sclk;
wire spi_mosi;
wire spi_miso;
wire flash_csb;
wire flash_clk;
wire flash_io0;
wire flash_io1;
wire flash_io2;
wire flash_io3;
always @(gpio_o) begin
$write("<GPIO:%02x>", gpio_o[7:0]);
@ -34,16 +55,20 @@ module testbench;
.clk (clk ),
.gpio_i (gpio_i ),
.gpio_o (gpio_o ),
.spi_cs (spi_cs ),
.spi_sclk(spi_sclk),
.spi_mosi(spi_mosi),
.spi_miso(spi_miso)
.flash_csb(flash_csb),
.flash_clk(flash_clk),
.flash_io0(flash_io0),
.flash_io1(flash_io1),
.flash_io2(flash_io2),
.flash_io3(flash_io3)
);
spiflash spiflash (
.spi_cs (spi_cs ),
.spi_sclk(spi_sclk),
.spi_mosi(spi_mosi),
.spi_miso(spi_miso)
.csb(flash_csb),
.clk(flash_clk),
.io0(flash_io0),
.io1(flash_io1),
.io2(flash_io2),
.io3(flash_io3)
);
endmodule

View File

@ -1,3 +1,22 @@
/*
* Top-level for "spiflash" SoC demo
*
* Copyright (C) 2017 Clifford Wolf <clifford@clifford.at>
*
* Permission to use, copy, modify, and/or distribute this software for any
* purpose with or without fee is hereby granted, provided that the above
* copyright notice and this permission notice appear in all copies.
*
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*
*/
module top (
input clk,
output trap,
@ -5,10 +24,12 @@ module top (
input [31:0] gpio_i,
output reg [31:0] gpio_o,
output spi_cs,
output spi_sclk,
output spi_mosi,
input spi_miso
output flash_csb,
output flash_clk,
output flash_io0,
input flash_io1,
input flash_io2,
input flash_io3
);
parameter integer MEM_WORDS = 256;
parameter [31:0] STACKADDR = (4*MEM_WORDS); // end of memory
@ -51,16 +72,17 @@ module top (
spimemio spimemio (
.clk(clk),
.resetn(resetn),
.valid (mem_valid && mem_addr[31:30] == 2'b10),
.ready (spimem_ready),
.addr (mem_addr[23:0]),
.rdata (spimem_rdata),
.spi_cs (spi_cs ),
.spi_sclk (spi_sclk),
.spi_mosi (spi_mosi),
.spi_miso (spi_miso)
.flash_csb (flash_csb),
.flash_clk (flash_clk),
.flash_io0 (flash_io0),
.flash_io1 (flash_io1),
.flash_io2 (flash_io2),
.flash_io3 (flash_io3)
);
reg [31:0] memory [0:MEM_WORDS-1];