X-Git-Url: http://git.linex4red.de/pub/lufa.git/blobdiff_plain/de0df8b16259458c78c742b5dbedd4ac90856285..6da32f7dc0eea65f4ea5d0d1641b42436d1c61ed:/Projects/AVRISP-MKII/Lib/V2ProtocolParams.c diff --git a/Projects/AVRISP-MKII/Lib/V2ProtocolParams.c b/Projects/AVRISP-MKII/Lib/V2ProtocolParams.c index 135521adf..c42c6c4bb 100644 --- a/Projects/AVRISP-MKII/Lib/V2ProtocolParams.c +++ b/Projects/AVRISP-MKII/Lib/V2ProtocolParams.c @@ -1,13 +1,13 @@ /* LUFA Library - Copyright (C) Dean Camera, 2013. + Copyright (C) Dean Camera, 2021. dean [at] fourwalledcubicle [dot] com www.lufa-lib.org */ /* - Copyright 2013 Dean Camera (dean [at] fourwalledcubicle [dot] com) + Copyright 2021 Dean Camera (dean [at] fourwalledcubicle [dot] com) Permission to use, copy, modify, distribute, and sell this software and its documentation for any purpose is hereby granted @@ -39,6 +39,17 @@ /* Non-Volatile Parameter Values for EEPROM storage */ static uint8_t EEMEM EEPROM_Reset_Polarity = 0x01; +/* Non-Volatile Parameter Values for EEPROM storage */ +static uint8_t EEMEM EEPROM_SCK_Duration = 0x06; + +/* Randomly generated magic constant for EEPROM data validation */ +#define EEPROM_MAGIC 0xEB401412UL + +/* EEPROM location which should contain EEPROM_MAGIC. If this value is not correct, all other EEPROM locations are + * assumed to contain invalid data, therefore all parameters will be reset to their default values. + */ +static uint32_t EEMEM EEPROM_Magic_Value = EEPROM_MAGIC; + /* Volatile Parameter Values for RAM storage */ static ParameterItem_t ParameterTable[] = { @@ -84,11 +95,40 @@ static ParameterItem_t ParameterTable[] = }; +/** Check the non-volatile storage in the EEPROM for the proper magic value, and erase the parameter area if the magic + * value was not correct. + */ +static void V2Params_CheckNonVolatileStorage(void) +{ + /* Read the magic value from the EEPROM */ + uint32_t MagicValue = eeprom_read_dword(&EEPROM_Magic_Value); + if (MagicValue != EEPROM_MAGIC) { + /* If the magic value was not correct, erase the parameter values in the EEPROM */ + eeprom_update_byte(&EEPROM_Reset_Polarity, 0xFF); + eeprom_update_byte(&EEPROM_SCK_Duration, 0xFF); + + /* Write the correct magic value to confirm that the data stored in the EEPROM is now valid */ + eeprom_update_dword(&EEPROM_Magic_Value, EEPROM_MAGIC); + } +} + /** Loads saved non-volatile parameter values from the EEPROM into the parameter table, as needed. */ void V2Params_LoadNonVolatileParamValues(void) { - /* Target RESET line polarity is a non-volatile value, retrieve current parameter value from EEPROM */ - V2Params_GetParamFromTable(PARAM_RESET_POLARITY)->ParamValue = eeprom_read_byte(&EEPROM_Reset_Polarity); + /* Check that the EEPROM contains valid data */ + V2Params_CheckNonVolatileStorage(); + + /* Read parameter values that are stored in non-volatile EEPROM */ + uint8_t ResetPolarity = eeprom_read_byte(&EEPROM_Reset_Polarity); + uint8_t SCKDuration = eeprom_read_byte(&EEPROM_SCK_Duration); + + /* Update current parameter table if the EEPROM contents was not blank */ + if (ResetPolarity != 0xFF) + V2Params_GetParamFromTable(PARAM_RESET_POLARITY)->ParamValue = ResetPolarity; + + /* Update current parameter table if the EEPROM contents was not blank */ + if (SCKDuration != 0xFF) + V2Params_GetParamFromTable(PARAM_SCK_DURATION)->ParamValue = SCKDuration; } /** Updates any parameter values that are sourced from hardware rather than explicitly set by the host, such as @@ -112,7 +152,7 @@ void V2Params_UpdateParamValues(void) */ uint8_t V2Params_GetParameterPrivileges(const uint8_t ParamID) { - ParameterItem_t* ParamInfo = V2Params_GetParamFromTable(ParamID); + ParameterItem_t* const ParamInfo = V2Params_GetParamFromTable(ParamID); if (ParamInfo == NULL) return 0; @@ -132,7 +172,7 @@ uint8_t V2Params_GetParameterPrivileges(const uint8_t ParamID) */ uint8_t V2Params_GetParameterValue(const uint8_t ParamID) { - ParameterItem_t* ParamInfo = V2Params_GetParamFromTable(ParamID); + ParameterItem_t* const ParamInfo = V2Params_GetParamFromTable(ParamID); if (ParamInfo == NULL) return 0; @@ -154,7 +194,7 @@ uint8_t V2Params_GetParameterValue(const uint8_t ParamID) void V2Params_SetParameterValue(const uint8_t ParamID, const uint8_t Value) { - ParameterItem_t* ParamInfo = V2Params_GetParamFromTable(ParamID); + ParameterItem_t* const ParamInfo = V2Params_GetParamFromTable(ParamID); if (ParamInfo == NULL) return; @@ -164,6 +204,10 @@ void V2Params_SetParameterValue(const uint8_t ParamID, /* The target RESET line polarity is a non-volatile parameter, save to EEPROM when changed */ if (ParamID == PARAM_RESET_POLARITY) eeprom_update_byte(&EEPROM_Reset_Polarity, Value); + + /* The target SCK line period is a non-volatile parameter, save to EEPROM when changed */ + if (ParamID == PARAM_SCK_DURATION) + eeprom_update_byte(&EEPROM_SCK_Duration, Value); } /** Retrieves a parameter entry (including ID, value and privileges) from the parameter table that matches the given @@ -173,7 +217,7 @@ void V2Params_SetParameterValue(const uint8_t ParamID, * * \return Pointer to the associated parameter information from the parameter table if found, NULL otherwise */ -static ParameterItem_t* V2Params_GetParamFromTable(const uint8_t ParamID) +static ParameterItem_t* const V2Params_GetParamFromTable(const uint8_t ParamID) { ParameterItem_t* CurrTableItem = ParameterTable;