diff --git a/openrtx/include/settings.h b/openrtx/include/settings.h index c6d6db9b..b72b7038 100644 --- a/openrtx/include/settings.h +++ b/openrtx/include/settings.h @@ -50,9 +50,9 @@ typedef struct uint8_t contrast; // Display contrast uint8_t sqlLevel; // Squelch level uint8_t voxLevel; // Vox level - int8_t utc_timezone; // Timezone - bool gps_enabled; // GPS active - char callsign[10]; // Plaintext callsign, for future use + int8_t utc_timezone; // Timezone + bool gps_enabled; // GPS active + char callsign[10]; // Plaintext callsign, for future use uint8_t display_timer : 4, // Standby timer not_in_use : 4; } diff --git a/platform/drivers/NVM/nvmem_settings_MDx.c b/platform/drivers/NVM/nvmem_settings_MDx.c index a830b62f..f085b42e 100644 --- a/platform/drivers/NVM/nvmem_settings_MDx.c +++ b/platform/drivers/NVM/nvmem_settings_MDx.c @@ -23,7 +23,10 @@ #include #include "flash.h" - +/* + * Data structure defining the memory layout used for saving and restore + * of user settings and VFO configuration. + */ typedef struct { uint32_t magic; @@ -37,7 +40,10 @@ typedef struct } __attribute__((packed)) memory_t; -static const uint32_t validMagic = 0x5854524F; // "ORTX" +// Legacy magic number, from previous versions +static const uint32_t legacyMagic = 0x5854524F; // "ORTX" + +static const uint32_t validMagic = 0x5854504F; // "OPTX" static const uint32_t baseAddress = 0x080E0000; memory_t *memory = ((memory_t *) baseAddress); @@ -45,13 +51,17 @@ memory_t *memory = ((memory_t *) baseAddress); /** * \internal * Utility function to find the currently active data block inside memory, that - * is the one containing the last saved settings. + * is the one containing the last saved settings. Blocks containing legacy data + * are marked with numbers starting from 4096. * * @return number currently active data block or -1 if memory data is invalid. */ int findActiveBlock() { - if(memory->magic != validMagic) return -1; + if((memory->magic != validMagic) && (memory->magic != legacyMagic)) + { + return -1; // Invalid memory data + } uint16_t block = 0; for(; block < 64; block++) @@ -72,6 +82,10 @@ int findActiveBlock() } block = (block * 32) + bit; + + // Mark block as containing legacy data by adding 4096. + if(memory->magic == legacyMagic) block += 4096; + return block - 1; } @@ -80,16 +94,45 @@ int findActiveBlock() int nvm_readVFOChannelData(channel_t *channel) { int block = findActiveBlock(); + + // Invalid data found if(block < 0) return -1; - memcpy(channel, &(memory->data[block].vfoData), sizeof(channel_t)); + + // Try loading data, if "block" is greater than 2048 the pointer will be + // overwritten below without harm. + uint8_t *ptr = ((uint8_t *) &(memory->data[block].vfoData)); + + // Check if we have to load legacy data. + if(block > 2048) + { + block -= 4096; + ptr = ((uint8_t *) &(memory->data[block].vfoData)); + ptr -= 1; + } + + memcpy(channel, ptr, sizeof(channel_t)); return 0; } int nvm_readSettings(settings_t *settings) { int block = findActiveBlock(); + + // Invalid data found if(block < 0) return -1; - memcpy(settings, &(memory->data[block].settings), sizeof(settings_t)); + + // Check if we have to load legacy data. + if(block > 2048) + { + block -= 4096; + memcpy(settings, &(memory->data[block].settings), sizeof(settings_t)); + settings->display_timer = TIMER_30S; + } + else + { + memcpy(settings, &(memory->data[block].settings), sizeof(settings_t)); + } + return 0; }