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)
|
static void i2cSlaveCallback(I2C_Type *base, i2c_slave_transfer_t *xfer, void *userData)
|
||||||
{
|
{
|
||||||
switch (xfer->event)
|
switch (xfer->event) {
|
||||||
{
|
|
||||||
case kI2C_SlaveTransmitEvent:
|
case kI2C_SlaveTransmitEvent:
|
||||||
SlaveProtocolHandler();
|
SlaveProtocolHandler();
|
||||||
xfer->data = SlaveTxBuffer;
|
xfer->data = (uint8_t*)&txMessage;
|
||||||
xfer->dataSize = SlaveTxSize;
|
xfer->dataSize = txMessage.length+3;
|
||||||
break;
|
break;
|
||||||
case kI2C_SlaveReceiveEvent:
|
case kI2C_SlaveReceiveEvent:
|
||||||
|
xfer->data = (uint8_t*)&rxMessage;
|
||||||
|
// xfer->dataSize = I2C_BUFFER_MAX_LENGTH;
|
||||||
SlaveProtocolHandler();
|
SlaveProtocolHandler();
|
||||||
xfer->data = SlaveRxBuffer;
|
|
||||||
xfer->dataSize = SLAVE_RX_BUFFER_SIZE;
|
|
||||||
break;
|
break;
|
||||||
case kI2C_SlaveCompletionEvent:
|
case kI2C_SlaveCompletionEvent:
|
||||||
xfer->data = NULL;
|
//SlaveProtocolHandler();
|
||||||
xfer->dataSize = 0;
|
|
||||||
break;
|
|
||||||
case kI2C_SlaveTransmitAckEvent:
|
|
||||||
break;
|
|
||||||
default:
|
default:
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -10,12 +10,15 @@
|
|||||||
#include "bool_array_converter.h"
|
#include "bool_array_converter.h"
|
||||||
#include "bootloader.h"
|
#include "bootloader.h"
|
||||||
|
|
||||||
|
i2c_message_t rxMessage;
|
||||||
|
i2c_message_t txMessage;
|
||||||
|
|
||||||
void SetError(uint8_t error);
|
void SetError(uint8_t error);
|
||||||
void SetGenericError(void);
|
void SetGenericError(void);
|
||||||
void SetResponseByte(uint8_t response);
|
void SetResponseByte(uint8_t response);
|
||||||
|
|
||||||
void SetError(uint8_t error) {
|
void SetError(uint8_t error) {
|
||||||
SlaveTxBuffer[0] = error;
|
txMessage.data[0] = error;
|
||||||
}
|
}
|
||||||
|
|
||||||
void SetGenericError(void)
|
void SetGenericError(void)
|
||||||
@@ -26,26 +29,26 @@ void SetGenericError(void)
|
|||||||
// Set a single byte as the response.
|
// Set a single byte as the response.
|
||||||
void SetResponseByte(uint8_t response)
|
void SetResponseByte(uint8_t response)
|
||||||
{
|
{
|
||||||
SlaveTxBuffer[1] = response;
|
txMessage.data[1] = response;
|
||||||
}
|
}
|
||||||
|
|
||||||
void SlaveProtocolHandler(void)
|
void SlaveProtocolHandler(void)
|
||||||
{
|
{
|
||||||
uint8_t commandId = SlaveRxBuffer[0];
|
uint8_t commandId = rxMessage.data[0];
|
||||||
switch (commandId) {
|
switch (commandId) {
|
||||||
case SlaveCommand_RequestKeyStates:
|
case SlaveCommand_RequestKeyStates:
|
||||||
SlaveTxSize = KEY_STATE_BUFFER_SIZE;
|
BoolBytesToBits(keyMatrix.keyStates, txMessage.data, LEFT_KEYBOARD_HALF_KEY_COUNT);
|
||||||
BoolBytesToBits(keyMatrix.keyStates, SlaveTxBuffer, LEFT_KEYBOARD_HALF_KEY_COUNT);
|
txMessage.length = KEY_STATE_SIZE;
|
||||||
CRC16_AppendToMessage(SlaveTxBuffer, KEY_STATE_SIZE);
|
CRC16_UpdateMessageChecksum(&txMessage);
|
||||||
break;
|
break;
|
||||||
case SlaveCommand_SetTestLed:
|
case SlaveCommand_SetTestLed:
|
||||||
SlaveTxSize = 0;
|
txMessage.length = 0;
|
||||||
// bool isLedOn = SlaveRxBuffer[1];
|
bool isLedOn = rxMessage.data[1];
|
||||||
// TEST_LED_SET(isLedOn);
|
TEST_LED_SET(isLedOn);
|
||||||
break;
|
break;
|
||||||
case SlaveCommand_SetLedPwmBrightness:
|
case SlaveCommand_SetLedPwmBrightness:
|
||||||
SlaveTxSize = 0;
|
txMessage.length = 0;
|
||||||
uint8_t brightnessPercent = SlaveRxBuffer[1];
|
uint8_t brightnessPercent = rxMessage.data[1];
|
||||||
LedPwm_SetBrightness(brightnessPercent);
|
LedPwm_SetBrightness(brightnessPercent);
|
||||||
break;
|
break;
|
||||||
case SlaveCommand_JumpToBootloader:
|
case SlaveCommand_JumpToBootloader:
|
||||||
|
|||||||
@@ -5,24 +5,20 @@
|
|||||||
|
|
||||||
#include "fsl_port.h"
|
#include "fsl_port.h"
|
||||||
#include "crc16.h"
|
#include "crc16.h"
|
||||||
|
#include "slave_protocol.h"
|
||||||
|
|
||||||
// Macros:
|
// Macros:
|
||||||
|
|
||||||
#define SLAVE_RX_BUFFER_SIZE 100
|
|
||||||
#define SLAVE_TX_BUFFER_SIZE 100
|
|
||||||
|
|
||||||
#define PROTOCOL_RESPONSE_SUCCESS 0
|
#define PROTOCOL_RESPONSE_SUCCESS 0
|
||||||
#define PROTOCOL_RESPONSE_GENERIC_ERROR 1
|
#define PROTOCOL_RESPONSE_GENERIC_ERROR 1
|
||||||
|
|
||||||
#define LEFT_KEYBOARD_HALF_KEY_COUNT (KEYBOARD_MATRIX_COLS_NUM*KEYBOARD_MATRIX_ROWS_NUM)
|
#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_SIZE (LEFT_KEYBOARD_HALF_KEY_COUNT/8 + 1)
|
||||||
#define KEY_STATE_BUFFER_SIZE (KEY_STATE_SIZE + CRC16_HASH_LENGTH)
|
|
||||||
|
|
||||||
// Variables:
|
// Variables:
|
||||||
|
|
||||||
uint8_t SlaveRxBuffer[SLAVE_RX_BUFFER_SIZE];
|
extern i2c_message_t rxMessage;
|
||||||
uint8_t SlaveTxBuffer[SLAVE_TX_BUFFER_SIZE];
|
extern i2c_message_t txMessage;
|
||||||
uint8_t SlaveTxSize;
|
|
||||||
|
|
||||||
// Functions:
|
// Functions:
|
||||||
|
|
||||||
|
|||||||
Submodule lib/KSDK_2.0_MK22FN512xxx12 updated: cd173f98d1...7142722eed
@@ -1,4 +1,5 @@
|
|||||||
#include "i2c.h"
|
#include "i2c.h"
|
||||||
|
#include "crc16.h"
|
||||||
|
|
||||||
i2c_master_handle_t I2cMasterHandle;
|
i2c_master_handle_t I2cMasterHandle;
|
||||||
i2c_master_transfer_t masterTransfer;
|
i2c_master_transfer_t masterTransfer;
|
||||||
@@ -9,8 +10,19 @@ status_t I2cAsyncWrite(uint8_t i2cAddress, uint8_t *data, size_t dataSize)
|
|||||||
masterTransfer.direction = kI2C_Write;
|
masterTransfer.direction = kI2C_Write;
|
||||||
masterTransfer.data = data;
|
masterTransfer.data = data;
|
||||||
masterTransfer.dataSize = dataSize;
|
masterTransfer.dataSize = dataSize;
|
||||||
status_t status = I2C_MasterTransferNonBlocking(I2C_MAIN_BUS_BASEADDR, &I2cMasterHandle, &masterTransfer);
|
I2cMasterHandle.userData = NULL;
|
||||||
return status;
|
return I2C_MasterTransferNonBlocking(I2C_MAIN_BUS_BASEADDR, &I2cMasterHandle, &masterTransfer);
|
||||||
|
}
|
||||||
|
|
||||||
|
status_t I2cAsyncWriteMessage(uint8_t i2cAddress, i2c_message_t *message)
|
||||||
|
{
|
||||||
|
masterTransfer.slaveAddress = i2cAddress;
|
||||||
|
masterTransfer.direction = kI2C_Write;
|
||||||
|
masterTransfer.data = (uint8_t*)message;
|
||||||
|
masterTransfer.dataSize = message->length+3;
|
||||||
|
I2cMasterHandle.userData = NULL;
|
||||||
|
CRC16_UpdateMessageChecksum(message);
|
||||||
|
return I2C_MasterTransferNonBlocking(I2C_MAIN_BUS_BASEADDR, &I2cMasterHandle, &masterTransfer);
|
||||||
}
|
}
|
||||||
|
|
||||||
status_t I2cAsyncRead(uint8_t i2cAddress, uint8_t *data, size_t dataSize)
|
status_t I2cAsyncRead(uint8_t i2cAddress, uint8_t *data, size_t dataSize)
|
||||||
@@ -19,6 +31,16 @@ status_t I2cAsyncRead(uint8_t i2cAddress, uint8_t *data, size_t dataSize)
|
|||||||
masterTransfer.direction = kI2C_Read;
|
masterTransfer.direction = kI2C_Read;
|
||||||
masterTransfer.data = data;
|
masterTransfer.data = data;
|
||||||
masterTransfer.dataSize = dataSize;
|
masterTransfer.dataSize = dataSize;
|
||||||
status_t status = I2C_MasterTransferNonBlocking(I2C_MAIN_BUS_BASEADDR, &I2cMasterHandle, &masterTransfer);
|
I2cMasterHandle.userData = NULL;
|
||||||
return status;
|
return I2C_MasterTransferNonBlocking(I2C_MAIN_BUS_BASEADDR, &I2cMasterHandle, &masterTransfer);
|
||||||
|
}
|
||||||
|
|
||||||
|
status_t I2cAsyncReadMessage(uint8_t i2cAddress, i2c_message_t *message)
|
||||||
|
{
|
||||||
|
masterTransfer.slaveAddress = i2cAddress;
|
||||||
|
masterTransfer.direction = kI2C_Read;
|
||||||
|
masterTransfer.data = (uint8_t*)message;
|
||||||
|
masterTransfer.dataSize = I2C_MESSAGE_MAX_LENGTH;
|
||||||
|
I2cMasterHandle.userData = (void*)1;
|
||||||
|
return I2C_MasterTransferNonBlocking(I2C_MAIN_BUS_BASEADDR, &I2cMasterHandle, &masterTransfer);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -4,6 +4,7 @@
|
|||||||
// Includes:
|
// Includes:
|
||||||
|
|
||||||
#include "fsl_i2c.h"
|
#include "fsl_i2c.h"
|
||||||
|
#include "slave_protocol.h"
|
||||||
|
|
||||||
// Macros:
|
// Macros:
|
||||||
|
|
||||||
@@ -45,5 +46,7 @@
|
|||||||
|
|
||||||
status_t I2cAsyncWrite(uint8_t i2cAddress, uint8_t *data, size_t dataSize);
|
status_t I2cAsyncWrite(uint8_t i2cAddress, uint8_t *data, size_t dataSize);
|
||||||
status_t I2cAsyncRead(uint8_t i2cAddress, uint8_t *data, size_t dataSize);
|
status_t I2cAsyncRead(uint8_t i2cAddress, uint8_t *data, size_t dataSize);
|
||||||
|
status_t I2cAsyncWriteMessage(uint8_t i2cAddress, i2c_message_t *message);
|
||||||
|
status_t I2cAsyncReadMessage(uint8_t i2cAddress, i2c_message_t *message);
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
|||||||
@@ -10,8 +10,9 @@
|
|||||||
|
|
||||||
uhk_module_state_t UhkModuleStates[UHK_MODULE_MAX_COUNT];
|
uhk_module_state_t UhkModuleStates[UHK_MODULE_MAX_COUNT];
|
||||||
uhk_module_phase_t uhkModulePhase = UhkModulePhase_RequestKeyStates;
|
uhk_module_phase_t uhkModulePhase = UhkModulePhase_RequestKeyStates;
|
||||||
uint8_t txBuffer[2];
|
|
||||||
uint8_t rxBuffer[KEY_STATE_BUFFER_SIZE];
|
i2c_message_t rxMessage;
|
||||||
|
i2c_message_t txMessage;
|
||||||
|
|
||||||
void UhkModuleSlaveDriver_Init(uint8_t uhkModuleId)
|
void UhkModuleSlaveDriver_Init(uint8_t uhkModuleId)
|
||||||
{
|
{
|
||||||
@@ -27,31 +28,34 @@ status_t UhkModuleSlaveDriver_Update(uint8_t uhkModuleId)
|
|||||||
|
|
||||||
switch (uhkModulePhase) {
|
switch (uhkModulePhase) {
|
||||||
case UhkModulePhase_RequestKeyStates:
|
case UhkModulePhase_RequestKeyStates:
|
||||||
txBuffer[0] = SlaveCommand_RequestKeyStates;
|
txMessage.data[0] = SlaveCommand_RequestKeyStates;
|
||||||
status = I2cAsyncWrite(I2C_ADDRESS_LEFT_KEYBOARD_HALF_FIRMWARE, txBuffer, 1);
|
txMessage.length = 1;
|
||||||
|
status = I2cAsyncWriteMessage(I2C_ADDRESS_LEFT_KEYBOARD_HALF_FIRMWARE, &txMessage);
|
||||||
uhkModulePhase = UhkModulePhase_ReceiveKeystates;
|
uhkModulePhase = UhkModulePhase_ReceiveKeystates;
|
||||||
break;
|
break;
|
||||||
case UhkModulePhase_ReceiveKeystates:
|
case UhkModulePhase_ReceiveKeystates:
|
||||||
status = I2cAsyncRead(I2C_ADDRESS_LEFT_KEYBOARD_HALF_FIRMWARE, rxBuffer, KEY_STATE_BUFFER_SIZE);
|
status = I2cAsyncReadMessage(I2C_ADDRESS_LEFT_KEYBOARD_HALF_FIRMWARE, &rxMessage);
|
||||||
uhkModulePhase = UhkModulePhase_ProcessKeystates;
|
uhkModulePhase = UhkModulePhase_ProcessKeystates;
|
||||||
break;
|
break;
|
||||||
case UhkModulePhase_ProcessKeystates:
|
case UhkModulePhase_ProcessKeystates:
|
||||||
if (CRC16_IsMessageValid(rxBuffer, KEY_STATE_SIZE)) {
|
if (CRC16_IsMessageValid(&rxMessage)) {
|
||||||
BoolBitsToBytes(rxBuffer, CurrentKeyStates[SLOT_ID_LEFT_KEYBOARD_HALF], LEFT_KEYBOARD_HALF_KEY_COUNT);
|
BoolBitsToBytes(rxMessage.data, CurrentKeyStates[SLOT_ID_LEFT_KEYBOARD_HALF], LEFT_KEYBOARD_HALF_KEY_COUNT);
|
||||||
}
|
}
|
||||||
status = kStatus_Uhk_NoOp;
|
status = kStatus_Uhk_NoOp;
|
||||||
uhkModulePhase = UhkModulePhase_SetLedPwmBrightness;
|
|
||||||
break;
|
|
||||||
case UhkModulePhase_SetLedPwmBrightness:
|
|
||||||
txBuffer[0] = SlaveCommand_SetLedPwmBrightness;
|
|
||||||
txBuffer[1] = uhkModuleInternalState->ledPwmBrightness;
|
|
||||||
status = I2cAsyncWrite(I2C_ADDRESS_LEFT_KEYBOARD_HALF_FIRMWARE, txBuffer, 2);
|
|
||||||
uhkModulePhase = UhkModulePhase_SetTestLed;
|
uhkModulePhase = UhkModulePhase_SetTestLed;
|
||||||
break;
|
break;
|
||||||
case UhkModulePhase_SetTestLed:
|
case UhkModulePhase_SetTestLed:
|
||||||
txBuffer[0] = SlaveCommand_SetTestLed;
|
txMessage.data[0] = SlaveCommand_SetTestLed;
|
||||||
txBuffer[1] = uhkModuleInternalState->isTestLedOn;
|
txMessage.data[1] = uhkModuleInternalState->isTestLedOn;
|
||||||
status = I2cAsyncWrite(I2C_ADDRESS_LEFT_KEYBOARD_HALF_FIRMWARE, txBuffer, 2);
|
txMessage.length = 2;
|
||||||
|
status = I2cAsyncWriteMessage(I2C_ADDRESS_LEFT_KEYBOARD_HALF_FIRMWARE, &txMessage);
|
||||||
|
uhkModulePhase = UhkModulePhase_SetLedPwmBrightness;
|
||||||
|
break;
|
||||||
|
case UhkModulePhase_SetLedPwmBrightness:
|
||||||
|
txMessage.data[0] = SlaveCommand_SetLedPwmBrightness;
|
||||||
|
txMessage.data[1] = uhkModuleInternalState->ledPwmBrightness;
|
||||||
|
txMessage.length = 2;
|
||||||
|
status = I2cAsyncWriteMessage(I2C_ADDRESS_LEFT_KEYBOARD_HALF_FIRMWARE, &txMessage);
|
||||||
uhkModulePhase = UhkModulePhase_RequestKeyStates;
|
uhkModulePhase = UhkModulePhase_RequestKeyStates;
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -10,7 +10,6 @@
|
|||||||
|
|
||||||
#define UHK_MODULE_MAX_COUNT 3
|
#define UHK_MODULE_MAX_COUNT 3
|
||||||
#define KEY_STATE_SIZE (LEFT_KEYBOARD_HALF_KEY_COUNT/8 + 1)
|
#define KEY_STATE_SIZE (LEFT_KEYBOARD_HALF_KEY_COUNT/8 + 1)
|
||||||
#define KEY_STATE_BUFFER_SIZE (KEY_STATE_SIZE + CRC16_HASH_LENGTH)
|
|
||||||
|
|
||||||
// Typedefs:
|
// Typedefs:
|
||||||
|
|
||||||
|
|||||||
@@ -36,21 +36,20 @@ void crc16_finalize(crc16_data_t *crc16Config, uint16_t *hash)
|
|||||||
|
|
||||||
crc16_data_t crc16data;
|
crc16_data_t crc16data;
|
||||||
|
|
||||||
void CRC16_AppendToMessage(uint8_t *message, uint32_t lengthInBytes)
|
void CRC16_UpdateMessageChecksum(i2c_message_t *message)
|
||||||
{
|
{
|
||||||
uint16_t hash;
|
uint16_t hash;
|
||||||
crc16_init(&crc16data);
|
crc16_init(&crc16data);
|
||||||
crc16_update(&crc16data, message, lengthInBytes);
|
crc16_update(&crc16data, message->data, message->length);
|
||||||
crc16_finalize(&crc16data, &hash);
|
crc16_finalize(&crc16data, &hash);
|
||||||
message[lengthInBytes] = hash & 0xff;
|
message->crc = hash;
|
||||||
message[lengthInBytes+1] = hash >> 8;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
bool CRC16_IsMessageValid(uint8_t *message, uint32_t lengthInBytes)
|
bool CRC16_IsMessageValid(i2c_message_t *message)
|
||||||
{
|
{
|
||||||
uint16_t hash;
|
uint16_t hash;
|
||||||
crc16_init(&crc16data);
|
crc16_init(&crc16data);
|
||||||
crc16_update(&crc16data, message, lengthInBytes);
|
crc16_update(&crc16data, message->data, message->length);
|
||||||
crc16_finalize(&crc16data, &hash);
|
crc16_finalize(&crc16data, &hash);
|
||||||
return (message[lengthInBytes] == (hash & 0xff)) && (message[lengthInBytes+1] == (hash >> 8));
|
return message->crc == hash;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -3,6 +3,7 @@
|
|||||||
|
|
||||||
#include <stdint.h>
|
#include <stdint.h>
|
||||||
#include <stdbool.h>
|
#include <stdbool.h>
|
||||||
|
#include "slave_protocol.h"
|
||||||
|
|
||||||
#define CRC16_HASH_LENGTH 2 // bytes
|
#define CRC16_HASH_LENGTH 2 // bytes
|
||||||
|
|
||||||
@@ -25,7 +26,7 @@ void crc16_update(crc16_data_t *crc16Config, const uint8_t *src, uint32_t length
|
|||||||
//! @param hash Pointer to the value returned for the final calculated crc value.
|
//! @param hash Pointer to the value returned for the final calculated crc value.
|
||||||
void crc16_finalize(crc16_data_t *crc16Config, uint16_t *hash);
|
void crc16_finalize(crc16_data_t *crc16Config, uint16_t *hash);
|
||||||
|
|
||||||
void CRC16_AppendToMessage(uint8_t *message, uint32_t lengthInBytes);
|
void CRC16_UpdateMessageChecksum(i2c_message_t *message);
|
||||||
bool CRC16_IsMessageValid(uint8_t *message, uint32_t lengthInBytes);
|
bool CRC16_IsMessageValid(i2c_message_t *message);
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
|||||||
@@ -1,6 +1,11 @@
|
|||||||
#ifndef __SLAVE_PROTOCOL_H__
|
#ifndef __SLAVE_PROTOCOL_H__
|
||||||
#define __SLAVE_PROTOCOL_H__
|
#define __SLAVE_PROTOCOL_H__
|
||||||
|
|
||||||
|
// Macros:
|
||||||
|
|
||||||
|
#define I2C_MESSAGE_MAX_LENGTH 255
|
||||||
|
#define I2C_BUFFER_MAX_LENGTH (I2C_MESSAGE_MAX_LENGTH + 3)
|
||||||
|
|
||||||
// Typedefs:
|
// Typedefs:
|
||||||
|
|
||||||
typedef enum {
|
typedef enum {
|
||||||
@@ -10,4 +15,10 @@
|
|||||||
SlaveCommand_JumpToBootloader,
|
SlaveCommand_JumpToBootloader,
|
||||||
} slave_command_t;
|
} slave_command_t;
|
||||||
|
|
||||||
|
typedef struct {
|
||||||
|
uint8_t length;
|
||||||
|
uint16_t crc;
|
||||||
|
uint8_t data[I2C_MESSAGE_MAX_LENGTH];
|
||||||
|
} __attribute__ ((packed)) i2c_message_t;
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
|||||||
Reference in New Issue
Block a user