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 /* Power down MCU with watchdog timer
34 * wdto: watchdog timer timeout defined in <avr/wdt.h>
46 static uint8_t wdt_timeout = 0;
47 static void power_down(uint8_t wdto)
50 if (USB_DeviceState == DEVICE_STATE_Configured) return;
54 // Watchdog Interrupt Mode
55 wdt_intr_enable(wdto);
57 // TODO: more power saving
58 // See PicoPower application note
59 // - I/O port input with pullup
62 // - Power Reduction Register PRR
63 set_sleep_mode(SLEEP_MODE_PWR_DOWN);
69 // Disable watchdog after sleep
73 #ifdef SUSPEND_MODE_STANDBY
74 static void standby(void)
76 #ifdef SLEEP_MODE_STANDBY
77 set_sleep_mode(SLEEP_MODE_STANDBY);
86 static void idle(void)
88 set_sleep_mode(SLEEP_MODE_IDLE);
96 void suspend_idle(uint8_t time)
101 void suspend_power_down(void)
103 #ifdef NO_SUSPEND_POWER_DOWN
105 #elif defined(SUSPEND_MODE_NOPOWERSAVE)
107 #elif defined(SUSPEND_MODE_STANDBY)
109 #elif defined(SUSPEND_MODE_IDLE)
112 power_down(WDTO_15MS);
116 bool suspend_wakeup_condition(void)
121 for (uint8_t r = 0; r < MATRIX_ROWS; r++) {
122 if (matrix_get_row(r)) return true;
127 // run immediately after wakeup
128 void suspend_wakeup_init(void)
130 // clear keyboard state
133 #ifdef BACKLIGHT_ENABLE
138 #ifndef NO_SUSPEND_POWER_DOWN
139 /* watchdog timeout */
142 // compensate timer for sleep
143 switch (wdt_timeout) {
145 timer_count += 15 + 2; // WDTO_15MS + 2(from observation)