V9.7.1 The Watchdog as a System Test

HardVerify35 minap_soc v1.5 · firmware · serverReview the topic

Test the watchdog the way the product will use it. Arm it with its key, kick it inside its window, let a warm reset disarm it, and finally let it bite. The test passes on ap_soc and fails visibly on every faulty chip of a hidden set.

Hardware context: The watchdog of ap_soc v1.5 on clk_aon: key, window, early warning and reset. A watchdog test: disarm by a warm reset, kicks in the window, the bite.

A watchdog is a timer that resets the chip unless software kicks it in time. Firmware relies on it to recover from a hang. So the chip level tests it as a system: key, window, warning, reset and the record of the reset.

Run three boots. At power-on, check RESET_CAUSE is 0x01, arm the watchdog, and ask the board for a pin reset (TB_EVENT 2). After the pin reset, check the cause is 0x03. Then prove the warm reset disarmed it: no early warning may come for 1,500 cycles. Arm it again and kick it three times, each kick inside its window.

Then stop kicking. The early warning must come, and then the watchdog's reset. After it, check the cause is 0x07 and print watchdog ok. Keep your step in TB_ARG, which only power-on clears. Every wait needs a limit and a fail code.

A faulty chip is caught when your test writes a fail word, or a check of the bench fails. A run that ends at the cycle limit is not caught.

Full specification
RegisterAddressUse
KEY0x1000_30000x51, then 0xA2 on the next watchdog write, unlocks one write to CONTROL
CONTROL0x1000_3004Write 1 while unlocked: armed, 6 ticks left. Only a reset disarms it
SERVICE0x1000_3008Write 1: the kick. With more than 3 ticks left it is too early and raises the warning
PLIC PENDING0x0C00_1000Bit 6: the watchdog's early warning, interrupt source 6 (give it a priority first)
Fact of ap_soc v1.5Value
A tickone in 32 cycles of clk_aon: 448 ns, about 45 cycles of clk_sys
The warningon the tick that finds 1 left; the reset request follows two ticks later
A warm reset (pin, software, watchdog)resets the watchdog too: it is disarmed afterwards
All watchdog registerswrite-only; a read answers ERROR

Recorded on the good chip (V9.7 lab): three kicks 160 cycles of mtime apart were accepted. The reset came 321 cycles after the last kick. The window opens 3 ticks after an arm or a kick and closes before the sixth. The tick's phase against your kick is unknown, so aim for the middle.

Given Files

Read-only files supplied by the bench.

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

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

irq.h
#ifndef AP_IRQ_H
#define AP_IRQ_H
typedef void (*irq_handler)(void);
unsigned plic_claim(void);
void plic_complete(unsigned id);
void irq_enable(unsigned id, irq_handler handler);
unsigned long long mtime_read(void);
void mtime_set_compare(unsigned long long deadline);
void msip_enable(irq_handler handler);
void mtime_enable(irq_handler handler);
#endif

