From be67058675da159d5e690e67d2ab333cf61d7eff Mon Sep 17 00:00:00 2001 From: Georg Date: Wed, 24 Jan 2018 15:38:10 +0100 Subject: [PATCH] added function for storing single setting data to eeprom, added reader threshold being stored in eeprom --- .../Chameleon-Mini/Application/Reader14443A.c | 7 +--- Firmware/Chameleon-Mini/Codec/Codec.c | 19 +++++----- Firmware/Chameleon-Mini/Codec/Codec.h | 4 +- Firmware/Chameleon-Mini/Makefile | 2 + Firmware/Chameleon-Mini/Settings.c | 37 +++++++++---------- Firmware/Chameleon-Mini/Settings.h | 28 +++++++++++++- Firmware/Chameleon-Mini/Terminal/Commands.c | 8 ++-- 7 files changed, 62 insertions(+), 43 deletions(-) diff --git a/Firmware/Chameleon-Mini/Application/Reader14443A.c b/Firmware/Chameleon-Mini/Application/Reader14443A.c index 7cea928..36b8e6e 100644 --- a/Firmware/Chameleon-Mini/Application/Reader14443A.c +++ b/Firmware/Chameleon-Mini/Application/Reader14443A.c @@ -10,14 +10,9 @@ #define CHECK_BCC(B) ((B[0] ^ B[1] ^ B[2] ^ B[3]) == B[4]) #define IS_CASCADE_BIT_SET(buf) (buf[0] & 0x04) #define IS_ISO14443A_4_COMPLIANT(buf) (buf[0] & 0x20) -#define MF_CLASSIC_READER_NONCE 0x00000000 #define TRYCOUNT_MAX 16 -#define FLAGS_MASK 0x03 -#define FLAGS_PARITY_OK 0x01 -#define FLAGS_NO_DATA 0x02 - // TODO replace remaining magic numbers static bool Selected = false; @@ -589,7 +584,7 @@ uint16_t Reader14443AAppProcess(uint8_t* Buffer, uint16_t BitCount) uint16_t rVal = Reader14443A_Select(Buffer, BitCount); if (Selected) // we are done finding the threshold { - Thresholds[(ReaderThreshold - CODEC_THRESHOLD_CALIBRATE_MIN) / CODEC_THRESHOLD_CALIBRATE_STEPS] += 1; + Thresholds[(GlobalSettings.ActiveSettingPtr->ReaderThreshold - CODEC_THRESHOLD_CALIBRATE_MIN) / CODEC_THRESHOLD_CALIBRATE_STEPS] += 1; if (TryCount == TRYCOUNT_MAX) { CodecThresholdIncrement(); diff --git a/Firmware/Chameleon-Mini/Codec/Codec.c b/Firmware/Chameleon-Mini/Codec/Codec.c index f9ac2fa..56f2774 100644 --- a/Firmware/Chameleon-Mini/Codec/Codec.c +++ b/Firmware/Chameleon-Mini/Codec/Codec.c @@ -23,7 +23,6 @@ static volatile struct { } ReaderFieldFlags = { false }; uint8_t CodecBuffer[CODEC_BUFFER_SIZE]; -uint16_t ReaderThreshold = 400; // standard value // the following three functions prevent sending data directly after turning on the reader field void CodecReaderFieldStart(void) // DO NOT CALL THIS FUNCTION INSIDE APPLICATION! @@ -82,22 +81,22 @@ bool CodecIsReaderToBeRestarted(void) return false; } -void CodecThresholdSet(uint16_t th) +void CodecThresholdSet(uint16_t th) // threshold has to be saved back to eeprom by the caller, if wanted { - ReaderThreshold = th; + GlobalSettings.ActiveSettingPtr->ReaderThreshold = th; DACB.CH0DATA = th; } -uint16_t CodecThresholdIncrement(void) +uint16_t CodecThresholdIncrement(void) // threshold has to be saved back to eeprom by the caller, if wanted { - ReaderThreshold += CODEC_THRESHOLD_CALIBRATE_STEPS; - DACB.CH0DATA = ReaderThreshold; - return ReaderThreshold; + GlobalSettings.ActiveSettingPtr->ReaderThreshold += CODEC_THRESHOLD_CALIBRATE_STEPS; + DACB.CH0DATA = GlobalSettings.ActiveSettingPtr->ReaderThreshold; + return GlobalSettings.ActiveSettingPtr->ReaderThreshold; } -void CodecThresholdReset(void) +void CodecThresholdReset(void) // threshold has to be saved back to eeprom by the caller, if wanted { - ReaderThreshold = 400; - DACB.CH0DATA = ReaderThreshold; + GlobalSettings.ActiveSettingPtr->ReaderThreshold = DEFAULT_READER_THRESHOLD; + DACB.CH0DATA = GlobalSettings.ActiveSettingPtr->ReaderThreshold; } diff --git a/Firmware/Chameleon-Mini/Codec/Codec.h b/Firmware/Chameleon-Mini/Codec/Codec.h index 54f9e9d..cf9d67d 100644 --- a/Firmware/Chameleon-Mini/Codec/Codec.h +++ b/Firmware/Chameleon-Mini/Codec/Codec.h @@ -13,6 +13,7 @@ #include #include "../Common.h" #include "../Configuration.h" +#include "../Settings.h" #include "ISO14443-2A.h" #include "Reader14443-2A.h" @@ -93,7 +94,6 @@ #define CodecPtrRegister1 (*((volatile uint8_t**) &GPIOR8)) #define CodecPtrRegister2 (*((volatile uint8_t**) &GPIORA)) -extern uint16_t ReaderThreshold; extern uint16_t Reader_FWT; #define FWI2FWT(x) ((uint32_t)(256 * 16 * ((uint32_t)1 << (x))) / (CODEC_CARRIER_FREQ / 1000) + 1) @@ -176,7 +176,7 @@ INLINE void CodecInitCommon(void) DACB.CTRLB = DAC_CHSEL_SINGLE_gc; DACB.CTRLC = DAC_REFSEL_AVCC_gc; DACB.CTRLA = DAC_IDOEN_bm | DAC_ENABLE_bm; - DACB.CH0DATA = ReaderThreshold; // real threshold voltage can be calculated with ch0data * Vref / 0xFFF + DACB.CH0DATA = GlobalSettings.ActiveSettingPtr->ReaderThreshold; // real threshold voltage can be calculated with ch0data * Vref / 0xFFF /* Configure Analog Comparator 0 to detect changes in demodulated reader field */ ACA.AC0MUXCTRL = AC_MUXPOS_DAC_gc | AC_MUXNEG_PIN7_gc; diff --git a/Firmware/Chameleon-Mini/Makefile b/Firmware/Chameleon-Mini/Makefile index 9ac56db..9d3175f 100644 --- a/Firmware/Chameleon-Mini/Makefile +++ b/Firmware/Chameleon-Mini/Makefile @@ -63,6 +63,8 @@ SETTINGS += -DDEFAULT_SETTING=SETTINGS_FIRST #Default pending task timeout SETTINGS += -DDEFAULT_PENDING_TASK_TIMEOUT=50 #* 100ms +#Default reader threshold +SETTINGS += -DDEFAULT_READER_THRESHOLD=400 #Use EEPROM to store settings SETTINGS += -DENABLE_EEPROM_SETTINGS diff --git a/Firmware/Chameleon-Mini/Settings.c b/Firmware/Chameleon-Mini/Settings.c index 6ef2d33..5ea9174 100644 --- a/Firmware/Chameleon-Mini/Settings.c +++ b/Firmware/Chameleon-Mini/Settings.c @@ -25,7 +25,8 @@ SettingsType EEMEM StoredSettings = { .LogMode = DEFAULT_LOG_MODE, .LEDRedFunction = DEFAULT_RED_LED_ACTION, .LEDGreenFunction = DEFAULT_GREEN_LED_ACTION, - .PendingTaskTimeout = DEFAULT_PENDING_TASK_TIMEOUT + .PendingTaskTimeout = DEFAULT_PENDING_TASK_TIMEOUT, + .ReaderThreshold = DEFAULT_READER_THRESHOLD }} }; @@ -39,13 +40,6 @@ void SettingsSave(void) { #endif } -void ActiveSettingNumberSave(void) { -#if ENABLE_EEPROM_SETTINGS - eeprom_update_byte(&StoredSettings.ActiveSettingIdx, GlobalSettings.ActiveSettingIdx); - eeprom_update_word((uint16_t*)&StoredSettings.ActiveSettingPtr, (uint16_t)GlobalSettings.ActiveSettingPtr); -#endif -} - void SettingsCycle(void) { uint8_t i = SETTINGS_COUNT; uint8_t SettingIdx = GlobalSettings.ActiveSettingIdx; @@ -69,22 +63,25 @@ bool SettingsSetActiveById(uint8_t Setting) { /* Break potentially pending timeout task (manual timeout) */ CommandLinePendingTaskBreak(); - /* Store current memory contents permanently */ - MemoryStore(); + if (SettingIdx != GlobalSettings.ActiveSettingIdx) + { + /* Store current memory contents permanently */ + MemoryStore(); - GlobalSettings.ActiveSettingIdx = SettingIdx; - GlobalSettings.ActiveSettingPtr = - &GlobalSettings.Settings[SettingIdx]; + GlobalSettings.ActiveSettingIdx = SettingIdx; + GlobalSettings.ActiveSettingPtr = + &GlobalSettings.Settings[SettingIdx]; - /* Settings have changed. Progress changes through system */ - ConfigurationSetById(GlobalSettings.ActiveSettingPtr->Configuration); - LogSetModeById(GlobalSettings.ActiveSettingPtr->LogMode); + /* Settings have changed. Progress changes through system */ + ConfigurationSetById(GlobalSettings.ActiveSettingPtr->Configuration); + LogSetModeById(GlobalSettings.ActiveSettingPtr->LogMode); - /* Recall new memory contents */ - MemoryRecall(); + /* Recall new memory contents */ + MemoryRecall(); - /* Store new setting number. */ - ActiveSettingNumberSave(); + SETTING_UPDATE(GlobalSettings.ActiveSettingIdx); + SETTING_UPDATE(GlobalSettings.ActiveSettingPtr); + } /* Notify LED. blink according to current setting */ LEDHook(LED_SETTING_CHANGE, LED_BLINK + SettingIdx); diff --git a/Firmware/Chameleon-Mini/Settings.h b/Firmware/Chameleon-Mini/Settings.h index 4d5bd3d..d7bd8de 100644 --- a/Firmware/Chameleon-Mini/Settings.h +++ b/Firmware/Chameleon-Mini/Settings.h @@ -13,6 +13,7 @@ #include "Log.h" #include "LED.h" #include "Memory.h" +#include #define SETTINGS_COUNT (MEMORY_SIZE / MEMORY_SIZE_PER_SETTING) #define SETTINGS_FIRST 1 @@ -29,6 +30,7 @@ typedef struct { LEDHookEnum LEDRedFunction; /// Red LED function for this setting. LEDHookEnum LEDGreenFunction; /// Green LED function for this setting. uint16_t PendingTaskTimeout; /// Timeout for timeout commands for this setting, in multiples of 100 ms. + uint16_t ReaderThreshold; /// Reader threshold } SettingsEntryType; typedef struct { @@ -37,11 +39,33 @@ typedef struct { SettingsEntryType Settings[SETTINGS_COUNT]; } SettingsType; -extern SettingsType GlobalSettings; +extern SettingsType GlobalSettings, StoredSettings; + +INLINE void SettingUpdate(const void * addr, uint16_t size) +{ +#if ENABLE_EEPROM_SETTINGS + uintptr_t EEAddr = (uintptr_t)addr - (uintptr_t)&GlobalSettings + (uintptr_t)&StoredSettings; + switch (size) + { + case 1: + eeprom_update_byte((uint8_t *)EEAddr, *(uint8_t*)addr); + break; + + case 2: + eeprom_update_word((uint16_t *)EEAddr, *(uint16_t*)addr); + break; + + default: + eeprom_update_block((uint8_t*)addr, (uint8_t*)EEAddr, size); + } + +#endif +} + +#define SETTING_UPDATE(x) SettingUpdate(&(x), sizeof(x)) void SettingsLoad(void); void SettingsSave(void); -void ActiveSettingNumberSave(void); void SettingsCycle(void); bool SettingsSetActiveById(uint8_t Setting); diff --git a/Firmware/Chameleon-Mini/Terminal/Commands.c b/Firmware/Chameleon-Mini/Terminal/Commands.c index 5eb60b6..82453ec 100644 --- a/Firmware/Chameleon-Mini/Terminal/Commands.c +++ b/Firmware/Chameleon-Mini/Terminal/Commands.c @@ -42,7 +42,7 @@ CommandStatusIdType CommandSetConfig(char* OutMessage, const char* InParam) ConfigurationGetList(OutMessage, TERMINAL_BUFFER_SIZE); return COMMAND_INFO_OK_WITH_TEXT_ID; } else if (ConfigurationSetByName(InParam)) { - SettingsSave(); + SETTING_UPDATE(GlobalSettings.ActiveSettingPtr->Configuration); return COMMAND_INFO_OK_ID; } else { return COMMAND_ERR_INVALID_PARAM_ID; @@ -572,12 +572,13 @@ CommandStatusIdType CommandSetTimeout(char* OutMessage, const char* InParam) if (!sscanf_P(InParam, PSTR("%5d"), &tmp) || tmp > 600) return COMMAND_ERR_INVALID_PARAM_ID; GlobalSettings.ActiveSettingPtr->PendingTaskTimeout = tmp; + SETTING_UPDATE(GlobalSettings.ActiveSettingPtr->PendingTaskTimeout); return COMMAND_INFO_OK_ID; } CommandStatusIdType CommandGetThreshold(char* OutParam) { - snprintf_P(OutParam, TERMINAL_BUFFER_SIZE, PSTR("%u"), ReaderThreshold); + snprintf_P(OutParam, TERMINAL_BUFFER_SIZE, PSTR("%u"), GlobalSettings.ActiveSettingPtr->ReaderThreshold); return COMMAND_INFO_OK_WITH_TEXT_ID; } @@ -592,7 +593,8 @@ CommandStatusIdType CommandSetThreshold(char* OutMessage, const char* InParam) if (!sscanf_P(InParam, PSTR("%5d"), &tmp) || tmp > CODEC_MAXIMUM_THRESHOLD) return COMMAND_ERR_INVALID_PARAM_ID; DACB.CH0DATA = tmp; - ReaderThreshold = tmp; + GlobalSettings.ActiveSettingPtr->ReaderThreshold = tmp; + SETTING_UPDATE(GlobalSettings.ActiveSettingPtr->ReaderThreshold); return COMMAND_INFO_OK_ID; }