From 4d64fd8faa8b1a0ceb9019446ba6915aaf1812ea Mon Sep 17 00:00:00 2001 From: tmk Date: Sat, 9 Mar 2013 11:22:27 +0900 Subject: [PATCH] Add bootmagic.c and fix bootloader_jump --- common.mk | 1 + common/bootloader.c | 100 +++++++++++++++++++++++++++++++---------- common/bootmagic.c | 45 +++++++++++++++++++ common/bootmagic.h | 39 ++++++++++++++++ common/command.c | 4 +- common/keyboard.c | 25 +---------- keyboard/gh60/keymap.c | 6 ++- protocol/lufa.mk | 4 +- protocol/pjrc.mk | 8 +++- 9 files changed, 178 insertions(+), 54 deletions(-) create mode 100644 common/bootmagic.c create mode 100644 common/bootmagic.h diff --git a/common.mk b/common.mk index a99e335e..2735f5be 100644 --- a/common.mk +++ b/common.mk @@ -10,6 +10,7 @@ SRC += $(COMMON_DIR)/host.c \ $(COMMON_DIR)/print.c \ $(COMMON_DIR)/debug.c \ $(COMMON_DIR)/bootloader.c \ + $(COMMON_DIR)/bootmagic.c \ $(COMMON_DIR)/eeconfig.c \ $(COMMON_DIR)/util.c diff --git a/common/bootloader.c b/common/bootloader.c index 6e04efbb..77fa1b30 100644 --- a/common/bootloader.c +++ b/common/bootloader.c @@ -1,15 +1,16 @@ +#include +#include #include #include +#include #include #include "bootloader.h" -/* Start Bootloader from Application - * See - * http://www.pjrc.com/teensy/jump_to_bootloader.html - * http://www.fourwalledcubicle.com/files/LUFA/Doc/120219/html/_page__software_bootloader_start.html - */ +#ifdef PROTOCOL_LUFA +#include +#endif + -// TODO: support usbasp /* Boot Section Size in bytes * Teensy halfKay 512 * Atmel DFU loader 4096 @@ -18,28 +19,82 @@ #ifndef BOOT_SIZE #define BOOT_SIZE 512 #endif - #define FLASH_SIZE (FLASHEND + 1) -#define BOOTLOADER_START (FLASHEND - BOOT_SIZE) +#define BOOTLOADER_START (FLASH_SIZE - BOOT_SIZE) + +/* + * Entering the Bootloader via Software + * http://www.fourwalledcubicle.com/files/LUFA/Doc/120730/html/_page__software_bootloader_start.html + */ +#define BOOTLOADER_RESET_KEY 0xB007B007 +uint32_t reset_key __attribute__ ((section (".noinit"))); + +/* initialize MCU status by watchdog reset */ void bootloader_jump(void) { +#ifdef PROTOCOL_LUFA + USB_Disable(); cli(); + _delay_ms(2000); +#endif - // - //Teensy - // -#if defined(__AVR_AT90USB162__) || defined(__AVR_ATmega32U4__) || defined(__AVR_AT90USB646__) || defined(__AVR_AT90USB1286__) - // disable watchdog, if enabled - // disable all peripherals +#ifdef PROTOCOL_PJRC + cli(); UDCON = 1; - USBCON = (1< +#include +#include +#include "matrix.h" +#include "keymap.h" +#include "eeconfig.h" +#include "bootloader.h" +#include "bootmagic.h" + + +void bootmagic(void) +{ + /* do scans in case of bounce */ + uint8_t scan = 100; + while (scan--) { matrix_scan(); _delay_ms(1); } + + if (!BOOTMAGIC_IS_ENABLE()) { return; } + + if (bootmagic_scan_keycode(BOOTMAGIC_BOOTLOADER_KEY)) { + bootloader_jump(); + } + + if (bootmagic_scan_keycode(BOOTMAGIC_DEBUG_ENABLE_KEY)) { + eeconfig_write_debug(eeconfig_read_debug() ^ EECONFIG_DEBUG_ENABLE); + } + + if (bootmagic_scan_keycode(BOOTMAGIC_EEPROM_CLEAR_KEY)) { + eeconfig_init(); + } +} + +bool bootmagic_scan_keycode(uint8_t keycode) +{ + for (uint8_t r = 0; r < MATRIX_ROWS; r++) { + matrix_row_t matrix_row = matrix_get_row(r); + for (uint8_t c = 0; c < MATRIX_COLS; c++) { + if (matrix_row & ((matrix_row_t)1<. #include "command.h" #include "util.h" #include "sendchar.h" -#include "bootloader.h" +#include "bootmagic.h" #ifdef MOUSEKEY_ENABLE #include "mousekey.h" #endif @@ -64,27 +64,7 @@ void keyboard_init(void) ps2_mouse_init(); #endif - /* matrix scan for boot magic keys */ -#ifdef DEBOUNCE - uint8_t scan = DEBOUNCE * 2; - while (scan--) { matrix_scan(); _delay_ms(1); } -#else - matrix_scan(); -#endif - - /* boot magic keys */ -#ifdef IS_BOOTMAGIC_BOOTLOADER - /* kick up bootloader */ - if (IS_BOOTMAGIC_BOOTLOADER()) bootloader_jump(); -#endif -#ifdef IS_BOOTMAGIC_DEBUG - if (IS_BOOTMAGIC_DEBUG()) { - eeconfig_write_debug(eeconfig_read_debug() ^ EECONFIG_DEBUG_ENABLE); - } -#endif -#ifdef IS_BOOTMAGIC_EEPROM_CLEAR - if (IS_BOOTMAGIC_EEPROM_CLEAR()) eeconfig_init(); -#endif + bootmagic(); if (eeconfig_initialized()) { uint8_t config; @@ -96,7 +76,6 @@ void keyboard_init(void) } else { eeconfig_init(); } - } /* diff --git a/keyboard/gh60/keymap.c b/keyboard/gh60/keymap.c index 1f5344d4..d6af1696 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) if (layer < OVERLAYS_SIZE) { return pgm_read_byte(&overlays[(layer)][(key.row)][(key.col)]); } else { - debug("key_to_keycode: overlay "); debug_dec(layer); debug(" is invalid.\n"); + // XXX: this may cuaes bootlaoder_jump incositent fail. + //debug("key_to_keycode: overlay "); debug_dec(layer); debug(" is invalid.\n"); return KC_TRANSPARENT; } } @@ -240,8 +241,9 @@ uint8_t keymap_key_to_keycode(uint8_t layer, key_t key) if (layer < KEYMAPS_SIZE) { return pgm_read_byte(&keymaps[(layer)][(key.row)][(key.col)]); } else { + // XXX: this may cuaes bootlaoder_jump incositent fail. + //debug("key_to_keycode: base "); debug_dec(layer); debug(" is invalid.\n"); // fall back to layer 0 - debug("key_to_keycode: base "); debug_dec(layer); debug(" is invalid.\n"); return pgm_read_byte(&keymaps[0][(key.row)][(key.col)]); } } diff --git a/protocol/lufa.mk b/protocol/lufa.mk index 443b8534..8ea071af 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 OPT_DEFS += -DF_USB=$(F_USB)UL OPT_DEFS += -DARCH=ARCH_$(ARCH) OPT_DEFS += $(LUFA_OPTS) -OPT_DEFS += -DHOST_LUFA + +# This indicates using LUFA stack +OPT_DEFS += -DPROTOCOL_LUFA diff --git a/protocol/pjrc.mk b/protocol/pjrc.mk index cccdf620..f5693ba9 100644 --- a/protocol/pjrc.mk +++ b/protocol/pjrc.mk @@ -1,7 +1,5 @@ PJRC_DIR = protocol/pjrc -OPT_DEFS += -DHOST_PJRC - SRC += $(PJRC_DIR)/main.c \ $(PJRC_DIR)/pjrc.c \ $(PJRC_DIR)/usb_keyboard.c \ @@ -19,3 +17,9 @@ endif # Search Path VPATH += $(TOP_DIR)/$(PJRC_DIR) + +# This indicates using LUFA stack +# TODO: remove HOST_PJRC +OPT_DEFS += -DHOST_PJRC +OPT_DEFS += -DPROTOCOL_PJRC + -- 2.39.3