]> git.zerfleddert.de Git - proxmark3-svn/blobdiff - common/usb_cdc.c
gcc10 compiler fixes:
[proxmark3-svn] / common / usb_cdc.c
index 348b97e924145278b2af9a1fc33211dd05497778..acd16b4e704b689b8d7156d194144c6580b810ee 100644 (file)
  */\r
 \r
 #include "usb_cdc.h"\r
  */\r
 \r
 #include "usb_cdc.h"\r
+\r
+#include <stddef.h>\r
+#include <stdint.h>\r
+#include <stdbool.h>\r
+\r
+#include "common.h"\r
 #include "at91sam7s512.h"\r
 #include "config_gpio.h"\r
 \r
 #include "at91sam7s512.h"\r
 #include "config_gpio.h"\r
 \r
-\r
 #define AT91C_EP_CONTROL     0\r
 #define AT91C_EP_OUT         1\r
 #define AT91C_EP_IN          2\r
 #define AT91C_EP_CONTROL     0\r
 #define AT91C_EP_OUT         1\r
 #define AT91C_EP_IN          2\r
@@ -49,6 +54,7 @@
 #define STR_MANUFACTURER   0x01\r
 #define STR_PRODUCT        0x02\r
 \r
 #define STR_MANUFACTURER   0x01\r
 #define STR_PRODUCT        0x02\r
 \r
+\r
 static const char devDescriptor[] = {\r
        /* Device descriptor */\r
        0x12,      // bLength\r
 static const char devDescriptor[] = {\r
        /* Device descriptor */\r
        0x12,      // bLength\r
@@ -67,6 +73,7 @@ static const char devDescriptor[] = {
        0x01       // bNumConfigs\r
 };\r
 \r
        0x01       // bNumConfigs\r
 };\r
 \r
+\r
 static const char cfgDescriptor[] = {\r
        /* ============== CONFIGURATION 1 =========== */\r
        /* Configuration 1 descriptor */\r
 static const char cfgDescriptor[] = {\r
        /* ============== CONFIGURATION 1 =========== */\r
        /* Configuration 1 descriptor */\r
@@ -157,41 +164,50 @@ static const char cfgDescriptor[] = {
        0x00    // bInterval\r
 };\r
 \r
        0x00    // bInterval\r
 };\r
 \r
+\r
 static const char StrDescLanguageCodes[] = {\r
 static const char StrDescLanguageCodes[] = {\r
-  4,                   // Length\r
-  0x03,                        // Type is string\r
-  0x09, 0x04   // supported language Code 0 = 0x0409 (English)\r
+       4,          // Length\r
+       0x03,       // Type is string\r
+       0x09, 0x04  // supported language Code 0 = 0x0409 (English)\r
 };\r
 \r
 };\r
 \r
+\r
 // Note: ModemManager (Linux) ignores Proxmark3 devices by matching the\r
 // manufacturer string "proxmark.org". Don't change this.\r
 static const char StrDescManufacturer[] = {\r
 // Note: ModemManager (Linux) ignores Proxmark3 devices by matching the\r
 // manufacturer string "proxmark.org". Don't change this.\r
 static const char StrDescManufacturer[] = {\r
-  26,                  // Length\r
-  0x03,                        // Type is string\r
-  'p', 0x00,\r
-  'r', 0x00,\r
-  'o', 0x00,\r
-  'x', 0x00,\r
-  'm', 0x00,\r
-  'a', 0x00,\r
-  'r', 0x00,\r
-  'k', 0x00,\r
-  '.', 0x00,\r
-  'o', 0x00,\r
-  'r', 0x00,\r
-  'g', 0x00\r
+       26,         // Length\r
+       0x03,       // Type is string\r
+       'p', 0x00,\r
+       'r', 0x00,\r
+       'o', 0x00,\r
+       'x', 0x00,\r
+       'm', 0x00,\r
+       'a', 0x00,\r
+       'r', 0x00,\r
+       'k', 0x00,\r
+       '.', 0x00,\r
+       'o', 0x00,\r
+       'r', 0x00,\r
+       'g', 0x00\r
 };\r
 \r
 };\r
 \r
+\r
 static const char StrDescProduct[] = {\r
 static const char StrDescProduct[] = {\r
-  8,                   // Length\r
-  0x03,                        // Type is string\r
-  'P', 0x00,\r
-  'M', 0x00,\r
-  '3', 0x00\r
+       20,         // Length\r
+       0x03,       // Type is string\r
+       'p', 0x00,\r
+       'r', 0x00,\r
+       'o', 0x00,\r
+       'x', 0x00,\r
+       'm', 0x00,\r
+       'a', 0x00,\r
+       'r', 0x00,\r
+       'k', 0x00,\r
+       '3', 0x00\r
 };\r
 \r
 };\r
 \r
-const char* getStringDescriptor(uint8_t idx)\r
-{\r
+\r
+static const char* getStringDescriptor(uint8_t idx) {\r
        switch (idx) {\r
                case STR_LANGUAGE_CODES:\r
                        return StrDescLanguageCodes;\r
        switch (idx) {\r
                case STR_LANGUAGE_CODES:\r
                        return StrDescLanguageCodes;\r
@@ -204,29 +220,31 @@ const char* getStringDescriptor(uint8_t idx)
        }\r
 }\r
 \r
        }\r
 }\r
 \r
+\r
 // Bitmap for all status bits in CSR which must be written as 1 to cause no effect\r
 // Bitmap for all status bits in CSR which must be written as 1 to cause no effect\r
-#define REG_NO_EFFECT_1_ALL      AT91C_UDP_RX_DATA_BK0 | AT91C_UDP_RX_DATA_BK1 \\r
-                                |AT91C_UDP_STALLSENT   | AT91C_UDP_RXSETUP \\r
-                                |AT91C_UDP_TXCOMP\r
+#define REG_NO_EFFECT_1_ALL     (AT91C_UDP_RX_DATA_BK0 | AT91C_UDP_RX_DATA_BK1 | AT91C_UDP_STALLSENT | AT91C_UDP_RXSETUP | AT91C_UDP_TXCOMP)\r
+\r
 \r
 // Clear flags in the UDP_CSR register\r
 #define UDP_CLEAR_EP_FLAGS(endpoint, flags) { \\r
        volatile unsigned int reg; \\r
 \r
 // Clear flags in the UDP_CSR register\r
 #define UDP_CLEAR_EP_FLAGS(endpoint, flags) { \\r
        volatile unsigned int reg; \\r
-       reg = pUdp->UDP_CSR[(endpoint)]; \\r
+       reg = AT91C_BASE_UDP->UDP_CSR[(endpoint)]; \\r
        reg |= REG_NO_EFFECT_1_ALL; \\r
        reg &= ~(flags); \\r
        reg |= REG_NO_EFFECT_1_ALL; \\r
        reg &= ~(flags); \\r
-       pUdp->UDP_CSR[(endpoint)] = reg; \\r
-} \r
+       AT91C_BASE_UDP->UDP_CSR[(endpoint)] = reg; \\r
+}\r
+\r
 \r
 // Set flags in the UDP_CSR register\r
 #define UDP_SET_EP_FLAGS(endpoint, flags) { \\r
        volatile unsigned int reg; \\r
 \r
 // Set flags in the UDP_CSR register\r
 #define UDP_SET_EP_FLAGS(endpoint, flags) { \\r
        volatile unsigned int reg; \\r
