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:
László Monda
2016-10-19 19:39:00 +02:00
parent 1286d9cfc3
commit 7a6e5523fb
9 changed files with 167 additions and 37 deletions

View 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;
}
}

View 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

View File

@@ -2,14 +2,13 @@
#include "init_clock.h"
#include "fsl_port.h"
#include "fsl_i2c.h"
#include "main.h"
#include "key_matrix.h"
#include "test_led.h"
#include "i2c_addresses.h"
#include "i2c.h"
#include "init_peripherials.h"
#define KEYBOARD_MATRIX_COLS_NUM 7
#define KEYBOARD_MATRIX_ROWS_NUM 5
#include "bridge_protocol_handler.h"
key_matrix_t keyMatrix = {
.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)
{
case kI2C_SlaveTransmitEvent:
xfer->data = keyMatrix.keyStates;
xfer->dataSize = KEYBOARD_MATRIX_COLS_NUM*KEYBOARD_MATRIX_ROWS_NUM;
BridgeProtocolHandler();
xfer->data = BridgeTxBuffer;
xfer->dataSize = BridgeTxSize;
break;
case kI2C_SlaveReceiveEvent:
BridgeProtocolHandler();
xfer->data = BridgeRxBuffer;
xfer->dataSize = BRIDGE_RX_BUFFER_SIZE;
break;
case kI2C_SlaveCompletionEvent:
xfer->data = NULL;
xfer->dataSize = 0;
break;
case kI2C_SlaveTransmitAckEvent:
break;
@@ -64,6 +69,7 @@ int main(void)
slaveConfig.addressingMode = kI2C_Address7bit/kI2C_RangeMatch;
I2C_SlaveInit(I2C_BUS_BASEADDR, &slaveConfig);
I2C_SlaveTransferCreateHandle(I2C_BUS_BASEADDR, &slaveHandle, i2c_slave_callback, NULL);
slaveHandle.eventMask |= kI2C_SlaveCompletionEvent;
I2C_SlaveTransferNonBlocking(I2C_BUS_BASEADDR, &slaveHandle, kI2C_SlaveCompletionEvent);
KeyMatrix_Init(&keyMatrix);

17
left/src/main.h Normal file
View 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

View File

@@ -10,13 +10,14 @@
#define LOGIC_LED_ON 0U
#define LOGIC_LED_OFF 1U
#define TEST_LED_GPIO GPIOA
#define TEST_LED_PORT PORTA
#define TEST_LED_CLOCK kCLOCK_PortA
#define TEST_LED_PIN 12
#define TEST_LED_GPIO GPIOA
#define TEST_LED_PORT PORTA
#define TEST_LED_CLOCK kCLOCK_PortA
#define TEST_LED_PIN 12
#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_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)
// Functions: