diff --git a/keyboards/system76/launch_beta_1/i2c.c b/keyboards/system76/launch_beta_1/i2c.c index 28fc9813fd8..a46927fa59e 100644 --- a/keyboards/system76/launch_beta_1/i2c.c +++ b/keyboards/system76/launch_beta_1/i2c.c @@ -2,6 +2,8 @@ #include #include +#include "i2c.h" + #define TIMEOUT (F_CPU/1000) // Initialize I2C with specified buad rate @@ -140,6 +142,8 @@ int i2c_send(uint8_t addr, uint8_t* data, int length) { return res; } +// Get data from a specified address and register on the I2C bus +// Returns bytes read on success or negative number on error int i2c_get(uint8_t addr, uint8_t reg, uint8_t* data, int length) { int res = 0; @@ -152,6 +156,8 @@ int i2c_get(uint8_t addr, uint8_t reg, uint8_t* data, int length) { return i2c_recv(addr, data, length); } +// Set data in a specified address and register on the I2C bus +// Returns bytes written on success or negative number on error int i2c_set(uint8_t addr, uint8_t reg, uint8_t* data, int length) { int res = 0; diff --git a/keyboards/system76/launch_beta_1/i2c.h b/keyboards/system76/launch_beta_1/i2c.h new file mode 100644 index 00000000000..f980a308dce --- /dev/null +++ b/keyboards/system76/launch_beta_1/i2c.h @@ -0,0 +1,46 @@ +#ifndef I2C_H +#define I2C_H + +#include +#include + +// Initialize I2C with specified buad rate +void i2c_init(unsigned long baud); + +// Send an I2C start condition, a 7-bit address, and a read bit +// Returns zero on success or negative number on error +int i2c_start(uint8_t addr, bool read); + +// Send an I2C stop condition +// Always successful +void i2c_stop(void); + +// Write data to the I2C bus +// Returns bytes written on success or negative number on error +int i2c_write(uint8_t * data, int length); + +// Read a byte from the I2C bus, sending an ack if specified +// Returns byte data on success or negative number on error +int i2c_read_byte(bool ack); + +// Read data from the I2C bus +// Returns bytes read on success or negative number on error +int i2c_read(uint8_t * data, int length); + +// Receive data from a specified address on the I2C bus +// Returns bytes read on success or negative number on error +int i2c_recv(uint8_t addr, uint8_t* data, int length); + +// Send data to a specified address on the I2C bus +// Returns bytes written on success or negative number on error +int i2c_send(uint8_t addr, uint8_t* data, int length); + +// Get data from a specified address and register on the I2C bus +// Returns bytes read on success or negative number on error +int i2c_get(uint8_t addr, uint8_t reg, uint8_t* data, int length); + +// Set data in a specified address and register on the I2C bus +// Returns bytes written on success or negative number on error +int i2c_set(uint8_t addr, uint8_t reg, uint8_t* data, int length); + +#endif // I2C_H diff --git a/keyboards/system76/launch_beta_1/launch_beta_1.c b/keyboards/system76/launch_beta_1/launch_beta_1.c index 5bd8f82c759..fef5a6abe25 100644 --- a/keyboards/system76/launch_beta_1/launch_beta_1.c +++ b/keyboards/system76/launch_beta_1/launch_beta_1.c @@ -1,94 +1,8 @@ #include "dynamic_keymap.h" -#include "raw_hid.h" #include "tmk_core/common/eeprom.h" -#include "version.h" #include "launch_beta_1.h" - -//TODO: refactor -#include "usb_mux.c" - -enum Command { - // Probe for System76 EC protocol - CMD_PROBE = 1, - // Read board string - CMD_BOARD = 2, - // Read version string - CMD_VERSION = 3, - // Get keyboard map index - CMD_KEYMAP_GET = 9, - // Set keyboard map index - CMD_KEYMAP_SET = 10, -}; - -static bool keymap_get(uint8_t layer, uint8_t output, uint8_t input, uint16_t *value) { - if (layer < dynamic_keymap_get_layer_count()) { - if (output < MATRIX_ROWS) { - if (input < MATRIX_COLS) { - *value = dynamic_keymap_get_keycode(layer, output, input); - return true; - } - } - } - return false; -} - -static bool keymap_set(uint8_t layer, uint8_t output, uint8_t input, uint16_t value) { - if (layer < dynamic_keymap_get_layer_count()) { - if (output < MATRIX_ROWS) { - if (input < MATRIX_COLS) { - dynamic_keymap_set_keycode(layer, output, input, value); - return true; - } - } - } - return false; -} - -void raw_hid_receive(uint8_t *data, uint8_t length) { - // Error response by default, set to success by commands - data[1] = 1; - - switch (data[0]) { - case CMD_PROBE: - // Signature - data[2] = 0x76; - data[3] = 0xEC; - // Version - data[4] = 0x01; - data[1] = 0; - break; - case CMD_BOARD: - strncpy((char *)&data[2], QMK_KEYBOARD, length - 2); - data[1] = 0; - break; - case CMD_VERSION: - strncpy((char *)&data[2], QMK_VERSION, length - 2); - data[1] = 0; - break; - case CMD_KEYMAP_GET: - { - uint16_t value = 0; - if (keymap_get(data[2], data[3], data[4], &value)) { - data[5] = (uint8_t)value; - data[6] = (uint8_t)(value >> 8); - data[1] = 0; - } - } - break; - case CMD_KEYMAP_SET: - { - uint16_t value = - ((uint16_t)data[5]) | - (((uint16_t)data[6]) << 8); - if (keymap_set(data[2], data[3], data[4], value)) { - data[1] = 0; - } - } - } - - raw_hid_send(data, length); -} +#include "usb_mux.h" bool eeprom_is_valid(void) { return (eeprom_read_word(((void*)EEPROM_MAGIC_ADDR)) == EEPROM_MAGIC && diff --git a/keyboards/system76/launch_beta_1/rules.mk b/keyboards/system76/launch_beta_1/rules.mk index a532ea4821f..fe4a1ac9f7b 100644 --- a/keyboards/system76/launch_beta_1/rules.mk +++ b/keyboards/system76/launch_beta_1/rules.mk @@ -29,3 +29,12 @@ DYNAMIC_KEYMAP_ENABLE = yes # Reconfigurable keyboard without flashing firmware NKRO_ENABLE = yes # USB Nkey Rollover - if this doesn't work, see here: https://github.com/tmk/tmk_keyboard/wiki/FAQ#nkro-doesnt-work RAW_ENABLE = yes # Enable RAW HID commands (used by keyboard configurator) RGBLIGHT_ENABLE = yes # Support for RGB backlight + +# Add System76 EC command interface +SRC+=system76_ec.c + +# Add I2C driver +SRC+=i2c.c + +# Add USB mux driver +SRC+=usb_mux.c diff --git a/keyboards/system76/launch_beta_1/system76_ec.c b/keyboards/system76/launch_beta_1/system76_ec.c new file mode 100644 index 00000000000..e9c2949dba0 --- /dev/null +++ b/keyboards/system76/launch_beta_1/system76_ec.c @@ -0,0 +1,87 @@ +#include + +#include "dynamic_keymap.h" +#include "raw_hid.h" +#include "version.h" + +enum Command { + // Probe for System76 EC protocol + CMD_PROBE = 1, + // Read board string + CMD_BOARD = 2, + // Read version string + CMD_VERSION = 3, + // Get keyboard map index + CMD_KEYMAP_GET = 9, + // Set keyboard map index + CMD_KEYMAP_SET = 10, +}; + +static bool keymap_get(uint8_t layer, uint8_t output, uint8_t input, uint16_t *value) { + if (layer < dynamic_keymap_get_layer_count()) { + if (output < MATRIX_ROWS) { + if (input < MATRIX_COLS) { + *value = dynamic_keymap_get_keycode(layer, output, input); + return true; + } + } + } + return false; +} + +static bool keymap_set(uint8_t layer, uint8_t output, uint8_t input, uint16_t value) { + if (layer < dynamic_keymap_get_layer_count()) { + if (output < MATRIX_ROWS) { + if (input < MATRIX_COLS) { + dynamic_keymap_set_keycode(layer, output, input, value); + return true; + } + } + } + return false; +} + +void raw_hid_receive(uint8_t *data, uint8_t length) { + // Error response by default, set to success by commands + data[1] = 1; + + switch (data[0]) { + case CMD_PROBE: + // Signature + data[2] = 0x76; + data[3] = 0xEC; + // Version + data[4] = 0x01; + data[1] = 0; + break; + case CMD_BOARD: + strncpy((char *)&data[2], QMK_KEYBOARD, length - 2); + data[1] = 0; + break; + case CMD_VERSION: + strncpy((char *)&data[2], QMK_VERSION, length - 2); + data[1] = 0; + break; + case CMD_KEYMAP_GET: + { + uint16_t value = 0; + if (keymap_get(data[2], data[3], data[4], &value)) { + data[5] = (uint8_t)value; + data[6] = (uint8_t)(value >> 8); + data[1] = 0; + } + } + break; + case CMD_KEYMAP_SET: + { + uint16_t value = + ((uint16_t)data[5]) | + (((uint16_t)data[6]) << 8); + if (keymap_set(data[2], data[3], data[4], value)) { + data[1] = 0; + } + } + } + + raw_hid_send(data, length); +} diff --git a/keyboards/system76/launch_beta_1/usb_mux.c b/keyboards/system76/launch_beta_1/usb_mux.c index b2404cac1dc..65243ba790e 100644 --- a/keyboards/system76/launch_beta_1/usb_mux.c +++ b/keyboards/system76/launch_beta_1/usb_mux.c @@ -1,4 +1,5 @@ -#include "i2c.c" +#include "i2c.h" +#include "usb_mux.h" #define PRT_SWAP 0xBF8030FA #define I2S_FEAT_SEL 0xBFD23412 diff --git a/keyboards/system76/launch_beta_1/usb_mux.h b/keyboards/system76/launch_beta_1/usb_mux.h new file mode 100644 index 00000000000..7268125dee9 --- /dev/null +++ b/keyboards/system76/launch_beta_1/usb_mux.h @@ -0,0 +1,7 @@ +#ifndef USB_MUX_H +#define USB_MUX_H + +void usb_mux_init(void); +void usb_mux_event(void); + +#endif // USB_MUX_H