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.

This commit is contained in:
László Monda
2017-10-05 02:45:22 +02:00
parent 259f4d3299
commit 377fe4a2b2
6 changed files with 67 additions and 39 deletions

View File

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

View File

@@ -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

View File

@@ -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();

View File

@@ -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

View File

@@ -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;

View File

@@ -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);