4 #include <avr/interrupt.h>
8 #include "suspend_avr.h"
20 #endif /* AUDIO_ENABLE */
22 #ifdef RGBLIGHT_ANIMATIONS
27 #define wdt_intr_enable(value) \
28 __asm__ __volatile__ ( \
29 "in __tmp_reg__,__SREG__" "\n\t" \
33 "out __SREG__,__tmp_reg__" "\n\t" \
36 : "M" (_SFR_MEM_ADDR(_WD_CONTROL_REG)), \
37 "r" (_BV(_WD_CHANGE_BIT) | _BV(WDE)), \
38 "r" ((uint8_t) ((value & 0x08 ? _WD_PS3_MASK : 0x00) | \
39 _BV(WDIE) | (value & 0x07)) ) \
44 /** \brief Suspend idle
48 void suspend_idle(uint8_t time)
51 set_sleep_mode(SLEEP_MODE_IDLE);
58 #ifndef NO_SUSPEND_POWER_DOWN
59 /** \brief Power down MCU with watchdog timer
61 * wdto: watchdog timer timeout defined in <avr/wdt.h>
73 static uint8_t wdt_timeout = 0;
79 static void power_down(uint8_t wdto)
82 if (USB_DeviceState == DEVICE_STATE_Configured) return;
86 // Watchdog Interrupt Mode
87 wdt_intr_enable(wdto);
89 #ifdef BACKLIGHT_ENABLE
93 // Turn off LED indicators
97 // This sometimes disables the start-up noise, so it's been disabled
99 #endif /* AUDIO_ENABLE */
100 #ifdef RGBLIGHT_SLEEP
101 #ifdef RGBLIGHT_ANIMATIONS
102 rgblight_timer_disable();
106 // TODO: more power saving
107 // See PicoPower application note
108 // - I/O port input with pullup
111 // - Power Reduction Register PRR
112 set_sleep_mode(SLEEP_MODE_PWR_DOWN);
118 // Disable watchdog after sleep
123 /** \brief Suspend power down
127 void suspend_power_down(void)
129 #ifndef NO_SUSPEND_POWER_DOWN
130 power_down(WDTO_15MS);
134 __attribute__ ((weak)) void matrix_power_up(void) {}
135 __attribute__ ((weak)) void matrix_power_down(void) {}
136 bool suspend_wakeup_condition(void)
141 for (uint8_t r = 0; r < MATRIX_ROWS; r++) {
142 if (matrix_get_row(r)) return true;
147 /** \brief run immediately after wakeup
151 void suspend_wakeup_init(void)
153 // clear keyboard state
155 #ifdef BACKLIGHT_ENABLE
158 led_set(host_keyboard_leds());
159 #ifdef RGBLIGHT_SLEEP
161 #ifdef RGBLIGHT_ANIMATIONS
162 rgblight_timer_enable();
167 #ifndef NO_SUSPEND_POWER_DOWN
168 /* watchdog timeout */
171 // compensate timer for sleep
172 switch (wdt_timeout) {
174 timer_count += 15 + 2; // WDTO_15MS + 2(from observation)