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