4 #include <avr/interrupt.h>
8 #include "suspend_avr.h"
13 #include "rgblight_reconfig.h"
15 #include "split_flags.h"
24 #endif /* AUDIO_ENABLE */
26 #if defined(RGBLIGHT_SLEEP) && defined(RGBLIGHT_ENABLE)
28 extern rgblight_config_t rgblight_config;
29 static bool rgblight_enabled;
30 static bool is_suspended;
34 #define wdt_intr_enable(value) \
35 __asm__ __volatile__ ( \
36 "in __tmp_reg__,__SREG__" "\n\t" \
40 "out __SREG__,__tmp_reg__" "\n\t" \
43 : "M" (_SFR_MEM_ADDR(_WD_CONTROL_REG)), \
44 "r" (_BV(_WD_CHANGE_BIT) | _BV(WDE)), \
45 "r" ((uint8_t) ((value & 0x08 ? _WD_PS3_MASK : 0x00) | \
46 _BV(WDIE) | (value & 0x07)) ) \
51 /** \brief Suspend idle
55 void suspend_idle(uint8_t time) {
57 set_sleep_mode(SLEEP_MODE_IDLE);
65 // TODO: This needs some cleanup
67 /** \brief Run keyboard level Power down
71 __attribute__ ((weak))
72 void suspend_power_down_user (void) { }
73 /** \brief Run keyboard level Power down
77 __attribute__ ((weak))
78 void suspend_power_down_kb(void) {
79 suspend_power_down_user();
82 #ifndef NO_SUSPEND_POWER_DOWN
83 /** \brief Power down MCU with watchdog timer
85 * wdto: watchdog timer timeout defined in <avr/wdt.h>
97 static uint8_t wdt_timeout = 0;
103 static void power_down(uint8_t wdto) {
105 if (USB_DeviceState == DEVICE_STATE_Configured) return;
109 // Watchdog Interrupt Mode
110 wdt_intr_enable(wdto);
112 #ifdef BACKLIGHT_ENABLE
116 // Turn off LED indicators
117 uint8_t leds_off = 0;
118 #if defined(BACKLIGHT_CAPS_LOCK) && defined(BACKLIGHT_ENABLE)
119 if (is_backlight_enabled()) {
120 // Don't try to turn off Caps Lock indicator as it is backlight and backlight is already off
121 leds_off |= (1<<USB_LED_CAPS_LOCK);
127 // This sometimes disables the start-up noise, so it's been disabled
129 #endif /* AUDIO_ENABLE */
130 #if defined(RGBLIGHT_SLEEP) && defined(RGBLIGHT_ENABLE)
131 #ifdef RGBLIGHT_ANIMATIONS
132 rgblight_timer_disable();
136 rgblight_enabled = rgblight_config.enable;
137 rgblight_disable_noeeprom();
138 #ifdef SPLIT_KEYBOARD
143 suspend_power_down_kb();
145 // TODO: more power saving
146 // See PicoPower application note
147 // - I/O port input with pullup
150 // - Power Reduction Register PRR
151 set_sleep_mode(SLEEP_MODE_PWR_DOWN);
157 // Disable watchdog after sleep
162 /** \brief Suspend power down
166 void suspend_power_down(void) {
167 suspend_power_down_kb();
169 #ifndef NO_SUSPEND_POWER_DOWN
170 power_down(WDTO_15MS);
174 __attribute__ ((weak)) void matrix_power_up(void) {}
175 __attribute__ ((weak)) void matrix_power_down(void) {}
176 bool suspend_wakeup_condition(void) {
180 for (uint8_t r = 0; r < MATRIX_ROWS; r++) {
181 if (matrix_get_row(r)) return true;
186 /** \brief run user level code immediately after wakeup
190 __attribute__ ((weak))
191 void suspend_wakeup_init_user(void) { }
193 /** \brief run keyboard level code immediately after wakeup
197 __attribute__ ((weak))
198 void suspend_wakeup_init_kb(void) {
199 suspend_wakeup_init_user();
201 /** \brief run immediately after wakeup
205 void suspend_wakeup_init(void) {
206 // clear keyboard state
208 #ifdef BACKLIGHT_ENABLE
211 led_set(host_keyboard_leds());
212 #if defined(RGBLIGHT_SLEEP) && defined(RGBLIGHT_ENABLE)
213 is_suspended = false;
214 if (rgblight_enabled) {
215 #ifdef BOOTLOADER_TEENSY
218 rgblight_enable_noeeprom();
219 #ifdef SPLIT_KEYBOARD
223 #ifdef RGBLIGHT_ANIMATIONS
224 rgblight_timer_enable();
227 suspend_wakeup_init_kb();
230 #ifndef NO_SUSPEND_POWER_DOWN
231 /* watchdog timeout */
233 // compensate timer for sleep
234 switch (wdt_timeout) {
236 timer_count += 15 + 2; // WDTO_15MS + 2(from observation)