fix: implement WDT-driven sleep mode with debug toggle #41

Merged
daru merged 2 commits from fix/wdt-sleep-mode into main 2026-08-08 18:56:44 +00:00
Contributor

Fixes #40

Summary: Enables WDT as the wakeup source for deep sleep, replacing the __delay_ms(4000) static delay. Adds a DEBUG_MODE define to toggle between WDT sleep and static delay.

Changes:

  • Uncommented #pragma config WDTE = ON — WDT always on, including during Sleep
  • init_wdt(0b10010) — 256s prescaler (1:8388608 from 31kHz LFINTOSC)
  • static volatile uint8_t wdt_counter — persists across sleep cycles
  • go_to_sleep() in normal mode; __delay_ms(4000) in debug mode
  • stop_fvr_tmp_adc() now called before sleep to minimize current
  • wdt_clear() called before delay in debug mode (WDT is always active with WDTE=ON)

Verified against datasheet (PIC12LF1840T39A):

  • WDT cleared on entry to and exit from Sleep (datasheet §9.0, §10.4)
  • WDT timeout during Sleep wakes device without Reset — TO=0, PD=0 (datasheet §10.4)
  • Prescaler 0b10010 = 1:8388608 → 256s typical interval (datasheet §10.2)

With MAX_COUNTER=3, effective update period ≈ 4×256s = 17.7 min

Fixes #40 **Summary:** Enables WDT as the wakeup source for deep sleep, replacing the `__delay_ms(4000)` static delay. Adds a `DEBUG_MODE` define to toggle between WDT sleep and static delay. **Changes:** - Uncommented `#pragma config WDTE = ON` — WDT always on, including during Sleep - `init_wdt(0b10010)` — 256s prescaler (1:8388608 from 31kHz LFINTOSC) - `static volatile uint8_t wdt_counter` — persists across sleep cycles - `go_to_sleep()` in normal mode; `__delay_ms(4000)` in debug mode - `stop_fvr_tmp_adc()` now called before sleep to minimize current - `wdt_clear()` called before delay in debug mode (WDT is always active with WDTE=ON) **Verified against datasheet (PIC12LF1840T39A):** - WDT cleared on entry to and exit from Sleep (datasheet §9.0, §10.4) - WDT timeout during Sleep wakes device without Reset — TO=0, PD=0 (datasheet §10.4) - Prescaler 0b10010 = 1:8388608 → 256s typical interval (datasheet §10.2) **With MAX_COUNTER=3, effective update period ≈ 4×256s = 17.7 min**
The WDT (256s prescaler, WDTE=ON) now serves as the primary wakeup source
during deep sleep, replacing the static __delay_ms(4000). Verified against
the PIC12LF1840 datasheet: WDT is auto-cleared on sleep entry/exit, and a
timeout during sleep wakes the device without a Reset (TO=0, PD=0).

Added DEBUG_MODE define (default 0) to switch between WDT sleep and static
delay for debugging. Made wdt_counter static volatile so it persists across
sleep cycles. Also enabled stop_fvr_tmp_adc() before sleep to minimize
current draw.
daru merged commit 380c7b1250 into main 2026-08-08 18:56:44 +00:00
daru deleted branch fix/wdt-sleep-mode 2026-08-08 18:56:49 +00:00
Sign in to join this conversation.
No reviewers
No milestone
No project
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set

Reference
daru/PICsense!41
No description provided.