Simplify split_common Code significantly (#4772)

* Eliminate separate slave loop

Both master and slave run the standard keyboard_task main loop now.

* Refactor i2c/serial specific code

Simplify some of the preprocessor mess by using common function names.

* Fix missing #endif

* Move direct pin mapping support from miniaxe to split_common

For boards with more pins than sense--sorry, switches.

* Reordering and reformatting only

* Don't run matrix_scan_quantum on slave side

* Clean up the offset/slaveOffset calculations

* Cut undebounced matrix size in half

* Refactor debouncing

* Minor fixups

* Split split_common transport and debounce code into their own files

Can now be replaced with custom versions per keyboard using
CUSTOM_TRANSPORT = yes and CUSTOM_DEBOUNCE = yes

* Refactor debounce for non-split keyboards too

* Update handwired/xealous to build using new split_common

* Fix debounce breaking basic test

* Dodgy method to allow a split kb to only include one of i2c/serial

SPLIT_TRANSPORT = serial or SPLIT_TRANSPORT = i2c will include only
that driver code in the binary.

SPLIT_TRANSPORT = custom (or anything else) will include neither, the
keyboard must supply it's own code

if SPLIT_TRANSPORT is not defined then the original behaviour (include
both avr i2c and serial code) is maintained.

This could be better but it would require explicitly updating all the
existing split keyboards.

* Enable LTO to get lets_split/sockets under the line

* Add docs for SPLIT_TRANSPORT, CUSTOM_MATRIX, CUSTOM_DEBOUNCE

* Remove avr-specific sei() from split matrix_setup

Not needed now that slave doesn't have a separate main loop.
Both sides (on avr) call sei() in lufa's main() after exiting
keyboard_setup().

* Fix QUANTUM_LIB_SRC references and simplify SPLIT_TRANSPORT.

* Add comments and fix formatting.
This commit is contained in:
James Churchill
2019-01-18 04:08:14 +10:00
committed by Drashna Jaelre
parent 5fcca9a226
commit 28929ad017
24 changed files with 768 additions and 1378 deletions

View File

@ -254,20 +254,34 @@ QUANTUM_SRC:= \
$(QUANTUM_DIR)/keymap_common.c \ $(QUANTUM_DIR)/keymap_common.c \
$(QUANTUM_DIR)/keycode_config.c $(QUANTUM_DIR)/keycode_config.c
ifeq ($(strip $(SPLIT_KEYBOARD)), yes) # Include the standard or split matrix code if needed
ifneq ($(strip $(CUSTOM_MATRIX)), yes) ifneq ($(strip $(CUSTOM_MATRIX)), yes)
QUANTUM_SRC += $(QUANTUM_DIR)/split_common/matrix.c ifeq ($(strip $(SPLIT_KEYBOARD)), yes)
# Do not use $(QUANTUM_DIR)/matrix.c. QUANTUM_SRC += $(QUANTUM_DIR)/split_common/matrix.c
CUSTOM_MATRIX=yes else
QUANTUM_SRC += $(QUANTUM_DIR)/matrix.c
endif endif
OPT_DEFS += -DSPLIT_KEYBOARD
QUANTUM_SRC += $(QUANTUM_DIR)/split_common/split_flags.c \
$(QUANTUM_DIR)/split_common/split_util.c
QUANTUM_LIB_SRC += $(QUANTUM_DIR)/split_common/i2c.c
QUANTUM_LIB_SRC += $(QUANTUM_DIR)/split_common/serial.c
COMMON_VPATH += $(QUANTUM_PATH)/split_common
endif endif
ifneq ($(strip $(CUSTOM_MATRIX)), yes) # Include the standard debounce code if needed
QUANTUM_SRC += $(QUANTUM_DIR)/matrix.c ifneq ($(strip $(CUSTOM_DEBOUNCE)), yes)
QUANTUM_SRC += $(QUANTUM_DIR)/debounce.c
endif
ifeq ($(strip $(SPLIT_KEYBOARD)), yes)
OPT_DEFS += -DSPLIT_KEYBOARD
# Include files used by all split keyboards
QUANTUM_SRC += $(QUANTUM_DIR)/split_common/split_flags.c \
$(QUANTUM_DIR)/split_common/split_util.c
# Determine which (if any) transport files are required
ifneq ($(strip $(SPLIT_TRANSPORT)), custom)
QUANTUM_SRC += $(QUANTUM_DIR)/split_common/transport.c
# Functions added via QUANTUM_LIB_SRC are only included in the final binary if they're called.
# Unused functions are pruned away, which is why we can add both drivers here without bloat.
QUANTUM_LIB_SRC += $(QUANTUM_DIR)/split_common/i2c.c \
$(QUANTUM_DIR)/split_common/serial.c
endif
COMMON_VPATH += $(QUANTUM_PATH)/split_common
endif endif

View File

@ -143,7 +143,7 @@ If you define these options you will enable the associated feature, which may in
* Breaks any Tap Toggle functionality (`TT` or the One Shot Tap Toggle) * Breaks any Tap Toggle functionality (`TT` or the One Shot Tap Toggle)
* `#define LEADER_TIMEOUT 300` * `#define LEADER_TIMEOUT 300`
* how long before the leader key times out * how long before the leader key times out
* If you're having issues finishing the sequence before it times out, you may need to increase the timeout setting. Or you may want to enable the `LEADER_PER_KEY_TIMING` option, which resets the timeout after each key is tapped. * If you're having issues finishing the sequence before it times out, you may need to increase the timeout setting. Or you may want to enable the `LEADER_PER_KEY_TIMING` option, which resets the timeout after each key is tapped.
* `#define LEADER_PER_KEY_TIMING` * `#define LEADER_PER_KEY_TIMING`
* sets the timer for leader key chords to run on each key press rather than overall * sets the timer for leader key chords to run on each key press rather than overall
* `#define LEADER_KEY_STRICT_KEY_PROCESSING` * `#define LEADER_KEY_STRICT_KEY_PROCESSING`
@ -197,6 +197,9 @@ If you define these options you will enable the associated feature, which may in
Split Keyboard specific options, make sure you have 'SPLIT_KEYBOARD = yes' in your rules.mk Split Keyboard specific options, make sure you have 'SPLIT_KEYBOARD = yes' in your rules.mk
* `SPLIT_TRANSPORT = custom`
* Allows replacing the standard split communication routines with a custom one. ARM based split keyboards must use this at present.
### Setting Handedness ### Setting Handedness
One thing to remember, the side that the USB port is plugged into is always the master half. The side not plugged into USB is the slave. One thing to remember, the side that the USB port is plugged into is always the master half. The side not plugged into USB is the slave.
@ -208,7 +211,7 @@ There are a few different ways to set handedness for split keyboards (listed in
3. Set `MASTER_RIGHT`: Half that is plugged into the USB port is determined to be the master and right half (inverse of the default) 3. Set `MASTER_RIGHT`: Half that is plugged into the USB port is determined to be the master and right half (inverse of the default)
4. Default: The side that is plugged into the USB port is the master half and is assumed to be the left half. The slave side is the right half 4. Default: The side that is plugged into the USB port is the master half and is assumed to be the left half. The slave side is the right half
* `#define SPLIT_HAND_PIN B7` * `#define SPLIT_HAND_PIN B7`
* For using high/low pin to determine handedness, low = right hand, high = left hand. Replace `B7` with the pin you are using. This is optional, and if you leave `SPLIT_HAND_PIN` undefined, then you can still use the EE_HANDS method or MASTER_LEFT / MASTER_RIGHT defines like the stock Let's Split uses. * For using high/low pin to determine handedness, low = right hand, high = left hand. Replace `B7` with the pin you are using. This is optional, and if you leave `SPLIT_HAND_PIN` undefined, then you can still use the EE_HANDS method or MASTER_LEFT / MASTER_RIGHT defines like the stock Let's Split uses.
* `#define EE_HANDS` (only works if `SPLIT_HAND_PIN` is not defined) * `#define EE_HANDS` (only works if `SPLIT_HAND_PIN` is not defined)
@ -302,6 +305,10 @@ Use these to enable or disable building certain features. The more you have enab
* Current options are AdafruitEzKey, AdafruitBLE, RN42 * Current options are AdafruitEzKey, AdafruitBLE, RN42
* `SPLIT_KEYBOARD` * `SPLIT_KEYBOARD`
* Enables split keyboard support (dual MCU like the let's split and bakingpy's boards) and includes all necessary files located at quantum/split_common * Enables split keyboard support (dual MCU like the let's split and bakingpy's boards) and includes all necessary files located at quantum/split_common
* `CUSTOM_MATRIX`
* Allows replacing the standard matrix scanning routine with a custom one.
* `CUSTOM_DEBOUNCE`
* Allows replacing the standard key debouncing routine with a custom one.
* `WAIT_FOR_USB` * `WAIT_FOR_USB`
* Forces the keyboard to wait for a USB connection to be established before it starts up * Forces the keyboard to wait for a USB connection to be established before it starts up
* `NO_USB_STARTUP_CHECK` * `NO_USB_STARTUP_CHECK`

View File

@ -97,7 +97,7 @@ This allows you to send Unicode characters using `UC(<code point>)` in your keym
`UNICODEMAP_ENABLE` `UNICODEMAP_ENABLE`
This allows you to send Unicode characters using `X(<map index>)` in your keymap. You will need to maintain a mapping table in your keymap file. All possible code points (up to `0x10FFFF`) are supported. This allows you to send Unicode characters using `X(<map index>)` in your keymap. You will need to maintain a mapping table in your keymap file. All possible code points (up to `0x10FFFF`) are supported.
`UCIS_ENABLE` `UCIS_ENABLE`
@ -135,6 +135,18 @@ This enables [key lock](feature_key_lock.md). This consumes an additional 260 by
This enables split keyboard support (dual MCU like the let's split and bakingpy's boards) and includes all necessary files located at quantum/split_common This enables split keyboard support (dual MCU like the let's split and bakingpy's boards) and includes all necessary files located at quantum/split_common
`SPLIT_TRANSPORT`
As there is no standard split communication driver for ARM-based split keyboards yet, `SPLIT_TRANSPORT = custom` must be used for these. It will prevent the standard split keyboard communication code (which is AVR-specific) from being included, allowing a custom implementation to be used.
`CUSTOM_MATRIX`
Lets you replace the default matrix scanning routine with your own code. You will need to provide your own implementations of matrix_init() and matrix_scan().
`CUSTOM_DEBOUNCE`
Lets you replace the default key debouncing routine with your own code. You will need to provide your own implementation of debounce().
## Customizing Makefile Options on a Per-Keymap Basis ## Customizing Makefile Options on a Per-Keymap Basis
If your keymap directory has a file called `rules.mk` any options you set in that file will take precedence over other `rules.mk` options for your particular keyboard. If your keymap directory has a file called `rules.mk` any options you set in that file will take precedence over other `rules.mk` options for your particular keyboard.

View File

@ -0,0 +1,63 @@
#include <string.h>
#include "config.h"
#include "matrix.h"
#include "timer.h"
#include "quantum.h"
#ifndef DEBOUNCING_DELAY
# define DEBOUNCING_DELAY 5
#endif
//Debouncing counters
typedef uint8_t debounce_counter_t;
#define DEBOUNCE_COUNTER_MODULO 250
#define DEBOUNCE_COUNTER_INACTIVE 251
static debounce_counter_t *debounce_counters;
void debounce_init(uint8_t num_rows)
{
debounce_counters = malloc(num_rows*MATRIX_COLS);
memset(debounce_counters, DEBOUNCE_COUNTER_INACTIVE, num_rows*MATRIX_COLS);
}
void update_debounce_counters(uint8_t num_rows, uint8_t current_time)
{
for (uint8_t row = 0; row < num_rows; row++)
{
for (uint8_t col = 0; col < MATRIX_COLS; col++)
{
if (debounce_counters[row*MATRIX_COLS + col] != DEBOUNCE_COUNTER_INACTIVE)
{
if (TIMER_DIFF(current_time, debounce_counters[row*MATRIX_COLS + col], DEBOUNCE_COUNTER_MODULO) >= DEBOUNCING_DELAY) {
debounce_counters[row*MATRIX_COLS + col] = DEBOUNCE_COUNTER_INACTIVE;
}
}
}
}
}
void transfer_matrix_values(matrix_row_t raw[], matrix_row_t cooked[], uint8_t num_rows, uint8_t current_time)
{
for (uint8_t row = 0; row < num_rows; row++)
{
matrix_row_t delta = raw[row] ^ cooked[row];
for (uint8_t col = 0; col < MATRIX_COLS; col++)
{
if (debounce_counters[row*MATRIX_COLS + col] == DEBOUNCE_COUNTER_INACTIVE && (delta & (1<<col)))
{
debounce_counters[row*MATRIX_COLS + col] = current_time;
cooked[row] ^= (1 << col);
}
}
}
}
void debounce(matrix_row_t raw[], matrix_row_t cooked[], uint8_t num_rows, bool changed)
{
uint8_t current_time = timer_read() % DEBOUNCE_COUNTER_MODULO;
update_debounce_counters(num_rows, current_time);
transfer_matrix_values(raw, cooked, num_rows, current_time);
}

View File

@ -1,4 +1,5 @@
SRC += matrix_scanrate.c matrix.c #SRC += matrix_scanrate.c matrix.c
SRC += debounce.c
# MCU name # MCU name
MCU = atmega32u4 MCU = atmega32u4
@ -37,7 +38,7 @@ F_USB = $(F_CPU)
# Bootloader # Bootloader
# This definition is optional, and if your keyboard supports multiple bootloaders of # This definition is optional, and if your keyboard supports multiple bootloaders of
# different sizes, comment this out, and the correct address will be loaded # different sizes, comment this out, and the correct address will be loaded
# automatically (+60). See bootloader.mk for all options. # automatically (+60). See bootloader.mk for all options.
BOOTLOADER = caterina BOOTLOADER = caterina
@ -59,14 +60,15 @@ MIDI_ENABLE = no # MIDI controls
AUDIO_ENABLE = yes # Audio output on port C6 AUDIO_ENABLE = yes # Audio output on port C6
UNICODE_ENABLE = no # Unicode UNICODE_ENABLE = no # Unicode
BLUETOOTH_ENABLE = no # Enable Bluetooth with the Adafruit EZ-Key HID BLUETOOTH_ENABLE = no # Enable Bluetooth with the Adafruit EZ-Key HID
RGBLIGHT_ENABLE = no # Enable WS2812 RGB underlight. RGBLIGHT_ENABLE = no # Enable WS2812 RGB underlight.
SPLIT_KEYBOARD = yes # Use shared split_common code SPLIT_KEYBOARD = yes # Use shared split_common code
SUBPROJECT_rev1 = yes SUBPROJECT_rev1 = yes
# Do not enable SLEEP_LED_ENABLE. it uses the same timer as BACKLIGHT_ENABLE # Do not enable SLEEP_LED_ENABLE. it uses the same timer as BACKLIGHT_ENABLE
SLEEP_LED_ENABLE = no # Breathing sleep LED during USB suspend SLEEP_LED_ENABLE = no # Breathing sleep LED during USB suspend
CUSTOM_MATRIX = yes CUSTOM_MATRIX = no
CUSTOM_DEBOUNCE = yes
LAYOUTS = split60 LAYOUTS = split60

View File

@ -85,3 +85,12 @@ along with this program. If not, see <http://www.gnu.org/licenses/>.
//#define NO_ACTION_ONESHOT //#define NO_ACTION_ONESHOT
//#define NO_ACTION_MACRO //#define NO_ACTION_MACRO
//#define NO_ACTION_FUNCTION //#define NO_ACTION_FUNCTION
#ifdef USE_Link_Time_Optimization
// LTO has issues with macros (action_get_macro) and "functions" (fn_actions),
// so just disable them
#define NO_ACTION_MACRO
#define NO_ACTION_FUNCTION
#define DISABLE_LEADER
#endif // USE_Link_Time_Optimization

View File

@ -1,3 +1,5 @@
BACKLIGHT_ENABLE = no BACKLIGHT_ENABLE = no
AUDIO_ENABLE = yes AUDIO_ENABLE = yes
RGBLIGHT_ENABLE = yes #Don't enable this along with I2C RGBLIGHT_ENABLE = yes #Don't enable this along with I2C
EXTRAFLAGS += -flto -DUSE_Link_Time_Optimization

View File

@ -44,8 +44,7 @@ along with this program. If not, see <http://www.gnu.org/licenses/>.
*/ */
// #define MATRIX_ROW_PINS { D0, D5 } // #define MATRIX_ROW_PINS { D0, D5 }
// #define MATRIX_COL_PINS { F1, F0, B0 } // #define MATRIX_COL_PINS { F1, F0, B0 }
#define NO_PIN 0xFF #define DIRECT_PINS { \
#define MATRIX_ROW_COL_PINS { \
{ F1, E6, B0, B2, B3 }, \ { F1, E6, B0, B2, B3 }, \
{ F5, F0, B1, B7, D2 }, \ { F5, F0, B1, B7, D2 }, \
{ F6, F7, C7, D5, D3 }, \ { F6, F7, C7, D5, D3 }, \
@ -54,7 +53,7 @@ along with this program. If not, see <http://www.gnu.org/licenses/>.
#define UNUSED_PINS #define UNUSED_PINS
/* COL2ROW, ROW2COL, or CUSTOM_MATRIX */ /* COL2ROW, ROW2COL, or CUSTOM_MATRIX */
#define DIODE_DIRECTION CUSTOM_MATRIX //#define DIODE_DIRECTION CUSTOM_MATRIX
// #define BACKLIGHT_PIN B7 // #define BACKLIGHT_PIN B7
// #define BACKLIGHT_BREATHING // #define BACKLIGHT_BREATHING

File diff suppressed because it is too large Load Diff

View File

@ -1,4 +1,3 @@
SRC += matrix.c
# MCU name # MCU name
#MCU = at90usb1286 #MCU = at90usb1286
@ -83,6 +82,6 @@ FAUXCLICKY_ENABLE = no # Use buzzer to emulate clicky switches
HD44780_ENABLE = no # Enable support for HD44780 based LCDs (+400) HD44780_ENABLE = no # Enable support for HD44780 based LCDs (+400)
DEBUG_ENABLE = no DEBUG_ENABLE = no
CUSTOM_MATRIX = yes # Use custom matrix code CUSTOM_MATRIX = no # Use custom matrix code
SPLIT_KEYBOARD = yes # Use shared split_common code SPLIT_KEYBOARD = yes # Use shared split_common code

View File

@ -21,6 +21,9 @@
#define ROW2COL 1 #define ROW2COL 1
#define CUSTOM_MATRIX 2 /* Disables built-in matrix scanning code */ #define CUSTOM_MATRIX 2 /* Disables built-in matrix scanning code */
// useful for direct pin mapping
#define NO_PIN (~0)
#ifdef __AVR__ #ifdef __AVR__
#ifndef __ASSEMBLER__ #ifndef __ASSEMBLER__
#include <avr/io.h> #include <avr/io.h>

52
quantum/debounce.c Normal file
View File

@ -0,0 +1,52 @@
#include "matrix.h"
#include "timer.h"
#include "quantum.h"
#ifndef DEBOUNCING_DELAY
# define DEBOUNCING_DELAY 5
#endif
void debounce_init(uint8_t num_rows) {
}
#if DEBOUNCING_DELAY > 0
static bool debouncing = false;
void debounce(matrix_row_t raw[], matrix_row_t cooked[], uint8_t num_rows, bool changed) {
static uint16_t debouncing_time;
if (changed) {
debouncing = true;
debouncing_time = timer_read();
}
if (debouncing && (timer_elapsed(debouncing_time) > DEBOUNCING_DELAY)) {
for (uint8_t i = 0; i < num_rows; i++) {
cooked[i] = raw[i];
}
debouncing = false;
}
}
bool debounce_active(void) {
return debouncing;
}
#else
// no debounce
void debounce(matrix_row_t raw[], matrix_row_t cooked[], uint8_t num_rows, bool changed) {
if (changed)
{
for (uint8_t i = 0; i < num_rows; i++) {
cooked[i] = raw[i];
}
}
}
bool debounce_active(void) {
return false;
}
#endif

11
quantum/debounce.h Normal file
View File

@ -0,0 +1,11 @@
#pragma once
// raw is the current key state
// on entry cooked is the previous debounced state
// on exit cooked is the current debounced state
// changed is true if raw has changed since the last call
void debounce(matrix_row_t raw[], matrix_row_t cooked[], uint8_t num_rows, bool changed);
bool debounce_active(void);
void debounce_init(uint8_t num_rows);

View File

@ -21,21 +21,9 @@ along with this program. If not, see <http://www.gnu.org/licenses/>.
#include "debug.h" #include "debug.h"
#include "util.h" #include "util.h"
#include "matrix.h" #include "matrix.h"
#include "timer.h" #include "debounce.h"
#include "quantum.h" #include "quantum.h"
/* Set 0 if debouncing isn't needed */
#ifndef DEBOUNCING_DELAY
# define DEBOUNCING_DELAY 5
#endif
#if (DEBOUNCING_DELAY > 0)
static uint16_t debouncing_time;
static bool debouncing = false;
#endif
#if (MATRIX_COLS <= 8) #if (MATRIX_COLS <= 8)
# define print_matrix_header() print("\nr/c 01234567\n") # define print_matrix_header() print("\nr/c 01234567\n")
# define print_matrix_row(row) print_bin_reverse8(matrix_get_row(row)) # define print_matrix_row(row) print_bin_reverse8(matrix_get_row(row))
@ -63,9 +51,9 @@ static const pin_t col_pins[MATRIX_COLS] = MATRIX_COL_PINS;
#endif #endif
/* matrix state(1:on, 0:off) */ /* matrix state(1:on, 0:off) */
static matrix_row_t matrix[MATRIX_ROWS]; static matrix_row_t raw_matrix[MATRIX_ROWS];
static matrix_row_t matrix_debouncing[MATRIX_ROWS]; static matrix_row_t matrix[MATRIX_ROWS];
#if (DIODE_DIRECTION == COL2ROW) #if (DIODE_DIRECTION == COL2ROW)
@ -157,70 +145,39 @@ void matrix_init(void) {
// initialize matrix state: all keys off // initialize matrix state: all keys off
for (uint8_t i=0; i < MATRIX_ROWS; i++) { for (uint8_t i=0; i < MATRIX_ROWS; i++) {
raw_matrix[i] = 0;
matrix[i] = 0; matrix[i] = 0;
matrix_debouncing[i] = 0;
} }
debounce_init(MATRIX_ROWS);
matrix_init_quantum(); matrix_init_quantum();
} }
uint8_t matrix_scan(void) uint8_t matrix_scan(void)
{ {
bool changed = false;
#if (DIODE_DIRECTION == COL2ROW) #if (DIODE_DIRECTION == COL2ROW)
// Set row, read cols
// Set row, read cols for (uint8_t current_row = 0; current_row < MATRIX_ROWS; current_row++) {
for (uint8_t current_row = 0; current_row < MATRIX_ROWS; current_row++) { changed |= read_cols_on_row(raw_matrix, current_row);
# if (DEBOUNCING_DELAY > 0) }
bool matrix_changed = read_cols_on_row(matrix_debouncing, current_row);
if (matrix_changed) {
debouncing = true;
debouncing_time = timer_read();
}
# else
read_cols_on_row(matrix, current_row);
# endif
}
#elif (DIODE_DIRECTION == ROW2COL) #elif (DIODE_DIRECTION == ROW2COL)
// Set col, read rows
// Set col, read rows for (uint8_t current_col = 0; current_col < MATRIX_COLS; current_col++) {
for (uint8_t current_col = 0; current_col < MATRIX_COLS; current_col++) { changed |= read_rows_on_col(raw_matrix, current_col);
# if (DEBOUNCING_DELAY > 0) }
bool matrix_changed = read_rows_on_col(matrix_debouncing, current_col);
if (matrix_changed) {
debouncing = true;
debouncing_time = timer_read();
}
# else
read_rows_on_col(matrix, current_col);
# endif
}
#endif #endif
# if (DEBOUNCING_DELAY > 0) debounce(raw_matrix, matrix, MATRIX_ROWS, changed);
if (debouncing && (timer_elapsed(debouncing_time) > DEBOUNCING_DELAY)) {
for (uint8_t i = 0; i < MATRIX_ROWS; i++) {
matrix[i] = matrix_debouncing[i];
}
debouncing = false;
}
# endif
matrix_scan_quantum(); matrix_scan_quantum();
return 1; return 1;
} }
bool matrix_is_modified(void) bool matrix_is_modified(void)
{ {
#if (DEBOUNCING_DELAY > 0) if (debounce_active()) return false;
if (debouncing) return false;
#endif
return true; return true;
} }

View File

@ -1,5 +1,4 @@
#ifndef I2C_H #pragma once
#define I2C_H
#include <stdint.h> #include <stdint.h>
@ -58,5 +57,3 @@ extern unsigned char i2c_readNak(void);
extern unsigned char i2c_read(unsigned char ack); extern unsigned char i2c_read(unsigned char ack);
#define i2c_read(ack) (ack) ? i2c_readAck() : i2c_readNak(); #define i2c_read(ack) (ack) ? i2c_readAck() : i2c_readNak();
#endif

File diff suppressed because it is too large Load Diff

View File

@ -1,31 +1,3 @@
#ifndef SPLIT_COMMON_MATRIX_H #pragma once
#define SPLIT_COMMON_MATRIX_H
#include <common/matrix.h> #include <common/matrix.h>
#ifdef RGBLIGHT_ENABLE
# include "rgblight.h"
#endif
typedef struct _Serial_m2s_buffer_t {
#ifdef BACKLIGHT_ENABLE
uint8_t backlight_level;
#endif
#if defined(RGBLIGHT_ENABLE) && defined(RGBLIGHT_SPLIT)
rgblight_config_t rgblight_config; //not yet use
//
// When MCUs on both sides drive their respective RGB LED chains,
// it is necessary to synchronize, so it is necessary to communicate RGB information.
// In that case, define the RGBLIGHT_SPLIT macro.
//
// Otherwise, if the master side MCU drives both sides RGB LED chains,
// there is no need to communicate.
#endif
} Serial_m2s_buffer_t;
extern volatile Serial_m2s_buffer_t serial_m2s_buffer;
void serial_master_init(void);
void serial_slave_init(void);
#endif

View File

@ -1,5 +1,4 @@
#ifndef SOFT_SERIAL_H #pragma once
#define SOFT_SERIAL_H
#include <stdbool.h> #include <stdbool.h>
@ -61,5 +60,3 @@ int soft_serial_transaction(int sstd_index);
#ifdef SERIAL_USE_MULTI_TRANSACTION #ifdef SERIAL_USE_MULTI_TRANSACTION
int soft_serial_get_and_clean_status(int sstd_index); int soft_serial_get_and_clean_status(int sstd_index);
#endif #endif
#endif /* SOFT_SERIAL_H */

View File

@ -1,10 +1,9 @@
#ifndef SPLIT_FLAGS_H #pragma once
#define SPLIT_FLAGS_H
#include <stdbool.h> #include <stdbool.h>
#include <stdint.h> #include <stdint.h>
/** /**
* Global Flags * Global Flags
**/ **/
@ -14,7 +13,3 @@ extern volatile bool RGB_DIRTY;
//Backlight Stuff //Backlight Stuff
extern volatile bool BACKLIT_DIRTY; extern volatile bool BACKLIT_DIRTY;
#endif

View File

@ -4,142 +4,84 @@
#include "config.h" #include "config.h"
#include "timer.h" #include "timer.h"
#include "split_flags.h" #include "split_flags.h"
#include "transport.h"
#include "quantum.h" #include "quantum.h"
#ifdef EE_HANDS #ifdef EE_HANDS
# include "tmk_core/common/eeprom.h" # include "tmk_core/common/eeprom.h"
#endif # include "eeconfig.h"
#ifdef BACKLIGHT_ENABLE
# include "backlight.h"
#endif
#if defined(USE_I2C) || defined(EH)
# include "i2c.h"
#endif #endif
volatile bool isLeftHand = true; volatile bool isLeftHand = true;
volatile uint8_t setTries = 0; __attribute__((weak))
bool is_keyboard_left(void) {
static void setup_handedness(void) {
#ifdef SPLIT_HAND_PIN #ifdef SPLIT_HAND_PIN
// Test pin SPLIT_HAND_PIN for High/Low, if low it's right hand // Test pin SPLIT_HAND_PIN for High/Low, if low it's right hand
setPinInput(SPLIT_HAND_PIN); setPinInput(SPLIT_HAND_PIN);
isLeftHand = readPin(SPLIT_HAND_PIN); return readPin(SPLIT_HAND_PIN);
#else #else
#ifdef EE_HANDS #ifdef EE_HANDS
isLeftHand = eeprom_read_byte(EECONFIG_HANDEDNESS); return eeprom_read_byte(EECONFIG_HANDEDNESS);
#else #else
#ifdef MASTER_RIGHT #ifdef MASTER_RIGHT
isLeftHand = !has_usb(); return !is_keyboard_master();
#else #else
isLeftHand = has_usb(); return is_keyboard_master();
#endif #endif
#endif #endif
#endif #endif
} }
bool is_keyboard_master(void)
{
#ifdef __AVR__
static enum { UNKNOWN, MASTER, SLAVE } usbstate = UNKNOWN;
// only check once, as this is called often
if (usbstate == UNKNOWN)
{
USBCON |= (1 << OTGPADE); // enables VBUS pad
wait_us(5);
usbstate = (USBSTA & (1 << VBUS)) ? MASTER : SLAVE; // checks state of VBUS
}
return (usbstate == MASTER);
#else
return true;
#endif
}
static void keyboard_master_setup(void) { static void keyboard_master_setup(void) {
#if defined(USE_I2C) || defined(EH) #if defined(USE_I2C) || defined(EH)
i2c_master_init();
#ifdef SSD1306OLED #ifdef SSD1306OLED
matrix_master_OLED_init (); matrix_master_OLED_init ();
#endif #endif
#else
serial_master_init();
#endif #endif
transport_master_init();
// For master the Backlight info needs to be sent on startup // For master the Backlight info needs to be sent on startup
// Otherwise the salve won't start with the proper info until an update // Otherwise the salve won't start with the proper info until an update
BACKLIT_DIRTY = true; BACKLIT_DIRTY = true;
} }
static void keyboard_slave_setup(void) { static void keyboard_slave_setup(void)
timer_init(); {
#if defined(USE_I2C) || defined(EH) transport_slave_init();
i2c_slave_init(SLAVE_I2C_ADDRESS);
#else
serial_slave_init();
#endif
}
bool has_usb(void) {
USBCON |= (1 << OTGPADE); //enables VBUS pad
_delay_us(5);
return (USBSTA & (1<<VBUS)); //checks state of VBUS
}
void split_keyboard_setup(void) {
setup_handedness();
if (has_usb()) {
keyboard_master_setup();
} else {
keyboard_slave_setup();
}
sei();
}
void keyboard_slave_loop(void) {
matrix_init();
//Init RGB
#ifdef RGBLIGHT_ENABLE
rgblight_init();
#endif
while (1) {
// Matrix Slave Scan
matrix_slave_scan();
// Read Backlight Info
#ifdef BACKLIGHT_ENABLE
#ifdef USE_I2C
if (BACKLIT_DIRTY) {
backlight_set(i2c_slave_buffer[I2C_BACKLIT_START]);
BACKLIT_DIRTY = false;
}
#else // USE_SERIAL
backlight_set(serial_m2s_buffer.backlight_level);
#endif
#endif
// Read RGB Info
#ifdef RGBLIGHT_ENABLE
#ifdef USE_I2C
if (RGB_DIRTY) {
// Disable interupts (RGB data is big)
cli();
// Create new DWORD for RGB data
uint32_t dword;
// Fill the new DWORD with the data that was sent over
uint8_t *dword_dat = (uint8_t *)(&dword);
for (int i = 0; i < 4; i++) {
dword_dat[i] = i2c_slave_buffer[I2C_RGB_START+i];
}
// Update the RGB now with the new data and set RGB_DIRTY to false
rgblight_update_dword(dword);
RGB_DIRTY = false;
// Re-enable interupts now that RGB is set
sei();
}
#else // USE_SERIAL
#ifdef RGBLIGHT_SPLIT
// Add serial implementation for RGB here
#endif
#endif
#endif
}
} }
// this code runs before the usb and keyboard is initialized // this code runs before the usb and keyboard is initialized
void matrix_setup(void) { void matrix_setup(void)
split_keyboard_setup(); {
isLeftHand = is_keyboard_left();
if (!has_usb()) { if (is_keyboard_master())
//rgblight_init(); {
keyboard_slave_loop(); keyboard_master_setup();
} }
else
{
keyboard_slave_setup();
}
} }

