hhkb: Update binary
[tmk_keyboard.git] / tmk_core / common / avr / suspend.c
CommitLineData
a074364c 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
a074364c 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
8f969d65 73static void standby(void)
74{
75 set_sleep_mode(SLEEP_MODE_STANDBY);
76 sleep_enable();
77 sei();
78 sleep_cpu();
79 sleep_disable();
80}
81
82static void idle(void)
83{
84 set_sleep_mode(SLEEP_MODE_IDLE);
85 sleep_enable();
86 sei();
87 sleep_cpu();
88 sleep_disable();
89}
90
91
92void suspend_idle(uint8_t time)
93{
94 idle();
95}
96
a074364c 97void suspend_power_down(void)
98{
8f969d65 99#ifdef SUSPEND_MODE_STANDBY
100 standby();
101#elif defined(SUSPEND_MODE_IDLE)
102 idle();
103#else
a074364c 104 power_down(WDTO_15MS);
8f969d65 105#endif
a074364c 106}
107
108bool suspend_wakeup_condition(void)
109{
110 matrix_power_up();
111 matrix_scan();
112 matrix_power_down();
113 for (uint8_t r = 0; r < MATRIX_ROWS; r++) {
114 if (matrix_get_row(r)) return true;
115 }
116 return false;
117}
118
119// run immediately after wakeup
120void suspend_wakeup_init(void)
121{
122 // clear keyboard state
f76a786b 123 matrix_clear();
a074364c 124 clear_keyboard();
125#ifdef BACKLIGHT_ENABLE
126 backlight_init();
127#endif
128}
129
130#ifndef NO_SUSPEND_POWER_DOWN
131/* watchdog timeout */
132ISR(WDT_vect)
133{
134 // compensate timer for sleep
135 switch (wdt_timeout) {
136 case WDTO_15MS:
137 timer_count += 15 + 2; // WDTO_15MS + 2(from observation)
138 break;
139 default:
140 ;
141 }
142}
143#endif
Imprint / Impressum