aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorEric Tang <e_l_tang@outlook.com>2016-05-23 20:42:21 -0700
committerJack Humbert <jack.humb@gmail.com>2016-05-23 23:42:21 -0400
commitaaa758f1d3f97dda39879f2b055ad2da9680adfe (patch)
treecb0749cda23c0507dd2b230af3eb0ca81a97b8af
parentd66aa0abf96b5e887250cf0a7fa5e575f18c5a91 (diff)
downloadqmk_firmware-aaa758f1d3f97dda39879f2b055ad2da9680adfe.tar.gz
qmk_firmware-aaa758f1d3f97dda39879f2b055ad2da9680adfe.zip
Optimize matrix scanning (#343)
-rw-r--r--HAND_WIRE.md26
-rw-r--r--keyboard/Bantam44/config.h7
-rw-r--r--keyboard/atomic/config.h17
-rw-r--r--keyboard/atomic/keymaps/pvc/config.h7
-rw-r--r--keyboard/atreus/config.h12
-rw-r--r--keyboard/clueboard1/config.h25
-rw-r--r--keyboard/clueboard2/config.h19
-rw-r--r--keyboard/cluepad/config.h19
-rw-r--r--keyboard/gh60_rev_c/config.h17
-rw-r--r--keyboard/jd45/config.h9
-rw-r--r--keyboard/planck/config.h7
-rw-r--r--keyboard/planck/keymaps/pvc/config.h7
-rw-r--r--keyboard/preonic/config.h7
-rw-r--r--keyboard/retro_refit/config.h9
-rw-r--r--quantum/config_common.h125
-rw-r--r--quantum/matrix.c369
-rw-r--r--quantum/template/config.h17
-rw-r--r--tmk_core/common/avr/suspend.c5
-rw-r--r--tmk_core/common/bootmagic.c14
-rw-r--r--tmk_core/common/keyboard.c134
-rw-r--r--tmk_core/common/matrix.h61
21 files changed, 408 insertions, 505 deletions
diff --git a/HAND_WIRE.md b/HAND_WIRE.md
index 3f4d75b27..0b7367eda 100644
--- a/HAND_WIRE.md
+++ b/HAND_WIRE.md
@@ -51,11 +51,11 @@ A problem arises when you start pressing more than one key at a time. Looking at
51 x row0 ---(-+-0)---(-+-1) x row0 ---(-+-0)---(-+-1) 51 x row0 ---(-+-0)---(-+-1) x row0 ---(-+-0)---(-+-1)
52 | | | | 52 | | | |
53 x row1 ---(key2)---(-+-3) x row1 ---(key2)---(-+-3) 53 x row1 ---(key2)---(-+-3) x row1 ---(key2)---(-+-3)
54 54
55 Remember that this ^ is still connected to row1 55 Remember that this ^ is still connected to row1
56 56
57The data we get from that is: 57The data we get from that is:
58 58
59 col0: 0b11 59 col0: 0b11
60 col1: 0b11 60 col1: 0b11
61 │└row0 61 │└row0
@@ -73,7 +73,7 @@ Which isn't accurate, since we only have 3 keys pressed down, not all 4. This be
73 │ │ | │ 73 │ │ | │
74 (key2) (key3) (key2) (key3) 74 (key2) (key3) (key2) (key3)
75 ! ! ! ! 75 ! ! ! !
76 row1 ─────┴────────┘ row1 ─────┴────────┘ 76 row1 ─────┴────────┘ row1 ─────┴────────┘
77 77
78In practical applications, the black line of the diode will be placed facing the row, and away from the keyswitch - the `!` in this case is the diode, where the gap represents the black line. A good way to remember this is to think of this symbol: `>|` 78In practical applications, the black line of the diode will be placed facing the row, and away from the keyswitch - the `!` in this case is the diode, where the gap represents the black line. A good way to remember this is to think of this symbol: `>|`
79 79
@@ -89,10 +89,10 @@ Now when we press the three keys, invoking what would be a ghosting scenario:
89 │ │ │ │ 89 │ │ │ │
90 (key2) (┌─┘3) (key2) (┌─┘3) 90 (key2) (┌─┘3) (key2) (┌─┘3)
91 ! ! ! ! 91 ! ! ! !
92 row1 ─────┴────────┘ x row1 ─────┴────────┘ 92 row1 ─────┴────────┘ x row1 ─────┴────────┘
93 93
94Things act as they should! Which will get us the following data: 94Things act as they should! Which will get us the following data:
95 95
96 col0: 0b01 96 col0: 0b01
97 col1: 0b11 97 col1: 0b11
98 │└row0 98 │└row0
@@ -106,7 +106,7 @@ The firmware can then use this correct data to detect what it should do, and eve
106 106
107When starting this, you should have all of your stabilisers and keyswitches already installed (and optionally keycaps). If you're using a Cherry-type stabiliser (plate-mounted only, obviously), you'll need to install that before your keyswitches. If you're using Costar ones, you can installed them afterwards. 107When starting this, you should have all of your stabilisers and keyswitches already installed (and optionally keycaps). If you're using a Cherry-type stabiliser (plate-mounted only, obviously), you'll need to install that before your keyswitches. If you're using Costar ones, you can installed them afterwards.
108 108
109To make things easier on yourself, make sure all of the keyswitches are oriented the same way (if they can be - not all layouts support this). Despite this, it's important to remember that the contacts on the keyswitches are completely symmetrical. We'll be using the keyswitch's left side contact for wiring the rows, and the right side one for wiring the columns. 109To make things easier on yourself, make sure all of the keyswitches are oriented the same way (if they can be - not all layouts support this). Despite this, it's important to remember that the contacts on the keyswitches are completely symmetrical. We'll be using the keyswitch's left side contact for wiring the rows, and the right side one for wiring the columns.
110 110
111Get your soldering iron heated-up and collect the rest of the materials from the part list at the beginning of the guide. Place your keyboard so that the bottoms of the keyswitches are accessible - it may be a good idea to place it on a cloth to protect your keyswitches/keycaps. 111Get your soldering iron heated-up and collect the rest of the materials from the part list at the beginning of the guide. Place your keyboard so that the bottoms of the keyswitches are accessible - it may be a good idea to place it on a cloth to protect your keyswitches/keycaps.
112 112
@@ -155,7 +155,7 @@ When all of the diodes are completely soldered, it's a good idea to quickly insp
155 155
156### Soldering the columns 156### Soldering the columns
157 157
158You'll have some options in the next process - it's a good idea to insulate the column wires (since the diodes aren't), but if you're careful enough, you can use exposed wires for the columns - it's not recommended, though. If you're using single-cored wire, stripping the plastic off of the whole wire and feeding it back on is probably the best option, but can be difficult depending on the size and materials. You'll want to leave parts of the wire exposed where you're going to be solder it onto the keyswitch. 158You'll have some options in the next process - it's a good idea to insulate the column wires (since the diodes aren't), but if you're careful enough, you can use exposed wires for the columns - it's not recommended, though. If you're using single-cored wire, stripping the plastic off of the whole wire and feeding it back on is probably the best option, but can be difficult depending on the size and materials. You'll want to leave parts of the wire exposed where you're going to be solder it onto the keyswitch.
159 159
160If you're using stranded wire, it's probably easiest to just use a lot of small wires to connect each keyswitch along the column. It's possible to use one and melt through the insulation, but this isn't recommended, will produce even more harmful fumes, and can ruin your soldering iron. 160If you're using stranded wire, it's probably easiest to just use a lot of small wires to connect each keyswitch along the column. It's possible to use one and melt through the insulation, but this isn't recommended, will produce even more harmful fumes, and can ruin your soldering iron.
161 161
@@ -195,15 +195,13 @@ You'll want to navigate to the `keyboard/<project_name>/` folder by typing, like
195 195
196#### config.h 196#### config.h
197 197
198The first thing we're going to want to modify is the `config.h` file. On line 32 and 33, you'll see `MATRIX_ROWS` and `MATRIX_COLS` - set both these variables to however many rows and columns you have on your keyboard. 198The first thing you're going to want to modify is the `config.h` file. Find `MATRIX_ROWS` and `MATRIX_COLS` and them to match the dimensions of your keyboard's matrix.
199
200On line 38 and 39 you'll see the `COLS` and `ROWS` definitions - this is where you'll enter the pins you used, in order (left-to-right when looking at the top of the keyboard, but right-to-left when looking at the bottom).
201 199
202There are some other variables that you'll be able to modify (lines 23-29), but it's not necessary to do that now (or ever, really). 200Farther down are `MATRIX_ROW_PINS` and `MATRIX_COL_PINS`. Change their definitions to match how you wired up your matrix (looking from the top of the keyboard, the rows run top-to-bottom and the columns run left-to-right). Likewise, change the definition of `UNUSED_PINS` to match the pins you did not use (this will save power).
203 201
204#### \<project_name\>.h 202#### \<project_name\>.h
205 203
206The next file you'll want to look at is `<project_name>.h`. You're going to want to rewrite the `KEYMAP` definition - the format and syntax here is extremely important, so pay attention to how things are setup. The first half of the definition are considered the arguments - this is the format that you'll be following in your keymap later on, so you'll want to have as many k*xy* variables here as you do keys. The second half is the part that the firmware actually looks at, and will contain gaps depending on how you wired your matrix. 204The next file you'll want to look at is `<project_name>.h`. You're going to want to rewrite the `KEYMAP` definition - the format and syntax here is extremely important, so pay attention to how things are setup. The first half of the definition are considered the arguments - this is the format that you'll be following in your keymap later on, so you'll want to have as many k*xy* variables here as you do keys. The second half is the part that the firmware actually looks at, and will contain gaps depending on how you wired your matrix.
207 205
208We'll dive into how this will work with the following example. Say we have a keyboard like this: 206We'll dive into how this will work with the following example. Say we have a keyboard like this:
209 207
@@ -230,7 +228,7 @@ The middle column is unused on the bottom row in this example. Our `KEYMAP` defi
230 { \ 228 { \
231 { k00, k01, k02 }, \ 229 { k00, k01, k02 }, \
232 { k10, KC_NO, k11 }, \ 230 { k10, KC_NO, k11 }, \
233 } 231 }
234 232
235Notice how the top half is spaced to resemble our physical layout - this helps us understand which keys are associated with which columns. The bottom half uses the keycode `KC_NO` where there is no keyswitch wired in. It's easiest to keep the bottom half aligned in a grid to help us make sense of how the firmware actually sees the wiring. 233Notice how the top half is spaced to resemble our physical layout - this helps us understand which keys are associated with which columns. The bottom half uses the keycode `KC_NO` where there is no keyswitch wired in. It's easiest to keep the bottom half aligned in a grid to help us make sense of how the firmware actually sees the wiring.
236 234
@@ -251,7 +249,7 @@ This would require our `KEYMAP` definition to look like this:
251 { \ 249 { \
252 { k00, k01, k02 }, \ 250 { k00, k01, k02 }, \
253 { k10, k11, KC_NO }, \ 251 { k10, k11, KC_NO }, \
254 } 252 }
255 253
256Notice how the `k11` and `KC_NO` switched places to represent the wiring, and the unused final column on the bottom row. Sometimes it'll make more sense to put a keyswitch on a particular column, but in the end, it won't matter, as long as all of them are accounted for. You can use this process to write out the `KEYMAP` for your entire keyboard - be sure to remember that your keyboard is actually backwards when looking at the underside of it. 254Notice how the `k11` and `KC_NO` switched places to represent the wiring, and the unused final column on the bottom row. Sometimes it'll make more sense to put a keyswitch on a particular column, but in the end, it won't matter, as long as all of them are accounted for. You can use this process to write out the `KEYMAP` for your entire keyboard - be sure to remember that your keyboard is actually backwards when looking at the underside of it.
257 255
diff --git a/keyboard/Bantam44/config.h b/keyboard/Bantam44/config.h
index 26d680704..a55f62999 100644
--- a/keyboard/Bantam44/config.h
+++ b/keyboard/Bantam44/config.h
@@ -35,8 +35,9 @@ along with this program. If not, see <http://www.gnu.org/licenses/>.
35// Planck PCB default pin-out 35// Planck PCB default pin-out
36// Change this to how you wired your keyboard 36// Change this to how you wired your keyboard
37// COLS: Left to right, ROWS: Top to bottom 37// COLS: Left to right, ROWS: Top to bottom
38#define COLS (int []){ B0, B1, B2, B3, B7, D0, B6, F7, F6, F5, F4, F1 } 38#define MATRIX_ROW_PINS { F0, D6, D4, D5 }
39#define ROWS (int []){ F0, D6, D4, D5 } 39#define MATRIX_COL_PINS { B0, B1, B2, B3, B7, D0, B6, F7, F6, F5, F4, F1 }
40#define UNUSED_PINS
40 41
41/* COL2ROW or ROW2COL */ 42/* COL2ROW or ROW2COL */
42#define DIODE_DIRECTION COL2ROW 43#define DIODE_DIRECTION COL2ROW
@@ -48,7 +49,7 @@ along with this program. If not, see <http://www.gnu.org/licenses/>.
48#define BACKLIGHT_LEVELS 3 49#define BACKLIGHT_LEVELS 3
49 50
50/* Set 0 if debouncing isn't needed */ 51/* Set 0 if debouncing isn't needed */
51#define DEBOUNCE 5 52#define DEBOUNCING_DELAY 5
52 53
53/* Mechanical locking support. Use KC_LCAP, KC_LNUM or KC_LSCR instead in keymap */ 54/* Mechanical locking support. Use KC_LCAP, KC_LNUM or KC_LSCR instead in keymap */
54#define LOCKING_SUPPORT_ENABLE 55#define LOCKING_SUPPORT_ENABLE
diff --git a/keyboard/atomic/config.h b/keyboard/atomic/config.h
index 1b34decf9..bf9550e94 100644
--- a/keyboard/atomic/config.h
+++ b/keyboard/atomic/config.h
@@ -41,15 +41,16 @@ along with this program. If not, see <http://www.gnu.org/licenses/>.
41 * DIODE_DIRECTION: COL2ROW = COL = Anode (+), ROW = Cathode (-, marked on diode) 41 * DIODE_DIRECTION: COL2ROW = COL = Anode (+), ROW = Cathode (-, marked on diode)
42 * ROW2COL = ROW = Anode (+), COL = Cathode (-, marked on diode) 42 * ROW2COL = ROW = Anode (+), COL = Cathode (-, marked on diode)
43 * 43 *
44*/ 44*/
45#define COLS (int []){ F1, F0, B0, C7, F4, F5, F6, F7, D4, D6, B4, D7, D3, D2, D1 } 45#define MATRIX_ROW_PINS { D0, D5, B5, B6, C6 }
46#define ROWS (int []){ D0, D5, B5, B6, C6 } 46#define MATRIX_COL_PINS { F1, F0, B0, C7, F4, F5, F6, F7, D4, D6, B4, D7, D3, D2, D1 }
47#define UNUSED_PINS
47 48
48/* COL2ROW or ROW2COL */ 49/* COL2ROW or ROW2COL */
49#define DIODE_DIRECTION COL2ROW 50#define DIODE_DIRECTION COL2ROW
50 51
51/* Debounce reduces chatter (unintended double-presses) - set 0 if debouncing is not needed */ 52/* Debounce reduces chatter (unintended double-presses) - set 0 if debouncing is not needed */
52#define DEBOUNCE 5 53#define DEBOUNCING_DELAY 5
53 54
54/* define if matrix has ghost (lacks anti-ghosting diodes) */ 55/* define if matrix has ghost (lacks anti-ghosting diodes) */
55//#define MATRIX_HAS_GHOST 56//#define MATRIX_HAS_GHOST
@@ -62,17 +63,17 @@ along with this program. If not, see <http://www.gnu.org/licenses/>.
62/* Locking resynchronize hack */ 63/* Locking resynchronize hack */
63#define LOCKING_RESYNC_ENABLE 64#define LOCKING_RESYNC_ENABLE
64 65
65/* 66/*
66 * Force NKRO 67 * Force NKRO
67 * 68 *
68 * Force NKRO (nKey Rollover) to be enabled by default, regardless of the saved 69 * Force NKRO (nKey Rollover) to be enabled by default, regardless of the saved
69 * state in the bootmagic EEPROM settings. (Note that NKRO must be enabled in the 70 * state in the bootmagic EEPROM settings. (Note that NKRO must be enabled in the
70 * makefile for this to work.) 71 * makefile for this to work.)
71 * 72 *
72 * If forced on, NKRO can be disabled via magic key (default = LShift+RShift+N) 73 * If forced on, NKRO can be disabled via magic key (default = LShift+RShift+N)
73 * until the next keyboard reset. 74 * until the next keyboard reset.
74 * 75 *
75 * NKRO may prevent your keystrokes from being detected in the BIOS, but it is 76 * NKRO may prevent your keystrokes from being detected in the BIOS, but it is
76 * fully operational during normal computer usage. 77 * fully operational during normal computer usage.
77 * 78 *
78 * For a less heavy-handed approach, enable NKRO via magic key (LShift+RShift+N) 79 * For a less heavy-handed approach, enable NKRO via magic key (LShift+RShift+N)
@@ -90,7 +91,7 @@ along with this program. If not, see <http://www.gnu.org/licenses/>.
90 * the keyboard. They are best used in combination with the HID Listen program, 91 * the keyboard. They are best used in combination with the HID Listen program,
91 * found here: https://www.pjrc.com/teensy/hid_listen.html 92 * found here: https://www.pjrc.com/teensy/hid_listen.html
92 * 93 *
93 * The options below allow the magic key functionality to be changed. This is 94 * The options below allow the magic key functionality to be changed. This is
94 * useful if your keyboard/keypad is missing keys and you want magic key support. 95 * useful if your keyboard/keypad is missing keys and you want magic key support.
95 * 96 *
96 */ 97 */
diff --git a/keyboard/atomic/keymaps/pvc/config.h b/keyboard/atomic/keymaps/pvc/config.h
index 5e956286b..1473aac34 100644
--- a/keyboard/atomic/keymaps/pvc/config.h
+++ b/keyboard/atomic/keymaps/pvc/config.h
@@ -42,14 +42,15 @@ along with this program. If not, see <http://www.gnu.org/licenses/>.
42 * ROW2COL = ROW = Anode (+), COL = Cathode (-, marked on diode) 42 * ROW2COL = ROW = Anode (+), COL = Cathode (-, marked on diode)
43 * 43 *
44*/ 44*/
45#define COLS (int []){ F1, F0, B0, C7, F4, F5, F6, F7, D4, D6, B4, D7, D3, D2, D1 } 45#define MATRIX_ROW_PINS { D0, D5, B5, B6, B3 }
46#define ROWS (int []){ D0, D5, B5, B6, B3 } 46#define MATRIX_COL_PINS { F1, F0, B0, C7, F4, F5, F6, F7, D4, D6, B4, D7, D3, D2, D1 }
47#define UNUSED_PINS
47 48
48/* COL2ROW or ROW2COL */ 49/* COL2ROW or ROW2COL */
49#define DIODE_DIRECTION COL2ROW 50#define DIODE_DIRECTION COL2ROW
50 51
51/* Debounce reduces chatter (unintended double-presses) - set 0 if debouncing is not needed */ 52/* Debounce reduces chatter (unintended double-presses) - set 0 if debouncing is not needed */
52#define DEBOUNCE 5 53#define DEBOUNCING_DELAY 5
53 54
54/* define if matrix has ghost (lacks anti-ghosting diodes) */ 55/* define if matrix has ghost (lacks anti-ghosting diodes) */
55//#define MATRIX_HAS_GHOST 56//#define MATRIX_HAS_GHOST
diff --git a/keyboard/atreus/config.h b/keyboard/atreus/config.h
index 55d3b62b9..624d90188 100644
--- a/keyboard/atreus/config.h
+++ b/keyboard/atreus/config.h
@@ -36,11 +36,13 @@ along with this program. If not, see <http://www.gnu.org/licenses/>.
36// Change this to how you wired your keyboard 36// Change this to how you wired your keyboard
37// COLS: Left to right, ROWS: Top to bottom 37// COLS: Left to right, ROWS: Top to bottom
38#if defined(ATREUS_ASTAR) 38#if defined(ATREUS_ASTAR)
39 #define COLS (int []){ D7, C6, B5, B4, E6, D4, B6, F6, F7, D6, B7 } 39# define MATRIX_ROW_PINS { D0, D1, D3, D2 }
40 #define ROWS (int []){ D0, D1, D3, D2 } 40# define MATRIX_COL_PINS { D7, C6, B5, B4, E6, D4, B6, F6, F7, D6, B7 }
41# define UNUSED_PINS
41#elif defined(ATREUS_TEENSY2) 42#elif defined(ATREUS_TEENSY2)
42 #define COLS (int []){ F6, F5, F4, B7, B6, B5, B4, B3, B2, B1, B0} 43# define MATRIX_ROW_PINS { D0, D1, D2, D3 }
43 #define ROWS (int []){ D0, D1, D2, D3 } 44# define MATRIX_COL_PINS { F6, F5, F4, B7, B6, B5, B4, B3, B2, B1, B0 }
45# define UNUSED_PINS
44#endif 46#endif
45 47
46/* COL2ROW or ROW2COL */ 48/* COL2ROW or ROW2COL */
@@ -53,7 +55,7 @@ along with this program. If not, see <http://www.gnu.org/licenses/>.
53//#define BACKLIGHT_LEVELS 3 55//#define BACKLIGHT_LEVELS 3
54 56
55/* Set 0 if debouncing isn't needed */ 57/* Set 0 if debouncing isn't needed */
56#define DEBOUNCE 5 58#define DEBOUNCING_DELAY 5
57 59
58/* Mechanical locking support. Use KC_LCAP, KC_LNUM or KC_LSCR instead in keymap */ 60/* Mechanical locking support. Use KC_LCAP, KC_LNUM or KC_LSCR instead in keymap */
59#define LOCKING_SUPPORT_ENABLE 61#define LOCKING_SUPPORT_ENABLE
diff --git a/keyboard/clueboard1/config.h b/keyboard/clueboard1/config.h
index 2b20c3873..16338ddb0 100644
--- a/keyboard/clueboard1/config.h
+++ b/keyboard/clueboard1/config.h
@@ -32,19 +32,18 @@ along with this program. If not, see <http://www.gnu.org/licenses/>.
32#define MATRIX_ROWS 5 32#define MATRIX_ROWS 5
33#define MATRIX_COLS 16 33#define MATRIX_COLS 16
34 34
35// COLS: Left to right, ROWS: Top to bottom 35// ROWS: Top to bottom, COLS: Left to right
36
37/* Column pin configuration 36/* Column pin configuration
38 * col: 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 37* col: 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
39 * pin: B3 F1 F4 F5 F6 C7 C6 B6 B5 B4 D7 D6 D4 F7 B0 B1 38* pin: B3 F1 F4 F5 F6 C7 C6 B6 B5 B4 D7 D6 D4 F7 B0 B1
40 */ 39*/
41#define COLS (int []){ B3, F1, F4, F5, F6, C7, C6, B6, B5, B4, D7, D6, D4, F7, B0, B1 } 40#define MATRIX_COL_PINS { B3, F1, F4, F5, F6, C7, C6, B6, B5, B4, D7, D6, D4, F7, B0, B1 }
42 41/* Row pin configuration
43 /* Row pin configuration 42* row: 0 1 2 3 4
44 * row: 0 1 2 3 4 43* pin: D1 D0 D2 D5 D3
45 * pin: D1 D0 D2 D5 D3 44*/
46 */ 45#define MATRIX_ROW_PINS { D1, D0, D2, D5, D3 }
47#define ROWS (int []){ D1, D0, D2, D5, D3 } 46#define UNUSED_PINS
48 47
49/* COL2ROW or ROW2COL */ 48/* COL2ROW or ROW2COL */
50#define DIODE_DIRECTION COL2ROW 49#define DIODE_DIRECTION COL2ROW
@@ -53,7 +52,7 @@ along with this program. If not, see <http://www.gnu.org/licenses/>.
53//#define MATRIX_HAS_GHOST 52//#define MATRIX_HAS_GHOST
54 53
55/* Set 0 if debouncing isn't needed */ 54/* Set 0 if debouncing isn't needed */
56#define DEBOUNCE 5 55#define DEBOUNCING_DELAY 5
57 56
58/* Mechanical locking support. Use KC_LCAP, KC_LNUM or KC_LSCR instead in keymap */ 57/* Mechanical locking support. Use KC_LCAP, KC_LNUM or KC_LSCR instead in keymap */
59#define LOCKING_SUPPORT_ENABLE 58#define LOCKING_SUPPORT_ENABLE
diff --git a/keyboard/clueboard2/config.h b/keyboard/clueboard2/config.h
index 04166c20f..2887519af 100644
--- a/keyboard/clueboard2/config.h
+++ b/keyboard/clueboard2/config.h
@@ -32,19 +32,18 @@ along with this program. If not, see <http://www.gnu.org/licenses/>.
32#define MATRIX_ROWS 10 32#define MATRIX_ROWS 10
33#define MATRIX_COLS 8 33#define MATRIX_COLS 8
34 34
35// COLS: Left to right, ROWS: Top to bottom 35// ROWS: Top to bottom, COLS: Left to right
36 36/* Row pin configuration
37* row: 0 1 2 3 4 5 6 7 8 9
38* pin: B2 C7 C6 B6 B5 B0 B3 D5 D3 D2
39*/
40#define MATRIX_ROW_PINS { B2, C7, C6, B6, B5, B0, B3, D5, D3, D2 }
37/* Column pin configuration 41/* Column pin configuration
38 * col: 0 1 2 3 4 5 6 7 42 * col: 0 1 2 3 4 5 6 7
39 * pin: F0 F1 F4 F5 F6 F7 E6 B1 43 * pin: F0 F1 F4 F5 F6 F7 E6 B1
40 */ 44 */
41#define COLS (int []){ F0, F1, F4, F5, F6, F7, E6, B1 } 45#define MATRIX_COL_PINS { F0, F1, F4, F5, F6, F7, E6, B1 }
42 46#define UNUSED_PINS
43 /* Row pin configuration
44 * row: 0 1 2 3 4 5 6 7 8 9
45 * pin: B2 C7 C6 B6 B5 B0 B3 D5 D3 D2
46 */
47#define ROWS (int []){ B2, C7, C6, B6, B5, B0, B3, D5, D3, D2 }
48 47
49/* COL2ROW or ROW2COL */ 48/* COL2ROW or ROW2COL */
50#define DIODE_DIRECTION COL2ROW 49#define DIODE_DIRECTION COL2ROW
@@ -53,7 +52,7 @@ along with this program. If not, see <http://www.gnu.org/licenses/>.
53//#define MATRIX_HAS_GHOST 52//#define MATRIX_HAS_GHOST
54 53
55/* Set 0 if debouncing isn't needed */ 54/* Set 0 if debouncing isn't needed */
56#define DEBOUNCE 5 55#define DEBOUNCING_DELAY 5
57 56
58/* Mechanical locking support. Use KC_LCAP, KC_LNUM or KC_LSCR instead in keymap */ 57/* Mechanical locking support. Use KC_LCAP, KC_LNUM or KC_LSCR instead in keymap */
59#define LOCKING_SUPPORT_ENABLE 58#define LOCKING_SUPPORT_ENABLE
diff --git a/keyboard/cluepad/config.h b/keyboard/cluepad/config.h
index 5ed421a6e..0955e0f70 100644
--- a/keyboard/cluepad/config.h
+++ b/keyboard/cluepad/config.h
@@ -32,19 +32,18 @@ along with this program. If not, see <http://www.gnu.org/licenses/>.
32#define MATRIX_ROWS 5 32#define MATRIX_ROWS 5
33#define MATRIX_COLS 4 33#define MATRIX_COLS 4
34 34
35// COLS: Left to right, ROWS: Top to bottom 35// ROWS: Top to bottom, COLS: Left to right
36 36/* Row pin configuration
37* row: 0 1 2 3 4
38* pin:
39*/
40#define MATRIX_ROW_PINS { B0, D3, D5, D4, D6 }
37/* Column pin configuration 41/* Column pin configuration
38 * col: 0 1 2 3 42 * col: 0 1 2 3
39 * pin: F4 E6 B1 D2 43 * pin: F4 E6 B1 D2
40 */ 44 */
41#define COLS (int []){ F4, E6, B1, D2 } 45#define MATRIX_COL_PINS { F4, E6, B1, D2 }
42 46#define UNUSED_PINS
43 /* Row pin configuration
44 * row: 0 1 2 3 4
45 * pin:
46 */
47#define ROWS (int []){ B0, D3, D5, D4, D6 }
48 47
49/* COL2ROW or ROW2COL */ 48/* COL2ROW or ROW2COL */
50#define DIODE_DIRECTION COL2ROW 49#define DIODE_DIRECTION COL2ROW
@@ -53,7 +52,7 @@ along with this program. If not, see <http://www.gnu.org/licenses/>.
53//#define MATRIX_HAS_GHOST 52//#define MATRIX_HAS_GHOST
54 53
55/* Set 0 if debouncing isn't needed */ 54/* Set 0 if debouncing isn't needed */
56#define DEBOUNCE 5 55#define DEBOUNCING_DELAY 5
57 56
58/* Number of backlighting levels */ 57/* Number of backlighting levels */
59#define BACKLIGHT_LEVELS 3 58#define BACKLIGHT_LEVELS 3
diff --git a/keyboard/gh60_rev_c/config.h b/keyboard/gh60_rev_c/config.h
index 2fd8f5743..827c08d1b 100644
--- a/keyboard/gh60_rev_c/config.h
+++ b/keyboard/gh60_rev_c/config.h
@@ -41,15 +41,16 @@ along with this program. If not, see <http://www.gnu.org/licenses/>.
41 * DIODE_DIRECTION: COL2ROW = COL = Anode (+), ROW = Cathode (-, marked on diode) 41 * DIODE_DIRECTION: COL2ROW = COL = Anode (+), ROW = Cathode (-, marked on diode)
42 * ROW2COL = ROW = Anode (+), COL = Cathode (-, marked on diode) 42 * ROW2COL = ROW = Anode (+), COL = Cathode (-, marked on diode)
43 * 43 *
44*/ 44*/
45#define COLS (int []){ F0, F1, E6, C7, C6, B6, D4, B1, B7, B5, B4, D7, D6, B3 } 45#define MATRIX_ROW_PINS { D0, D1, D2, D3, D5 }
46#define ROWS (int []){ D0, D1, D2, D3, D5 } 46#define MATRIX_COL_PINS { F0, F1, E6, C7, C6, B6, D4, B1, B7, B5, B4, D7, D6, B3 }
47#define UNUSED_PINS
47 48
48/* COL2ROW or ROW2COL */ 49/* COL2ROW or ROW2COL */
49#define DIODE_DIRECTION COL2ROW 50#define DIODE_DIRECTION COL2ROW
50 51
51/* Debounce reduces chatter (unintended double-presses) - set 0 if debouncing is not needed */ 52/* Debounce reduces chatter (unintended double-presses) - set 0 if debouncing is not needed */
52#define DEBOUNCE 5 53#define DEBOUNCING_DELAY 5
53 54
54/* define if matrix has ghost (lacks anti-ghosting diodes) */ 55/* define if matrix has ghost (lacks anti-ghosting diodes) */
55//#define MATRIX_HAS_GHOST 56//#define MATRIX_HAS_GHOST
@@ -62,17 +63,17 @@ along with this program. If not, see <http://www.gnu.org/licenses/>.
62/* Locking resynchronize hack */ 63/* Locking resynchronize hack */
63#define LOCKING_RESYNC_ENABLE 64#define LOCKING_RESYNC_ENABLE
64 65
65/* 66/*
66 * Force NKRO 67 * Force NKRO
67 * 68 *
68 * Force NKRO (nKey Rollover) to be enabled by default, regardless of the saved 69 * Force NKRO (nKey Rollover) to be enabled by default, regardless of the saved
69 * state in the bootmagic EEPROM settings. (Note that NKRO must be enabled in the 70 * state in the bootmagic EEPROM settings. (Note that NKRO must be enabled in the
70 * makefile for this to work.) 71 * makefile for this to work.)
71 * 72 *
72 * If forced on, NKRO can be disabled via magic key (default = LShift+RShift+N) 73 * If forced on, NKRO can be disabled via magic key (default = LShift+RShift+N)
73 * until the next keyboard reset. 74 * until the next keyboard reset.
74 * 75 *
75 * NKRO may prevent your keystrokes from being detected in the BIOS, but it is 76 * NKRO may prevent your keystrokes from being detected in the BIOS, but it is
76 * fully operational during normal computer usage. 77 * fully operational during normal computer usage.
77 * 78 *
78 * For a less heavy-handed approach, enable NKRO via magic key (LShift+RShift+N) 79 * For a less heavy-handed approach, enable NKRO via magic key (LShift+RShift+N)
@@ -90,7 +91,7 @@ along with this program. If not, see <http://www.gnu.org/licenses/>.
90 * the keyboard. They are best used in combination with the HID Listen program, 91 * the keyboard. They are best used in combination with the HID Listen program,
91 * found here: https://www.pjrc.com/teensy/hid_listen.html 92 * found here: https://www.pjrc.com/teensy/hid_listen.html
92 * 93 *
93 * The options below allow the magic key functionality to be changed. This is 94 * The options below allow the magic key functionality to be changed. This is
94 * useful if your keyboard/keypad is missing keys and you want magic key support. 95 * useful if your keyboard/keypad is missing keys and you want magic key support.
95 * 96 *
96 */ 97 */
diff --git a/keyboard/jd45/config.h b/keyboard/jd45/config.h
index 1b81fe32e..ffedb6a1a 100644
--- a/keyboard/jd45/config.h
+++ b/keyboard/jd45/config.h
@@ -30,11 +30,12 @@ along with this program. If not, see <http://www.gnu.org/licenses/>.
30 30
31/* key matrix size */ 31/* key matrix size */
32#define MATRIX_ROWS 4 32#define MATRIX_ROWS 4
33#define MATRIX_COLS 13 33#define MATRIX_COLS 13
34 34
35/* Planck PCB default pin-out */ 35/* Planck PCB default pin-out */
36#define COLS (int []){F4, D7, B5, B6, C6, C7, D4, D6, D5, D0, D1, D2, B0} 36#define MATRIX_ROW_PINS { F0, F1, F5, B4 }
37#define ROWS (int []){F0, F1, F5, B4} 37#define MATRIX_COL_PINS { F4, D7, B5, B6, C6, C7, D4, D6, D5, D0, D1, D2, B0 }
38#define UNUSED_PINS
38 39
39/* COL2ROW or ROW2COL */ 40/* COL2ROW or ROW2COL */
40#define DIODE_DIRECTION COL2ROW 41#define DIODE_DIRECTION COL2ROW
@@ -46,7 +47,7 @@ along with this program. If not, see <http://www.gnu.org/licenses/>.
46#define BACKLIGHT_LEVELS 3 47#define BACKLIGHT_LEVELS 3
47 48
48/* Set 0 if debouncing isn't needed */ 49/* Set 0 if debouncing isn't needed */
49#define DEBOUNCE 5 50#define DEBOUNCING_DELAY 5
50 51
51/* Mechanical locking support. Use KC_LCAP, KC_LNUM or KC_LSCR instead in keymap */ 52/* Mechanical locking support. Use KC_LCAP, KC_LNUM or KC_LSCR instead in keymap */
52#define LOCKING_SUPPORT_ENABLE 53#define LOCKING_SUPPORT_ENABLE
diff --git a/keyboard/planck/config.h b/keyboard/planck/config.h
index 7d64f0977..b64893108 100644
--- a/keyboard/planck/config.h
+++ b/keyboard/planck/config.h
@@ -33,8 +33,9 @@ along with this program. If not, see <http://www.gnu.org/licenses/>.
33#define MATRIX_COLS 12 33#define MATRIX_COLS 12
34 34
35/* Planck PCB default pin-out */ 35/* Planck PCB default pin-out */
36#define COLS (int []){ F1, F0, B0, C7, F4, F5, F6, F7, D4, D6, B4, D7 } 36#define MATRIX_ROW_PINS { D0, D5, B5, B6 }
37#define ROWS (int []){ D0, D5, B5, B6 } 37#define MATRIX_COL_PINS { F1, F0, B0, C7, F4, F5, F6, F7, D4, D6, B4, D7 }
38#define UNUSED_PINS
38 39
39/* COL2ROW or ROW2COL */ 40/* COL2ROW or ROW2COL */
40#define DIODE_DIRECTION COL2ROW 41#define DIODE_DIRECTION COL2ROW
@@ -46,7 +47,7 @@ along with this program. If not, see <http://www.gnu.org/licenses/>.
46#define BACKLIGHT_LEVELS 3 47#define BACKLIGHT_LEVELS 3
47 48
48/* Set 0 if debouncing isn't needed */ 49/* Set 0 if debouncing isn't needed */
49#define DEBOUNCE 5 50#define DEBOUNCING_DELAY 5
50 51
51/* Mechanical locking support. Use KC_LCAP, KC_LNUM or KC_LSCR instead in keymap */ 52/* Mechanical locking support. Use KC_LCAP, KC_LNUM or KC_LSCR instead in keymap */
52#define LOCKING_SUPPORT_ENABLE 53#define LOCKING_SUPPORT_ENABLE
diff --git a/keyboard/planck/keymaps/pvc/config.h b/keyboard/planck/keymaps/pvc/config.h
index d435cc795..b8960038a 100644
--- a/keyboard/planck/keymaps/pvc/config.h
+++ b/keyboard/planck/keymaps/pvc/config.h
@@ -33,8 +33,9 @@ along with this program. If not, see <http://www.gnu.org/licenses/>.
33#define MATRIX_COLS 12 33#define MATRIX_COLS 12
34 34
35/* Planck PCB default pin-out */ 35/* Planck PCB default pin-out */
36#define COLS (int []){ F1, F0, B0, C7, F4, F5, F6, F7, D4, D6, B4, D7 } 36#define MATRIX_ROW_PINS { D0, D5, B5, B6 }
37#define ROWS (int []){ D0, D5, B5, B6 } 37#define MATRIX_COL_PINS { F1, F0, B0, C7, F4, F5, F6, F7, D4, D6, B4, D7 }
38#define UNUSED_PINS
38 39
39/* COL2ROW or ROW2COL */ 40/* COL2ROW or ROW2COL */
40#define DIODE_DIRECTION COL2ROW 41#define DIODE_DIRECTION COL2ROW
@@ -46,7 +47,7 @@ along with this program. If not, see <http://www.gnu.org/licenses/>.
46#define BACKLIGHT_LEVELS 3 47#define BACKLIGHT_LEVELS 3
47 48
48/* Set 0 if debouncing isn't needed */ 49/* Set 0 if debouncing isn't needed */
49#define DEBOUNCE 5 50#define DEBOUNCING_DELAY 5
50 51
51/* Mechanical locking support. Use KC_LCAP, KC_LNUM or KC_LSCR instead in keymap */ 52/* Mechanical locking support. Use KC_LCAP, KC_LNUM or KC_LSCR instead in keymap */
52#define LOCKING_SUPPORT_ENABLE 53#define LOCKING_SUPPORT_ENABLE
diff --git a/keyboard/preonic/config.h b/keyboard/preonic/config.h
index bb9d29dab..e8d0e82fc 100644
--- a/keyboard/preonic/config.h
+++ b/keyboard/preonic/config.h
@@ -33,8 +33,9 @@ along with this program. If not, see <http://www.gnu.org/licenses/>.
33#define MATRIX_COLS 12 33#define MATRIX_COLS 12
34 34
35/* Planck PCB default pin-out */ 35/* Planck PCB default pin-out */
36#define COLS (int []){ F1, F0, B0, C7, F4, F5, F6, F7, D4, D6, B4, D7 } 36#define MATRIX_ROW_PINS { D2, D5, B5, B6, D3 }
37#define ROWS (int []){ D2, D5, B5, B6, D3 } 37#define MATRIX_COL_PINS { F1, F0, B0, C7, F4, F5, F6, F7, D4, D6, B4, D7 }
38#define UNUSED_PINS
38 39
39/* COL2ROW or ROW2COL */ 40/* COL2ROW or ROW2COL */
40#define DIODE_DIRECTION COL2ROW 41#define DIODE_DIRECTION COL2ROW
@@ -46,7 +47,7 @@ along with this program. If not, see <http://www.gnu.org/licenses/>.
46#define BACKLIGHT_LEVELS 3 47#define BACKLIGHT_LEVELS 3
47 48
48/* Set 0 if debouncing isn't needed */ 49/* Set 0 if debouncing isn't needed */
49#define DEBOUNCE 5 50#define DEBOUNCING_DELAY 5
50 51
51/* Mechanical locking support. Use KC_LCAP, KC_LNUM or KC_LSCR instead in keymap */ 52/* Mechanical locking support. Use KC_LCAP, KC_LNUM or KC_LSCR instead in keymap */
52#define LOCKING_SUPPORT_ENABLE 53#define LOCKING_SUPPORT_ENABLE
diff --git a/keyboard/retro_refit/config.h b/keyboard/retro_refit/config.h
index 05fceb92b..f2194e550 100644
--- a/keyboard/retro_refit/config.h
+++ b/keyboard/retro_refit/config.h
@@ -33,8 +33,9 @@ along with this program. If not, see <http://www.gnu.org/licenses/>.
33#define MATRIX_COLS 8 33#define MATRIX_COLS 8
34 34
35// See note in retro_refit.h for an explanation of how this matrix is wired up 35// See note in retro_refit.h for an explanation of how this matrix is wired up
36#define COLS (int []){ B0, B1, B2, B3, D2, D3, C7, D5 } 36#define MATRIX_ROW_PINS { D4, D7, B4, B5, B6, F7, F6, F5, F4, F1, F0 }
37#define ROWS (int []){ D4, D7, B4, B5, B6, F7, F6, F5, F4, F1, F0 } 37#define MATRIX_COL_PINS { B0, B1, B2, B3, D2, D3, C7, D5 }
38#define UNUSED_PINS
38 39
39/* COL2ROW or ROW2COL */ 40/* COL2ROW or ROW2COL */
40#define DIODE_DIRECTION COL2ROW 41#define DIODE_DIRECTION COL2ROW
@@ -46,7 +47,7 @@ along with this program. If not, see <http://www.gnu.org/licenses/>.
46#define BACKLIGHT_LEVELS 0 47#define BACKLIGHT_LEVELS 0
47 48
48/* Set 0 if debouncing isn't needed */ 49/* Set 0 if debouncing isn't needed */
49#define DEBOUNCE 5 50#define DEBOUNCING_DELAY 5
50 51
51/* Mechanical locking support. Use KC_LCAP, KC_LNUM or KC_LSCR instead in keymap */ 52/* Mechanical locking support. Use KC_LCAP, KC_LNUM or KC_LSCR instead in keymap */
52#define LOCKING_SUPPORT_ENABLE 53#define LOCKING_SUPPORT_ENABLE
@@ -119,4 +120,4 @@ along with this program. If not, see <http://www.gnu.org/licenses/>.
119//#define NO_ACTION_MACRO 120//#define NO_ACTION_MACRO
120//#define NO_ACTION_FUNCTION 121//#define NO_ACTION_FUNCTION
121 122
122#endif \ No newline at end of file 123#endif
diff --git a/quantum/config_common.h b/quantum/config_common.h
index da53fce89..02f11d979 100644
--- a/quantum/config_common.h
+++ b/quantum/config_common.h
@@ -1,70 +1,74 @@
1#ifndef CONFIG_DEFINITIONS_H 1#ifndef CONFIG_DEFINITIONS_H
2#define CONFIG_DEFINITIONS_H 2#define CONFIG_DEFINITIONS_H
3 3
4#define B0 0x20 4/* diode directions */
5#define B1 0x21 5#define COL2ROW 0
6#define B2 0x22 6#define ROW2COL 1
7#define B3 0x23 7/* I/O pins */
8#define B4 0x24 8#define B0 { .input_addr = 3, .bit = 0 }
9#define B5 0x25 9#define B1 { .input_addr = 3, .bit = 1 }
10#define B6 0x26 10#define B2 { .input_addr = 3, .bit = 2 }
11#define B7 0x27 11#define B3 { .input_addr = 3, .bit = 3 }
12#define C0 0x30 12#define B4 { .input_addr = 3, .bit = 4 }
13#define C1 0x31 13#define B5 { .input_addr = 3, .bit = 5 }
14#define C2 0x32 14#define B6 { .input_addr = 3, .bit = 6 }
15#define C3 0x33 15#define B7 { .input_addr = 3, .bit = 7 }
16#define C4 0x34 16#define C0 { .input_addr = 6, .bit = 0 }
17#define C5 0x35 17#define C1 { .input_addr = 6, .bit = 1 }
18#define C6 0x36 18#define C2 { .input_addr = 6, .bit = 2 }
19#define C7 0x37 19#define C3 { .input_addr = 6, .bit = 3 }
20#define D0 0x40 20#define C4 { .input_addr = 6, .bit = 4 }
21#define D1 0x41 21#define C5 { .input_addr = 6, .bit = 5 }
22#define D2 0x42 22#define C6 { .input_addr = 6, .bit = 6 }
23#define D3 0x43 23#define C7 { .input_addr = 6, .bit = 7 }
24#define D4 0x44 24#define D0 { .input_addr = 9, .bit = 0 }
25#define D5 0x45 25#define D1 { .input_addr = 9, .bit = 1 }
26#define D6 0x46 26#define D2 { .input_addr = 9, .bit = 2 }
27#define D7 0x47 27#define D3 { .input_addr = 9, .bit = 3 }
28#define E0 0x50 28#define D4 { .input_addr = 9, .bit = 4 }
29#define E1 0x51 29#define D5 { .input_addr = 9, .bit = 5 }
30#define E2 0x52 30#define D6 { .input_addr = 9, .bit = 6 }
31#define E3 0x53 31#define D7 { .input_addr = 9, .bit = 7 }
32#define E4 0x54 32#define E0 { .input_addr = 0xC, .bit = 0 }
33#define E5 0x55 33#define E1 { .input_addr = 0xC, .bit = 1 }
34#define E6 0x56 34#define E2 { .input_addr = 0xC, .bit = 2 }
35#define E7 0x57 35#define E3 { .input_addr = 0xC, .bit = 3 }
36#define F0 0x60 36#define E4 { .input_addr = 0xC, .bit = 4 }
37#define F1 0x61 37#define E5 { .input_addr = 0xC, .bit = 5 }
38#define F2 0x62 38#define E6 { .input_addr = 0xC, .bit = 6 }
39#define F3 0x63 39#define E7 { .input_addr = 0xC, .bit = 7 }
40#define F4 0x64 40#define F0 { .input_addr = 0xF, .bit = 0 }
41#define F5 0x65 41#define F1 { .input_addr = 0xF, .bit = 1 }
42#define F6 0x66 42#define F2 { .input_addr = 0xF, .bit = 2 }
43#define F7 0x67 43#define F3 { .input_addr = 0xF, .bit = 3 }
44 44#define F4 { .input_addr = 0xF, .bit = 4 }
45#define COL2ROW 0x0 45#define F5 { .input_addr = 0xF, .bit = 5 }
46#define ROW2COL 0x1 46#define F6 { .input_addr = 0xF, .bit = 6 }
47#define F7 { .input_addr = 0xF, .bit = 7 }
47 48
49/* USART configuration */
48#ifdef BLUETOOTH_ENABLE 50#ifdef BLUETOOTH_ENABLE
49#ifdef __AVR_ATmega32U4__ 51# ifdef __AVR_ATmega32U4__
50 #define SERIAL_UART_BAUD 9600 52# define SERIAL_UART_BAUD 9600
51 #define SERIAL_UART_DATA UDR1 53# define SERIAL_UART_DATA UDR1
52 #define SERIAL_UART_UBRR ((F_CPU/(16UL*SERIAL_UART_BAUD))-1) 54# define SERIAL_UART_UBRR (F_CPU / (16UL * SERIAL_UART_BAUD) - 1)
53 #define SERIAL_UART_RXD_VECT USART1_RX_vect 55# define SERIAL_UART_RXD_VECT USART1_RX_vect
54 #define SERIAL_UART_TXD_READY (UCSR1A&(1<<UDRE1)) 56# define SERIAL_UART_TXD_READY (UCSR1A & _BV(UDRE1))
55 #define SERIAL_UART_INIT() do { \ 57# define SERIAL_UART_INIT() do { \
56 UBRR1L = (uint8_t) SERIAL_UART_UBRR; /* baud rate */ \ 58 /* baud rate */ \
57 UBRR1H = (uint8_t) (SERIAL_UART_UBRR>>8); /* baud rate */ \ 59 UBRR1L = SERIAL_UART_UBRR; \
58 UCSR1B = (1<<TXEN1); /* TX: enable */ \ 60 /* baud rate */ \
59 UCSR1C = (0<<UPM11) | (0<<UPM10) | /* parity: none(00), even(01), odd(11) */ \ 61 UBRR1H = SERIAL_UART_UBRR >> 8; \
60 (0<<UCSZ12) | (1<<UCSZ11) | (1<<UCSZ10); /* data-8bit(011) */ \ 62 /* enable TX */ \
61 sei(); \ 63 UCSR1B = _BV(TXEN1); \
62 } while(0) 64 /* 8-bit data */ \
63#else 65 UCSR1C = _BV(UCSZ11) | _BV(UCSZ10); \
64# error "USART configuration is needed." 66 sei(); \
67 } while(0)
68# else
69# error "USART configuration is needed."
65#endif 70#endif
66 71
67
68// I'm fairly sure these aren't needed, but oh well - Jack 72// I'm fairly sure these aren't needed, but oh well - Jack
69 73
70/* 74/*
@@ -113,4 +117,3 @@
113#endif 117#endif
114 118
115#endif 119#endif
116
diff --git a/quantum/matrix.c b/quantum/matrix.c
index cab39e117..22126aa7a 100644
--- a/quantum/matrix.c
+++ b/quantum/matrix.c
@@ -1,6 +1,6 @@
1/* 1/*
2Copyright 2012 Jun Wako 2Copyright 2012 Jun Wako
3Generated by planckkeyboard.com (2014 Jack Humbert) 3Copyright 2014 Jack Humbert
4 4
5This program is free software: you can redistribute it and/or modify 5This program is free software: you can redistribute it and/or modify
6it under the terms of the GNU General Public License as published by 6it under the terms of the GNU General Public License as published by
@@ -15,300 +15,211 @@ GNU General Public License for more details.
15You should have received a copy of the GNU General Public License 15You should have received a copy of the GNU General Public License
16along with this program. If not, see <http://www.gnu.org/licenses/>. 16along with this program. If not, see <http://www.gnu.org/licenses/>.
17*/ 17*/
18
19/*
20 * scan matrix
21 */
22#include <stdint.h> 18#include <stdint.h>
23#include <stdbool.h> 19#include <stdbool.h>
24#include <avr/io.h> 20#include <avr/io.h>
25#include <util/delay.h> 21#include "wait.h"
26#include "print.h" 22#include "print.h"
27#include "debug.h" 23#include "debug.h"
28#include "util.h" 24#include "util.h"
29#include "matrix.h" 25#include "matrix.h"
30 26
31#ifndef DEBOUNCE 27#ifdef MATRIX_HAS_GHOST
32# define DEBOUNCE 10 28# error "The universal matrix.c file cannot be used for this keyboard."
33#endif 29#endif
34static uint8_t debouncing = DEBOUNCE;
35 30
36/* matrix state(1:on, 0:off) */ 31#ifndef DEBOUNCING_DELAY
37static matrix_row_t matrix[MATRIX_ROWS]; 32# define DEBOUNCING_DELAY 5
38static matrix_row_t matrix_debouncing[MATRIX_ROWS];
39
40#if DIODE_DIRECTION == ROW2COL
41 static matrix_row_t matrix_reversed[MATRIX_COLS];
42 static matrix_row_t matrix_reversed_debouncing[MATRIX_COLS];
43#endif 33#endif
44 34
45 35static const io_pin_t row_pins[MATRIX_ROWS] = MATRIX_ROW_PINS;
46#if MATRIX_COLS > 16 36static const io_pin_t col_pins[MATRIX_COLS] = MATRIX_COL_PINS;
47 #define SHIFTER 1UL 37/* matrix state */
38#if DIODE_DIRECTION == COL2ROW
39static matrix_row_t matrix[MATRIX_ROWS];
40static matrix_row_t debouncing_matrix[MATRIX_ROWS];
48#else 41#else
49 #define SHIFTER 1 42static matrix_col_t matrix[MATRIX_COLS];
43static matrix_col_t debouncing_matrix[MATRIX_COLS];
50#endif 44#endif
45static int8_t debouncing_delay = -1;
51 46
47#if DIODE_DIRECTION == COL2ROW
48static void toggle_row(uint8_t row);
52static matrix_row_t read_cols(void); 49static matrix_row_t read_cols(void);
53static void init_cols(void); 50#else
54static void unselect_rows(void); 51static void toggle_col(uint8_t col);
55static void select_row(uint8_t row); 52static matrix_col_t read_rows(void);
53#endif
56 54
57__attribute__ ((weak)) 55__attribute__ ((weak))
58void matrix_init_quantum(void) { 56void matrix_init_quantum(void) {
59
60} 57}
61 58
62__attribute__ ((weak)) 59__attribute__ ((weak))
63void matrix_scan_quantum(void) { 60void matrix_scan_quantum(void) {
64
65} 61}
66 62
67inline 63uint8_t matrix_rows(void) {
68uint8_t matrix_rows(void)
69{
70 return MATRIX_ROWS; 64 return MATRIX_ROWS;
71} 65}
72 66
73inline 67uint8_t matrix_cols(void) {
74uint8_t matrix_cols(void)
75{
76 return MATRIX_COLS; 68 return MATRIX_COLS;
77} 69}
78 70
79void matrix_init(void) 71void matrix_init(void) {
80{ 72 /* frees PORTF by setting the JTD bit twice within four cycles */
81 // To use PORTF disable JTAG with writing JTD bit twice within four cycles. 73 MCUCR |= _BV(JTD);
82 MCUCR |= (1<<JTD); 74 MCUCR |= _BV(JTD);
83 MCUCR |= (1<<JTD); 75 /* initializes the I/O pins */
84 76#if DIODE_DIRECTION == COL2ROW
85 77 for (int8_t r = MATRIX_ROWS - 1; r >= 0; --r) {
86 // initialize row and col 78 /* DDRxn */
87 unselect_rows(); 79 _SFR_IO8(row_pins[r].input_addr + 1) |= _BV(row_pins[r].bit);
88 init_cols(); 80 toggle_row(r);
89
90 // initialize matrix state: all keys off
91 for (uint8_t i=0; i < MATRIX_ROWS; i++) {
92 matrix[i] = 0;
93 matrix_debouncing[i] = 0;
94 } 81 }
95 82 for (int8_t c = MATRIX_COLS - 1; c >= 0; --c) {
83 /* PORTxn */
84 _SFR_IO8(col_pins[c].input_addr + 2) |= _BV(col_pins[c].bit);
85 }
86#else
87 for (int8_t c = MATRIX_COLS - 1; c >= 0; --c) {
88 /* DDRxn */
89 _SFR_IO8(col_pins[c].input_addr + 1) |= _BV(col_pins[c].bit);
90 toggle_col(c);
91 }
92 for (int8_t r = MATRIX_ROWS - 1; r >= 0; --r) {
93 /* PORTxn */
94 _SFR_IO8(row_pins[r].input_addr + 2) |= _BV(row_pins[r].bit);
95 }
96#endif
96 matrix_init_quantum(); 97 matrix_init_quantum();
97} 98}
98 99
99
100uint8_t matrix_scan(void)
101{
102
103#if DIODE_DIRECTION == COL2ROW 100#if DIODE_DIRECTION == COL2ROW
104 for (uint8_t i = 0; i < MATRIX_ROWS; i++) { 101uint8_t matrix_scan(void) {
105 select_row(i); 102 for (int8_t r = MATRIX_ROWS - 1; r >= 0; --r) {
106 _delay_us(30); // without this wait read unstable value. 103 toggle_row(r);
107 matrix_row_t cols = read_cols(); 104 matrix_row_t state = read_cols();
108 if (matrix_debouncing[i] != cols) { 105 if (debouncing_matrix[r] != state) {
109 matrix_debouncing[i] = cols; 106 debouncing_matrix[r] = state;
110 if (debouncing) { 107 debouncing_delay = DEBOUNCING_DELAY;
111 debug("bounce!: "); debug_hex(debouncing); debug("\n");
112 }
113 debouncing = DEBOUNCE;
114 } 108 }
115 unselect_rows(); 109 toggle_row(r);
116 } 110 }
117 111 if (debouncing_delay >= 0) {
118 if (debouncing) { 112 dprintf("Debouncing delay remaining: %X\n", debouncing_delay);
119 if (--debouncing) { 113 --debouncing_delay;
120 _delay_ms(1); 114 if (debouncing_delay >= 0) {
121 } else { 115 wait_ms(1);
122 for (uint8_t i = 0; i < MATRIX_ROWS; i++) {
123 matrix[i] = matrix_debouncing[i];
124 }
125 } 116 }
126 } 117 else {
127#else 118 for (int8_t r = MATRIX_ROWS - 1; r >= 0; --r) {
128 for (uint8_t i = 0; i < MATRIX_COLS; i++) { 119 matrix[r] = debouncing_matrix[r];
129 select_row(i);
130 _delay_us(30); // without this wait read unstable value.
131 matrix_row_t rows = read_cols();
132 if (matrix_reversed_debouncing[i] != rows) {
133 matrix_reversed_debouncing[i] = rows;
134 if (debouncing) {
135 debug("bounce!: "); debug_hex(debouncing); debug("\n");
136 } 120 }
137 debouncing = DEBOUNCE;
138 } 121 }
139 unselect_rows();
140 } 122 }
141
142 if (debouncing) {
143 if (--debouncing) {
144 _delay_ms(1);
145 } else {
146 for (uint8_t i = 0; i < MATRIX_COLS; i++) {
147 matrix_reversed[i] = matrix_reversed_debouncing[i];
148 }
149 }
150 }
151 for (uint8_t y = 0; y < MATRIX_ROWS; y++) {
152 matrix_row_t row = 0;
153 for (uint8_t x = 0; x < MATRIX_COLS; x++) {
154 row |= ((matrix_reversed[x] & (1<<y)) >> y) << x;
155 }
156 matrix[y] = row;
157 }
158#endif
159
160 matrix_scan_quantum(); 123 matrix_scan_quantum();
161
162 return 1; 124 return 1;
163} 125}
164 126
165bool matrix_is_modified(void) 127static void toggle_row(uint8_t row) {
166{ 128 /* PINxn */
167 if (debouncing) return false; 129 _SFR_IO8(row_pins[row].input_addr) = _BV(row_pins[row].bit);
168 return true;
169} 130}
170 131
171inline 132static matrix_row_t read_cols(void) {
172bool matrix_is_on(uint8_t row, uint8_t col) 133 matrix_row_t state = 0;
173{ 134 for (int8_t c = MATRIX_COLS - 1; c >= 0; --c) {
174 return (matrix[row] & ((matrix_row_t)1<col)); 135 /* PINxn */
136 if (!(_SFR_IO8(col_pins[c].input_addr) & _BV(col_pins[c].bit))) {
137 state |= (matrix_row_t)1 << c;
138 }
139 }
140 return state;
175} 141}
176 142
177inline 143matrix_row_t matrix_get_row(uint8_t row) {
178matrix_row_t matrix_get_row(uint8_t row)
179{
180 return matrix[row]; 144 return matrix[row];
181} 145}
182 146
183void matrix_print(void) 147#else
184{ 148uint8_t matrix_scan(void) {
185 print("\nr/c 0123456789ABCDEF\n"); 149 for (int8_t c = MATRIX_COLS - 1; c >= 0; --c) {
186 for (uint8_t row = 0; row < MATRIX_ROWS; row++) { 150 toggle_col(c);
187 phex(row); print(": "); 151 matrix_col_t state = read_rows();
188 pbin_reverse16(matrix_get_row(row)); 152 if (debouncing_matrix[c] != state) {
189 print("\n"); 153 debouncing_matrix[c] = state;
154 debouncing_delay = DEBOUNCING_DELAY;
155 }
156 toggle_col(c);
190 } 157 }
191} 158 if (debouncing_delay >= 0) {
192 159 dprintf("Debouncing delay remaining: %X\n", debouncing_delay);
193uint8_t matrix_key_count(void) 160 --debouncing_delay;
194{ 161 if (debouncing_delay >= 0) {
195 uint8_t count = 0; 162 wait_ms(1);
196 for (uint8_t i = 0; i < MATRIX_ROWS; i++) { 163 }
197 count += bitpop16(matrix[i]); 164 else {
165 for (int8_t c = MATRIX_COLS - 1; c >= 0; --c) {
166 matrix[c] = debouncing_matrix[c];
167 }
168 }
198 } 169 }
199 return count; 170 matrix_scan_quantum();
171 return 1;
200} 172}
201 173
202static void init_cols(void) 174static void toggle_col(uint8_t col) {
203{ 175 /* PINxn */
204 int B = 0, C = 0, D = 0, E = 0, F = 0; 176 _SFR_IO8(col_pins[col].input_addr) = _BV(col_pins[col].bit);
177}
205 178
206#if DIODE_DIRECTION == COL2ROW 179static matrix_col_t read_rows(void) {
207 for(int x = 0; x < MATRIX_COLS; x++) { 180 matrix_col_t state = 0;
208 int col = COLS[x]; 181 for (int8_t r = MATRIX_ROWS - 1; r >= 0; --r) {
209#else 182 /* PINxn */
210 for(int x = 0; x < MATRIX_ROWS; x++) { 183 if (!(_SFR_IO8(row_pins[r].input_addr) & _BV(row_pins[r].bit))) {
211 int col = ROWS[x]; 184 state |= (matrix_col_t)1 << r;
212#endif 185 }
213 if ((col & 0xF0) == 0x20) {
214 B |= (1<<(col & 0x0F));
215 } else if ((col & 0xF0) == 0x30) {
216 C |= (1<<(col & 0x0F));
217 } else if ((col & 0xF0) == 0x40) {
218 D |= (1<<(col & 0x0F));
219 } else if ((col & 0xF0) == 0x50) {
220 E |= (1<<(col & 0x0F));
221 } else if ((col & 0xF0) == 0x60) {
222 F |= (1<<(col & 0x0F));
223 }
224 } 186 }
225 DDRB &= ~(B); PORTB |= (B); 187 return state;
226 DDRC &= ~(C); PORTC |= (C);
227 DDRD &= ~(D); PORTD |= (D);
228 DDRE &= ~(E); PORTE |= (E);
229 DDRF &= ~(F); PORTF |= (F);
230} 188}
231 189
232static matrix_row_t read_cols(void) 190matrix_row_t matrix_get_row(uint8_t row) {
233{ 191 matrix_row_t state = 0;
234 matrix_row_t result = 0; 192 matrix_col_t mask = (matrix_col_t)1 << row;
193 for (int8_t c = MATRIX_COLS - 1; c >= 0; --c) {
194 if (matrix[c] & mask) {
195 state |= (matrix_row_t)1 << c;
196 }
197 }
198 return state;
199}
235 200
236#if DIODE_DIRECTION == COL2ROW
237 for(int x = 0; x < MATRIX_COLS; x++) {
238 int col = COLS[x];
239#else
240 for(int x = 0; x < MATRIX_ROWS; x++) {
241 int col = ROWS[x];
242#endif 201#endif
243 202
244 if ((col & 0xF0) == 0x20) { 203bool matrix_is_modified(void) {
245 result |= (PINB&(1<<(col & 0x0F)) ? 0 : (SHIFTER<<x)); 204 if (debouncing_delay >= 0) return false;
246 } else if ((col & 0xF0) == 0x30) { 205 return true;
247 result |= (PINC&(1<<(col & 0x0F)) ? 0 : (SHIFTER<<x));
248 } else if ((col & 0xF0) == 0x40) {
249 result |= (PIND&(1<<(col & 0x0F)) ? 0 : (SHIFTER<<x));
250 } else if ((col & 0xF0) == 0x50) {
251 result |= (PINE&(1<<(col & 0x0F)) ? 0 : (SHIFTER<<x));
252 } else if ((col & 0xF0) == 0x60) {
253 result |= (PINF&(1<<(col & 0x0F)) ? 0 : (SHIFTER<<x));
254 }
255 }
256 return result;
257} 206}
258 207
259static void unselect_rows(void) 208bool matrix_is_on(uint8_t row, uint8_t col) {
260{ 209 return matrix_get_row(row) & (matrix_row_t)1 << col;
261 int B = 0, C = 0, D = 0, E = 0, F = 0; 210}
262 211
263#if DIODE_DIRECTION == COL2ROW 212void matrix_print(void) {
264 for(int x = 0; x < MATRIX_ROWS; x++) { 213 dprintln("Human-readable matrix state:");
265 int row = ROWS[x]; 214 for (uint8_t r = 0; r < MATRIX_ROWS; r++) {
266#else 215 dprintf("State of row %X: %016b\n", r, bitrev16(matrix_get_row(r)));
267 for(int x = 0; x < MATRIX_COLS; x++) {
268 int row = COLS[x];
269#endif
270 if ((row & 0xF0) == 0x20) {
271 B |= (1<<(row & 0x0F));
272 } else if ((row & 0xF0) == 0x30) {
273 C |= (1<<(row & 0x0F));
274 } else if ((row & 0xF0) == 0x40) {
275 D |= (1<<(row & 0x0F));
276 } else if ((row & 0xF0) == 0x50) {
277 E |= (1<<(row & 0x0F));
278 } else if ((row & 0xF0) == 0x60) {
279 F |= (1<<(row & 0x0F));
280 }
281 } 216 }
282 DDRB &= ~(B); PORTB |= (B);
283 DDRC &= ~(C); PORTC |= (C);
284 DDRD &= ~(D); PORTD |= (D);
285 DDRE &= ~(E); PORTE |= (E);
286 DDRF &= ~(F); PORTF |= (F);
287} 217}
288 218
289static void select_row(uint8_t row) 219uint8_t matrix_key_count(void) {
290{ 220 uint8_t count = 0;
291 221 for (int8_t r = MATRIX_ROWS - 1; r >= 0; --r) {
292#if DIODE_DIRECTION == COL2ROW 222 count += bitpop16(matrix_get_row(r));
293 int row_pin = ROWS[row]; 223 }
294#else 224 return count;
295 int row_pin = COLS[row]; 225}
296#endif
297
298 if ((row_pin & 0xF0) == 0x20) {
299 DDRB |= (1<<(row_pin & 0x0F));
300 PORTB &= ~(1<<(row_pin & 0x0F));
301 } else if ((row_pin & 0xF0) == 0x30) {
302 DDRC |= (1<<(row_pin & 0x0F));
303 PORTC &= ~(1<<(row_pin & 0x0F));
304 } else if ((row_pin & 0xF0) == 0x40) {
305 DDRD |= (1<<(row_pin & 0x0F));
306 PORTD &= ~(1<<(row_pin & 0x0F));
307 } else if ((row_pin & 0xF0) == 0x50) {
308 DDRE |= (1<<(row_pin & 0x0F));
309 PORTE &= ~(1<<(row_pin & 0x0F));
310 } else if ((row_pin & 0xF0) == 0x60) {
311 DDRF |= (1<<(row_pin & 0x0F));
312 PORTF &= ~(1<<(row_pin & 0x0F));
313 }
314} \ No newline at end of file
diff --git a/quantum/template/config.h b/quantum/template/config.h
index e6fb7866c..cad3e3260 100644
--- a/quantum/template/config.h
+++ b/quantum/template/config.h
@@ -41,15 +41,16 @@ along with this program. If not, see <http://www.gnu.org/licenses/>.
41 * DIODE_DIRECTION: COL2ROW = COL = Anode (+), ROW = Cathode (-, marked on diode) 41 * DIODE_DIRECTION: COL2ROW = COL = Anode (+), ROW = Cathode (-, marked on diode)
42 * ROW2COL = ROW = Anode (+), COL = Cathode (-, marked on diode) 42 * ROW2COL = ROW = Anode (+), COL = Cathode (-, marked on diode)
43 * 43 *
44*/ 44*/
45#define COLS (int []){ F1, F0, B0 } 45#define MATRIX_ROW_PINS { D0, D5 }
46#define ROWS (int []){ D0, D5 } 46#define MATRIX_COL_PINS { F1, F0, B0 }
47#define UNUSED_PINS
47 48
48/* COL2ROW or ROW2COL */ 49/* COL2ROW or ROW2COL */
49#define DIODE_DIRECTION COL2ROW 50#define DIODE_DIRECTION COL2ROW
50 51
51/* Debounce reduces chatter (unintended double-presses) - set 0 if debouncing is not needed */ 52/* Debounce reduces chatter (unintended double-presses) - set 0 if debouncing is not needed */
52#define DEBOUNCE 5 53#define DEBOUNCING_DELAY 5
53 54
54/* define if matrix has ghost (lacks anti-ghosting diodes) */ 55/* define if matrix has ghost (lacks anti-ghosting diodes) */
55//#define MATRIX_HAS_GHOST 56//#define MATRIX_HAS_GHOST
@@ -62,17 +63,17 @@ along with this program. If not, see <http://www.gnu.org/licenses/>.
62/* Locking resynchronize hack */ 63/* Locking resynchronize hack */
63#define LOCKING_RESYNC_ENABLE 64#define LOCKING_RESYNC_ENABLE
64 65
65/* 66/*
66 * Force NKRO 67 * Force NKRO
67 * 68 *
68 * Force NKRO (nKey Rollover) to be enabled by default, regardless of the saved 69 * Force NKRO (nKey Rollover) to be enabled by default, regardless of the saved
69 * state in the bootmagic EEPROM settings. (Note that NKRO must be enabled in the 70 * state in the bootmagic EEPROM settings. (Note that NKRO must be enabled in the
70 * makefile for this to work.) 71 * makefile for this to work.)
71 * 72 *
72 * If forced on, NKRO can be disabled via magic key (default = LShift+RShift+N) 73 * If forced on, NKRO can be disabled via magic key (default = LShift+RShift+N)
73 * until the next keyboard reset. 74 * until the next keyboard reset.
74 * 75 *
75 * NKRO may prevent your keystrokes from being detected in the BIOS, but it is 76 * NKRO may prevent your keystrokes from being detected in the BIOS, but it is
76 * fully operational during normal computer usage. 77 * fully operational during normal computer usage.
77 * 78 *
78 * For a less heavy-handed approach, enable NKRO via magic key (LShift+RShift+N) 79 * For a less heavy-handed approach, enable NKRO via magic key (LShift+RShift+N)
@@ -90,7 +91,7 @@ along with this program. If not, see <http://www.gnu.org/licenses/>.
90 * the keyboard. They are best used in combination with the HID Listen program, 91 * the keyboard. They are best used in combination with the HID Listen program,
91 * found here: https://www.pjrc.com/teensy/hid_listen.html 92 * found here: https://www.pjrc.com/teensy/hid_listen.html
92 * 93 *
93 * The options below allow the magic key functionality to be changed. This is 94 * The options below allow the magic key functionality to be changed. This is
94 * useful if your keyboard/keypad is missing keys and you want magic key support. 95 * useful if your keyboard/keypad is missing keys and you want magic key support.
95 * 96 *
96 */ 97 */
diff --git a/tmk_core/common/avr/suspend.c b/tmk_core/common/avr/suspend.c
index 498068019..a6f3c6441 100644
--- a/tmk_core/common/avr/suspend.c
+++ b/tmk_core/common/avr/suspend.c
@@ -114,9 +114,7 @@ bool suspend_wakeup_condition(void)
114 matrix_power_up(); 114 matrix_power_up();
115 matrix_scan(); 115 matrix_scan();
116 matrix_power_down(); 116 matrix_power_down();
117 for (uint8_t r = 0; r < MATRIX_ROWS; r++) { 117 if (matrix_key_count()) return true;
118 if (matrix_get_row(r)) return true;
119 }
120 return false; 118 return false;
121} 119}
122 120
@@ -146,4 +144,3 @@ ISR(WDT_vect)
146 } 144 }
147} 145}
148#endif 146#endif
149
diff --git a/tmk_core/common/bootmagic.c b/tmk_core/common/bootmagic.c
index 2c1b1adfc..30e8a0f20 100644
--- a/tmk_core/common/bootmagic.c
+++ b/tmk_core/common/bootmagic.c
@@ -105,15 +105,13 @@ void bootmagic(void)
105 } 105 }
106} 106}
107 107
108static bool scan_keycode(uint8_t keycode) 108static bool scan_keycode(uint8_t keycode) {
109{ 109 for (int8_t r = MATRIX_ROWS - 1; r >= 0; --r) {
110 for (uint8_t r = 0; r < MATRIX_ROWS; r++) {
111 matrix_row_t matrix_row = matrix_get_row(r); 110 matrix_row_t matrix_row = matrix_get_row(r);
112 for (uint8_t c = 0; c < MATRIX_COLS; c++) { 111 for (int8_t c = MATRIX_COLS - 1; c >= 0; --c) {
113 if (matrix_row & ((matrix_row_t)1<<c)) { 112 if (matrix_row & (matrix_row_t)1 << c) {
114 if (keycode == keymap_key_to_keycode(0, (keypos_t){ .row = r, .col = c })) { 113 keypos_t key = (keypos_t){ .row = r, .col = c };
115 return true; 114 if (keycode == keymap_key_to_keycode(0, key)) return true;
116 }
117 } 115 }
118 } 116 }
119 } 117 }
diff --git a/tmk_core/common/keyboard.c b/tmk_core/common/keyboard.c
index 1d9981848..c6d54ae33 100644
--- a/tmk_core/common/keyboard.c
+++ b/tmk_core/common/keyboard.c
@@ -1,5 +1,5 @@
1/* 1/*
2Copyright 2011,2012,2013 Jun Wako <wakojun@gmail.com> 2Copyright 2011, 2012, 2013 Jun Wako <wakojun@gmail.com>
3 3
4This program is free software: you can redistribute it and/or modify 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 5it under the terms of the GNU General Public License as published by
@@ -27,13 +27,13 @@ along with this program. If not, see <http://www.gnu.org/licenses/>.
27#include "command.h" 27#include "command.h"
28#include "util.h" 28#include "util.h"
29#include "sendchar.h" 29#include "sendchar.h"
30#include "eeconfig.h"
31#include "backlight.h"
30#ifdef BOOTMAGIC_ENABLE 32#ifdef BOOTMAGIC_ENABLE
31 #include "bootmagic.h" 33# include "bootmagic.h"
32#else 34#else
33 #include "magic.h" 35# include "magic.h"
34#endif 36#endif
35#include "eeconfig.h"
36#include "backlight.h"
37#ifdef MOUSEKEY_ENABLE 37#ifdef MOUSEKEY_ENABLE
38# include "mousekey.h" 38# include "mousekey.h"
39#endif 39#endif
@@ -41,40 +41,35 @@ along with this program. If not, see <http://www.gnu.org/licenses/>.
41# include "ps2_mouse.h" 41# include "ps2_mouse.h"
42#endif 42#endif
43#ifdef SERIAL_MOUSE_ENABLE 43#ifdef SERIAL_MOUSE_ENABLE
44#include "serial_mouse.h" 44# include "serial_mouse.h"
45#endif 45#endif
46#ifdef ADB_MOUSE_ENABLE 46#ifdef ADB_MOUSE_ENABLE
47#include "adb.h" 47# include "adb.h"
48#endif 48#endif
49 49
50
51#ifdef MATRIX_HAS_GHOST 50#ifdef MATRIX_HAS_GHOST
52static bool has_ghost_in_row(uint8_t row) 51static bool is_row_ghosting(uint8_t row){
53{ 52 matrix_row_t state = matrix_get_row(row);
54 matrix_row_t matrix_row = matrix_get_row(row); 53 /* no ghosting happens when only one key in the row is pressed */
55 // No ghost exists when less than 2 keys are down on the row 54 if (!(state - 1 & state)) return false;
56 if (((matrix_row - 1) & matrix_row) == 0) 55 /* ghosting occurs when two keys in the same column are pressed */
57 return false; 56 for (int8_t r = MATRIX_ROWS - 1; r >= 0; --r) {
58 57 if (r != row && matrix_get_row(r) & state) return true;
59 // Ghost occurs when the row shares column line with other row
60 for (uint8_t i=0; i < MATRIX_ROWS; i++) {
61 if (i != row && (matrix_get_row(i) & matrix_row))
62 return true;
63 } 58 }
64 return false; 59 return false;
65} 60}
61
66#endif 62#endif
67 63
64__attribute__ ((weak))
65void matrix_setup(void) {
66}
68 67
69__attribute__ ((weak)) void matrix_setup(void) {} 68void keyboard_setup(void) {
70void keyboard_setup(void)
71{
72 matrix_setup(); 69 matrix_setup();
73} 70}
74 71
75void keyboard_init(void) 72void keyboard_init(void) {
76{
77
78 timer_init(); 73 timer_init();
79 matrix_init(); 74 matrix_init();
80#ifdef PS2_MOUSE_ENABLE 75#ifdef PS2_MOUSE_ENABLE
@@ -86,104 +81,87 @@ void keyboard_init(void)
86#ifdef ADB_MOUSE_ENABLE 81#ifdef ADB_MOUSE_ENABLE
87 adb_mouse_init(); 82 adb_mouse_init();
88#endif 83#endif
89
90
91#ifdef BOOTMAGIC_ENABLE 84#ifdef BOOTMAGIC_ENABLE
92 bootmagic(); 85 bootmagic();
93#else 86#else
94 magic(); 87 magic();
95#endif 88#endif
96
97#ifdef BACKLIGHT_ENABLE 89#ifdef BACKLIGHT_ENABLE
98 backlight_init(); 90 backlight_init();
99#endif 91#endif
100
101#if defined(NKRO_ENABLE) && defined(FORCE_NKRO) 92#if defined(NKRO_ENABLE) && defined(FORCE_NKRO)
102 keyboard_nkro = true; 93 keyboard_nkro = true;
103#endif 94#endif
104
105} 95}
106 96
107/* 97/* does routine keyboard jobs */
108 * Do keyboard routine jobs: scan mantrix, light LEDs, ... 98void keyboard_task(void) {
109 * This is repeatedly called as fast as possible. 99 static matrix_row_t previous_matrix[MATRIX_ROWS];
110 */
111void keyboard_task(void)
112{
113 static matrix_row_t matrix_prev[MATRIX_ROWS];
114#ifdef MATRIX_HAS_GHOST 100#ifdef MATRIX_HAS_GHOST
115 static matrix_row_t matrix_ghost[MATRIX_ROWS]; 101 static matrix_row_t deghosting_matrix[MATRIX_ROWS];
116#endif 102#endif
117 static uint8_t led_status = 0; 103 static uint8_t led_status = 0;
118 matrix_row_t matrix_row = 0;
119 matrix_row_t matrix_change = 0;
120
121 matrix_scan(); 104 matrix_scan();
122 for (uint8_t r = 0; r < MATRIX_ROWS; r++) { 105 for (int8_t r = MATRIX_ROWS - 1; r >= 0; --r) {
123 matrix_row = matrix_get_row(r); 106 matrix_row_t state = matrix_get_row(r);
124 matrix_change = matrix_row ^ matrix_prev[r]; 107 matrix_row_t changes = state ^ previous_matrix[r];
125 if (matrix_change) { 108 if (changes) {
126#ifdef MATRIX_HAS_GHOST 109#ifdef MATRIX_HAS_GHOST
127 if (has_ghost_in_row(r)) { 110 if (is_row_ghosting(r)) {
128 /* Keep track of whether ghosted status has changed for 111 /* debugs the deghosting mechanism */
129 * debugging. But don't update matrix_prev until un-ghosted, or 112 /* doesn't update previous_matrix until the ghosting has stopped
130 * the last key would be lost. 113 * in order to prevent the last key from being lost
131 */ 114 */
132 if (debug_matrix && matrix_ghost[r] != matrix_row) { 115 if (debug_matrix && deghosting_matrix[r] != state) {
133 matrix_print(); 116 matrix_print();
134 } 117 }
135 matrix_ghost[r] = matrix_row; 118 deghosting_matrix[r] = state;
136 continue; 119 continue;
137 } 120 }
138 matrix_ghost[r] = matrix_row; 121 deghosting_matrix[r] = state;
139#endif 122#endif
140 if (debug_matrix) matrix_print(); 123 if (debug_matrix) matrix_print();
141 for (uint8_t c = 0; c < MATRIX_COLS; c++) { 124 for (int8_t c = MATRIX_COLS - 1; c >= 0; --c) {
142 if (matrix_change & ((matrix_row_t)1<<c)) { 125 matrix_row_t mask = (matrix_row_t)1 << c;
143 action_exec((keyevent_t){ 126 if (changes & mask) {
144 .key = (keypos_t){ .row = r, .col = c }, 127 keyevent_t event;
145 .pressed = (matrix_row & ((matrix_row_t)1<<c)), 128 event.key = (keypos_t){ .row = r, .col = c };
146 .time = (timer_read() | 1) /* time should not be 0 */ 129 event.pressed = state & mask;
147 }); 130 /* the time should not be 0 */
148 // record a processed key 131 event.time = timer_read() | 1;
149 matrix_prev[r] ^= ((matrix_row_t)1<<c); 132 action_exec(event);
150 // process a key per task call 133 /* records the processed key event */
151 goto MATRIX_LOOP_END; 134 previous_matrix[r] ^= mask;
135 /* processes one key event per call */
136 goto event_processed;
152 } 137 }
153 } 138 }
154 } 139 }
155 } 140 }
156 // call with pseudo tick event when no real key event. 141 /* sends tick events when the keyboard is idle */
157 action_exec(TICK); 142 action_exec(TICK);
158 143event_processed:
159MATRIX_LOOP_END:
160
161#ifdef MOUSEKEY_ENABLE 144#ifdef MOUSEKEY_ENABLE
162 // mousekey repeat & acceleration 145 /* repeats and accelerates the mouse keys */
163 mousekey_task(); 146 mousekey_task();
164#endif 147#endif
165
166#ifdef PS2_MOUSE_ENABLE 148#ifdef PS2_MOUSE_ENABLE
167 ps2_mouse_task(); 149 ps2_mouse_task();
168#endif 150#endif
169
170#ifdef SERIAL_MOUSE_ENABLE 151#ifdef SERIAL_MOUSE_ENABLE
171 serial_mouse_task(); 152 serial_mouse_task();
172#endif 153#endif
173
174#ifdef ADB_MOUSE_ENABLE 154#ifdef ADB_MOUSE_ENABLE
175 adb_mouse_task(); 155 adb_mouse_task();
176#endif 156#endif
177 157 /* updates the LEDs */
178 // update LED
179 if (led_status != host_keyboard_leds()) { 158 if (led_status != host_keyboard_leds()) {
180 led_status = host_keyboard_leds(); 159 led_status = host_keyboard_leds();
181 keyboard_set_leds(led_status); 160 keyboard_set_leds(led_status);
182 } 161 }
183} 162}
184 163
185void keyboard_set_leds(uint8_t leds) 164void keyboard_set_leds(uint8_t leds) {
186{ 165 if (debug_keyboard) dprintf("Keyboard LEDs state: %x\n", leds);
187 if (debug_keyboard) { debug("keyboard_set_led: "); debug_hex8(leds); debug("\n"); }
188 led_set(leds); 166 led_set(leds);
189} 167}
diff --git a/tmk_core/common/matrix.h b/tmk_core/common/matrix.h
index ad0871bfb..5f380aaab 100644
--- a/tmk_core/common/matrix.h
+++ b/tmk_core/common/matrix.h
@@ -14,59 +14,68 @@ GNU General Public License for more details.
14You should have received a copy of the GNU General Public License 14You should have received a copy of the GNU General Public License
15along with this program. If not, see <http://www.gnu.org/licenses/>. 15along with this program. If not, see <http://www.gnu.org/licenses/>.
16*/ 16*/
17
18#ifndef MATRIX_H 17#ifndef MATRIX_H
19#define MATRIX_H 18#define MATRIX_H
20 19
21#include <stdint.h> 20#include <stdint.h>
22#include <stdbool.h> 21#include <stdbool.h>
23 22
24 23#if MATRIX_COLS <= 8
25#if (MATRIX_COLS <= 8) 24typedef uint8_t matrix_row_t;
26typedef uint8_t matrix_row_t; 25#elif MATRIX_COLS <= 16
27#elif (MATRIX_COLS <= 16) 26typedef uint16_t matrix_row_t;
28typedef uint16_t matrix_row_t; 27#elif MATRIX_COLS <= 32
29#elif (MATRIX_COLS <= 32) 28typedef uint32_t matrix_row_t;
30typedef uint32_t matrix_row_t;
31#else 29#else
32#error "MATRIX_COLS: invalid value" 30# error "There are too many columns."
33#endif 31#endif
34 32
35#define MATRIX_IS_ON(row, col) (matrix_get_row(row) && (1<<col)) 33#if DIODE_DIRECTION == ROW2COL
34# if MATRIX_ROWS <= 8
35typedef uint8_t matrix_col_t;
36# elif MATRIX_ROWS <= 16
37typedef uint16_t matrix_col_t;
38# elif MATRIX_ROWS <= 32
39typedef uint32_t matrix_col_t;
40# else
41# error "There are too many rows."
42# endif
43#endif
36 44
45typedef struct {
46 uint8_t input_addr:4;
47 uint8_t bit:4;
48} io_pin_t;
37 49
38#ifdef __cplusplus 50#ifdef __cplusplus
39extern "C" { 51extern "C" {
40#endif 52#endif
41 53/* counts the number of rows in the matrix */
42/* number of matrix rows */
43uint8_t matrix_rows(void); 54uint8_t matrix_rows(void);
44/* number of matrix columns */ 55/* counts the number of columns in the matrix */
45uint8_t matrix_cols(void); 56uint8_t matrix_cols(void);
46/* should be called at early stage of startup before matrix_init.(optional) */ 57/* sets up the matrix before matrix_init */
47void matrix_setup(void); 58void matrix_setup(void);
48/* intialize matrix for scaning. */ 59/* intializes the matrix */
49void matrix_init(void); 60void matrix_init(void);
50/* scan all key states on matrix */ 61/* scans the entire matrix */
51uint8_t matrix_scan(void); 62uint8_t matrix_scan(void);
52/* whether modified from previous scan. used after matrix_scan. */ 63/* checks if the matrix has been modified */
53bool matrix_is_modified(void) __attribute__ ((deprecated)); 64bool matrix_is_modified(void) __attribute__ ((deprecated));
54/* whether a swtich is on */ 65/* checks if a key is pressed */
55bool matrix_is_on(uint8_t row, uint8_t col); 66bool matrix_is_on(uint8_t row, uint8_t col);
56/* matrix state on row */ 67/* inspects the state of a row in the matrix */
57matrix_row_t matrix_get_row(uint8_t row); 68matrix_row_t matrix_get_row(uint8_t row);
58/* print matrix for debug */ 69/* prints the matrix for debugging */
59void matrix_print(void); 70void matrix_print(void);
60 71/* counts the total number of keys pressed */
61 72uint8_t matrix_key_count(void);
62/* power control */ 73/* controls power to the matrix */
63void matrix_power_up(void); 74void matrix_power_up(void);
64void matrix_power_down(void); 75void matrix_power_down(void);
65 76/* executes code for Quantum */
66/* keyboard-specific setup/loop functionality */
67void matrix_init_quantum(void); 77void matrix_init_quantum(void);
68void matrix_scan_quantum(void); 78void matrix_scan_quantum(void);
69
70#ifdef __cplusplus 79#ifdef __cplusplus
71} 80}
72#endif 81#endif