-       reg = pUdp->UDP_CSR[(endpoint)]; \\r
+       reg = AT91C_BASE_UDP->UDP_CSR[(endpoint)]; \\r
        reg |= REG_NO_EFFECT_1_ALL; \\r
        reg |= (flags); \\r
        reg |= REG_NO_EFFECT_1_ALL; \\r
        reg |= (flags); \\r
-       pUdp->UDP_CSR[(endpoint)] = reg; \\r
+       AT91C_BASE_UDP->UDP_CSR[(endpoint)] = reg; \\r
 }\r
 \r
 }\r
 \r
+\r
 /* USB standard request codes */\r
 #define STD_GET_STATUS_ZERO           0x0080\r
 #define STD_GET_STATUS_INTERFACE      0x0081\r
 /* USB standard request codes */\r
 #define STD_GET_STATUS_ZERO           0x0080\r
 #define STD_GET_STATUS_INTERFACE      0x0081\r
@@ -254,6 +272,7 @@ const char* getStringDescriptor(uint8_t idx)
 #define SET_LINE_CODING               0x2021\r
 #define SET_CONTROL_LINE_STATE        0x2221\r
 \r
 #define SET_LINE_CODING               0x2021\r
 #define SET_CONTROL_LINE_STATE        0x2221\r
 \r
+\r
 typedef struct {\r
        unsigned int dwDTERRate;\r
        char bCharFormat;\r
 typedef struct {\r
        unsigned int dwDTERRate;\r
        char bCharFormat;\r
@@ -261,19 +280,17 @@ typedef struct {
        char bDataBits;\r
 } AT91S_CDC_LINE_CODING, *AT91PS_CDC_LINE_CODING;\r
 \r
        char bDataBits;\r
 } AT91S_CDC_LINE_CODING, *AT91PS_CDC_LINE_CODING;\r
 \r
-AT91S_CDC_LINE_CODING line = {\r
+\r
+static AT91S_CDC_LINE_CODING line = {\r
        115200, // baudrate\r
        0,      // 1 Stop Bit\r
        0,      // None Parity\r
        8};     // 8 Data bits\r
 \r
 \r
        115200, // baudrate\r
        0,      // 1 Stop Bit\r
        0,      // None Parity\r
        8};     // 8 Data bits\r
 \r
 \r
-void AT91F_CDC_Enumerate();\r
-\r
-AT91PS_UDP pUdp = AT91C_BASE_UDP;\r
-byte_t btConfiguration = 0;\r
-byte_t btConnection    = 0;\r
-byte_t btReceiveBank   = AT91C_UDP_RX_DATA_BK0;\r
+static uint8_t btConfiguration = 0;\r
+static uint8_t btConnection    = 0;\r
+static uint8_t btReceiveBank   = AT91C_UDP_RX_DATA_BK0;\r
 \r
 \r
 //*----------------------------------------------------------------------------\r
 \r
 \r
 //*----------------------------------------------------------------------------\r
@@ -285,8 +302,8 @@ void usb_disable() {
        AT91C_BASE_PIOA->PIO_ODR = GPIO_USB_PU;\r
 \r
        // Clear all lingering interrupts\r
        AT91C_BASE_PIOA->PIO_ODR = GPIO_USB_PU;\r
 \r
        // Clear all lingering interrupts\r
-       if(pUdp->UDP_ISR & AT91C_UDP_ENDBUSRES) {\r
-               pUdp->UDP_ICR = AT91C_UDP_ENDBUSRES;\r
+       if (AT91C_BASE_UDP->UDP_ISR & AT91C_UDP_ENDBUSRES) {\r
+               AT91C_BASE_UDP->UDP_ICR = AT91C_UDP_ENDBUSRES;\r
        }\r
 }\r
 \r
        }\r
 }\r
 \r
