aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authortmk <nobody@nowhere>2013-03-09 11:22:27 +0900
committertmk <nobody@nowhere>2013-03-09 11:22:27 +0900
commit4d64fd8faa8b1a0ceb9019446ba6915aaf1812ea (patch)
tree25410ede4bbf7d5a8994bb385e6c7ee4ad2f4c0c
parent359b68d35f0763ab0cafa2fb800e0a3497291f95 (diff)
downloadqmk_firmware-4d64fd8faa8b1a0ceb9019446ba6915aaf1812ea.tar.gz
qmk_firmware-4d64fd8faa8b1a0ceb9019446ba6915aaf1812ea.zip
Add bootmagic.c and fix bootloader_jump
-rw-r--r--common.mk1
-rw-r--r--common/bootloader.c100
-rw-r--r--common/bootmagic.c45
-rw-r--r--common/bootmagic.h39
-rw-r--r--common/command.c4
-rw-r--r--common/keyboard.c25
-rw-r--r--keyboard/gh60/keymap.c6
-rw-r--r--protocol/lufa.mk4
-rw-r--r--protocol/pjrc.mk8
9 files changed, 178 insertions, 54 deletions
diff --git a/common.mk b/common.mk
index a99e335ee..2735f5bec 100644
--- a/common.mk
+++ b/common.mk
@@ -10,6 +10,7 @@ SRC += $(COMMON_DIR)/host.c \
10 $(COMMON_DIR)/print.c \ 10 $(COMMON_DIR)/print.c \
11 $(COMMON_DIR)/debug.c \ 11 $(COMMON_DIR)/debug.c \
12 $(COMMON_DIR)/bootloader.c \ 12 $(COMMON_DIR)/bootloader.c \
13 $(COMMON_DIR)/bootmagic.c \
13 $(COMMON_DIR)/eeconfig.c \ 14 $(COMMON_DIR)/eeconfig.c \
14 $(COMMON_DIR)/util.c 15 $(COMMON_DIR)/util.c
15 16
diff --git a/common/bootloader.c b/common/bootloader.c
index 6e04efbbd..77fa1b30a 100644
--- a/common/bootloader.c
+++ b/common/bootloader.c
@@ -1,15 +1,16 @@
1#include <stdint.h>
2#include <stdbool.h>
1#include <avr/io.h> 3#include <avr/io.h>
2#include <avr/interrupt.h> 4#include <avr/interrupt.h>
5#include <avr/wdt.h>
3#include <util/delay.h> 6#include <util/delay.h>
4#include "bootloader.h" 7#include "bootloader.h"
5 8
6/* Start Bootloader from Application 9#ifdef PROTOCOL_LUFA
7 * See 10#include <LUFA/Drivers/USB/USB.h>
8 * http://www.pjrc.com/teensy/jump_to_bootloader.html 11#endif
9 * http://www.fourwalledcubicle.com/files/LUFA/Doc/120219/html/_page__software_bootloader_start.html 12
10 */
11 13
12// TODO: support usbasp
13/* Boot Section Size in bytes 14/* Boot Section Size in bytes
14 * Teensy halfKay 512 15 * Teensy halfKay 512
15 * Atmel DFU loader 4096 16 * Atmel DFU loader 4096
@@ -18,28 +19,82 @@
18#ifndef BOOT_SIZE 19#ifndef BOOT_SIZE
19#define BOOT_SIZE 512 20#define BOOT_SIZE 512
20#endif 21#endif
21
22#define FLASH_SIZE (FLASHEND + 1) 22#define FLASH_SIZE (FLASHEND + 1)
23#define BOOTLOADER_START (FLASHEND - BOOT_SIZE) 23#define BOOTLOADER_START (FLASH_SIZE - BOOT_SIZE)
24 24
25
26/*
27 * Entering the Bootloader via Software
28 * http://www.fourwalledcubicle.com/files/LUFA/Doc/120730/html/_page__software_bootloader_start.html
29 */
30#define BOOTLOADER_RESET_KEY 0xB007B007
31uint32_t reset_key __attribute__ ((section (".noinit")));
32
33/* initialize MCU status by watchdog reset */
25void bootloader_jump(void) { 34void bootloader_jump(void) {
35#ifdef PROTOCOL_LUFA
36 USB_Disable();
26 cli(); 37 cli();
38 _delay_ms(2000);
39#endif
27 40
28 // 41#ifdef PROTOCOL_PJRC
29 //Teensy 42 cli();
30 //
31#if defined(__AVR_AT90USB162__) || defined(__AVR_ATmega32U4__) || defined(__AVR_AT90USB646__) || defined(__AVR_AT90USB1286__)
32 // disable watchdog, if enabled
33 // disable all peripherals
34 UDCON = 1; 43 UDCON = 1;
35 USBCON = (1<<FRZCLK); // disable USB 44 USBCON = (1<<FRZCLK);
36 UCSR1B = 0; 45 UCSR1B = 0;
37 _delay_ms(5); 46 _delay_ms(5);
38#else
39 // This makes custom USBasploader come up.
40 MCUSR = 0;
41#endif 47#endif
42 48
49 // watchdog reset
50 reset_key = BOOTLOADER_RESET_KEY;
51 wdt_enable(WDTO_250MS);
52 for (;;);
53}
54
55
56/* this runs before main() */
57void bootloader_jump_after_watchdog_reset(void) __attribute__ ((used, naked, section (".init3")));
58void bootloader_jump_after_watchdog_reset(void)
59{
60 if ((MCUSR & (1<<WDRF)) && reset_key == BOOTLOADER_RESET_KEY) {
61
62 #if defined(__AVR_ATmega168__) || defined(__AVR_ATmega168P__) || defined(__AVR_ATmega328P__)
63 // This makes custom USBasploader come up.
64 MCUSR = 0;
65 #endif
66
67 reset_key = 0;
68 ((void (*)(void))BOOTLOADER_START)();
69 }
70}
71
72
73#if 0
74/* Jumping To The Bootloader
75 * http://www.pjrc.com/teensy/jump_to_bootloader.html
76 *
77 * This method doen't work when using LUFA. idk why.
78 * - needs to initialize more regisers or interrupt setting?
79 */
80void bootloader_jump(void) {
81#ifdef PROTOCOL_LUFA
82 USB_Disable();
83 cli();
84 _delay_ms(2000);
85#endif
86
87#ifdef PROTOCOL_PJRC
88 cli();
89 UDCON = 1;
90 USBCON = (1<<FRZCLK);
91 UCSR1B = 0;
92 _delay_ms(5);
93#endif
94
95 /*
96 * Initialize
97 */
43#if defined(__AVR_AT90USB162__) 98#if defined(__AVR_AT90USB162__)
44 EIMSK = 0; PCICR = 0; SPCR = 0; ACSR = 0; EECR = 0; 99 EIMSK = 0; PCICR = 0; SPCR = 0; ACSR = 0; EECR = 0;
45 TIMSK0 = 0; TIMSK1 = 0; UCSR1B = 0; 100 TIMSK0 = 0; TIMSK1 = 0; UCSR1B = 0;
@@ -62,10 +117,9 @@ void bootloader_jump(void) {
62 PORTA = 0; PORTB = 0; PORTC = 0; PORTD = 0; PORTE = 0; PORTF = 0; 117 PORTA = 0; PORTB = 0; PORTC = 0; PORTD = 0; PORTE = 0; PORTF = 0;
63#endif 118#endif
64 119
65 120 /*
66 // 121 * USBaspLoader
67 //USBasp 122 */
68 //
69#if defined(__AVR_ATmega168__) || defined(__AVR_ATmega168P__) || defined(__AVR_ATmega328P__) 123#if defined(__AVR_ATmega168__) || defined(__AVR_ATmega168P__) || defined(__AVR_ATmega328P__)
70 // This makes custom USBasploader come up. 124 // This makes custom USBasploader come up.
71 MCUSR = 0; 125 MCUSR = 0;
@@ -81,7 +135,7 @@ void bootloader_jump(void) {
81 ADCSRA = 0; TWCR = 0; UCSR0B = 0; 135 ADCSRA = 0; TWCR = 0; UCSR0B = 0;
82#endif 136#endif
83 137
84
85 // start Bootloader 138 // start Bootloader
86 ((void (*)(void))BOOTLOADER_START)(); 139 ((void (*)(void))BOOTLOADER_START)();
87} 140}
141#endif
diff --git a/common/bootmagic.c b/common/bootmagic.c
new file mode 100644
index 000000000..31b8ae5e6
--- /dev/null
+++ b/common/bootmagic.c
@@ -0,0 +1,45 @@
1#include <stdint.h>
2#include <stdbool.h>
3#include <util/delay.h>
4#include "matrix.h"
5#include "keymap.h"
6#include "eeconfig.h"
7#include "bootloader.h"
8#include "bootmagic.h"
9
10
11void bootmagic(void)
12{
13 /* do scans in case of bounce */
14 uint8_t scan = 100;
15 while (scan--) { matrix_scan(); _delay_ms(1); }
16
17 if (!BOOTMAGIC_IS_ENABLE()) { return; }
18
19 if (bootmagic_scan_keycode(BOOTMAGIC_BOOTLOADER_KEY)) {
20 bootloader_jump();
21 }
22
23 if (bootmagic_scan_keycode(BOOTMAGIC_DEBUG_ENABLE_KEY)) {
24 eeconfig_write_debug(eeconfig_read_debug() ^ EECONFIG_DEBUG_ENABLE);
25 }
26
27 if (bootmagic_scan_keycode(BOOTMAGIC_EEPROM_CLEAR_KEY)) {
28 eeconfig_init();
29 }
30}
31
32bool bootmagic_scan_keycode(uint8_t keycode)
33{
34 for (uint8_t r = 0; r < MATRIX_ROWS; r++) {
35 matrix_row_t matrix_row = matrix_get_row(r);
36 for (uint8_t c = 0; c < MATRIX_COLS; c++) {
37 if (matrix_row & ((matrix_row_t)1<<c)) {
38 if (keycode == keymap_key_to_keycode(0, (key_t){ .row = r, .col = c })) {
39 return true;
40 }
41 }
42 }
43 }
44 return false;
45}
diff --git a/common/bootmagic.h b/common/bootmagic.h
new file mode 100644
index 000000000..7aa224def
--- /dev/null
+++ b/common/bootmagic.h
@@ -0,0 +1,39 @@
1#ifndef BOOTMAGIC_H
2#define BOOTMAGIC_H
3
4
5#ifndef BOOTMAGIC_IS_ENABLE
6#define BOOTMAGIC_IS_ENABLE() true
7#endif
8
9/* bootloader */
10#ifndef BOOTMAGIC_BOOTLOADER_KEY
11#define BOOTMAGIC_BOOTLOADER_KEY KC_B
12#endif
13/* debug enable */
14#ifndef BOOTMAGIC_DEBUG_ENABLE_KEY
15#define BOOTMAGIC_DEBUG_ENABLE_KEY KC_D
16#endif
17/* eeprom clear */
18#ifndef BOOTMAGIC_EEPROM_CLEAR_KEY
19#define BOOTMAGIC_EEPROM_CLEAR_KEY KC_BSPACE
20#endif
21
22/* change default layer */
23#ifndef BOOTMAGIC_DEFAULT_LAYER_0_KEY
24#define BOOTMAGIC_DEFAULT_LAYER_0_KEY KC_0
25#endif
26#ifndef BOOTMAGIC_DEFAULT_LAYER_1_KEY
27#define BOOTMAGIC_DEFAULT_LAYER_1_KEY KC_1
28#endif
29#ifndef BOOTMAGIC_DEFAULT_LAYER_2_KEY
30#define BOOTMAGIC_DEFAULT_LAYER_2_KEY KC_2
31#endif
32#ifndef BOOTMAGIC_DEFAULT_LAYER_3_KEY
33#define BOOTMAGIC_DEFAULT_LAYER_3_KEY KC_3
34#endif
35
36void bootmagic(void);
37bool bootmagic_scan_keycode(uint8_t keycode);
38
39#endif
diff --git a/common/command.c b/common/command.c
index 40932e050..b82d1884c 100644
--- a/common/command.c
+++ b/common/command.c
@@ -109,7 +109,7 @@ static void command_common_help(void)
109 print("v: print device version & info\n"); 109 print("v: print device version & info\n");
110 print("t: print timer count\n"); 110 print("t: print timer count\n");
111 print("s: print status\n"); 111 print("s: print status\n");
112 print("e: print eeprom config\n"); 112 print("e: print eeprom boot config\n");
113#ifdef NKRO_ENABLE 113#ifdef NKRO_ENABLE
114 print("n: toggle NKRO\n"); 114 print("n: toggle NKRO\n");
115#endif 115#endif
@@ -127,8 +127,6 @@ static void print_eeprom_config(void)
127{ 127{
128 uint8_t eebyte; 128 uint8_t eebyte;
129 129
130 print("magic: "); print_hex16(eeprom_read_word((uint16_t)0)); print("\n");
131
132 eebyte = eeconfig_read_debug(); 130 eebyte = eeconfig_read_debug();
133 print("debug: "); print_hex8(eebyte); print("\n"); 131 print("debug: "); print_hex8(eebyte); print("\n");
134 132
diff --git a/common/keyboard.c b/common/keyboard.c
index 2206f1675..0a0bacd43 100644
--- a/common/keyboard.c
+++ b/common/keyboard.c
@@ -28,7 +28,7 @@ along with this program. If not, see <http://www.gnu.org/licenses/>.
28#include "command.h" 28#include "command.h"
29#include "util.h" 29#include "util.h"
30#include "sendchar.h" 30#include "sendchar.h"
31#include "bootloader.h" 31#include "bootmagic.h"
32#ifdef MOUSEKEY_ENABLE 32#ifdef MOUSEKEY_ENABLE
33#include "mousekey.h" 33#include "mousekey.h"
34#endif 34#endif
@@ -64,27 +64,7 @@ void keyboard_init(void)
64 ps2_mouse_init(); 64 ps2_mouse_init();
65#endif 65#endif
66 66
67 /* matrix scan for boot magic keys */ 67 bootmagic();
68#ifdef DEBOUNCE
69 uint8_t scan = DEBOUNCE * 2;
70 while (scan--) { matrix_scan(); _delay_ms(1); }
71#else
72 matrix_scan();
73#endif
74
75 /* boot magic keys */
76#ifdef IS_BOOTMAGIC_BOOTLOADER
77 /* kick up bootloader */
78 if (IS_BOOTMAGIC_BOOTLOADER()) bootloader_jump();
79#endif
80#ifdef IS_BOOTMAGIC_DEBUG
81 if (IS_BOOTMAGIC_DEBUG()) {
82 eeconfig_write_debug(eeconfig_read_debug() ^ EECONFIG_DEBUG_ENABLE);
83 }
84#endif
85#ifdef IS_BOOTMAGIC_EEPROM_CLEAR
86 if (IS_BOOTMAGIC_EEPROM_CLEAR()) eeconfig_init();
87#endif
88 68
89 if (eeconfig_initialized()) { 69 if (eeconfig_initialized()) {
90 uint8_t config; 70 uint8_t config;
@@ -96,7 +76,6 @@ void keyboard_init(void)
96 } else { 76 } else {
97 eeconfig_init(); 77 eeconfig_init();
98 } 78 }
99
100} 79}
101 80
102/* 81/*
diff --git a/keyboard/gh60/keymap.c b/keyboard/gh60/keymap.c
index 1f5344d4c..d6af16961 100644
--- a/keyboard/gh60/keymap.c
+++ b/keyboard/gh60/keymap.c
@@ -231,7 +231,8 @@ uint8_t keymap_key_to_keycode(uint8_t layer, key_t key)
231 if (layer < OVERLAYS_SIZE) { 231 if (layer < OVERLAYS_SIZE) {
232 return pgm_read_byte(&overlays[(layer)][(key.row)][(key.col)]); 232 return pgm_read_byte(&overlays[(layer)][(key.row)][(key.col)]);
233 } else { 233 } else {
234 debug("key_to_keycode: overlay "); debug_dec(layer); debug(" is invalid.\n"); 234 // XXX: this may cuaes bootlaoder_jump incositent fail.
235 //debug("key_to_keycode: overlay "); debug_dec(layer); debug(" is invalid.\n");
235 return KC_TRANSPARENT; 236 return KC_TRANSPARENT;
236 } 237 }
237 } 238 }
@@ -240,8 +241,9 @@ uint8_t keymap_key_to_keycode(uint8_t layer, key_t key)
240 if (layer < KEYMAPS_SIZE) { 241 if (layer < KEYMAPS_SIZE) {
241 return pgm_read_byte(&keymaps[(layer)][(key.row)][(key.col)]); 242 return pgm_read_byte(&keymaps[(layer)][(key.row)][(key.col)]);
242 } else { 243 } else {
244 // XXX: this may cuaes bootlaoder_jump incositent fail.
245 //debug("key_to_keycode: base "); debug_dec(layer); debug(" is invalid.\n");
243 // fall back to layer 0 246 // fall back to layer 0
244 debug("key_to_keycode: base "); debug_dec(layer); debug(" is invalid.\n");
245 return pgm_read_byte(&keymaps[0][(key.row)][(key.col)]); 247 return pgm_read_byte(&keymaps[0][(key.row)][(key.col)]);
246 } 248 }
247 } 249 }
diff --git a/protocol/lufa.mk b/protocol/lufa.mk
index 443b85344..8ea071afb 100644
--- a/protocol/lufa.mk
+++ b/protocol/lufa.mk
@@ -39,4 +39,6 @@ LUFA_OPTS += -D USE_STATIC_OPTIONS="(USB_DEVICE_OPT_FULLSPEED | USB_OPT_REG_ENAB
39OPT_DEFS += -DF_USB=$(F_USB)UL 39OPT_DEFS += -DF_USB=$(F_USB)UL
40OPT_DEFS += -DARCH=ARCH_$(ARCH) 40OPT_DEFS += -DARCH=ARCH_$(ARCH)
41OPT_DEFS += $(LUFA_OPTS) 41OPT_DEFS += $(LUFA_OPTS)
42OPT_DEFS += -DHOST_LUFA 42
43# This indicates using LUFA stack
44OPT_DEFS += -DPROTOCOL_LUFA
diff --git a/protocol/pjrc.mk b/protocol/pjrc.mk
index cccdf6204..f5693ba99 100644
--- a/protocol/pjrc.mk
+++ b/protocol/pjrc.mk
@@ -1,7 +1,5 @@
1PJRC_DIR = protocol/pjrc 1PJRC_DIR = protocol/pjrc
2 2
3OPT_DEFS += -DHOST_PJRC
4
5SRC += $(PJRC_DIR)/main.c \ 3SRC += $(PJRC_DIR)/main.c \
6 $(PJRC_DIR)/pjrc.c \ 4 $(PJRC_DIR)/pjrc.c \
7 $(PJRC_DIR)/usb_keyboard.c \ 5 $(PJRC_DIR)/usb_keyboard.c \
@@ -19,3 +17,9 @@ endif
19 17
20# Search Path 18# Search Path
21VPATH += $(TOP_DIR)/$(PJRC_DIR) 19VPATH += $(TOP_DIR)/$(PJRC_DIR)
20
21# This indicates using LUFA stack
22# TODO: remove HOST_PJRC
23OPT_DEFS += -DHOST_PJRC
24OPT_DEFS += -DPROTOCOL_PJRC
25