]> git.donarmstrong.com Git - tmk_firmware.git/commitdiff
Fix common files for mbed
authortmk <nobody@nowhere>
Tue, 17 Jun 2014 13:41:14 +0000 (22:41 +0900)
committertmk <nobody@nowhere>
Wed, 30 Jul 2014 05:07:43 +0000 (14:07 +0900)
26 files changed:
common.mk
common/avr/bootloader.c [new file with mode: 0644]
common/avr/eeconfig.c [new file with mode: 0644]
common/avr/suspend.c [new file with mode: 0644]
common/avr/suspend_avr.h [new file with mode: 0644]
common/bootloader.c [deleted file]
common/eeconfig.c [deleted file]
common/host.h
common/keyboard.c
common/keymap.c
common/mbed/bootloader.c [new file with mode: 0644]
common/mbed/suspend.c [new file with mode: 0644]
common/mbed/xprintf.cpp [new file with mode: 0644]
common/mbed/xprintf.h [new file with mode: 0644]
common/mousekey.c
common/mousekey.h
common/progmem.h [new file with mode: 0644]
common/suspend.c [deleted file]
common/suspend.h
keyboard/mbed_onekey/Makefile
keyboard/mbed_onekey/common.mk
keyboard/mbed_onekey/config.h [new file with mode: 0644]
keyboard/mbed_onekey/gcc.mk
keyboard/mbed_onekey/main.cpp
keyboard/mbed_onekey/mbed_driver.cpp [new file with mode: 0644]
keyboard/mbed_onekey/mbed_driver.h [new file with mode: 0644]

index 9b5ef0ea88f6e420d1ec70cc3145c356ea7ab172..04f036477115de2a1410d5e7fb517ec3f2d2ed2c 100644 (file)
--- a/common.mk
+++ b/common.mk
@@ -7,18 +7,18 @@ SRC +=        $(COMMON_DIR)/host.c \
        $(COMMON_DIR)/action_layer.c \
        $(COMMON_DIR)/action_util.c \
        $(COMMON_DIR)/keymap.c \
-       $(COMMON_DIR)/avr/timer.c \
        $(COMMON_DIR)/print.c \
-       $(COMMON_DIR)/bootloader.c \
-       $(COMMON_DIR)/suspend.c \
+       $(COMMON_DIR)/util.c \
+       $(COMMON_DIR)/avr/suspend.c \
        $(COMMON_DIR)/avr/xprintf.S \
-       $(COMMON_DIR)/util.c
+       $(COMMON_DIR)/avr/timer.c \
+       $(COMMON_DIR)/avr/bootloader.c
 
 
 # Option modules
 ifdef BOOTMAGIC_ENABLE
     SRC += $(COMMON_DIR)/bootmagic.c
-    SRC += $(COMMON_DIR)/eeconfig.c
+    SRC += $(COMMON_DIR)/avr/eeconfig.c
     OPT_DEFS += -DBOOTMAGIC_ENABLE
 endif
 
