lf cotag sim: COTAG simulation improvements
- Implemented passive mode for COTAG sim:
- load modulation with an fc/2 subcarrier locked to the reader carrier.
- This mode is now a default as it has several advantages:
- phase coherent response,
- can detect reader field drop mid frame "transmission" and restart
immediately -- important with some readers that may drop field
early and send reactivation shortly afterwards expecting the
tag to respond there as well.
- "active tx mode" is now via CLI toggle --active: it may give better range,
but may not work with all readers.
- Improved COTAG activation sequence detection for a wider range of COTAG
readers that may vary slightly in activation sequence timings (thresholds
for the rise-to-rise mode).
This commit is contained in:
1 parent
3810eee3e0
commit
93631d06d7
3 files changed
+153
-22
No files matched your search
+137
-14
@@ -2992,16 +2992,18 @@ typedef struct {
|
||||
static bool cotag_activation_sequence_detect(const lf_cotagsim_t *params, cotag_detect_val_t *out) {
|
||||
|
||||
// Reader's tag activation sequence:
|
||||
// - ON 800 us, OFF 2200 us
|
||||
// - ON 800 us, OFF 2200 us
|
||||
// - ON ~5800 us (the tag is expected to respond in this window)
|
||||
// - OFF ~16500 us (reader's pause if no tag detected, then repeat)
|
||||
//
|
||||
// We detect 2 rising edges between ON(800),OFF(2200),ON(5800), i.e. approx 3000us apart.
|
||||
// Another reader activation sequence identified as: ON(1500),OFF(1900),ON(Z)
|
||||
//
|
||||
// We detect 2 rising edges delta between ON(X),OFF(Y),ON(Z).
|
||||
// Plus minus some tolerance, thus win_lo and win_hi.
|
||||
|
||||
// Number of samples between 2 rising edges to look for an activation sequence
|
||||
const uint32_t win_lo = (3000 - 300) * 12 / (LF_COTAG_DIVISOR + 1);
|
||||
const uint32_t win_hi = (3000 + 300) * 12 / (LF_COTAG_DIVISOR + 1);
|
||||
// Number of samples between 2 rising edges to look for an activation sequence:
|
||||
const uint32_t win_lo = 2700 * 12 / (LF_COTAG_DIVISOR + 1);
|
||||
const uint32_t win_hi = 3700 * 12 / (LF_COTAG_DIVISOR + 1);
|
||||
|
||||
// Samples ring buffer
|
||||
uint8_t ring[2 * COTAG_STEP_K] = {0};
|
||||
@@ -3076,9 +3078,21 @@ static bool cotag_activation_sequence_detect(const lf_cotagsim_t *params, cotag_
|
||||
}
|
||||
}
|
||||
|
||||
#define COTAG_TX_HALFBIT_CYCLES (384 / 2) // Number of samples per half bit
|
||||
#define COTAG_TX_SYNC_HALFBITS 4 // two leading sync 0 bits
|
||||
#define COTAG_TX_MAX_HALFBITS (COTAG_TX_SYNC_HALFBITS + 2 * 8 * LF_COTAG_SIM_DATA_LEN)
|
||||
#define COTAG_TX_HALFBIT_CARRIERS 384 // reader carrier cycles per half bit
|
||||
#define COTAG_TX_HALFBIT_SAMPLES (COTAG_TX_HALFBIT_CARRIERS / 2) // samples per half bit at the 66 kHz TX divisor
|
||||
|
||||
#define COTAG_TX_SYNC_HALFBITS 4 // two leading sync 0 bits
|
||||
#define COTAG_TX_MAX_HALFBITS (COTAG_TX_SYNC_HALFBITS + 2 * 8 * LF_COTAG_SIM_DATA_LEN)
|
||||
|
||||
#define COTAG_CARRIER_TIMEOUT 17 // precision counter ticks (1.5 MHz) for approx 1.5 carrier periods at 132 kHz
|
||||
#define COTAG_FIELD_LOST 64 // consecutive missed carrier edges threshold for determining the reader field is gone
|
||||
|
||||
// a passive answer, for debug output
|
||||
typedef struct {
|
||||
uint32_t missed_edges; // carrier edges missed, replaced by the timeout
|
||||
uint16_t nhb_sent; // num half bits sent
|
||||
bool field_lost; // stopped because the reader field went away
|
||||
} cotag_tx_val_t;
|
||||
|
||||
#define COTAG_SSC_STALL 100000
|
||||
#define COTAG_SETTLE_US 10000
|
||||
@@ -3117,8 +3131,10 @@ static uint16_t cotag_build_halfbits(uint8_t *half_bits, const uint8_t *data, ui
|
||||
return nhb;
|
||||
}
|
||||
|
||||
// Send COTAG frame's half bits. Return false on button press, USB data, or error.
|
||||
static bool cotag_tx_frame(const uint8_t *half_bits, uint16_t nhb, uint8_t repeats, uint16_t hb_delay_us) {
|
||||
// Send COTAG frame's half bits with "active tx".
|
||||
// May give better range but may not work with all types of readers.
|
||||
// Return false on button press, USB data, or error.
|
||||
static bool cotag_tx_frame_active(const uint8_t *half_bits, uint16_t nhb, uint8_t repeats, uint16_t hb_delay_us) {
|
||||
|
||||
Gpio_SSC_DOUT_High();
|
||||
FpgaSendCommand(FPGA_CMD_SET_DIVISOR, LF_COTAG_TX_DIVISOR);
|
||||
@@ -3136,7 +3152,7 @@ static bool cotag_tx_frame(const uint8_t *half_bits, uint16_t nhb, uint8_t repea
|
||||
|
||||
if (hb_delay_us) {
|
||||
SpinDelayUsPrecision(hb_delay_us);
|
||||
} else if (cotag_skip_samples(COTAG_TX_HALFBIT_CYCLES) == false) {
|
||||
} else if (cotag_skip_samples(COTAG_TX_HALFBIT_SAMPLES) == false) {
|
||||
ok = false;
|
||||
break;
|
||||
}
|
||||
@@ -3160,6 +3176,89 @@ static bool cotag_tx_frame(const uint8_t *half_bits, uint16_t nhb, uint8_t repea
|
||||
return ok;
|
||||
}
|
||||
|
||||
// Wait for the next rising edge of cross_lo, one reader carrier cycle after t_last_edge.
|
||||
// Return false on timeout: reader field gone or the edge was missed (e.g. hidden by our own load).
|
||||
static bool cotag_wait_carrier(uint16_t t_last_edge) {
|
||||
while (Gpio_SSC_CLK_Read()) {
|
||||
if (GetPrecisionCounterDelta(t_last_edge) > COTAG_CARRIER_TIMEOUT) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
while (Gpio_SSC_CLK_Read() == false) {
|
||||
if (GetPrecisionCounterDelta(t_last_edge) > COTAG_CARRIER_TIMEOUT) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
// Send COTAG frame's half bits, load modulating with an fc/2 subcarrier clocked by the
|
||||
// reader's carrier (cross_lo): subcarrier on for a high half bit, off for a low one.
|
||||
//
|
||||
// If reader field is gone before all half bits are sent, true is returned and tx_val->field_lost is set.
|
||||
// Return false on button press or USB data.
|
||||
static bool cotag_tx_frame_passive(const uint8_t *half_bits, uint16_t nhb, uint8_t repeats, cotag_tx_val_t *tx_val) {
|
||||
Gpio_SSC_DOUT_Low();
|
||||
FpgaWriteConfWord(FPGA_MAJOR_MODE_LF_EDGE_DETECT);
|
||||
gpio_fpga_mod_feedback_setup();
|
||||
|
||||
bool ok = true;
|
||||
uint8_t lost = 0;
|
||||
uint16_t t_last_edge = GetPrecisionCounterRaw();
|
||||
|
||||
for (uint8_t r = 0; r < repeats && ok; r++) {
|
||||
// Send precomputed half bits. Leading sync bits are sent only at first repetition
|
||||
for (uint16_t i = (r == 0) ? 0 : COTAG_TX_SYNC_HALFBITS; i < nhb; i++) {
|
||||
|
||||
// Modulate the current half bit:
|
||||
for (uint16_t c = 0; c < COTAG_TX_HALFBIT_CARRIERS; c++) {
|
||||
// Determine the level before the edge, so that we write it right after it:
|
||||
// - half bit high: toggle the load every carrier cycle (fc/2)
|
||||
// - half bit low: unloaded for the whole half bit
|
||||
bool loaded = half_bits[i] && (c & 1);
|
||||
|
||||
bool edge = cotag_wait_carrier(t_last_edge);
|
||||
if (loaded) {
|
||||
Gpio_SSC_DOUT_High();
|
||||
} else {
|
||||
Gpio_SSC_DOUT_Low();
|
||||
}
|
||||
t_last_edge = GetPrecisionCounterRaw();
|
||||
|
||||
if (edge) {
|
||||
lost = 0;
|
||||
} else {
|
||||
tx_val->missed_edges++;
|
||||
if (++lost >= COTAG_FIELD_LOST) {
|
||||
tx_val->field_lost = true;
|
||||
// reader field gone: back to listening for the next activation
|
||||
goto out;
|
||||
}
|
||||
}
|
||||
}
|
||||
tx_val->nhb_sent++;
|
||||
|
||||
if (BUTTON_PRESS()) {
|
||||
ok = false;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
WDT_HIT();
|
||||
if (data_available()) {
|
||||
ok = false;
|
||||
}
|
||||
}
|
||||
|
||||
out:
|
||||
Gpio_SSC_DOUT_Low();
|
||||
FpgaWriteConfWord(FPGA_MAJOR_MODE_LF_ADC);
|
||||
FpgaSetupSsc(FPGA_MAJOR_MODE_LF_READER);
|
||||
gpio_fpga_mod_only_setup();
|
||||
Gpio_SSC_DOUT_Low(); // back to "listening"
|
||||
return ok;
|
||||
}
|
||||
|
||||
void SimulateCotag(const lf_cotagsim_t *params, bool ledcontrol) {
|
||||
|
||||
LFSetupFPGAForADC(LF_COTAG_DIVISOR, false);
|
||||
@@ -3184,14 +3283,22 @@ void SimulateCotag(const lf_cotagsim_t *params, bool ledcontrol) {
|
||||
|
||||
const uint32_t settle = COTAG_SETTLE_US * 12 / (LF_COTAG_DIVISOR + 1);
|
||||
|
||||
if (params->active == false) {
|
||||
StartPrecisionCounter();
|
||||
}
|
||||
|
||||
cotag_detect_val_t val;
|
||||
|
||||
while (cotag_activation_sequence_detect(params, &val)) {
|
||||
cotag_tx_val_t tx_val = {0};
|
||||
|
||||
if (nbits) {
|
||||
if (ledcontrol) LED_C_ON();
|
||||
|
||||
// Transmit frame
|
||||
bool ok = cotag_tx_frame(half_bits, nhb, params->repeats, params->hb_delay_us);
|
||||
bool ok = params->active
|
||||
? cotag_tx_frame_active(half_bits, nhb, params->repeats, params->hb_delay_us)
|
||||
: cotag_tx_frame_passive(half_bits, nhb, params->repeats, &tx_val);
|
||||
|
||||
if (ledcontrol) LED_C_OFF();
|
||||
|
||||
@@ -3207,11 +3314,27 @@ void SimulateCotag(const lf_cotagsim_t *params, bool ledcontrol) {
|
||||
|
||||
// Debug print only after done TXing frame
|
||||
if (params->verbose || nbits == 0) {
|
||||
Dbprintf("COTAG reader activation sequence detected (rise-to-rise " _YELLOW_("%u") " us, threshold %d)",
|
||||
val.dt * (LF_COTAG_DIVISOR + 1) / 12, val.thr);
|
||||
Dbprintf("COTAG reader activation sequence detected");
|
||||
if (g_dbglevel >= DBG_DEBUG) {
|
||||
Dbprintf(" rise-to-rise " _YELLOW_("%u") " us, threshold %d", val.dt * (LF_COTAG_DIVISOR + 1) / 12, val.thr);
|
||||
}
|
||||
|
||||
if (params->active == false && nbits) {
|
||||
uint16_t total = nhb + (params->repeats - 1) * (nhb - COTAG_TX_SYNC_HALFBITS);
|
||||
if (tx_val.field_lost) {
|
||||
Dbprintf(" Answer stopped after " _YELLOW_("%u") " of %u half bits, reader field gone", tx_val.nhb_sent, total);
|
||||
} else {
|
||||
Dbprintf(" Sent " _YELLOW_("%u") " of %u half bits", tx_val.nhb_sent, total);
|
||||
if (g_dbglevel >= DBG_DEBUG) Dbprintf(" missed carrier edges %u", tx_val.missed_edges);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (params->active == false) {
|
||||
StopPrecisionCounter();
|
||||
}
|
||||
|
||||
FpgaWriteConfWord(FPGA_MAJOR_MODE_OFF);
|
||||
if (ledcontrol) LEDsoff();
|
||||
BigBuf_free();
|
||||
|
||||
Reference in new issue
Block a user