]> git.donarmstrong.com Git - qmk_firmware.git/blob - tmk_core/common/avr/suspend.c
Merge pull request #5 from jackhumbert/master
[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 #ifdef PROTOCOL_LUFA
13 #include "lufa.h"
14 #endif
15
16
17 #define wdt_intr_enable(value)   \
18 __asm__ __volatile__ (  \
19     "in __tmp_reg__,__SREG__" "\n\t"    \
20     "cli" "\n\t"    \
21     "wdr" "\n\t"    \
22     "sts %0,%1" "\n\t"  \
23     "out __SREG__,__tmp_reg__" "\n\t"   \
24     "sts %0,%2" "\n\t" \
25     : /* no outputs */  \
26     : "M" (_SFR_MEM_ADDR(_WD_CONTROL_REG)), \
27     "r" (_BV(_WD_CHANGE_BIT) | _BV(WDE)), \
28     "r" ((uint8_t) ((value & 0x08 ? _WD_PS3_MASK : 0x00) | \
29         _BV(WDIE) | (value & 0x07)) ) \
30     : "r0"  \
31 )
32
33
34 void suspend_idle(uint8_t time)
35 {
36     cli();
37     set_sleep_mode(SLEEP_MODE_IDLE);
38     sleep_enable();
39     sei();
40     sleep_cpu();
41     sleep_disable();
42 }
43
44 /* Power down MCU with watchdog timer
45  * wdto: watchdog timer timeout defined in <avr/wdt.h>
46  *          WDTO_15MS
47  *          WDTO_30MS
48  *          WDTO_60MS
49  *          WDTO_120MS
50  *          WDTO_250MS
51  *          WDTO_500MS
52  *          WDTO_1S
53  *          WDTO_2S
54  *          WDTO_4S
55  *          WDTO_8S
56  */
57 static uint8_t wdt_timeout = 0;
58 static void power_down(uint8_t wdto)
59 {
60 #ifdef PROTOCOL_LUFA
61     if (USB_DeviceState == DEVICE_STATE_Configured) return;
62 #endif
63     wdt_timeout = wdto;
64
65     // Watchdog Interrupt Mode
66     wdt_intr_enable(wdto);
67
68 #ifdef BACKLIGHT_ENABLE
69         backlight_set(0);
70 #endif
71
72         // Turn off LED indicators
73         led_set(0);
74
75     // TODO: more power saving
76     // See PicoPower application note
77     // - I/O port input with pullup
78     // - prescale clock
79     // - BOD disable
80     // - Power Reduction Register PRR
81     set_sleep_mode(SLEEP_MODE_PWR_DOWN);
82     sleep_enable();
83     sei();
84     sleep_cpu();
85     sleep_disable();
86
87     // Disable watchdog after sleep
88     wdt_disable();
89 }
90
91 void suspend_power_down(void)
92 {
93     power_down(WDTO_15MS);
94 }
95
96 __attribute__ ((weak)) void matrix_power_up(void) {}
97 __attribute__ ((weak)) void matrix_power_down(void) {}
98 bool suspend_wakeup_condition(void)
99 {
100 #ifdef BACKLIGHT_ENABLE
101     backlight_set(0);
102 #endif
103     matrix_power_up();
104     matrix_scan();
105     matrix_power_down();
106     for (uint8_t r = 0; r < MATRIX_ROWS; r++) {
107         if (matrix_get_row(r)) return true;
108     }
109     return false;
110 }
111
112 // run immediately after wakeup
113 void suspend_wakeup_init(void)
114 {
115     // clear keyboard state
116     clear_keyboard();
117 #ifdef BACKLIGHT_ENABLE
118     backlight_set(0);
119     backlight_init();
120 #endif
121 led_set(host_keyboard_leds());
122 }
123
124 #ifndef NO_SUSPEND_POWER_DOWN
125 /* watchdog timeout */
126 ISR(WDT_vect)
127 {
128     // compensate timer for sleep
129     switch (wdt_timeout) {
130         case WDTO_15MS:
131             timer_count += 15 + 2;  // WDTO_15MS + 2(from observation)
132             break;
133         default:
134             ;
135     }
136 }
137 #endif
138