Toolchain part 3 - UART bootloader
The assembler gives a binary image of a program. To run it, the program has to get into the SDRAM of the FPGC. This post is about how that works: a small bootloader in the boot ROM that receives a program over the UART, and the program on the host PC that sends it.
Getting a program onto the FPGC
After a reset, the CPU starts in the boot ROM: 4 KiB of memory inside the FPGA, whose contents are part of the FPGA design. There are a few ways to get a program to run:
- Put the program in the ROM. This works, but every change to the program means building the whole FPGA design again, which takes minutes.
- Load it through JTAG, the same cable that programs the FPGA. Possible, but it needs extra logic in the design, and tools that are specific to Intel’s FPGAs.
- Load it from flash memory on the board. Good for a program that should start on its own, like an operating system, but the flash first has to be written somehow, and the SPI flash on the FPGC is not a removable module. The micro SD card on the board could be used for this, but that requires a complex sd card controller and file system if I also want to use it for data.
- Load it over a UART, with a small bootloader in the ROM that receives it and writes it to SDRAM.
For running new programs over and over during development, the last one is the most convenient, and is what I went with since many FPGC revisions ago. Loading from the SPI flash (for programs that should start on their own) and over Ethernet (from an operating system) will come in later posts.
What can go wrong on a serial line
A serial line is simple, but it is not perfect, and the protocol has to deal with what can go wrong:
- A bit can flip, from noise on the line, and then the program is wrong without anyone knowing.
- Bytes can get lost, for example when the receiver is not fast enough.
- The FPGC might not be ready to receive, for example because it is not in UART bootloader mode.
- The program itself might hang, and never give the board back.
The design of the protocol follows from this list.
The protocol
When the boot ROM is ready, it prints a line, boot: ready, and waits. The PC then sends one command:
| Bytes | What |
|---|---|
| 1 | L, for load |
| 4 | length of the program in bytes |
| N | the program itself |
| 4 | CRC-32 of the program |
The boot ROM writes the program to SDRAM from address 0 while it receives it, and computes the CRC-32 of every byte at the same time. At the end, it compares its own CRC with the one from the PC, and answers with a line including the CRC-32 hex:
boot: ok 9afbeb43, and it runs the programboot: bad 1c2d3e4f, and it is ready for the next command
Everything the board sends is text, so I can also just watch it in a terminal. What the PC sends is binary, because a program is binary.
Lost bytes do not need anything special: if bytes get lost, the boot ROM keeps waiting for the rest of the program, and the PC does not get an answer. A flipped bit gives a wrong CRC. In both cases, the PC tries again.
This is a new UART bootloader protocol, as the previous ones I made did not include error handling and used a more complex setup as there the UART loader was executed from RAM and had to rely on interrupts and L1 cache management. This should be a lot simpler and more robust.
The CRC
A CRC (cyclic redundancy check) is a number computed from the data, which changes when the data changes. The simplest form of such a check would be the sum of all bytes, but a sum misses a lot: swap two bytes, and the sum is the same.
A CRC treats the whole program as one very long binary number, and divides it by a fixed number, the polynomial. The remainder of that division is the CRC. Division mixes every bit into the result, so a single flipped bit, two swapped bytes, or a burst of wrong bits always gives a different remainder. The division is done in a special way, without carries, so it is only shifts and XORs, which is easy for a CPU.
The boot ROM uses CRC-32, the same one that Ethernet and ZIP files use. That has one big advantage: the PC side does not need its own implementation, as every programming language has it in its standard library already.
There are two ways to compute it. The fast way uses a table of 256 precomputed values, and handles a whole byte in one step. The table takes 1 KiB, a quarter of the boot ROM. The slow way handles the byte one bit at a time, in a small loop of 8 steps of a shift and an XOR. The slow way still takes far less time than a byte takes to arrive: at 1 Mbaud, a byte takes 10 µs, which is 1,000 cycles of the CPU. So the boot ROM uses the loop, and the PC can send the whole program without pauses.
Starting the program, and coming back
When the CRC is right, the boot ROM runs ccache before it jumps to the program. The program was written to SDRAM with normal writes, so parts of it may still be in the data cache, and the instruction cache could still have an older program. ccache writes the data cache back and clears both, so the CPU fetches the new program from SDRAM.
A program that is done jumps back to the start of the boot ROM. That works as a soft reset: the boot ROM switches off the devices and interrupts that the program might have left on, and is ready for the next program, without a real reset of the board.
Before it says boot: ready, the boot ROM also prints the CPU’s error register: boot: err 00000000 pc 00000000. The CPU keeps this register through a reset, so after a program crashed and the board was reset, the first line tells what went wrong and where.
Resetting the board from the PC
The PC has to be able to reset the board, to prevent needing physically pushing a button every time a program hangs or does not return to the bootloader after execution. The USB serial chip on the board has two extra lines next to the data, RTS and DTR, which a program on the PC can switch on and off. The FPGC resets when RTS is on and DTR is off for 1 ms.
Opening a serial port on the PC switches both lines on together, and closing it switches both off together, so just opening a terminal never resets the board. The 1 ms filters out the short moment in between, when one line has already changed and the other has not yet. Only a program that wants a reset sets that combination on purpose.
The PC side
The program on the PC, uartload, is the other half of the protocol. It does the same every time:
- Reset the board, and wait for
boot: ready. - Send the program, with its length and CRC.
- Wait for the answer. If it is not
boot: okwith the right CRC, start again from the reset, up to three times. - Show what the board sends, until the program jumps back to the boot ROM.
It resets the board before every load, even when the boot ROM is already waiting. This way, every load starts from the same known state, and it does not matter whether the previous program returned, crashed or is still running. A reset only takes a few milliseconds.
Like the assembler, uartload is a small standalone tool. Together, the two are all that is needed on the PC side right now: one turns the source into a binary image, and the other gets the image onto the FPGC.
Hello world
Putting everything together: the hello world from the first post of this series, assembled by b32asm, and loaded by uartload:
~/repos/FPGC$ b32asm Software/programs/hello.asm build/hello.bin && uartload build/hello.bin
boot: err 00000000 pc 00000000
boot: ready
boot: ok 9afbeb43
hello, world
boot: err 00000000 pc 00000000
boot: readyThe first two output lines are the boot ROM after the reset. Then the program arrives with the right CRC, runs, prints its line, and jumps back to the boot ROM, which is ready for the next program.
Next steps
With the language, the assembler and the bootloader, the FPGC has a complete, if minimal, toolchain: write a program, assemble it, and run it on the board within a second. The Toolchain series will continue with the SPI flash bootloader and later the C compiler. For the SPI flash bootloader, we first need to have SPI support, so that is probably the next post (in the SoC series).