Capstone Step: The Watchdog Joins Bring-Up
Four steps pass. Now arm the guard that resets the chip when firmware stops. It must see a real failure, and leave a note about it.
You Will Be Able To
- Explain why the watchdog is armed last, and arm it with its key sequence.
- Place and pace the kick so that it proves that the main program is alive.
- Use the early warning to record where a program failed, for the next boot.
- Lesson
- S6.18
- Time
- 25 min
- Runs on
- Your browser
- Check
- 6 questions
A field unit resets itself about once a day. The log after each boot says only "started". Nobody knows what failed, because the reset wiped the evidence.
Before You Start
You need The Watchdog: the counter, the kick, the window, the early warning and the reset. This page does not teach them again.
You need Firmware Bring-Up: steps 1 to 4, and the claim of ap_plic. mtime comes from Timers.
| Question | |
|---|---|
| 1 | Why is the watchdog armed last in bring-up? |
| 2 | How is the watchdog of ap_soc armed, and can firmware disarm it? |
| 3 | Where should the kick be, and how often? |
| 4 | What happens, tick by tick, when the main program fails? |
| 5 | What can firmware learn from a watchdog reset? |
The Idea: The Last Step Guards the Others
A watchdog resets the chip when software stops kicking it. During bring-up that is a nuisance. A slow or failing step would be cut short by a reset, and the evidence would vanish.
So the watchdog is armed last, as step 5. It joins only when steps 1 to 4 pass. From then on, it must catch a real failure of the main program.
Two things make that work. The kick must come from the main loop, paced by time, so that only a living main can kick. And the early warning must be used to write down what went wrong, before the reset.
New Words
| Register | Address | What a write does |
|---|---|---|
| KEY | 0x1000_3000 | 0x51, then 0xA2 on the next watchdog write: unlocks the next register once |
| CONTROL | 0x1000_3004 | 1, while unlocked: arms the watchdog |
| SERVICE | 0x1000_3008 | 1: the kick |
The watchdog of ap_soc, from watchdog_supervisor.sv. It has no register to read.
| Word | Meaning |
|---|---|
| Armed watchdog | A watchdog that has been started. In ap_soc it is armed once, and then only a reset stops it. |
| Key sequence | Two fixed values written to the KEY register, one right after the other. Only then does a write to CONTROL arm the watchdog. |
| Supervision tick | One step of the watchdog counter. In the lab there is one in every 32 ticks of mtime. |
| Pacing | Waiting for a set time before an action, so that it happens at a steady rate. |
| Failure path | The code that runs when a check fails. Here it is a loop that never ends, like "while (1);" in C. |
| Last words | A small record that the warning handler leaves in SRAM: the source, and the address where the program was. |
One Picture: Step 5
- Control / FSM
- Clock / reset
- Bus
- Processor
- Memory
- Debug
- Arm, last
Steps 1 to 4 have passed. Main writes two keys in a row, then 1 to CONTROL. From now on nothing can disarm the watchdog.
- Kick, on time
After each job, main waits for the time of the next kick and kicks. 128 ticks are four supervision ticks, inside the window.
- Main fails
Job 3 fails, and main stays in a loop for ever. No kick comes. The watchdog loses 1 at every supervision tick.
- The warning
The tick that finds 1 left raises the early warning. It is source 6, so ap_plic interrupts the processor.
- Last words
The handler stores the source and mepc, the address where main was stuck, in SRAM. Then it waits.
- The reset
Two supervision ticks after the warning, the watchdog asks for a reset. SRAM keeps the last words for the next boot.
Read the diagram as text
- Main loop: one job, then a kick when its time comes
- Watchdog: loads 6 at a kick; loses 1 at each supervision tick
- ap_plic: the early warning is source 6
- Warning handler: claims 6 and reads mepc
- SRAM: keeps its words through a reset
- Reset request: two supervision ticks after the warning
- Main loop → Watchdog: keys, then CONTROL
- Main loop → Watchdog: a kick every 128 ticks
- Watchdog → ap_plic: early warning
- ap_plic → Warning handler: external request
- Warning handler → SRAM: last words
- Watchdog → Reset request: reset request
Bring-up step 5 on ap_soc. The lab runs both stories: the healthy run, and job 3 failing.
The first two steps of the picture are the healthy run. The last four are what the watchdog is for.
The Smallest Example, Worked by Hand
The watchdog loads 6 at a kick. Each supervision tick takes 1 away. A kick is early while more than 3 are left. The tick that finds 1 left raises the warning.
Main kicks every 128 ticks of mtime. That is 128 / 32 = 4 supervision ticks. So each kick finds 6 - 4 = 2 left: inside the window.
Now job 3 fails right after a kick. Count the supervision ticks after that last kick.
| Supervision tick | Left after it | What happens |
|---|---|---|
| 1 | 5 | nothing |
| 2 | 4 | nothing |
| 3 | 3 | a kick would now be on time; none comes |
| 4 | 2 | nothing |
| 5 | 1 | nothing |
| 6 | - | it finds 1 left: the early warning |
| 7 | - | one tick of grace |
| 8 | - | the reset request |
So the warning comes six supervision ticks after the last kick: at most 6 × 32 = 192 ticks of mtime, and more than 5 × 32 = 160. The reset request follows 2 × 32 = 64 ticks later.
The Rules
Rule 1: Arm the watchdog last.
Only after steps 1 to 4 pass. A reset during a slow step hides the fault you are looking for.
Rule 2: Arm it with the key sequence.
In ap_soc: 0x51 and then 0xA2 to KEY, with no other watchdog write between, then 1 to CONTROL.
Rule 3: Kick from the main loop, after its work.
Never from a timer handler. A timer keeps running when main has failed.
Rule 4: Pace the kicks by time, inside the window.
In the lab: every 3 to 5 supervision ticks. The lab uses 4.
Rule 5: Use the warning for last words.
Record the source and mepc in SRAM, then wait for the reset.
Rule 6: Read the last words at the next boot.
Keep them where start-up code does not clear, and read the reset cause too.
Sources for These Rules
The key values, the window of 3, the grace of 2 and the arm-once rule come from watchdog_supervisor.sv. The address 0x1000_3000 is in map.json. These are our own choices.
The SRAM model of ap_soc, ap_sram.sv, has no reset input, so its words survive a watchdog reset. The reset cause register of ap_sysctl marks a watchdog reset in bit 2.
Rules 1, 3, 5 and 6 are common practice. No source in our library lists them.
Now Run It
The lab at the end of this page is step 5. The watchdog of ap_soc, ap_plic and the ap_clint timer are the chip files, unchanged. The processor is the model of S6.16.
t=26 | watchdog armed, 6 ticks left
t=158 | watchdog kick, 2 ticks were left
t=286 | watchdog kick, 2 ticks were left
t=414 | watchdog kick, 2 ticks were left
t=542 | watchdog kick, 2 ticks were left
t=670 | watchdog kick, 2 ticks were left
t=798 | watchdog kick, 2 ticks were left
t=926 | watchdog kick, 2 ticks were left
t=1054 | watchdog kick, 2 ticks were left
t=1182 | watchdog kick, 2 ticks were left
still running after 1200 instructions, 0 exceptions and 0 interrupts
jobs=10 kicks=9 | armed=1 warning=0 reset=0Rules 2 and 4 at work. The watchdog arms with 6 left. Every kick finds 2 left, as worked out by hand. No warning, no reset.
Now the same program, with job 3 failing. Main enters its failure path, a loop that never ends.
t=26 | watchdog armed, 6 ticks left
t=158 | watchdog kick, 2 ticks were left
t=286 | watchdog kick, 2 ticks were left
t=448 | watchdog early warning: source 6 asks ap_plic
t=449 | interrupt, external: mcause=8000000b mepc=000000a8, next is 000000ac
t=449 | PLIC claim returns 6
t=512 | watchdog reset request: the chip resets here
jobs=3 kicks=2 | last words: source 6 at 000000a8 | armed=1 warning=1 reset=1The last kick is at 286. The warning comes at 448, which is 162 ticks later: between 160 and 192, as the hand table said. The reset request follows 64 ticks after the warning.
Rule 5 at work. The handler claims source 6, and stores mepc. mepc is 000000a8, the address of the failure path. The next boot can read exactly where main was stuck.
A probe ran these programs on the real five-stage ap_rv32, with the same chip files behind its bus. The final results were equal to the model.
uart= gpio=0000 ram= end=time exceptions=0 armed=1 warning=0 reset=0
uart= gpio=0000 ram=00000003,00000002,000000a8,00000006 end=reset exceptions=0 armed=1 warning=1 reset=1
uart= gpio=0000 ram=00000003 end=time exceptions=0 armed=1 warning=0 reset=0This lab leaves things out. The reset itself is not modelled: the run stops at the request. The next boot, which reads the last words, is not run.
Why It Is Built This Way
| Design choice | The alternative | What the choice buys, and costs |
|---|---|---|
| A key sequence before arming | One write arms | A stray write cannot arm it. Firmware writes three words, in order. |
| Armed until reset | Software may disarm it | A failed program cannot switch off its own guard. So bring-up must arm it last. |
| An early warning before the reset | Reset at once | A handler gets two ticks to leave last words. The watchdog needs an interrupt line. |
| A window for the kick | Any kick counts | A loop stuck on the kick itself is caught. Firmware must pace its kicks. |
The Trap: A Watchdog That Never Barks
Back to the field unit. Say the team moves the kick into the timer handler, to be safe. The resets stop. Here is that program, with the same failure of job 3.
t=33 | watchdog armed, 6 ticks left
t=147 | interrupt, machine timer: mcause=80000007 mepc=000000a4, next is 000000b4
t=155 | watchdog kick, 3 ticks were left
t=159 | mret: back to 000000a4
t=275 | interrupt, machine timer: mcause=80000007 mepc=000000a4, next is 000000b4jobs=3 kicks=9 | armed=1 warning=0 reset=0Three jobs, then main is stuck for good. Yet the kicks go on, nine of them, and there is no warning. The timer handler proves only that the timer works.
The unit no longer resets. It hangs instead, and nobody is told. Put the kick back in the main loop, after the work, as rule 3 says.
At Scale
This section looks ahead. A product has more than one loop to watch, and a debugger on the bench.
Many tasks, one kick
Each task sets a flag when it completes its work. One place kicks only when every flag is set, then clears them all.
The debugger
A core stopped by a debugger cannot kick. In ap_soc the watchdog then pauses only in development mode. Otherwise a long stop ends in a reset.
A section that start-up skips
Last words need a section that start-up code does not clear. The linker script of S6.17 can place one.
Seconds, not ticks
A real watchdog counts in milliseconds or seconds, on a clock that keeps running when the main clock fails.
Try It
Run the lab. Count the supervision ticks between two kicks, from the times after "t=".
Change both rows that add 128 to x21 so that they add 64. Predict the line of the first kick, then run.
Put 128 back. Delete the kick: the store of x5 to offset 8 of x9. Predict when the warning comes.
Put the kick back. Write 0xa3 as the second key. Predict whether the watchdog arms.
t=26 | watchdog armed, 6 ticks left t=158 | watchdog kick, 2 ticks were left t=286 | watchdog kick, 2 ticks were left t=414 | watchdog kick, 2 ticks were left t=542 | watchdog kick, 2 ticks were left t=670 | watchdog kick, 2 ticks were left t=798 | watchdog kick, 2 ticks were left t=926 | watchdog kick, 2 ticks were left t=1054 | watchdog kick, 2 ticks were left t=1182 | watchdog kick, 2 ticks were left still running after 1200 instructions, 0 exceptions and 0 interrupts jobs=10 kicks=9 | armed=1 warning=0 reset=0Read included file: ap_plic.sv
// One machine context, synchronous level sources. Register-side interface:
// commit pulses once at a successful word transfer's data edge.
module ap_plic #(parameter integer SOURCES = 16) (
input logic clk, reset_n, commit, write_en,
input logic [21:0] offset,
input logic [31:0] wdata,
input logic [SOURCES:1] level_irq,
output logic [31:0] rdata,
output logic notify_irq
);
localparam ENABLE = 'h2000, THRESHOLD = 'h200000, CLAIM = 'h200004;
logic [2:0] weight [1:SOURCES];
logic [2:0] threshold, best;
logic [SOURCES:1] pending, busy, enabled;
integer winner;
always_comb begin
best = 0;
winner = 0;
for (int id = 1; id <= SOURCES; id++) begin
if (pending[id] && enabled[id] && weight[id] > best) begin
best = weight[id];
winner = id;
end
end
notify_irq = best > threshold;
rdata = 0;
for (int id = 1; id <= SOURCES; id++) begin
if (offset == 4*id) rdata = weight[id];
if (offset == 'h1000 + 4*(id/32)) rdata[id%32] = pending[id];
if (offset == ENABLE + 4*(id/32)) rdata[id%32] = enabled[id];
end
case (offset)
THRESHOLD: rdata = threshold;
CLAIM: rdata = winner;
endcase
end
always_ff @(posedge clk) begin
if (!reset_n) begin
pending <= '0;
busy <= '0;
enabled <= '0;
threshold <= 0;
for (int id = 1; id <= SOURCES; id++) weight[id] <= 0;
end else begin
for (int id = 1; id <= SOURCES; id++) begin
if (level_irq[id] && !busy[id]) begin
pending[id] <= 1;
busy[id] <= 1;
end
if (commit && write_en && offset == 4*id) weight[id] <= wdata[2:0];
if (commit && write_en && offset == ENABLE + 4*(id/32))
enabled[id] <= wdata[id%32];
if (commit && offset == CLAIM) begin
if (!write_en && winner == id) pending[id] <= 0;
if (write_en && enabled[id] && wdata == id && busy[id] && !pending[id]) busy[id] <= 0;
end
end
if (commit && write_en && offset == THRESHOLD) threshold <= wdata[2:0];
end
end
endmodule
Read included file: ap_clint_timer.sv
// Timer slice of ap_soc's local interrupt block. One hart, one timebase tick
// per clk and a registered interrupt output. Word strobes arrive at commitment.
module ap_clint_timer #(parameter logic [63:0] RESET_COMPARE = '1) (
input logic clk, reset_n, write_cmp_low, write_cmp_high,
input logic [31:0] wdata,
output logic [63:0] mtime, mtimecmp,
output logic mtip
);
always_ff @(posedge clk) begin
if (!reset_n) begin
mtime <= 0;
mtimecmp <= RESET_COMPARE;
mtip <= 0;
end else begin
mtime <= mtime + 1'b1;
if (write_cmp_low) mtimecmp[31:0] <= wdata;
if (write_cmp_high) mtimecmp[63:32] <= wdata;
mtip <= mtime >= mtimecmp;
end
end
endmodule
Read included file: watchdog_supervisor.sv
// ap_soc teaching policy. Input tick is an enable from the supervision clock.
// A privileged boot writer may arm once. Reset request persists until reset.
module watchdog_supervisor (
input logic clk, reset_n, tick, debug_halt, dev_mode, privileged,
input logic psel, penable, pwrite,
input logic [11:0] offset,
input logic [31:0] pwdata,
output logic armed, irq, reset_req,
output logic [3:0] remaining
);
localparam KEY = 0, CONTROL = 4, SERVICE = 8;
localparam RELOAD = 6, OPEN = 3;
logic first_key, unlocked;
logic [1:0] grace;
wire write_word = psel && penable && pwrite;
wire arm = write_word && privileged && unlocked && offset == CONTROL && pwdata == 1;
wire kick = write_word && offset == SERVICE && pwdata == 1;
always_ff @(posedge clk) begin
if (!reset_n) begin
first_key <= 0;
unlocked <= 0;
armed <= 0;
remaining <= 0;
irq <= 0;
reset_req <= 0;
grace <= 0;
end else begin
if (write_word) begin
first_key <= privileged && offset == KEY && pwdata == 'h51;
unlocked <= privileged && first_key && offset == KEY && pwdata == 'ha2;
end
if (!armed && arm) begin
armed <= 1;
remaining <= RELOAD;
end else if (armed && !reset_req && !(dev_mode && debug_halt)) begin
if (irq) begin
if (tick && grace == 1) reset_req <= 1;
else if (tick) grace <= grace - 1'b1;
end else if ((kick && remaining > OPEN) || (tick && remaining == 1 && !kick)) begin
irq <= 1;
grace <= 2;
remaining <= 0;
end else if (kick) remaining <= RELOAD;
else if (tick) remaining <= remaining - 1'b1;
end
end
end
endmodule
Check Yourself
Common Mistakes
Wrong Arm the watchdog in the first bring-up program.
Every slow or failing step is cut short by a reset, and its evidence is lost.
Fix Arm it last, once steps 1 to 4 pass.
Wrong Kick from a timer handler.
The timer keeps running when main has failed, so the watchdog never sees the failure.
Fix Kick from the main loop, after the work of a pass.
Wrong Kick as often as possible.
A kick with more than 3 left is early, and raises the warning.
Fix Pace the kicks by time, inside the window.
Remember
- Arm the watchdog last in bring-up, with its key sequence. In ap_soc only a reset stops it.
- Kick from the main loop, after its work, paced by time inside the window.
- A kick from a timer handler hides a failure of main.
- The warning comes on the sixth supervision tick without a kick; the reset request two ticks later.
- Use the warning to leave last words in SRAM for the next boot.