aboutsummaryrefslogtreecommitdiff
path: root/common/avr
diff options
context:
space:
mode:
authortmk <nobody@nowhere>2014-06-17 22:41:14 +0900
committertmk <nobody@nowhere>2014-07-30 14:07:43 +0900
commite81c70149ecf73256f8bb7d77cefc07f2b91d2be (patch)
treed918595b92aa099537640cd02285f914b343bd67 /common/avr
parentee70fe59ee8ebc6dcbf55171b1a2dd72e1744ae6 (diff)
downloadqmk_firmware-e81c70149ecf73256f8bb7d77cefc07f2b91d2be.tar.gz
qmk_firmware-e81c70149ecf73256f8bb7d77cefc07f2b91d2be.zip
Fix common files for mbed
Diffstat (limited to 'common/avr')
-rw-r--r--common/avr/bootloader.c148
-rw-r--r--common/avr/eeconfig.c45
-rw-r--r--common/avr/suspend.c98
-rw-r--r--common/avr/suspend_avr.h27
4 files changed, 318 insertions, 0 deletions
diff --git a/common/avr/bootloader.c b/common/avr/bootloader.c
new file mode 100644
index 000000000..cda295b18
--- /dev/null
+++ b/common/avr/bootloader.c
@@ -0,0 +1,148 @@
1#include <stdint.h>
2#include <stdbool.h>
3#include <avr/io.h>
4#include <avr/interrupt.h>
5#include <avr/wdt.h>
6#include <util/delay.h>
7#include "bootloader.h"
8
9#ifdef PROTOCOL_LUFA
10#include <LUFA/Drivers/USB/USB.h>
11#endif
12
13
14/* Boot Section Size in *BYTEs*
15 * Teensy halfKay 512
16 * Teensy++ halfKay 1024
17 * Atmel DFU loader 4096
18 * LUFA bootloader 4096
19 * USBaspLoader 2048
20 */
21#ifndef BOOTLOADER_SIZE
22#warning To use bootloader_jump() you need to define BOOTLOADER_SIZE in config.h.
23#define BOOTLOADER_SIZE 4096
24#endif
25
26#define FLASH_SIZE (FLASHEND + 1L)
27#define BOOTLOADER_START (FLASH_SIZE - BOOTLOADER_SIZE)
28
29
30/*
31 * Entering the Bootloader via Software
32 * http://www.fourwalledcubicle.com/files/LUFA/Doc/120730/html/_page__software_bootloader_start.html
33 */
34#define BOOTLOADER_RESET_KEY 0xB007B007
35uint32_t reset_key __attribute__ ((section (".noinit")));
36
37/* initialize MCU status by watchdog reset */
38void bootloader_jump(void) {
39#ifdef PROTOCOL_LUFA
40 USB_Disable();
41 cli();
42 _delay_ms(2000);
43#endif
44
45#ifdef PROTOCOL_PJRC
46 cli();
47 UDCON = 1;
48 USBCON = (1<<FRZCLK);
49 UCSR1B = 0;
50 _delay_ms(5);
51#endif
52
53 // watchdog reset
54 reset_key = BOOTLOADER_RESET_KEY;
55 wdt_enable(WDTO_250MS);
56 for (;;);
57}
58
59
60/* this runs before main() */
61void bootloader_jump_after_watchdog_reset(void) __attribute__ ((used, naked, section (".init3")));
62void bootloader_jump_after_watchdog_reset(void)
63{
64 if ((MCUSR & (1<<WDRF)) && reset_key == BOOTLOADER_RESET_KEY) {
65 reset_key = 0;
66
67 // My custom USBasploader requires this to come up.
68 MCUSR = 0;
69
70 // Seems like Teensy halfkay loader requires clearing WDRF and disabling watchdog.
71 MCUSR &= ~(1<<WDRF);
72 wdt_disable();
73
74 // This is compled into 'icall', address should be in word unit, not byte.
75 ((void (*)(void))(BOOTLOADER_START/2))();
76 }
77}
78
79
80#if 0
81/* Jumping To The Bootloader
82 * http://www.pjrc.com/teensy/jump_to_bootloader.html
83 *
84 * This method doen't work when using LUFA. idk why.
85 * - needs to initialize more regisers or interrupt setting?
86 */
87void bootloader_jump(void) {
88#ifdef PROTOCOL_LUFA
89 USB_Disable();
90 cli();
91 _delay_ms(2000);
92#endif
93
94#ifdef PROTOCOL_PJRC
95 cli();
96 UDCON = 1;
97 USBCON = (1<<FRZCLK);
98 UCSR1B = 0;
99 _delay_ms(5);
100#endif
101
102 /*
103 * Initialize
104 */
105#if defined(__AVR_AT90USB162__)
106 EIMSK = 0; PCICR = 0; SPCR = 0; ACSR = 0; EECR = 0;
107 TIMSK0 = 0; TIMSK1 = 0; UCSR1B = 0;
108 DDRB = 0; DDRC = 0; DDRD = 0;
109 PORTB = 0; PORTC = 0; PORTD = 0;
110#elif defined(__AVR_ATmega32U4__)
111 EIMSK = 0; PCICR = 0; SPCR = 0; ACSR = 0; EECR = 0; ADCSRA = 0;
112 TIMSK0 = 0; TIMSK1 = 0; TIMSK3 = 0; TIMSK4 = 0; UCSR1B = 0; TWCR = 0;
113 DDRB = 0; DDRC = 0; DDRD = 0; DDRE = 0; DDRF = 0; TWCR = 0;
114 PORTB = 0; PORTC = 0; PORTD = 0; PORTE = 0; PORTF = 0;
115#elif defined(__AVR_AT90USB646__)
116 EIMSK = 0; PCICR = 0; SPCR = 0; ACSR = 0; EECR = 0; ADCSRA = 0;
117 TIMSK0 = 0; TIMSK1 = 0; TIMSK2 = 0; TIMSK3 = 0; UCSR1B = 0; TWCR = 0;
118 DDRA = 0; DDRB = 0; DDRC = 0; DDRD = 0; DDRE = 0; DDRF = 0;
119 PORTA = 0; PORTB = 0; PORTC = 0; PORTD = 0; PORTE = 0; PORTF = 0;
120#elif defined(__AVR_AT90USB1286__)
121 EIMSK = 0; PCICR = 0; SPCR = 0; ACSR = 0; EECR = 0; ADCSRA = 0;
122 TIMSK0 = 0; TIMSK1 = 0; TIMSK2 = 0; TIMSK3 = 0; UCSR1B = 0; TWCR = 0;
123 DDRA = 0; DDRB = 0; DDRC = 0; DDRD = 0; DDRE = 0; DDRF = 0;
124 PORTA = 0; PORTB = 0; PORTC = 0; PORTD = 0; PORTE = 0; PORTF = 0;
125#endif
126
127 /*
128 * USBaspLoader
129 */
130#if defined(__AVR_ATmega168__) || defined(__AVR_ATmega168P__) || defined(__AVR_ATmega328P__)
131 // This makes custom USBasploader come up.
132 MCUSR = 0;
133
134 // initialize ports
135 PORTB = 0; PORTC= 0; PORTD = 0;
136 DDRB = 0; DDRC= 0; DDRD = 0;
137
138 // disable interrupts
139 EIMSK = 0; EECR = 0; SPCR = 0;
140 ACSR = 0; SPMCSR = 0; WDTCSR = 0; PCICR = 0;
141 TIMSK0 = 0; TIMSK1 = 0; TIMSK2 = 0;
142 ADCSRA = 0; TWCR = 0; UCSR0B = 0;
143#endif
144
145 // This is compled into 'icall', address should be in word unit, not byte.
146 ((void (*)(void))(BOOTLOADER_START/2))();
147}
148#endif
diff --git a/common/avr/eeconfig.c b/common/avr/eeconfig.c
new file mode 100644
index 000000000..5bd47dc6a
--- /dev/null
+++ b/common/avr/eeconfig.c
@@ -0,0 +1,45 @@
1#include <stdint.h>
2#include <stdbool.h>
3#include <avr/eeprom.h>
4#include "eeconfig.h"
5
6void eeconfig_init(void)
7{
8 eeprom_write_word(EECONFIG_MAGIC, EECONFIG_MAGIC_NUMBER);
9 eeprom_write_byte(EECONFIG_DEBUG, 0);
10 eeprom_write_byte(EECONFIG_DEFAULT_LAYER, 0);
11 eeprom_write_byte(EECONFIG_KEYMAP, 0);
12 eeprom_write_byte(EECONFIG_MOUSEKEY_ACCEL, 0);
13#ifdef BACKLIGHT_ENABLE
14 eeprom_write_byte(EECONFIG_BACKLIGHT, 0);
15#endif
16}
17
18void eeconfig_enable(void)
19{
20 eeprom_write_word(EECONFIG_MAGIC, EECONFIG_MAGIC_NUMBER);
21}
22
23void eeconfig_disable(void)
24{
25 eeprom_write_word(EECONFIG_MAGIC, 0xFFFF);
26}
27
28bool eeconfig_is_enabled(void)
29{
30 return (eeprom_read_word(EECONFIG_MAGIC) == EECONFIG_MAGIC_NUMBER);
31}
32
33uint8_t eeconfig_read_debug(void) { return eeprom_read_byte(EECONFIG_DEBUG); }
34void eeconfig_write_debug(uint8_t val) { eeprom_write_byte(EECONFIG_DEBUG, val); }
35
36uint8_t eeconfig_read_default_layer(void) { return eeprom_read_byte(EECONFIG_DEFAULT_LAYER); }
37void eeconfig_write_default_layer(uint8_t val) { eeprom_write_byte(EECONFIG_DEFAULT_LAYER, val); }
38
39uint8_t eeconfig_read_keymap(void) { return eeprom_read_byte(EECONFIG_KEYMAP); }
40void eeconfig_write_keymap(uint8_t val) { eeprom_write_byte(EECONFIG_KEYMAP, val); }
41
42#ifdef BACKLIGHT_ENABLE
43uint8_t eeconfig_read_backlight(void) { return eeprom_read_byte(EECONFIG_BACKLIGHT); }
44void eeconfig_write_backlight(uint8_t val) { eeprom_write_byte(EECONFIG_BACKLIGHT, val); }
45#endif
diff --git a/common/avr/suspend.c b/common/avr/suspend.c
new file mode 100644
index 000000000..f44a036be
--- /dev/null
+++ b/common/avr/suspend.c
@@ -0,0 +1,98 @@
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
29void 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
64bool 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
74void 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 */
85ISR(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
diff --git a/common/avr/suspend_avr.h b/common/avr/suspend_avr.h
new file mode 100644
index 000000000..357102da4
--- /dev/null
+++ b/common/avr/suspend_avr.h
@@ -0,0 +1,27 @@
1#ifndef SUSPEND_AVR_H
2#define SUSPEND_AVR_H
3
4#include <stdint.h>
5#include <stdbool.h>
6#include <avr/sleep.h>
7#include <avr/wdt.h>
8#include <avr/interrupt.h>
9
10
11#define wdt_intr_enable(value) \
12__asm__ __volatile__ ( \
13 "in __tmp_reg__,__SREG__" "\n\t" \
14 "cli" "\n\t" \
15 "wdr" "\n\t" \
16 "sts %0,%1" "\n\t" \
17 "out __SREG__,__tmp_reg__" "\n\t" \
18 "sts %0,%2" "\n\t" \
19 : /* no outputs */ \
20 : "M" (_SFR_MEM_ADDR(_WD_CONTROL_REG)), \
21 "r" (_BV(_WD_CHANGE_BIT) | _BV(WDE)), \
22 "r" ((uint8_t) ((value & 0x08 ? _WD_PS3_MASK : 0x00) | \
23 _BV(WDIE) | (value & 0x07)) ) \
24 : "r0" \
25)
26
27#endif