Skip to content

Commit 821de78

Browse files
committed
SniffISO15693: Add functions to codec to enable autocalibration
Signed-off-by: cacke-r <[email protected]>
1 parent 97d7ad8 commit 821de78

File tree

2 files changed

+64
-10
lines changed

2 files changed

+64
-10
lines changed

Firmware/Chameleon-Mini/Codec/SniffISO15693.c

+52-10
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@ static volatile uint8_t ReaderByteCount;
5252
static volatile uint8_t CardByteCount;
5353
static volatile uint16_t SampleDataCount;
5454
static volatile uint8_t bBrokenFrame;
55+
static bool bAutoThreshold = true;
5556

5657
/////////////////////////////////////////////////
5758
// VCD->VICC
@@ -272,6 +273,20 @@ INLINE void SNIFF_ISO15693_READER_EOC_VCD(void) {
272273
// VICC->VCD
273274
/////////////////////////////////////////////////
274275

276+
277+
/* Inline function to set the threshold, if bAutoThreshold is enabled*/
278+
INLINE void SetDacCh0Data(uint16_t ch0data){
279+
/* TODO: Check if either of the branches can be avoided */
280+
/* to optimize the performance */
281+
if(bAutoThreshold){
282+
if (ch0data < 0xFFF)
283+
DACB.CH0DATA = ch0data;
284+
else
285+
DACB.CH0DATA = 0xFFF;
286+
}
287+
return;
288+
}
289+
275290
/* Initialize card sniffing options
276291
Note: Currently implemented only single subcarrier SOC detection
277292
*/
@@ -394,8 +409,8 @@ INLINE void CardSniffInit(void) {
394409
ADCA.EVCTRL = ADC_SWEEP_01_gc;
395410

396411
/* Write threshold to the DAC channel 0 (connected to Analog Comparator positive input) */
397-
DACB.CH0DATA = DemodFloorNoiseLevel + (DemodFloorNoiseLevel >> 3); /* Slightly increase DAC output to ease triggering */
398-
DACB.CH0DATA |= -( (DACB.CH0DATA & 0xFFF) < DemodFloorNoiseLevel); /* Branchfree saturating addition */
412+
SetDacCh0Data(DemodFloorNoiseLevel + (DemodFloorNoiseLevel >> 3)); /* Slightly increase DAC output to ease triggering */
413+
399414

400415
/**
401416
* Finally, now that we have the DAC set up, configure analog comparator A (the only one in this MCU)
@@ -449,8 +464,8 @@ ISR_SHARED isr_SNIFF_ISO15693_ACA_AC0_VECT(void) {
449464

450465
ACA.AC0CTRL = AC_ENABLE_bm | AC_HSMODE_bm | AC_HYSMODE_LARGE_gc | AC_INTMODE_RISING_gc | AC_INTLVL_OFF_gc; /* Disable this interrupt */
451466

452-
DACB.CH0DATA = DemodFloorNoiseLevel + (DemodFloorNoiseLevel >> 2); /* Blindly increase threshold after 1 pulse */
453-
DACB.CH0DATA |= -( (DACB.CH0DATA & 0xFFF) < DemodFloorNoiseLevel); /* Branchfree saturating addition */
467+
SetDacCh0Data(DemodFloorNoiseLevel + (DemodFloorNoiseLevel >> 2)); /* Blindly increase threshold after 1 pulse */
468+
454469
/* Note: by the time the DAC has changed its output, we're already after the 2nd pulse */
455470
}
456471

@@ -460,9 +475,8 @@ ISR_SHARED isr_SNIFF_ISO15693_ACA_AC0_VECT(void) {
460475
ISR_SHARED isr_SNIFF_ISO15693_CODEC_TIMER_TIMESTAMPS_CCA_VECT(void) {
461476

462477
uint16_t TempRead = ADCA.CH1RES; /* Can't possibly be greater than 0xFFF since the dac has 12 bit res */
463-
DACB.CH0DATA = TempRead - ANTENNA_LEVEL_OFFSET; /* Further increase DAC output after 3 pulses with value from PORTA Pin 2 (DEMOD-READER/2.3) */
464-
DACB.CH0DATA &= -(DACB.CH0DATA <= TempRead); /* Branchfree saturating subtraction */
465478

479+
SetDacCh0Data(TempRead - ANTENNA_LEVEL_OFFSET); /* Further increase DAC output after 3 pulses with value from PORTA Pin 2 (DEMOD-READER/2.3) */
466480
CODEC_TIMER_TIMESTAMPS.INTCTRLB = TC_CCAINTLVL_OFF_gc; /* Disable this interrupt */
467481
}
468482

@@ -480,8 +494,8 @@ ISR_SHARED isr_SNIFF_ISO15693_CODEC_TIMER_LOADMOD_CCA_VECT(void) {
480494

481495
CODEC_TIMER_LOADMOD.INTCTRLB = TC_CCAINTLVL_OFF_gc; /* Disable all compare interrupts, including this one */
482496

483-
DACB.CH0DATA = DemodFloorNoiseLevel + (DemodFloorNoiseLevel >> 3); /* Restore DAC output (AC negative comparation threshold) to pre-AC0 interrupt update value */
484-
DACB.CH0DATA |= -( (DACB.CH0DATA & 0xFFF) < DemodFloorNoiseLevel); /* Branchfree saturating addition */
497+
SetDacCh0Data(DemodFloorNoiseLevel + (DemodFloorNoiseLevel >> 3)); /* Restore DAC output (AC negative comparation threshold) to pre-AC0 interrupt update value */
498+
485499
ACA.AC0CTRL |= AC_INTLVL_HI_gc; /* Re-enable analog comparator interrupt to search for another pulse */
486500

487501
CODEC_TIMER_TIMESTAMPS.INTCTRLB = TC_CCAINTLVL_HI_gc; /* Re enable CCA interrupt in case it was triggered and then is now disabled */
@@ -564,8 +578,8 @@ ISR_SHARED isr_SNIFF_ISO15693_CODEC_TIMER_LOADMOD_OVF_VECT(void) {
564578
/* No SOC. The train of pulses was actually garbage. */
565579

566580
/* Restore DAC output to intial value */
567-
DACB.CH0DATA = DemodFloorNoiseLevel + (DemodFloorNoiseLevel >> 3);
568-
DACB.CH0DATA |= -( (DACB.CH0DATA & 0xFFF) < DemodFloorNoiseLevel); /* Branchfree saturating addition */
581+
SetDacCh0Data(DemodFloorNoiseLevel + (DemodFloorNoiseLevel >> 3));
582+
569583

570584
/* Re enable AC interrupt */
571585
ACA.AC0CTRL = AC_ENABLE_bm | AC_HSMODE_bm | AC_HYSMODE_LARGE_gc | AC_INTMODE_RISING_gc | AC_INTLVL_HI_gc;
@@ -692,6 +706,34 @@ void SniffISO15693CodecInit(void) {
692706
ReaderSniffInit();
693707
}
694708

709+
/************************************************************
710+
Function used by the Terminal command to display (GET)
711+
the state of the Autothreshold Feature.
712+
************************************************************/
713+
bool SniffISO15693GetAutoThreshold(void){
714+
return bAutoThreshold;
715+
}
716+
717+
/************************************************************
718+
Function used by the Application level to disable the
719+
Autothreshold Feature.
720+
If it is disabled: The threshold will be taken from
721+
GlobalSettings.ActiveSettingPtr->ReaderThreshold
722+
************************************************************/
723+
void SniffISO15693CtrlAutoThreshold(bool enable){
724+
bAutoThreshold = enable;
725+
return;
726+
}
727+
728+
/************************************************************
729+
Function used by the Application level to get the FloorNoise
730+
FloorNoise can be used to define the scanning range for the
731+
Autocalibration.
732+
************************************************************/
733+
uint16_t SniffISO15693GetFloorNoise(void){
734+
return DemodFloorNoiseLevel;
735+
}
736+
695737
void SniffISO15693CodecDeInit(void) {
696738
/* Gracefully shutdown codec */
697739
CODEC_DEMOD_IN_PORT.INT0MASK = 0;

Firmware/Chameleon-Mini/Codec/SniffISO15693.h

+12
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,18 @@ void SniffISO15693CodecTask(void);
3232
/* Application Interface */
3333
void SniffISO15693CodecStart(void);
3434
void SniffISO15693CodecReset(void);
35+
/* Can be used by an application to disable */
36+
/* The auto-threshold algorithm */
37+
/* This is needed in corner cases - where the algo doesnt */
38+
/* find a proper threshold */
39+
/* In this case, the application can run an autocalibration session */
40+
void SniffISO15693CtrlAutoThreshold(bool enable);
41+
bool SniffISO15693GetAutoThreshold(void);
42+
43+
/* Function is used to receive the measured FloorNoise */
44+
/* This is used to narrow down the range, which is considered for */
45+
/* Threshold */
46+
uint16_t SniffISO15693GetFloorNoise(void);
3547

3648
/* Internal functions */
3749
INLINE void SNIFF_ISO15693_READER_EOC_VCD(void);

0 commit comments

Comments
 (0)