mirror of
https://github.com/RfidResearchGroup/ChameleonMini.git
synced 2026-05-12 11:20:37 -07:00
added function for storing single setting data to eeprom, added reader threshold being stored in eeprom
This commit is contained in:
@@ -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();
|
||||
|
||||
@@ -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;
|
||||
}
|
||||
|
||||
|
||||
@@ -13,6 +13,7 @@
|
||||
#include <stdbool.h>
|
||||
#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;
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -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);
|
||||
|
||||
@@ -13,6 +13,7 @@
|
||||
#include "Log.h"
|
||||
#include "LED.h"
|
||||
#include "Memory.h"
|
||||
#include <avr/eeprom.h>
|
||||
|
||||
#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);
|
||||
|
||||
@@ -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;
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user