2016-10-07 23:44:50 +02:00
|
|
|
/*
|
|
|
|
|
* CODEC.c
|
|
|
|
|
*
|
|
|
|
|
* Created on: 18.02.2013
|
|
|
|
|
* Author: skuser
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
#include "Codec.h"
|
|
|
|
|
#include "../System.h"
|
|
|
|
|
#include "../LEDHook.h"
|
|
|
|
|
|
2017-04-04 16:18:10 +02:00
|
|
|
uint16_t Reader_FWT = ISO14443A_RX_PENDING_TIMEOUT;
|
|
|
|
|
|
2016-10-07 23:44:50 +02:00
|
|
|
#define READER_FIELD_MINIMUM_WAITING_TIME 70 // ms
|
|
|
|
|
|
|
|
|
|
static uint16_t ReaderFieldStartTimestamp = 0;
|
2017-04-04 16:26:09 +02:00
|
|
|
static uint16_t ReaderFieldRestartTimestamp = 0;
|
|
|
|
|
static uint16_t ReaderFieldRestartDelay = 0;
|
2016-10-07 23:44:50 +02:00
|
|
|
static volatile struct {
|
2017-11-07 15:53:23 +01:00
|
|
|
bool Started;
|
|
|
|
|
bool Ready;
|
|
|
|
|
bool ToBeRestarted;
|
2016-10-07 23:44:50 +02:00
|
|
|
} ReaderFieldFlags = { false };
|
|
|
|
|
|
|
|
|
|
uint8_t CodecBuffer[CODEC_BUFFER_SIZE];
|
2018-07-14 18:52:48 +01:00
|
|
|
uint8_t CodecBuffer2[CODEC_BUFFER_SIZE];
|
2020-08-26 20:19:33 +02:00
|
|
|
|
|
|
|
|
void (* volatile isr_func_CODEC_TIMER_LOADMOD_CCB_VECT)(void) = NULL;
|
|
|
|
|
void (* volatile isr_func_TCD0_CCC_vect)(void) = NULL;
|
|
|
|
|
void (* volatile isr_func_CODEC_DEMOD_IN_INT0_VECT)(void) = NULL;
|
|
|
|
|
|
2016-10-07 23:44:50 +02:00
|
|
|
// the following three functions prevent sending data directly after turning on the reader field
|
2019-11-15 10:21:40 +01:00
|
|
|
void CodecReaderFieldStart(void) { // DO NOT CALL THIS FUNCTION INSIDE APPLICATION!
|
|
|
|
|
if (!CodecGetReaderField() && !ReaderFieldFlags.ToBeRestarted) {
|
2017-11-07 15:53:23 +01:00
|
|
|
CodecSetReaderField(true);
|
|
|
|
|
ReaderFieldFlags.Started = true;
|
|
|
|
|
ReaderFieldFlags.Ready = false;
|
|
|
|
|
ReaderFieldStartTimestamp = SystemGetSysTick();
|
|
|
|
|
}
|
2016-10-07 23:44:50 +02:00
|
|
|
}
|
|
|
|
|
|
2019-11-15 10:21:40 +01:00
|
|
|
void CodecReaderFieldStop(void) {
|
2017-11-07 15:53:23 +01:00
|
|
|
CodecSetReaderField(false);
|
|
|
|
|
ReaderFieldFlags.Started = false;
|
|
|
|
|
ReaderFieldFlags.Ready = false;
|
2016-10-07 23:44:50 +02:00
|
|
|
}
|
|
|
|
|
|
2019-11-15 10:21:40 +01:00
|
|
|
void CodecReaderFieldRestart(uint16_t delay) {
|
2017-11-07 15:53:23 +01:00
|
|
|
ReaderFieldFlags.ToBeRestarted = true;
|
|
|
|
|
ReaderFieldRestartTimestamp = SystemGetSysTick();
|
|
|
|
|
ReaderFieldRestartDelay = delay;
|
|
|
|
|
CodecReaderFieldStop();
|
2017-04-04 16:26:09 +02:00
|
|
|
}
|
|
|
|
|
|
2016-10-07 23:44:50 +02:00
|
|
|
/*
|
|
|
|
|
* This function returns false if and only if the reader field has been turned on in the last READER_FIELD_MIN..._TIME ms.
|
|
|
|
|
* If the reader field is not active, true is returned.
|
|
|
|
|
*/
|
2019-11-15 10:21:40 +01:00
|
|
|
bool CodecIsReaderFieldReady(void) {
|
2017-11-07 15:53:23 +01:00
|
|
|
if (!ReaderFieldFlags.Started)
|
|
|
|
|
return true;
|
2019-11-15 10:21:40 +01:00
|
|
|
if (ReaderFieldFlags.Ready || (ReaderFieldFlags.Started && SYSTICK_DIFF(ReaderFieldStartTimestamp) >= READER_FIELD_MINIMUM_WAITING_TIME)) {
|
2017-11-07 15:53:23 +01:00
|
|
|
ReaderFieldFlags.Ready = true;
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
return false;
|
2016-10-07 23:44:50 +02:00
|
|
|
}
|
2017-04-04 16:26:09 +02:00
|
|
|
|
2019-11-15 10:21:40 +01:00
|
|
|
bool CodecIsReaderToBeRestarted(void) {
|
|
|
|
|
if (ReaderFieldFlags.ToBeRestarted) {
|
|
|
|
|
if (SYSTICK_DIFF(ReaderFieldRestartTimestamp) >= ReaderFieldRestartDelay) {
|
2017-11-07 15:53:23 +01:00
|
|
|
ReaderFieldFlags.ToBeRestarted = false;
|
|
|
|
|
CodecReaderFieldStart();
|
|
|
|
|
}
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
return false;
|
2017-04-04 16:26:09 +02:00
|
|
|
}
|
|
|
|
|
|
2019-11-15 10:21:40 +01:00
|
|
|
void CodecThresholdSet(uint16_t th) { // threshold has to be saved back to eeprom by the caller, if wanted
|
2018-01-24 15:38:10 +01:00
|
|
|
GlobalSettings.ActiveSettingPtr->ReaderThreshold = th;
|
2017-07-12 10:52:05 +02:00
|
|
|
DACB.CH0DATA = th;
|
|
|
|
|
}
|
|
|
|
|
|
2019-11-15 10:21:40 +01:00
|
|
|
uint16_t CodecThresholdIncrement(void) { // threshold has to be saved back to eeprom by the caller, if wanted
|
2018-01-24 15:38:10 +01:00
|
|
|
GlobalSettings.ActiveSettingPtr->ReaderThreshold += CODEC_THRESHOLD_CALIBRATE_STEPS;
|
|
|
|
|
DACB.CH0DATA = GlobalSettings.ActiveSettingPtr->ReaderThreshold;
|
|
|
|
|
return GlobalSettings.ActiveSettingPtr->ReaderThreshold;
|
2017-07-12 10:52:05 +02:00
|
|
|
}
|
|
|
|
|
|
2019-11-15 10:21:40 +01:00
|
|
|
void CodecThresholdReset(void) { // threshold has to be saved back to eeprom by the caller, if wanted
|
2018-01-24 15:38:10 +01:00
|
|
|
GlobalSettings.ActiveSettingPtr->ReaderThreshold = DEFAULT_READER_THRESHOLD;
|
|
|
|
|
DACB.CH0DATA = GlobalSettings.ActiveSettingPtr->ReaderThreshold;
|
2017-07-12 10:52:05 +02:00
|
|
|
}
|
|
|
|
|
|