Fixed multiple bugs and errors.
- Added WARNING handler - put voltage calculations to dedicated module fixed last errors. Updated menu repair screen without ERROR from PID This is version 0.9.0.3, which is used for the first duration test Will also be tagged git-svn-id: https://svn.vbchaos.nl/svn/hsb/trunk@272 05563f52-14a8-4384-a975-3d1654cca0fa
This commit is contained in:
@@ -27,6 +27,17 @@
|
||||
|
||||
#include "DeviceParameters.h"
|
||||
|
||||
#include "hsb-mrts.h"
|
||||
#include "Error.h"
|
||||
#include "PIDParameters.h"
|
||||
#include "PIN.h"
|
||||
|
||||
#include "CachedStorage.h"
|
||||
#include "crc32.h"
|
||||
#include "Logger.h"
|
||||
#include "MemoryDevice.h"
|
||||
|
||||
|
||||
// -----------------------------------------------------------------------------
|
||||
// Constant and macro definitions
|
||||
// -----------------------------------------------------------------------------
|
||||
@@ -42,6 +53,8 @@ struct DeviceParameters
|
||||
bool initialized;
|
||||
struct CachedStorage* parametersStorage;
|
||||
struct MemoryDevice* memoryDevice;
|
||||
unsigned int PIDParametersOffset;
|
||||
unsigned int PINOffset;
|
||||
};
|
||||
|
||||
struct PIDParametersStorageClass
|
||||
@@ -60,14 +73,14 @@ struct PINStorageClass
|
||||
// File-scope variables
|
||||
// -----------------------------------------------------------------------------
|
||||
|
||||
static struct DeviceParameters _self = {.initialized = false};
|
||||
struct DeviceParameters* const self = &_self;
|
||||
static struct DeviceParameters _dParam = {.initialized = false};
|
||||
struct DeviceParameters* const dParam = &_dParam;
|
||||
|
||||
// -----------------------------------------------------------------------------
|
||||
// Function declarations
|
||||
// -----------------------------------------------------------------------------
|
||||
|
||||
|
||||
static void DeviceParameters_verifyCRCs(void);
|
||||
|
||||
// -----------------------------------------------------------------------------
|
||||
// Function definitions
|
||||
@@ -76,13 +89,14 @@ struct DeviceParameters* const self = &_self;
|
||||
ErrorStatus DeviceParameters_construct(struct CachedStorage* parametersStorage, struct MemoryDevice* memoryDevice)
|
||||
{
|
||||
ErrorStatus returnValue = SUCCESS;
|
||||
if (!self->initialized)
|
||||
|
||||
if (!dParam->initialized)
|
||||
{
|
||||
if (returnValue == SUCCESS)
|
||||
{
|
||||
if (parametersStorage != NULL)
|
||||
{
|
||||
self->parametersStorage = parametersStorage;
|
||||
dParam->parametersStorage = parametersStorage;
|
||||
}
|
||||
else
|
||||
{
|
||||
@@ -96,7 +110,7 @@ ErrorStatus DeviceParameters_construct(struct CachedStorage* parametersStorage,
|
||||
{
|
||||
if (memoryDevice->initialized)
|
||||
{
|
||||
self->memoryDevice = memoryDevice;
|
||||
dParam->memoryDevice = memoryDevice;
|
||||
}
|
||||
else
|
||||
{
|
||||
@@ -108,7 +122,95 @@ ErrorStatus DeviceParameters_construct(struct CachedStorage* parametersStorage,
|
||||
returnValue = ERROR;
|
||||
}
|
||||
}
|
||||
self->initialized = true;
|
||||
|
||||
if (returnValue == SUCCESS)
|
||||
{
|
||||
// Create new cachedStorage with preset page number
|
||||
returnValue = CachedStorage_construct(dParam->parametersStorage, dParam->memoryDevice, APP_FLASH_PARAMETERS_PAGE, (sizeof(struct PIDParametersStorageClass) + sizeof(struct PINStorageClass)) / 4);
|
||||
}
|
||||
|
||||
if (returnValue == SUCCESS)
|
||||
{
|
||||
dParam->PIDParametersOffset = 0;
|
||||
dParam->PINOffset = sizeof(struct PIDParametersStorageClass) / 4;
|
||||
dParam->initialized = true;
|
||||
}
|
||||
|
||||
// Check the CRC on the loaded parameters
|
||||
// If a CRC fails, corrupted data will automatically be replaced with defaults
|
||||
if (returnValue == SUCCESS)
|
||||
{
|
||||
DeviceParameters_verifyCRCs();
|
||||
}
|
||||
|
||||
}
|
||||
else
|
||||
{
|
||||
returnValue = ERROR;
|
||||
}
|
||||
|
||||
return returnValue;
|
||||
}
|
||||
|
||||
|
||||
void DeviceParameters_destruct(void)
|
||||
{
|
||||
if (dParam->initialized)
|
||||
{
|
||||
CachedStorage_destruct(dParam->parametersStorage);
|
||||
dParam->parametersStorage = NULL;
|
||||
dParam->memoryDevice = NULL;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
struct PIDParameters* DeviceParameters_getPIDParameters(void)
|
||||
{
|
||||
struct PIDParameters* returnValue = NULL;
|
||||
if (dParam->initialized)
|
||||
{
|
||||
struct PIDParametersStorageClass* tempValue;
|
||||
tempValue = (struct PIDParametersStorageClass*)CachedStorage_readBlob(dParam->parametersStorage, dParam->PIDParametersOffset);
|
||||
returnValue = &tempValue->pidParameters;
|
||||
}
|
||||
return returnValue;
|
||||
}
|
||||
|
||||
|
||||
struct PIN* DeviceParameters_getPIN(void)
|
||||
{
|
||||
struct PIN* returnValue = NULL;
|
||||
if (dParam->initialized)
|
||||
{
|
||||
struct PINStorageClass* tempValue;
|
||||
tempValue = (struct PINStorageClass*)CachedStorage_readBlob(dParam->parametersStorage, dParam->PINOffset);
|
||||
returnValue = &tempValue->pin;
|
||||
}
|
||||
return returnValue;
|
||||
}
|
||||
|
||||
|
||||
void DeviceParameters_saveParameters(void)
|
||||
{
|
||||
if (dParam->initialized)
|
||||
{
|
||||
// Commit cache to memory - will not write if no changes have been made
|
||||
CachedStorage_commit(dParam->parametersStorage);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
ErrorStatus DeviceParameters_writePIDParameters(struct PIDParameters* parameters)
|
||||
{
|
||||
ErrorStatus returnValue = SUCCESS;
|
||||
if (dParam->initialized)
|
||||
{
|
||||
struct PIDParametersStorageClass tempPIDStorage;
|
||||
tempPIDStorage.pidParameters = *parameters;
|
||||
// Calculate CRC over preset
|
||||
tempPIDStorage.crc = crc32_calculate(0, &tempPIDStorage.pidParameters, sizeof(struct PIDParameters));
|
||||
// Put default preset on Cache
|
||||
CachedStorage_writeBlob(dParam->parametersStorage, dParam->PIDParametersOffset, &tempPIDStorage, sizeof(struct PIDParametersStorageClass) / 4);
|
||||
}
|
||||
else
|
||||
{
|
||||
@@ -118,19 +220,70 @@ ErrorStatus DeviceParameters_construct(struct CachedStorage* parametersStorage,
|
||||
}
|
||||
|
||||
|
||||
void DeviceParameters_destruct(void)
|
||||
ErrorStatus DeviceParameters_writePIN(struct PIN* pin)
|
||||
{
|
||||
|
||||
ErrorStatus returnValue = SUCCESS;
|
||||
if (dParam->initialized)
|
||||
{
|
||||
struct PINStorageClass tempPINStorage;
|
||||
tempPINStorage.pin = *pin;
|
||||
// Calculate CRC over preset
|
||||
tempPINStorage.crc = crc32_calculate(0, &tempPINStorage.pin, sizeof(struct PIN));
|
||||
// Put default preset on Cache
|
||||
CachedStorage_writeBlob(dParam->parametersStorage, dParam->PINOffset, &tempPINStorage, sizeof(struct PINStorageClass) / 4);
|
||||
}
|
||||
else
|
||||
{
|
||||
returnValue = ERROR;
|
||||
}
|
||||
return returnValue;
|
||||
}
|
||||
|
||||
|
||||
struct PIDParameters* DeviceParameters_getPIDParameters(void)
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
|
||||
struct PIN* DeviceParameters_getPIN(void)
|
||||
static void DeviceParameters_verifyCRCs(void)
|
||||
{
|
||||
uint32_t tempCRC;
|
||||
if (dParam->initialized)
|
||||
{
|
||||
// PID PARAMETERS CHECK
|
||||
struct PIDParametersStorageClass _tempPIDParameters;
|
||||
struct PIDParametersStorageClass* tempPIDParameters = &_tempPIDParameters;
|
||||
tempPIDParameters = (struct PIDParametersStorageClass*)CachedStorage_readBlob(dParam->parametersStorage, dParam->PIDParametersOffset);
|
||||
|
||||
// Calculate the CRC of the parameters
|
||||
tempCRC = crc32_calculate(0, &tempPIDParameters->pidParameters, sizeof(struct PIDParameters));
|
||||
// Compare CRC
|
||||
if (tempCRC != tempPIDParameters->crc)
|
||||
{
|
||||
Error_postError(ERROR_CRC_PARAMETERS);
|
||||
// CRC do not match
|
||||
LOGGER_ERROR(mainLog, "CRC ERROR at Device Parameters (calculated %X but loaded %X)", (unsigned int)tempCRC, (unsigned int)tempPIDParameters->crc);
|
||||
// Replace corrupt Device parameters with defaults
|
||||
PIDParameters_generateDefaultParameters(&tempPIDParameters->pidParameters);
|
||||
// Write parameters to cache including the CRC (calculated inside write function)
|
||||
DeviceParameters_writePIDParameters(&tempPIDParameters->pidParameters);
|
||||
}
|
||||
|
||||
|
||||
// PIN CHECK
|
||||
struct PINStorageClass _tempPIN;
|
||||
struct PINStorageClass* tempPIN = &_tempPIN;
|
||||
tempPIN = (struct PINStorageClass*)CachedStorage_readBlob(dParam->parametersStorage, dParam->PINOffset);
|
||||
|
||||
// Calculate the CRC of the PIN
|
||||
// Calculate the CRC of the parameters
|
||||
tempCRC = crc32_calculate(0, &tempPIN->pin, sizeof(struct PIN));
|
||||
// Compare CRC
|
||||
if (tempCRC != tempPIN->crc)
|
||||
{
|
||||
Error_postError(ERROR_CRC_PIN);
|
||||
// CRC do not match
|
||||
LOGGER_ERROR(mainLog, "CRC ERROR at Device PIN (calculated %X but loaded %X)", (unsigned int)tempCRC, (unsigned int)tempPIN->crc);
|
||||
// Replace corrupt Device parameters with defaults
|
||||
PIN_generateDefaultPIN(&tempPIN->pin);
|
||||
// Write parameters to cache including the CRC (calculated inside write function)
|
||||
DeviceParameters_writePIN(&tempPIN->pin);
|
||||
}
|
||||
DeviceParameters_saveParameters();
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user