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