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 static void standby(void)
75 #ifdef SLEEP_MODE_STANDBY
76 set_sleep_mode(SLEEP_MODE_STANDBY);
84 static void idle(void)
86 set_sleep_mode(SLEEP_MODE_IDLE);
94 void suspend_idle(uint8_t time)
99 void suspend_power_down(void)
101 #ifdef NO_SUSPEND_POWER_DOWN
103 #elif defined(SUSPEND_MODE_NOPOWERSAVE)
105 #elif defined(SUSPEND_MODE_STANDBY)
107 #elif defined(SUSPEND_MODE_IDLE)
110 power_down(WDTO_15MS);
114 bool suspend_wakeup_condition(void)
119 for (uint8_t r = 0; r < MATRIX_ROWS; r++) {
120 if (matrix_get_row(r)) return true;
125 // run immediately after wakeup
126 void suspend_wakeup_init(void)
128 // clear keyboard state
131 #ifdef BACKLIGHT_ENABLE
136 #ifndef NO_SUSPEND_POWER_DOWN
137 /* watchdog timeout */
140 // compensate timer for sleep
141 switch (wdt_timeout) {
143 timer_count += 15 + 2; // WDTO_15MS + 2(from observation)