EP09 - SPI Flash bootloader and A/B upgrade on ALINX PI-AR035-F1-1 ================================================================ Board : ALINX PI-AR035-F1-1, XC7A35T-FGG484-2 CPU : OpenHW CVE2 / CV32E20, RV32IM, 50 MHz Flash : GD25Q128E, 16 MiB, JEDEC 0x00c84018, SPI 6.25 MHz RAM : 64 KiB unified true-dual-port block RAM UART : 115200 8N1 Build : Vivado 2025.2, RISC-V GCC 10.2.0 What is tested -------------- 1. A bootloader runs from the FPGA block RAM, identifies the flash and offers a three-second UART loader window. 2. A 1,021-byte application is written into slot A over UART, including a 4 KiB erase and 256-byte page programming. 3. The same path writes slot B and switches to it without a chip reset. 4. A second application image is written to slot A with a deliberately wrong advertised CRC (force mode). The write is allowed, then the image check fails and boot() falls back to slot B. 5. Two independent cold boots are captured with no host loader. Both select A, fail its CRC, print FALLBACK, load B and run five heartbeats. 6. Slot A is restored with a good image (metadata seq 4), then a final cold boot selects A directly and runs five heartbeats. Flash map --------- 0x000000..0xBFFFFF FPGA bitstream / factory area (untouched) 0x00C00000..0x00C7FFFF slot A, 512 KiB 0x00C80000..0x00CFFFFF slot B, 512 KiB 0x00D00000..0x00D00FFF metadata A, 4 KiB sector 0x00D01000..0x00D01FFF metadata B, 4 KiB sector RAM map ------- 0x0000..0x3FFF bootloader, stack top 0x4000 0x4000..0xFFFF application, max 48 KiB, stack top 0x10000 The bootloader and application share one 64 KiB true-dual-port RAM. Port A is the instruction port (read only); port B is the data port (read/write). That is why an application written to 0x4000 by the bootloader is visible to the instruction fetch port after the jump. The RAM is initialized with NOPs before firmware.hex is loaded, so an empty application region is not an illegal instruction. UART frame ---------- 15-byte header, little endian: 'P' | slot(1) | force(1) | len(4) | crc32(4) | version(4) | payload[len] CRC is standard CRC-32 (zlib polynomial 0xEDB88320, init/final 0xFFFFFFFF). force=0 rejects a CRC mismatch. force=1 writes the payload even when the advertised CRC is wrong; this is used only to create a bad slot for fallback testing. Metadata record --------------- 32 bytes: magic, seq, slot, len, crc, version, attempts, rec_crc. magic = 0x45503952. rec_crc covers the first 28 bytes. The record with the larger seq is active; if its image fails CRC, boot() tries the other record. Boot flow --------- main() banner + flash ID 3 s window: if 'L' is received, enter loader boot(): read A/B metadata, load newest, verify CRC, jump loader(): print LOADER READY, then wait forever for frames bad frame / bad CRC / program fail / meta fail -> continue good frame -> write slot, write metadata, load and jump switch fail -> boot() older slot, no chip reset The first board version rebooted about once per second. It was not caused by EP08. EP09 itself had a stale firmware.hex in the bitstream, separate instruction/data RAMs, and a loader path that returned to main() and armed the watchdog. The shipped version fixes all three and has no executed reboot call in the normal flow. Results ------- Slot A (metadata seq 1) FRAME slot=0 force=0 len=1021 crc=a8a0cdcd ver=00000001 RX_CRC=a8a0cdcd EXPECT=a8a0cdcd PROGRAM_PASS META_PASS SEQ=1 ACTIVE=A LOAD SLOT=A ... LOAD_PASS EP09 APP A VER=00000001 RUNNING + 5 heartbeats Slot B (metadata seq 2) FRAME slot=1 force=0 len=1021 crc=0a74fc2c ver=00000002 RX_CRC=0a74fc2c EXPECT=0a74fc2c PROGRAM_PASS META_PASS SEQ=2 ACTIVE=B LOAD SLOT=B ... LOAD_PASS EP09 APP B VER=00000002 RUNNING + 5 heartbeats Forced A (metadata seq 3) FRAME slot=0 force=1 len=1021 crc=575f3232 ver=00000003 RX_CRC=a8a0cdcd EXPECT=575f3232 PROGRAM_PASS META_PASS SEQ=3 ACTIVE=A LOAD SLOT=A ... LOAD_FAIL SWITCH_FAIL LOAD SLOT=A ... LOAD_FAIL FALLBACK LOAD SLOT=B ... LOAD_PASS EP09 APP B VER=00000002 RUNNING + 5 heartbeats Cold boot, two independent runs (both 336 bytes, cmp identical) EP09 FLASH BOOTLOADER FLASH ID=00c84018 OK PRESS L FOR LOADER (3s)... LOAD SLOT=A LEN=1021 VER=00000003 CRC=a8a0cdcd LOAD_FAIL FALLBACK LOAD SLOT=B LEN=1021 VER=00000002 CRC=0a74fc2c LOAD_PASS EP09 APP B VER=00000002 RUNNING + 5 heartbeats Restore A (metadata seq 4) FRAME slot=0 force=0 len=1021 crc=a8a0cdcd ver=00000004 RX_CRC=a8a0cdcd EXPECT=a8a0cdcd PROGRAM_PASS META_PASS SEQ=4 ACTIVE=A LOAD SLOT=A ... LOAD_PASS EP09 APP A VER=00000001 RUNNING + 5 heartbeats Final cold boot (268 bytes) EP09 FLASH BOOTLOADER FLASH ID=00c84018 OK PRESS L FOR LOADER (3s)... LOAD SLOT=A LEN=1021 VER=00000004 CRC=a8a0cdcd LOAD_PASS EP09 APP A VER=00000001 RUNNING + 5 heartbeats The A application banner is a compile-time string and still says VER=00000001; the version actually written to metadata is 4. Use the LOAD/META line for the image version, not the banner. B prints VER=00000002. Timing / resources (final bitstream) ------------------------------------ WNS 0.735 ns, WHS 0.054 ns, TNS/THS 0, 0 failing endpoints, 11,147 endpoints. LUT 4,261 (20.49%), FF 3,720 (8.94%), BRAM 16/50 (32%), DSP 11, IOB 25. DRC 0 errors, 76 warnings, 0 critical warnings. Bitstream 2,192,137 B, SHA-256 bbb72e23...bbca665. Bootloader 7,192 B, SHA-256 c5d90cc0...65649ba8. firmware.hex 16,384 words / 65,536 B, SHA-256 dfcaaa3f...af4dac3d. App A 1,021 B, SHA-256 c1457fe5...6aebbfba. App B 1,021 B, SHA-256 5246f22c...6f887dbc. Warnings are 33 DPIP, 12 DPOP and 21 REQP-1839; the remaining 10 are other warnings. REQP-1839 is the CVE2 register-file asynchronous reset driving BRAM address pins. The reset is only asserted during configuration; it is recorded as a boundary, not hidden. Boundaries ---------- - The loader uses a raw UART frame, not a filesystem or a network protocol. - Slot size is 512 KiB, but the test image is only 1,021 B; large-image erase/program time is not measured. - Metadata uses two alternating 4 KiB sectors; there is no journal, power-fail atomicity test, or wear-leveling. - CRC is checked before jump, but there is no rollback after an application has started and crashed; fallback happens at boot/switch time. - No flash protection, secure boot, signature, or key storage. - No power-cycle test with a write in progress. - The application banner is a compile-time string, not the metadata version. - The 3 s loader window is a fixed loop count, not a calibrated timer. Build ----- Vivado 2025.2, part xc7a35t-2fgg484. vivado -mode batch -source build_ep09.tcl vivado -mode batch -source program_ep09.tcl Firmware: cd sw && make clean all cd sw/app && make clean all The bitstream embeds sw/firmware.hex through dpram_64k's INIT_FILE, so a bootloader change requires re-synthesis. App A/B are loaded over UART and do not require re-synthesis. Host tools: python3 host_flash_loader.py python3 cap_serial.py Two cold-boot logs are included as run1/run2 and compare byte-for-byte.