SoC part 5 - SPI controller and SPI flash
The FPGC can now load programs over UART, but it forgets them at every power cycle. The board has SPI flash chips for exactly that, and four more chips that also talk SPI: two USB host chips, an Ethernet chip and a micro SD card. This post is about SPI itself, one controller for all six of them, getting the SPI flash to run at 50 MHz on four data lines, and a small trick that makes reading much faster.
What SPI is
SPI (Serial Peripheral Interface) is one of the simplest ways for two chips to talk. One side is in charge (here the FPGA), and the other is the device. They are connected with four wires:
- CS (chip select): the FPGA pulls it low to talk to the device, and high when it’s done
- CLK: the clock, made by the FPGA
- MOSI: data from the FPGA to the device
- MISO: data from the device to the FPGA
Data goes one bit per clock, most significant bit first. In the most common form, called mode 0, the clock is low when nothing happens, a bit is taken at the rising edge of the clock, and changed at the falling edge. So each side changes its bit at a falling edge, and the other side takes it at the next rising edge, half a clock period later, when it has been stable for a while:
CS ‾‾‾\____________________________________ ... ___/‾‾‾
CLK ________/‾‾‾\___/‾‾‾\___/‾‾‾\___/‾‾‾\___ ...
MOSI ----< b7 X b6 X b5 X b4 X b3 ...
^ taken ^ taken ^ taken ^ takenThere is no address on the bus and no protocol in the hardware: everything is a stream of bytes, and each device defines what the bytes mean. A flash chip, for example, takes a command byte, then an address, and then sends the data back. Because both data lines are clocked by the same clock, the FPGA always receives a byte while it sends one, but in practice a transfer is either sending (and ignoring what comes back) or receiving (and sending something meaningless). That is how the FPGC uses it too.
Six devices
Every SPI chip on the FPGC has its own pins, so each gets its own bus:
| Device | What for | Fastest clock |
|---|---|---|
| 2× W25Q128JV flash | programs, and files | 50 MHz for a plain read, 133 MHz for the rest |
| 2× CH376T | USB host | about 23 MHz |
| ENC28J60 | 10 Mbit Ethernet | 20 MHz, and at least 8 MHz |
| micro SD card | removable storage | 25 MHz, and at most 400 kHz while it starts up |
Reading the datasheets of all four, they turned out to be very similar: they all work in mode 0, with bytes and a chip select that stays low for a whole command. The differences are the clock speed, and a few rules that software can follow: the CH376 wants a short pause after every byte, the SD card wants 74 clocks before its first command, and the ENC28J60 sends a dummy byte before some of its registers.
In the previous FPGC version, the SPI hardware grew over time into three different modules: a simple one, one with buffers and bursts for a DMA engine, and one for quad reads from the SPI flash. This time, I wanted one module for all six buses.
One controller
The controller runs on the same 100 MHz clock as the rest of the FPGC, just like the UART controller. The SPI clock is simply a register that the controller flips every N cycles: every cycle for 50 MHz, every 2 cycles for 25 MHz, and every 4 for 12.5 MHz. As each device on the board is fixed, the speed of each bus is fixed too: 50 MHz for the flashes, 25 MHz for the SD card, and 12.5 MHz for the USB and Ethernet chips. The SD card should start at 400 kHz according to its specification, but I never did that in the previous version, and every card I tried was fine with 25 MHz from the start.
For software, each bus has five registers: one to send or receive a byte, one to send or receive a word (four bytes in one go), one with the chip select, one with the state of the device’s interrupt pin, and one to enable its interrupt. Every access waits until the transfer before it is done, so a program never has to check whether the bus is busy.
The USB and Ethernet chips have an interrupt pin, which they pull low when they need attention: a USB device was plugged in, a command is done, or a network packet arrived. With its interrupt enabled, a bus interrupts the CPU when that happens, just like the UART and timer do. The CPU’s interrupt handler then asks the chip what happened, over SPI, and that also makes the chip release its pin.
The SPI flash
The SPI flash is where speed matters most. Later, BRFS (my file system) will keep its files on the second flash, and read the whole file system into SDRAM when the FPGC starts. With a 16 MiB partition, that is a lot of data to read.
Writing to SPI flash is a different story: it’s set by the chip, not by the bus. A flash can only change bits from 1 to 0. To get 1s back, a whole sector of 4 KiB (or a block of 64 KiB) has to be erased, which takes about 45 ms (extremely slow!) for a sector. Programming a page of 256 bytes then takes about 0.4 ms. Sending those 256 bytes at 50 MHz takes only 41 µs, so a faster bus would hardly change anything for writing. Reading, on the other hand, is as fast as the bus can go.
Four data lines
The W25Q128JV has six pins, and the two that are normally called write protect and hold can also be data lines. In quad mode, the flash uses all four data lines to send, so a byte takes two clocks instead of eight. On the FPGC, all six pins of both flashes go to the FPGA, so both can do this.
The flash has two quad read commands. The fastest one sends the address on four lines too, and can even skip the command byte on the next read. The simpler one, Fast Read Quad Output (6Bh), sends the command and address on one line like every other command, and only the data comes back on four lines. For reading a few kilobytes at a time, the difference is a few clocks out of thousands, so I chose the simpler one: the controller only needs to stop driving the lines and take four bits per clock when receiving.
Between the address and the data, the flash needs 8 clocks to prepare the data (the dummy clocks). With four lines, a word takes exactly 8 clocks, so the program simply reads one word and throws it away.
50 MHz and the pin timing
In the SDRAM post, I explained setup and hold, and how the timing at the pins is calculated. The same applies here, but with a twist: the SPI clock is not a clock in the FPGA, but a signal from a register, which goes out through a pin like any other.
In mode 0, the textbook way is to take the flash’s bit at the rising edge of the SPI clock. At 50 MHz, that edge comes 10 ns after the falling edge at which the flash changes its bit. In those 10 ns, the falling edge has to get out of the FPGA, travel over the PCB to the flash, the flash needs up to 6 ns to put the new bit on its pin, and the bit has to travel back and into the FPGA’s input register. In the slow corner, that adds up to about 11.4 ns.
The fix is simple: take the bit one cycle later. At 20 ns, the bit has arrived with time to spare, and it stays until a few nanoseconds after the next falling edge. The SDC file tells Quartus all of this (a clock on each SPI clock pin, the delays of each device from its datasheet, the trace lengths from the PCB, and the one cycle later), and Quartus then checks every SPI pin after every compile.
At 25 MHz, there is time plenty for all of this, so no changes are needed at those speeds.
Read-ahead
With quad reads at 50 MHz, a word takes 16 cycles of the FPGC’s clock on the bus. When I measured a copy of the whole 16 MiB flash into SDRAM on the board, though, a word took 30 cycles. The reason is that the CPU and the bus take turns:
CPU ask ........ wait ........ write, loop ask ........ wait ........
bus . [ word 1: 16 cycles ] . . . [ word 2: 16 cycles ]The bus only starts a word when the CPU asks for it, and the CPU only asks for the next word after it has written the previous one to SDRAM. Each side waits for the other about half of the time.
The fix is called read-ahead: after a word, the controller doesn’t wait for the CPU, but starts receiving the next word right away, and keeps it until the CPU asks for it. When the CPU then asks, the word is often already there, and the controller starts on the next one. Now the CPU and the bus work at the same time:
CPU ask/wait .... write ask write ask write ask write
bus [ word 1 ][ word 2 ][ word 3 ][ word 4 ][ word 5 ]The controller holds at most two words: one waiting for the CPU, and one more that it finished receiving. Then it stops the clock and waits. The flash chip doesn’t mind: it just continues where it was when the clock starts again.
There is one catch. The controller doesn’t know when the program will stop reading. When the program then does something else, like ending the command with the chip select, the two words that were read ahead are simply thrown away. For SPI flash, that’s no problem: reading a few extra bytes doesn’t change anything. For the other devices it would be: reading data from the SD card or the Ethernet chip moves on in their buffers, so a word read too early would be lost. So only the two flash buses read ahead.
With read-ahead, the copy of 16 MiB takes 0.79 s instead of 1.28 s: 21 MB/s, three and a half times what the flash gives on one data line. The bus is now busy most of the time, so this is about as fast as flash will go without a faster SPI clock.
I chose to optimize this part right now, as long OS boot times due to SPI flash loading were quite annoying on previous FPGC designs. Optimizing it now means that I do not have to bother later.
Next hardware test
On the FPGC board, I first sent one command to each device, at its own speed: the ID of both flashes, the CH376’s check command (it answers with the inverse of the byte it gets), writing and reading back a register of the Ethernet chip, and the reset command of the SD card. All six answered correctly, meaning it works!
For the interrupts, I plugged a USB device into both USB ports, and made each chip pull its interrupt pin: the CH376s by resetting their USB bus, so the device connects again, and the Ethernet chip by letting it copy a few bytes in its own memory, with an interrupt when it’s done. All three interrupts arrived, with the right numbers I configured for them.
Then a test of the whole second flash: erasing all 256 blocks, programming every page with a pseudo-random pattern, and copying everything back into SDRAM, once with quad reads and once with one data line, comparing every word.
Simulation

Here you can see the Quad read in action: the command and address are sent on IO0, then all four lines switch to input, and the data is received four bits per clock. The controller prefetches the next word before the driver requests it.
Next steps
With the SPI flash working, the boot ROM can now load a program from it instead of over UART, so the FPGC can start on its own after a power cycle. That is the topic of the next Toolchain post. The USB, Ethernet and SD card buses are ready for their drivers, which will come later at some point.