This commit is contained in:
Jeremy Soller 2020-10-21 13:59:03 -06:00
parent 6397b6b8b8
commit 00ea663a1a
No known key found for this signature in database
GPG Key ID: E988B49EE78A7FB1
7 changed files with 158 additions and 88 deletions

View File

@ -2,6 +2,8 @@
#include <avr/io.h>
#include <util/twi.h>
#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;

View File

@ -0,0 +1,46 @@
#ifndef I2C_H
#define I2C_H
#include <stdbool.h>
#include <stdint.h>
// 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

View File

@ -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 &&

View File

@ -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

View File

@ -0,0 +1,87 @@
#include <string.h>
#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);
}

View File

@ -1,4 +1,5 @@
#include "i2c.c"
#include "i2c.h"
#include "usb_mux.h"
#define PRT_SWAP 0xBF8030FA
#define I2S_FEAT_SEL 0xBFD23412

View File

@ -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