]> git.donarmstrong.com Git - qmk_firmware.git/blob - tmk_core/common/avr/suspend.c
Generate API docs from source code comments (#2491)
[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
14 #ifdef PROTOCOL_LUFA
15         #include "lufa.h"
16 #endif
17
18 #ifdef AUDIO_ENABLE
19     #include "audio.h"
20 #endif /* AUDIO_ENABLE */
21
22 #ifdef RGBLIGHT_ANIMATIONS
23   #include "rgblight.h"
24 #endif
25
26
27 #define wdt_intr_enable(value)   \
28 __asm__ __volatile__ (  \
29     "in __tmp_reg__,__SREG__" "\n\t"    \
30     "cli" "\n\t"    \
31     "wdr" "\n\t"    \
32     "sts %0,%1" "\n\t"  \
33     "out __SREG__,__tmp_reg__" "\n\t"   \
34     "sts %0,%2" "\n\t" \
35     : /* no outputs */  \
36     : "M" (_SFR_MEM_ADDR(_WD_CONTROL_REG)), \
37     "r" (_BV(_WD_CHANGE_BIT) | _BV(WDE)), \
38     "r" ((uint8_t) ((value & 0x08 ? _WD_PS3_MASK : 0x00) | \
39         _BV(WDIE) | (value & 0x07)) ) \
40     : "r0"  \
41 )
42
43
44 /** \brief Suspend idle
45  *
46  * FIXME: needs doc
47  */
48 void suspend_idle(uint8_t time)
49 {
50     cli();
51     set_sleep_mode(SLEEP_MODE_IDLE);
52     sleep_enable();
53     sei();
54     sleep_cpu();
55     sleep_disable();
56 }
57
58 #ifndef NO_SUSPEND_POWER_DOWN
59 /** \brief Power down MCU with watchdog timer
60  *
61  * wdto: watchdog timer timeout defined in <avr/wdt.h>
62  *          WDTO_15MS
63  *          WDTO_30MS
64  *          WDTO_60MS
65  *          WDTO_120MS
66  *          WDTO_250MS
67  *          WDTO_500MS
68  *          WDTO_1S
69  *          WDTO_2S
70  *          WDTO_4S
71  *          WDTO_8S
72  */
73 static uint8_t wdt_timeout = 0;
74
75 /** \brief Power down
76  *
77  * FIXME: needs doc
78  */
79 static void power_down(uint8_t wdto)
80 {
81 #ifdef PROTOCOL_LUFA
82     if (USB_DeviceState == DEVICE_STATE_Configured) return;
83 #endif
84     wdt_timeout = wdto;
85
86     // Watchdog Interrupt Mode
87     wdt_intr_enable(wdto);
88
89 #ifdef BACKLIGHT_ENABLE
90         backlight_set(0);
91 #endif
92
93         // Turn off LED indicators
94         led_set(0);
95
96         #ifdef AUDIO_ENABLE
97         // This sometimes disables the start-up noise, so it's been disabled
98                 // stop_all_notes();
99         #endif /* AUDIO_ENABLE */
100 #ifdef RGBLIGHT_SLEEP
101 #ifdef RGBLIGHT_ANIMATIONS
102   rgblight_timer_disable();
103 #endif
104   rgblight_disable();
105 #endif
106     // TODO: more power saving
107     // See PicoPower application note
108     // - I/O port input with pullup
109     // - prescale clock
110     // - BOD disable
111     // - Power Reduction Register PRR
112     set_sleep_mode(SLEEP_MODE_PWR_DOWN);
113     sleep_enable();
114     sei();
115     sleep_cpu();
116     sleep_disable();
117
118     // Disable watchdog after sleep
119     wdt_disable();
120 }
121 #endif
122
123 /** \brief Suspend power down
124  *
125  * FIXME: needs doc
126  */
127 void suspend_power_down(void)
128 {
129 #ifndef NO_SUSPEND_POWER_DOWN
130     power_down(WDTO_15MS);
131 #endif
132 }
133
134 __attribute__ ((weak)) void matrix_power_up(void) {}
135 __attribute__ ((weak)) void matrix_power_down(void) {}
136 bool suspend_wakeup_condition(void)
137 {
138     matrix_power_up();
139     matrix_scan();
140     matrix_power_down();
141     for (uint8_t r = 0; r < MATRIX_ROWS; r++) {
142         if (matrix_get_row(r)) return true;
143     }
144      return false;
145 }
146
147 /** \brief run immediately after wakeup
148  *
149  * FIXME: needs doc
150  */
151 void suspend_wakeup_init(void)
152 {
153     // clear keyboard state
154     clear_keyboard();
155 #ifdef BACKLIGHT_ENABLE
156     backlight_init();
157 #endif
158         led_set(host_keyboard_leds());
159 #ifdef RGBLIGHT_SLEEP
160   rgblight_enable();
161 #ifdef RGBLIGHT_ANIMATIONS
162   rgblight_timer_enable();
163 #endif
164 #endif
165 }
166
167 #ifndef NO_SUSPEND_POWER_DOWN
168 /* watchdog timeout */
169 ISR(WDT_vect)
170 {
171     // compensate timer for sleep
172     switch (wdt_timeout) {
173         case WDTO_15MS:
174             timer_count += 15 + 2;  // WDTO_15MS + 2(from observation)
175             break;
176         default:
177             ;
178     }
179 }
180 #endif