Add i2c_message_t and use it all across the codebase. This will allow handling variable-length I2C messages and validation with minimal effort. The test LED and brightness PWM update features got temporarily broken and will fix them soon.
This commit is contained in:
@@ -12,24 +12,19 @@
|
||||
|
||||
static void i2cSlaveCallback(I2C_Type *base, i2c_slave_transfer_t *xfer, void *userData)
|
||||
{
|
||||
switch (xfer->event)
|
||||
{
|
||||
switch (xfer->event) {
|
||||
case kI2C_SlaveTransmitEvent:
|
||||
SlaveProtocolHandler();
|
||||
xfer->data = SlaveTxBuffer;
|
||||
xfer->dataSize = SlaveTxSize;
|
||||
xfer->data = (uint8_t*)&txMessage;
|
||||
xfer->dataSize = txMessage.length+3;
|
||||
break;
|
||||
case kI2C_SlaveReceiveEvent:
|
||||
xfer->data = (uint8_t*)&rxMessage;
|
||||
// xfer->dataSize = I2C_BUFFER_MAX_LENGTH;
|
||||
SlaveProtocolHandler();
|
||||
xfer->data = SlaveRxBuffer;
|
||||
xfer->dataSize = SLAVE_RX_BUFFER_SIZE;
|
||||
break;
|
||||
case kI2C_SlaveCompletionEvent:
|
||||
xfer->data = NULL;
|
||||
xfer->dataSize = 0;
|
||||
break;
|
||||
case kI2C_SlaveTransmitAckEvent:
|
||||
break;
|
||||
//SlaveProtocolHandler();
|
||||
default:
|
||||
break;
|
||||
}
|
||||
|
||||
@@ -10,12 +10,15 @@
|
||||
#include "bool_array_converter.h"
|
||||
#include "bootloader.h"
|
||||
|
||||
i2c_message_t rxMessage;
|
||||
i2c_message_t txMessage;
|
||||
|
||||
void SetError(uint8_t error);
|
||||
void SetGenericError(void);
|
||||
void SetResponseByte(uint8_t response);
|
||||
|
||||
void SetError(uint8_t error) {
|
||||
SlaveTxBuffer[0] = error;
|
||||
txMessage.data[0] = error;
|
||||
}
|
||||
|
||||
void SetGenericError(void)
|
||||
@@ -26,26 +29,26 @@ void SetGenericError(void)
|
||||
// Set a single byte as the response.
|
||||
void SetResponseByte(uint8_t response)
|
||||
{
|
||||
SlaveTxBuffer[1] = response;
|
||||
txMessage.data[1] = response;
|
||||
}
|
||||
|
||||
void SlaveProtocolHandler(void)
|
||||
{
|
||||
uint8_t commandId = SlaveRxBuffer[0];
|
||||
uint8_t commandId = rxMessage.data[0];
|
||||
switch (commandId) {
|
||||
case SlaveCommand_RequestKeyStates:
|
||||
SlaveTxSize = KEY_STATE_BUFFER_SIZE;
|
||||
BoolBytesToBits(keyMatrix.keyStates, SlaveTxBuffer, LEFT_KEYBOARD_HALF_KEY_COUNT);
|
||||
CRC16_AppendToMessage(SlaveTxBuffer, KEY_STATE_SIZE);
|
||||
BoolBytesToBits(keyMatrix.keyStates, txMessage.data, LEFT_KEYBOARD_HALF_KEY_COUNT);
|
||||
txMessage.length = KEY_STATE_SIZE;
|
||||
CRC16_UpdateMessageChecksum(&txMessage);
|
||||
break;
|
||||
case SlaveCommand_SetTestLed:
|
||||
SlaveTxSize = 0;
|
||||
// bool isLedOn = SlaveRxBuffer[1];
|
||||
// TEST_LED_SET(isLedOn);
|
||||
txMessage.length = 0;
|
||||
bool isLedOn = rxMessage.data[1];
|
||||
TEST_LED_SET(isLedOn);
|
||||
break;
|
||||
case SlaveCommand_SetLedPwmBrightness:
|
||||
SlaveTxSize = 0;
|
||||
uint8_t brightnessPercent = SlaveRxBuffer[1];
|
||||
txMessage.length = 0;
|
||||
uint8_t brightnessPercent = rxMessage.data[1];
|
||||
LedPwm_SetBrightness(brightnessPercent);
|
||||
break;
|
||||
case SlaveCommand_JumpToBootloader:
|
||||
|
||||
@@ -5,24 +5,20 @@
|
||||
|
||||
#include "fsl_port.h"
|
||||
#include "crc16.h"
|
||||
#include "slave_protocol.h"
|
||||
|
||||
// Macros:
|
||||
|
||||
#define SLAVE_RX_BUFFER_SIZE 100
|
||||
#define SLAVE_TX_BUFFER_SIZE 100
|
||||
|
||||
#define PROTOCOL_RESPONSE_SUCCESS 0
|
||||
#define PROTOCOL_RESPONSE_GENERIC_ERROR 1
|
||||
|
||||
#define LEFT_KEYBOARD_HALF_KEY_COUNT (KEYBOARD_MATRIX_COLS_NUM*KEYBOARD_MATRIX_ROWS_NUM)
|
||||
#define KEY_STATE_SIZE (LEFT_KEYBOARD_HALF_KEY_COUNT/8 + 1)
|
||||
#define KEY_STATE_BUFFER_SIZE (KEY_STATE_SIZE + CRC16_HASH_LENGTH)
|
||||
|
||||
// Variables:
|
||||
|
||||
uint8_t SlaveRxBuffer[SLAVE_RX_BUFFER_SIZE];
|
||||
uint8_t SlaveTxBuffer[SLAVE_TX_BUFFER_SIZE];
|
||||
uint8_t SlaveTxSize;
|
||||
extern i2c_message_t rxMessage;
|
||||
extern i2c_message_t txMessage;
|
||||
|
||||
// Functions:
|
||||
|
||||
|
||||
Reference in New Issue
Block a user