Watchdog · register manual
OffsetRegisterBitsFieldAccessResetHeld byMeaning
0x000KEY31:0valuewonot stored0x51, then 0xA2 on the next watchdog write, unlocks one write to CONTROL. Key word.
0x004CONTROL31:0valuewonot storedWrite 1 while unlocked: armed. Arms once. 1 arms the watchdog.
0x008SERVICE31:0valuewonot storedWrite 1: the kick. 1 is a kick.
ap_sysctl · register manual
OffsetRegisterBitsFieldAccessResetHeld byMeaning
0x000CHIP_ID31:0valuero0x2A5310C5constThe chip's identity: the IDCODE of the TAP. Fixed number.
0x004RESET_CAUSE0power_onroblockSticky record of reset sources since power-on. Power-on reset.
0x004RESET_CAUSE1pinroblockReset pin.
0x004RESET_CAUSE2watchdogroblockWatchdog.
0x004RESET_CAUSE3softwareroblockSoftware request.
0x004RESET_CAUSE4debugroblockDebug request.
0x008SOFT_RESET0timerwonot storedReset requests from software. 1 arms one restart of the timer.
0x008SOFT_RESET1chipwonot stored1 requests the software reset (new in v1.5).
0x00CSTRAPS0bootroblockStraps as sampled at power-up (new in v1.5). 0: start in the boot ROM. 1: start in SRAM.
0x010PINMUX31:0selectrw0x00000000regsTwo bits for each of sixteen pads: 0 released, 1 GPIO, 2 serial, 3 auxiliary. Pad n uses bits 2n+1 and 2n.
0x014CLK_GATE0uart0rw0x1regs1 lets that block's clock run (new in v1.5). UART0.
0x014CLK_GATE1gpiorw0x1regsGPIO.
0x014CLK_GATE2timerrw0x1regsTimer and PWM.
0x014CLK_GATE3spirw0x1regsSPI.
0x014CLK_GATE4dmarw0x1regsDMA.
0x018LOCK0pinmuxw1s0x0regsWrite-once locks. Only a reset clears one (new in v1.5). 1: PINMUX ignores writes.
0x018LOCK1clk_gatew1s0x0regs1: CLK_GATE ignores writes.
0x018LOCK2debugw1s0x0regs1: the debug lock is closed.
0x020TB_STATUS31:0valuerw0x00000000regs (power-on reset only)Scratch word 0. The firmware kit reports progress, pass and fail here. Plain storage.
0x024TB_PUTC31:0valuerw0x00000000regs (power-on reset only)Scratch word 1. The firmware kit writes one character here. Plain storage.
0x028TB_EVENT31:0valuerw0x00000000regs (power-on reset only)Scratch word 2. The firmware kit writes an event number here. Plain storage.
0x02CTB_ARG31:0valuerw0x00000000regs (power-on reset only)Scratch word 3. A bench leaves a word for the firmware here. Plain storage.
ap_plic · register manual
OffsetRegisterBitsFieldAccessResetHeld byMeaning
0x004 + 4 × (n − 1), n = 1 to 16PRIORITY2:0levelrw0x0blockPriority of source n, 0 to 7. 0 never competes. Priority.
0x001000PENDING16:1sourcesroblockPending bits; bit number = source ID. One bit for each source.
0x002000ENABLE16:1sourcesrw0x0000blockEnable bits; bit number = source ID. One bit for each source.
0x200000THRESHOLD2:0levelrw0x0blockThe core sees a request only above this priority. Threshold.
0x200004CLAIM4:0idrw0x00blockRead: claim, returns the winner's ID and clears its pending bit. Write: complete, names the ID that is done. Source ID.

Examples

Example 1

The Good Chip
Input
Power-on, a pin reset, kicks, then the bite
Output
console "watchdog ok"; pass

Three boots: power-on, the pin reset, the watchdog's reset. The cause word grows 0x01, 0x03, 0x07.

Example 2

A Kick Outside the Window
Input
your test, on the good chip
Output
a warning or a bite you did not plan

Too early raises the warning at once; too late lets the sixth tick warn. The good chip then fails your test.

Example 3

A Watchdog That Never Bites
Input
a hidden chip
Output
your fail word

Only a test that waits for the warning and the reset, with a limit, sees that nothing came.

Constraints and Contract

  • C firmware with the kit, SRAM layout.
  • Print exactly watchdog ok, only after the watchdog's reset.
  • mtime_read() from irq.h counts cycles of clk_sys; it restarts with every reset of the bus.

Hints

Hint 1

The watchdog's registers cannot be read. How can a test see that it is armed, or that it warned?

Hint 2

Why prove the disarm before you let it bite? What happens to a chip whose watchdog ignores warm resets, once it bites?

Hint 3

Kick too early and it warns; too late and it bites. Where is the middle of the window in cycles of clk_sys?

What the grader checks: Each case runs your firmware on the good chip and on every hidden faulty chip. The good chip must print the line and pass. Each faulty chip must end with a fail word or a bench check. The grader cannot see when your kicks land, only that the good chip accepts them.

2 checks on ap_soc v1.5 · firmware · 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 1 problem on this path

Report a problem with this page

Your code CSaved locally
Ln 1, Col 1Tab indents · Ctrl/⌘ + Enter runs samples
Not run yet

The Good Chip

Input
Power-on, a pin reset, kicks, then the bite
Expected output
console "watchdog ok"; pass

Three boots: power-on, the pin reset, the watchdog's reset. The cause word grows 0x01, 0x03, 0x07.

Run samples executes all 1 public checks. Check all tests runs 2 checks on the server.

ap_soc v1.5 · firmware · server