View File

@ -1,23 +1,10 @@
#ifndef SPLIT_KEYBOARD_UTIL_H #pragma once
#define SPLIT_KEYBOARD_UTIL_H
#include <stdbool.h> #include <stdbool.h>
#include <stdint.h> #include <stdint.h>
#include <stdio.h> #include <stdio.h>
#include <stdlib.h> #include <stdlib.h>
#include "eeconfig.h"
#define SLAVE_I2C_ADDRESS 0x32
extern volatile bool isLeftHand; extern volatile bool isLeftHand;
// slave version of matix scan, defined in matrix.c
void matrix_slave_scan(void);
void split_keyboard_setup(void);
bool has_usb(void);
void keyboard_slave_loop(void);
void matrix_master_OLED_init (void); void matrix_master_OLED_init (void);
#endif

View File

@ -0,0 +1,224 @@
#include "config.h"
#include "matrix.h"
#include "quantum.h"
#define ROWS_PER_HAND (MATRIX_ROWS/2)
#ifdef RGBLIGHT_ENABLE
# include "rgblight.h"
#endif
#ifdef BACKLIGHT_ENABLE
# include "backlight.h"
extern backlight_config_t backlight_config;
#endif
#if defined(USE_I2C) || defined(EH)
#include "i2c.h"
#ifndef SLAVE_I2C_ADDRESS
# define SLAVE_I2C_ADDRESS 0x32
#endif
#if (MATRIX_COLS > 8)
# error "Currently only supports 8 COLS"
#endif
// Get rows from other half over i2c
bool transport_master(matrix_row_t matrix[]) {
int err = 0;
// write backlight info
#ifdef BACKLIGHT_ENABLE
if (BACKLIT_DIRTY) {
err = i2c_master_start(SLAVE_I2C_ADDRESS + I2C_WRITE);
if (err) { goto i2c_error; }
// Backlight location
err = i2c_master_write(I2C_BACKLIT_START);
if (err) { goto i2c_error; }
// Write backlight
i2c_master_write(get_backlight_level());
BACKLIT_DIRTY = false;
}
#endif
err = i2c_master_start(SLAVE_I2C_ADDRESS + I2C_WRITE);
if (err) { goto i2c_error; }
// start of matrix stored at I2C_KEYMAP_START
err = i2c_master_write(I2C_KEYMAP_START);
if (err) { goto i2c_error; }
// Start read
err = i2c_master_start(SLAVE_I2C_ADDRESS + I2C_READ);
if (err) { goto i2c_error; }
if (!err) {
int i;
for (i = 0; i < ROWS_PER_HAND-1; ++i) {
matrix[i] = i2c_master_read(I2C_ACK);
}
matrix[i] = i2c_master_read(I2C_NACK);
i2c_master_stop();
} else {
i2c_error: // the cable is disconnceted, or something else went wrong
i2c_reset_state();
return false;
}
#ifdef RGBLIGHT_ENABLE
if (RGB_DIRTY) {
err = i2c_master_start(SLAVE_I2C_ADDRESS + I2C_WRITE);
if (err) { goto i2c_error; }
// RGB Location
err = i2c_master_write(I2C_RGB_START);
if (err) { goto i2c_error; }
uint32_t dword = eeconfig_read_rgblight();
// Write RGB
err = i2c_master_write_data(&dword, 4);
if (err) { goto i2c_error; }
RGB_DIRTY = false;
i2c_master_stop();
}
#endif
return true;
}
void transport_slave(matrix_row_t matrix[]) {
for (int i = 0; i < ROWS_PER_HAND; ++i)
{
i2c_slave_buffer[I2C_KEYMAP_START + i] = matrix[i];
}
// Read Backlight Info
#ifdef BACKLIGHT_ENABLE
if (BACKLIT_DIRTY)
{
backlight_set(i2c_slave_buffer[I2C_BACKLIT_START]);
BACKLIT_DIRTY = false;
}
#endif
#ifdef RGBLIGHT_ENABLE
if (RGB_DIRTY)
{
// Disable interupts (RGB data is big)
cli();
// Create new DWORD for RGB data
uint32_t dword;
// Fill the new DWORD with the data that was sent over
uint8_t * dword_dat = (uint8_t *)(&dword);
for (int i = 0; i < 4; i++)
{
dword_dat[i] = i2c_slave_buffer[I2C_RGB_START + i];
}
// Update the RGB now with the new data and set RGB_DIRTY to false
rgblight_update_dword(dword);
RGB_DIRTY = false;
// Re-enable interupts now that RGB is set
sei();
}
#endif
}
void transport_master_init(void) {
i2c_master_init();
}
void transport_slave_init(void) {
i2c_slave_init(SLAVE_I2C_ADDRESS);
}
#else // USE_SERIAL
#include "serial.h"
typedef struct _Serial_s2m_buffer_t {
// TODO: if MATRIX_COLS > 8 change to uint8_t packed_matrix[] for pack/unpack
matrix_row_t smatrix[ROWS_PER_HAND];
} Serial_s2m_buffer_t;
typedef struct _Serial_m2s_buffer_t {
#ifdef BACKLIGHT_ENABLE
uint8_t backlight_level;
#endif
#if defined(RGBLIGHT_ENABLE) && defined(RGBLIGHT_SPLIT)
rgblight_config_t rgblight_config; //not yet use
//
// When MCUs on both sides drive their respective RGB LED chains,
// it is necessary to synchronize, so it is necessary to communicate RGB information.
// In that case, define the RGBLIGHT_SPLIT macro.
//
// Otherwise, if the master side MCU drives both sides RGB LED chains,
// there is no need to communicate.
#endif
} Serial_m2s_buffer_t;
volatile Serial_s2m_buffer_t serial_s2m_buffer = {};
volatile Serial_m2s_buffer_t serial_m2s_buffer = {};
uint8_t volatile status0 = 0;
SSTD_t transactions[] = {
{ (uint8_t *)&status0,
sizeof(serial_m2s_buffer), (uint8_t *)&serial_m2s_buffer,
sizeof(serial_s2m_buffer), (uint8_t *)&serial_s2m_buffer
}
};
void transport_master_init(void)
{ soft_serial_initiator_init(transactions, TID_LIMIT(transactions)); }
void transport_slave_init(void)
{ soft_serial_target_init(transactions, TID_LIMIT(transactions)); }
bool transport_master(matrix_row_t matrix[]) {
if (soft_serial_transaction()) {
return false;
}
// TODO: if MATRIX_COLS > 8 change to unpack()
for (int i = 0; i < ROWS_PER_HAND; ++i) {
matrix[i] = serial_s2m_buffer.smatrix[i];
}
#if defined(RGBLIGHT_ENABLE) && defined(RGBLIGHT_SPLIT)
// Code to send RGB over serial goes here (not implemented yet)
#endif
#ifdef BACKLIGHT_ENABLE
// Write backlight level for slave to read
serial_m2s_buffer.backlight_level = backlight_config.enable ? backlight_config.level : 0;
#endif
return true;
}
void transport_slave(matrix_row_t matrix[]) {
// TODO: if MATRIX_COLS > 8 change to pack()
for (int i = 0; i < ROWS_PER_HAND; ++i)
{
serial_s2m_buffer.smatrix[i] = matrix[i];
}
#ifdef BACKLIGHT_ENABLE
backlight_set(serial_m2s_buffer.backlight_level);
#endif
#if defined(RGBLIGHT_ENABLE) && defined(RGBLIGHT_SPLIT)
// Add serial implementation for RGB here
#endif
}
#endif

View File

@ -0,0 +1,10 @@
#pragma once
#include <common/matrix.h>
void transport_master_init(void);
void transport_slave_init(void);
// returns false if valid data not received from slave
bool transport_master(matrix_row_t matrix[]);
void transport_slave(matrix_row_t matrix[]);

View File

@ -67,6 +67,8 @@ void keyboard_init(void);
void keyboard_task(void); void keyboard_task(void);
/* it runs when host LED status is updated */ /* it runs when host LED status is updated */
void keyboard_set_leds(uint8_t leds); void keyboard_set_leds(uint8_t leds);
/* it runs whenever code has to behave differently on a slave */
bool is_keyboard_master(void);
#ifdef __cplusplus #ifdef __cplusplus
} }