4 #include <avr/interrupt.h>
8 #include "suspend_avr.h"
19 #endif /* AUDIO_ENABLE */
23 #define wdt_intr_enable(value) \
24 __asm__ __volatile__ ( \
25 "in __tmp_reg__,__SREG__" "\n\t" \
29 "out __SREG__,__tmp_reg__" "\n\t" \
32 : "M" (_SFR_MEM_ADDR(_WD_CONTROL_REG)), \
33 "r" (_BV(_WD_CHANGE_BIT) | _BV(WDE)), \
34 "r" ((uint8_t) ((value & 0x08 ? _WD_PS3_MASK : 0x00) | \
35 _BV(WDIE) | (value & 0x07)) ) \
40 void suspend_idle(uint8_t time)
43 set_sleep_mode(SLEEP_MODE_IDLE);
50 /* Power down MCU with watchdog timer
51 * wdto: watchdog timer timeout defined in <avr/wdt.h>
63 static uint8_t wdt_timeout = 0;
64 static void power_down(uint8_t wdto)
67 if (USB_DeviceState == DEVICE_STATE_Configured) return;
71 // Watchdog Interrupt Mode
72 wdt_intr_enable(wdto);
74 #ifdef BACKLIGHT_ENABLE
78 // Turn off LED indicators
83 #endif /* AUDIO_ENABLE */
85 // TODO: more power saving
86 // See PicoPower application note
87 // - I/O port input with pullup
90 // - Power Reduction Register PRR
91 set_sleep_mode(SLEEP_MODE_PWR_DOWN);
97 // Disable watchdog after sleep
101 void suspend_power_down(void)
103 power_down(WDTO_15MS);
106 __attribute__ ((weak)) void matrix_power_up(void) {}
107 __attribute__ ((weak)) void matrix_power_down(void) {}
108 bool suspend_wakeup_condition(void)
110 #ifdef BACKLIGHT_ENABLE
116 for (uint8_t r = 0; r < MATRIX_ROWS; r++) {
117 if (matrix_get_row(r)) return true;
122 // run immediately after wakeup
123 void suspend_wakeup_init(void)
125 // clear keyboard state
127 #ifdef BACKLIGHT_ENABLE
131 led_set(host_keyboard_leds());
134 #ifndef NO_SUSPEND_POWER_DOWN
135 /* watchdog timeout */
138 // compensate timer for sleep
139 switch (wdt_timeout) {
141 timer_count += 15 + 2; // WDTO_15MS + 2(from observation)