|
| 1 | +/** |
| 2 | + * AVR8 Watchdog Timer Test Suite |
| 3 | + * Part of AVR8js |
| 4 | + * |
| 5 | + * Copyright (C) 2021 Uri Shaked |
| 6 | + */ |
| 7 | + |
| 8 | +import { AVRClock, clockConfig } from '..'; |
| 9 | +import { CPU } from '../cpu/cpu'; |
| 10 | +import { asmProgram, TestProgramRunner } from '../utils/test-utils'; |
| 11 | +import { AVRWatchdog, watchdogConfig } from './watchdog'; |
| 12 | + |
| 13 | +const R20 = 20; |
| 14 | + |
| 15 | +const MCUSR = 0x54; |
| 16 | +const WDRF = 1 << 3; |
| 17 | + |
| 18 | +const WDTCSR = 0x60; |
| 19 | +const WDP0 = 1 << 0; |
| 20 | +const WDP1 = 1 << 1; |
| 21 | +const WDP2 = 1 << 2; |
| 22 | +const WDE = 1 << 3; |
| 23 | +const WDCE = 1 << 4; |
| 24 | +const WDP3 = 1 << 5; |
| 25 | +const WDIE = 1 << 6; |
| 26 | + |
| 27 | +const INT_WDT = 0xc; |
| 28 | + |
| 29 | +describe('Watchdog', () => { |
| 30 | + it('should correctly calculate the prescaler from WDTCSR', () => { |
| 31 | + const cpu = new CPU(new Uint16Array(1024)); |
| 32 | + const clock = new AVRClock(cpu, 16e6, clockConfig); |
| 33 | + const watchdog = new AVRWatchdog(cpu, watchdogConfig, clock); |
| 34 | + cpu.writeData(WDTCSR, WDCE | WDE); |
| 35 | + cpu.writeData(WDTCSR, 0); |
| 36 | + expect(watchdog.prescaler).toEqual(2048); |
| 37 | + cpu.writeData(WDTCSR, WDP2 | WDP1 | WDP0); |
| 38 | + expect(watchdog.prescaler).toEqual(256 * 1024); |
| 39 | + cpu.writeData(WDTCSR, WDP3 | WDP0); |
| 40 | + expect(watchdog.prescaler).toEqual(1024 * 1024); |
| 41 | + }); |
| 42 | + |
| 43 | + it('should not change the prescaler unless WDCE is set', () => { |
| 44 | + const cpu = new CPU(new Uint16Array(1024)); |
| 45 | + const clock = new AVRClock(cpu, 16e6, clockConfig); |
| 46 | + const watchdog = new AVRWatchdog(cpu, watchdogConfig, clock); |
| 47 | + cpu.writeData(WDTCSR, 0); |
| 48 | + expect(watchdog.prescaler).toEqual(2048); |
| 49 | + cpu.writeData(WDTCSR, WDP2 | WDP1 | WDP0); |
| 50 | + expect(watchdog.prescaler).toEqual(2048); |
| 51 | + |
| 52 | + cpu.writeData(WDTCSR, WDCE | WDE); |
| 53 | + cpu.cycles += 5; // WDCE should expire after 4 cycles |
| 54 | + cpu.writeData(WDTCSR, WDP2 | WDP1 | WDP0); |
| 55 | + expect(watchdog.prescaler).toEqual(2048); |
| 56 | + }); |
| 57 | + |
| 58 | + it('should reset the CPU when the timer expires', () => { |
| 59 | + const { program } = asmProgram(` |
| 60 | + ; register addresses |
| 61 | + _REPLACE WDTCSR, ${WDTCSR} |
| 62 | +
|
| 63 | + ; Setup watchdog |
| 64 | + ldi r16, ${WDE | WDCE} |
| 65 | + sts WDTCSR, r16 |
| 66 | + ldi r16, ${WDE} |
| 67 | + sts WDTCSR, r16 |
| 68 | + |
| 69 | + nop |
| 70 | +
|
| 71 | + break |
| 72 | + `); |
| 73 | + const cpu = new CPU(program); |
| 74 | + const clock = new AVRClock(cpu, 16e6, clockConfig); |
| 75 | + const watchdog = new AVRWatchdog(cpu, watchdogConfig, clock); |
| 76 | + const runner = new TestProgramRunner(cpu); |
| 77 | + |
| 78 | + // Setup: enable watchdog timer |
| 79 | + runner.runInstructions(4); |
| 80 | + expect(watchdog.enabled).toBe(true); |
| 81 | + |
| 82 | + // Now we skip 8ms. Watchdog shouldn't fire, yet |
| 83 | + cpu.cycles += 16000 * 8; |
| 84 | + runner.runInstructions(1); |
| 85 | + |
| 86 | + // Now we skip an extra 8ms. Watchdog should fire and reset! |
| 87 | + cpu.cycles += 16000 * 8; |
| 88 | + cpu.tick(); |
| 89 | + expect(cpu.pc).toEqual(0); |
| 90 | + expect(cpu.readData(MCUSR)).toEqual(WDRF); |
| 91 | + }); |
| 92 | + |
| 93 | + it('should extend the watchdog timeout when executing a WDR instruction', () => { |
| 94 | + const { program } = asmProgram(` |
| 95 | + ; register addresses |
| 96 | + _REPLACE WDTCSR, ${WDTCSR} |
| 97 | +
|
| 98 | + ; Setup watchdog |
| 99 | + ldi r16, ${WDE | WDCE} |
| 100 | + sts WDTCSR, r16 |
| 101 | + ldi r16, ${WDE} |
| 102 | + sts WDTCSR, r16 |
| 103 | + |
| 104 | + wdr |
| 105 | + nop |
| 106 | +
|
| 107 | + break |
| 108 | + `); |
| 109 | + const cpu = new CPU(program); |
| 110 | + const clock = new AVRClock(cpu, 16e6, clockConfig); |
| 111 | + const watchdog = new AVRWatchdog(cpu, watchdogConfig, clock); |
| 112 | + const runner = new TestProgramRunner(cpu); |
| 113 | + |
| 114 | + // Setup: enable watchdog timer |
| 115 | + runner.runInstructions(4); |
| 116 | + expect(watchdog.enabled).toBe(true); |
| 117 | + |
| 118 | + // Now we skip 8ms. Watchdog shouldn't fire, yet |
| 119 | + cpu.cycles += 16000 * 8; |
| 120 | + runner.runInstructions(1); |
| 121 | + |
| 122 | + // Now we skip an extra 8ms. We extended the timeout with WDR, so watchdog won't fire yet |
| 123 | + cpu.cycles += 16000 * 8; |
| 124 | + runner.runInstructions(1); |
| 125 | + |
| 126 | + // Finally, another 8ms bring us to 16ms since last WDR, and watchdog should fire |
| 127 | + cpu.cycles += 16000 * 8; |
| 128 | + cpu.tick(); |
| 129 | + expect(cpu.pc).toEqual(0); |
| 130 | + }); |
| 131 | + |
| 132 | + it('should fire an interrupt when the watchdog expires and WDIE is set', () => { |
| 133 | + const { program } = asmProgram(` |
| 134 | + ; register addresses |
| 135 | + _REPLACE WDTCSR, ${WDTCSR} |
| 136 | +
|
| 137 | + ; Setup watchdog |
| 138 | + ldi r16, ${WDE | WDCE} |
| 139 | + sts WDTCSR, r16 |
| 140 | + ldi r16, ${WDE | WDIE} |
| 141 | + sts WDTCSR, r16 |
| 142 | + |
| 143 | + nop |
| 144 | + sei |
| 145 | +
|
| 146 | + break |
| 147 | + `); |
| 148 | + const cpu = new CPU(program); |
| 149 | + const clock = new AVRClock(cpu, 16e6, clockConfig); |
| 150 | + const watchdog = new AVRWatchdog(cpu, watchdogConfig, clock); |
| 151 | + const runner = new TestProgramRunner(cpu); |
| 152 | + |
| 153 | + // Setup: enable watchdog timer |
| 154 | + runner.runInstructions(4); |
| 155 | + expect(watchdog.enabled).toBe(true); |
| 156 | + |
| 157 | + // Now we skip 8ms. Watchdog shouldn't fire, yet |
| 158 | + cpu.cycles += 16000 * 8; |
| 159 | + runner.runInstructions(1); |
| 160 | + |
| 161 | + // Now we skip an extra 8ms. Watchdog should fire and jump to the interrupt handler |
| 162 | + cpu.cycles += 16000 * 8; |
| 163 | + runner.runInstructions(1); |
| 164 | + |
| 165 | + expect(cpu.pc).toEqual(INT_WDT); |
| 166 | + // The watchdog timer should also clean the WDIE bit, so next timeout will reset the MCU. |
| 167 | + expect(cpu.readData(WDTCSR) & WDIE).toEqual(0); |
| 168 | + }); |
| 169 | + |
| 170 | + it('should not reset the CPU if the watchdog has been disabled', () => { |
| 171 | + const { program } = asmProgram(` |
| 172 | + ; register addresses |
| 173 | + _REPLACE WDTCSR, ${WDTCSR} |
| 174 | +
|
| 175 | + ; Setup watchdog |
| 176 | + ldi r16, ${WDE | WDCE} |
| 177 | + sts WDTCSR, r16 |
| 178 | + ldi r16, ${WDE} |
| 179 | + sts WDTCSR, r16 |
| 180 | + |
| 181 | + ; disable watchdog |
| 182 | + ldi r16, ${WDE | WDCE} |
| 183 | + sts WDTCSR, r16 |
| 184 | + ldi r16, 0 |
| 185 | + sts WDTCSR, r16 |
| 186 | +
|
| 187 | + ldi r20, 55 |
| 188 | +
|
| 189 | + break |
| 190 | + `); |
| 191 | + const cpu = new CPU(program); |
| 192 | + const clock = new AVRClock(cpu, 16e6, clockConfig); |
| 193 | + const watchdog = new AVRWatchdog(cpu, watchdogConfig, clock); |
| 194 | + const runner = new TestProgramRunner(cpu); |
| 195 | + |
| 196 | + // Setup: enable watchdog timer |
| 197 | + runner.runInstructions(4); |
| 198 | + expect(watchdog.enabled).toBe(true); |
| 199 | + |
| 200 | + // Now we skip 8ms. Watchdog shouldn't fire, yet. We disable it. |
| 201 | + cpu.cycles += 16000 * 8; |
| 202 | + runner.runInstructions(4); |
| 203 | + |
| 204 | + // Now we skip an extra 20ms. Watchdog shouldn't reset! |
| 205 | + cpu.cycles += 16000 * 20; |
| 206 | + runner.runInstructions(1); |
| 207 | + expect(cpu.pc).not.toEqual(0); |
| 208 | + expect(cpu.data[R20]).toEqual(55); // assert that `ldi r20, 55` ran |
| 209 | + }); |
| 210 | +}); |
0 commit comments