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