diff --git a/keyboards/lets_split/imgs/split-keyboard-i2c-schematic.png b/keyboards/lets_split/imgs/split-keyboard-i2c-schematic.png new file mode 100644 index 000000000..888294718 Binary files /dev/null and b/keyboards/lets_split/imgs/split-keyboard-i2c-schematic.png differ diff --git a/keyboards/lets_split/imgs/split-keyboard-serial-schematic.png b/keyboards/lets_split/imgs/split-keyboard-serial-schematic.png new file mode 100644 index 000000000..7621d38ed Binary files /dev/null and b/keyboards/lets_split/imgs/split-keyboard-serial-schematic.png differ diff --git a/keyboards/lets_split/matrix.c b/keyboards/lets_split/matrix.c index 16c2ba0ba..1d768c59b 100644 --- a/keyboards/lets_split/matrix.c +++ b/keyboards/lets_split/matrix.c @@ -29,6 +29,7 @@ along with this program. If not, see . #include "util.h" #include "matrix.h" #include "i2c.h" +#include "serial.h" #include "split_util.h" #include "pro_micro.h" #include "config.h" diff --git a/keyboards/lets_split/readme.md b/keyboards/lets_split/readme.md index e69de29bb..73fdb0f78 100644 --- a/keyboards/lets_split/readme.md +++ b/keyboards/lets_split/readme.md @@ -0,0 +1,102 @@ +Let's Split +====== + +This readme and most of the code are from https://github.com/ahtn/tmk_keyboard/ + +Split keyboard firmware for Arduino Pro Micro or other ATmega32u4 +based boards. + +Features +-------- + +Some features supported by the firmware: + +* Either half can connect to the computer via USB, or both halves can be used + independently. +* You only need 3 wires to connect the two halves. Two for VCC and GND and one + for serial communication. +* Optional support for I2C connection between the two halves if for some + reason you require a faster connection between the two halves. Note this + requires an extra wire between halves and pull-up resistors on the data lines. + +Required Hardware +----------------- + +Apart from diodes and key switches for the keyboard matrix in each half, you +will need: + +* 2 Arduino Pro Micro's. You can find theses on aliexpress for ≈3.50USD each. +* 2 TRS sockets +* 1 TRS cable. + +Alternatively, you can use any sort of cable and socket that has at least 3 +wires. If you want to use I2C to communicate between halves, you will need a +cable with at least 4 wires and 2x 4.7kΩ pull-up resistors + +Optional Hardware +----------------- + +A speaker can be hooked-up to either side to the `5` (`C6`) pin and `GND`, and turned on via `AUDIO_ENABLE`. + +Wiring +------ + +The 3 wires of the TRS cable need to connect GND, VCC, and digital pin 3 (i.e. +PD0 on the ATmega32u4) between the two Pro Micros. + +Then wire your key matrix to any of the remaining 17 IO pins of the pro micro +and modify the `matrix.c` accordingly. + +The wiring for serial: + +![serial wiring](imgs/split-keyboard-serial-schematic.png) + +The wiring for i2c: + +![i2c wiring](imgs/split-keyboard-i2c-schematic.png) + +The pull-up resistors may be placed on either half. It is also possible +to use 4 resistors and have the pull-ups in both halves, but this is +unnecessary in simple use cases. + +Notes on Software Configuration +------------------------------- + +Configuring the firmware is similar to any other TMK project. One thing +to note is that `MATIX_ROWS` in `config.h` is the total number of rows between +the two halves, i.e. if your split keyboard has 4 rows in each half, then +`MATRIX_ROWS=8`. + +Also the current implementation assumes a maximum of 8 columns, but it would +not be very difficult to adapt it to support more if required. + + +Flashing +-------- + +If you define `EE_HANDS` in your `config.h`, you will need to set the +EEPROM for the left and right halves. The EEPROM is used to store whether the +half is left handed or right handed. This makes it so that the same firmware +file will run on both hands instead of having to flash left and right handed +versions of the firmware to each half. To flash the EEPROM file for the left +half run: +``` +make eeprom-left +``` +and similarly for right half +``` +make eeprom-right +``` + +After you have flashed the EEPROM for the first time, you then need to program +the flash memory: +``` +make program +``` +Note that you need to program both halves, but you have the option of using +different keymaps for each half. You could program the left half with a QWERTY +layout and the right half with a Colemak layout. Then if you connect the left +half to a computer by USB the keyboard will use QWERTY and Colemak when the +right half is connected. + + diff --git a/keyboards/lets_split/serial.c b/keyboards/lets_split/serial.c new file mode 100644 index 000000000..f439c2f20 --- /dev/null +++ b/keyboards/lets_split/serial.c @@ -0,0 +1,225 @@ +/* + * WARNING: be careful changing this code, it is very timing dependent + */ + +#ifndef F_CPU +#define F_CPU 16000000 +#endif + +#include +#include +#include +#include + +#include "serial.h" + +// Serial pulse period in microseconds. Its probably a bad idea to lower this +// value. +#define SERIAL_DELAY 24 + +uint8_t volatile serial_slave_buffer[SERIAL_SLAVE_BUFFER_LENGTH] = {0}; +uint8_t volatile serial_master_buffer[SERIAL_MASTER_BUFFER_LENGTH] = {0}; + +#define SLAVE_DATA_CORRUPT (1<<0) +volatile uint8_t status = 0; + +inline static +void serial_delay(void) { + _delay_us(SERIAL_DELAY); +} + +inline static +void serial_output(void) { + SERIAL_PIN_DDR |= SERIAL_PIN_MASK; +} + +// make the serial pin an input with pull-up resistor +inline static +void serial_input(void) { + SERIAL_PIN_DDR &= ~SERIAL_PIN_MASK; + SERIAL_PIN_PORT |= SERIAL_PIN_MASK; +} + +inline static +uint8_t serial_read_pin(void) { + return !!(SERIAL_PIN_INPUT & SERIAL_PIN_MASK); +} + +inline static +void serial_low(void) { + SERIAL_PIN_PORT &= ~SERIAL_PIN_MASK; +} + +inline static +void serial_high(void) { + SERIAL_PIN_PORT |= SERIAL_PIN_MASK; +} + +void serial_master_init(void) { + serial_output(); + serial_high(); +} + +void serial_slave_init(void) { + serial_input(); + + // Enable INT0 + EIMSK |= _BV(INT0); + // Trigger on falling edge of INT0 + EICRA &= ~(_BV(ISC00) | _BV(ISC01)); +} + +// Used by the master to synchronize timing with the slave. +static +void sync_recv(void) { + serial_input(); + // This shouldn't hang if the slave disconnects because the + // serial line will float to high if the slave does disconnect. + while (!serial_read_pin()); + serial_delay(); +} + +// Used by the slave to send a synchronization signal to the master. +static +void sync_send(void) { + serial_output(); + + serial_low(); + serial_delay(); + + serial_high(); +} + +// Reads a byte from the serial line +static +uint8_t serial_read_byte(void) { + uint8_t byte = 0; + serial_input(); + for ( uint8_t i = 0; i < 8; ++i) { + byte = (byte << 1) | serial_read_pin(); + serial_delay(); + _delay_us(1); + } + + return byte; +} + +// Sends a byte with MSB ordering +static +void serial_write_byte(uint8_t data) { + uint8_t b = 8; + serial_output(); + while( b-- ) { + if(data & (1 << b)) { + serial_high(); + } else { + serial_low(); + } + serial_delay(); + } +} + +// interrupt handle to be used by the slave device +ISR(SERIAL_PIN_INTERRUPT) { + sync_send(); + + uint8_t checksum = 0; + for (int i = 0; i < SERIAL_SLAVE_BUFFER_LENGTH; ++i) { + serial_write_byte(serial_slave_buffer[i]); + sync_send(); + checksum += serial_slave_buffer[i]; + } + serial_write_byte(checksum); + sync_send(); + + // wait for the sync to finish sending + serial_delay(); + + // read the middle of pulses + _delay_us(SERIAL_DELAY/2); + + uint8_t checksum_computed = 0; + for (int i = 0; i < SERIAL_MASTER_BUFFER_LENGTH; ++i) { + serial_master_buffer[i] = serial_read_byte(); + sync_send(); + checksum_computed += serial_master_buffer[i]; + } + uint8_t checksum_received = serial_read_byte(); + sync_send(); + + serial_input(); // end transaction + + if ( checksum_computed != checksum_received ) { + status |= SLAVE_DATA_CORRUPT; + } else { + status &= ~SLAVE_DATA_CORRUPT; + } +} + +inline +bool serial_slave_DATA_CORRUPT(void) { + return status & SLAVE_DATA_CORRUPT; +} + +// Copies the serial_slave_buffer to the master and sends the +// serial_master_buffer to the slave. +// +// Returns: +// 0 => no error +// 1 => slave did not respond +int serial_update_buffers(void) { + // this code is very time dependent, so we need to disable interrupts + cli(); + + // signal to the slave that we want to start a transaction + serial_output(); + serial_low(); + _delay_us(1); + + // wait for the slaves response + serial_input(); + serial_high(); + _delay_us(SERIAL_DELAY); + + // check if the slave is present + if (serial_read_pin()) { + // slave failed to pull the line low, assume not present + sei(); + return 1; + } + + // if the slave is present syncronize with it + sync_recv(); + + uint8_t checksum_computed = 0; + // receive data from the slave + for (int i = 0; i < SERIAL_SLAVE_BUFFER_LENGTH; ++i) { + serial_slave_buffer[i] = serial_read_byte(); + sync_recv(); + checksum_computed += serial_slave_buffer[i]; + } + uint8_t checksum_received = serial_read_byte(); + sync_recv(); + + if (checksum_computed != checksum_received) { + sei(); + return 1; + } + + uint8_t checksum = 0; + // send data to the slave + for (int i = 0; i < SERIAL_MASTER_BUFFER_LENGTH; ++i) { + serial_write_byte(serial_master_buffer[i]); + sync_recv(); + checksum += serial_master_buffer[i]; + } + serial_write_byte(checksum); + sync_recv(); + + // always, release the line when not in use + serial_output(); + serial_high(); + + sei(); + return 0; +} diff --git a/keyboards/lets_split/serial.h b/keyboards/lets_split/serial.h new file mode 100644 index 000000000..15fe4db7b --- /dev/null +++ b/keyboards/lets_split/serial.h @@ -0,0 +1,26 @@ +#ifndef MY_SERIAL_H +#define MY_SERIAL_H + +#include "config.h" +#include + +/* TODO: some defines for interrupt setup */ +#define SERIAL_PIN_DDR DDRD +#define SERIAL_PIN_PORT PORTD +#define SERIAL_PIN_INPUT PIND +#define SERIAL_PIN_MASK _BV(PD0) +#define SERIAL_PIN_INTERRUPT INT0_vect + +#define SERIAL_SLAVE_BUFFER_LENGTH MATRIX_ROWS/2 +#define SERIAL_MASTER_BUFFER_LENGTH 1 + +// Buffers for master - slave communication +extern volatile uint8_t serial_slave_buffer[SERIAL_SLAVE_BUFFER_LENGTH]; +extern volatile uint8_t serial_master_buffer[SERIAL_MASTER_BUFFER_LENGTH]; + +void serial_master_init(void); +void serial_slave_init(void); +int serial_update_buffers(void); +bool serial_slave_data_corrupt(void); + +#endif diff --git a/keyboards/lets_split/split_util.c b/keyboards/lets_split/split_util.c index c394596e0..65003a71a 100644 --- a/keyboards/lets_split/split_util.c +++ b/keyboards/lets_split/split_util.c @@ -7,13 +7,22 @@ #include "split_util.h" #include "matrix.h" #include "i2c.h" +#include "serial.h" #include "keyboard.h" #include "config.h" volatile bool isLeftHand = true; static void setup_handedness(void) { + #ifdef EE_HANDS isLeftHand = eeprom_read_byte(EECONFIG_HANDEDNESS); + #else + #ifdef I2C_MASTER_RIGHT + isLeftHand = !has_usb(); + #else + isLeftHand = has_usb(); + #endif + #endif } static void keyboard_master_setup(void) { diff --git a/keyboards/lets_split/split_util.h b/keyboards/lets_split/split_util.h index cf6890d37..6b896679c 100644 --- a/keyboards/lets_split/split_util.h +++ b/keyboards/lets_split/split_util.h @@ -3,8 +3,10 @@ #include -#define EECONFIG_BOOTMAGIC_END (uint8_t *)10 -#define EECONFIG_HANDEDNESS EECONFIG_BOOTMAGIC_END +#ifdef EE_HANDS + #define EECONFIG_BOOTMAGIC_END (uint8_t *)10 + #define EECONFIG_HANDEDNESS EECONFIG_BOOTMAGIC_END +#endif #define SLAVE_I2C_ADDRESS 0x32 diff --git a/keyboards/lets_split/uno-slave/Makefile b/keyboards/lets_split/uno_slave/Makefile similarity index 100% rename from keyboards/lets_split/uno-slave/Makefile rename to keyboards/lets_split/uno_slave/Makefile diff --git a/keyboards/lets_split/uno-slave/keyboard-i2c-slave.c b/keyboards/lets_split/uno_slave/keyboard-i2c-slave.c similarity index 100% rename from keyboards/lets_split/uno-slave/keyboard-i2c-slave.c rename to keyboards/lets_split/uno_slave/keyboard-i2c-slave.c diff --git a/keyboards/lets_split/uno-slave/readme.md b/keyboards/lets_split/uno_slave/readme.md similarity index 100% rename from keyboards/lets_split/uno-slave/readme.md rename to keyboards/lets_split/uno_slave/readme.md diff --git a/keyboards/lets_split/uno-slave/uno-matrix.c b/keyboards/lets_split/uno_slave/uno-matrix.c similarity index 100% rename from keyboards/lets_split/uno-slave/uno-matrix.c rename to keyboards/lets_split/uno_slave/uno-matrix.c diff --git a/keyboards/lets_split/uno-slave/uno-matrix.h b/keyboards/lets_split/uno_slave/uno-matrix.h similarity index 100% rename from keyboards/lets_split/uno-slave/uno-matrix.h rename to keyboards/lets_split/uno_slave/uno-matrix.h