4 #include <avr/interrupt.h>
8 #include "suspend_avr.h"
20 #endif /* AUDIO_ENABLE */
24 #define wdt_intr_enable(value) \
25 __asm__ __volatile__ ( \
26 "in __tmp_reg__,__SREG__" "\n\t" \
30 "out __SREG__,__tmp_reg__" "\n\t" \
33 : "M" (_SFR_MEM_ADDR(_WD_CONTROL_REG)), \
34 "r" (_BV(_WD_CHANGE_BIT) | _BV(WDE)), \
35 "r" ((uint8_t) ((value & 0x08 ? _WD_PS3_MASK : 0x00) | \
36 _BV(WDIE) | (value & 0x07)) ) \
41 void suspend_idle(uint8_t time)
44 set_sleep_mode(SLEEP_MODE_IDLE);
51 #ifndef NO_SUSPEND_POWER_DOWN
52 /* Power down MCU with watchdog timer
53 * wdto: watchdog timer timeout defined in <avr/wdt.h>
65 static uint8_t wdt_timeout = 0;
67 static void power_down(uint8_t wdto)
70 if (USB_DeviceState == DEVICE_STATE_Configured) return;
74 // Watchdog Interrupt Mode
75 wdt_intr_enable(wdto);
77 #ifdef BACKLIGHT_ENABLE
81 // Turn off LED indicators
85 // This sometimes disables the start-up noise, so it's been disabled
87 #endif /* AUDIO_ENABLE */
89 // TODO: more power saving
90 // See PicoPower application note
91 // - I/O port input with pullup
94 // - Power Reduction Register PRR
95 set_sleep_mode(SLEEP_MODE_PWR_DOWN);
101 // Disable watchdog after sleep
106 void suspend_power_down(void)
108 #ifndef NO_SUSPEND_POWER_DOWN
109 power_down(WDTO_15MS);
113 __attribute__ ((weak)) void matrix_power_up(void) {}
114 __attribute__ ((weak)) void matrix_power_down(void) {}
115 bool suspend_wakeup_condition(void)
120 for (uint8_t r = 0; r < MATRIX_ROWS; r++) {
121 if (matrix_get_row(r)) return true;
126 // run immediately after wakeup
127 void suspend_wakeup_init(void)
129 // clear keyboard state
131 #ifdef BACKLIGHT_ENABLE
134 led_set(host_keyboard_leds());
137 #ifndef NO_SUSPEND_POWER_DOWN
138 /* watchdog timeout */
141 // compensate timer for sleep
142 switch (wdt_timeout) {
144 timer_count += 15 + 2; // WDTO_15MS + 2(from observation)