From cdd4177968c0539e88f3acce7a901a1b3a8a298b Mon Sep 17 00:00:00 2001 From: Philippe Teuwen Date: Mon, 22 Apr 2019 00:30:01 +0200 Subject: [PATCH] usart_writebuffer_sync simplified as we're always sending in blocking mode anyway --- armsrc/appmain.c | 6 +++--- common/cmd.c | 4 ++-- common/usart.c | 26 ++++++++------------------ 3 files changed, 13 insertions(+), 23 deletions(-) diff --git a/armsrc/appmain.c b/armsrc/appmain.c index aa85df22d..298f4df7f 100644 --- a/armsrc/appmain.c +++ b/armsrc/appmain.c @@ -1100,7 +1100,7 @@ static void PacketReceived(PacketCommandNG *packet) { strncat(at, s_at, sizeof(at) - strlen(at) - 1); DbpString("Try AT baud rate setting"); usart_init(); - int16_t res = usart_writebuffer((uint8_t*)&at, sizeof(at)); + int16_t res = usart_writebuffer_sync((uint8_t*)&at, sizeof(at)); WaitMS(1); Dbprintf("SEND %d | %c%c%c%c%c%c%c%c%c%c%c", res, at[0], at[1], at[2], at[3], at[4], at[5], at[6], at[7], at[8], at[9], at[10]); @@ -1122,7 +1122,7 @@ static void PacketReceived(PacketCommandNG *packet) { } static const char *welcome = "Proxmark3 Serial interface via FPC ready\r\n"; - usart_writebuffer((uint8_t *)welcome, strlen(welcome)); + usart_writebuffer_sync((uint8_t *)welcome, strlen(welcome)); sprintf(dest, "| bytes 0x%02x 0x%02x 0x%02x 0x%02x\r\n" , packet->data.asBytes[0] @@ -1130,7 +1130,7 @@ static void PacketReceived(PacketCommandNG *packet) { , packet->data.asBytes[2] , packet->data.asBytes[3] ); - usart_writebuffer((uint8_t *)dest, strlen(dest)); + usart_writebuffer_sync((uint8_t *)dest, strlen(dest)); LED_A_ON(); diff --git a/common/cmd.c b/common/cmd.c index fb8396051..13ca856d8 100644 --- a/common/cmd.c +++ b/common/cmd.c @@ -72,7 +72,7 @@ int16_t reply_old(uint64_t cmd, uint64_t arg0, uint64_t arg1, uint64_t arg2, voi if (reply_via_fpc) { #ifdef WITH_FPC_HOST - sendlen = usart_writebuffer((uint8_t *)&txcmd, sizeof(PacketResponseOLD)); + sendlen = usart_writebuffer_sync((uint8_t *)&txcmd, sizeof(PacketResponseOLD)); // Dbprintf_usb("Sent %i bytes over usart", len); #else return PM3_EDEVNOTSUPP; @@ -124,7 +124,7 @@ static int16_t reply_ng_internal(uint16_t cmd, int16_t status, uint8_t *data, si if (reply_via_fpc) { #ifdef WITH_FPC_HOST - sendlen = usart_writebuffer((uint8_t *)&txBufferNG, txBufferNGLen); + sendlen = usart_writebuffer_sync((uint8_t *)&txBufferNG, txBufferNGLen); // Dbprintf_usb("Sent %i bytes over usart", len); #else return PM3_EDEVNOTSUPP; diff --git a/common/usart.c b/common/usart.c index 8d9b74257..cb4153062 100644 --- a/common/usart.c +++ b/common/usart.c @@ -129,25 +129,15 @@ uint32_t usart_read_ng(uint8_t *data, size_t len) { } // transfer from device to client -inline int16_t usart_writebuffer(uint8_t *data, size_t len) { +inline int16_t usart_writebuffer_sync(uint8_t *data, size_t len) { - // Wait for one free PDC bank - while (pUS1->US_TCR && pUS1->US_TNCR) {}; - - // Check if the current PDC bank is free - if (pUS1->US_TCR == 0) { - pUS1->US_TPR = (uint32_t)data; - pUS1->US_TCR = len; - } - // Check if the backup PDC bank is free - else if (pUS1->US_TNCR == 0) { - pUS1->US_TNPR = (uint32_t)data; - pUS1->US_TNCR = len; - } else { - // we shouldn't be here - return 0; - } - //wait until finishing all transfers + // Wait for current PDC bank to be free + // (and check next bank too, in case there will be a usart_writebuffer_async) + while (pUS1->US_TNCR || pUS1->US_TCR) {}; + pUS1->US_TPR = (uint32_t)data; + pUS1->US_TCR = len; + // Wait until finishing all transfers to make sure "data" buffer can be discarded + // (if we don't wait here, bulk send as e.g. "hw status" will fail) while (pUS1->US_TNCR || pUS1->US_TCR) {}; return len; }