From 377fe4a2b2ee61692b052dd2e8665ccdab70f449 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?L=C3=A1szl=C3=B3=20Monda?= Date: Thu, 5 Oct 2017 02:45:22 +0200 Subject: [PATCH] Make EEPROM transfers receive an operation and a buffer id parameter. This allows reading and writing both staging and validated user configurations which will aid future debugging. This API is also cleaner. --- right/src/config_parser/config_globals.c | 15 ++++++++++ right/src/config_parser/config_globals.h | 10 +++---- right/src/eeprom.c | 36 +++++++++++------------- right/src/eeprom.h | 18 +++++++----- right/src/main.c | 4 +-- right/src/usb_protocol_handler.c | 23 +++++++++++---- 6 files changed, 67 insertions(+), 39 deletions(-) diff --git a/right/src/config_parser/config_globals.c b/right/src/config_parser/config_globals.c index 5c59247..e923267 100644 --- a/right/src/config_parser/config_globals.c +++ b/right/src/config_parser/config_globals.c @@ -1,5 +1,6 @@ #include "config_globals.h" #include "attributes.h" +#include "eeprom.h" static uint8_t hardwareConfig[HARDWARE_CONFIG_SIZE]; static uint8_t ATTR_DATA2 stagingUserConfig[USER_CONFIG_SIZE]; @@ -10,3 +11,17 @@ config_buffer_t StagingUserConfigBuffer = { stagingUserConfig }; config_buffer_t ValidatedUserConfigBuffer = { validatedUserConfig }; bool ParserRunDry; + +config_buffer_t* ConfigBufferIdToConfigBuffer(config_buffer_id_t configBufferId) +{ + switch (configBufferId) { + case ConfigBufferId_HardwareConfig: + return &HardwareConfigBuffer; + case ConfigBufferId_StagingUserConfig: + return &StagingUserConfigBuffer; + case ConfigBufferId_ValidatedUserConfig: + return &ValidatedUserConfigBuffer; + default: + return NULL; + } +} diff --git a/right/src/config_parser/config_globals.h b/right/src/config_parser/config_globals.h index bef7539..1bd5464 100644 --- a/right/src/config_parser/config_globals.h +++ b/right/src/config_parser/config_globals.h @@ -4,14 +4,8 @@ // Includes: #include "fsl_common.h" - #include "eeprom.h" #include "basic_types.h" -// Macros: - - #define HARDWARE_CONFIG_SIZE 64 - #define USER_CONFIG_SIZE (EEPROM_SIZE - HARDWARE_CONFIG_SIZE) - // Typedefs: typedef enum { @@ -27,4 +21,8 @@ extern config_buffer_t StagingUserConfigBuffer; extern config_buffer_t ValidatedUserConfigBuffer; + // Functions: + + config_buffer_t* ConfigBufferIdToConfigBuffer(config_buffer_id_t configBufferId); + #endif diff --git a/right/src/eeprom.c b/right/src/eeprom.c index 700a304..c6db190 100644 --- a/right/src/eeprom.c +++ b/right/src/eeprom.c @@ -3,9 +3,11 @@ #include "i2c_addresses.h" #include "i2c.h" #include "eeprom.h" +#include "config_parser/config_globals.h" bool IsEepromBusy; -eeprom_transfer_t CurrentEepromTransfer; +static eeprom_operation_t CurrentEepromOperation; +static config_buffer_id_t CurrentConfigBufferId; status_t LastEepromTransferStatus; void (*SuccessCallback)(); @@ -54,10 +56,8 @@ static void i2cCallback(I2C_Type *base, i2c_master_handle_t *handle, status_t st { LastEepromTransferStatus = status; - bool isHardwareConfig = CurrentEepromTransfer == EepromTransfer_ReadHardwareConfiguration; - switch (CurrentEepromTransfer) { - case EepromTransfer_ReadHardwareConfiguration: - case EepromTransfer_ReadUserConfiguration: + switch (CurrentEepromOperation) { + case EepromOperation_Read: if (isReadSent) { IsEepromBusy = false; if (SuccessCallback) { @@ -66,14 +66,13 @@ static void i2cCallback(I2C_Type *base, i2c_master_handle_t *handle, status_t st return; } LastEepromTransferStatus = i2cAsyncRead( - isHardwareConfig ? HardwareConfigBuffer.buffer : ValidatedUserConfigBuffer.buffer, - isHardwareConfig ? HARDWARE_CONFIG_SIZE : USER_CONFIG_SIZE + ConfigBufferIdToConfigBuffer(CurrentConfigBufferId)->buffer, + CurrentConfigBufferId == ConfigBufferId_HardwareConfig ? HARDWARE_CONFIG_SIZE : USER_CONFIG_SIZE ); IsEepromBusy = true; isReadSent = true; break; - case EepromTransfer_WriteHardwareConfiguration: - case EepromTransfer_WriteUserConfiguration: + case EepromOperation_Write: if (status == kStatus_Success) { sourceOffset += writeLength; } @@ -97,29 +96,28 @@ void EEPROM_Init(void) I2C_MasterTransferCreateHandle(I2C_EEPROM_BUS_BASEADDR, &i2cHandle, i2cCallback, NULL); } -status_t EEPROM_LaunchTransfer(eeprom_transfer_t transferType, void (*successCallback)) +status_t EEPROM_LaunchTransfer(eeprom_operation_t operation, config_buffer_id_t config_buffer_id, void (*successCallback)) { if (IsEepromBusy) { return kStatus_I2C_Busy; } - CurrentEepromTransfer = transferType; + CurrentEepromOperation = operation; + CurrentConfigBufferId = config_buffer_id; + SuccessCallback = successCallback; - bool isHardwareConfig = CurrentEepromTransfer == EepromTransfer_ReadHardwareConfiguration || - CurrentEepromTransfer == EepromTransfer_WriteHardwareConfiguration; + bool isHardwareConfig = CurrentConfigBufferId == ConfigBufferId_HardwareConfig; eepromStartAddress = isHardwareConfig ? 0 : HARDWARE_CONFIG_SIZE; addressBuffer[0] = eepromStartAddress >> 8; addressBuffer[1] = eepromStartAddress & 0xff; - switch (transferType) { - case EepromTransfer_ReadHardwareConfiguration: - case EepromTransfer_ReadUserConfiguration: + switch (CurrentEepromOperation) { + case EepromOperation_Read: isReadSent = false; LastEepromTransferStatus = i2cAsyncWrite(addressBuffer, EEPROM_ADDRESS_LENGTH); break; - case EepromTransfer_WriteHardwareConfiguration: - case EepromTransfer_WriteUserConfiguration: - sourceBuffer = isHardwareConfig ? HardwareConfigBuffer.buffer : ValidatedUserConfigBuffer.buffer; + case EepromOperation_Write: + sourceBuffer = ConfigBufferIdToConfigBuffer(CurrentConfigBufferId)->buffer; sourceOffset = 0; sourceLength = isHardwareConfig ? HARDWARE_CONFIG_SIZE : USER_CONFIG_SIZE; LastEepromTransferStatus = writePage(); diff --git a/right/src/eeprom.h b/right/src/eeprom.h index ea99956..4cda56b 100644 --- a/right/src/eeprom.h +++ b/right/src/eeprom.h @@ -1,9 +1,16 @@ #ifndef __EEPROM_H__ #define __EEPROM_H__ +// Includes: + + #include "config_parser/config_globals.h" + // Macros: #define EEPROM_SIZE (32*1024) + #define HARDWARE_CONFIG_SIZE 64 + #define USER_CONFIG_SIZE (EEPROM_SIZE - HARDWARE_CONFIG_SIZE) + #define EEPROM_ADDRESS_LENGTH 2 #define EEPROM_PAGE_SIZE 64 #define EEPROM_BUFFER_SIZE (EEPROM_ADDRESS_LENGTH + EEPROM_PAGE_SIZE) @@ -11,21 +18,18 @@ // Typedefs: typedef enum { - EepromTransfer_ReadHardwareConfiguration, - EepromTransfer_WriteHardwareConfiguration, - EepromTransfer_ReadUserConfiguration, - EepromTransfer_WriteUserConfiguration, - } eeprom_transfer_t; + EepromOperation_Read, + EepromOperation_Write, + } eeprom_operation_t; // Variables: extern bool IsEepromBusy; - extern eeprom_transfer_t CurrentEepromTransfer; extern status_t EepromTransferStatus; // Functions: void EEPROM_Init(void); - status_t EEPROM_LaunchTransfer(eeprom_transfer_t transferType, void (*successCallback)); + status_t EEPROM_LaunchTransfer(eeprom_operation_t operation, config_buffer_id_t config_buffer_id, void (*successCallback)); #endif diff --git a/right/src/main.c b/right/src/main.c index 314eab0..25013b7 100644 --- a/right/src/main.c +++ b/right/src/main.c @@ -70,14 +70,14 @@ void userConfigurationReadFinished(void) void hardwareConfigurationReadFinished(void) { - EEPROM_LaunchTransfer(EepromTransfer_ReadUserConfiguration, userConfigurationReadFinished); + EEPROM_LaunchTransfer(EepromOperation_Read, ConfigBufferId_StagingUserConfig, userConfigurationReadFinished); } void main(void) { InitClock(); InitPeripherals(); - EEPROM_LaunchTransfer(EepromTransfer_ReadHardwareConfiguration, hardwareConfigurationReadFinished); + EEPROM_LaunchTransfer(EepromOperation_Read, ConfigBufferId_HardwareConfig, hardwareConfigurationReadFinished); #ifdef FORCE_BUSPAL Wormhole.magicNumber = WORMHOLE_MAGIC_NUMBER; diff --git a/right/src/usb_protocol_handler.c b/right/src/usb_protocol_handler.c index 898610a..55caf16 100644 --- a/right/src/usb_protocol_handler.c +++ b/right/src/usb_protocol_handler.c @@ -158,10 +158,23 @@ void getAdcValue(void) GenericHidOutBuffer[3] = adcValue >> 24; } -void launchEepromTransfer(void) +void legacyLaunchEepromTransfer(void) { - eeprom_transfer_t transferType = GenericHidInBuffer[1]; - EEPROM_LaunchTransfer(transferType, NULL); + uint8_t legacyEepromTransferId = GenericHidInBuffer[1]; + switch (legacyEepromTransferId) { + case 0: + EEPROM_LaunchTransfer(EepromOperation_Read, ConfigBufferId_HardwareConfig, NULL); + break; + case 1: + EEPROM_LaunchTransfer(EepromOperation_Write, ConfigBufferId_HardwareConfig, NULL); + break; + case 2: + EEPROM_LaunchTransfer(EepromOperation_Read, ConfigBufferId_ValidatedUserConfig, NULL); + break; + case 3: + EEPROM_LaunchTransfer(EepromOperation_Write, ConfigBufferId_StagingUserConfig, NULL); + break; + } } void readConfiguration(bool isHardware) @@ -174,7 +187,7 @@ void readConfiguration(bool isHardware) return; } - uint8_t *buffer = isHardware ? HardwareConfigBuffer.buffer : StagingUserConfigBuffer.buffer; + uint8_t *buffer = isHardware ? HardwareConfigBuffer.buffer : ValidatedUserConfigBuffer.buffer; uint16_t bufferLength = isHardware ? HARDWARE_CONFIG_SIZE : USER_CONFIG_SIZE; if (offset + length > bufferLength) { @@ -281,7 +294,7 @@ void UsbProtocolHandler(void) getAdcValue(); break; case UsbCommand_LaunchEepromTransfer: - launchEepromTransfer(); + legacyLaunchEepromTransfer(); break; case UsbCommand_ReadHardwareConfiguration: readConfiguration(true);