From 3b12ba2e9319706e23f2b041262781146c20dc49 Mon Sep 17 00:00:00 2001 From: Philippe Teuwen Date: Mon, 3 Jun 2019 00:01:08 +0200 Subject: [PATCH] replace usb_poll_validate_length() by data_available() that supports USART too --- armsrc/Standalone/hf_young.c | 6 +++--- armsrc/Standalone/lf_hidbrute.c | 2 +- armsrc/Standalone/lf_icerun.c | 2 +- armsrc/Standalone/lf_proxbrute.c | 2 +- armsrc/Standalone/lf_samyrun.c | 2 +- armsrc/Standalone/readme.md | 2 +- armsrc/felica.c | 2 +- armsrc/hfsnoop.c | 2 +- armsrc/hitag2.c | 8 ++++---- armsrc/hitagS.c | 6 +++--- armsrc/iclass.c | 8 ++++---- armsrc/iso14443a.c | 6 +++--- armsrc/iso14443b.c | 2 +- armsrc/iso15693.c | 2 +- armsrc/legicrfsim.c | 2 +- armsrc/lfops.c | 14 +++++++------- armsrc/lfsampling.c | 8 ++++---- armsrc/mifarecmd.c | 4 ++-- armsrc/mifaresim.c | 2 +- armsrc/util.c | 8 ++++++++ armsrc/util.h | 1 + 21 files changed, 50 insertions(+), 41 deletions(-) diff --git a/armsrc/Standalone/hf_young.c b/armsrc/Standalone/hf_young.c index e2afec266..59d1750b9 100644 --- a/armsrc/Standalone/hf_young.c +++ b/armsrc/Standalone/hf_young.c @@ -39,7 +39,7 @@ void RunMod() { for (;;) { WDT_HIT(); // exit from Standalone Mode, send a usbcommand. - if (usb_poll_validate_length()) return; + if (data_available()) return; SpinDelay(300); @@ -57,7 +57,7 @@ void RunMod() { for (;;) { // exit from Standalone Mode, send a usbcommand. - if (usb_poll_validate_length()) return; + if (data_available()) return; if (BUTTON_PRESS()) { if (cardRead[selected]) { @@ -204,7 +204,7 @@ void RunMod() { DbpString("Playing"); for (; ;) { // exit from Standalone Mode, send a usbcommand. - if (usb_poll_validate_length()) return; + if (data_available()) return; int button_action = BUTTON_HELD(1000); if (button_action == 0) { // No button action, proceed with sim diff --git a/armsrc/Standalone/lf_hidbrute.c b/armsrc/Standalone/lf_hidbrute.c index 3a7866821..06b0049ad 100644 --- a/armsrc/Standalone/lf_hidbrute.c +++ b/armsrc/Standalone/lf_hidbrute.c @@ -48,7 +48,7 @@ void RunMod() { WDT_HIT(); // exit from SamyRun, send a usbcommand. - if (usb_poll_validate_length()) break; + if (data_available()) break; // Was our button held down or pressed? int button_pressed = BUTTON_HELD(1000); diff --git a/armsrc/Standalone/lf_icerun.c b/armsrc/Standalone/lf_icerun.c index 5ce5a7955..e62c79329 100644 --- a/armsrc/Standalone/lf_icerun.c +++ b/armsrc/Standalone/lf_icerun.c @@ -24,7 +24,7 @@ void RunMod() { WDT_HIT(); // exit from IceRun, send a usbcommand. - if (usb_poll_validate_length()) break; + if (data_available()) break; // Was our button held down or pressed? int button_pressed = BUTTON_HELD(1000); diff --git a/armsrc/Standalone/lf_proxbrute.c b/armsrc/Standalone/lf_proxbrute.c index 3336e9ee9..d10a1fbfe 100644 --- a/armsrc/Standalone/lf_proxbrute.c +++ b/armsrc/Standalone/lf_proxbrute.c @@ -33,7 +33,7 @@ void RunMod() { WDT_HIT(); // exit from SamyRun, send a usbcommand. - if (usb_poll_validate_length()) break; + if (data_available()) break; // Was our button held down or pressed? int button_pressed = BUTTON_HELD(1000); diff --git a/armsrc/Standalone/lf_samyrun.c b/armsrc/Standalone/lf_samyrun.c index e1ba185d1..03c2ddd12 100644 --- a/armsrc/Standalone/lf_samyrun.c +++ b/armsrc/Standalone/lf_samyrun.c @@ -32,7 +32,7 @@ void RunMod() { WDT_HIT(); // exit from SamyRun, send a usbcommand. - if (usb_poll_validate_length()) break; + if (data_available()) break; // Was our button held down or pressed? int button_pressed = BUTTON_HELD(1000); diff --git a/armsrc/Standalone/readme.md b/armsrc/Standalone/readme.md index 491d4ab4d..529bc7f70 100644 --- a/armsrc/Standalone/readme.md +++ b/armsrc/Standalone/readme.md @@ -34,7 +34,7 @@ void RunMod(void) { WDT_HIT(); // exit from standalone mode, just send a usbcommand - if (usb_poll_validate_length()) break; + if (data_available()) break; // do your standalone stuff.. } diff --git a/armsrc/felica.c b/armsrc/felica.c index 6efff73e0..58d976774 100644 --- a/armsrc/felica.c +++ b/armsrc/felica.c @@ -731,7 +731,7 @@ void felica_dump_lite_s() { uint16_t cnt = 0, cntfails = 0; uint8_t *dest = BigBuf_get_addr(); - while (!BUTTON_PRESS() && !usb_poll_validate_length()) { + while (!BUTTON_PRESS() && !data_available()) { WDT_HIT(); diff --git a/armsrc/hfsnoop.c b/armsrc/hfsnoop.c index 11ceff3d9..bb5d8699c 100644 --- a/armsrc/hfsnoop.c +++ b/armsrc/hfsnoop.c @@ -46,7 +46,7 @@ void HfSniff(int samplesToSkip, int triggersToSkip) { SpinDelay(100); uint16_t r = 0; - while (!BUTTON_PRESS() && !usb_poll_validate_length()) { + while (!BUTTON_PRESS() && !data_available()) { WDT_HIT(); if (AT91C_BASE_SSC->SSC_SR & (AT91C_SSC_RXRDY)) { diff --git a/armsrc/hitag2.c b/armsrc/hitag2.c index e291ece36..4f3a1856d 100644 --- a/armsrc/hitag2.c +++ b/armsrc/hitag2.c @@ -751,7 +751,7 @@ void SniffHitag(void) { bSkip = true; tag_sof = 4; - while (!BUTTON_PRESS() && !usb_poll_validate_length()) { + while (!BUTTON_PRESS() && !data_available()) { // Watchdog hit WDT_HIT(); @@ -978,7 +978,7 @@ void SimulateHitagTag(bool tag_mem_supplied, uint8_t *data) { // synchronized startup procedure while (AT91C_BASE_TC0->TC_CV > 0) {}; // wait until TC0 returned to zero - while (!BUTTON_PRESS() && !usb_poll_validate_length()) { + while (!BUTTON_PRESS() && !data_available()) { // Watchdog hit WDT_HIT(); @@ -1203,7 +1203,7 @@ void ReaderHitag(hitag_function htf, hitag_data *htd) { } uint8_t attempt_count = 0; - while (!bStop && !BUTTON_PRESS() && !usb_poll_validate_length()) { + while (!bStop && !BUTTON_PRESS() && !data_available()) { WDT_HIT(); @@ -1464,7 +1464,7 @@ void WriterHitag(hitag_function htf, hitag_data *htd, int page) { return; } - while (!bStop && !BUTTON_PRESS() && !usb_poll_validate_length()) { + while (!bStop && !BUTTON_PRESS() && !data_available()) { WDT_HIT(); diff --git a/armsrc/hitagS.c b/armsrc/hitagS.c index fc38d10b3..83263bbfd 100644 --- a/armsrc/hitagS.c +++ b/armsrc/hitagS.c @@ -1022,7 +1022,7 @@ void SimulateHitagSTag(bool tag_mem_supplied, uint8_t *data) { // synchronized startup procedure while (AT91C_BASE_TC0->TC_CV > 0); // wait until TC0 returned to zero - while (!BUTTON_PRESS() && !usb_poll_validate_length()) { + while (!BUTTON_PRESS() && !data_available()) { WDT_HIT(); @@ -1228,7 +1228,7 @@ void ReadHitagS(hitag_function htf, hitag_data *htd) { // Reset the received frame, frame count and timing info t_wait = 200; - while (!bStop && !BUTTON_PRESS() && !usb_poll_validate_length()) { + while (!bStop && !BUTTON_PRESS() && !data_available()) { WDT_HIT(); @@ -1558,7 +1558,7 @@ void WritePageHitagS(hitag_function htf, hitag_data *htd, int page) { reset_sof = 1; t_wait = 200; - while (!bStop && !BUTTON_PRESS() && !usb_poll_validate_length()) { + while (!bStop && !BUTTON_PRESS() && !data_available()) { WDT_HIT(); diff --git a/armsrc/iclass.c b/armsrc/iclass.c index 03b4b408f..c12c9dc1f 100644 --- a/armsrc/iclass.c +++ b/armsrc/iclass.c @@ -1993,7 +1993,7 @@ void ReaderIClass(uint8_t arg0) { setupIclassReader(); - bool userCancelled = BUTTON_PRESS() || usb_poll_validate_length(); + bool userCancelled = BUTTON_PRESS() || data_available(); while (!userCancelled) { WDT_HIT(); @@ -2092,7 +2092,7 @@ void ReaderIClass(uint8_t arg0) { } } LED_B_OFF(); - userCancelled = BUTTON_PRESS() || usb_poll_validate_length(); + userCancelled = BUTTON_PRESS() || data_available(); } if (userCancelled) { @@ -2290,7 +2290,7 @@ void iClass_Authentication_fast(uint64_t arg0, uint64_t arg1, uint8_t *datain) { uint8_t startup_limit = 10; while (read_status != 2) { - if (BUTTON_PRESS() && !usb_poll_validate_length()) goto out; + if (BUTTON_PRESS() && !data_available()) goto out; read_status = handshakeIclassTag_ext(card_data, use_credit_key); if (startup_limit-- == 0) { @@ -2305,7 +2305,7 @@ void iClass_Authentication_fast(uint64_t arg0, uint64_t arg1, uint8_t *datain) { for (i = 0; i < keyCount; i++) { // Allow button press / usb cmd to interrupt device - if (BUTTON_PRESS() && !usb_poll_validate_length()) break; + if (BUTTON_PRESS() && !data_available()) break; WDT_HIT(); LED_B_ON(); diff --git a/armsrc/iso14443a.c b/armsrc/iso14443a.c index 763eada77..8d5bceccc 100644 --- a/armsrc/iso14443a.c +++ b/armsrc/iso14443a.c @@ -762,7 +762,7 @@ static bool GetIso14443aCommandFromReader(uint8_t *received, uint8_t *par, int * for (;;) { if ( check == 1000 ) { - if ( BUTTON_PRESS() || usb_poll_validate_length() ) + if ( BUTTON_PRESS() || data_available() ) return false; check = 0; } @@ -1722,7 +1722,7 @@ int EmGetCmd(uint8_t *received, uint16_t *len, uint8_t *par) { WDT_HIT(); if ( check == 1000 ) { - if (BUTTON_PRESS() || usb_poll_validate_length()) + if (BUTTON_PRESS() || data_available()) return 1; check = 0; } @@ -2923,7 +2923,7 @@ void DetectNACKbug() { WDT_HIT(); // Test if the action was cancelled - if (BUTTON_PRESS() || usb_poll_validate_length()) { + if (BUTTON_PRESS() || data_available()) { status = PM3_EOPABORTED; break; } diff --git a/armsrc/iso14443b.c b/armsrc/iso14443b.c index ee90b7729..ffdfc41dd 100644 --- a/armsrc/iso14443b.c +++ b/armsrc/iso14443b.c @@ -619,7 +619,7 @@ void SimulateIso14443bTag(uint32_t pupi) { memcpy(encodedOK, ToSend, ToSendMax); // Simulation loop - while (!BUTTON_PRESS() && !usb_poll_validate_length()) { + while (!BUTTON_PRESS() && !data_available()) { WDT_HIT(); // find reader field diff --git a/armsrc/iso15693.c b/armsrc/iso15693.c index f1a06a7bb..c7d15a824 100644 --- a/armsrc/iso15693.c +++ b/armsrc/iso15693.c @@ -891,7 +891,7 @@ void SimTagIso15693(uint32_t parameter, uint8_t *uid) { uint8_t cmd[CMD_INV_RESP] = {0}; BuildInventoryResponse(cmd, uid); - while (!BUTTON_PRESS() && !usb_poll_validate_length()) { + while (!BUTTON_PRESS() && !data_available()) { WDT_HIT(); // Listen to reader diff --git a/armsrc/legicrfsim.c b/armsrc/legicrfsim.c index c32558921..0500e3e32 100644 --- a/armsrc/legicrfsim.c +++ b/armsrc/legicrfsim.c @@ -459,7 +459,7 @@ void LegicRfSimulate(uint8_t cardtype) { LED_A_ON(); DbpString("Starting Legic emulator, press button to end"); - while (!BUTTON_PRESS() && !usb_poll_validate_length()) { + while (!BUTTON_PRESS() && !data_available()) { WDT_HIT(); // wait for carrier, restart after timeout diff --git a/armsrc/lfops.c b/armsrc/lfops.c index a45c20d16..870b56303 100644 --- a/armsrc/lfops.c +++ b/armsrc/lfops.c @@ -607,7 +607,7 @@ void SimulateTagLowFrequencyEx(int period, int gap, bool ledcontrol, int numcycl while (!(AT91C_BASE_PIOA->PIO_PDSR & GPIO_SSC_CLK)) { WDT_HIT(); if ( check == 1000) { - if (usb_poll_validate_length() || BUTTON_PRESS()) + if (data_available() || BUTTON_PRESS()) goto OUT; check = 0; } @@ -627,7 +627,7 @@ void SimulateTagLowFrequencyEx(int period, int gap, bool ledcontrol, int numcycl while (AT91C_BASE_PIOA->PIO_PDSR & GPIO_SSC_CLK) { WDT_HIT(); if (check == 1000) { - if (usb_poll_validate_length() || BUTTON_PRESS()) + if (data_available() || BUTTON_PRESS()) goto OUT; check = 0; } @@ -1011,7 +1011,7 @@ void CmdHIDdemodFSK(int findone, uint32_t *high, uint32_t *low, int ledcontrol) //clear read buffer BigBuf_Clear_keep_EM(); - while (!BUTTON_PRESS() && !usb_poll_validate_length()) { + while (!BUTTON_PRESS() && !data_available()) { WDT_HIT(); if (ledcontrol) LED_A_ON(); @@ -1105,7 +1105,7 @@ void CmdAWIDdemodFSK(int findone, uint32_t *high, uint32_t *low, int ledcontrol) LFSetupFPGAForADC(95, true); - while (!BUTTON_PRESS() && !usb_poll_validate_length()) { + while (!BUTTON_PRESS() && !data_available()) { WDT_HIT(); if (ledcontrol) LED_A_ON(); @@ -1197,7 +1197,7 @@ void CmdEM410xdemod(int findone, uint32_t *high, uint64_t *low, int ledcontrol) LFSetupFPGAForADC(95, true); - while (!BUTTON_PRESS() && !usb_poll_validate_length()) { + while (!BUTTON_PRESS() && !data_available()) { WDT_HIT(); if (ledcontrol) LED_A_ON(); @@ -1263,7 +1263,7 @@ void CmdIOdemodFSK(int findone, uint32_t *high, uint32_t *low, int ledcontrol) { // Configure to go in 125Khz listen mode LFSetupFPGAForADC(95, true); - while (!BUTTON_PRESS() && !usb_poll_validate_length()) { + while (!BUTTON_PRESS() && !data_available()) { WDT_HIT(); if (ledcontrol) LED_A_ON(); @@ -1603,7 +1603,7 @@ void T55xx_ChkPwds() { uint32_t pwd = 0, curr = 0, prev = 0; for (uint16_t i = 0; i < pwdCount; ++i) { - if (BUTTON_PRESS() && !usb_poll_validate_length()) { + if (BUTTON_PRESS() && !data_available()) { goto OUT; } diff --git a/armsrc/lfsampling.c b/armsrc/lfsampling.c index 53215581e..aca064ecc 100644 --- a/armsrc/lfsampling.c +++ b/armsrc/lfsampling.c @@ -137,7 +137,7 @@ uint32_t DoAcquisition(uint8_t decimation, uint32_t bits_per_sample, bool averag uint32_t sample_total_saved = 0; uint32_t cancel_counter = 0; - while (!BUTTON_PRESS() && !usb_poll_validate_length()) { + while (!BUTTON_PRESS() && !data_available()) { WDT_HIT(); if (AT91C_BASE_SSC->SSC_SR & AT91C_SSC_RXRDY) { @@ -283,7 +283,7 @@ void doT55x7Acquisition(size_t sample_size) { bool highFound = false; bool lowFound = false; - while (!BUTTON_PRESS() && !usb_poll_validate_length() && skipCnt < 1000 && (i < bufsize)) { + while (!BUTTON_PRESS() && !data_available() && skipCnt < 1000 && (i < bufsize)) { WDT_HIT(); @@ -348,7 +348,7 @@ void doCotagAcquisition(size_t sample_size) { uint16_t i = 0; uint16_t noise_counter = 0; - while (!BUTTON_PRESS() && !usb_poll_validate_length() && (i < bufsize) && (noise_counter < (COTAG_T1 << 1))) { + while (!BUTTON_PRESS() && !data_available() && (i < bufsize) && (noise_counter < (COTAG_T1 << 1))) { WDT_HIT(); if (AT91C_BASE_SSC->SSC_SR & AT91C_SSC_RXRDY) { @@ -398,7 +398,7 @@ uint32_t doCotagAcquisitionManchester() { uint8_t curr = 0, prev = 0; uint16_t noise_counter = 0; - while (!BUTTON_PRESS() && !usb_poll_validate_length() && (sample_counter < bufsize) && (noise_counter < (COTAG_T1 << 1))) { + while (!BUTTON_PRESS() && !data_available() && (sample_counter < bufsize) && (noise_counter < (COTAG_T1 << 1))) { WDT_HIT(); if (AT91C_BASE_SSC->SSC_SR & AT91C_SSC_RXRDY) { diff --git a/armsrc/mifarecmd.c b/armsrc/mifarecmd.c index 7b5a6518a..f64723355 100644 --- a/armsrc/mifarecmd.c +++ b/armsrc/mifarecmd.c @@ -1328,7 +1328,7 @@ void MifareChkKeys_fast(uint32_t arg0, uint32_t arg1, uint32_t arg2, uint8_t *da //if ( i % 100 == 0) Dbprintf("ChkKeys_fast: sector %d | checking %d | %d found | s_point %d", s, i, foundkeys, s_point); // Allow button press / usb cmd to interrupt device - if (BUTTON_PRESS() && !usb_poll_validate_length()) { + if (BUTTON_PRESS() && !data_available()) { goto OUT; } @@ -1420,7 +1420,7 @@ void MifareChkKeys_fast(uint32_t arg0, uint32_t arg1, uint32_t arg2, uint8_t *da for (uint16_t i = 0; i < keyCount; i++) { // Allow button press / usb cmd to interrupt device - if (BUTTON_PRESS() && !usb_poll_validate_length()) break; + if (BUTTON_PRESS() && !data_available()) break; // found all keys? if (foundkeys == allkeys) diff --git a/armsrc/mifaresim.c b/armsrc/mifaresim.c index c503c2926..8ad397ac3 100644 --- a/armsrc/mifaresim.c +++ b/armsrc/mifaresim.c @@ -502,7 +502,7 @@ void Mifare1ksim(uint16_t flags, uint8_t exitAfterNReads, uint8_t *datain) { bool finished = false; bool button_pushed = BUTTON_PRESS(); - while (!button_pushed && !finished && !usb_poll_validate_length()) { + while (!button_pushed && !finished && !data_available()) { WDT_HIT(); // find reader field diff --git a/armsrc/util.c b/armsrc/util.c index b50715541..cad2d2ac5 100644 --- a/armsrc/util.c +++ b/armsrc/util.c @@ -300,3 +300,11 @@ void FormatVersionInformation(char *dst, int len, const char *prefix, void *vers strncat(dst, v->buildtime, len - strlen(dst) - 1); strncat(dst, "\n", len - strlen(dst) - 1); } + +bool data_available(void) { +#ifdef WITH_FPC_USART_HOST + return usb_poll_validate_length() || (usart_rxdata_available() > 0); +#else + return usb_poll_validate_length(); +#endif +} diff --git a/armsrc/util.h b/armsrc/util.h index 86e8cfb7f..8649f0431 100644 --- a/armsrc/util.h +++ b/armsrc/util.h @@ -92,5 +92,6 @@ void SpinUp(uint32_t speed); int BUTTON_CLICKED(int ms); int BUTTON_HELD(int ms); void FormatVersionInformation(char *dst, int len, const char *prefix, void *version_information); +bool data_available(void); #endif