summaryrefslogtreecommitdiff
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 @@
#include <stdint.h>
#include <stdbool.h>
#include <avr/io.h>
+#include <avr/eeprom.h>
#include <avr/interrupt.h>
#include <avr/wdt.h>
#include <util/delay.h>
@@ -89,6 +90,12 @@ void bootloader_jump(void) {
_delay_ms(5);
#endif
+ #ifdef BOOTLOADHID_BOOTLOADER
+ // force bootloadHID to stay in bootloader mode, so that it waits
+ // for a new firmware to be flashed
+ eeprom_write_byte((uint8_t *)1, 0x00);
+ #endif
+
// watchdog reset
reset_key = BOOTLOADER_RESET_KEY;
wdt_enable(WDTO_250MS);
@@ -114,6 +121,11 @@ void bootloader_jump(void) {
#endif
}
+#ifdef __AVR_ATmega32A__
+// MCUSR is actually called MCUCSR in ATmega32A
+#define MCUSR MCUCSR
+#endif
+
/* this runs before main() */
void bootloader_jump_after_watchdog_reset(void) __attribute__ ((used, naked, section (".init3")));
void 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)
sleep_disable();
}
+#ifndef NO_SUSPEND_POWER_DOWN
/* Power down MCU with watchdog timer
* wdto: watchdog timer timeout defined in <avr/wdt.h>
* WDTO_15MS
@@ -61,6 +62,7 @@ void suspend_idle(uint8_t time)
* WDTO_8S
*/
static uint8_t wdt_timeout = 0;
+
static void power_down(uint8_t wdto)
{
#ifdef PROTOCOL_LUFA
@@ -98,19 +100,19 @@ static void power_down(uint8_t wdto)
// Disable watchdog after sleep
wdt_disable();
}
+#endif
void suspend_power_down(void)
{
+#ifndef NO_SUSPEND_POWER_DOWN
power_down(WDTO_15MS);
+#endif
}
__attribute__ ((weak)) void matrix_power_up(void) {}
__attribute__ ((weak)) void matrix_power_down(void) {}
bool suspend_wakeup_condition(void)
{
-#ifdef BACKLIGHT_ENABLE
- backlight_set(0);
-#endif
matrix_power_up();
matrix_scan();
matrix_power_down();
@@ -126,10 +128,9 @@ void suspend_wakeup_init(void)
// clear keyboard state
clear_keyboard();
#ifdef BACKLIGHT_ENABLE
- backlight_set(0);
backlight_init();
#endif
-led_set(host_keyboard_leds());
+ led_set(host_keyboard_leds());
}
#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/>.
#include <avr/io.h>
#include <avr/interrupt.h>
+#include <util/atomic.h>
#include <stdint.h>
#include "timer_avr.h"
#include "timer.h"
@@ -24,38 +25,47 @@ along with this program. If not, see <http://www.gnu.org/licenses/>.
// counter resolution 1ms
// NOTE: union { uint32_t timer32; struct { uint16_t dummy; uint16_t timer16; }}
-volatile uint32_t timer_count = 0;
+volatile uint32_t timer_count;
void timer_init(void)
{
- // Timer0 CTC mode
- TCCR0A = 0x02;
-
#if TIMER_PRESCALER == 1
- TCCR0B = 0x01;
+ uint8_t prescaler = 0x01;
#elif TIMER_PRESCALER == 8
- TCCR0B = 0x02;
+ uint8_t prescaler = 0x02;
#elif TIMER_PRESCALER == 64
- TCCR0B = 0x03;
+ uint8_t prescaler = 0x03;
#elif TIMER_PRESCALER == 256
- TCCR0B = 0x04;
+ uint8_t prescaler = 0x04;
#elif TIMER_PRESCALER == 1024
- TCCR0B = 0x05;
+ uint8_t prescaler = 0x05;
#else
# error "Timer prescaler value is NOT vaild."
#endif
+#ifndef __AVR_ATmega32A__
+ // Timer0 CTC mode
+ TCCR0A = 0x02;
+
+ TCCR0B = prescaler;
+
OCR0A = TIMER_RAW_TOP;
TIMSK0 = (1<<OCIE0A);
+#else
+ // Timer0 CTC mode
+ TCCR0 = (1 << WGM01) | prescaler;
+
+ OCR0 = TIMER_RAW_TOP;
+ TIMSK = (1 << OCIE0);
+#endif
}
inline
void timer_clear(void)
{
- uint8_t sreg = SREG;
- cli();
+ ATOMIC_BLOCK(ATOMIC_RESTORESTATE) {
timer_count = 0;
- SREG = sreg;
+ }
}
inline
@@ -63,10 +73,9 @@ uint16_t timer_read(void)
{
uint32_t t;
- uint8_t sreg = SREG;
- cli();
- t = timer_count;
- SREG = sreg;
+ ATOMIC_BLOCK(ATOMIC_RESTORESTATE) {
+ t = timer_count;
+ }
return (t & 0xFFFF);
}
@@ -76,10 +85,9 @@ uint32_t timer_read32(void)
{
uint32_t t;
- uint8_t sreg = SREG;
- cli();
- t = timer_count;
- SREG = sreg;
+ ATOMIC_BLOCK(ATOMIC_RESTORESTATE) {
+ t = timer_count;
+ }
return t;
}
@@ -89,10 +97,9 @@ uint16_t timer_elapsed(uint16_t last)
{
uint32_t t;
- uint8_t sreg = SREG;
- cli();
- t = timer_count;
- SREG = sreg;
+ ATOMIC_BLOCK(ATOMIC_RESTORESTATE) {
+ t = timer_count;
+ }
return TIMER_DIFF_16((t & 0xFFFF), last);
}
@@ -102,16 +109,20 @@ uint32_t timer_elapsed32(uint32_t last)
{
uint32_t t;
- uint8_t sreg = SREG;
- cli();
- t = timer_count;
- SREG = sreg;
+ ATOMIC_BLOCK(ATOMIC_RESTORESTATE) {
+ t = timer_count;
+ }
return TIMER_DIFF_32(t, last);
}
// excecuted once per 1ms.(excess for just timer count?)
-ISR(TIMER0_COMPA_vect)
+#ifndef __AVR_ATmega32A__
+#define TIMER_INTERRUPT_VECTOR TIMER0_COMPA_vect
+#else
+#define TIMER_INTERRUPT_VECTOR TIMER0_COMP_vect
+#endif
+ISR(TIMER_INTERRUPT_VECTOR, ISR_NOBLOCK)
{
timer_count++;
}