4 #include <avr/interrupt.h>
8 #include "suspend_avr.h"
16 #define wdt_intr_enable(value) \
17 __asm__ __volatile__ ( \
18 "in __tmp_reg__,__SREG__" "\n\t" \
22 "out __SREG__,__tmp_reg__" "\n\t" \
25 : "M" (_SFR_MEM_ADDR(_WD_CONTROL_REG)), \
26 "r" (_BV(_WD_CHANGE_BIT) | _BV(WDE)), \
27 "r" ((uint8_t) ((value & 0x08 ? _WD_PS3_MASK : 0x00) | \
28 _BV(WDIE) | (value & 0x07)) ) \
33 void suspend_idle(uint8_t time)
36 set_sleep_mode(SLEEP_MODE_IDLE);
43 /* Power down MCU with watchdog timer
44 * wdto: watchdog timer timeout defined in <avr/wdt.h>
56 static uint8_t wdt_timeout = 0;
57 static void power_down(uint8_t wdto)
60 if (USB_DeviceState == DEVICE_STATE_Configured) return;
64 // Watchdog Interrupt Mode
65 wdt_intr_enable(wdto);
67 // TODO: more power saving
68 // See PicoPower application note
69 // - I/O port input with pullup
72 // - Power Reduction Register PRR
73 set_sleep_mode(SLEEP_MODE_PWR_DOWN);
79 // Disable watchdog after sleep
83 void suspend_power_down(void)
85 power_down(WDTO_15MS);
88 bool suspend_wakeup_condition(void)
93 for (uint8_t r = 0; r < MATRIX_ROWS; r++) {
94 if (matrix_get_row(r)) return true;
99 // run immediately after wakeup
100 void suspend_wakeup_init(void)
102 // clear keyboard state
104 #ifdef BACKLIGHT_ENABLE
109 #ifndef NO_SUSPEND_POWER_DOWN
110 /* watchdog timeout */
113 // compensate timer for sleep
114 switch (wdt_timeout) {
116 timer_count += 15 + 2; // WDTO_15MS + 2(from observation)