]> git.gir.st - tmk_keyboard.git/blob - common/avr/suspend.c
Fix common files for mbed
[tmk_keyboard.git] / 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
11
12 #define wdt_intr_enable(value) \
13 __asm__ __volatile__ ( \
14 "in __tmp_reg__,__SREG__" "\n\t" \
15 "cli" "\n\t" \
16 "wdr" "\n\t" \
17 "sts %0,%1" "\n\t" \
18 "out __SREG__,__tmp_reg__" "\n\t" \
19 "sts %0,%2" "\n\t" \
20 : /* no outputs */ \
21 : "M" (_SFR_MEM_ADDR(_WD_CONTROL_REG)), \
22 "r" (_BV(_WD_CHANGE_BIT) | _BV(WDE)), \
23 "r" ((uint8_t) ((value & 0x08 ? _WD_PS3_MASK : 0x00) | \
24 _BV(WDIE) | (value & 0x07)) ) \
25 : "r0" \
26 )
27
28
29 void suspend_power_down(void)
30 {
31 #ifdef BACKLIGHT_ENABLE
32 backlight_set(0);
33 #endif
34 #ifndef NO_SUSPEND_POWER_DOWN
35 // Enable watchdog to wake from MCU sleep
36 cli();
37 wdt_reset();
38
39 // Watchdog Interrupt and System Reset Mode
40 //wdt_enable(WDTO_1S);
41 //WDTCSR |= _BV(WDIE);
42
43 // Watchdog Interrupt Mode
44 wdt_intr_enable(WDTO_120MS);
45
46 // TODO: more power saving
47 // See PicoPower application note
48 // - I/O port input with pullup
49 // - prescale clock
50 // - BOD disable
51 // - Power Reduction Register PRR
52 // sleep in power down mode
53 set_sleep_mode(SLEEP_MODE_PWR_DOWN);
54 sleep_enable();
55 sei();
56 sleep_cpu();
57 sleep_disable();
58
59 // Disable watchdog after sleep
60 wdt_disable();
61 #endif
62 }
63
64 bool suspend_wakeup_condition(void)
65 {
66 matrix_scan();
67 for (uint8_t r = 0; r < MATRIX_ROWS; r++) {
68 if (matrix_get_row(r)) return true;
69 }
70 return false;
71 }
72
73 // run immediately after wakeup
74 void suspend_wakeup_init(void)
75 {
76 // clear keyboard state
77 clear_keyboard();
78 #ifdef BACKLIGHT_ENABLE
79 backlight_init();
80 #endif
81 }
82
83 #ifndef NO_SUSPEND_POWER_DOWN
84 /* watchdog timeout */
85 ISR(WDT_vect)
86 {
87 /* wakeup from MCU sleep mode */
88 /*
89 // blink LED
90 static uint8_t led_state = 0;
91 static uint8_t led_count = 0;
92 led_count++;
93 if ((led_count & 0x07) == 0) {
94 led_set((led_state ^= (1<<USB_LED_CAPS_LOCK)));
95 }
96 */
97 }
98 #endif
Imprint / Impressum