From 7904d962aaff9c40e3253858bfb0f59b66ff804a Mon Sep 17 00:00:00 2001 From: iceman1001 Date: Sat, 5 Sep 2020 12:50:30 +0200 Subject: [PATCH] timeout in waiting loops 4x50 --- armsrc/em4x50.c | 42 +++++++++++++++++++++++++++--------------- 1 file changed, 27 insertions(+), 15 deletions(-) diff --git a/armsrc/em4x50.c b/armsrc/em4x50.c index bcc646fcc..45ffc8bbd 100644 --- a/armsrc/em4x50.c +++ b/armsrc/em4x50.c @@ -313,22 +313,36 @@ static int get_next_bit(void) { static uint32_t get_pulse_length(void) { + int32_t timeout = (T0 * 3 * EM4X50_T_TAG_FULL_PERIOD); + // iterates pulse length (low -> high -> low) - uint8_t sample = 0; + volatile uint8_t sample = (uint8_t)AT91C_BASE_SSC->SSC_RHR; - sample = (uint8_t)AT91C_BASE_SSC->SSC_RHR; - - while (sample > gLow) + while (sample > gLow || (timeout--)) { sample = (uint8_t)AT91C_BASE_SSC->SSC_RHR; + } + + if (timeout == 0) + return 0; AT91C_BASE_TC1->TC_CCR = AT91C_TC_SWTRG; + timeout = (T0 * 3 * EM4X50_T_TAG_FULL_PERIOD); - while (sample < gHigh) + while (sample < gHigh || (timeout--)) { sample = (uint8_t)AT91C_BASE_SSC->SSC_RHR; + } - while (sample > gLow) + if (timeout == 0) + return 0; + + timeout = (T0 * 3 * EM4X50_T_TAG_FULL_PERIOD); + while (sample > gLow || (timeout--)) { sample = (uint8_t)AT91C_BASE_SSC->SSC_RHR; + } + + if (timeout == 0) + return 0; return (uint32_t)AT91C_BASE_TC1->TC_CV; } @@ -413,13 +427,13 @@ static void em4x50_send_word(const uint8_t bytes[4]) { em4x50_send_bit(0); } -static bool find_single_listen_window(int16_t timeout) { +static bool find_single_listen_window(void) { // find single listen window int cnt_pulses = 0; - while (cnt_pulses < EM4X50_T_WAITING_FOR_SNGLLIW && timeout--) { + while (cnt_pulses < EM4X50_T_WAITING_FOR_SNGLLIW) { // identification of listen window is done via evaluation of // pulse lengths @@ -432,16 +446,14 @@ static bool find_single_listen_window(int16_t timeout) { } } else { - cnt_pulses++; - } } return false; } -static bool find_double_listen_window(bool bcommand, int16_t timeout) { +static bool find_double_listen_window(bool bcommand) { // find two successive listen windows that indicate the beginning of // data transmission @@ -451,7 +463,7 @@ static bool find_double_listen_window(bool bcommand, int16_t timeout) { int cnt_pulses = 0; - while (cnt_pulses < EM4X50_T_WAITING_FOR_DBLLIW && timeout--) { + while (cnt_pulses < EM4X50_T_WAITING_FOR_DBLLIW) { // identification of listen window is done via evaluation of // pulse lengths @@ -508,7 +520,7 @@ static bool find_em4x50_tag(void) { // function is used to check wether a tag on the proxmark is an // EM4x50 tag or not -> speed up "lf search" process - return (find_single_listen_window(EM4X50_COMMAND_TIMEOUT)); + return (find_single_listen_window()); } @@ -520,7 +532,7 @@ static bool request_receive_mode(void) { bool bcommand = true; - return find_double_listen_window(bcommand, EM4X50_COMMAND_TIMEOUT); + return find_double_listen_window(bcommand); } static bool check_ack(bool bliw) { @@ -726,7 +738,7 @@ static bool standard_read(int *now) { uint8_t bits[EM4X50_TAG_WORD] = {0}; // start with the identification of two succsessive listening windows - if (find_double_listen_window(false, EM4X50_COMMAND_TIMEOUT)) { + if (find_double_listen_window(false)) { // read and save words until following double listen window is detected while (get_word_from_bitstream(bits) == EM4X50_TAG_WORD)