aboutsummaryrefslogtreecommitdiff
path: root/tmk_core/common/avr
diff options
context:
space:
mode:
Diffstat (limited to 'tmk_core/common/avr')
-rw-r--r--tmk_core/common/avr/bootloader.c12
-rw-r--r--tmk_core/common/avr/suspend.c11
-rw-r--r--tmk_core/common/avr/timer.c69
3 files changed, 58 insertions, 34 deletions
diff --git a/tmk_core/common/avr/bootloader.c b/tmk_core/common/avr/bootloader.c
index ad547b985..34db8d0b0 100644
--- a/tmk_core/common/avr/bootloader.c
+++ b/tmk_core/common/avr/bootloader.c
@@ -1,6 +1,7 @@
1#include <stdint.h> 1#include <stdint.h>
2#include <stdbool.h> 2#include <stdbool.h>
3#include <avr/io.h> 3#include <avr/io.h>
4#include <avr/eeprom.h>
4#include <avr/interrupt.h> 5#include <avr/interrupt.h>
5#include <avr/wdt.h> 6#include <avr/wdt.h>
6#include <util/delay.h> 7#include <util/delay.h>
@@ -89,6 +90,12 @@ void bootloader_jump(void) {
89 _delay_ms(5); 90 _delay_ms(5);
90 #endif 91 #endif
91 92
93 #ifdef BOOTLOADHID_BOOTLOADER
94 // force bootloadHID to stay in bootloader mode, so that it waits
95 // for a new firmware to be flashed
96 eeprom_write_byte((uint8_t *)1, 0x00);
97 #endif
98
92 // watchdog reset 99 // watchdog reset
93 reset_key = BOOTLOADER_RESET_KEY; 100 reset_key = BOOTLOADER_RESET_KEY;
94 wdt_enable(WDTO_250MS); 101 wdt_enable(WDTO_250MS);
@@ -114,6 +121,11 @@ void bootloader_jump(void) {
114 #endif 121 #endif
115} 122}
116 123
124#ifdef __AVR_ATmega32A__
125// MCUSR is actually called MCUCSR in ATmega32A
126#define MCUSR MCUCSR
127#endif
128
117/* this runs before main() */ 129/* this runs before main() */
118void bootloader_jump_after_watchdog_reset(void) __attribute__ ((used, naked, section (".init3"))); 130void bootloader_jump_after_watchdog_reset(void) __attribute__ ((used, naked, section (".init3")));
119void bootloader_jump_after_watchdog_reset(void) 131void bootloader_jump_after_watchdog_reset(void)
diff --git a/tmk_core/common/avr/suspend.c b/tmk_core/common/avr/suspend.c
index 8a7272bbc..0c81e8361 100644
--- a/tmk_core/common/avr/suspend.c
+++ b/tmk_core/common/avr/suspend.c
@@ -47,6 +47,7 @@ void suspend_idle(uint8_t time)
47 sleep_disable(); 47 sleep_disable();
48} 48}
49 49
50#ifndef NO_SUSPEND_POWER_DOWN
50/* Power down MCU with watchdog timer 51/* Power down MCU with watchdog timer
51 * wdto: watchdog timer timeout defined in <avr/wdt.h> 52 * wdto: watchdog timer timeout defined in <avr/wdt.h>
52 * WDTO_15MS 53 * WDTO_15MS
@@ -61,6 +62,7 @@ void suspend_idle(uint8_t time)
61 * WDTO_8S 62 * WDTO_8S
62 */ 63 */
63static uint8_t wdt_timeout = 0; 64static uint8_t wdt_timeout = 0;
65
64static void power_down(uint8_t wdto) 66static void power_down(uint8_t wdto)
65{ 67{
66#ifdef PROTOCOL_LUFA 68#ifdef PROTOCOL_LUFA
@@ -98,19 +100,19 @@ static void power_down(uint8_t wdto)
98 // Disable watchdog after sleep 100 // Disable watchdog after sleep
99 wdt_disable(); 101 wdt_disable();
100} 102}
103#endif
101 104
102void suspend_power_down(void) 105void suspend_power_down(void)
103{ 106{
107#ifndef NO_SUSPEND_POWER_DOWN
104 power_down(WDTO_15MS); 108 power_down(WDTO_15MS);
109#endif
105} 110}
106 111
107__attribute__ ((weak)) void matrix_power_up(void) {} 112__attribute__ ((weak)) void matrix_power_up(void) {}
108__attribute__ ((weak)) void matrix_power_down(void) {} 113__attribute__ ((weak)) void matrix_power_down(void) {}
109bool suspend_wakeup_condition(void) 114bool suspend_wakeup_condition(void)
110{ 115{
111#ifdef BACKLIGHT_ENABLE
112 backlight_set(0);
113#endif
114 matrix_power_up(); 116 matrix_power_up();
115 matrix_scan(); 117 matrix_scan();
116 matrix_power_down(); 118 matrix_power_down();
@@ -126,10 +128,9 @@ void suspend_wakeup_init(void)
126 // clear keyboard state 128 // clear keyboard state
127 clear_keyboard(); 129 clear_keyboard();
128#ifdef BACKLIGHT_ENABLE 130#ifdef BACKLIGHT_ENABLE
129 backlight_set(0);
130 backlight_init(); 131 backlight_init();
131#endif 132#endif
132led_set(host_keyboard_leds()); 133 led_set(host_keyboard_leds());
133} 134}
134 135
135#ifndef NO_SUSPEND_POWER_DOWN 136#ifndef NO_SUSPEND_POWER_DOWN
diff --git a/tmk_core/common/avr/timer.c b/tmk_core/common/avr/timer.c
index 292b41c3a..369015200 100644
--- a/tmk_core/common/avr/timer.c
+++ b/tmk_core/common/avr/timer.c
@@ -17,6 +17,7 @@ along with this program. If not, see <http://www.gnu.org/licenses/>.
17 17
18#include <avr/io.h> 18#include <avr/io.h>
19#include <avr/interrupt.h> 19#include <avr/interrupt.h>
20#include <util/atomic.h>
20#include <stdint.h> 21#include <stdint.h>
21#include "timer_avr.h" 22#include "timer_avr.h"
22#include "timer.h" 23#include "timer.h"
@@ -24,38 +25,47 @@ along with this program. If not, see <http://www.gnu.org/licenses/>.
24 25
25// counter resolution 1ms 26// counter resolution 1ms
26// NOTE: union { uint32_t timer32; struct { uint16_t dummy; uint16_t timer16; }} 27// NOTE: union { uint32_t timer32; struct { uint16_t dummy; uint16_t timer16; }}
27volatile uint32_t timer_count = 0; 28volatile uint32_t timer_count;
28 29
29void timer_init(void) 30void timer_init(void)
30{ 31{
31 // Timer0 CTC mode
32 TCCR0A = 0x02;
33
34#if TIMER_PRESCALER == 1 32#if TIMER_PRESCALER == 1
35 TCCR0B = 0x01; 33 uint8_t prescaler = 0x01;
36#elif TIMER_PRESCALER == 8 34#elif TIMER_PRESCALER == 8
37 TCCR0B = 0x02; 35 uint8_t prescaler = 0x02;
38#elif TIMER_PRESCALER == 64 36#elif TIMER_PRESCALER == 64
39 TCCR0B = 0x03; 37 uint8_t prescaler = 0x03;
40#elif TIMER_PRESCALER == 256 38#elif TIMER_PRESCALER == 256
41 TCCR0B = 0x04; 39 uint8_t prescaler = 0x04;
42#elif TIMER_PRESCALER == 1024 40#elif TIMER_PRESCALER == 1024
43 TCCR0B = 0x05; 41 uint8_t prescaler = 0x05;
44#else 42#else
45# error "Timer prescaler value is NOT vaild." 43# error "Timer prescaler value is NOT vaild."
46#endif 44#endif
47 45
46#ifndef __AVR_ATmega32A__
47 // Timer0 CTC mode
48 TCCR0A = 0x02;
49
50 TCCR0B = prescaler;
51
48 OCR0A = TIMER_RAW_TOP; 52 OCR0A = TIMER_RAW_TOP;
49 TIMSK0 = (1<<OCIE0A); 53 TIMSK0 = (1<<OCIE0A);
54#else
55 // Timer0 CTC mode
56 TCCR0 = (1 << WGM01) | prescaler;
57
58 OCR0 = TIMER_RAW_TOP;
59 TIMSK = (1 << OCIE0);
60#endif
50} 61}
51 62
52inline 63inline
53void timer_clear(void) 64void timer_clear(void)
54{ 65{
55 uint8_t sreg = SREG; 66 ATOMIC_BLOCK(ATOMIC_RESTORESTATE) {
56 cli();
57 timer_count = 0; 67 timer_count = 0;
58 SREG = sreg; 68 }
59} 69}
60 70
61inline 71inline
@@ -63,10 +73,9 @@ uint16_t timer_read(void)
63{ 73{
64 uint32_t t; 74 uint32_t t;
65 75
66 uint8_t sreg = SREG; 76 ATOMIC_BLOCK(ATOMIC_RESTORESTATE) {
67 cli(); 77 t = timer_count;
68 t = timer_count; 78 }
69 SREG = sreg;
70 79
71 return (t & 0xFFFF); 80 return (t & 0xFFFF);
72} 81}
@@ -76,10 +85,9 @@ uint32_t timer_read32(void)
76{ 85{
77 uint32_t t; 86 uint32_t t;
78 87
79 uint8_t sreg = SREG; 88 ATOMIC_BLOCK(ATOMIC_RESTORESTATE) {
80 cli(); 89 t = timer_count;
81 t = timer_count; 90 }
82 SREG = sreg;
83 91
84 return t; 92 return t;
85} 93}
@@ -89,10 +97,9 @@ uint16_t timer_elapsed(uint16_t last)
89{ 97{
90 uint32_t t; 98 uint32_t t;
91 99
92 uint8_t sreg = SREG; 100 ATOMIC_BLOCK(ATOMIC_RESTORESTATE) {
93 cli(); 101 t = timer_count;
94 t = timer_count; 102 }
95 SREG = sreg;
96 103
97 return TIMER_DIFF_16((t & 0xFFFF), last); 104 return TIMER_DIFF_16((t & 0xFFFF), last);
98} 105}
@@ -102,16 +109,20 @@ uint32_t timer_elapsed32(uint32_t last)
102{ 109{
103 uint32_t t; 110 uint32_t t;
104 111
105 uint8_t sreg = SREG; 112 ATOMIC_BLOCK(ATOMIC_RESTORESTATE) {
106 cli(); 113 t = timer_count;
107 t = timer_count; 114 }
108 SREG = sreg;
109 115
110 return TIMER_DIFF_32(t, last); 116 return TIMER_DIFF_32(t, last);
111} 117}
112 118
113// excecuted once per 1ms.(excess for just timer count?) 119// excecuted once per 1ms.(excess for just timer count?)
114ISR(TIMER0_COMPA_vect) 120#ifndef __AVR_ATmega32A__
121#define TIMER_INTERRUPT_VECTOR TIMER0_COMPA_vect
122#else
123#define TIMER_INTERRUPT_VECTOR TIMER0_COMP_vect
124#endif
125ISR(TIMER_INTERRUPT_VECTOR, ISR_NOBLOCK)
115{ 126{
116 timer_count++; 127 timer_count++;
117} 128}