@@ -316,7 +333,7 @@ void usb_enable() {
        usb_disable();\r
 \r
        // Wait for a short while\r
        usb_disable();\r
 \r
        // Wait for a short while\r
-       for (volatile size_t i=0; i<0x100000; i++);\r
+       for (volatile size_t i = 0; i < 0x100000; i++);\r
 \r
        // Reconnect USB reconnect\r
        AT91C_BASE_PIOA->PIO_SODR = GPIO_USB_PU;\r
 \r
        // Reconnect USB reconnect\r
        AT91C_BASE_PIOA->PIO_SODR = GPIO_USB_PU;\r
@@ -325,127 +342,16 @@ void usb_enable() {
 \r
 \r
 //*----------------------------------------------------------------------------\r
 \r
 \r
 //*----------------------------------------------------------------------------\r
-//* \fn    usb_check\r
-//* \brief Test if the device is configured and handle enumeration\r
-//*----------------------------------------------------------------------------\r
-bool usb_check() {\r
-       AT91_REG isr = pUdp->UDP_ISR;\r
-\r
-       if (isr & AT91C_UDP_ENDBUSRES) {\r
-               pUdp->UDP_ICR = AT91C_UDP_ENDBUSRES;\r
-               // reset all endpoints\r
-               pUdp->UDP_RSTEP  = (unsigned int)-1;\r
-               pUdp->UDP_RSTEP  = 0;\r
-               // Enable the function\r
-               pUdp->UDP_FADDR = AT91C_UDP_FEN;\r
-               // Configure endpoint 0\r
-               pUdp->UDP_CSR[AT91C_EP_CONTROL] = (AT91C_UDP_EPEDS | AT91C_UDP_EPTYPE_CTRL);\r
-       } else if (isr & AT91C_UDP_EPINT0) {\r
-               pUdp->UDP_ICR = AT91C_UDP_EPINT0;\r
-               AT91F_CDC_Enumerate();\r
-       }\r
-       return (btConfiguration) ? true : false;\r
-}\r
-\r
-\r
-bool usb_poll()\r
-{\r
-       if (!usb_check()) return false;\r
-       return (pUdp->UDP_CSR[AT91C_EP_OUT] & btReceiveBank);\r
-}\r
-\r
-\r
-/**\r
-       In github PR #129, some users appears to get a false positive from\r
-       usb_poll, which returns true, but the usb_read operation\r
-       still returns 0.\r
-       This check is basically the same as above, but also checks\r
-       that the length available to read is non-zero, thus hopefully fixes the\r
-       bug.\r
-**/\r
-bool usb_poll_validate_length()\r
-{\r
-       if (!usb_check()) return false;\r
-       if (!(pUdp->UDP_CSR[AT91C_EP_OUT] & btReceiveBank)) return false;\r
-       return (pUdp->UDP_CSR[AT91C_EP_OUT] >> 16) >  0;\r
-}\r
-\r
-//*----------------------------------------------------------------------------\r
-//* \fn    usb_read\r
-//* \brief Read available data from Endpoint OUT\r
-//*----------------------------------------------------------------------------\r
-uint32_t usb_read(byte_t* data, size_t len) {\r
-       byte_t bank = btReceiveBank;\r
-       uint32_t packetSize, nbBytesRcv = 0;\r
-       uint32_t time_out = 0;\r
-  \r
-       while (len)  {\r
-               if (!usb_check()) break;\r
-\r
-               if ( pUdp->UDP_CSR[AT91C_EP_OUT] & bank ) {\r
-                       packetSize = MIN(pUdp->UDP_CSR[AT91C_EP_OUT] >> 16, len);\r
-                       len -= packetSize;\r
-                       while(packetSize--)\r
-                               data[nbBytesRcv++] = pUdp->UDP_FDR[AT91C_EP_OUT];\r
-                       UDP_CLEAR_EP_FLAGS(AT91C_EP_OUT, bank);\r
-                       if (bank == AT91C_UDP_RX_DATA_BK0) {\r
-                               bank = AT91C_UDP_RX_DATA_BK1;\r
-                       } else {\r
-                               bank = AT91C_UDP_RX_DATA_BK0;\r
-                       }\r
-               }\r
-               if (time_out++ == 0x1fff) break;\r
-       }\r
-\r
-       btReceiveBank = bank;\r
-       return nbBytesRcv;\r
-}\r
-\r
-\r
-//*----------------------------------------------------------------------------\r
-//* \fn    usb_write\r
-//* \brief Send through endpoint 2\r
+//* \fn    AT91F_USB_SendZlp\r
+//* \brief Send zero length packet through an endpoint\r
 //*----------------------------------------------------------------------------\r
 //*----------------------------------------------------------------------------\r
-uint32_t usb_write(const byte_t* data, const size_t len) {\r
-       size_t length = len;\r
-       uint32_t cpt = 0;\r
-\r
-       if (!length) return 0;\r
-       if (!usb_check()) return 0;\r
-\r
-       // Send the first packet\r
-       cpt = MIN(length, AT91C_EP_IN_SIZE);\r
-       length -= cpt;\r
-       while (cpt--) {\r
-               pUdp->UDP_FDR[AT91C_EP_IN] = *data++;\r
-       }\r
-       UDP_SET_EP_FLAGS(AT91C_EP_IN, AT91C_UDP_TXPKTRDY);\r
-\r
-       while (length) {\r
-               // Fill the next bank\r
-               cpt = MIN(length, AT91C_EP_IN_SIZE);\r
-               length -= cpt;\r
-               while (cpt--) {\r
-                       pUdp->UDP_FDR[AT91C_EP_IN] = *data++;\r
-               }\r
-               // Wait for the previous bank to be sent\r
-               while (!(pUdp->UDP_CSR[AT91C_EP_IN] & AT91C_UDP_TXCOMP)) {\r
-                       if (!usb_check()) return length;\r
-               }\r
-               UDP_CLEAR_EP_FLAGS(AT91C_EP_IN, AT91C_UDP_TXCOMP);\r
-               while (pUdp->UDP_CSR[AT91C_EP_IN] & AT91C_UDP_TXCOMP);\r
-               UDP_SET_EP_FLAGS(AT91C_EP_IN, AT91C_UDP_TXPKTRDY);\r
-       }\r
-\r
-       // Wait for the end of transfer\r
-       while (!(pUdp->UDP_CSR[AT91C_EP_IN] & AT91C_UDP_TXCOMP)) {\r
-               if (!usb_check()) return length;\r
-       }\r
-\r
-       UDP_CLEAR_EP_FLAGS(AT91C_EP_IN, AT91C_UDP_TXCOMP);\r
-       while (pUdp->UDP_CSR[AT91C_EP_IN] & AT91C_UDP_TXCOMP);\r
-\r
-       return length;\r
+static void AT91F_USB_SendZlp(uint8_t endpoint) {\r
+       UDP_SET_EP_FLAGS(endpoint, AT91C_UDP_TXPKTRDY);\r
+       while (!(AT91C_BASE_UDP->UDP_CSR[endpoint] & AT91C_UDP_TXCOMP))\r
+               /* wait */;\r
+       UDP_CLEAR_EP_FLAGS(endpoint, AT91C_UDP_TXCOMP);\r
+       while (AT91C_BASE_UDP->UDP_CSR[endpoint] & AT91C_UDP_TXCOMP)\r
+               /* wait */;\r
 }\r
 \r
 \r
 }\r
 \r
 \r
@@ -453,10 +359,7 @@ uint32_t usb_write(const byte_t* data, const size_t len) {
 //* \fn    AT91F_USB_SendData\r
 //* \brief Send Data through the control endpoint\r
 //*----------------------------------------------------------------------------\r
 //* \fn    AT91F_USB_SendData\r
 //* \brief Send Data through the control endpoint\r
 //*----------------------------------------------------------------------------\r
-unsigned int csrTab[100] = {0x00};\r
-unsigned char csrIdx = 0;\r
-\r
-static void AT91F_USB_SendData(AT91PS_UDP pUdp, const char *pData, uint32_t length) {\r
+static void AT91F_USB_SendData(const char *pData, uint32_t length) {\r
        uint32_t cpt = 0;\r
        AT91_REG csr;\r
 \r
        uint32_t cpt = 0;\r
        AT91_REG csr;\r
 \r
@@ -465,54 +368,46 @@ static void AT91F_USB_SendData(AT91PS_UDP pUdp, const char *pData, uint32_t leng
                length -= cpt;\r
 \r
                while (cpt--)\r
                length -= cpt;\r
 \r
                while (cpt--)\r
-                       pUdp->UDP_FDR[0] = *pData++;\r
+                       AT91C_BASE_UDP->UDP_FDR[0] = *pData++;\r
 \r
 \r
-               if (pUdp->UDP_CSR[AT91C_EP_CONTROL] & AT91C_UDP_TXCOMP) {\r
+               if (AT91C_BASE_UDP->UDP_CSR[AT91C_EP_CONTROL] & AT91C_UDP_TXCOMP) {\r
                        UDP_CLEAR_EP_FLAGS(AT91C_EP_CONTROL, AT91C_UDP_TXCOMP);\r
                        UDP_CLEAR_EP_FLAGS(AT91C_EP_CONTROL, AT91C_UDP_TXCOMP);\r
-                       while (pUdp->UDP_CSR[AT91C_EP_CONTROL] & AT91C_UDP_TXCOMP);\r
+                       while (AT91C_BASE_UDP->UDP_CSR[AT91C_EP_CONTROL] & AT91C_UDP_TXCOMP)\r
+                               /* wait */;\r
                }\r
 \r
                UDP_SET_EP_FLAGS(AT91C_EP_CONTROL, AT91C_UDP_TXPKTRDY);\r
                do {\r
                }\r
 \r
                UDP_SET_EP_FLAGS(AT91C_EP_CONTROL, AT91C_UDP_TXPKTRDY);\r
                do {\r
-                       csr = pUdp->UDP_CSR[AT91C_EP_CONTROL];\r
+                       csr = AT91C_BASE_UDP->UDP_CSR[AT91C_EP_CONTROL];\r
 \r
                        // Data IN stage has been stopped by a status OUT\r
                        if (csr & AT91C_UDP_RX_DATA_BK0) {\r
                                UDP_CLEAR_EP_FLAGS(AT91C_EP_CONTROL, AT91C_UDP_RX_DATA_BK0);\r
                                return;\r
                        }\r
 \r
                        // Data IN stage has been stopped by a status OUT\r
                        if (csr & AT91C_UDP_RX_DATA_BK0) {\r
                                UDP_CLEAR_EP_FLAGS(AT91C_EP_CONTROL, AT91C_UDP_RX_DATA_BK0);\r
                                return;\r
                        }\r
-               } while ( !(csr & AT91C_UDP_TXCOMP) );\r
+               } while (!(csr & AT91C_UDP_TXCOMP));\r
 \r
        } while (length);\r
 \r
 \r
        } while (length);\r
 \r
-       if (pUdp->UDP_CSR[AT91C_EP_CONTROL] & AT91C_UDP_TXCOMP) {\r
+       if (AT91C_BASE_UDP->UDP_CSR[AT91C_EP_CONTROL] & AT91C_UDP_TXCOMP) {\r
                UDP_CLEAR_EP_FLAGS(AT91C_EP_CONTROL, AT91C_UDP_TXCOMP);\r
                UDP_CLEAR_EP_FLAGS(AT91C_EP_CONTROL, AT91C_UDP_TXCOMP);\r
-               while (pUdp->UDP_CSR[AT91C_EP_CONTROL] & AT91C_UDP_TXCOMP);\r
+               while (AT91C_BASE_UDP->UDP_CSR[AT91C_EP_CONTROL] & AT91C_UDP_TXCOMP)\r
+                       /* wait */;\r
        }\r
 }\r
 \r
 \r
        }\r
 }\r
 \r
 \r
-//*----------------------------------------------------------------------------\r
-//* \fn    AT91F_USB_SendZlp\r
-//* \brief Send zero length packet through the control endpoint\r
-//*----------------------------------------------------------------------------\r
-void AT91F_USB_SendZlp(AT91PS_UDP pUdp) {\r
-       UDP_SET_EP_FLAGS(AT91C_EP_CONTROL, AT91C_UDP_TXPKTRDY);\r
-       while ( !(pUdp->UDP_CSR[AT91C_EP_CONTROL] & AT91C_UDP_TXCOMP) );\r
-       UDP_CLEAR_EP_FLAGS(AT91C_EP_CONTROL, AT91C_UDP_TXCOMP);\r
-       while (pUdp->UDP_CSR[AT91C_EP_CONTROL] & AT91C_UDP_TXCOMP);\r
-}\r
-\r
-\r
 //*----------------------------------------------------------------------------\r
 //* \fn    AT91F_USB_SendStall\r
 //* \brief Stall the control endpoint\r
 //*----------------------------------------------------------------------------\r
 //*----------------------------------------------------------------------------\r
 //* \fn    AT91F_USB_SendStall\r
 //* \brief Stall the control endpoint\r
 //*----------------------------------------------------------------------------\r
-void AT91F_USB_SendStall(AT91PS_UDP pUdp) {\r
+static void AT91F_USB_SendStall(void) {\r
        UDP_SET_EP_FLAGS(AT91C_EP_CONTROL, AT91C_UDP_FORCESTALL);\r
        UDP_SET_EP_FLAGS(AT91C_EP_CONTROL, AT91C_UDP_FORCESTALL);\r
-       while ( !(pUdp->UDP_CSR[AT91C_EP_CONTROL] & AT91C_UDP_ISOERROR) );\r
+       while (!(AT91C_BASE_UDP->UDP_CSR[AT91C_EP_CONTROL] & AT91C_UDP_ISOERROR))\r
+               /* wait */;\r
        UDP_CLEAR_EP_FLAGS(AT91C_EP_CONTROL, AT91C_UDP_FORCESTALL | AT91C_UDP_ISOERROR);\r
        UDP_CLEAR_EP_FLAGS(AT91C_EP_CONTROL, AT91C_UDP_FORCESTALL | AT91C_UDP_ISOERROR);\r
-       while (pUdp->UDP_CSR[AT91C_EP_CONTROL] & (AT91C_UDP_FORCESTALL | AT91C_UDP_ISOERROR));\r
+       while (AT91C_BASE_UDP->UDP_CSR[AT91C_EP_CONTROL] & (AT91C_UDP_FORCESTALL | AT91C_UDP_ISOERROR))\r
+               /* wait */;\r
 }\r
 \r
 \r
 }\r
 \r
 \r
@@ -520,136 +415,349 @@ void AT91F_USB_SendStall(AT91PS_UDP pUdp) {
 //* \fn    AT91F_CDC_Enumerate\r
 //* \brief This function is a callback invoked when a SETUP packet is received\r
 //*----------------------------------------------------------------------------\r
 //* \fn    AT91F_CDC_Enumerate\r
 //* \brief This function is a callback invoked when a SETUP packet is received\r
 //*----------------------------------------------------------------------------\r
-void AT91F_CDC_Enumerate() {\r
-       byte_t bmRequestType, bRequest;\r
+static void AT91F_CDC_Enumerate() {\r
+       uint8_t bmRequestType, bRequest;\r
        uint16_t wValue, wIndex, wLength, wStatus;\r
 \r
        uint16_t wValue, wIndex, wLength, wStatus;\r
 \r
-       if ( !(pUdp->UDP_CSR[AT91C_EP_CONTROL] & AT91C_UDP_RXSETUP) )\r
+       if (!(AT91C_BASE_UDP->UDP_CSR[AT91C_EP_CONTROL] & AT91C_UDP_RXSETUP))\r
                return;\r
 \r
                return;\r
 \r
-       bmRequestType = pUdp->UDP_FDR[AT91C_EP_CONTROL];\r
-       bRequest      = pUdp->UDP_FDR[AT91C_EP_CONTROL];\r
-       wValue        = (pUdp->UDP_FDR[AT91C_EP_CONTROL] & 0xFF);\r
-       wValue       |= (pUdp->UDP_FDR[AT91C_EP_CONTROL] << 8);\r
-       wIndex        = (pUdp->UDP_FDR[AT91C_EP_CONTROL] & 0xFF);\r
-       wIndex       |= (pUdp->UDP_FDR[AT91C_EP_CONTROL] << 8);\r
-       wLength       = (pUdp->UDP_FDR[AT91C_EP_CONTROL] & 0xFF);\r
-       wLength      |= (pUdp->UDP_FDR[AT91C_EP_CONTROL] << 8);\r
+       bmRequestType = AT91C_BASE_UDP->UDP_FDR[AT91C_EP_CONTROL];\r
+       bRequest      = AT91C_BASE_UDP->UDP_FDR[AT91C_EP_CONTROL];\r
+       wValue        = (AT91C_BASE_UDP->UDP_FDR[AT91C_EP_CONTROL] & 0xFF);\r
+       wValue       |= (AT91C_BASE_UDP->UDP_FDR[AT91C_EP_CONTROL] << 8);\r
+       wIndex        = (AT91C_BASE_UDP->UDP_FDR[AT91C_EP_CONTROL] & 0xFF);\r
+       wIndex       |= (AT91C_BASE_UDP->UDP_FDR[AT91C_EP_CONTROL] << 8);\r
+       wLength       = (AT91C_BASE_UDP->UDP_FDR[AT91C_EP_CONTROL] & 0xFF);\r
+       wLength      |= (AT91C_BASE_UDP->UDP_FDR[AT91C_EP_CONTROL] << 8);\r
 \r
 \r
-       if (bmRequestType & 0x80) {     // Data Phase Transfer Direction Device to Host\r
+       if (bmRequestType & 0x80) { // Data Phase Transfer Direction Device to Host\r
                UDP_SET_EP_FLAGS(AT91C_EP_CONTROL, AT91C_UDP_DIR);\r
                UDP_SET_EP_FLAGS(AT91C_EP_CONTROL, AT91C_UDP_DIR);\r
-               while ( !(pUdp->UDP_CSR[AT91C_EP_CONTROL] & AT91C_UDP_DIR) );\r
+               while (!(AT91C_BASE_UDP->UDP_CSR[AT91C_EP_CONTROL] & AT91C_UDP_DIR))\r
+                       /* wait */;\r
        }\r
        UDP_CLEAR_EP_FLAGS(AT91C_EP_CONTROL, AT91C_UDP_RXSETUP);\r
        }\r
        UDP_CLEAR_EP_FLAGS(AT91C_EP_CONTROL, AT91C_UDP_RXSETUP);\r
-       while ( (pUdp->UDP_CSR[AT91C_EP_CONTROL] & AT91C_UDP_RXSETUP)  );\r
+       while (AT91C_BASE_UDP->UDP_CSR[AT91C_EP_CONTROL] & AT91C_UDP_RXSETUP)\r
+               /* wait */;\r
 \r
        // Handle supported standard device request Cf Table 9-3 in USB specification Rev 1.1\r
        switch ((bRequest << 8) | bmRequestType) {\r
        case STD_GET_DESCRIPTOR:\r
                if (wValue == 0x100)       // Return Device Descriptor\r
 \r
        // Handle supported standard device request Cf Table 9-3 in USB specification Rev 1.1\r
        switch ((bRequest << 8) | bmRequestType) {\r
        case STD_GET_DESCRIPTOR:\r
                if (wValue == 0x100)       // Return Device Descriptor\r
-                       AT91F_USB_SendData(pUdp, devDescriptor, MIN(sizeof(devDescriptor), wLength));\r
+                       AT91F_USB_SendData(devDescriptor, MIN(sizeof(devDescriptor), wLength));\r
                else if (wValue == 0x200)  // Return Configuration Descriptor\r
                else if (wValue == 0x200)  // Return Configuration Descriptor\r
-                       AT91F_USB_SendData(pUdp, cfgDescriptor, MIN(sizeof(cfgDescriptor), wLength));\r
+                       AT91F_USB_SendData(cfgDescriptor, MIN(sizeof(cfgDescriptor), wLength));\r
                else if ((wValue & 0xF00) == 0x300) { // Return String Descriptor\r
                        const char *strDescriptor = getStringDescriptor(wValue & 0xff);\r
                        if (strDescriptor != NULL) {\r
                else if ((wValue & 0xF00) == 0x300) { // Return String Descriptor\r
                        const char *strDescriptor = getStringDescriptor(wValue & 0xff);\r
                        if (strDescriptor != NULL) {\r
-                               AT91F_USB_SendData(pUdp, strDescriptor, MIN(strDescriptor[0], wLength));\r
+                               AT91F_USB_SendData(strDescriptor, MIN(strDescriptor[0], wLength));\r
                        } else {\r
                        } else {\r
-                               AT91F_USB_SendStall(pUdp);\r
+                               AT91F_USB_SendStall();\r
                        }\r
                }\r
                else\r
                        }\r
                }\r
                else\r
-                       AT91F_USB_SendStall(pUdp);\r
+                       AT91F_USB_SendStall();\r
                break;\r
        case STD_SET_ADDRESS:\r
                break;\r
        case STD_SET_ADDRESS:\r
-               AT91F_USB_SendZlp(pUdp);\r
-               pUdp->UDP_FADDR = (AT91C_UDP_FEN | wValue);\r
-               pUdp->UDP_GLBSTATE  = (wValue) ? AT91C_UDP_FADDEN : 0;\r
+               AT91F_USB_SendZlp(AT91C_EP_CONTROL);\r
+               AT91C_BASE_UDP->UDP_FADDR = (AT91C_UDP_FEN | wValue);\r
+               AT91C_BASE_UDP->UDP_GLBSTATE  = (wValue) ? AT91C_UDP_FADDEN : 0;\r
                break;\r
        case STD_SET_CONFIGURATION:\r
                btConfiguration = wValue;\r
                break;\r
        case STD_SET_CONFIGURATION:\r
                btConfiguration = wValue;\r
-               AT91F_USB_SendZlp(pUdp);\r
-               pUdp->UDP_GLBSTATE  = (wValue) ? AT91C_UDP_CONFG : AT91C_UDP_FADDEN;\r
-               pUdp->UDP_CSR[AT91C_EP_OUT]    = (wValue) ? (AT91C_UDP_EPEDS | AT91C_UDP_EPTYPE_BULK_OUT) : 0;\r
-               pUdp->UDP_CSR[AT91C_EP_IN]     = (wValue) ? (AT91C_UDP_EPEDS | AT91C_UDP_EPTYPE_BULK_IN)  : 0;\r
-               pUdp->UDP_CSR[AT91C_EP_NOTIFY] = (wValue) ? (AT91C_UDP_EPEDS | AT91C_UDP_EPTYPE_INT_IN)   : 0;\r
+               AT91F_USB_SendZlp(AT91C_EP_CONTROL);\r
+               AT91C_BASE_UDP->UDP_GLBSTATE  = (wValue) ? AT91C_UDP_CONFG : AT91C_UDP_FADDEN;\r
+               AT91C_BASE_UDP->UDP_CSR[AT91C_EP_OUT]    = (wValue) ? (AT91C_UDP_EPEDS | AT91C_UDP_EPTYPE_BULK_OUT) : 0;\r
+               AT91C_BASE_UDP->UDP_CSR[AT91C_EP_IN]     = (wValue) ? (AT91C_UDP_EPEDS | AT91C_UDP_EPTYPE_BULK_IN)  : 0;\r
+               AT91C_BASE_UDP->UDP_CSR[AT91C_EP_NOTIFY] = (wValue) ? (AT91C_UDP_EPEDS | AT91C_UDP_EPTYPE_INT_IN)   : 0;\r
                break;\r
        case STD_GET_CONFIGURATION:\r
                break;\r
        case STD_GET_CONFIGURATION:\r
-               AT91F_USB_SendData(pUdp, (char *) &(btConfiguration), sizeof(btConfiguration));\r
+               AT91F_USB_SendData((char *) &(btConfiguration), sizeof(btConfiguration));\r
                break;\r
        case STD_GET_STATUS_ZERO:\r
                break;\r
        case STD_GET_STATUS_ZERO:\r
-               wStatus = 0;    // Device is Bus powered, remote wakeup disabled\r
-               AT91F_USB_SendData(pUdp, (char *) &wStatus, sizeof(wStatus));\r
+               wStatus = 0;    // Device is Bus powered, remote wakeup disabled\r
+               AT91F_USB_SendData((char *) &wStatus, sizeof(wStatus));\r
                break;\r
        case STD_GET_STATUS_INTERFACE:\r
                break;\r
        case STD_GET_STATUS_INTERFACE:\r
-               wStatus = 0;    // reserved for future use\r
-               AT91F_USB_SendData(pUdp, (char *) &wStatus, sizeof(wStatus));\r
+               wStatus = 0;    // reserved for future use\r
+               AT91F_USB_SendData((char *) &wStatus, sizeof(wStatus));\r
                break;\r
        case STD_GET_STATUS_ENDPOINT:\r
                wStatus = 0;\r
                wIndex &= 0x0F;\r
                break;\r
        case STD_GET_STATUS_ENDPOINT:\r
                wStatus = 0;\r
                wIndex &= 0x0F;\r
-               if ((pUdp->UDP_GLBSTATE & AT91C_UDP_CONFG) && (wIndex <= AT91C_EP_NOTIFY)) {\r
-                       wStatus = (pUdp->UDP_CSR[wIndex] & AT91C_UDP_EPEDS) ? 0 : 1;\r
-                       AT91F_USB_SendData(pUdp, (char *) &wStatus, sizeof(wStatus));\r
-               }\r
-               else if ((pUdp->UDP_GLBSTATE & AT91C_UDP_FADDEN) && (wIndex == AT91C_EP_CONTROL)) {\r
-                       wStatus = (pUdp->UDP_CSR[wIndex] & AT91C_UDP_EPEDS) ? 0 : 1;\r
-                       AT91F_USB_SendData(pUdp, (char *) &wStatus, sizeof(wStatus));\r
-               }\r
-               else\r
-                       AT91F_USB_SendStall(pUdp);\r
+               if ((AT91C_BASE_UDP->UDP_GLBSTATE & AT91C_UDP_CONFG) && (wIndex <= AT91C_EP_NOTIFY)) {\r
+                       wStatus = (AT91C_BASE_UDP->UDP_CSR[wIndex] & AT91C_UDP_EPEDS) ? 0 : 1;\r
+                       AT91F_USB_SendData((char *) &wStatus, sizeof(wStatus));\r
+               } else if ((AT91C_BASE_UDP->UDP_GLBSTATE & AT91C_UDP_FADDEN) && (wIndex == AT91C_EP_CONTROL)) {\r
+                       wStatus = (AT91C_BASE_UDP->UDP_CSR[wIndex] & AT91C_UDP_EPEDS) ? 0 : 1;\r
+                       AT91F_USB_SendData((char *) &wStatus, sizeof(wStatus));\r
+               } else\r
+                       AT91F_USB_SendStall();\r
                break;\r
        case STD_SET_FEATURE_ZERO:\r
                break;\r
        case STD_SET_FEATURE_ZERO:\r
-               AT91F_USB_SendStall(pUdp);\r
-           break;\r
+               AT91F_USB_SendStall();\r
+               break;\r
        case STD_SET_FEATURE_INTERFACE:\r
        case STD_SET_FEATURE_INTERFACE:\r
-               AT91F_USB_SendZlp(pUdp);\r
+               AT91F_USB_SendZlp(AT91C_EP_CONTROL);\r
                break;\r
        case STD_SET_FEATURE_ENDPOINT:\r
                wIndex &= 0x0F;\r
                if ((wValue == 0) && (wIndex >= AT91C_EP_OUT) && (wIndex <= AT91C_EP_NOTIFY)) {\r
                break;\r
        case STD_SET_FEATURE_ENDPOINT:\r
                wIndex &= 0x0F;\r
                if ((wValue == 0) && (wIndex >= AT91C_EP_OUT) && (wIndex <= AT91C_EP_NOTIFY)) {\r
-                       pUdp->UDP_CSR[wIndex] = 0;\r
-                       AT91F_USB_SendZlp(pUdp);\r
-               }\r
-               else\r
-                       AT91F_USB_SendStall(pUdp);\r
+                       AT91C_BASE_UDP->UDP_CSR[wIndex] = 0;\r
+                       AT91F_USB_SendZlp(AT91C_EP_CONTROL);\r
+               } else\r
+                       AT91F_USB_SendStall();\r
                break;\r
        case STD_CLEAR_FEATURE_ZERO:\r
                break;\r
        case STD_CLEAR_FEATURE_ZERO:\r
-               AT91F_USB_SendStall(pUdp);\r
-           break;\r
+               AT91F_USB_SendStall();\r
+               break;\r
        case STD_CLEAR_FEATURE_INTERFACE:\r
        case STD_CLEAR_FEATURE_INTERFACE:\r
-               AT91F_USB_SendZlp(pUdp);\r
+               AT91F_USB_SendZlp(AT91C_EP_CONTROL);\r
                break;\r
        case STD_CLEAR_FEATURE_ENDPOINT:\r
                wIndex &= 0x0F;\r
                if ((wValue == 0) && (wIndex >= AT91C_EP_OUT) && (wIndex <= AT91C_EP_NOTIFY)) {\r
                        if (wIndex == AT91C_EP_OUT)\r
                break;\r
        case STD_CLEAR_FEATURE_ENDPOINT:\r
                wIndex &= 0x0F;\r
                if ((wValue == 0) && (wIndex >= AT91C_EP_OUT) && (wIndex <= AT91C_EP_NOTIFY)) {\r
                        if (wIndex == AT91C_EP_OUT)\r
-                               pUdp->UDP_CSR[AT91C_EP_OUT] = (AT91C_UDP_EPEDS | AT91C_UDP_EPTYPE_BULK_OUT);\r
+                               AT91C_BASE_UDP->UDP_CSR[AT91C_EP_OUT] = (AT91C_UDP_EPEDS | AT91C_UDP_EPTYPE_BULK_OUT);\r
                        else if (wIndex == AT91C_EP_IN)\r
                        else if (wIndex == AT91C_EP_IN)\r
-                               pUdp->UDP_CSR[AT91C_EP_IN] = (AT91C_UDP_EPEDS | AT91C_UDP_EPTYPE_BULK_IN);\r
+                               AT91C_BASE_UDP->UDP_CSR[AT91C_EP_IN] = (AT91C_UDP_EPEDS | AT91C_UDP_EPTYPE_BULK_IN);\r
                        else if (wIndex == AT91C_EP_NOTIFY)\r
                        else if (wIndex == AT91C_EP_NOTIFY)\r
-                               pUdp->UDP_CSR[AT91C_EP_NOTIFY] = (AT91C_UDP_EPEDS | AT91C_UDP_EPTYPE_INT_IN);\r
-                       AT91F_USB_SendZlp(pUdp);\r
+                               AT91C_BASE_UDP->UDP_CSR[AT91C_EP_NOTIFY] = (AT91C_UDP_EPEDS | AT91C_UDP_EPTYPE_INT_IN);\r
+                       AT91F_USB_SendZlp(AT91C_EP_CONTROL);\r
                }\r
                else\r
                }\r
                else\r
-                       AT91F_USB_SendStall(pUdp);\r
+                       AT91F_USB_SendStall();\r
                break;\r
 \r
        // handle CDC class requests\r
        case SET_LINE_CODING:\r
                break;\r
 \r
        // handle CDC class requests\r
        case SET_LINE_CODING:\r
-               while ( !(pUdp->UDP_CSR[AT91C_EP_CONTROL] & AT91C_UDP_RX_DATA_BK0) );\r
+               while (!(AT91C_BASE_UDP->UDP_CSR[AT91C_EP_CONTROL] & AT91C_UDP_RX_DATA_BK0))\r
+                       /* wait */;\r
                UDP_CLEAR_EP_FLAGS(AT91C_EP_CONTROL, AT91C_UDP_RX_DATA_BK0);\r
                UDP_CLEAR_EP_FLAGS(AT91C_EP_CONTROL, AT91C_UDP_RX_DATA_BK0);\r
-               AT91F_USB_SendZlp(pUdp);\r
+               AT91F_USB_SendZlp(AT91C_EP_CONTROL);\r
                break;\r
        case GET_LINE_CODING:\r
                break;\r
        case GET_LINE_CODING:\r
-               AT91F_USB_SendData(pUdp, (char *) &line, MIN(sizeof(line), wLength));\r
+               AT91F_USB_SendData((char *) &line, MIN(sizeof(line), wLength));\r
                break;\r
        case SET_CONTROL_LINE_STATE:\r
                btConnection = wValue;\r
                break;\r
        case SET_CONTROL_LINE_STATE:\r
                btConnection = wValue;\r
-               AT91F_USB_SendZlp(pUdp);\r
+               AT91F_USB_SendZlp(AT91C_EP_CONTROL);\r
                break;\r
        default:\r
                break;\r
        default:\r
-               AT91F_USB_SendStall(pUdp);\r
-           break;\r
+               AT91F_USB_SendStall();\r
+               break;\r
+       }\r
+}\r
+\r
+\r
+//*----------------------------------------------------------------------------\r
+//* \fn    usb_check\r
+//* \brief Test if the device is configured and handle enumeration\r
+//*----------------------------------------------------------------------------\r
+static bool usb_check() {\r
+       AT91_REG isr = AT91C_BASE_UDP->UDP_ISR;\r
+\r
+       if (isr & AT91C_UDP_ENDBUSRES) {\r
+               AT91C_BASE_UDP->UDP_ICR = AT91C_UDP_ENDBUSRES;\r
+               // reset all endpoints\r
+               AT91C_BASE_UDP->UDP_RSTEP  = (unsigned int)-1;\r
+               AT91C_BASE_UDP->UDP_RSTEP  = 0;\r
+               // Enable the function\r
+               AT91C_BASE_UDP->UDP_FADDR = AT91C_UDP_FEN;\r
+               // Configure endpoint 0\r
+               AT91C_BASE_UDP->UDP_CSR[AT91C_EP_CONTROL] = (AT91C_UDP_EPEDS | AT91C_UDP_EPTYPE_CTRL);\r
+       } else if (isr & AT91C_UDP_EPINT0) {\r
+               AT91C_BASE_UDP->UDP_ICR = AT91C_UDP_EPINT0;\r
+               AT91F_CDC_Enumerate();\r
+       }\r
+       return (btConfiguration) ? true : false;\r
+}\r
+\r
+\r
+bool usb_poll() {\r
+       if (!usb_check()) return false;\r
+       return (AT91C_BASE_UDP->UDP_CSR[AT91C_EP_OUT] & btReceiveBank);\r
+}\r
+\r
+\r
+/**\r
+       In github PR #129, some users appears to get a false positive from\r
+       usb_poll, which returns true, but the usb_read operation\r
+       still returns 0.\r
+       This check is basically the same as above, but also checks\r
+       that the length available to read is non-zero, thus hopefully fixes the\r
+       bug.\r
+**/\r
+bool usb_poll_validate_length() {\r
+       if (!usb_check()) return false;\r
+       if (!(AT91C_BASE_UDP->UDP_CSR[AT91C_EP_OUT] & btReceiveBank)) return false;\r
+       return (AT91C_BASE_UDP->UDP_CSR[AT91C_EP_OUT] >> 16) >  0;\r
+}\r
+\r
+\r
+//*----------------------------------------------------------------------------\r
+//* \fn    usb_read\r
+//* \brief Read available data from Endpoint OUT\r
+//*----------------------------------------------------------------------------\r
+static uint32_t usb_read(uint8_t* data, size_t len) {\r
+       uint8_t bank = btReceiveBank;\r
+       uint32_t packetSize, nbBytesRcv = 0;\r
+       uint32_t time_out = 0;\r
+\r
+       while (len)  {\r
+               if (!usb_check()) break;\r
+\r
+               if ( AT91C_BASE_UDP->UDP_CSR[AT91C_EP_OUT] & bank ) {\r
+                       packetSize = MIN(AT91C_BASE_UDP->UDP_CSR[AT91C_EP_OUT] >> 16, len);\r
+                       len -= packetSize;\r
+                       while (packetSize--)\r
+                               data[nbBytesRcv++] = AT91C_BASE_UDP->UDP_FDR[AT91C_EP_OUT];\r
+                       UDP_CLEAR_EP_FLAGS(AT91C_EP_OUT, bank);\r
+                       if (bank == AT91C_UDP_RX_DATA_BK0) {\r
+                               bank = AT91C_UDP_RX_DATA_BK1;\r
+                       } else {\r
+                               bank = AT91C_UDP_RX_DATA_BK0;\r
+                       }\r
+               }\r
+               if (time_out++ == 0x1fff) break;\r
+       }\r
+\r
+       btReceiveBank = bank;\r
+       return nbBytesRcv;\r
+}\r
+\r
+\r
+//*----------------------------------------------------------------------------\r
+//* \fn    usb_write\r
+//* \brief Send through endpoint 2\r
+//*----------------------------------------------------------------------------\r
+static uint32_t usb_write(const uint8_t* data, const size_t len) {\r
+       size_t length = len;\r
+       uint32_t cpt = 0;\r
+\r
+       if (!length) return 0;\r
+       if (!usb_check()) return 0;\r
+\r
+       // Send the first packet\r
+       cpt = MIN(length, AT91C_EP_IN_SIZE);\r
+       length -= cpt;\r
+       while (cpt--) {\r
+               AT91C_BASE_UDP->UDP_FDR[AT91C_EP_IN] = *data++;\r
+       }\r
+       UDP_SET_EP_FLAGS(AT91C_EP_IN, AT91C_UDP_TXPKTRDY);\r
+       while (!(AT91C_BASE_UDP->UDP_CSR[AT91C_EP_IN] & AT91C_UDP_TXPKTRDY))\r
+               /* wait */;\r
+\r
+       while (length) {\r
+               // Fill the next bank\r
+               cpt = MIN(length, AT91C_EP_IN_SIZE);\r
+               length -= cpt;\r
+               while (cpt--) {\r
+                       AT91C_BASE_UDP->UDP_FDR[AT91C_EP_IN] = *data++;\r
+               }\r
+               // Wait for the previous bank to be sent\r
+               while (!(AT91C_BASE_UDP->UDP_CSR[AT91C_EP_IN] & AT91C_UDP_TXCOMP)) {\r
+                       if (!usb_check()) return length;\r
+               }\r
+               UDP_SET_EP_FLAGS(AT91C_EP_IN, AT91C_UDP_TXPKTRDY);\r
+               while (!(AT91C_BASE_UDP->UDP_CSR[AT91C_EP_IN] & AT91C_UDP_TXPKTRDY))\r
+                       /* wait */;\r
+               UDP_CLEAR_EP_FLAGS(AT91C_EP_IN, AT91C_UDP_TXCOMP);\r
+               while (AT91C_BASE_UDP->UDP_CSR[AT91C_EP_IN] & AT91C_UDP_TXCOMP)\r
+                       /* wait */;\r
+       }\r
+\r
+       // Wait for the end of transfer\r
+       while (!(AT91C_BASE_UDP->UDP_CSR[AT91C_EP_IN] & AT91C_UDP_TXCOMP)) {\r
+               if (!usb_check()) return length;\r
+       }\r
+       UDP_CLEAR_EP_FLAGS(AT91C_EP_IN, AT91C_UDP_TXCOMP);\r
+       while (AT91C_BASE_UDP->UDP_CSR[AT91C_EP_IN] & AT91C_UDP_TXCOMP)\r
+               /* wait */;\r
+\r
+       if (len % AT91C_EP_IN_SIZE == 0) { // need to send a zero length packet to complete the transfer\r
+               AT91F_USB_SendZlp(AT91C_EP_IN);\r
        }\r
        }\r
+\r
+       return length;\r
+}\r
+\r
+\r
+//***************************************************************************\r
+// Interface to the main program\r
+//***************************************************************************\r
+\r
+// The function to receive a command from the client via USB\r
+bool cmd_receive(UsbCommand* cmd) {\r
+\r
+       // Check if there is a usb packet available\r
+       if (!usb_poll())\r
+               return false;\r
+\r
+       // Try to retrieve the available command frame\r
+       size_t rxlen = usb_read((uint8_t*)cmd, sizeof(UsbCommand));\r
+\r
+       // Check if the transfer was complete\r
+       if (rxlen != sizeof(UsbCommand))\r
+               return false;\r
+\r
+       // Received command successfully\r
+       return true;\r
 }\r
 }\r
+\r
+\r
+// The function to send a response to the client via USB\r
+bool cmd_send(uint16_t cmd, uint32_t arg0, uint32_t arg1, uint32_t arg2, void* data, uint16_t datalen) {\r
+\r
+       UsbResponse txcmd;\r
+\r
+       // Compose the outgoing response frame\r
+       txcmd.cmd = cmd | CMD_VARIABLE_SIZE_FLAG;\r
+       txcmd.arg[0] = arg0;\r
+       txcmd.arg[1] = arg1;\r
+       txcmd.arg[2] = arg2;\r
+\r
+       // Add the (optional) content to the frame, with a maximum size of USB_CMD_DATA_SIZE\r
+       if (data) {\r
+               datalen = MIN(datalen, USB_CMD_DATA_SIZE);\r
+               for (uint16_t i = 0; i < datalen; i++) {\r
+                       txcmd.d.asBytes[i] = ((uint8_t*)data)[i];\r
+               }\r
+               txcmd.datalen = datalen;\r
+       } else {\r
+               txcmd.datalen = 0;\r
+       }\r
+\r
+       // Send frame and make sure all bytes are transmitted\r
+       size_t tx_size = offsetof(UsbResponse, d) + datalen;\r
+       if (usb_write((uint8_t*)&txcmd, tx_size) != 0) return false;\r
+\r
+       return true;\r
+}\r
+\r
+\r
+// For compatibility only: legacy function to send a response with fixed size to the client via USB\r
+bool cmd_send_old(uint16_t cmd, uint32_t arg0, uint32_t arg1, uint32_t arg2, void* data, uint16_t datalen) {\r
+\r
+       UsbCommand txcmd;\r
+\r
+       // Compose the outgoing response frame\r
+       txcmd.cmd = cmd;\r
+       txcmd.arg[0] = arg0;\r
+       txcmd.arg[1] = arg1;\r
+       txcmd.arg[2] = arg2;\r
+\r
+       // Add the (optional) content to the frame, with a maximum size of USB_CMD_DATA_SIZE\r
+       if (data) {\r
+               datalen = MIN(datalen, USB_CMD_DATA_SIZE);\r
+               for (uint16_t i = 0; i < datalen; i++) {\r
+                       txcmd.d.asBytes[i] = ((uint8_t*)data)[i];\r
+               }\r
+       }\r
+\r
+       // Send frame and make sure all bytes are transmitted\r
+       if (usb_write((uint8_t*)&txcmd, sizeof(UsbCommand)) != 0) return false;\r
+\r
+       return true;\r
+}\r
+\r
Impressum, Datenschutz