]> git.donarmstrong.com Git - qmk_firmware.git/blob - tmk_core/common/chibios/suspend.c
Merge branch 'master' of github.com:qmk/qmk_firmware into hf/shinydox
[qmk_firmware.git] / tmk_core / common / chibios / suspend.c
1 /* TODO */
2
3 #include "ch.h"
4 #include "hal.h"
5
6 #include "matrix.h"
7 #include "action.h"
8 #include "action_util.h"
9 #include "mousekey.h"
10 #include "host.h"
11 #include "backlight.h"
12 #include "suspend.h"
13 #include "wait.h"
14
15 /** \brief suspend idle
16  *
17  * FIXME: needs doc
18  */
19 void suspend_idle(uint8_t time) {
20         // TODO: this is not used anywhere - what units is 'time' in?
21         wait_ms(time);
22 }
23
24 /** \brief Run keyboard level Power down
25  *
26  * FIXME: needs doc
27  */
28 __attribute__ ((weak))
29 void suspend_power_down_user (void) { }
30 /** \brief Run keyboard level Power down
31  *
32  * FIXME: needs doc
33  */
34 __attribute__ ((weak))
35 void suspend_power_down_kb(void) {
36   suspend_power_down_user();
37 }
38
39 /** \brief suspend power down
40  *
41  * FIXME: needs doc
42  */
43 void suspend_power_down(void) {
44         // TODO: figure out what to power down and how
45         // shouldn't power down TPM/FTM if we want a breathing LED
46         // also shouldn't power down USB
47
48   suspend_power_down_kb();
49         // on AVR, this enables the watchdog for 15ms (max), and goes to
50         // SLEEP_MODE_PWR_DOWN
51
52         wait_ms(17);
53 }
54
55 /** \brief suspend wakeup condition
56  *
57  * FIXME: needs doc
58  */
59 __attribute__ ((weak)) void matrix_power_up(void) {}
60 __attribute__ ((weak)) void matrix_power_down(void) {}
61 bool suspend_wakeup_condition(void)
62 {
63     matrix_power_up();
64     matrix_scan();
65     matrix_power_down();
66     for (uint8_t r = 0; r < MATRIX_ROWS; r++) {
67         if (matrix_get_row(r)) return true;
68     }
69     return false;
70 }
71
72 /** \brief run user level code immediately after wakeup
73  *
74  * FIXME: needs doc
75  */
76 __attribute__ ((weak))
77 void suspend_wakeup_init_user(void) { }
78
79 /** \brief run keyboard level code immediately after wakeup
80  *
81  * FIXME: needs doc
82  */
83 __attribute__ ((weak))
84 void suspend_wakeup_init_kb(void) {
85   suspend_power_down_user();
86 }
87
88 /** \brief suspend wakeup condition
89  *
90  * run immediately after wakeup
91  * FIXME: needs doc
92  */
93 void suspend_wakeup_init(void)
94 {
95     // clear keyboard state
96     // need to do it manually, because we're running from ISR
97     //  and clear_keyboard() calls print
98     // so only clear the variables in memory
99     // the reports will be sent from main.c afterwards
100     // or if the PC asks for GET_REPORT
101     clear_mods();
102     clear_weak_mods();
103     clear_keys();
104 #ifdef MOUSEKEY_ENABLE
105     mousekey_clear();
106 #endif /* MOUSEKEY_ENABLE */
107 #ifdef EXTRAKEY_ENABLE
108     host_system_send(0);
109     host_consumer_send(0);
110 #endif /* EXTRAKEY_ENABLE */
111 #ifdef BACKLIGHT_ENABLE
112     backlight_init();
113 #endif /* BACKLIGHT_ENABLE */
114   suspend_wakeup_init_kb();
115 }