]> git.donarmstrong.com Git - qmk_firmware.git/blob - tmk_core/common/avr/suspend.c
[Keyboard] Add QMK configurator JSON for Alice PCB (#6397)
[qmk_firmware.git] / tmk_core / common / avr / suspend.c
1 #include <stdbool.h>
2 #include <avr/sleep.h>
3 #include <avr/wdt.h>
4 #include <avr/interrupt.h>
5 #include "matrix.h"
6 #include "action.h"
7 #include "backlight.h"
8 #include "suspend_avr.h"
9 #include "suspend.h"
10 #include "timer.h"
11 #include "led.h"
12 #include "host.h"
13 #include "rgblight_reconfig.h"
14
15 #ifdef PROTOCOL_LUFA
16         #include "lufa.h"
17 #endif
18
19 #ifdef AUDIO_ENABLE
20     #include "audio.h"
21 #endif /* AUDIO_ENABLE */
22
23 #if defined(RGBLIGHT_SLEEP) && defined(RGBLIGHT_ENABLE)
24   #include "rgblight.h"
25   extern rgblight_config_t rgblight_config;
26   static bool rgblight_enabled;
27   static bool is_suspended;
28 #endif
29
30
31 #define wdt_intr_enable(value)   \
32 __asm__ __volatile__ (  \
33     "in __tmp_reg__,__SREG__" "\n\t"    \
34     "cli" "\n\t"    \
35     "wdr" "\n\t"    \
36     "sts %0,%1" "\n\t"  \
37     "out __SREG__,__tmp_reg__" "\n\t"   \
38     "sts %0,%2" "\n\t" \
39     : /* no outputs */  \
40     : "M" (_SFR_MEM_ADDR(_WD_CONTROL_REG)), \
41     "r" (_BV(_WD_CHANGE_BIT) | _BV(WDE)), \
42     "r" ((uint8_t) ((value & 0x08 ? _WD_PS3_MASK : 0x00) | \
43         _BV(WDIE) | (value & 0x07)) ) \
44     : "r0"  \
45 )
46
47
48 /** \brief Suspend idle
49  *
50  * FIXME: needs doc
51  */
52 void suspend_idle(uint8_t time) {
53     cli();
54     set_sleep_mode(SLEEP_MODE_IDLE);
55     sleep_enable();
56     sei();
57     sleep_cpu();
58     sleep_disable();
59 }
60
61
62 // TODO: This needs some cleanup
63
64 /** \brief Run keyboard level Power down
65  *
66  * FIXME: needs doc
67  */
68 __attribute__ ((weak))
69 void suspend_power_down_user (void) { }
70 /** \brief Run keyboard level Power down
71  *
72  * FIXME: needs doc
73  */
74 __attribute__ ((weak))
75 void suspend_power_down_kb(void) {
76   suspend_power_down_user();
77 }
78
79 #ifndef NO_SUSPEND_POWER_DOWN
80 /** \brief Power down MCU with watchdog timer
81  *
82  * wdto: watchdog timer timeout defined in <avr/wdt.h>
83  *          WDTO_15MS
84  *          WDTO_30MS
85  *          WDTO_60MS
86  *          WDTO_120MS
87  *          WDTO_250MS
88  *          WDTO_500MS
89  *          WDTO_1S
90  *          WDTO_2S
91  *          WDTO_4S
92  *          WDTO_8S
93  */
94 static uint8_t wdt_timeout = 0;
95
96 /** \brief Power down
97  *
98  * FIXME: needs doc
99  */
100 static void power_down(uint8_t wdto) {
101 #ifdef PROTOCOL_LUFA
102   if (USB_DeviceState == DEVICE_STATE_Configured) return;
103 #endif
104   wdt_timeout = wdto;
105
106   // Watchdog Interrupt Mode
107   wdt_intr_enable(wdto);
108
109 #ifdef BACKLIGHT_ENABLE
110   backlight_set(0);
111 #endif
112
113   // Turn off LED indicators
114   uint8_t leds_off = 0;
115 #if defined(BACKLIGHT_CAPS_LOCK) && defined(BACKLIGHT_ENABLE)
116   if (is_backlight_enabled()) {
117     // Don't try to turn off Caps Lock indicator as it is backlight and backlight is already off
118     leds_off |= (1<<USB_LED_CAPS_LOCK);
119   }
120 #endif
121   led_set(leds_off);
122
123 #ifdef AUDIO_ENABLE
124   // This sometimes disables the start-up noise, so it's been disabled
125   // stop_all_notes();
126 #endif /* AUDIO_ENABLE */
127 #if defined(RGBLIGHT_SLEEP) && defined(RGBLIGHT_ENABLE)
128 #ifdef RGBLIGHT_ANIMATIONS
129   rgblight_timer_disable();
130 #endif
131   if (!is_suspended) {
132     is_suspended = true;
133     rgblight_enabled = rgblight_config.enable;
134     rgblight_disable_noeeprom();
135   }
136 #endif
137   suspend_power_down_kb();
138
139     // TODO: more power saving
140     // See PicoPower application note
141     // - I/O port input with pullup
142     // - prescale clock
143     // - BOD disable
144     // - Power Reduction Register PRR
145     set_sleep_mode(SLEEP_MODE_PWR_DOWN);
146     sleep_enable();
147     sei();
148     sleep_cpu();
149     sleep_disable();
150
151     // Disable watchdog after sleep
152     wdt_disable();
153 }
154 #endif
155
156 /** \brief Suspend power down
157  *
158  * FIXME: needs doc
159  */
160 void suspend_power_down(void) {
161         suspend_power_down_kb();
162
163 #ifndef NO_SUSPEND_POWER_DOWN
164     power_down(WDTO_15MS);
165 #endif
166 }
167
168 __attribute__ ((weak)) void matrix_power_up(void) {}
169 __attribute__ ((weak)) void matrix_power_down(void) {}
170 bool suspend_wakeup_condition(void) {
171     matrix_power_up();
172     matrix_scan();
173     matrix_power_down();
174     for (uint8_t r = 0; r < MATRIX_ROWS; r++) {
175         if (matrix_get_row(r)) return true;
176     }
177      return false;
178 }
179
180 /** \brief run user level code immediately after wakeup
181  *
182  * FIXME: needs doc
183  */
184 __attribute__ ((weak))
185 void suspend_wakeup_init_user(void) { }
186
187 /** \brief run keyboard level code immediately after wakeup
188  *
189  * FIXME: needs doc
190  */
191 __attribute__ ((weak))
192 void suspend_wakeup_init_kb(void) {
193   suspend_wakeup_init_user();
194 }
195 /** \brief run immediately after wakeup
196  *
197  * FIXME: needs doc
198  */
199 void suspend_wakeup_init(void) {
200     // clear keyboard state
201     clear_keyboard();
202 #ifdef BACKLIGHT_ENABLE
203     backlight_init();
204 #endif
205         led_set(host_keyboard_leds());
206 #if defined(RGBLIGHT_SLEEP) && defined(RGBLIGHT_ENABLE)
207   is_suspended = false;
208   if (rgblight_enabled) {
209     #ifdef BOOTLOADER_TEENSY
210       wait_ms(10);
211     #endif
212     rgblight_enable_noeeprom();
213   }
214 #ifdef RGBLIGHT_ANIMATIONS
215   rgblight_timer_enable();
216 #endif
217 #endif
218     suspend_wakeup_init_kb();
219 }
220
221 #ifndef NO_SUSPEND_POWER_DOWN
222 /* watchdog timeout */
223 ISR(WDT_vect) {
224     // compensate timer for sleep
225     switch (wdt_timeout) {
226         case WDTO_15MS:
227             timer_count += 15 + 2;  // WDTO_15MS + 2(from observation)
228             break;
229         default:
230             ;
231     }
232 }
233 #endif