diff --git a/common/avr/bootloader.c b/common/avr/bootloader.c
new file mode 100644 (file)
index 0000000..cda295b
--- /dev/null
@@ -0,0 +1,148 @@
+#include <stdint.h>
+#include <stdbool.h>
+#include <avr/io.h>
+#include <avr/interrupt.h>
+#include <avr/wdt.h>
+#include <util/delay.h>
+#include "bootloader.h"
+
+#ifdef PROTOCOL_LUFA
+#include <LUFA/Drivers/USB/USB.h>
+#endif
+
+
+/* Boot Section Size in *BYTEs*
+ *   Teensy   halfKay    512
+ *   Teensy++ halfKay    1024
+ *   Atmel DFU loader    4096
+ *   LUFA bootloader     4096
+ *   USBaspLoader        2048
+ */
+#ifndef BOOTLOADER_SIZE
+#warning To use bootloader_jump() you need to define BOOTLOADER_SIZE in config.h.
+#define BOOTLOADER_SIZE     4096
+#endif
+
+#define FLASH_SIZE          (FLASHEND + 1L)
+#define BOOTLOADER_START    (FLASH_SIZE - BOOTLOADER_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
+
+#ifdef PROTOCOL_PJRC
+    cli();
+    UDCON = 1;
+    USBCON = (1<<FRZCLK);
+    UCSR1B = 0;
+    _delay_ms(5);
+#endif
+
+    // watchdog reset
+    reset_key = BOOTLOADER_RESET_KEY;
+    wdt_enable(WDTO_250MS);
+    for (;;);
+}
+
+
+/* this runs before main() */
+void bootloader_jump_after_watchdog_reset(void) __attribute__ ((used, naked, section (".init3")));
+void bootloader_jump_after_watchdog_reset(void)
+{
+    if ((MCUSR & (1<<WDRF)) && reset_key == BOOTLOADER_RESET_KEY) {
+        reset_key = 0;
+
+        // My custom USBasploader requires this to come up.
+        MCUSR = 0;
+
+        // Seems like Teensy halfkay loader requires clearing WDRF and disabling watchdog.
+        MCUSR &= ~(1<<WDRF);
+        wdt_disable();
+
+        // This is compled into 'icall', address should be in word unit, not byte.
+        ((void (*)(void))(BOOTLOADER_START/2))();
+    }
+}
+
+
+#if 0
+/* Jumping To The Bootloader
+ * http://www.pjrc.com/teensy/jump_to_bootloader.html
+ * 
+ * This method doen't work when using LUFA. idk why.
+ * - needs to initialize more regisers or interrupt setting?
+ */
+void bootloader_jump(void) {
+#ifdef PROTOCOL_LUFA
+    USB_Disable();
+    cli();
+    _delay_ms(2000);
+#endif
+
+#ifdef PROTOCOL_PJRC
+    cli();
+    UDCON = 1;
+    USBCON = (1<<FRZCLK);
+    UCSR1B = 0;
+    _delay_ms(5);
+#endif
+
+    /*
+     * Initialize
+     */
+#if defined(__AVR_AT90USB162__)
+    EIMSK = 0; PCICR = 0; SPCR = 0; ACSR = 0; EECR = 0;
+    TIMSK0 = 0; TIMSK1 = 0; UCSR1B = 0;
+    DDRB = 0; DDRC = 0; DDRD = 0;
+    PORTB = 0; PORTC = 0; PORTD = 0;
+#elif defined(__AVR_ATmega32U4__)
+    EIMSK = 0; PCICR = 0; SPCR = 0; ACSR = 0; EECR = 0; ADCSRA = 0;
+    TIMSK0 = 0; TIMSK1 = 0; TIMSK3 = 0; TIMSK4 = 0; UCSR1B = 0; TWCR = 0;
+    DDRB = 0; DDRC = 0; DDRD = 0; DDRE = 0; DDRF = 0; TWCR = 0;
+    PORTB = 0; PORTC = 0; PORTD = 0; PORTE = 0; PORTF = 0;
+#elif defined(__AVR_AT90USB646__)
+    EIMSK = 0; PCICR = 0; SPCR = 0; ACSR = 0; EECR = 0; ADCSRA = 0;
+    TIMSK0 = 0; TIMSK1 = 0; TIMSK2 = 0; TIMSK3 = 0; UCSR1B = 0; TWCR = 0;
+    DDRA = 0; DDRB = 0; DDRC = 0; DDRD = 0; DDRE = 0; DDRF = 0;
+    PORTA = 0; PORTB = 0; PORTC = 0; PORTD = 0; PORTE = 0; PORTF = 0;
+#elif defined(__AVR_AT90USB1286__)
+    EIMSK = 0; PCICR = 0; SPCR = 0; ACSR = 0; EECR = 0; ADCSRA = 0;
+    TIMSK0 = 0; TIMSK1 = 0; TIMSK2 = 0; TIMSK3 = 0; UCSR1B = 0; TWCR = 0;
+    DDRA = 0; DDRB = 0; DDRC = 0; DDRD = 0; DDRE = 0; DDRF = 0;
+    PORTA = 0; PORTB = 0; PORTC = 0; PORTD = 0; PORTE = 0; PORTF = 0;
+#endif
+
+    /*
+     * USBaspLoader
+     */
+#if defined(__AVR_ATmega168__) || defined(__AVR_ATmega168P__) || defined(__AVR_ATmega328P__)
+    // This makes custom USBasploader come up.
+    MCUSR = 0;
+
+    // initialize ports
+    PORTB = 0; PORTC= 0; PORTD = 0;
+    DDRB = 0; DDRC= 0; DDRD = 0;
+
+    // disable interrupts
+    EIMSK = 0; EECR = 0; SPCR = 0;
+    ACSR = 0; SPMCSR = 0; WDTCSR = 0; PCICR = 0;
+    TIMSK0 = 0; TIMSK1 = 0; TIMSK2 = 0;
+    ADCSRA = 0; TWCR = 0; UCSR0B = 0;
+#endif
+
+    // This is compled into 'icall', address should be in word unit, not byte.
+    ((void (*)(void))(BOOTLOADER_START/2))();
+}
+#endif
diff --git a/common/avr/eeconfig.c b/common/avr/eeconfig.c
new file mode 100644 (file)
index 0000000..5bd47dc
--- /dev/null
@@ -0,0 +1,45 @@
+#include <stdint.h>
+#include <stdbool.h>
+#include <avr/eeprom.h>
+#include "eeconfig.h"
+
+void eeconfig_init(void)
+{
+    eeprom_write_word(EECONFIG_MAGIC,          EECONFIG_MAGIC_NUMBER);
+    eeprom_write_byte(EECONFIG_DEBUG,          0);
+    eeprom_write_byte(EECONFIG_DEFAULT_LAYER,  0);
+    eeprom_write_byte(EECONFIG_KEYMAP,         0);
+    eeprom_write_byte(EECONFIG_MOUSEKEY_ACCEL, 0);
+#ifdef BACKLIGHT_ENABLE
+    eeprom_write_byte(EECONFIG_BACKLIGHT,      0);
+#endif
+}
+
+void eeconfig_enable(void)
+{
+    eeprom_write_word(EECONFIG_MAGIC, EECONFIG_MAGIC_NUMBER);
+}
+
+void eeconfig_disable(void)
+{
+    eeprom_write_word(EECONFIG_MAGIC, 0xFFFF);
+}
+
+bool eeconfig_is_enabled(void)
+{
+    return (eeprom_read_word(EECONFIG_MAGIC) == EECONFIG_MAGIC_NUMBER);
+}
+
+uint8_t eeconfig_read_debug(void)      { return eeprom_read_byte(EECONFIG_DEBUG); }
+void eeconfig_write_debug(uint8_t val) { eeprom_write_byte(EECONFIG_DEBUG, val); }
+
+uint8_t eeconfig_read_default_layer(void)      { return eeprom_read_byte(EECONFIG_DEFAULT_LAYER); }
+void eeconfig_write_default_layer(uint8_t val) { eeprom_write_byte(EECONFIG_DEFAULT_LAYER, val); }
+
+uint8_t eeconfig_read_keymap(void)      { return eeprom_read_byte(EECONFIG_KEYMAP); }
+void eeconfig_write_keymap(uint8_t val) { eeprom_write_byte(EECONFIG_KEYMAP, val); }
+
+#ifdef BACKLIGHT_ENABLE
+uint8_t eeconfig_read_backlight(void)      { return eeprom_read_byte(EECONFIG_BACKLIGHT); }
+void eeconfig_write_backlight(uint8_t val) { eeprom_write_byte(EECONFIG_BACKLIGHT, val); }
+#endif
diff --git a/common/avr/suspend.c b/common/avr/suspend.c
new file mode 100644 (file)
index 0000000..f44a036
--- /dev/null
@@ -0,0 +1,98 @@
+#include <stdbool.h>
+#include <avr/sleep.h>
+#include <avr/wdt.h>
+#include <avr/interrupt.h>
+#include "matrix.h"
+#include "action.h"
+#include "backlight.h"
+#include "suspend_avr.h"
+#include "suspend.h"
+
+
+#define wdt_intr_enable(value)   \
+__asm__ __volatile__ (  \
+    "in __tmp_reg__,__SREG__" "\n\t"    \
+    "cli" "\n\t"    \
+    "wdr" "\n\t"    \
+    "sts %0,%1" "\n\t"  \
+    "out __SREG__,__tmp_reg__" "\n\t"   \
+    "sts %0,%2" "\n\t" \
+    : /* no outputs */  \
+    : "M" (_SFR_MEM_ADDR(_WD_CONTROL_REG)), \
+    "r" (_BV(_WD_CHANGE_BIT) | _BV(WDE)), \
+    "r" ((uint8_t) ((value & 0x08 ? _WD_PS3_MASK : 0x00) | \
+        _BV(WDIE) | (value & 0x07)) ) \
+    : "r0"  \
+)
+
+
+void suspend_power_down(void)
+{
+#ifdef BACKLIGHT_ENABLE
+    backlight_set(0);
+#endif
+#ifndef NO_SUSPEND_POWER_DOWN
+    // Enable watchdog to wake from MCU sleep
+    cli();
+    wdt_reset();
+
+    // Watchdog Interrupt and System Reset Mode
+    //wdt_enable(WDTO_1S);
+    //WDTCSR |= _BV(WDIE);
+    
+    // Watchdog Interrupt Mode
+    wdt_intr_enable(WDTO_120MS);
+    
+    // TODO: more power saving
+    // See PicoPower application note
+    // - I/O port input with pullup
+    // - prescale clock
+    // - BOD disable
+    // - Power Reduction Register PRR
+    // sleep in power down mode
+    set_sleep_mode(SLEEP_MODE_PWR_DOWN);
+    sleep_enable();
+    sei();
+    sleep_cpu();
+    sleep_disable();
+
+    // Disable watchdog after sleep
+    wdt_disable();
+#endif
+}
+
+bool suspend_wakeup_condition(void)
+{
+    matrix_scan();
+    for (uint8_t r = 0; r < MATRIX_ROWS; r++) {
+        if (matrix_get_row(r)) return true;
+    }
+    return false;
+}
+
+// run immediately after wakeup
+void suspend_wakeup_init(void)
+{
+    // clear keyboard state
+    clear_keyboard();
+#ifdef BACKLIGHT_ENABLE
+    backlight_init();
+#endif
+}
+
+#ifndef NO_SUSPEND_POWER_DOWN
+/* watchdog timeout */
+ISR(WDT_vect)
+{
+    /* wakeup from MCU sleep mode */
+/*
+    // blink LED
+    static uint8_t led_state = 0;
+    static uint8_t led_count = 0;
+    led_count++;
+    if ((led_count & 0x07) == 0) {
+        led_set((led_state ^= (1<<USB_LED_CAPS_LOCK)));
+    }
+*/
+}
+#endif
diff --git a/common/avr/suspend_avr.h b/common/avr/suspend_avr.h
new file mode 100644 (file)
index 0000000..357102d
--- /dev/null
@@ -0,0 +1,27 @@
+#ifndef SUSPEND_AVR_H
+#define SUSPEND_AVR_H
+
+#include <stdint.h>
+#include <stdbool.h>
+#include <avr/sleep.h>
+#include <avr/wdt.h>
+#include <avr/interrupt.h>
+
+
+#define wdt_intr_enable(value)   \
+__asm__ __volatile__ (  \
+    "in __tmp_reg__,__SREG__" "\n\t"    \
+    "cli" "\n\t"    \
+    "wdr" "\n\t"    \
+    "sts %0,%1" "\n\t"  \
+    "out __SREG__,__tmp_reg__" "\n\t"   \
+    "sts %0,%2" "\n\t" \
+    : /* no outputs */  \
+    : "M" (_SFR_MEM_ADDR(_WD_CONTROL_REG)), \
+    "r" (_BV(_WD_CHANGE_BIT) | _BV(WDE)), \
+    "r" ((uint8_t) ((value & 0x08 ? _WD_PS3_MASK : 0x00) | \
+        _BV(WDIE) | (value & 0x07)) ) \
+    : "r0"  \
+)
+
+#endif
diff --git a/common/bootloader.c b/common/bootloader.c
deleted file mode 100644 (file)
index cda295b..0000000
+++ /dev/null
@@ -1,148 +0,0 @@
-#include <stdint.h>
-#include <stdbool.h>
-#include <avr/io.h>
-#include <avr/interrupt.h>
-#include <avr/wdt.h>
-#include <util/delay.h>
-#include "bootloader.h"
-
-#ifdef PROTOCOL_LUFA
-#include <LUFA/Drivers/USB/USB.h>
-#endif
-
-
-/* Boot Section Size in *BYTEs*
- *   Teensy   halfKay    512
- *   Teensy++ halfKay    1024
- *   Atmel DFU loader    4096
- *   LUFA bootloader     4096
- *   USBaspLoader        2048
- */
-#ifndef BOOTLOADER_SIZE
-#warning To use bootloader_jump() you need to define BOOTLOADER_SIZE in config.h.
-#define BOOTLOADER_SIZE     4096
-#endif
-
-#define FLASH_SIZE          (FLASHEND + 1L)
-#define BOOTLOADER_START    (FLASH_SIZE - BOOTLOADER_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
-
-#ifdef PROTOCOL_PJRC
-    cli();
-    UDCON = 1;
-    USBCON = (1<<FRZCLK);
-    UCSR1B = 0;
-    _delay_ms(5);
-#endif
-
-    // watchdog reset
-    reset_key = BOOTLOADER_RESET_KEY;
-    wdt_enable(WDTO_250MS);
-    for (;;);
-}
-
-
-/* this runs before main() */
-void bootloader_jump_after_watchdog_reset(void) __attribute__ ((used, naked, section (".init3")));
-void bootloader_jump_after_watchdog_reset(void)
-{
-    if ((MCUSR & (1<<WDRF)) && reset_key == BOOTLOADER_RESET_KEY) {
-        reset_key = 0;
-
-        // My custom USBasploader requires this to come up.
-        MCUSR = 0;
-
-        // Seems like Teensy halfkay loader requires clearing WDRF and disabling watchdog.
-        MCUSR &= ~(1<<WDRF);
-        wdt_disable();
-
-        // This is compled into 'icall', address should be in word unit, not byte.
-        ((void (*)(void))(BOOTLOADER_START/2))();
-    }
-}
-
-
-#if 0
-/* Jumping To The Bootloader
- * http://www.pjrc.com/teensy/jump_to_bootloader.html
- * 
- * This method doen't work when using LUFA. idk why.
- * - needs to initialize more regisers or interrupt setting?
- */
-void bootloader_jump(void) {
-#ifdef PROTOCOL_LUFA
-    USB_Disable();
-    cli();
-    _delay_ms(2000);
-#endif
-
-#ifdef PROTOCOL_PJRC
-    cli();
-    UDCON = 1;
-    USBCON = (1<<FRZCLK);
-    UCSR1B = 0;
-    _delay_ms(5);
-#endif
-
-    /*
-     * Initialize
-     */
-#if defined(__AVR_AT90USB162__)
-    EIMSK = 0; PCICR = 0; SPCR = 0; ACSR = 0; EECR = 0;
-    TIMSK0 = 0; TIMSK1 = 0; UCSR1B = 0;
-    DDRB = 0; DDRC = 0; DDRD = 0;
-    PORTB = 0; PORTC = 0; PORTD = 0;
-#elif defined(__AVR_ATmega32U4__)
-    EIMSK = 0; PCICR = 0; SPCR = 0; ACSR = 0; EECR = 0; ADCSRA = 0;
-    TIMSK0 = 0; TIMSK1 = 0; TIMSK3 = 0; TIMSK4 = 0; UCSR1B = 0; TWCR = 0;
-    DDRB = 0; DDRC = 0; DDRD = 0; DDRE = 0; DDRF = 0; TWCR = 0;
-    PORTB = 0; PORTC = 0; PORTD = 0; PORTE = 0; PORTF = 0;
-#elif defined(__AVR_AT90USB646__)
-    EIMSK = 0; PCICR = 0; SPCR = 0; ACSR = 0; EECR = 0; ADCSRA = 0;
-    TIMSK0 = 0; TIMSK1 = 0; TIMSK2 = 0; TIMSK3 = 0; UCSR1B = 0; TWCR = 0;
-    DDRA = 0; DDRB = 0; DDRC = 0; DDRD = 0; DDRE = 0; DDRF = 0;
-    PORTA = 0; PORTB = 0; PORTC = 0; PORTD = 0; PORTE = 0; PORTF = 0;
-#elif defined(__AVR_AT90USB1286__)
-    EIMSK = 0; PCICR = 0; SPCR = 0; ACSR = 0; EECR = 0; ADCSRA = 0;
-    TIMSK0 = 0; TIMSK1 = 0; TIMSK2 = 0; TIMSK3 = 0; UCSR1B = 0; TWCR = 0;
-    DDRA = 0; DDRB = 0; DDRC = 0; DDRD = 0; DDRE = 0; DDRF = 0;
-    PORTA = 0; PORTB = 0; PORTC = 0; PORTD = 0; PORTE = 0; PORTF = 0;
-#endif
-
-    /*
-     * USBaspLoader
-     */
-#if defined(__AVR_ATmega168__) || defined(__AVR_ATmega168P__) || defined(__AVR_ATmega328P__)
-    // This makes custom USBasploader come up.
-    MCUSR = 0;
-
-    // initialize ports
-    PORTB = 0; PORTC= 0; PORTD = 0;
-    DDRB = 0; DDRC= 0; DDRD = 0;
-
-    // disable interrupts
-    EIMSK = 0; EECR = 0; SPCR = 0;
-    ACSR = 0; SPMCSR = 0; WDTCSR = 0; PCICR = 0;
-    TIMSK0 = 0; TIMSK1 = 0; TIMSK2 = 0;
-    ADCSRA = 0; TWCR = 0; UCSR0B = 0;
-#endif
-
-    // This is compled into 'icall', address should be in word unit, not byte.
-    ((void (*)(void))(BOOTLOADER_START/2))();
-}
-#endif
diff --git a/common/eeconfig.c b/common/eeconfig.c
deleted file mode 100644 (file)
index 5bd47dc..0000000
+++ /dev/null
@@ -1,45 +0,0 @@
-#include <stdint.h>
-#include <stdbool.h>
-#include <avr/eeprom.h>
-#include "eeconfig.h"
-
-void eeconfig_init(void)
-{
-    eeprom_write_word(EECONFIG_MAGIC,          EECONFIG_MAGIC_NUMBER);
-    eeprom_write_byte(EECONFIG_DEBUG,          0);
-    eeprom_write_byte(EECONFIG_DEFAULT_LAYER,  0);
-    eeprom_write_byte(EECONFIG_KEYMAP,         0);
-    eeprom_write_byte(EECONFIG_MOUSEKEY_ACCEL, 0);
-#ifdef BACKLIGHT_ENABLE
-    eeprom_write_byte(EECONFIG_BACKLIGHT,      0);
-#endif
-}
-
-void eeconfig_enable(void)
-{
-    eeprom_write_word(EECONFIG_MAGIC, EECONFIG_MAGIC_NUMBER);
-}
-
-void eeconfig_disable(void)
-{
-    eeprom_write_word(EECONFIG_MAGIC, 0xFFFF);
-}
-
-bool eeconfig_is_enabled(void)
-{
-    return (eeprom_read_word(EECONFIG_MAGIC) == EECONFIG_MAGIC_NUMBER);
-}
-
-uint8_t eeconfig_read_debug(void)      { return eeprom_read_byte(EECONFIG_DEBUG); }
-void eeconfig_write_debug(uint8_t val) { eeprom_write_byte(EECONFIG_DEBUG, val); }
-
-uint8_t eeconfig_read_default_layer(void)      { return eeprom_read_byte(EECONFIG_DEFAULT_LAYER); }
-void eeconfig_write_default_layer(uint8_t val) { eeprom_write_byte(EECONFIG_DEFAULT_LAYER, val); }
-
-uint8_t eeconfig_read_keymap(void)      { return eeprom_read_byte(EECONFIG_KEYMAP); }
-void eeconfig_write_keymap(uint8_t val) { eeprom_write_byte(EECONFIG_KEYMAP, val); }
-
-#ifdef BACKLIGHT_ENABLE
-uint8_t eeconfig_read_backlight(void)      { return eeprom_read_byte(EECONFIG_BACKLIGHT); }
-void eeconfig_write_backlight(uint8_t val) { eeprom_write_byte(EECONFIG_BACKLIGHT, val); }
-#endif
index a56e6c3b04f05d115eb8d408213c7fbc86f6b445..918af69e8f41242083912519d9486b16f758572a 100644 (file)
@@ -32,8 +32,8 @@ extern "C" {
 extern bool keyboard_nkro;
 #endif
 
-uint8_t keyboard_idle;
-uint8_t keyboard_protocol;
+extern uint8_t keyboard_idle;
+extern uint8_t keyboard_protocol;
 
 
 /* host driver */
index b71d5bf13c9a0050de280807b2dbd0826377bad1..9a809ff4a153dbfca52f1a248cb9e001e26cfd06 100644 (file)
@@ -15,7 +15,6 @@ You should have received a copy of the GNU General Public License
 along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
 #include <stdint.h>
-#include <util/delay.h>
 #include "keyboard.h"
 #include "matrix.h"
 #include "keymap.h"
index 0df2e2edff02024ccbe15d0a927c4e205e2e44ed..4c0b61b8c118cb17d719e82f2149ee158720bc79 100644 (file)
@@ -14,7 +14,6 @@ GNU General Public License for more details.
 You should have received a copy of the GNU General Public License
 along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
-#include <avr/pgmspace.h>
 #include "keymap.h"
 #include "report.h"
 #include "keycode.h"
diff --git a/common/mbed/bootloader.c b/common/mbed/bootloader.c
new file mode 100644 (file)
index 0000000..b51e839
--- /dev/null
@@ -0,0 +1,4 @@
+#include "bootloader.h"
+
+
+void bootloader_jump(void) {}
diff --git a/common/mbed/suspend.c b/common/mbed/suspend.c
new file mode 100644 (file)
index 0000000..3265157
--- /dev/null
@@ -0,0 +1,6 @@
+#include <stdbool.h>
+
+
+void suspend_power_down(void) {}
+bool suspend_wakeup_condition(void) { return true; }
+void suspend_wakeup_init(void) {}
diff --git a/common/mbed/xprintf.cpp b/common/mbed/xprintf.cpp
new file mode 100644 (file)
index 0000000..4342b79
--- /dev/null
@@ -0,0 +1,46 @@
+#include <cstdarg>
+//#include <stdarg.h>
+#include "mbed.h"
+#include "mbed/xprintf.h"
+
+
+#define STRING_STACK_LIMIT    120
+
+/* mbed Serial */
+Serial ser(UART_TX, UART_RX);
+
+/* TODO: Need small implementation for embedded */
+int xprintf(const char* format, ...)
+{
+    /* copy from mbed/common/RawSerial.cpp */
+    std::va_list arg;
+    va_start(arg, format);
+    int len = vsnprintf(NULL, 0, format, arg);
+    if (len < STRING_STACK_LIMIT) {
+        char temp[STRING_STACK_LIMIT];
+        vsprintf(temp, format, arg);
+        ser.puts(temp);
+    } else {
+        char *temp = new char[len + 1];
+        vsprintf(temp, format, arg);
+        ser.puts(temp);
+        delete[] temp;
+    }
+    va_end(arg);
+    return len;
+
+/* Fail: __builtin_va_arg_pack?
+ * https://gcc.gnu.org/onlinedocs/gcc-4.3.5/gcc/Constructing-Calls.html#Constructing-Calls
+    void *arg = __builtin_apply_args();
+    void *ret = __builtin_apply((void*)(&(ser.printf)), arg, 100);
+    __builtin_return(ret)
+*/
+/* Fail: varargs can not be passed to printf
+    //int r = ser.printf("test %i\r\n", 123);
+    va_list arg;
+    va_start(arg, format);
+    int r = ser.printf(format, arg);
+    va_end(arg);
+    return r;
+*/
+}
diff --git a/common/mbed/xprintf.h b/common/mbed/xprintf.h
new file mode 100644 (file)
index 0000000..26bc529
--- /dev/null
@@ -0,0 +1,17 @@
+#ifndef XPRINTF_H
+#define XPRINTF_H
+
+//#define xprintf(format, ...)            __xprintf(format, ##__VA_ARGS__)
+
+#ifdef __cplusplus
+extern "C" {
+#endif
+
+int xprintf(const char *format, ...);
+
+#ifdef __cplusplus
+}
+#endif
+
+
+#endif
index 017be94116f3792cf67561b884d7f3566f9e397f..23469476e264fd8bacdc56682dcf2e23c5a84c2b 100644 (file)
@@ -16,7 +16,6 @@ along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
 
 #include <stdint.h>
-#include <util/delay.h>
 #include "keycode.h"
 #include "host.h"
 #include "timer.h"
index d8d7beaaa3a5f4eda23c386f40be8c22fde62eaf..6eede06b442eccdcb2d5ff21002ac3410e4fc2e2 100644 (file)
@@ -52,12 +52,16 @@ along with this program.  If not, see <http://www.gnu.org/licenses/>.
 #endif
 
 
-uint8_t mk_delay;
-uint8_t mk_interval;
-uint8_t mk_max_speed;
-uint8_t mk_time_to_max;
-uint8_t mk_wheel_max_speed;
-uint8_t mk_wheel_time_to_max;
+#ifdef __cplusplus
+extern "C" {
+#endif
+
+extern uint8_t mk_delay;
+extern uint8_t mk_interval;
+extern uint8_t mk_max_speed;
+extern uint8_t mk_time_to_max;
+extern uint8_t mk_wheel_max_speed;
+extern uint8_t mk_wheel_time_to_max;
 
 
 void mousekey_task(void);
@@ -66,4 +70,8 @@ void mousekey_off(uint8_t code);
 void mousekey_clear(void);
 void mousekey_send(void);
 
+#ifdef __cplusplus
+}
+#endif
+
 #endif
diff --git a/common/progmem.h b/common/progmem.h
new file mode 100644 (file)
index 0000000..09aeb8b
--- /dev/null
@@ -0,0 +1,11 @@
+#ifndef PROGMEM_H
+#define PROGMEM_H 1
+
+#if defined(__AVR__)
+#   include <avr/pgmspace.h>
+#elif defined(__arm__)
+#   define PROGMEM
+#   define pgm_read_byte(p)     *(p)
+#endif
+
+#endif
diff --git a/common/suspend.c b/common/suspend.c
deleted file mode 100644 (file)
index 5b37889..0000000
+++ /dev/null
@@ -1,76 +0,0 @@
-#include "suspend.h"
-#include "matrix.h"
-#include "action.h"
-#include "backlight.h"
-
-
-void suspend_power_down(void)
-{
-#ifdef BACKLIGHT_ENABLE
-    backlight_set(0);
-#endif
-#ifndef NO_SUSPEND_POWER_DOWN
-    // Enable watchdog to wake from MCU sleep
-    cli();
-    wdt_reset();
-
-    // Watchdog Interrupt and System Reset Mode
-    //wdt_enable(WDTO_1S);
-    //WDTCSR |= _BV(WDIE);
-    
-    // Watchdog Interrupt Mode
-    wdt_intr_enable(WDTO_120MS);
-    
-    // TODO: more power saving
-    // See PicoPower application note
-    // - I/O port input with pullup
-    // - prescale clock
-    // - BOD disable
-    // - Power Reduction Register PRR
-    // sleep in power down mode
-    set_sleep_mode(SLEEP_MODE_PWR_DOWN);
-    sleep_enable();
-    sei();
-    sleep_cpu();
-    sleep_disable();
-
-    // Disable watchdog after sleep
-    wdt_disable();
-#endif
-}
-
-bool suspend_wakeup_condition(void)
-{
-    matrix_scan();
-    for (uint8_t r = 0; r < MATRIX_ROWS; r++) {
-        if (matrix_get_row(r)) return true;
-    }
-    return false;
-}
-
-// run immediately after wakeup
-void suspend_wakeup_init(void)
-{
-    // clear keyboard state
-    clear_keyboard();
-#ifdef BACKLIGHT_ENABLE
-    backlight_init();
-#endif
-}
-
-#ifndef NO_SUSPEND_POWER_DOWN
-/* watchdog timeout */
-ISR(WDT_vect)
-{
-    /* wakeup from MCU sleep mode */
-/*
-    // blink LED
-    static uint8_t led_state = 0;
-    static uint8_t led_count = 0;
-    led_count++;
-    if ((led_count & 0x07) == 0) {
-        led_set((led_state ^= (1<<USB_LED_CAPS_LOCK)));
-    }
-*/
-}
-#endif
index 1c1e41ac3e2e57fa42f660e5e587295584656c65..9b76f280d1f182f9c649fd2df07731fb70b3b2cd 100644 (file)
@@ -3,26 +3,6 @@
 
 #include <stdint.h>
 #include <stdbool.h>
-#include <avr/sleep.h>
-#include <avr/wdt.h>
-#include <avr/interrupt.h>
-
-
-#define wdt_intr_enable(value)   \
-__asm__ __volatile__ (  \
-    "in __tmp_reg__,__SREG__" "\n\t"    \
-    "cli" "\n\t"    \
-    "wdr" "\n\t"    \
-    "sts %0,%1" "\n\t"  \
-    "out __SREG__,__tmp_reg__" "\n\t"   \
-    "sts %0,%2" "\n\t" \
-    : /* no outputs */  \
-    : "M" (_SFR_MEM_ADDR(_WD_CONTROL_REG)), \
-    "r" (_BV(_WD_CHANGE_BIT) | _BV(WDE)), \
-    "r" ((uint8_t) ((value & 0x08 ? _WD_PS3_MASK : 0x00) | \
-        _BV(WDIE) | (value & 0x07)) ) \
-    : "r0"  \
-)
 
 
 void suspend_power_down(void);
index 1bc91bc7e7d77be2fcaef4c967e6d5c2571dbcac..e686f424e6fb521d8d3349b00a26db93908125bb 100644 (file)
@@ -18,6 +18,8 @@ OBJECTS = \
        $(OBJDIR)/./mbed_driver.o \
        $(OBJDIR)/./main.o
 
+CONFIG_H = config.h
+
 SYS_OBJECTS = 
 
 INCLUDE_PATHS = -I.
@@ -25,6 +27,12 @@ INCLUDE_PATHS = -I.
 LIBRARY_PATHS = 
 LIBRARIES = 
 
+# Build Options
+#   Comment out to disable
+#BOOTMAGIC_ENABLE = yes
+MOUSEKEY_ENABLE = yes
+
+
 include mbed.mk
 include common.mk
 include gcc.mk
index 6eb7f7699578bd23f9fde1268d424079f4a62ab9..1bd7d6edbee588039ed35055367165fd2b85c11b 100644 (file)
@@ -1,21 +1,79 @@
 COMMON_DIR = common
 OBJECTS += \
-       $(OBJDIR)/$(COMMON_DIR)/mbed/timer.o \
-       $(OBJDIR)/$(COMMON_DIR)/mbed/xprintf.o \
        $(OBJDIR)/$(COMMON_DIR)/action.o \
        $(OBJDIR)/$(COMMON_DIR)/action_tapping.o \
        $(OBJDIR)/$(COMMON_DIR)/action_macro.o \
        $(OBJDIR)/$(COMMON_DIR)/action_layer.o \
        $(OBJDIR)/$(COMMON_DIR)/action_util.o \
        $(OBJDIR)/$(COMMON_DIR)/host.o \
+       $(OBJDIR)/$(COMMON_DIR)/keymap.o \
+       $(OBJDIR)/$(COMMON_DIR)/keyboard.o \
+       $(OBJDIR)/$(COMMON_DIR)/util.o \
+       $(OBJDIR)/$(COMMON_DIR)/mbed/suspend.o \
+       $(OBJDIR)/$(COMMON_DIR)/mbed/timer.o \
+       $(OBJDIR)/$(COMMON_DIR)/mbed/xprintf.o \
+       $(OBJDIR)/$(COMMON_DIR)/mbed/bootloader.o \
 
 INCLUDE_PATHS += \
        -I$(TMK_DIR)/$(COMMON_DIR)
 
+CC_FLAGS += -include $(CONFIG_H)
+
+
+
+# Option modules
+ifdef BOOTMAGIC_ENABLE
+    $(error Not Supported)
+    OBJECTS += $(OBJDIR)/$(COMMON_DIR)/bootmagic.o
+    OBJECTS += $(OBJDIR)/$(COMMON_DIR)/mbed/eeprom.o
+    OPT_DEFS += -DBOOTMAGIC_ENABLE
+endif
+
+ifdef MOUSEKEY_ENABLE
+    OBJECTS += $(OBJDIR)/$(COMMON_DIR)/mousekey.o
+    OPT_DEFS += -DMOUSEKEY_ENABLE
+    OPT_DEFS += -DMOUSE_ENABLE
+endif
+
+ifdef EXTRAKEY_ENABLE
+    $(error Not Supported)
+    OPT_DEFS += -DEXTRAKEY_ENABLE
+endif
+
+ifdef CONSOLE_ENABLE
+    $(error Not Supported)
+    OPT_DEFS += -DCONSOLE_ENABLE
+else
+    OPT_DEFS += -DNO_PRINT
+    OPT_DEFS += -DNO_DEBUG
+endif
+
+ifdef COMMAND_ENABLE
+    $(error Not Supported)
+    SRC += $(COMMON_DIR)/command.c
+    OPT_DEFS += -DCOMMAND_ENABLE
+endif
+
+ifdef NKRO_ENABLE
+    $(error Not Supported)
+    OPT_DEFS += -DNKRO_ENABLE
+endif
 
+ifdef SLEEP_LED_ENABLE
+    $(error Not Supported)
+    SRC += $(COMMON_DIR)/sleep_led.c
+    OPT_DEFS += -DSLEEP_LED_ENABLE
+    OPT_DEFS += -DNO_SUSPEND_POWER_DOWN
+endif
 
+ifdef BACKLIGHT_ENABLE
+    $(error Not Supported)
+    SRC += $(COMMON_DIR)/backlight.c
+    OPT_DEFS += -DBACKLIGHT_ENABLE
+endif
 
-#      $(OBJDIR)/$(COMMON_DIR)/keyboard.o \
-#      $(OBJDIR)/$(COMMON_DIR)/keymap.o \
-#      $(OBJDIR)/$(COMMON_DIR)/bootloader.o \
-#      $(OBJDIR)/$(COMMON_DIR)/suspend.o \
+ifdef KEYMAP_SECTION_ENABLE
+    $(error Not Supported)
+    OPT_DEFS += -DKEYMAP_SECTION_ENABLE
+    EXTRALDFLAGS = -Wl,-L$(TOP_DIR),-Tldscript_keymap_avr5.x
+endif
diff --git a/keyboard/mbed_onekey/config.h b/keyboard/mbed_onekey/config.h
new file mode 100644 (file)
index 0000000..a3aadd0
--- /dev/null
@@ -0,0 +1,7 @@
+#ifndef CONFIG_H
+#define CONFIG_H
+
+#define MATRIX_ROWS 1
+#define MATRIX_COLS 1
+
+#endif
index e31527bbc13e8fbc02c245f4b2f2c134a0d3aa34..0fc41fe2757b3d5ac353f9830e5aa347c8d477a8 100644 (file)
@@ -10,7 +10,8 @@ SIZE  = $(GCC_BIN)arm-none-eabi-size
 CHKSUM  = ~/Dropbox/MBED/tool/lpc-vector-checksum
 
 CPU = -mcpu=cortex-m0 -mthumb
-CC_FLAGS = $(CPU) -c -g -fno-common -fmessage-length=0 -Wall -fno-exceptions -ffunction-sections -fdata-sections 
+
+CC_FLAGS += $(CPU) -c -g -fno-common -fmessage-length=0 -Wall -fno-exceptions -ffunction-sections -fdata-sections
 CC_FLAGS += -MMD -MP
 CC_SYMBOLS = -DTARGET_LPC11U35_401 -DTARGET_M0 -DTARGET_NXP -DTARGET_LPC11UXX -DTOOLCHAIN_GCC_ARM -DTOOLCHAIN_GCC -D__CORTEX_M0 -DARM_MATH_CM0 -DMBED_BUILD_TIMESTAMP=1399108688.49 -D__MBED__=1 
 
index 1df940aa9781045a922015ec3cc729a8ef066246..b2f7243c8575b6fcdb9ad5b111df541ae7a7a4cd 100644 (file)
@@ -1,66 +1,43 @@
-#include "mbed.h"\r
-#include "HIDKeyboard.h"\r
-#include "debug.h"\r
-#include "timer.h"\r
\r
-/*\r
-//#define DEBUG \r
-#ifdef DEBUG\r
-Serial ser(UART_TX, UART_RX);\r
-#define dprintf(fmt, ...)          ser.printf(fmt, ## __VA_ARGS__)\r
-#else\r
-#define dprintf(fmt, ...)\r
-#endif\r
-*/\r
-\r
-\r
-// button and LEDs\r
-DigitalIn isp(P0_1);\r
-DigitalOut led_red(P0_20);\r
-DigitalOut led_green(P0_21);\r
-\r
-//USBKeyboard\r
-HIDKeyboard keyboard;\r
\r
-\r
-int main(void) {\r
-    //isp.mode(PullUp);\r
-    //led_red = 0;\r
-    //led_green = 0;\r
-    debug_enable = true;\r
-    dprintf("HIDKeyboard:\r\n");\r
-\r
-    timer_init();\r
-    xprintf("timer: %i\r\n", timer_read());\r
-\r
-    report_keyboard_t report = { 2, 0, 4, }; //a\r
-    report_keyboard_t report_off = { 0 };\r
-\r
-    bool last_isp = isp;\r
-    uint32_t last_timer;\r
-    while (1) {\r
-        //keyboard.mediaControl(KEY_VOLUME_DOWN);\r
-        //keyboard.printf("Hello World from Mbed\r\n");\r
-        //keyboard.keyCode('s', KEY_CTRL);\r
-        //keyboard.keyCode(KEY_CAPS_LOCK);\r
-\r
-        //led_green = !led_green;\r
-        //leds = keyboard.lockStatus();\r
-        //ser.putc(ser.getc());\r
-\r
-        if (last_isp == isp) continue;\r
-        if (isp == 0) {\r
-            led_red = 0;    // on\r
-    xprintf("timer: %i\r\n", timer_read32());\r
-    xprintf("diff: %i\r\n", timer_elapsed32(last_timer));\r
-            //keyboard.sendReport(report);\r
-        } else {\r
-            led_red = 1;    // off\r
-            //keyboard.sendReport(report_off);\r
-        }\r
-        last_isp = isp;\r
-        last_timer = timer_read();\r
-        //led_green = !led_green;\r
-        //wait(0.5);\r
-    }\r
-}\r
+#include "mbed.h"
+#include "debug.h"
+#include "timer.h"
+#include "action.h"
+#include "keycode.h"
+#include "host.h"
+#include "host_driver.h"
+#include "mbed_driver.h"
+
+// Button and LEDs of LPC11U35 board
+DigitalIn isp(P0_1);            // ISP button
+DigitalOut led_red(P0_20);
+DigitalOut led_green(P0_21);
+
+
+int main(void) {
+    isp.mode(PullUp);
+    led_red = 1;
+    led_green = 0;
+
+    timer_init();
+    host_set_driver(&mbed_driver);
+
+    //debug_enable = true;
+    xprintf("mbed_onekey ver.eee:\r\n");
+
+
+    bool last_isp = isp;
+    while (1) {
+        //led_green = !led_green;
+        if (last_isp == isp) continue;
+        last_isp = isp;
+        if (last_isp == 0) {
+            led_red = 0;    // on
+            dprintf("timer: %i\r\n", timer_read());
+            register_code(KC_A);
+        } else {
+            led_red = 1;    // off
+            unregister_code(KC_A);
+        }
+    }
+}
diff --git a/keyboard/mbed_onekey/mbed_driver.cpp b/keyboard/mbed_onekey/mbed_driver.cpp
new file mode 100644 (file)
index 0000000..333f8e3
--- /dev/null
@@ -0,0 +1,41 @@
+#include "HIDKeyboard.h"
+#include "host.h"
+#include "host_driver.h"
+#include "mbed_driver.h"
+
+HIDKeyboard keyboard;
+
+
+/* Host driver */
+static uint8_t keyboard_leds(void);
+static void send_keyboard(report_keyboard_t *report);
+static void send_mouse(report_mouse_t *report);
+static void send_system(uint16_t data);
+static void send_consumer(uint16_t data);
+
+host_driver_t mbed_driver = {
+    keyboard_leds,
+    send_keyboard,
+    send_mouse,
+    send_system,
+    send_consumer
+};
+
+
+static uint8_t keyboard_leds(void)
+{
+    return 0;
+}
+static void send_keyboard(report_keyboard_t *report)
+{
+    keyboard.sendReport(*report);
+}
+static void send_mouse(report_mouse_t *report)
+{
+}
+static void send_system(uint16_t data)
+{
+}
+static void send_consumer(uint16_t data)
+{
+}
diff --git a/keyboard/mbed_onekey/mbed_driver.h b/keyboard/mbed_onekey/mbed_driver.h
new file mode 100644 (file)
index 0000000..dd1153b
--- /dev/null
@@ -0,0 +1,3 @@
+#include "host_driver.h"
+
+extern host_driver_t mbed_driver;