4 #include <avr/interrupt.h>
8 #include "suspend_avr.h"
13 #include "rgblight_reconfig.h"
21 #endif /* AUDIO_ENABLE */
23 #if defined(RGBLIGHT_SLEEP) && defined(RGBLIGHT_ENABLE)
28 #define wdt_intr_enable(value) \
29 __asm__ __volatile__ ( \
30 "in __tmp_reg__,__SREG__" "\n\t" \
34 "out __SREG__,__tmp_reg__" "\n\t" \
37 : "M" (_SFR_MEM_ADDR(_WD_CONTROL_REG)), \
38 "r" (_BV(_WD_CHANGE_BIT) | _BV(WDE)), \
39 "r" ((uint8_t) ((value & 0x08 ? _WD_PS3_MASK : 0x00) | \
40 _BV(WDIE) | (value & 0x07)) ) \
45 /** \brief Suspend idle
49 void suspend_idle(uint8_t time)
52 set_sleep_mode(SLEEP_MODE_IDLE);
60 // TODO: This needs some cleanup
62 /** \brief Run keyboard level Power down
66 __attribute__ ((weak))
67 void suspend_power_down_user (void) { }
68 /** \brief Run keyboard level Power down
72 __attribute__ ((weak))
73 void suspend_power_down_kb(void) {
74 suspend_power_down_user();
77 #ifndef NO_SUSPEND_POWER_DOWN
78 /** \brief Power down MCU with watchdog timer
80 * wdto: watchdog timer timeout defined in <avr/wdt.h>
92 static uint8_t wdt_timeout = 0;
98 static void power_down(uint8_t wdto)
101 if (USB_DeviceState == DEVICE_STATE_Configured) return;
105 // Watchdog Interrupt Mode
106 wdt_intr_enable(wdto);
108 #ifdef BACKLIGHT_ENABLE
112 // Turn off LED indicators
116 // This sometimes disables the start-up noise, so it's been disabled
118 #endif /* AUDIO_ENABLE */
119 #if defined(RGBLIGHT_SLEEP) && defined(RGBLIGHT_ENABLE)
120 #ifdef RGBLIGHT_ANIMATIONS
121 rgblight_timer_disable();
123 rgblight_disable_noeeprom();
125 suspend_power_down_kb();
127 // TODO: more power saving
128 // See PicoPower application note
129 // - I/O port input with pullup
132 // - Power Reduction Register PRR
133 set_sleep_mode(SLEEP_MODE_PWR_DOWN);
139 // Disable watchdog after sleep
144 /** \brief Suspend power down
148 void suspend_power_down(void)
150 suspend_power_down_kb();
152 #ifndef NO_SUSPEND_POWER_DOWN
153 power_down(WDTO_15MS);
157 __attribute__ ((weak)) void matrix_power_up(void) {}
158 __attribute__ ((weak)) void matrix_power_down(void) {}
159 bool suspend_wakeup_condition(void)
164 for (uint8_t r = 0; r < MATRIX_ROWS; r++) {
165 if (matrix_get_row(r)) return true;
170 /** \brief run user level code immediately after wakeup
174 __attribute__ ((weak))
175 void suspend_wakeup_init_user(void) { }
177 /** \brief run keyboard level code immediately after wakeup
181 __attribute__ ((weak))
182 void suspend_wakeup_init_kb(void) {
183 suspend_wakeup_init_user();
185 /** \brief run immediately after wakeup
189 void suspend_wakeup_init(void)
191 // clear keyboard state
193 #ifdef BACKLIGHT_ENABLE
196 led_set(host_keyboard_leds());
197 #if defined(RGBLIGHT_SLEEP) && defined(RGBLIGHT_ENABLE)
198 #ifdef BOOTLOADER_TEENSY
201 rgblight_enable_noeeprom();
202 #ifdef RGBLIGHT_ANIMATIONS
203 rgblight_timer_enable();
206 suspend_wakeup_init_kb();
209 #ifndef NO_SUSPEND_POWER_DOWN
210 /* watchdog timeout */
213 // compensate timer for sleep
214 switch (wdt_timeout) {
216 timer_count += 15 + 2; // WDTO_15MS + 2(from observation)