Transfer a synchronization message to make I2C recovery more robust.
This commit is contained in:
@@ -65,6 +65,11 @@ void SlaveTxHandler(void)
|
||||
case SlaveCommand_RequestProperty: {
|
||||
uint8_t propertyId = RxMessage.data[1];
|
||||
switch (propertyId) {
|
||||
case SlaveProperty_Sync: {
|
||||
memcpy(TxMessage.data, SlaveSyncString, SLAVE_SYNC_STRING_LENGTH);
|
||||
TxMessage.length = SLAVE_SYNC_STRING_LENGTH;
|
||||
break;
|
||||
}
|
||||
case SlaveProperty_Features: {
|
||||
uhk_module_features_t *moduleFeatures = (uhk_module_features_t*)&TxMessage.data;
|
||||
moduleFeatures->keyCount = MODULE_KEY_COUNT;
|
||||
|
||||
@@ -51,7 +51,7 @@ void UhkModuleSlaveDriver_Init(uint8_t uhkModuleDriverId)
|
||||
uhkModuleTargetVars->ledPwmBrightness = 0;
|
||||
|
||||
uhk_module_phase_t *uhkModulePhase = &uhkModuleState->phase;
|
||||
*uhkModulePhase = UhkModulePhase_RequestModuleFeatures;
|
||||
*uhkModulePhase = UhkModulePhase_RequestSync;
|
||||
|
||||
uhk_module_i2c_addresses_t *uhkModuleI2cAddresses = moduleIdsToI2cAddresses + uhkModuleDriverId;
|
||||
uhkModuleState->firmwareI2cAddress = uhkModuleI2cAddresses->firmwareI2cAddress;
|
||||
@@ -70,6 +70,28 @@ status_t UhkModuleSlaveDriver_Update(uint8_t uhkModuleDriverId)
|
||||
|
||||
switch (*uhkModulePhase) {
|
||||
|
||||
// Sync communication
|
||||
case UhkModulePhase_RequestSync:
|
||||
txMessage.data[0] = SlaveCommand_RequestProperty;
|
||||
txMessage.data[1] = SlaveProperty_Sync;
|
||||
txMessage.length = 2;
|
||||
status = tx(i2cAddress);
|
||||
*uhkModulePhase = UhkModulePhase_ReceiveSync;
|
||||
break;
|
||||
case UhkModulePhase_ReceiveSync:
|
||||
status = rx(rxMessage, i2cAddress);
|
||||
*uhkModulePhase = UhkModulePhase_ProcessSync;
|
||||
break;
|
||||
case UhkModulePhase_ProcessSync: {
|
||||
bool isMessageValid = CRC16_IsMessageValid(rxMessage);
|
||||
bool isSyncValid = memcmp(rxMessage->data, SlaveSyncString, SLAVE_SYNC_STRING_LENGTH) == 0;
|
||||
status = kStatus_Uhk_NoTransfer;
|
||||
*uhkModulePhase = isSyncValid && isMessageValid
|
||||
? UhkModulePhase_RequestModuleFeatures
|
||||
: UhkModulePhase_RequestSync;
|
||||
break;
|
||||
}
|
||||
|
||||
// Get module features
|
||||
case UhkModulePhase_RequestModuleFeatures:
|
||||
txMessage.data[0] = SlaveCommand_RequestProperty;
|
||||
|
||||
@@ -20,6 +20,9 @@
|
||||
} uhk_module_id_t;
|
||||
|
||||
typedef enum {
|
||||
UhkModulePhase_RequestSync,
|
||||
UhkModulePhase_ReceiveSync,
|
||||
UhkModulePhase_ProcessSync,
|
||||
UhkModulePhase_RequestModuleFeatures,
|
||||
UhkModulePhase_ReceiveModuleFeatures,
|
||||
UhkModulePhase_ProcessModuleFeatures,
|
||||
|
||||
@@ -1,12 +1,19 @@
|
||||
#ifndef __SLAVE_PROTOCOL_H__
|
||||
#define __SLAVE_PROTOCOL_H__
|
||||
|
||||
// Includes:
|
||||
|
||||
#include "fsl_common.h"
|
||||
|
||||
// Macros:
|
||||
|
||||
#define I2C_MESSAGE_HEADER_LENGTH 3
|
||||
#define I2C_MESSAGE_MAX_PAYLOAD_LENGTH 255
|
||||
#define I2C_MESSAGE_MAX_TOTAL_LENGTH (I2C_MESSAGE_HEADER_LENGTH + I2C_MESSAGE_MAX_PAYLOAD_LENGTH)
|
||||
|
||||
#define SLAVE_SYNC_STRING "SYNC"
|
||||
#define SLAVE_SYNC_STRING_LENGTH (sizeof(SLAVE_SYNC_STRING) - 1)
|
||||
|
||||
// Typedefs:
|
||||
|
||||
typedef enum {
|
||||
@@ -18,6 +25,7 @@
|
||||
} slave_command_t;
|
||||
|
||||
typedef enum {
|
||||
SlaveProperty_Sync,
|
||||
SlaveProperty_Features,
|
||||
} slave_property_t;
|
||||
|
||||
@@ -32,4 +40,8 @@
|
||||
uint8_t data[I2C_MESSAGE_MAX_PAYLOAD_LENGTH];
|
||||
} __attribute__ ((packed)) i2c_message_t;
|
||||
|
||||
// Variables:
|
||||
|
||||
extern char SlaveSyncString[];
|
||||
|
||||
#endif
|
||||
|
||||
Reference in New Issue
Block a user