hitag_common is restored to the FPGA-side detection edge and implemented using HAL.
This commit is contained in:
1 parent
e3511c5350
commit
68aeb51482
5 files changed
+306
-462
No files matched your search
+36
-122
@@ -97,7 +97,6 @@ static uint8_t key_no;
|
||||
static uint64_t cipher_state;
|
||||
|
||||
static int16_t blocknr;
|
||||
static size_t flipped_bit = 0;
|
||||
static uint32_t byte_value = 0;
|
||||
|
||||
static void hitag2_reset(void) {
|
||||
@@ -109,12 +108,11 @@ static void hitag2_init(void) {
|
||||
hitag2_reset();
|
||||
}
|
||||
|
||||
// Sam7s has several timers, we will use the source TIMER_CLOCK1 (aka AT91C_TC_CLKS_TIMER_DIV1_CLOCK)
|
||||
// TIMER_CLOCK1 = MCK/2, MCK is running at 48 MHz, Timer is running at 48/2 = 24 MHz
|
||||
// Hitag units (T0) have duration of 8 microseconds (us), which is 1/125000 per second (carrier)
|
||||
// T0 = TIMER_CLOCK1 / 125000 = 192
|
||||
// The input-capture timer (StartInputCapture, see common_arm/ticks) runs at
|
||||
// TIMER_CLOCK3 = MCK/32 = 1.5 MHz. Hitag units (T0) have a duration of 8 us
|
||||
// (1/125000 s, the carrier period), so T0 = 1.5 MHz / 125 kHz = 12 counter ticks.
|
||||
#ifndef HITAG_T0
|
||||
#define HITAG_T0 192
|
||||
#define HITAG_T0 12
|
||||
#endif
|
||||
|
||||
#define HITAG_FRAME_LEN 20
|
||||
@@ -148,50 +146,6 @@ static void hitag2_init(void) {
|
||||
|
||||
#define HT2_MAX_NRSZ ((8 * HITAG_FRAME_LEN + 5) * 2)
|
||||
|
||||
/*
|
||||
// sim
|
||||
static void hitag_send_bit(int bit, bool ledcontrol) {
|
||||
if (ledcontrol) LED_A_ON();
|
||||
|
||||
// Reset clock for the next bit
|
||||
AT91C_BASE_TC0->TC_CCR = AT91C_TC_SWTRG;
|
||||
|
||||
// Fixed modulation, earlier proxmark version used inverted signal
|
||||
// check datasheet if reader uses BiPhase?
|
||||
if (bit == 0) {
|
||||
// Manchester: Unloaded, then loaded |__--|
|
||||
Gpio_SSC_DOUT_Low();
|
||||
while (AT91C_BASE_TC0->TC_CV < HITAG_T0 * HITAG_T_TAG_HALF_PERIOD);
|
||||
Gpio_SSC_DOUT_High();
|
||||
while (AT91C_BASE_TC0->TC_CV < HITAG_T0 * HITAG_T_TAG_FULL_PERIOD);
|
||||
} else {
|
||||
// Manchester: Loaded, then unloaded |--__|
|
||||
Gpio_SSC_DOUT_High();
|
||||
while (AT91C_BASE_TC0->TC_CV < HITAG_T0 * HITAG_T_TAG_HALF_PERIOD);
|
||||
Gpio_SSC_DOUT_Low();
|
||||
while (AT91C_BASE_TC0->TC_CV < HITAG_T0 * HITAG_T_TAG_FULL_PERIOD);
|
||||
}
|
||||
if (ledcontrol) LED_A_OFF();
|
||||
}
|
||||
|
||||
// sim
|
||||
static void hitag_send_frame(const uint8_t *frame, size_t frame_len) {
|
||||
// SOF - send start of frame
|
||||
hitag_send_bit(1);
|
||||
hitag_send_bit(1);
|
||||
hitag_send_bit(1);
|
||||
hitag_send_bit(1);
|
||||
hitag_send_bit(1);
|
||||
|
||||
// Send the content of the frame
|
||||
for (size_t i = 0; i < frame_len; i++) {
|
||||
hitag_send_bit((frame[i / 8] >> (7 - (i % 8))) & 1);
|
||||
}
|
||||
|
||||
// Drop the modulation
|
||||
Gpio_SSC_DOUT_Low();
|
||||
}
|
||||
*/
|
||||
|
||||
// sim
|
||||
static void hitag2_handle_reader_command(uint8_t *rx, const size_t rxlen, uint8_t *tx, size_t *txlen) {
|
||||
@@ -1227,28 +1181,14 @@ void SniffHitag2(bool ledcontrol) {
|
||||
// Disable modulation, we are going to eavesdrop, not modulate ;)
|
||||
// Gpio_SSC_DOUT_Low();
|
||||
|
||||
// Enable Peripheral Clock for TIMER_CLOCK1, used to capture edges of the reader frames
|
||||
AT91C_BASE_PMC->PMC_PCER = (1 << AT91C_ID_TC1);
|
||||
AT91C_BASE_PIOA->PIO_BSR = GPIO_SSC_FRAME;
|
||||
|
||||
// Disable timer during configuration
|
||||
AT91C_BASE_TC1->TC_CCR = AT91C_TC_CLKDIS;
|
||||
|
||||
// Capture mode, default timer source = MCK/2 (TIMER_CLOCK1), TIOA is external trigger,
|
||||
// external trigger rising edge, load RA on rising edge of TIOA.
|
||||
AT91C_BASE_TC1->TC_CMR = AT91C_TC_CLKS_TIMER_DIV1_CLOCK | AT91C_TC_ETRGEDG_BOTH | AT91C_TC_ABETRG | AT91C_TC_LDRA_BOTH;
|
||||
|
||||
// Enable and reset counter
|
||||
AT91C_BASE_TC1->TC_CCR = AT91C_TC_CLKEN | AT91C_TC_SWTRG;
|
||||
|
||||
// Assert a sync signal. This sets all timers to 0 on next active clock edge
|
||||
AT91C_BASE_TCB->TCB_BCR = 1;
|
||||
// Configure the input capture (TC1) via the HAL: it enables the TC1 clock, routes
|
||||
// SSC_FRAME to the timer input, resets on each falling edge and captures RB
|
||||
// (falling->falling) / RA (falling->rising).
|
||||
StartLoEdgeCapture();
|
||||
|
||||
int frame_count = 0, response = 0, lastbit = 1, tag_sof = 4;
|
||||
int overflow = 0;
|
||||
bool rising_edge, reader_frame = false, bSkip = true;
|
||||
bool reader_frame = false, bSkip = true;
|
||||
|
||||
// bool exit_due_to_overflow;
|
||||
// HACK -- add one byte to avoid rewriting manchester decoder for edge case
|
||||
uint8_t rx[HITAG_FRAME_LEN + 1] = {0};
|
||||
size_t rxlen = 0;
|
||||
@@ -1262,57 +1202,45 @@ void SniffHitag2(bool ledcontrol) {
|
||||
|
||||
WDT_HIT();
|
||||
|
||||
// bool exit_due_to_overflow = false;
|
||||
// Receive frame, watch for at most HITAG_T0 * HITAG_T_EOF periods since the last edge
|
||||
while (GetLoEdgeCaptureCount() < (HITAG_T0 * HITAG_T_EOF)) {
|
||||
|
||||
// Receive frame, watch for at most T0 * EOF periods
|
||||
while (AT91C_BASE_TC1->TC_CV < (HITAG_T0 * HITAG_T_EOF)) {
|
||||
// Read (and clear) the input-capture edge-event flags.
|
||||
lo_edge_t lo_edge = GetLoEdgeCaptureStatus();
|
||||
|
||||
// Check if rising edge in modulation is detected
|
||||
if (AT91C_BASE_TC1->TC_SR & AT91C_TC_LDRAS) {
|
||||
// Rising edge: RA holds the falling->rising sub-period (the reader's tlow).
|
||||
if (lo_edge == LO_EDGE_RISING) {
|
||||
int ra = GetLoEdgeCaptureRising() / HITAG_T0;
|
||||
|
||||
// Retrieve the new timing values
|
||||
int ra = (AT91C_BASE_TC1->TC_RA / HITAG_T0) + overflow;
|
||||
overflow = 0;
|
||||
|
||||
// Find out if we are dealing with a rising or falling edge
|
||||
rising_edge = (Gpio_SSC_FRAME_Read()) > 0;
|
||||
|
||||
// Shorter periods will only happen with reader frames
|
||||
if (reader_frame == false && rising_edge && ra < HITAG_T_TAG_CAPTURE_ONE_HALF) {
|
||||
// Shorter periods only happen with reader frames (reader tlow is 4..10 T0,
|
||||
// while the shortest tag Manchester half-period is 16 T0).
|
||||
if (reader_frame == false && ra < HITAG_T_TAG_CAPTURE_ONE_HALF) {
|
||||
// Switch from tag to reader capture
|
||||
if (ledcontrol) LED_C_OFF();
|
||||
reader_frame = true;
|
||||
rxlen = 0;
|
||||
}
|
||||
}
|
||||
|
||||
// Only handle if reader frame and rising edge, or tag frame and falling edge
|
||||
if (reader_frame != rising_edge) {
|
||||
overflow += ra;
|
||||
continue;
|
||||
}
|
||||
|
||||
// Add the buffered timing values of earlier captured edges which were skipped
|
||||
ra += overflow;
|
||||
overflow = 0;
|
||||
// Falling edge: RB holds the falling->falling full period (the bit timing).
|
||||
if (lo_edge == LO_EDGE_FALLING) {
|
||||
int rb = GetLoEdgeCaptureFalling() / HITAG_T0;
|
||||
|
||||
if (reader_frame) {
|
||||
|
||||
if (ledcontrol) LED_B_ON();
|
||||
// Capture reader frame
|
||||
if (ra >= HITAG_T_STOP) {
|
||||
// if (rxlen != 0) {
|
||||
//DbpString("wierd0?");
|
||||
// }
|
||||
if (rb >= HITAG_T_STOP) {
|
||||
// Capture the T0 periods that have passed since last communication or field drop (reset)
|
||||
response = (ra - HITAG_T_LOW);
|
||||
if (rxlen != 0) { Dbprintf("ra - HITAG_T_LOW... %i", response); }
|
||||
response = (rb - HITAG_T_LOW);
|
||||
if (rxlen != 0) { Dbprintf("rb - HITAG_T_LOW... %i", response); }
|
||||
|
||||
} else if (ra >= HITAG_T_1_MIN) {
|
||||
} else if (rb >= HITAG_T_1_MIN) {
|
||||
// '1' bit
|
||||
rx[rxlen / 8] |= 1 << (7 - (rxlen % 8));
|
||||
rxlen++;
|
||||
|
||||
} else if (ra >= HITAG_T_0_MIN) {
|
||||
} else if (rb >= HITAG_T_0_MIN) {
|
||||
// '0' bit
|
||||
rx[rxlen / 8] |= 0 << (7 - (rxlen % 8));
|
||||
rxlen++;
|
||||
@@ -1321,22 +1249,19 @@ void SniffHitag2(bool ledcontrol) {
|
||||
} else {
|
||||
if (ledcontrol) LED_C_ON();
|
||||
// Capture tag frame (manchester decoding using only falling edges)
|
||||
if (ra >= HITAG_T_EOF) {
|
||||
// if (rxlen != 0) {
|
||||
//DbpString("wierd1?");
|
||||
// }
|
||||
if (rb >= HITAG_T_EOF) {
|
||||
// Capture the T0 periods that have passed since last communication or field drop (reset)
|
||||
// We always receive a 'one' first, which has the falling edge after a half period |-_|
|
||||
response = ra - HITAG_T_TAG_HALF_PERIOD;
|
||||
response = rb - HITAG_T_TAG_HALF_PERIOD;
|
||||
|
||||
} else if (ra >= HITAG_T_TAG_CAPTURE_FOUR_HALF) {
|
||||
} else if (rb >= HITAG_T_TAG_CAPTURE_FOUR_HALF) {
|
||||
// Manchester coding example |-_|_-|-_| (101)
|
||||
rx[rxlen / 8] |= 0 << (7 - (rxlen % 8));
|
||||
rxlen++;
|
||||
rx[rxlen / 8] |= 1 << (7 - (rxlen % 8));
|
||||
rxlen++;
|
||||
|
||||
} else if (ra >= HITAG_T_TAG_CAPTURE_THREE_HALF) {
|
||||
} else if (rb >= HITAG_T_TAG_CAPTURE_THREE_HALF) {
|
||||
// Manchester coding example |_-|...|_-|-_| (0...01)
|
||||
rx[rxlen / 8] |= 0 << (7 - (rxlen % 8));
|
||||
rxlen++;
|
||||
@@ -1348,7 +1273,7 @@ void SniffHitag2(bool ledcontrol) {
|
||||
lastbit = !lastbit;
|
||||
bSkip = !bSkip;
|
||||
|
||||
} else if (ra >= HITAG_T_TAG_CAPTURE_TWO_HALF) {
|
||||
} else if (rb >= HITAG_T_TAG_CAPTURE_TWO_HALF) {
|
||||
// Manchester coding example |_-|_-| (00) or |-_|-_| (11)
|
||||
if (tag_sof) {
|
||||
// Ignore bits that are transmitted during SOF
|
||||
@@ -1388,32 +1313,24 @@ void SniffHitag2(bool ledcontrol) {
|
||||
lastbit = 1;
|
||||
bSkip = true;
|
||||
tag_sof = 4;
|
||||
overflow = 0;
|
||||
|
||||
if (ledcontrol) {
|
||||
LED_B_OFF();
|
||||
LED_C_OFF();
|
||||
}
|
||||
|
||||
} else {
|
||||
// Save the timer overflow, will be 0 when frame was received
|
||||
overflow += (AT91C_BASE_TC1->TC_CV / HITAG_T0);
|
||||
}
|
||||
|
||||
// Reset the frame length
|
||||
rxlen = 0;
|
||||
|
||||
// Reset the timer to restart while-loop that receives frames
|
||||
AT91C_BASE_TC1->TC_CCR = AT91C_TC_SWTRG;
|
||||
|
||||
// Assert a sync signal. This sets all timers to 0 on next active clock edge
|
||||
AT91C_BASE_TCB->TCB_BCR = 1;
|
||||
// Reset the capture counter to restart the while-loop that receives frames.
|
||||
ResetLoEdgeCapture();
|
||||
}
|
||||
|
||||
if (ledcontrol) LEDsoff();
|
||||
|
||||
AT91C_BASE_TC1->TC_CCR = AT91C_TC_CLKDIS;
|
||||
AT91C_BASE_TC0->TC_CCR = AT91C_TC_CLKDIS;
|
||||
StopLoEdgeCapture();
|
||||
|
||||
DBG Dbprintf("frames.......... %d", frame_count);
|
||||
Dbprintf("Auth attempts... %d", (auth_table_len / 8));
|
||||
@@ -1620,7 +1537,6 @@ void SimulateHitag2(bool ledcontrol) {
|
||||
// Send and store the tag answer (if there is any)
|
||||
if (txlen) {
|
||||
// Transmit the tag frame
|
||||
//hitag_send_frame(tx, txlen);
|
||||
lf_manchester_send_bytes(tx, txlen, ledcontrol);
|
||||
|
||||
// Store the frame in the trace
|
||||
@@ -1778,7 +1694,6 @@ void ReaderHitag(const lf_hitag_data_t *payload, bool ledcontrol) {
|
||||
t_wait_1 = 204;
|
||||
t_wait_2 = 128;
|
||||
tag_size = 256;
|
||||
flipped_bit = 0;
|
||||
DBG DbpString("Configured for " _YELLOW_("Hitag 1") " reader");
|
||||
} else if (payload->cmd <= HT2_LAST_CMD) {
|
||||
// hitag 2 settings
|
||||
@@ -2148,7 +2063,6 @@ void WriterHitag(const lf_hitag_data_t *payload, bool ledcontrol) {
|
||||
t_wait_1 = 204;
|
||||
t_wait_2 = 128;
|
||||
tag_size = 256;
|
||||
flipped_bit = 0;
|
||||
DBG DbpString("Configured for " _YELLOW_("Hitag 1") " writer");
|
||||
} else if (payload->cmd <= HT2_LAST_CMD) {
|
||||
// hitag 2 settings
|
||||
|
||||
+6
-6
@@ -424,7 +424,7 @@ void hts_simulate(bool tag_mem_supplied, int8_t threshold, const uint8_t *data,
|
||||
LogTraceBits(rx, rxlen, start_time, TIMESTAMP, true);
|
||||
|
||||
// Disable timer 1 with external trigger to avoid triggers during our own modulation
|
||||
StopInputCapture();
|
||||
StopLoEdgeCapture();
|
||||
|
||||
// Process the incoming frame (rx) and prepare the outgoing frame (tx)
|
||||
hts_handle_reader_command(rx, rxlen, tx, &txlen);
|
||||
@@ -445,7 +445,7 @@ void hts_simulate(bool tag_mem_supplied, int8_t threshold, const uint8_t *data,
|
||||
}
|
||||
|
||||
// Enable and reset external trigger in timer for capturing future frames
|
||||
EnableInputCapture();
|
||||
EnableLoEdgeCapture();
|
||||
|
||||
// Reset the received frame and response timing info
|
||||
memset(rx, 0x00, sizeof(rx));
|
||||
@@ -455,9 +455,9 @@ void hts_simulate(bool tag_mem_supplied, int8_t threshold, const uint8_t *data,
|
||||
// Reset the frame length
|
||||
rxlen = 0;
|
||||
// Save the timer overflow, will be 0 when frame was received
|
||||
overflow += (GetInputCaptureCount() / T0);
|
||||
overflow += (GetLoEdgeCaptureCount() / T0);
|
||||
// Reset the timer to restart while-loop that receives frames
|
||||
ResetInputCapture();
|
||||
ResetLoEdgeCapture();
|
||||
|
||||
}
|
||||
|
||||
@@ -473,7 +473,7 @@ static int hts_send_receive(const uint8_t *tx, size_t txlen, uint8_t *rx, size_t
|
||||
|
||||
// Send and store the reader command
|
||||
// Disable timer 1 with external trigger to avoid triggers during our own modulation
|
||||
StopInputCapture();
|
||||
StopLoEdgeCapture();
|
||||
|
||||
DBG Dbprintf("tx %d bits:", txlen);
|
||||
DBG Dbhexdump((txlen + 7) / 8, tx, false);
|
||||
@@ -497,7 +497,7 @@ static int hts_send_receive(const uint8_t *tx, size_t txlen, uint8_t *rx, size_t
|
||||
LogTraceBits(tx, txlen, start_time, TIMESTAMP, true);
|
||||
|
||||
// Enable and reset external trigger in timer for capturing future frames
|
||||
EnableInputCapture();
|
||||
EnableLoEdgeCapture();
|
||||
|
||||
hts_set_frame_modulation(protocol_mode, ac_seq);
|
||||
|
||||
|
||||
+254
-317
@@ -34,56 +34,6 @@
|
||||
#include "protocols.h"
|
||||
#include "appmain.h" // tearoff_hook()
|
||||
|
||||
uint16_t timestamp_high = 0; // Timer Counter 2 overflow count, combined with TC2 counter for ~47min timing
|
||||
|
||||
static void hitag_stop_clock(void) {
|
||||
AT91C_BASE_TC0->TC_CCR = AT91C_TC_CLKDIS;
|
||||
AT91C_BASE_TC1->TC_CCR = AT91C_TC_CLKDIS;
|
||||
AT91C_BASE_TC2->TC_CCR = AT91C_TC_CLKDIS;
|
||||
}
|
||||
|
||||
static void hitag_init_clock(void) {
|
||||
// Enable Peripheral Clock for
|
||||
// Timer Counter 0, used to measure exact timing before answering
|
||||
// Timer Counter 1, used to capture edges of the tag frames
|
||||
// Timer Counter 2, used to log trace time
|
||||
AT91C_BASE_PMC->PMC_PCER |= (1 << AT91C_ID_TC0) | (1 << AT91C_ID_TC1) | (1 << AT91C_ID_TC2);
|
||||
|
||||
AT91C_BASE_PIOA->PIO_BSR = GPIO_SSC_FRAME;
|
||||
|
||||
// Disable timer during configuration
|
||||
hitag_stop_clock();
|
||||
|
||||
// TC0: Capture mode, default timer source = MCK/32 (TIMER_CLOCK3), no triggers
|
||||
AT91C_BASE_TC0->TC_CMR = AT91C_TC_CLKS_TIMER_DIV3_CLOCK;
|
||||
|
||||
// TC1: Capture mode, default timer source = MCK/32 (TIMER_CLOCK3), TIOA is external trigger,
|
||||
AT91C_BASE_TC1->TC_CMR = AT91C_TC_CLKS_TIMER_DIV3_CLOCK // use MCK/32 (TIMER_CLOCK3)
|
||||
| AT91C_TC_ABETRG // TIOA is used as an external trigger
|
||||
| AT91C_TC_ETRGEDG_FALLING // external trigger on falling edge
|
||||
| AT91C_TC_LDRA_RISING // load RA on on rising edge of TIOA
|
||||
| AT91C_TC_LDRB_FALLING; // load RB on on falling edge of TIOA
|
||||
|
||||
// TC2: Capture mode, default timer source = MCK/32 (TIMER_CLOCK3), no triggers
|
||||
AT91C_BASE_TC2->TC_CMR = AT91C_TC_CLKS_TIMER_DIV3_CLOCK;
|
||||
|
||||
// Enable and reset counters
|
||||
AT91C_BASE_TC0->TC_CCR = AT91C_TC_CLKEN | AT91C_TC_SWTRG;
|
||||
AT91C_BASE_TC1->TC_CCR = AT91C_TC_CLKEN | AT91C_TC_SWTRG;
|
||||
AT91C_BASE_TC2->TC_CCR = AT91C_TC_CLKEN | AT91C_TC_SWTRG;
|
||||
|
||||
// Assert a sync signal. This sets all timers to 0 on next active clock edge
|
||||
AT91C_BASE_TCB->TCB_BCR = 1;
|
||||
|
||||
// synchronized startup procedure
|
||||
// In theory, with MCK/32, we shouldn't be waiting longer than 32 instruction statements, right?
|
||||
while (AT91C_BASE_TC0->TC_CV != 0) {
|
||||
}; // wait until TC0 returned to zero
|
||||
|
||||
// reset timestamp
|
||||
timestamp_high = 0;
|
||||
}
|
||||
|
||||
// Initialize FPGA and timer for Hitag operations
|
||||
void hitag_setup_fpga(uint16_t conf, uint8_t threshold, bool ledcontrol) {
|
||||
StopTicks();
|
||||
@@ -94,15 +44,17 @@ void hitag_setup_fpga(uint16_t conf, uint8_t threshold, bool ledcontrol) {
|
||||
set_tracing(true);
|
||||
clear_trace();
|
||||
|
||||
if (ledcontrol) {
|
||||
LED_D_ON();
|
||||
}
|
||||
if (ledcontrol) LED_D_ON();
|
||||
|
||||
hitag_init_clock();
|
||||
// Configure the timers via the HAL: a precision counter (T0 timing), an
|
||||
// input capture (tag frame edges) and a timestamp counter (trace timing).
|
||||
StartPrecisionCounter();
|
||||
StartLoEdgeCapture();
|
||||
StartTimestamp();
|
||||
|
||||
// Set fpga in edge detect with/without reader field, we can modulate as reader/tag now
|
||||
FpgaWriteConfWord(FPGA_MAJOR_MODE_LF_EDGE_DETECT | conf);
|
||||
FpgaSendCommand(FPGA_CMD_SET_DIVISOR, LF_DIVISOR_125); //125kHz
|
||||
FpgaSendCommand(FPGA_CMD_SET_DIVISOR, LF_DIVISOR_125); //125kHz
|
||||
if (threshold != 127) FpgaSendCommand(FPGA_CMD_SET_EDGE_DETECT_THRESHOLD, threshold);
|
||||
SetAdcMuxFor(ADC_MUXSEL_LOPKD);
|
||||
|
||||
@@ -115,242 +67,248 @@ void hitag_setup_fpga(uint16_t conf, uint8_t threshold, bool ledcontrol) {
|
||||
|
||||
// Clean up and finalize Hitag operations
|
||||
void hitag_cleanup(bool ledcontrol) {
|
||||
hitag_stop_clock();
|
||||
StopPrecisionCounter();
|
||||
StopLoEdgeCapture();
|
||||
StopTimestamp();
|
||||
set_tracing(false);
|
||||
lf_finalize(ledcontrol);
|
||||
}
|
||||
|
||||
/**
|
||||
* Send data or condition to TAG
|
||||
*
|
||||
* This function will modulate a fixed length gap before modulating the data/condition of the specified period,
|
||||
* and the number of periods of the data/condition modulated later will subtract the number of gaps.
|
||||
*
|
||||
* @param periods The numbers of periods for data/condition modulation
|
||||
* @param ledcontrol Show the LED flash
|
||||
*/
|
||||
static RAMFUNC void hitag_reader_send_with_gap(int periods, bool ledcontrol) {
|
||||
if (ledcontrol)
|
||||
LED_A_ON();
|
||||
// Reader functions
|
||||
static void hitag_reader_send_bit(int bit, bool ledcontrol) {
|
||||
// Reset clock for the next bit
|
||||
ResetPrecisionCounter();
|
||||
|
||||
if (ledcontrol) LED_A_ON();
|
||||
|
||||
// Binary puls length modulation (BPLM) is used to encode the data stream
|
||||
// This means that a transmission of a one takes longer than that of a zero
|
||||
|
||||
// GPIOB->clr = GPIO_PINS_11; // TODO DXL 测试添加
|
||||
|
||||
// Send GAP
|
||||
Gpio_SSC_DOUT_High();
|
||||
lf_wait_periods(HITAG_T_LOW); // Wait for 4-10 times the carrier period
|
||||
|
||||
// GPIOB->scr = GPIO_PINS_11; // TODO DXL 测试添加
|
||||
// Wait for 4-10 times the carrier period
|
||||
while (GetPrecisionCounter() < T0 * HITAG_T_LOW) {};
|
||||
|
||||
Gpio_SSC_DOUT_Low();
|
||||
lf_wait_periods(periods - HITAG_T_LOW);
|
||||
|
||||
if (ledcontrol)
|
||||
LED_A_OFF();
|
||||
if (bit == 0) {
|
||||
// Zero bit: |_-|
|
||||
while (GetPrecisionCounter() < T0 * HITAG_T_0) {};
|
||||
} else {
|
||||
// One bit: |_--|
|
||||
while (GetPrecisionCounter() < T0 * HITAG_T_1) {};
|
||||
}
|
||||
|
||||
if (ledcontrol) LED_A_OFF();
|
||||
}
|
||||
|
||||
void hitag_reader_send_frame(const uint8_t *frame, size_t frame_len, bool ledcontrol, bool send_sof) {
|
||||
// Send SOF (Start of Frame) for Hitag µ if requested
|
||||
if (send_sof) {
|
||||
// The RWD requests in the data exchange mode always a start with a SOF pattern for ease
|
||||
// of synchronization. The SOF pattern consists of an encoded data bit ’0’ and a ’code violation’.
|
||||
hitag_reader_send_with_gap(HITAG_T_0, ledcontrol);
|
||||
// According to the manual description, the TFcv timing includes the TF1 timing,
|
||||
// so it is necessary to subtract the TF1 timing(subtract inside the 'hitag_reader_send_with_gap').
|
||||
hitag_reader_send_with_gap(HITAG_T_CODE_VIOLATION, ledcontrol);
|
||||
hitag_reader_send_bit(0, ledcontrol);
|
||||
|
||||
// Reset clock for the code violation
|
||||
ResetPrecisionCounter();
|
||||
|
||||
if (ledcontrol) LED_A_ON();
|
||||
|
||||
// SOF is HIGH for HITAG_T_LOW
|
||||
Gpio_SSC_DOUT_High();
|
||||
while (GetPrecisionCounter() < T0 * HITAG_T_LOW) {};
|
||||
|
||||
// Then LOW for HITAG_T_CODE_VIOLATION
|
||||
Gpio_SSC_DOUT_Low();
|
||||
while (GetPrecisionCounter() < T0 * HITAG_T_CODE_VIOLATION) {};
|
||||
|
||||
if (ledcontrol) LED_A_OFF();
|
||||
}
|
||||
|
||||
// Send the content of the frame
|
||||
for (size_t i = 0; i < frame_len; i++) {
|
||||
hitag_reader_send_with_gap(TEST_BIT_MSB(frame, i) ? HITAG_T_1 : HITAG_T_0, ledcontrol);
|
||||
hitag_reader_send_bit(TEST_BIT_MSB(frame, i), ledcontrol);
|
||||
}
|
||||
|
||||
// 36 + 8 = 44(t0), if you have the wait for 'twresp', the 'twresp' need to subtract HITAG_T_LOW + HITAG_T_STOP
|
||||
// twresp include the gap + eof, max = 212(t0).
|
||||
// The best way is not to block waiting for a response,
|
||||
// Otherwise, there is a possibility of missing the card's response.
|
||||
hitag_reader_send_with_gap(HITAG_T_STOP + HITAG_T_LOW, ledcontrol);
|
||||
// Send EOF
|
||||
ResetPrecisionCounter();
|
||||
|
||||
Gpio_SSC_DOUT_High();
|
||||
|
||||
// Wait for 4-10 times the carrier period
|
||||
while (GetPrecisionCounter() < T0 * HITAG_T_LOW) {};
|
||||
|
||||
Gpio_SSC_DOUT_Low();
|
||||
}
|
||||
|
||||
void hitag_reader_receive_frame(uint8_t *rx, size_t sizeofrx, size_t *rxlen, uint32_t *resptime, bool ledcontrol,
|
||||
hitag_mod_t modulation, int sof_bits) {
|
||||
bool sof_received = false; // Skip SOF bits in header
|
||||
bool is_first_edge = true;
|
||||
uint8_t lastbit = 1;
|
||||
bool skip_next_failing_edge = true;
|
||||
uint8_t error_count = 0; // Cannot exceed 255 times.
|
||||
uint8_t t_4half, t_3half, t_2half;
|
||||
size_t periods_rising_edge = 0; // for save the edge periods if is a rising edge
|
||||
|
||||
// Reset values for receiving frames
|
||||
memset(rx, 0x00, sizeofrx);
|
||||
*rxlen = 0;
|
||||
|
||||
// For debug, if the development has been completed,
|
||||
// please comment out the relevant definitions and calls to reduce resource usage.
|
||||
int rb_i = 0;
|
||||
int lastbit = 1;
|
||||
bool bSkip = true;
|
||||
uint32_t errorCount = 0;
|
||||
bool bStarted = false;
|
||||
lo_edge_t next_edge = LO_EDGE_FALLING;
|
||||
int double_speed = (modulation == AC4K || modulation == MC8K) ? 2 : 1;
|
||||
|
||||
uint32_t rb_i = 0;
|
||||
uint8_t edges[160] = {0};
|
||||
|
||||
// Precalculate the number of periods at different modulation speeds.
|
||||
if (modulation == AC4K || modulation == MC8K) {
|
||||
t_4half = HITAG_T_TAG_CAPTURE_FOUR_HALF / 2;
|
||||
t_3half = HITAG_T_TAG_CAPTURE_THREE_HALF / 2;
|
||||
t_2half = HITAG_T_TAG_CAPTURE_TWO_HALF / 2;
|
||||
} else {
|
||||
t_4half = HITAG_T_TAG_CAPTURE_FOUR_HALF;
|
||||
t_3half = HITAG_T_TAG_CAPTURE_THREE_HALF;
|
||||
t_2half = HITAG_T_TAG_CAPTURE_TWO_HALF;
|
||||
}
|
||||
// Skip SOF bits
|
||||
bool sof_received = false;
|
||||
|
||||
while (BUTTON_PRESS() == false) {
|
||||
// Note: It is necessary to wait for at least N periods after the card is powered on and stabilized.
|
||||
// Otherwise, an incorrect starting edge will be collected, resulting in a deviation in the periods calculation.
|
||||
// So, it's best to wait for about 1000 periods. To wait for first response.
|
||||
size_t periods = lf_count_edge_periods(1000);
|
||||
if (periods == 0) {
|
||||
break;
|
||||
}
|
||||
// Receive tag frame, watch for at most T0*HITAG_T_PROG_MAX periods
|
||||
while (GetPrecisionCounter() < (T0 * HITAG_T_PROG_MAX)) {
|
||||
// Check if edge in tag modulation is detected
|
||||
if (GetLoEdgeCaptureStatus() == next_edge) {
|
||||
next_edge = next_edge == LO_EDGE_RISING ? LO_EDGE_FALLING : LO_EDGE_RISING;
|
||||
|
||||
// Capture tag frame (manchester decoding using only falling edges)
|
||||
if (periods >= HITAG_T_WAIT_RESP - (HITAG_T_EOF + HITAG_T_LOW) && is_first_edge) {
|
||||
*resptime = TIMESTAMP; // TODO DXL 待实现计算时间
|
||||
// only use INPUT_CAPTURE_EVT_RB falling edge for now
|
||||
if (next_edge == LO_EDGE_FALLING) {
|
||||
continue;
|
||||
}
|
||||
|
||||
// We always receive a 'one' first, which has the falling edge after a half period |-_|
|
||||
rx[0] = 0x80;
|
||||
*rxlen = 1;
|
||||
// Retrieve the new timing values
|
||||
uint32_t rb = GetLoEdgeCaptureFalling() / T0;
|
||||
|
||||
is_first_edge = false;
|
||||
continue;
|
||||
}
|
||||
|
||||
// Only use falling edge for now
|
||||
if (!lf_get_tag_modulation()) {
|
||||
periods_rising_edge = periods;
|
||||
continue;
|
||||
}
|
||||
|
||||
// We have obtained an effective falling edge, now we can calculate the number of periods.
|
||||
periods += periods_rising_edge;
|
||||
periods_rising_edge = 0; // reset periods of rising edge for next time detect
|
||||
|
||||
if (rb_i < sizeof(edges)) {
|
||||
// For debug, save the edges for decoding manual
|
||||
edges[rb_i++] = periods;
|
||||
if (rb_i < sizeof(edges)) {
|
||||
edges[rb_i++] = rb;
|
||||
}
|
||||
|
||||
// Reset timer every frame, we have to capture the last edge for timing
|
||||
ResetPrecisionCounter();
|
||||
|
||||
if (ledcontrol) LED_B_INV();
|
||||
|
||||
// Capture tag frame (manchester decoding using only falling edges)
|
||||
if (bStarted == false) {
|
||||
if (rb >= HITAG_T_WAIT_RESP) {
|
||||
bStarted = true;
|
||||
|
||||
// Capture tag response timestamp
|
||||
*resptime = TIMESTAMP;
|
||||
|
||||
// We always receive a 'one' first, which has the falling edge after a half period |-_|
|
||||
rx[0] = 0x80;
|
||||
*rxlen = 1;
|
||||
} else {
|
||||
errorCount++;
|
||||
}
|
||||
} else {
|
||||
// Handle different modulation types
|
||||
if (modulation == AC2K || modulation == AC4K) {
|
||||
// Anticollision Coding
|
||||
if (rb >= HITAG_T_TAG_CAPTURE_FOUR_HALF / double_speed) {
|
||||
// Anticollision Coding example |--__|--__| (00)
|
||||
lastbit = 0;
|
||||
// CLEAR_BIT_MSB(rx, *rxlen);
|
||||
(*rxlen)++;
|
||||
} else if (rb >= HITAG_T_TAG_CAPTURE_THREE_HALF / double_speed) {
|
||||
// Anticollision Coding example |-_-_|--__| (10) or |--__|-_-_| (01)
|
||||
lastbit = !lastbit;
|
||||
if (lastbit) {
|
||||
SET_BIT_MSB(rx, *rxlen);
|
||||
}
|
||||
(*rxlen)++;
|
||||
|
||||
bSkip = !!lastbit;
|
||||
} else if (rb >= HITAG_T_TAG_CAPTURE_TWO_HALF / double_speed) {
|
||||
// Anticollision Coding example |-_-_| (1)
|
||||
if (bSkip == false) {
|
||||
lastbit = 1;
|
||||
SET_BIT_MSB(rx, *rxlen);
|
||||
(*rxlen)++;
|
||||
}
|
||||
|
||||
bSkip = !bSkip;
|
||||
} else {
|
||||
// Ignore weird value, is to small to mean anything
|
||||
errorCount++;
|
||||
}
|
||||
} else {
|
||||
// Manchester coding (MC4K, MC8K)
|
||||
if (rb >= HITAG_T_TAG_CAPTURE_FOUR_HALF / double_speed) {
|
||||
// Manchester coding example |-_|_-|-_| (101)
|
||||
// CLEAR_BIT_MSB(rx, *rxlen);
|
||||
(*rxlen)++;
|
||||
|
||||
SET_BIT_MSB(rx, *rxlen);
|
||||
(*rxlen)++;
|
||||
} else if (rb >= HITAG_T_TAG_CAPTURE_THREE_HALF / double_speed) {
|
||||
// Manchester coding example |_-|...|_-|-_| (0...01)
|
||||
// CLEAR_BIT_MSB(rx, *rxlen);
|
||||
(*rxlen)++;
|
||||
|
||||
// We have to skip this half period at start and add the 'one' the second time
|
||||
if (bSkip == false) {
|
||||
SET_BIT_MSB(rx, *rxlen);
|
||||
(*rxlen)++;
|
||||
}
|
||||
|
||||
lastbit = !lastbit;
|
||||
bSkip = !bSkip;
|
||||
} else if (rb >= HITAG_T_TAG_CAPTURE_TWO_HALF / double_speed) {
|
||||
// Manchester coding example |_-|_-| (00) or |-_|-_| (11)
|
||||
// bit is same as last bit
|
||||
if (lastbit) {
|
||||
SET_BIT_MSB(rx, *rxlen);
|
||||
}
|
||||
(*rxlen)++;
|
||||
} else {
|
||||
// Ignore weird value, is to small to mean anything
|
||||
errorCount++;
|
||||
}
|
||||
}
|
||||
|
||||
// Handle SOF bits
|
||||
if (sof_received == false && *rxlen >= sof_bits) {
|
||||
// Check if SOF is valid (all bits should be 1)
|
||||
if ((rx[0] >> (8 - sof_bits)) != ((1 << sof_bits) - 1)) {
|
||||
if (sof_bits == 4) {
|
||||
sof_bits = 3;
|
||||
// Hitag µ is LSB first 0b110
|
||||
if ((rx[0] & 0xE0) != 0xC0) {
|
||||
DBG Dbprintf("Warning, SOF is invalid rx[0]: 0x%02X", rx[0]);
|
||||
}
|
||||
} else {
|
||||
DBG DbpString("Warning, not all bits of SOF are 1");
|
||||
}
|
||||
}
|
||||
|
||||
*rxlen -= sof_bits;
|
||||
uint8_t tmp = rx[0];
|
||||
rx[0] = 0x00;
|
||||
for (size_t i = 0; i < *rxlen; i++) {
|
||||
if (TEST_BIT_MSB(&tmp, sof_bits + i)) {
|
||||
SET_BIT_MSB(rx, i);
|
||||
}
|
||||
}
|
||||
// DBG Dbprintf("after sof_bits rxlen: %d rx[0]: 0x%02X", *rxlen, rx[0]);
|
||||
sof_received = true;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// if we saw over 100 weird values break it probably isn't hitag...
|
||||
// Under some decoding conditions, two bits may be obtained,
|
||||
// so it is necessary to ensure that the remaining buf space can store at least two bits.
|
||||
if (error_count > 100 || (*rxlen + 2) >= sizeofrx * 8) {
|
||||
DBG Dbprintf("hitag receive overflow or error!!");
|
||||
if (errorCount > 100 || (*rxlen) / 8 >= sizeofrx) {
|
||||
break;
|
||||
}
|
||||
|
||||
if (ledcontrol)
|
||||
LED_B_INV();
|
||||
|
||||
if (modulation == AC2K || modulation == AC4K) {
|
||||
// Handle different modulation types
|
||||
// Anticollision Coding
|
||||
if (periods >= t_4half) {
|
||||
// Anticollision Coding example |--__|--__| (00)
|
||||
lastbit = 0;
|
||||
// CLEAR_BIT_MSB(rx, *rxlen);
|
||||
(*rxlen)++;
|
||||
} else if (periods >= t_3half) {
|
||||
// Anticollision Coding example |-_-_|--__| (10) or |--__|-_-_| (01)
|
||||
lastbit = !lastbit;
|
||||
if (lastbit) {
|
||||
SET_BIT_MSB(rx, *rxlen);
|
||||
}
|
||||
(*rxlen)++;
|
||||
|
||||
skip_next_failing_edge = !!lastbit;
|
||||
// Whether to skip the next falling edge depends on whether the current bit is 1 or not
|
||||
} else if (periods >= t_2half) {
|
||||
// Anticollision Coding example |-_-_| (1)
|
||||
if (skip_next_failing_edge == false) {
|
||||
lastbit = 1;
|
||||
SET_BIT_MSB(rx, *rxlen);
|
||||
(*rxlen)++;
|
||||
}
|
||||
|
||||
skip_next_failing_edge = !skip_next_failing_edge;
|
||||
} else {
|
||||
// Ignore weird value, is to small to mean anything
|
||||
error_count++;
|
||||
// We can break this loop if we received the last bit from a frame
|
||||
// max periods between 2 falling edge
|
||||
// RTF AC64 |--__|--__| (00) 64 * T0
|
||||
// RTF MC32 |_-|-_|_-| (010) 48 * T0
|
||||
if (GetLoEdgeCaptureCount() > (T0 * 80)) {
|
||||
if (bStarted) {
|
||||
break;
|
||||
}
|
||||
} else {
|
||||
// Manchester coding (MC4K, MC8K)
|
||||
if (periods >= t_4half) {
|
||||
// Manchester coding example |-_|_-|-_| (101)
|
||||
|
||||
// CLEAR_BIT_MSB(rx, *rxlen);
|
||||
(*rxlen)++;
|
||||
// The function has already been reset to 0 when entering, so there is no need to operate clear
|
||||
|
||||
SET_BIT_MSB(rx, *rxlen);
|
||||
(*rxlen)++;
|
||||
} else if (periods >= t_3half) {
|
||||
// Manchester coding example |_-|...|_-|-_| (0...01)
|
||||
|
||||
// CLEAR_BIT_MSB(rx, *rxlen);
|
||||
(*rxlen)++;
|
||||
// The function has already been reset to 0 when entering, so there is no need to operate clear
|
||||
|
||||
// We have to skip this half period at start and add the 'one' the second time
|
||||
if (skip_next_failing_edge == false) {
|
||||
SET_BIT_MSB(rx, *rxlen);
|
||||
(*rxlen)++;
|
||||
}
|
||||
|
||||
lastbit = !lastbit;
|
||||
skip_next_failing_edge = !skip_next_failing_edge;
|
||||
} else if (periods >= t_2half) {
|
||||
// Manchester coding example |_-|_-| (00) or |-_|-_| (11)
|
||||
// bit is same as last bit
|
||||
if (lastbit) {
|
||||
SET_BIT_MSB(rx, *rxlen);
|
||||
}
|
||||
(*rxlen)++;
|
||||
} else {
|
||||
// Ignore weird value, is to small to mean anything
|
||||
error_count++;
|
||||
}
|
||||
}
|
||||
|
||||
// Handle SOF bits
|
||||
if (sof_received == false && *rxlen >= sof_bits) {
|
||||
// Check if SOF is valid (all bits should be 1)
|
||||
if ((rx[0] >> (8 - sof_bits)) != ((1 << sof_bits) - 1)) {
|
||||
if (sof_bits == 4) {
|
||||
sof_bits = 3;
|
||||
// Hitag µ is LSB first 0b110
|
||||
if ((rx[0] & 0xE0) != 0xC0) {
|
||||
DBG Dbprintf("Warning, SOF is invalid rx[0]: 0x%02X", rx[0]);
|
||||
}
|
||||
} else {
|
||||
DBG Dbprintf("Warning, not all bits of SOF are 1: 0x%02X", rx[0]);
|
||||
}
|
||||
}
|
||||
|
||||
*rxlen -= sof_bits;
|
||||
uint8_t tmp = rx[0];
|
||||
rx[0] = 0x00;
|
||||
for (size_t i = 0; i < *rxlen; i++) {
|
||||
if (TEST_BIT_MSB(&tmp, sof_bits + i)) {
|
||||
SET_BIT_MSB(rx, i);
|
||||
}
|
||||
}
|
||||
// DBG Dbprintf("after sof_bits rxlen: %d rx[0]: 0x%02X", *rxlen, rx[0]);
|
||||
sof_received = true;
|
||||
}
|
||||
}
|
||||
|
||||
DBG {
|
||||
Dbprintf("RX %i:%02X.. resptime:%i, sof_bits: %d", *rxlen, rx[0], *resptime, sof_bits);
|
||||
Dbprintf("bStarted:%d bSkip:%d lastbit:%d sof_received:%d", bStarted, bSkip, lastbit, sof_received);
|
||||
Dbprintf("RX %i:%02X.. resptime:%i", *rxlen, rx[0], *resptime);
|
||||
Dbprintf("Edges count: %d, hex: ", rb_i);
|
||||
Dbhexdump(rb_i, edges, false);
|
||||
}
|
||||
@@ -363,15 +321,17 @@ int hitag_reader_transfer(const uint8_t *tx, size_t txlen, uint8_t *rx, size_t s
|
||||
DBG Dbprintf("tx %d bits:", txlen);
|
||||
DBG Dbhexdump((txlen + 7) / 8, tx, false);
|
||||
|
||||
// Disable input capture to avoid triggers during our own modulation.
|
||||
StopLoEdgeCapture();
|
||||
|
||||
// Wait for HITAG_T_WAIT_SC carrier periods after the last tag bit before transmitting,
|
||||
// Since the clock counts since the last falling edge, a 'one' means that the
|
||||
// falling edge occurred halfway the period. with respect to this falling edge,
|
||||
// we need to wait (T_Wait2 + half_tag_period) when the last was a 'one'.
|
||||
// All timer values are in terms of T0 units
|
||||
lf_wait_periods(t_wait);
|
||||
while (GetPrecisionCounter() < T0 * t_wait) {};
|
||||
|
||||
// TODO DXL 待实现计算时间
|
||||
// start_time = ?
|
||||
start_time = TIMESTAMP;
|
||||
|
||||
// Transmit the reader frame
|
||||
hitag_reader_send_frame(tx, txlen, ledcontrol, send_sof);
|
||||
@@ -383,10 +343,13 @@ int hitag_reader_transfer(const uint8_t *tx, size_t txlen, uint8_t *rx, size_t s
|
||||
|
||||
LogTraceBits(tx, txlen, start_time, TIMESTAMP, true);
|
||||
|
||||
// Enable and reset input capture for capturing the tag response.
|
||||
EnableLoEdgeCapture();
|
||||
|
||||
hitag_reader_receive_frame(rx, sizeofrx, rxlen, &start_time, ledcontrol, modulation, sof_bits);
|
||||
|
||||
DBG Dbprintf("rx %d bits:", *rxlen);
|
||||
DBG Dbhexdump((int) (*rxlen + 7) / 8, rx, false);
|
||||
DBG Dbhexdump((int)(*rxlen + 7) / 8, rx, false);
|
||||
|
||||
// Check if frame was captured and store it
|
||||
if (*rxlen > 0) {
|
||||
@@ -412,39 +375,32 @@ int hitag_reader_transfer(const uint8_t *tx, size_t txlen, uint8_t *rx, size_t s
|
||||
// Tag functions - depends on modulation type
|
||||
static void hitag_tag_send_bit(int bit, hitag_mod_t modulation, bool ledcontrol) {
|
||||
// Reset clock for the next bit
|
||||
AT91C_BASE_TC0->TC_CCR = AT91C_TC_SWTRG;
|
||||
ResetPrecisionCounter();
|
||||
|
||||
if (ledcontrol)
|
||||
LED_A_ON();
|
||||
if (ledcontrol) LED_A_ON();
|
||||
|
||||
switch (modulation) {
|
||||
case AC2K: {
|
||||
if (bit == 0) {
|
||||
// AC Coding --__
|
||||
Gpio_SSC_DOUT_High();
|
||||
while (AT91C_BASE_TC0->TC_CV < T0 * 32) {
|
||||
};
|
||||
while (GetPrecisionCounter() < T0 * 32) {};
|
||||
|
||||
Gpio_SSC_DOUT_Low();
|
||||
while (AT91C_BASE_TC0->TC_CV < T0 * 64) {
|
||||
};
|
||||
while (GetPrecisionCounter() < T0 * 64) {};
|
||||
} else {
|
||||
// AC coding -_-_
|
||||
Gpio_SSC_DOUT_High();
|
||||
while (AT91C_BASE_TC0->TC_CV < T0 * 16) {
|
||||
};
|
||||
while (GetPrecisionCounter() < T0 * 16) {};
|
||||
|
||||
Gpio_SSC_DOUT_Low();
|
||||
while (AT91C_BASE_TC0->TC_CV < T0 * 32) {
|
||||
};
|
||||
while (GetPrecisionCounter() < T0 * 32) {};
|
||||
|
||||
Gpio_SSC_DOUT_High();
|
||||
while (AT91C_BASE_TC0->TC_CV < T0 * 48) {
|
||||
};
|
||||
while (GetPrecisionCounter() < T0 * 48) {};
|
||||
|
||||
Gpio_SSC_DOUT_Low();
|
||||
while (AT91C_BASE_TC0->TC_CV < T0 * 64) {
|
||||
};
|
||||
while (GetPrecisionCounter() < T0 * 64) {};
|
||||
}
|
||||
break;
|
||||
}
|
||||
@@ -452,29 +408,23 @@ static void hitag_tag_send_bit(int bit, hitag_mod_t modulation, bool ledcontrol)
|
||||
if (bit == 0) {
|
||||
// AC Coding --__
|
||||
Gpio_SSC_DOUT_High();
|
||||
while (AT91C_BASE_TC0->TC_CV < T0 * HITAG_T_TAG_HALF_PERIOD) {
|
||||
};
|
||||
while (GetPrecisionCounter() < T0 * HITAG_T_TAG_HALF_PERIOD) {};
|
||||
|
||||
Gpio_SSC_DOUT_Low();
|
||||
while (AT91C_BASE_TC0->TC_CV < T0 * HITAG_T_TAG_FULL_PERIOD) {
|
||||
};
|
||||
while (GetPrecisionCounter() < T0 * HITAG_T_TAG_FULL_PERIOD) {};
|
||||
} else {
|
||||
// AC coding -_-_
|
||||
Gpio_SSC_DOUT_High();
|
||||
while (AT91C_BASE_TC0->TC_CV < T0 * 8) {
|
||||
};
|
||||
while (GetPrecisionCounter() < T0 * 8) {};
|
||||
|
||||
Gpio_SSC_DOUT_Low();
|
||||
while (AT91C_BASE_TC0->TC_CV < T0 * 16) {
|
||||
};
|
||||
while (GetPrecisionCounter() < T0 * 16) {};
|
||||
|
||||
Gpio_SSC_DOUT_High();
|
||||
while (AT91C_BASE_TC0->TC_CV < T0 * 24) {
|
||||
};
|
||||
while (GetPrecisionCounter() < T0 * 24) {};
|
||||
|
||||
Gpio_SSC_DOUT_Low();
|
||||
while (AT91C_BASE_TC0->TC_CV < T0 * 32) {
|
||||
};
|
||||
while (GetPrecisionCounter() < T0 * 32) {};
|
||||
}
|
||||
break;
|
||||
}
|
||||
@@ -482,21 +432,17 @@ static void hitag_tag_send_bit(int bit, hitag_mod_t modulation, bool ledcontrol)
|
||||
if (bit == 0) {
|
||||
// Manchester: Unloaded, then loaded |__--|
|
||||
Gpio_SSC_DOUT_Low();
|
||||
while (AT91C_BASE_TC0->TC_CV < T0 * 16) {
|
||||
};
|
||||
while (GetPrecisionCounter() < T0 * 16) {};
|
||||
|
||||
Gpio_SSC_DOUT_High();
|
||||
while (AT91C_BASE_TC0->TC_CV < T0 * 32) {
|
||||
};
|
||||
while (GetPrecisionCounter() < T0 * 32) {};
|
||||
} else {
|
||||
// Manchester: Loaded, then unloaded |--__|
|
||||
Gpio_SSC_DOUT_High();
|
||||
while (AT91C_BASE_TC0->TC_CV < T0 * 16) {
|
||||
};
|
||||
while (GetPrecisionCounter() < T0 * 16) {};
|
||||
|
||||
Gpio_SSC_DOUT_Low();
|
||||
while (AT91C_BASE_TC0->TC_CV < T0 * 32) {
|
||||
};
|
||||
while (GetPrecisionCounter() < T0 * 32) {};
|
||||
}
|
||||
break;
|
||||
}
|
||||
@@ -504,53 +450,49 @@ static void hitag_tag_send_bit(int bit, hitag_mod_t modulation, bool ledcontrol)
|
||||
if (bit == 0) {
|
||||
// Manchester: Unloaded, then loaded |__--|
|
||||
Gpio_SSC_DOUT_Low();
|
||||
while (AT91C_BASE_TC0->TC_CV < T0 * 8) {
|
||||
};
|
||||
while (GetPrecisionCounter() < T0 * 8) {};
|
||||
|
||||
Gpio_SSC_DOUT_High();
|
||||
while (AT91C_BASE_TC0->TC_CV < T0 * 16) {
|
||||
};
|
||||
while (GetPrecisionCounter() < T0 * 16) {};
|
||||
} else {
|
||||
// Manchester: Loaded, then unloaded |--__|
|
||||
Gpio_SSC_DOUT_High();
|
||||
while (AT91C_BASE_TC0->TC_CV < T0 * 8) {
|
||||
};
|
||||
while (GetPrecisionCounter() < T0 * 8) {};
|
||||
|
||||
Gpio_SSC_DOUT_Low();
|
||||
while (AT91C_BASE_TC0->TC_CV < T0 * 16) {
|
||||
};
|
||||
while (GetPrecisionCounter() < T0 * 16) {};
|
||||
}
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
if (ledcontrol)
|
||||
LED_A_OFF();
|
||||
if (ledcontrol) LED_A_OFF();
|
||||
}
|
||||
|
||||
void hitag_tag_receive_frame(uint8_t *rx, size_t sizeofrx, size_t *rxlen, uint32_t *start_time, bool ledcontrol,
|
||||
int *overflow) {
|
||||
uint16_t next_edge_event = AT91C_TC_LDRBS;
|
||||
void hitag_tag_receive_frame(uint8_t *rx, size_t sizeofrx, size_t *rxlen, uint32_t *start_time, bool ledcontrol, int *overflow) {
|
||||
lo_edge_t next_edge = LO_EDGE_FALLING;
|
||||
uint8_t edges[160] = {0};
|
||||
uint32_t rb_i = 0;
|
||||
|
||||
// Receive frame, watch for at most T0*EOF periods
|
||||
while (AT91C_BASE_TC1->TC_CV < T0 * HITAG_T_EOF) {
|
||||
// Check if edge in modulation is detected
|
||||
if (AT91C_BASE_TC1->TC_SR & next_edge_event) {
|
||||
next_edge_event = next_edge_event ^ (AT91C_TC_LDRAS | AT91C_TC_LDRBS);
|
||||
while (GetLoEdgeCaptureCount() < T0 * HITAG_T_EOF) {
|
||||
|
||||
// only use AT91C_TC_LDRBS falling edge for now
|
||||
if (next_edge_event == AT91C_TC_LDRBS) continue;
|
||||
// Check if edge in modulation is detected
|
||||
if (GetLoEdgeCaptureStatus() == next_edge) {
|
||||
next_edge = next_edge == LO_EDGE_RISING ? LO_EDGE_FALLING : LO_EDGE_RISING;
|
||||
|
||||
// only use INPUT_CAPTURE_EVT_RB falling edge for now
|
||||
if (next_edge == LO_EDGE_FALLING) {
|
||||
continue;
|
||||
}
|
||||
|
||||
// Retrieve the new timing values
|
||||
uint32_t rb = AT91C_BASE_TC1->TC_RB / T0 + *overflow;
|
||||
uint32_t rb = GetLoEdgeCaptureFalling() / T0 + *overflow;
|
||||
*overflow = 0;
|
||||
|
||||
edges[rb_i++] = rb;
|
||||
|
||||
if (ledcontrol)
|
||||
LED_B_INV();
|
||||
if (ledcontrol) LED_B_INV();
|
||||
|
||||
// Capture reader cmd start timestamp
|
||||
if (*start_time == 0) {
|
||||
@@ -579,33 +521,28 @@ void hitag_tag_receive_frame(uint8_t *rx, size_t sizeofrx, size_t *rxlen, uint32
|
||||
}
|
||||
}
|
||||
|
||||
if (ledcontrol)
|
||||
LED_B_OFF();
|
||||
if (ledcontrol) LED_B_OFF();
|
||||
|
||||
DBG
|
||||
if (rb_i) {
|
||||
Dbprintf("RX %i bits.. start_time:%i edges:", *rxlen, *start_time);
|
||||
Dbhexdump(rb_i, edges, false);
|
||||
}
|
||||
DBG if (rb_i) {
|
||||
Dbprintf("RX %i bits.. start_time:%i edges:", *rxlen, *start_time);
|
||||
Dbhexdump(rb_i, edges, false);
|
||||
}
|
||||
}
|
||||
|
||||
void hitag_tag_send_frame(const uint8_t *frame, size_t frame_len, int sof_bits, hitag_mod_t modulation,
|
||||
bool ledcontrol) {
|
||||
void hitag_tag_send_frame(const uint8_t *frame, size_t frame_len, int sof_bits, hitag_mod_t modulation, bool ledcontrol) {
|
||||
// The beginning of the frame is hidden in some high level; pause until our bits will have an effect
|
||||
AT91C_BASE_TC0->TC_CCR = AT91C_TC_SWTRG;
|
||||
ResetPrecisionCounter();
|
||||
Gpio_SSC_DOUT_High();
|
||||
|
||||
switch (modulation) {
|
||||
case AC4K:
|
||||
case MC8K: {
|
||||
while (AT91C_BASE_TC0->TC_CV < T0 * 40) {
|
||||
}; // FADV
|
||||
while (GetPrecisionCounter() < T0 * 40) {}; // FADV
|
||||
break;
|
||||
}
|
||||
case AC2K:
|
||||
case MC4K: {
|
||||
while (AT91C_BASE_TC0->TC_CV < T0 * 20) {
|
||||
}; // STD + ADV
|
||||
while (GetPrecisionCounter() < T0 * 20) {}; // STD + ADV
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
+4
-11
@@ -8,29 +8,23 @@
|
||||
// Hitag units (T0) have duration of 8 microseconds (us), which is 1/125000 per second (carrier)
|
||||
// T0 = TIMER_CLOCK3 / 125000 = 12
|
||||
|
||||
#define T0 12 // 12 timer period ~= 8us
|
||||
#define T0 12
|
||||
|
||||
#define HITAG_FRAME_LEN 20
|
||||
|
||||
// TC0 and TC1 are 16-bit counters and will overflow after 5461 * T0
|
||||
// Ensure not to set these timings above 5461 (~43ms) when comparing without considering overflow, as they will never reach that value.
|
||||
|
||||
// Gap time between bits
|
||||
#define HITAG_T_LOW 8 /* T_LOW should be 4..10 */
|
||||
// Logic 0 Bit length
|
||||
#define HITAG_T_0 20 /* T[0] should be 18..22 */
|
||||
// Logic 1 Bit length
|
||||
#define HITAG_T_1 28 /* T[1] should be 26..32 */
|
||||
#define HITAG_T_0_MIN 15 /* T[0] should be 18..22 */
|
||||
#define HITAG_T_1_MIN 25 /* T[1] should be 26..32 */
|
||||
// Duration for end of frame condition
|
||||
#define HITAG_T_STOP 36 /* T_EOF should be > 36 */
|
||||
#define HITAG_T_CODE_VIOLATION 36 /* Hitag µ TFcv should be 34..38 */
|
||||
#define HITAG_T_EOF 80 /* T_EOF should be > 36 */
|
||||
|
||||
#define HITAG_T_WAIT_RESP 200 /* T_wresp should be 204..212 */
|
||||
|
||||
// Read/Write Device waiting time before sending a subsequent command
|
||||
#define HITAG_T_WAIT_SC 200 /* T_wsc should be 90..5000 */
|
||||
// Read/Write Device waiting time before sending the first command
|
||||
#define HITAG_T_WAIT_FIRST 300 /* T_wfc should be 280..565 (T_ttf) */
|
||||
@@ -50,9 +44,8 @@
|
||||
#define HITAG_T_TAG_CAPTURE_THREE_HALF 41
|
||||
#define HITAG_T_TAG_CAPTURE_FOUR_HALF 57
|
||||
|
||||
extern uint16_t timestamp_high;
|
||||
#define TIMESTAMP 0 // TODO DXL 时间戳暂时全部归零,后期时间计算手动完成
|
||||
// #define TIMESTAMP ( (AT91C_BASE_TC2->TC_SR & AT91C_TC_COVFS) ? timestamp_high += 1 : 0, ((timestamp_high << 16) + AT91C_BASE_TC2->TC_CV) / T0)
|
||||
// Trace timestamp in T0 units, provided by the timers HAL (GetTimestamp).
|
||||
#define TIMESTAMP GetTimestamp()
|
||||
|
||||
// Common hitag functions
|
||||
void hitag_setup_fpga(uint16_t conf, uint8_t threshold, bool ledcontrol);
|
||||
@@ -61,7 +54,7 @@ void hitag_reader_send_frame(const uint8_t *frame, size_t frame_len, bool ledcon
|
||||
void hitag_reader_receive_frame(uint8_t *rx, size_t sizeofrx, size_t *rxlen, uint32_t *resptime, bool ledcontrol, hitag_mod_t modulation,
|
||||
int sof_bits);
|
||||
int hitag_reader_transfer(const uint8_t *tx, size_t txlen, uint8_t *rx, size_t sizeofrx, size_t *rxlen, int t_wait,
|
||||
bool ledcontrol, hitag_mod_t modulation, uint8_t sof_bits, uint8_t send_sof);
|
||||
bool ledcontrol, hitag_mod_t modulation, uint8_t sof_bits, uint8_t send_sof);
|
||||
void hitag_tag_receive_frame(uint8_t *rx, size_t sizeofrx, size_t *rxlen, uint32_t *start_time, bool ledcontrol, int *overflow);
|
||||
void hitag_tag_send_frame(const uint8_t *frame, size_t frame_len, int sof_bits, hitag_mod_t modulation, bool ledcontrol);
|
||||
|
||||
|
||||
+6
-6
@@ -418,7 +418,7 @@ void htu_simulate(bool tag_mem_supplied, int8_t threshold, const uint8_t *data,
|
||||
LogTraceBits(rx, rxlen, start_time, TIMESTAMP, true);
|
||||
|
||||
// Disable timer 1 with external trigger to avoid triggers during our own modulation
|
||||
StopInputCapture();
|
||||
StopLoEdgeCapture();
|
||||
|
||||
// Prepare tag response (tx)
|
||||
memset(tx, 0x00, sizeof(tx));
|
||||
@@ -444,7 +444,7 @@ void htu_simulate(bool tag_mem_supplied, int8_t threshold, const uint8_t *data,
|
||||
}
|
||||
|
||||
// Enable and reset external trigger in timer for capturing future frames
|
||||
EnableInputCapture();
|
||||
EnableLoEdgeCapture();
|
||||
|
||||
// Reset the received frame and response timing info
|
||||
memset(rx, 0x00, sizeof(rx));
|
||||
@@ -453,9 +453,9 @@ void htu_simulate(bool tag_mem_supplied, int8_t threshold, const uint8_t *data,
|
||||
// Reset the frame length
|
||||
rxlen = 0;
|
||||
// Save the timer overflow, will be 0 when frame was received
|
||||
overflow += (GetInputCaptureCount() / T0);
|
||||
overflow += (GetLoEdgeCaptureCount() / T0);
|
||||
// Reset the timer to restart while-loop that receives frames
|
||||
ResetInputCapture();
|
||||
ResetLoEdgeCapture();
|
||||
}
|
||||
|
||||
hitag_cleanup(ledcontrol);
|
||||
@@ -474,7 +474,7 @@ static int htu_reader_send_receive(uint8_t *tx, size_t txlen, uint8_t *rx, size_
|
||||
memset(rx, 0x00, sizeofrx);
|
||||
|
||||
// Disable timer 1 with external trigger to avoid triggers during our own modulation
|
||||
StopInputCapture();
|
||||
StopLoEdgeCapture();
|
||||
|
||||
DBG Dbprintf("tx %d bits:", txlen);
|
||||
DBG Dbhexdump((txlen + 7) / 8, tx, false);
|
||||
@@ -495,7 +495,7 @@ static int htu_reader_send_receive(uint8_t *tx, size_t txlen, uint8_t *rx, size_
|
||||
LogTraceBits(tx, txlen, start_time, TIMESTAMP, true);
|
||||
|
||||
// Enable and reset external trigger in timer for capturing future frames
|
||||
EnableInputCapture();
|
||||
EnableLoEdgeCapture();
|
||||
|
||||
// Capture response - SOF is automatically stripped by hitag_reader_receive_frame
|
||||
hitag_reader_receive_frame(rx, sizeofrx, rxlen, &start_time, ledcontrol, modulation, sof_bits);
|
||||
|
||||
Reference in new issue
Block a user