From 867e10a5fdf0d26c8ee63735b894e7d3953ebbb2 Mon Sep 17 00:00:00 2001 From: pwpiwi Date: Sat, 11 Jan 2020 22:10:40 +0100 Subject: [PATCH] usb communication (device side) refactoring * merge cmd.c into usb_cdc.c * move back usb_cdc.[ch] to common/ * declare low level functions usb_read() and usb_write() and more functions as static * use cmd_receive() in bootrom.c and appmain.c * remove unused memory wasting csrTab[100] in usb_cdc.c * replace more byte_t by uint8_t * more whitespace fixes --- armsrc/Makefile | 3 +- armsrc/appmain.c | 14 +++--- armsrc/cmd.c | 86 ------------------------------------ armsrc/cmd.h | 44 ------------------ armsrc/epa.c | 2 +- armsrc/hfsnoop.c | 3 +- armsrc/hitag2.c | 2 +- armsrc/hitagS.c | 2 +- armsrc/i2c.c | 2 +- armsrc/iclass.c | 3 +- armsrc/iso14443a.c | 2 +- armsrc/iso14443b.c | 2 +- armsrc/iso15693.c | 2 +- armsrc/legicrf.c | 2 +- armsrc/lfops.c | 3 +- armsrc/mifarecmd.c | 2 +- armsrc/mifaresim.c | 1 - armsrc/mifaresniff.c | 2 +- armsrc/pcf7931.c | 2 +- bootrom/Makefile | 2 +- bootrom/bootrom.c | 31 +++++-------- {armsrc => common}/usb_cdc.c | 73 +++++++++++++++++++++++++----- {armsrc => common}/usb_cdc.h | 8 ++-- 23 files changed, 98 insertions(+), 195 deletions(-) delete mode 100644 armsrc/cmd.c delete mode 100644 armsrc/cmd.h rename {armsrc => common}/usb_cdc.c (89%) rename {armsrc => common}/usb_cdc.h (88%) diff --git a/armsrc/Makefile b/armsrc/Makefile index 3d1ba79e..7b9e1356 100644 --- a/armsrc/Makefile +++ b/armsrc/Makefile @@ -50,8 +50,7 @@ THUMBSRC = start.c \ printf.c \ util.c \ string.c \ - usb_cdc.c \ - cmd.c + usb_cdc.c # Compile these in thumb mode optimized for speed (still smaller than ARM mode) THUMBOPTSRC = $(SRC_ISO15693) diff --git a/armsrc/appmain.c b/armsrc/appmain.c index 70cabd2e..38965c32 100644 --- a/armsrc/appmain.c +++ b/armsrc/appmain.c @@ -13,7 +13,6 @@ #include #include "usb_cdc.h" -#include "cmd.h" #include "proxmark3.h" #include "apps.h" #include "fpga.h" @@ -936,9 +935,7 @@ void ListenReaderField(int limit) { } -void UsbPacketReceived(uint8_t *packet, int len) { - - UsbCommand *c = (UsbCommand *)packet; +void UsbPacketReceived(UsbCommand *c) { // Dbprintf("received %d bytes, with command: 0x%04x and args: %d %d %d",len,c->cmd,c->arg[0],c->arg[1],c->arg[2]); @@ -1479,10 +1476,13 @@ void __attribute__((noreturn)) AppMain(void) { LCDInit(); #endif - uint8_t rx[sizeof(UsbCommand)]; - size_t rx_len; - + UsbCommand rx; + for(;;) { + if (cmd_receive(&rx)) { + UsbPacketReceived(&rx); + } + WDT_HIT(); if (usb_poll() && (rx_len = usb_read(rx, sizeof(rx)))) { UsbPacketReceived(rx, rx_len); diff --git a/armsrc/cmd.c b/armsrc/cmd.c deleted file mode 100644 index 71202522..00000000 --- a/armsrc/cmd.c +++ /dev/null @@ -1,86 +0,0 @@ -/* - * Proxmark send and receive commands - * - * Copyright (c) 2012, Roel Verdult - * All rights reserved. - * - * Redistribution and use in source and binary forms, with or without - * modification, are permitted provided that the following conditions are met: - * 1. Redistributions of source code must retain the above copyright - * notice, this list of conditions and the following disclaimer. - * 2. Redistributions in binary form must reproduce the above copyright - * notice, this list of conditions and the following disclaimer in the - * documentation and/or other materials provided with the distribution. - * 3. Neither the name of the copyright holders nor the - * names of its contributors may be used to endorse or promote products - * derived from this software without specific prior written permission. - * - * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS ''AS IS'' AND ANY - * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED - * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE - * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS BE LIABLE FOR ANY - * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES - * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; - * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND - * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT - * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS - * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. - * - * @file cmd.c - * @brief - */ - -#include "cmd.h" - -#include -#include -#include -#include "common.h" -#include "usb_cmd.h" -#include "usb_cdc.h" - - -bool cmd_receive(UsbCommand* cmd) { - - // Check if there is a usb packet available - if (!usb_poll()) - return false; - - // Try to retrieve the available command frame - size_t rxlen = usb_read((uint8_t*)cmd, sizeof(UsbCommand)); - - // Check if the transfer was complete - if (rxlen != sizeof(UsbCommand)) - return false; - - // Received command successfully - return true; -} - - -bool cmd_send(uint32_t cmd, uint32_t arg0, uint32_t arg1, uint32_t arg2, void* data, size_t len) { - UsbCommand txcmd; - - for (size_t i = 0; i < sizeof(UsbCommand); i++) { - ((uint8_t*)&txcmd)[i] = 0x00; - } - - // Compose the outgoing command frame - txcmd.cmd = cmd; - txcmd.arg[0] = arg0; - txcmd.arg[1] = arg1; - txcmd.arg[2] = arg2; - - // Add the (optional) content to the frame, with a maximum size of USB_CMD_DATA_SIZE - if (data && len) { - len = MIN(len, USB_CMD_DATA_SIZE); - for (size_t i = 0; i < len; i++) { - txcmd.d.asBytes[i] = ((uint8_t*)data)[i]; - } - } - - // Send frame and make sure all bytes are transmitted - if (usb_write((uint8_t*)&txcmd, sizeof(UsbCommand)) != 0) return false; - - return true; -} diff --git a/armsrc/cmd.h b/armsrc/cmd.h deleted file mode 100644 index 98e13748..00000000 --- a/armsrc/cmd.h +++ /dev/null @@ -1,44 +0,0 @@ -/* - * Proxmark send and receive commands - * - * Copyright (c) 2010, Roel Verdult - * All rights reserved. - * - * Redistribution and use in source and binary forms, with or without - * modification, are permitted provided that the following conditions are met: - * 1. Redistributions of source code must retain the above copyright - * notice, this list of conditions and the following disclaimer. - * 2. Redistributions in binary form must reproduce the above copyright - * notice, this list of conditions and the following disclaimer in the - * documentation and/or other materials provided with the distribution. - * 3. Neither the name of the copyright holders nor the - * names of its contributors may be used to endorse or promote products - * derived from this software without specific prior written permission. - * - * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS ''AS IS'' AND ANY - * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED - * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE - * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS BE LIABLE FOR ANY - * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES - * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; - * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND - * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT - * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS - * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. - * - * @file cmd.h - * @brief - */ - -#ifndef PROXMARK_CMD_H__ -#define PROXMARK_CMD_H__ - -#include -#include -#include -#include "usb_cmd.h" - -extern bool cmd_receive(UsbCommand* cmd); -extern bool cmd_send(uint32_t cmd, uint32_t arg0, uint32_t arg1, uint32_t arg2, void* data, size_t len); - -#endif // PROXMARK_CMD_H__ diff --git a/armsrc/epa.c b/armsrc/epa.c index 0e999e1e..ac907f6b 100644 --- a/armsrc/epa.c +++ b/armsrc/epa.c @@ -15,7 +15,7 @@ #include "iso14443a.h" #include "iso14443b.h" #include "epa.h" -#include "cmd.h" +#include "usb_cdc.h" #include "fpgaloader.h" #include "string.h" #include "util.h" diff --git a/armsrc/hfsnoop.c b/armsrc/hfsnoop.c index 2bb7fbce..3a0ac65f 100644 --- a/armsrc/hfsnoop.c +++ b/armsrc/hfsnoop.c @@ -14,8 +14,7 @@ #include "BigBuf.h" #include "util.h" #include "apps.h" -#include "cmd.h" -#include "usb_cdc.h" // for usb_poll_validate_length +#include "usb_cdc.h" #include "fpga.h" #include "fpgaloader.h" diff --git a/armsrc/hitag2.c b/armsrc/hitag2.c index 7cdabefa..688805be 100644 --- a/armsrc/hitag2.c +++ b/armsrc/hitag2.c @@ -19,7 +19,7 @@ #include "hitag2.h" #include "proxmark3.h" -#include "cmd.h" +#include "usb_cdc.h" #include "apps.h" #include "util.h" #include "hitag.h" diff --git a/armsrc/hitagS.c b/armsrc/hitagS.c index e7533da7..8f40146a 100644 --- a/armsrc/hitagS.c +++ b/armsrc/hitagS.c @@ -17,7 +17,7 @@ #include #include "proxmark3.h" #include "apps.h" -#include "cmd.h" +#include "usb_cdc.h" #include "util.h" #include "hitag.h" #include "string.h" diff --git a/armsrc/i2c.c b/armsrc/i2c.c index 30cbddd0..3d16b42c 100644 --- a/armsrc/i2c.c +++ b/armsrc/i2c.c @@ -18,7 +18,7 @@ #include "mifareutil.h" // for MF_DBGLEVEL #include "BigBuf.h" #include "apps.h" -#include "cmd.h" +#include "usb_cdc.h" #ifdef WITH_SMARTCARD #include "smartcard.h" diff --git a/armsrc/iclass.c b/armsrc/iclass.c index 28cfcaa6..1f814609 100644 --- a/armsrc/iclass.c +++ b/armsrc/iclass.c @@ -24,7 +24,7 @@ #include "string.h" #include "printf.h" #include "common.h" -#include "cmd.h" +#include "usb_cdc.h" #include "iso14443a.h" #include "iso15693.h" // Needed for CRC in emulation mode; @@ -34,7 +34,6 @@ #include "iso15693tools.h" #include "protocols.h" #include "optimized_cipher.h" -#include "usb_cdc.h" // for usb_poll_validate_length #include "fpgaloader.h" // iCLASS has a slightly different timing compared to ISO15693. According to the picopass data sheet the tag response is expected 330us after diff --git a/armsrc/iso14443a.c b/armsrc/iso14443a.c index 2d6c3e3b..a24e7a47 100644 --- a/armsrc/iso14443a.c +++ b/armsrc/iso14443a.c @@ -17,7 +17,7 @@ #include "proxmark3.h" #include "apps.h" #include "util.h" -#include "cmd.h" +#include "usb_cdc.h" #include "iso14443crc.h" #include "crapto1/crapto1.h" #include "mifareutil.h" diff --git a/armsrc/iso14443b.c b/armsrc/iso14443b.c index 5a335e25..0f36c7c7 100644 --- a/armsrc/iso14443b.c +++ b/armsrc/iso14443b.c @@ -14,7 +14,7 @@ #include "proxmark3.h" #include "apps.h" -#include "cmd.h" +#include "usb_cdc.h" #include "util.h" #include "string.h" #include "iso14443crc.h" diff --git a/armsrc/iso15693.c b/armsrc/iso15693.c index 351b0184..9f6516aa 100644 --- a/armsrc/iso15693.c +++ b/armsrc/iso15693.c @@ -58,7 +58,7 @@ #include "string.h" #include "iso15693tools.h" #include "protocols.h" -#include "cmd.h" +#include "usb_cdc.h" #include "BigBuf.h" #include "fpgaloader.h" diff --git a/armsrc/legicrf.c b/armsrc/legicrf.c index 3bc09732..71ff0321 100644 --- a/armsrc/legicrf.c +++ b/armsrc/legicrf.c @@ -14,7 +14,7 @@ #include "proxmark3.h" #include "apps.h" -#include "cmd.h" +#include "usb_cdc.h" #include "util.h" #include "string.h" #include "legic_prng.h" diff --git a/armsrc/lfops.c b/armsrc/lfops.c index a1a31f1a..995a8810 100644 --- a/armsrc/lfops.c +++ b/armsrc/lfops.c @@ -17,8 +17,7 @@ #include "lfdemod.h" #include "lfsampling.h" #include "protocols.h" -#include "cmd.h" -#include "usb_cdc.h" // for usb_poll_validate_length +#include "usb_cdc.h" #include "fpgaloader.h" /** diff --git a/armsrc/mifarecmd.c b/armsrc/mifarecmd.c index b9032c5f..e30a5639 100644 --- a/armsrc/mifarecmd.c +++ b/armsrc/mifarecmd.c @@ -18,7 +18,7 @@ #include #include "proxmark3.h" -#include "cmd.h" +#include "usb_cdc.h" #include "crapto1/crapto1.h" #include "iso14443a.h" #include "BigBuf.h" diff --git a/armsrc/mifaresim.c b/armsrc/mifaresim.c index 891e0daf..b63160c9 100644 --- a/armsrc/mifaresim.c +++ b/armsrc/mifaresim.c @@ -20,7 +20,6 @@ #include "fpgaloader.h" #include "proxmark3.h" #include "usb_cdc.h" -#include "cmd.h" #include "protocols.h" #include "apps.h" diff --git a/armsrc/mifaresniff.c b/armsrc/mifaresniff.c index 4dbcd904..b867c582 100644 --- a/armsrc/mifaresniff.c +++ b/armsrc/mifaresniff.c @@ -18,7 +18,7 @@ #include "crapto1/crapto1.h" #include "mifareutil.h" #include "common.h" -#include "cmd.h" +#include "usb_cdc.h" #include "BigBuf.h" #include "fpgaloader.h" diff --git a/armsrc/pcf7931.c b/armsrc/pcf7931.c index b0f8e4a3..2fae74cc 100644 --- a/armsrc/pcf7931.c +++ b/armsrc/pcf7931.c @@ -1,6 +1,6 @@ #include "proxmark3.h" #include "apps.h" -#include "cmd.h" +#include "usb_cdc.h" #include "lfsampling.h" #include "pcf7931.h" #include "util.h" diff --git a/bootrom/Makefile b/bootrom/Makefile index dd1e7e08..b1e2e6cd 100644 --- a/bootrom/Makefile +++ b/bootrom/Makefile @@ -8,7 +8,7 @@ # DO NOT use thumb mode in the phase 1 bootloader since that generates a section with glue code ARMSRC = -THUMBSRC = cmd.c usb_cdc.c bootrom.c +THUMBSRC = usb_cdc.c bootrom.c ASMSRC = ram-reset.s flash-reset.s VERSIONSRC = version.c diff --git a/bootrom/bootrom.c b/bootrom/bootrom.c index bbea07b5..b2500821 100644 --- a/bootrom/bootrom.c +++ b/bootrom/bootrom.c @@ -6,17 +6,15 @@ // Main code for the bootloader //----------------------------------------------------------------------------- -#include +#include "proxmark3.h" #include "usb_cdc.h" -#include "cmd.h" -//#include "usb_hid.h" void DbpString(char *str) { - byte_t len = 0; + uint8_t len = 0; while (str[len] != 0x00) { len++; } - cmd_send(CMD_DEBUG_PRINT_STRING,len,0,0,(byte_t*)str,len); + cmd_send(CMD_DEBUG_PRINT_STRING,len,0,0,(uint8_t*)str,len); } struct common_area common_area __attribute__((section(".commonarea"))); @@ -89,15 +87,10 @@ static void Fatal(void) for(;;); } -void UsbPacketReceived(uint8_t *packet, int len) { +void UsbPacketReceived(UsbCommand *c) { int i, dont_ack=0; - UsbCommand* c = (UsbCommand *)packet; volatile uint32_t *p; - if(len != sizeof(UsbCommand)) { - Fatal(); - } - uint32_t arg0 = (uint32_t)c->arg[0]; switch(c->cmd) { @@ -199,21 +192,17 @@ static void flash_mode(int externally_entered) start_addr = 0; end_addr = 0; bootrom_unlocked = 0; - byte_t rx[sizeof(UsbCommand)]; - size_t rx_len; + UsbCommand rx; - usb_enable(); - for (volatile size_t i=0; i<0x100000; i++) {}; + usb_enable(); + for (volatile size_t i=0; i<0x100000; i++) {}; for(;;) { WDT_HIT(); - if (usb_poll()) { - rx_len = usb_read(rx,sizeof(UsbCommand)); - if (rx_len) { - UsbPacketReceived(rx,rx_len); - } - } + if (cmd_receive(&rx)) { + UsbPacketReceived(&rx); + } if(!externally_entered && !BUTTON_PRESS()) { /* Perform a reset to leave flash mode */ diff --git a/armsrc/usb_cdc.c b/common/usb_cdc.c similarity index 89% rename from armsrc/usb_cdc.c rename to common/usb_cdc.c index 269d71d7..66b02e78 100644 --- a/armsrc/usb_cdc.c +++ b/common/usb_cdc.c @@ -207,7 +207,7 @@ static const char StrDescProduct[] = { }; -const char* getStringDescriptor(uint8_t idx) { +static const char* getStringDescriptor(uint8_t idx) { switch (idx) { case STR_LANGUAGE_CODES: return StrDescLanguageCodes; @@ -290,7 +290,7 @@ AT91S_CDC_LINE_CODING line = { 8}; // 8 Data bits -void AT91F_CDC_Enumerate(); +static void AT91F_CDC_Enumerate(); AT91PS_UDP pUdp = AT91C_BASE_UDP; uint8_t btConfiguration = 0; @@ -350,7 +350,7 @@ void usb_enable() { //* \fn usb_check //* \brief Test if the device is configured and handle enumeration //*---------------------------------------------------------------------------- -bool usb_check() { +static bool usb_check() { AT91_REG isr = pUdp->UDP_ISR; if (isr & AT91C_UDP_ENDBUSRES) { @@ -395,7 +395,7 @@ bool usb_poll_validate_length() { //* \fn usb_read //* \brief Read available data from Endpoint OUT //*---------------------------------------------------------------------------- -uint32_t usb_read(uint8_t* data, size_t len) { +static uint32_t usb_read(uint8_t* data, size_t len) { uint8_t bank = btReceiveBank; uint32_t packetSize, nbBytesRcv = 0; uint32_t time_out = 0; @@ -427,7 +427,7 @@ uint32_t usb_read(uint8_t* data, size_t len) { //* \fn usb_write //* \brief Send through endpoint 2 //*---------------------------------------------------------------------------- -uint32_t usb_write(const uint8_t* data, const size_t len) { +static uint32_t usb_write(const uint8_t* data, const size_t len) { size_t length = len; uint32_t cpt = 0; @@ -476,9 +476,6 @@ uint32_t usb_write(const uint8_t* data, const size_t len) { //* \fn AT91F_USB_SendData //* \brief Send Data through the control endpoint //*---------------------------------------------------------------------------- -unsigned int csrTab[100] = {0x00}; -unsigned char csrIdx = 0; - static void AT91F_USB_SendData(AT91PS_UDP pUdp, const char *pData, uint32_t length) { uint32_t cpt = 0; AT91_REG csr; @@ -521,7 +518,7 @@ static void AT91F_USB_SendData(AT91PS_UDP pUdp, const char *pData, uint32_t leng //* \fn AT91F_USB_SendZlp //* \brief Send zero length packet through the control endpoint //*---------------------------------------------------------------------------- -void AT91F_USB_SendZlp(AT91PS_UDP pUdp) { +static void AT91F_USB_SendZlp(AT91PS_UDP pUdp) { UDP_SET_EP_FLAGS(AT91C_EP_CONTROL, AT91C_UDP_TXPKTRDY); while (!(pUdp->UDP_CSR[AT91C_EP_CONTROL] & AT91C_UDP_TXCOMP)) /* wait */; @@ -535,7 +532,7 @@ void AT91F_USB_SendZlp(AT91PS_UDP pUdp) { //* \fn AT91F_USB_SendStall //* \brief Stall the control endpoint //*---------------------------------------------------------------------------- -void AT91F_USB_SendStall(AT91PS_UDP pUdp) { +static void AT91F_USB_SendStall(AT91PS_UDP pUdp) { UDP_SET_EP_FLAGS(AT91C_EP_CONTROL, AT91C_UDP_FORCESTALL); while (!(pUdp->UDP_CSR[AT91C_EP_CONTROL] & AT91C_UDP_ISOERROR)) /* wait */; @@ -549,7 +546,7 @@ void AT91F_USB_SendStall(AT91PS_UDP pUdp) { //* \fn AT91F_CDC_Enumerate //* \brief This function is a callback invoked when a SETUP packet is received //*---------------------------------------------------------------------------- -void AT91F_CDC_Enumerate() { +static void AT91F_CDC_Enumerate() { uint8_t bmRequestType, bRequest; uint16_t wValue, wIndex, wLength, wStatus; @@ -665,7 +662,7 @@ void AT91F_CDC_Enumerate() { // handle CDC class requests case SET_LINE_CODING: - while ( (pUdp->UDP_CSR[AT91C_EP_CONTROL] & AT91C_UDP_RX_DATA_BK0)) + while (!(pUdp->UDP_CSR[AT91C_EP_CONTROL] & AT91C_UDP_RX_DATA_BK0)) /* wait */; UDP_CLEAR_EP_FLAGS(AT91C_EP_CONTROL, AT91C_UDP_RX_DATA_BK0); AT91F_USB_SendZlp(pUdp); @@ -682,3 +679,55 @@ void AT91F_CDC_Enumerate() { break; } } + + +//*************************************************************************** +// Interface to the main program +//*************************************************************************** + +// The function to receive a command from the client via USB +bool cmd_receive(UsbCommand* cmd) { + + // Check if there is a usb packet available + if (!usb_poll()) + return false; + + // Try to retrieve the available command frame + size_t rxlen = usb_read((uint8_t*)cmd, sizeof(UsbCommand)); + + // Check if the transfer was complete + if (rxlen != sizeof(UsbCommand)) + return false; + + // Received command successfully + return true; +} + + +// The function to send a response to the client via USB +bool cmd_send(uint32_t cmd, uint32_t arg0, uint32_t arg1, uint32_t arg2, void* data, size_t len) { + UsbCommand txcmd; + + for (size_t i = 0; i < sizeof(UsbCommand); i++) { + ((uint8_t*)&txcmd)[i] = 0x00; + } + + // Compose the outgoing command frame + txcmd.cmd = cmd; + txcmd.arg[0] = arg0; + txcmd.arg[1] = arg1; + txcmd.arg[2] = arg2; + + // Add the (optional) content to the frame, with a maximum size of USB_CMD_DATA_SIZE + if (data && len) { + len = MIN(len, USB_CMD_DATA_SIZE); + for (size_t i = 0; i < len; i++) { + txcmd.d.asBytes[i] = ((uint8_t*)data)[i]; + } + } + + // Send frame and make sure all bytes are transmitted + if (usb_write((uint8_t*)&txcmd, sizeof(UsbCommand)) != 0) return false; + + return true; +} diff --git a/armsrc/usb_cdc.h b/common/usb_cdc.h similarity index 88% rename from armsrc/usb_cdc.h rename to common/usb_cdc.h index 085b9844..31399222 100644 --- a/armsrc/usb_cdc.h +++ b/common/usb_cdc.h @@ -38,13 +38,13 @@ #include #include #include +#include "usb_cmd.h" extern void usb_disable(); extern void usb_enable(); -extern bool usb_check(); extern bool usb_poll(); extern bool usb_poll_validate_length(); -extern uint32_t usb_read(uint8_t* data, size_t len); -extern uint32_t usb_write(const uint8_t* data, const size_t len); +extern bool cmd_receive(UsbCommand* cmd); +extern bool cmd_send(uint32_t cmd, uint32_t arg0, uint32_t arg1, uint32_t arg2, void* data, size_t len); -#endif // _USB_CDC_H__ +#endif // USB_CDC_H__ -- 2.39.5