Write a small SPI flash driver in layers, in C that runs on the host through the bus-manager seat. Read the flash twice, check what came back, and report a checksum the bench can compare.
Hardware context: One layered DMA driver run as firmware, as C on the host through the seat, and as a sequence table. A layered SPI flash driver in C on the host.
Your C runs on the server as a process of its own. Every reg_read32 and reg_write32 is one transfer of the bus-manager seat. The kit gives layer 1: spi_transfer(command) sends one command byte and returns the 24 bits the flash sent back.
Write layer 2: flash_id() with command 0x9F and flash_rewind() with 0x66. Then flash_read(out, n), which reads n bytes with command 0x03, three bytes a frame. Then write the test, layer 3. TB_ARG holds n in bits 7:0 and an expected checksum in bits 31:16.
The test routes the SPI pads (PINMUX = 0xAAA) and fails with code 1 unless the flash ID is 0xC0FFEE. It reads n bytes, rewinds, reads them again, and fails with code 3 if the two reads differ. It prints sum and the checksum with tb_put_hex, then a new line. It fails with code 2 if the checksum differs from TB_ARG's, and passes otherwise.
Full specification
| Command | The flash model answers |
|---|---|
| 0x9F | C0 FF EE |
| 0x03 | The next three bytes of its 256-byte image |
| 0x66 | Nothing; the next 0x03 starts at byte 0 again |
| Any other | C0 FF EE |
A frame is one command byte and 24 reply clocks. The first reply byte is bits 23:16 of spi_transfer's result. The checksum is s = (s × 31 + byte) mod 65,536 over the n bytes, from s = 0.
Fail codes: return 1, 2 or 3 from main; the start-up code writes the fail word 0xBAD0_0000 + code. Returning 0 writes the pass word.
Given Files
Read-only files supplied by the bench.
drivers/spi.h
#ifndef AP_SPI_H
#define AP_SPI_H
#include "hal.h"
static inline unsigned spi_transfer(unsigned char command)
{
while (reg_read32(SPI_STATUS) & SPI_STATUS_BUSY_MASK) { }
reg_write32(SPI_STATUS, SPI_STATUS_DONE_MASK);
reg_write32(SPI_DATA, command);
while (!FIELD_GET(SPI_STATUS_DONE, reg_read32(SPI_STATUS))) { }
return FIELD_GET(SPI_DATA_RECEIVED, reg_read32(SPI_DATA));
}
#endif
hal.h
#ifndef AP_HAL_H
#define AP_HAL_H
#include "ap_soc_regs.h"
/* V13.4 supplies these two functions through DPI in a host-native build. */
#ifdef AP_HOST_DPI
unsigned reg_read32(unsigned address);
void reg_write32(unsigned address, unsigned value);
#else
static inline unsigned reg_read32(unsigned address)
{
return *(volatile unsigned *)address;
}
static inline void reg_write32(unsigned address, unsigned value)
{
*(volatile unsigned *)address = value;
}
#endif
#define FIELD_GET(field, word) (((word) & field##_MASK) >> field##_SHIFT)
#define FIELD_PREP(field, value) (((unsigned)(value) << field##_SHIFT) & field##_MASK)
#endif
tb.h
#ifndef AP_TB_H
#define AP_TB_H
_Noreturn void tb_pass(void);
_Noreturn void tb_fail(unsigned code);
void tb_putc(char byte);
void tb_puts(const char *text);
void tb_put_hex(unsigned value);
void tb_event(unsigned id);
/* Slot zero is the single bench-to-firmware argument word. */
unsigned tb_arg(unsigned n);
void tb_wait_event(unsigned id);
#endif
soc_host.h
/* soc_host.h: what a C test that runs on the host may call besides hal.h and tb.h (stage E7).
The test is built as an executable with soc_host_shim.c and no C library (soc-bench.mjs,
buildHostTest; tools/soc-run.mjs) and started by the pre-built bench, as a process of its own,
with +core=bfm +ctest=<file>. reg_read32 and reg_write32 of hal.h, and so every call of
tb.h and of the drivers, become transfers of the bus-manager seat on the chip's data port.
A transfer that answers ERROR ends the test with fail code 0xffff, as the trap does in firmware;
the calls below return the error instead, for tests that look for it. */
#ifndef SOC_HOST_H
#define SOC_HOST_H
/* One transfer: size 1, 2 or 4 bytes. Returns 1 when the bus answered ERROR. */
int host_read(unsigned address, unsigned size, unsigned *value);
int host_write(unsigned address, unsigned size, unsigned value);
/* Let cycles of clk_sys pass with the bus idle. */
void host_idle(unsigned cycles);
/* The interrupt pins of the seat: bit 0 software, bit 1 timer, bit 2 external. */
unsigned host_irq_pins(void);
/* A word of ROM or SRAM by the back door. */
unsigned host_peek(unsigned address);
void host_poke(unsigned address, unsigned value);
#endif
SPI · register manual
| Offset | Register | Bits | Field | Access | Reset | Held by | Meaning |
|---|---|---|---|---|---|---|---|
0x000 | DATA | 7:0 | command | wo | not stored | Write: a command byte; the write starts a frame of 32 serial clocks. Read: the last 24 bits received. Command byte. | |
0x000 | DATA | 23:0 | received | ro | block | Last 24 bits received. | |
0x004 | STATUS | 0 | busy | ro | block | Frame state. A frame is running, and chip select is low. | |
0x004 | STATUS | 1 | done | w1c | block | A frame has ended (new in v1.5). | |
0x008 | IRQ_ENABLE | 0 | done | rw | 0x0 | regs | Lets DONE raise interrupt source 4 (new in v1.5). 1 enables the request. |
## SPI (base 0x1000_5000, interrupt source 4, clk_sys, PD_PER) | Offset | Register | Bits | Field | Access | Reset | Held by | Meaning | | --- | --- | --- | --- | --- | --- | --- | --- | | `0x000` | DATA | 7:0 | command | wo | | not stored | Write: a command byte; the write starts a frame of 32 serial clocks. Read: the last 24 bits received. Command byte. | | `0x000` | DATA | 23:0 | received | ro | | block | Last 24 bits received. | | `0x004` | STATUS | 0 | busy | ro | | block | Frame state. A frame is running, and chip select is low. | | `0x004` | STATUS | 1 | done | w1c | | block | A frame has ended (new in v1.5). | | `0x008` | IRQ_ENABLE | 0 | done | rw | `0x0` | regs | Lets DONE raise interrupt source 4 (new in v1.5). 1 enables the request. |
Examples
Example 1
Image A, 12 Bytes- Input
TB_ARG: n = 12 and the expected checksum
- Output
sum and pass
Four frames each way; both reads agree and the checksum matches.
Example 2
Image B, 7 Bytes- Input
TB_ARG: n = 7
- Output
sum and pass
The third frame brings three bytes; only the first is kept.
Example 3
A Wrong Checksum- Input
the bench gives a checksum that does not match
- Output
fail, code 2
The test must report what it found, not pass because it ran.
Constraints and Contract
- C on the host with the kit and soc_host.h; no C library.
- Exactly one console line,
sumand eight hex digits. - Fail with the codes of the statement; the bench may give a wrong checksum on purpose.
Hints
Hint 1
Which byte of the 24-bit reply came first from the flash?
Hint 2
Without the rewind, where does the second read start?
Hint 3
n need not be a multiple of 3: the last frame brings bytes you do not keep.
What the grader checks: Your C runs on the host in a process of its own against the pre-built ap_soc v1.5 bench with +core=bfm. Each case loads its own flash image and TB_ARG. Cases check the console line, the status word and that enough SPI frames ran. One hidden case gives a wrong checksum and expects fail code 2. The grader cannot see your layers, only what the chip and the bench saw.
5 checks on ap_soc v1.5 · host C · server. Run the examples first, then check the full regression. Full checks use the server judge. Downloads contain your text only; the chip, the bench and the hidden checks stay on the server. A passing result establishes the tested cases, not a proof for all possible inputs.
See all 2 problems on this path