aboutsummaryrefslogtreecommitdiff
path: root/common
diff options
context:
space:
mode:
Diffstat (limited to 'common')
-rw-r--r--common/bootloader.c116
-rw-r--r--common/bootmagic.c67
-rw-r--r--common/bootmagic.h75
-rw-r--r--common/command.c43
-rw-r--r--common/eeconfig.c38
-rw-r--r--common/eeconfig.h85
-rw-r--r--common/keyboard.c39
-rw-r--r--common/keymap.c86
-rw-r--r--common/keymap.h21
-rw-r--r--common/report.h2
10 files changed, 471 insertions, 101 deletions
diff --git a/common/bootloader.c b/common/bootloader.c
index 6e04efbbd..43a7e47ce 100644
--- a/common/bootloader.c
+++ b/common/bootloader.c
@@ -1,45 +1,106 @@
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
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
10 */ 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
11 25
12// TODO: support usbasp 26#define FLASH_SIZE (FLASHEND + 1L)
13/* Boot Section Size in bytes 27#define BOOTLOADER_START (FLASH_SIZE - BOOTLOADER_SIZE)
14 * Teensy halfKay 512 28
15 * Atmel DFU loader 4096 29
16 * LUFA bootloader 4096 30/*
31 * Entering the Bootloader via Software
32 * http://www.fourwalledcubicle.com/files/LUFA/Doc/120730/html/_page__software_bootloader_start.html
17 */ 33 */
18#ifndef BOOT_SIZE 34#define BOOTLOADER_RESET_KEY 0xB007B007
19#define BOOT_SIZE 512 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);
20#endif 51#endif
21 52
22#define FLASH_SIZE (FLASHEND + 1) 53 // watchdog reset
23#define BOOTLOADER_START (FLASHEND - BOOT_SIZE) 54 reset_key = BOOTLOADER_RESET_KEY;
55 wdt_enable(WDTO_250MS);
56 for (;;);
57}
24 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 ((void (*)(void))BOOTLOADER_START)();
75 }
76}
77
78
79#if 0
80/* Jumping To The Bootloader
81 * http://www.pjrc.com/teensy/jump_to_bootloader.html
82 *
83 * This method doen't work when using LUFA. idk why.
84 * - needs to initialize more regisers or interrupt setting?
85 */
25void bootloader_jump(void) { 86void bootloader_jump(void) {
87#ifdef PROTOCOL_LUFA
88 USB_Disable();
26 cli(); 89 cli();
90 _delay_ms(2000);
91#endif
27 92
28 // 93#ifdef PROTOCOL_PJRC
29 //Teensy 94 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; 95 UDCON = 1;
35 USBCON = (1<<FRZCLK); // disable USB 96 USBCON = (1<<FRZCLK);
36 UCSR1B = 0; 97 UCSR1B = 0;
37 _delay_ms(5); 98 _delay_ms(5);
38#else
39 // This makes custom USBasploader come up.
40 MCUSR = 0;
41#endif 99#endif
42 100
101 /*
102 * Initialize
103 */
43#if defined(__AVR_AT90USB162__) 104#if defined(__AVR_AT90USB162__)
44 EIMSK = 0; PCICR = 0; SPCR = 0; ACSR = 0; EECR = 0; 105 EIMSK = 0; PCICR = 0; SPCR = 0; ACSR = 0; EECR = 0;
45 TIMSK0 = 0; TIMSK1 = 0; UCSR1B = 0; 106 TIMSK0 = 0; TIMSK1 = 0; UCSR1B = 0;
@@ -62,10 +123,9 @@ void bootloader_jump(void) {
62 PORTA = 0; PORTB = 0; PORTC = 0; PORTD = 0; PORTE = 0; PORTF = 0; 123 PORTA = 0; PORTB = 0; PORTC = 0; PORTD = 0; PORTE = 0; PORTF = 0;
63#endif 124#endif
64 125
65 126 /*
66 // 127 * USBaspLoader
67 //USBasp 128 */
68 //
69#if defined(__AVR_ATmega168__) || defined(__AVR_ATmega168P__) || defined(__AVR_ATmega328P__) 129#if defined(__AVR_ATmega168__) || defined(__AVR_ATmega168P__) || defined(__AVR_ATmega328P__)
70 // This makes custom USBasploader come up. 130 // This makes custom USBasploader come up.
71 MCUSR = 0; 131 MCUSR = 0;
@@ -81,7 +141,7 @@ void bootloader_jump(void) {
81 ADCSRA = 0; TWCR = 0; UCSR0B = 0; 141 ADCSRA = 0; TWCR = 0; UCSR0B = 0;
82#endif 142#endif
83 143
84
85 // start Bootloader 144 // start Bootloader
86 ((void (*)(void))BOOTLOADER_START)(); 145 ((void (*)(void))BOOTLOADER_START)();
87} 146}
147#endif
diff --git a/common/bootmagic.c b/common/bootmagic.c
new file mode 100644
index 000000000..388099e2e
--- /dev/null
+++ b/common/bootmagic.c
@@ -0,0 +1,67 @@
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 if (!BOOTMAGIC_IS_ENABLED()) { return; }
14
15 /* do scans in case of bounce */
16 uint8_t scan = 100;
17 while (scan--) { matrix_scan(); _delay_ms(1); }
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 if (bootmagic_scan_keycode(BOOTMAGIC_SWAP_CONTROL_CPASLOCK)) {
32 eeconfig_write_keyconf(eeconfig_read_keyconf() ^ EECONFIG_KEYCONF_SWAP_CONTROL_CAPSLOCK);
33 }
34 if (bootmagic_scan_keycode(BOOTMAGIC_CAPSLOCK_TO_CONTROL)) {
35 eeconfig_write_keyconf(eeconfig_read_keyconf() ^ EECONFIG_KEYCONF_CAPSLOCK_TO_CONTROL);
36 }
37 if (bootmagic_scan_keycode(BOOTMAGIC_SWAP_LALT_LGUI)) {
38 eeconfig_write_keyconf(eeconfig_read_keyconf() ^ EECONFIG_KEYCONF_SWAP_LALT_LGUI);
39 }
40 if (bootmagic_scan_keycode(BOOTMAGIC_SWAP_RALT_RGUI)) {
41 eeconfig_write_keyconf(eeconfig_read_keyconf() ^ EECONFIG_KEYCONF_SWAP_RALT_RGUI);
42 }
43 if (bootmagic_scan_keycode(BOOTMAGIC_NO_GUI)) {
44 eeconfig_write_keyconf(eeconfig_read_keyconf() ^ EECONFIG_KEYCONF_NO_GUI);
45 }
46 if (bootmagic_scan_keycode(BOOTMAGIC_SWAP_GRAVE_ESC)) {
47 eeconfig_write_keyconf(eeconfig_read_keyconf() ^ EECONFIG_KEYCONF_SWAP_GRAVE_ESC);
48 }
49 if (bootmagic_scan_keycode(BOOTMAGIC_SWAP_BACKSLASH_BACKSPACE)) {
50 eeconfig_write_keyconf(eeconfig_read_keyconf() ^ EECONFIG_KEYCONF_SWAP_BACKSLASH_BACKSPACE);
51 }
52}
53
54bool bootmagic_scan_keycode(uint8_t keycode)
55{
56 for (uint8_t r = 0; r < MATRIX_ROWS; r++) {
57 matrix_row_t matrix_row = matrix_get_row(r);
58 for (uint8_t c = 0; c < MATRIX_COLS; c++) {
59 if (matrix_row & ((matrix_row_t)1<<c)) {
60 if (keycode == keymap_key_to_keycode(0, (key_t){ .row = r, .col = c })) {
61 return true;
62 }
63 }
64 }
65 }
66 return false;
67}
diff --git a/common/bootmagic.h b/common/bootmagic.h
new file mode 100644
index 000000000..5791b221f
--- /dev/null
+++ b/common/bootmagic.h
@@ -0,0 +1,75 @@
1#ifndef BOOTMAGIC_H
2#define BOOTMAGIC_H
3
4
5#ifndef BOOTMAGIC_IS_ENABLED
6#define BOOTMAGIC_IS_ENABLED() true
7#endif
8
9/* kick up 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/*
23 * key configure
24 */
25/* swap control and capslock */
26#ifndef BOOTMAGIC_SWAP_CONTROL_CPASLOCK
27#define BOOTMAGIC_SWAP_CONTROL_CPASLOCK KC_LCTRL
28#endif
29/* capslock to control */
30#ifndef BOOTMAGIC_CAPSLOCK_TO_CONTROL
31#define BOOTMAGIC_CAPSLOCK_TO_CONTROL KC_CAPSLOCK
32#endif
33/* swap alt and gui */
34#ifndef BOOTMAGIC_SWAP_LALT_LGUI
35#define BOOTMAGIC_SWAP_LALT_LGUI KC_LALT
36#endif
37/* swap alt and gui */
38#ifndef BOOTMAGIC_SWAP_RALT_RGUI
39#define BOOTMAGIC_SWAP_RALT_RGUI KC_RALT
40#endif
41/* no gui */
42#ifndef BOOTMAGIC_NO_GUI
43#define BOOTMAGIC_NO_GUI KC_LGUI
44#endif
45/* swap esc and grave */
46#ifndef BOOTMAGIC_SWAP_GRAVE_ESC
47#define BOOTMAGIC_SWAP_GRAVE_ESC KC_GRAVE
48#endif
49/* swap backslash and backspace */
50#ifndef BOOTMAGIC_SWAP_BACKSLASH_BACKSPACE
51#define BOOTMAGIC_SWAP_BACKSLASH_BACKSPACE KC_BSLASH
52#endif
53
54
55/*
56 * change default layer
57 */
58#ifndef BOOTMAGIC_DEFAULT_LAYER_0_KEY
59#define BOOTMAGIC_DEFAULT_LAYER_0_KEY KC_0
60#endif
61#ifndef BOOTMAGIC_DEFAULT_LAYER_1_KEY
62#define BOOTMAGIC_DEFAULT_LAYER_1_KEY KC_1
63#endif
64#ifndef BOOTMAGIC_DEFAULT_LAYER_2_KEY
65#define BOOTMAGIC_DEFAULT_LAYER_2_KEY KC_2
66#endif
67#ifndef BOOTMAGIC_DEFAULT_LAYER_3_KEY
68#define BOOTMAGIC_DEFAULT_LAYER_3_KEY KC_3
69#endif
70
71
72void bootmagic(void);
73bool bootmagic_scan_keycode(uint8_t keycode);
74
75#endif
diff --git a/common/command.c b/common/command.c
index 202d531fd..372ca291e 100644
--- a/common/command.c
+++ b/common/command.c
@@ -27,20 +27,21 @@ along with this program. If not, see <http://www.gnu.org/licenses/>.
27#include "keyboard.h" 27#include "keyboard.h"
28#include "bootloader.h" 28#include "bootloader.h"
29#include "layer_switch.h" 29#include "layer_switch.h"
30#include "eeconfig.h"
30#include "command.h" 31#include "command.h"
31 32
32#ifdef MOUSEKEY_ENABLE 33#ifdef MOUSEKEY_ENABLE
33#include "mousekey.h" 34#include "mousekey.h"
34#endif 35#endif
35 36
36#ifdef HOST_PJRC 37#ifdef PROTOCOL_PJRC
37# include "usb_keyboard.h" 38# include "usb_keyboard.h"
38# ifdef EXTRAKEY_ENABLE 39# ifdef EXTRAKEY_ENABLE
39# include "usb_extra.h" 40# include "usb_extra.h"
40# endif 41# endif
41#endif 42#endif
42 43
43#ifdef HOST_VUSB 44#ifdef PROTOCOL_VUSB
44# include "usbdrv.h" 45# include "usbdrv.h"
45#endif 46#endif
46 47
@@ -108,6 +109,7 @@ static void command_common_help(void)
108 print("v: print device version & info\n"); 109 print("v: print device version & info\n");
109 print("t: print timer count\n"); 110 print("t: print timer count\n");
110 print("s: print status\n"); 111 print("s: print status\n");
112 print("e: print eeprom boot config\n");
111#ifdef NKRO_ENABLE 113#ifdef NKRO_ENABLE
112 print("n: toggle NKRO\n"); 114 print("n: toggle NKRO\n");
113#endif 115#endif
@@ -121,10 +123,41 @@ static void command_common_help(void)
121 print("Paus: jump to bootloader\n"); 123 print("Paus: jump to bootloader\n");
122} 124}
123 125
126#ifdef BOOTMAGIC_ENABLE
127static void print_eeprom_config(void)
128{
129 uint8_t eebyte;
130
131 eebyte = eeconfig_read_debug();
132 print("debug: "); print_hex8(eebyte); print("\n");
133
134 eebyte = eeconfig_read_defalt_layer();
135 print("defalt_layer: "); print_hex8(eebyte); print("\n");
136
137 eebyte = eeconfig_read_keyconf();
138 print("keyconf: "); print_hex8(eebyte); print("\n");
139
140 keyconf kc = (keyconf){ .raw = eebyte };
141 print("keyconf.swap_control_capslock: "); print_hex8(kc.swap_control_capslock); print("\n");
142 print("keyconf.capslock_to_control: "); print_hex8(kc.capslock_to_control); print("\n");
143 print("keyconf.swap_lalt_lgui: "); print_hex8(kc.swap_lalt_lgui); print("\n");
144 print("keyconf.swap_ralt_rgui: "); print_hex8(kc.swap_ralt_rgui); print("\n");
145 print("keyconf.no_gui: "); print_hex8(kc.no_gui); print("\n");
146 print("keyconf.swap_grave_esc: "); print_hex8(kc.swap_grave_esc); print("\n");
147 print("keyconf.swap_backslash_backspace: "); print_hex8(kc.swap_backslash_backspace); print("\n");
148}
149#endif
150
124static bool command_common(uint8_t code) 151static bool command_common(uint8_t code)
125{ 152{
126 static host_driver_t *host_driver = 0; 153 static host_driver_t *host_driver = 0;
127 switch (code) { 154 switch (code) {
155#ifdef BOOTMAGIC_ENABLE
156 case KC_E:
157 print("eeprom config\n");
158 print_eeprom_config();
159 break;
160#endif
128 case KC_CAPSLOCK: 161 case KC_CAPSLOCK:
129 if (host_get_driver()) { 162 if (host_get_driver()) {
130 host_driver = host_get_driver(); 163 host_driver = host_get_driver();
@@ -218,7 +251,7 @@ static bool command_common(uint8_t code)
218 case KC_S: 251 case KC_S:
219 print("\n\n----- Status -----\n"); 252 print("\n\n----- Status -----\n");
220 print_val_hex8(host_keyboard_leds()); 253 print_val_hex8(host_keyboard_leds());
221#ifdef HOST_PJRC 254#ifdef PROTOCOL_PJRC
222 print_val_hex8(UDCON); 255 print_val_hex8(UDCON);
223 print_val_hex8(UDIEN); 256 print_val_hex8(UDIEN);
224 print_val_hex8(UDINT); 257 print_val_hex8(UDINT);
@@ -228,7 +261,7 @@ static bool command_common(uint8_t code)
228 print_val_hex8(usb_keyboard_idle_count); 261 print_val_hex8(usb_keyboard_idle_count);
229#endif 262#endif
230 263
231#ifdef HOST_VUSB 264#ifdef PROTOCOL_PJRC
232# if USB_COUNT_SOF 265# if USB_COUNT_SOF
233 print_val_hex8(usbSofCount); 266 print_val_hex8(usbSofCount);
234# endif 267# endif
@@ -247,7 +280,7 @@ static bool command_common(uint8_t code)
247#ifdef EXTRAKEY_ENABLE 280#ifdef EXTRAKEY_ENABLE
248 case KC_PSCREEN: 281 case KC_PSCREEN:
249 // TODO: Power key should take this feature? otherwise any key during suspend. 282 // TODO: Power key should take this feature? otherwise any key during suspend.
250#ifdef HOST_PJRC 283#ifdef PROTOCOL_PJRC
251 if (suspend && remote_wakeup) { 284 if (suspend && remote_wakeup) {
252 usb_remote_wakeup(); 285 usb_remote_wakeup();
253 } else { 286 } else {
diff --git a/common/eeconfig.c b/common/eeconfig.c
new file mode 100644
index 000000000..cea3810ee
--- /dev/null
+++ b/common/eeconfig.c
@@ -0,0 +1,38 @@
1#include <stdint.h>
2#include <stdbool.h>
3#include <avr/eeprom.h>
4#include "eeconfig.h"
5
6
7void eeconfig_init(void)
8{
9 eeprom_write_word(EECONFIG_MAGIC, EECONFIG_MAGIC_NUMBER);
10 eeprom_write_byte(EECONFIG_DEBUG, 0);
11 eeprom_write_byte(EECONFIG_DEFAULT_LAYER, 0);
12 eeprom_write_byte(EECONFIG_KEYCONF, 0);
13 eeprom_write_byte(EECONFIG_MOUSEKEY_ACCEL, 0);
14}
15
16void eeconfig_enable(void)
17{
18 eeprom_write_word(EECONFIG_MAGIC, EECONFIG_MAGIC_NUMBER);
19}
20
21void eeconfig_disable(void)
22{
23 eeprom_write_word(EECONFIG_MAGIC, 0xFFFF);
24}
25
26bool eeconfig_is_enabled(void)
27{
28 return EECONFIG_IS_ENABLED() && (eeprom_read_word(EECONFIG_MAGIC) == EECONFIG_MAGIC_NUMBER);
29}
30
31uint8_t eeconfig_read_debug(void) { return eeprom_read_byte(EECONFIG_DEBUG); }
32void eeconfig_write_debug(uint8_t val) { eeprom_write_byte(EECONFIG_DEBUG, val); }
33
34uint8_t eeconfig_read_defalt_layer(void) { return eeprom_read_byte(EECONFIG_DEFAULT_LAYER); }
35void eeconfig_write_defalt_layer(uint8_t val) { eeprom_write_byte(EECONFIG_DEFAULT_LAYER, val); }
36
37uint8_t eeconfig_read_keyconf(void) { return eeprom_read_byte(EECONFIG_KEYCONF); }
38void eeconfig_write_keyconf(uint8_t val) { eeprom_write_byte(EECONFIG_KEYCONF, val); }
diff --git a/common/eeconfig.h b/common/eeconfig.h
new file mode 100644
index 000000000..3e195478b
--- /dev/null
+++ b/common/eeconfig.h
@@ -0,0 +1,85 @@
1/*
2Copyright 2013 Jun Wako <wakojun@gmail.com>
3
4This program is free software: you can redistribute it and/or modify
5it under the terms of the GNU General Public License as published by
6the Free Software Foundation, either version 2 of the License, or
7(at your option) any later version.
8
9This program is distributed in the hope that it will be useful,
10but WITHOUT ANY WARRANTY; without even the implied warranty of
11MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12GNU General Public License for more details.
13
14You should have received a copy of the GNU General Public License
15along with this program. If not, see <http://www.gnu.org/licenses/>.
16*/
17
18#ifndef EECONFIG_H
19#define EECONFIG_H
20
21#include <stdint.h>
22
23#ifndef EECONFIG_IS_ENABLED
24#define EECONFIG_IS_ENABLED() true
25#endif
26
27#define EECONFIG_MAGIC_NUMBER (uint16_t)0xFEED
28
29/* eeprom parameteter address */
30#define EECONFIG_MAGIC (uint16_t *)0
31#define EECONFIG_DEBUG (uint8_t *)2
32#define EECONFIG_DEFAULT_LAYER (uint8_t *)3
33#define EECONFIG_KEYCONF (uint8_t *)4
34#define EECONFIG_MOUSEKEY_ACCEL (uint8_t *)5
35
36
37/* debug bit */
38#define EECONFIG_DEBUG_ENABLE (1<<0)
39#define EECONFIG_DEBUG_MATRIX (1<<1)
40#define EECONFIG_DEBUG_KEYBOARD (1<<2)
41#define EECONFIG_DEBUG_MOUSE (1<<3)
42
43/* keyconf bit */
44#define EECONFIG_KEYCONF_SWAP_CONTROL_CAPSLOCK (1<<0)
45#define EECONFIG_KEYCONF_CAPSLOCK_TO_CONTROL (1<<1)
46#define EECONFIG_KEYCONF_SWAP_LALT_LGUI (1<<2)
47#define EECONFIG_KEYCONF_SWAP_RALT_RGUI (1<<3)
48#define EECONFIG_KEYCONF_NO_GUI (1<<4)
49#define EECONFIG_KEYCONF_SWAP_GRAVE_ESC (1<<5)
50#define EECONFIG_KEYCONF_SWAP_BACKSLASH_BACKSPACE (1<<6)
51
52
53/* XXX: Not portable. Bit field order depends on implementation */
54typedef union {
55 uint8_t raw;
56 struct {
57 bool swap_control_capslock:1;
58 bool capslock_to_control:1;
59 bool swap_lalt_lgui:1;
60 bool swap_ralt_rgui:1;
61 bool no_gui:1;
62 bool swap_grave_esc:1;
63 bool swap_backslash_backspace:1;
64 bool reserved:1;
65 };
66} keyconf;
67
68bool eeconfig_is_enabled(void);
69
70void eeconfig_init(void);
71
72void eeconfig_enable(void);
73
74void eeconfig_disable(void);
75
76uint8_t eeconfig_read_debug(void);
77void eeconfig_write_debug(uint8_t val);
78
79uint8_t eeconfig_read_defalt_layer(void);
80void eeconfig_write_defalt_layer(uint8_t val);
81
82uint8_t eeconfig_read_keyconf(void);
83void eeconfig_write_keyconf(uint8_t val);
84
85#endif
diff --git a/common/keyboard.c b/common/keyboard.c
index 91f321d9c..401fdb4e1 100644
--- a/common/keyboard.c
+++ b/common/keyboard.c
@@ -28,10 +28,9 @@ 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#include "eeconfig.h"
33#include "mousekey.h" 33#include "mousekey.h"
34#endif
35 34
36 35
37#ifdef MATRIX_HAS_GHOST 36#ifdef MATRIX_HAS_GHOST
@@ -59,27 +58,25 @@ void keyboard_init(void)
59 58
60 timer_init(); 59 timer_init();
61 matrix_init(); 60 matrix_init();
62
63 /* matrix scan for boot magic keys */
64#ifdef DEBOUNCE
65 uint8_t scan = DEBOUNCE * 2;
66 while (scan--) { matrix_scan(); _delay_ms(1); }
67#else
68 matrix_scan();
69#endif
70
71 /* boot magic keys */
72#ifdef IS_BOOTMAGIC_BOOTLOADER
73 /* kick up bootloader */
74 if (IS_BOOTMAGIC_BOOTLOADER()) bootloader_jump();
75#endif
76#ifdef IS_BOOTMAGIC_DEBUG
77 if (IS_BOOTMAGIC_DEBUG()) debug_enable = true;
78#endif
79
80#ifdef PS2_MOUSE_ENABLE 61#ifdef PS2_MOUSE_ENABLE
81 ps2_mouse_init(); 62 ps2_mouse_init();
82#endif 63#endif
64
65#ifdef BOOTMAGIC_ENABLE
66 bootmagic();
67
68 if (eeconfig_is_enabled()) {
69 uint8_t config;
70 config = eeconfig_read_debug();
71 // ignored if debug is enabled by program before.
72 if (!debug_enable) debug_enable = (config & EECONFIG_DEBUG_ENABLE);
73 if (!debug_matrix) debug_matrix = (config & EECONFIG_DEBUG_MATRIX);
74 if (!debug_keyboard) debug_keyboard = (config & EECONFIG_DEBUG_KEYBOARD);
75 if (!debug_mouse) debug_mouse = (config & EECONFIG_DEBUG_MOUSE);
76 } else {
77 eeconfig_init();
78 }
79#endif
83} 80}
84 81
85/* 82/*
diff --git a/common/keymap.c b/common/keymap.c
index aa8d944a7..ace3f49b6 100644
--- a/common/keymap.c
+++ b/common/keymap.c
@@ -26,7 +26,7 @@ along with this program. If not, see <http://www.gnu.org/licenses/>.
26 26
27static action_t keycode_to_action(uint8_t keycode); 27static action_t keycode_to_action(uint8_t keycode);
28 28
29#ifdef USE_KEYMAP_V2 29
30/* converts key to action */ 30/* converts key to action */
31action_t action_for_key(uint8_t layer, key_t key) 31action_t action_for_key(uint8_t layer, key_t key)
32{ 32{
@@ -38,42 +38,20 @@ action_t action_for_key(uint8_t layer, key_t key)
38 return keycode_to_action(keycode); 38 return keycode_to_action(keycode);
39 } 39 }
40} 40}
41#else
42/*
43 * legacy keymap support
44 */
45/* translation for legacy keymap */
46action_t action_for_key(uint8_t layer, key_t key)
47{
48 /* convert from legacy keycode to action */
49 /* layer 16-31 indicate 'overlay' but not supported in legacy keymap */
50 uint8_t keycode = keymap_get_keycode((layer & OVERLAY_MASK), key.row, key.col);
51 action_t action;
52 switch (keycode) {
53 case KC_FN0 ... KC_FN31:
54 {
55 uint8_t layer = keymap_fn_layer(FN_INDEX(keycode));
56 uint8_t key = keymap_fn_keycode(FN_INDEX(keycode));
57 if (key) {
58 action.code = ACTION_KEYMAP_TAP_KEY(layer, key);
59 } else {
60 action.code = ACTION_KEYMAP_MOMENTARY(layer);
61 }
62 }
63 return action;
64 default:
65 return keycode_to_action(keycode);
66 }
67}
68#endif
69 41
70 42
43/* Macro */
71__attribute__ ((weak)) 44__attribute__ ((weak))
72const prog_macro_t *action_get_macro(keyrecord_t *record, uint8_t id, uint8_t opt) { return MACRO_NONE; } 45const prog_macro_t *action_get_macro(keyrecord_t *record, uint8_t id, uint8_t opt)
46{
47 return MACRO_NONE;
48}
73 49
50/* Function */
74__attribute__ ((weak)) 51__attribute__ ((weak))
75void action_function(keyrecord_t *record, uint8_t id, uint8_t opt) {} 52void action_function(keyrecord_t *record, uint8_t id, uint8_t opt)
76 53{
54}
77 55
78 56
79 57
@@ -83,14 +61,9 @@ static action_t keycode_to_action(uint8_t keycode)
83 action_t action; 61 action_t action;
84 switch (keycode) { 62 switch (keycode) {
85 case KC_A ... KC_EXSEL: 63 case KC_A ... KC_EXSEL:
64 case KC_LCTRL ... KC_RGUI:
86 action.code = ACTION_KEY(keycode); 65 action.code = ACTION_KEY(keycode);
87 break; 66 break;
88 case KC_LCTRL ... KC_LGUI:
89 action.code = ACTION_LMOD(keycode);
90 break;
91 case KC_RCTRL ... KC_RGUI:
92 action.code = ACTION_RMOD(keycode);
93 break;
94 case KC_SYSTEM_POWER ... KC_SYSTEM_WAKE: 67 case KC_SYSTEM_POWER ... KC_SYSTEM_WAKE:
95 action.code = ACTION_USAGE_SYSTEM(KEYCODE2SYSTEM(keycode)); 68 action.code = ACTION_USAGE_SYSTEM(KEYCODE2SYSTEM(keycode));
96 break; 69 break;
@@ -109,3 +82,40 @@ static action_t keycode_to_action(uint8_t keycode)
109 } 82 }
110 return action; 83 return action;
111} 84}
85
86
87
88#ifdef USE_LEGACY_KEYMAP
89/*
90 * Legacy keymap support
91 * Consider using new keymap API instead.
92 */
93__attribute__ ((weak))
94uint8_t keymap_key_to_keycode(uint8_t layer, key_t key)
95{
96 return keymap_get_keycode(layer, key.row, key.col);
97}
98
99
100/* Legacy keymap support */
101__attribute__ ((weak))
102action_t keymap_fn_to_action(uint8_t keycode)
103{
104 action_t action = { .code = ACTION_NO };
105 switch (keycode) {
106 case KC_FN0 ... KC_FN31:
107 {
108 uint8_t layer = keymap_fn_layer(FN_INDEX(keycode));
109 uint8_t key = keymap_fn_keycode(FN_INDEX(keycode));
110 if (key) {
111 action.code = ACTION_KEYMAP_TAP_KEY(layer, key);
112 } else {
113 action.code = ACTION_KEYMAP_MOMENTARY(layer);
114 }
115 }
116 return action;
117 default:
118 return action;
119 }
120}
121#endif
diff --git a/common/keymap.h b/common/keymap.h
index 0c483483f..7efd91f70 100644
--- a/common/keymap.h
+++ b/common/keymap.h
@@ -23,24 +23,29 @@ along with this program. If not, see <http://www.gnu.org/licenses/>.
23#include "action.h" 23#include "action.h"
24 24
25 25
26#ifdef USE_KEYMAP_V2 26/* translates key to keycode */
27/* translates key to keycode
28 * layer: 0-15 for base layers
29 * 16-31 for overlays
30 */
31uint8_t keymap_key_to_keycode(uint8_t layer, key_t key); 27uint8_t keymap_key_to_keycode(uint8_t layer, key_t key);
28
32/* translates Fn keycode to action */ 29/* translates Fn keycode to action */
33action_t keymap_fn_to_action(uint8_t keycode); 30action_t keymap_fn_to_action(uint8_t keycode);
34#else 31
35#warning "You are using LEGACY KEYAMP. Consider using NEW KEYMAP." 32
33
34#ifdef USE_LEGACY_KEYMAP
36/* 35/*
37 * legacy keymap support 36 * Legacy keymap
37 * Consider using new keymap API above instead.
38 */ 38 */
39/* keycode of key */ 39/* keycode of key */
40__attribute__ ((deprecated))
40uint8_t keymap_get_keycode(uint8_t layer, uint8_t row, uint8_t col); 41uint8_t keymap_get_keycode(uint8_t layer, uint8_t row, uint8_t col);
42
41/* layer to move during press Fn key */ 43/* layer to move during press Fn key */
44__attribute__ ((deprecated))
42uint8_t keymap_fn_layer(uint8_t fn_bits); 45uint8_t keymap_fn_layer(uint8_t fn_bits);
46
43/* keycode to send when release Fn key without using */ 47/* keycode to send when release Fn key without using */
48__attribute__ ((deprecated))
44uint8_t keymap_fn_keycode(uint8_t fn_bits); 49uint8_t keymap_fn_keycode(uint8_t fn_bits);
45#endif 50#endif
46 51
diff --git a/common/report.h b/common/report.h
index 0995189b3..480102768 100644
--- a/common/report.h
+++ b/common/report.h
@@ -71,7 +71,7 @@ along with this program. If not, see <http://www.gnu.org/licenses/>.
71 71
72 72
73/* key report size(NKRO or boot mode) */ 73/* key report size(NKRO or boot mode) */
74#if defined(HOST_PJRC) 74#if defined(PROTOCOL_PJRC)
75# include "usb.h" 75# include "usb.h"
76# if defined(KBD2_REPORT_KEYS) && KBD2_REPORT_KEYS > KBD_REPORT_KEYS 76# if defined(KBD2_REPORT_KEYS) && KBD2_REPORT_KEYS > KBD_REPORT_KEYS
77# define REPORT_KEYS KBD2_REPORT_KEYS 77# define REPORT_KEYS KBD2_REPORT_KEYS