Make bidirectional communication work between the keyboard halves. Make the set test LED USB command set the test LED of the left half, too.
This commit is contained in:
40
left/src/bridge_protocol_handler.c
Normal file
40
left/src/bridge_protocol_handler.c
Normal file
@@ -0,0 +1,40 @@
|
|||||||
|
#include "bridge_protocol_handler.h"
|
||||||
|
#include "test_led.h"
|
||||||
|
#include "main.h"
|
||||||
|
#include "i2c_addresses.h"
|
||||||
|
#include "i2c.h"
|
||||||
|
|
||||||
|
void SetError(uint8_t error);
|
||||||
|
void SetGenericError();
|
||||||
|
void SetResponseByte(uint8_t response);
|
||||||
|
|
||||||
|
void SetError(uint8_t error) {
|
||||||
|
BridgeTxBuffer[0] = error;
|
||||||
|
}
|
||||||
|
|
||||||
|
void SetGenericError()
|
||||||
|
{
|
||||||
|
SetError(PROTOCOL_RESPONSE_GENERIC_ERROR);
|
||||||
|
}
|
||||||
|
|
||||||
|
// Set a single byte as the response.
|
||||||
|
void SetResponseByte(uint8_t response)
|
||||||
|
{
|
||||||
|
BridgeTxBuffer[1] = response;
|
||||||
|
}
|
||||||
|
|
||||||
|
void BridgeProtocolHandler()
|
||||||
|
{
|
||||||
|
uint8_t commandId = BridgeRxBuffer[0];
|
||||||
|
switch (commandId) {
|
||||||
|
case BRIDGE_COMMAND_GET_KEY_STATES:
|
||||||
|
BridgeTxSize = KEYBOARD_MATRIX_COLS_NUM*KEYBOARD_MATRIX_ROWS_NUM;
|
||||||
|
memcpy(BridgeTxBuffer, keyMatrix.keyStates, BridgeTxSize);
|
||||||
|
break;
|
||||||
|
case BRIDGE_COMMAND_SET_LED:
|
||||||
|
TEST_LED_OFF();
|
||||||
|
BridgeTxSize = 0;
|
||||||
|
TEST_LED_SET(BridgeRxBuffer[1]);
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
29
left/src/bridge_protocol_handler.h
Normal file
29
left/src/bridge_protocol_handler.h
Normal file
@@ -0,0 +1,29 @@
|
|||||||
|
#ifndef __BRIDGE_PROTOCOL_HANDLER__
|
||||||
|
#define __BRIDGE_PROTOCOL_HANDLER__
|
||||||
|
|
||||||
|
// Includes:
|
||||||
|
|
||||||
|
#include "fsl_port.h"
|
||||||
|
|
||||||
|
// Macros:
|
||||||
|
|
||||||
|
#define BRIDGE_RX_BUFFER_SIZE 100
|
||||||
|
#define BRIDGE_TX_BUFFER_SIZE 100
|
||||||
|
|
||||||
|
#define PROTOCOL_RESPONSE_SUCCESS 0
|
||||||
|
#define PROTOCOL_RESPONSE_GENERIC_ERROR 1
|
||||||
|
|
||||||
|
#define BRIDGE_COMMAND_GET_KEY_STATES 0
|
||||||
|
#define BRIDGE_COMMAND_SET_LED 1
|
||||||
|
|
||||||
|
// Variables:
|
||||||
|
|
||||||
|
uint8_t BridgeRxBuffer[BRIDGE_RX_BUFFER_SIZE];
|
||||||
|
uint8_t BridgeTxBuffer[BRIDGE_TX_BUFFER_SIZE];
|
||||||
|
uint8_t BridgeTxSize;
|
||||||
|
|
||||||
|
// Functions:
|
||||||
|
|
||||||
|
extern void BridgeProtocolHandler();
|
||||||
|
|
||||||
|
#endif
|
||||||
@@ -2,14 +2,13 @@
|
|||||||
#include "init_clock.h"
|
#include "init_clock.h"
|
||||||
#include "fsl_port.h"
|
#include "fsl_port.h"
|
||||||
#include "fsl_i2c.h"
|
#include "fsl_i2c.h"
|
||||||
|
#include "main.h"
|
||||||
#include "key_matrix.h"
|
#include "key_matrix.h"
|
||||||
#include "test_led.h"
|
#include "test_led.h"
|
||||||
#include "i2c_addresses.h"
|
#include "i2c_addresses.h"
|
||||||
#include "i2c.h"
|
#include "i2c.h"
|
||||||
#include "init_peripherials.h"
|
#include "init_peripherials.h"
|
||||||
|
#include "bridge_protocol_handler.h"
|
||||||
#define KEYBOARD_MATRIX_COLS_NUM 7
|
|
||||||
#define KEYBOARD_MATRIX_ROWS_NUM 5
|
|
||||||
|
|
||||||
key_matrix_t keyMatrix = {
|
key_matrix_t keyMatrix = {
|
||||||
.colNum = KEYBOARD_MATRIX_COLS_NUM,
|
.colNum = KEYBOARD_MATRIX_COLS_NUM,
|
||||||
@@ -40,12 +39,18 @@ static void i2c_slave_callback(I2C_Type *base, i2c_slave_transfer_t *xfer, void
|
|||||||
switch (xfer->event)
|
switch (xfer->event)
|
||||||
{
|
{
|
||||||
case kI2C_SlaveTransmitEvent:
|
case kI2C_SlaveTransmitEvent:
|
||||||
xfer->data = keyMatrix.keyStates;
|
BridgeProtocolHandler();
|
||||||
xfer->dataSize = KEYBOARD_MATRIX_COLS_NUM*KEYBOARD_MATRIX_ROWS_NUM;
|
xfer->data = BridgeTxBuffer;
|
||||||
|
xfer->dataSize = BridgeTxSize;
|
||||||
break;
|
break;
|
||||||
case kI2C_SlaveReceiveEvent:
|
case kI2C_SlaveReceiveEvent:
|
||||||
|
BridgeProtocolHandler();
|
||||||
|
xfer->data = BridgeRxBuffer;
|
||||||
|
xfer->dataSize = BRIDGE_RX_BUFFER_SIZE;
|
||||||
break;
|
break;
|
||||||
case kI2C_SlaveCompletionEvent:
|
case kI2C_SlaveCompletionEvent:
|
||||||
|
xfer->data = NULL;
|
||||||
|
xfer->dataSize = 0;
|
||||||
break;
|
break;
|
||||||
case kI2C_SlaveTransmitAckEvent:
|
case kI2C_SlaveTransmitAckEvent:
|
||||||
break;
|
break;
|
||||||
@@ -64,6 +69,7 @@ int main(void)
|
|||||||
slaveConfig.addressingMode = kI2C_Address7bit/kI2C_RangeMatch;
|
slaveConfig.addressingMode = kI2C_Address7bit/kI2C_RangeMatch;
|
||||||
I2C_SlaveInit(I2C_BUS_BASEADDR, &slaveConfig);
|
I2C_SlaveInit(I2C_BUS_BASEADDR, &slaveConfig);
|
||||||
I2C_SlaveTransferCreateHandle(I2C_BUS_BASEADDR, &slaveHandle, i2c_slave_callback, NULL);
|
I2C_SlaveTransferCreateHandle(I2C_BUS_BASEADDR, &slaveHandle, i2c_slave_callback, NULL);
|
||||||
|
slaveHandle.eventMask |= kI2C_SlaveCompletionEvent;
|
||||||
I2C_SlaveTransferNonBlocking(I2C_BUS_BASEADDR, &slaveHandle, kI2C_SlaveCompletionEvent);
|
I2C_SlaveTransferNonBlocking(I2C_BUS_BASEADDR, &slaveHandle, kI2C_SlaveCompletionEvent);
|
||||||
|
|
||||||
KeyMatrix_Init(&keyMatrix);
|
KeyMatrix_Init(&keyMatrix);
|
||||||
|
|||||||
17
left/src/main.h
Normal file
17
left/src/main.h
Normal file
@@ -0,0 +1,17 @@
|
|||||||
|
#ifndef __MAIN_H__
|
||||||
|
#define __MAIN_H__
|
||||||
|
|
||||||
|
// Includes:
|
||||||
|
|
||||||
|
#include "key_matrix.h"
|
||||||
|
|
||||||
|
// Macros:
|
||||||
|
|
||||||
|
#define KEYBOARD_MATRIX_COLS_NUM 7
|
||||||
|
#define KEYBOARD_MATRIX_ROWS_NUM 5
|
||||||
|
|
||||||
|
// Variables:
|
||||||
|
|
||||||
|
extern key_matrix_t keyMatrix;
|
||||||
|
|
||||||
|
#endif
|
||||||
@@ -10,13 +10,14 @@
|
|||||||
#define LOGIC_LED_ON 0U
|
#define LOGIC_LED_ON 0U
|
||||||
#define LOGIC_LED_OFF 1U
|
#define LOGIC_LED_OFF 1U
|
||||||
|
|
||||||
#define TEST_LED_GPIO GPIOA
|
#define TEST_LED_GPIO GPIOA
|
||||||
#define TEST_LED_PORT PORTA
|
#define TEST_LED_PORT PORTA
|
||||||
#define TEST_LED_CLOCK kCLOCK_PortA
|
#define TEST_LED_CLOCK kCLOCK_PortA
|
||||||
#define TEST_LED_PIN 12
|
#define TEST_LED_PIN 12
|
||||||
|
|
||||||
#define TEST_LED_ON() GPIO_SetPinsOutput(TEST_LED_GPIO, 1U << TEST_LED_PIN)
|
#define TEST_LED_ON() GPIO_SetPinsOutput(TEST_LED_GPIO, 1U << TEST_LED_PIN)
|
||||||
#define TEST_LED_OFF() GPIO_ClearPinsOutput(TEST_LED_GPIO, 1U << TEST_LED_PIN)
|
#define TEST_LED_OFF() GPIO_ClearPinsOutput(TEST_LED_GPIO, 1U << TEST_LED_PIN)
|
||||||
|
#define TEST_LED_SET(state) GPIO_WritePinOutput(TEST_LED_GPIO, TEST_LED_PIN, !(state))
|
||||||
#define TEST_LED_TOGGLE() GPIO_TogglePinsOutput(TEST_LED_GPIO, 1U << TEST_LED_GPIO_PIN)
|
#define TEST_LED_TOGGLE() GPIO_TogglePinsOutput(TEST_LED_GPIO, 1U << TEST_LED_GPIO_PIN)
|
||||||
|
|
||||||
// Functions:
|
// Functions:
|
||||||
|
|||||||
@@ -81,15 +81,10 @@ static usb_status_t UsbKeyboardAction(void)
|
|||||||
UsbKeyboardReport.scancodes[scancodeIdx] = 0;
|
UsbKeyboardReport.scancodes[scancodeIdx] = 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
i2c_master_transfer_t masterXfer;
|
uint8_t data[] = {0};
|
||||||
masterXfer.slaveAddress = I2C_ADDRESS_LEFT_KEYBOARD_HALF;
|
I2cWrite(I2C_MAIN_BUS_BASEADDR, I2C_ADDRESS_LEFT_KEYBOARD_HALF, data, sizeof(data));
|
||||||
masterXfer.direction = kI2C_Read;
|
|
||||||
masterXfer.subaddress = 0;
|
I2cRead(I2C_MAIN_BUS_BASEADDR, I2C_ADDRESS_LEFT_KEYBOARD_HALF, leftKeyStates, KEY_STATE_COUNT);
|
||||||
masterXfer.subaddressSize = 0;
|
|
||||||
masterXfer.data = leftKeyStates;
|
|
||||||
masterXfer.dataSize = KEY_STATE_COUNT;
|
|
||||||
masterXfer.flags = kI2C_TransferDefaultFlag;
|
|
||||||
I2C_MasterTransferBlocking(I2C_MAIN_BUS_BASEADDR, &masterXfer);
|
|
||||||
|
|
||||||
scancodeIdx = 0;
|
scancodeIdx = 0;
|
||||||
for (uint8_t keyId=0; keyId<KEYBOARD_MATRIX_COLS_NUM*KEYBOARD_MATRIX_ROWS_NUM; keyId++) {
|
for (uint8_t keyId=0; keyId<KEYBOARD_MATRIX_COLS_NUM*KEYBOARD_MATRIX_ROWS_NUM; keyId++) {
|
||||||
|
|||||||
@@ -103,8 +103,11 @@ void JumpToBootloader() {
|
|||||||
|
|
||||||
void GetSetTestLed()
|
void GetSetTestLed()
|
||||||
{
|
{
|
||||||
uint8_t arg = GenericHidInBuffer[1];
|
uint8_t ledState = GenericHidInBuffer[1];
|
||||||
switch (arg) {
|
uint8_t data[] = {1, ledState};
|
||||||
|
I2cWrite(I2C_MAIN_BUS_BASEADDR, I2C_ADDRESS_LEFT_KEYBOARD_HALF, data, sizeof(data));
|
||||||
|
|
||||||
|
switch (ledState) {
|
||||||
case 0:
|
case 0:
|
||||||
TEST_LED_ON();
|
TEST_LED_ON();
|
||||||
break;
|
break;
|
||||||
|
|||||||
28
shared/i2c_adddresses.c
Normal file
28
shared/i2c_adddresses.c
Normal file
@@ -0,0 +1,28 @@
|
|||||||
|
#include "fsl_i2c.h"
|
||||||
|
#include "i2c_addresses.h"
|
||||||
|
|
||||||
|
void I2cRead(I2C_Type *baseAddress, uint8_t i2cAddress, uint8_t *data, uint8_t size)
|
||||||
|
{
|
||||||
|
i2c_master_transfer_t masterXfer;
|
||||||
|
masterXfer.slaveAddress = i2cAddress;
|
||||||
|
masterXfer.direction = kI2C_Read;
|
||||||
|
masterXfer.subaddress = 0;
|
||||||
|
masterXfer.subaddressSize = 0;
|
||||||
|
masterXfer.data = data;
|
||||||
|
masterXfer.dataSize = size;
|
||||||
|
masterXfer.flags = kI2C_TransferDefaultFlag;
|
||||||
|
I2C_MasterTransferBlocking(baseAddress, &masterXfer);
|
||||||
|
}
|
||||||
|
|
||||||
|
void I2cWrite(I2C_Type *baseAddress, uint8_t i2cAddress, uint8_t *data, uint8_t size)
|
||||||
|
{
|
||||||
|
i2c_master_transfer_t masterXfer;
|
||||||
|
masterXfer.slaveAddress = i2cAddress;
|
||||||
|
masterXfer.direction = kI2C_Write;
|
||||||
|
masterXfer.subaddress = 0;
|
||||||
|
masterXfer.subaddressSize = 0;
|
||||||
|
masterXfer.data = data;
|
||||||
|
masterXfer.dataSize = size;
|
||||||
|
masterXfer.flags = kI2C_TransferDefaultFlag;
|
||||||
|
I2C_MasterTransferBlocking(baseAddress, &masterXfer);
|
||||||
|
}
|
||||||
@@ -1,24 +1,35 @@
|
|||||||
#ifndef __I2C_ADDRESSES_H__
|
#ifndef __I2C_ADDRESSES_H__
|
||||||
#define __I2C_ADDRESSES_H__
|
#define __I2C_ADDRESSES_H__
|
||||||
|
|
||||||
// 7-bit I2C addresses - see http://www.i2c-bus.org/addressing/
|
// Includes:
|
||||||
|
|
||||||
// General call / Start byte 0b0000000
|
#include "fsl_gpio.h"
|
||||||
// CBUS address 0b0000001
|
|
||||||
// Reserved for different bus formats 0b0000010
|
|
||||||
// Reserved for future purposes 0b0000011
|
|
||||||
// High-Speed master code 0b00001XX
|
|
||||||
#define I2C_ADDRESS_LEFT_KEYBOARD_HALF 0b0001000
|
|
||||||
#define I2C_ADDRESS_EEPROM 0b1010000
|
|
||||||
#define I2C_ADDRESS_LED_DRIVER_LEFT 0b1110100
|
|
||||||
// LED driver / touchpad 0b1110101
|
|
||||||
// LED driver / touchpad 0b1110110
|
|
||||||
#define I2C_ADDRESS_LED_DRIVER_RIGHT 0b1110111
|
|
||||||
// Touchpad 0b00001XX
|
|
||||||
// 10-bit slave addressing 0b11110XX
|
|
||||||
// Reserved for future purposes 0b11111XX
|
|
||||||
|
|
||||||
#define IS_I2C_LED_DRIVER_ADDRESS(address) \
|
// Macros:
|
||||||
(I2C_ADDRESS_LED_DRIVER_LEFT <= (address) && (address) <= I2C_ADDRESS_LED_DRIVER_RIGHT)
|
|
||||||
|
// 7-bit I2C addresses - see http://www.i2c-bus.org/addressing/
|
||||||
|
|
||||||
|
// General call / Start byte 0b0000000
|
||||||
|
// CBUS address 0b0000001
|
||||||
|
// Reserved for different bus formats 0b0000010
|
||||||
|
// Reserved for future purposes 0b0000011
|
||||||
|
// High-Speed master code 0b00001XX
|
||||||
|
#define I2C_ADDRESS_LEFT_KEYBOARD_HALF 0b0001000
|
||||||
|
#define I2C_ADDRESS_EEPROM 0b1010000
|
||||||
|
#define I2C_ADDRESS_LED_DRIVER_LEFT 0b1110100
|
||||||
|
// LED driver / touchpad 0b1110101
|
||||||
|
// LED driver / touchpad 0b1110110
|
||||||
|
#define I2C_ADDRESS_LED_DRIVER_RIGHT 0b1110111
|
||||||
|
// Touchpad 0b00001XX
|
||||||
|
// 10-bit slave addressing 0b11110XX
|
||||||
|
// Reserved for future purposes 0b11111XX
|
||||||
|
|
||||||
|
#define IS_I2C_LED_DRIVER_ADDRESS(address) \
|
||||||
|
(I2C_ADDRESS_LED_DRIVER_LEFT <= (address) && (address) <= I2C_ADDRESS_LED_DRIVER_RIGHT)
|
||||||
|
|
||||||
|
// Functions:
|
||||||
|
|
||||||
|
void I2cRead(I2C_Type *baseAddress, uint8_t i2cAddress, uint8_t *data, uint8_t size);
|
||||||
|
void I2cWrite(I2C_Type *baseAddress, uint8_t i2cAddress, uint8_t *data, uint8_t size);
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
|||||||
Reference in New Issue
Block a user