\r
\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
- reg = pUdp->UDP_CSR[(endpoint)]; \\r
+ reg = AT91C_BASE_UDP->UDP_CSR[(endpoint)]; \\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
// 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
- pUdp->UDP_CSR[(endpoint)] = reg; \\r
+ AT91C_BASE_UDP->UDP_CSR[(endpoint)] = reg; \\r
}\r
\r
\r
} AT91S_CDC_LINE_CODING, *AT91PS_CDC_LINE_CODING;\r
\r
\r
-AT91S_CDC_LINE_CODING line = {\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
-static void AT91F_CDC_Enumerate();\r
-\r
-AT91PS_UDP pUdp = AT91C_BASE_UDP;\r
-uint8_t btConfiguration = 0;\r
-uint8_t btConnection = 0;\r
-uint8_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
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
-//* \fn usb_check\r
-//* \brief Test if the device is configured and handle enumeration\r
-//*----------------------------------------------------------------------------\r
-static 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
- 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
- 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
-//*----------------------------------------------------------------------------\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 ( 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
-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
- 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
- /* wait */;\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
+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
- return length;\r
}\r
\r
\r
//* \fn AT91F_USB_SendData\r
//* \brief Send Data through the control endpoint\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
length -= cpt;\r
\r
while (cpt--)\r
- pUdp->UDP_FDR[0] = *pData++;\r
+ AT91C_BASE_UDP->UDP_FDR[0] = *pData++;\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
- 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
- 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
\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
- 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
-//* \fn AT91F_USB_SendZlp\r
-//* \brief Send zero length packet through the control endpoint\r
-//*----------------------------------------------------------------------------\r
-static 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
- /* wait */;\r
- UDP_CLEAR_EP_FLAGS(AT91C_EP_CONTROL, AT91C_UDP_TXCOMP);\r
- while (pUdp->UDP_CSR[AT91C_EP_CONTROL] & AT91C_UDP_TXCOMP)\r
- /* wait */;\r
-}\r
-\r
-\r
//*----------------------------------------------------------------------------\r
//* \fn AT91F_USB_SendStall\r
//* \brief Stall the control endpoint\r
//*----------------------------------------------------------------------------\r
-static 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
- 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
- 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
uint8_t bmRequestType, bRequest;\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
- 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
if (bmRequestType & 0x80) { // Data Phase Transfer Direction Device to Host\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
- 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
- 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
- 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
- AT91F_USB_SendData(pUdp, strDescriptor, MIN(strDescriptor[0], wLength));\r
+ AT91F_USB_SendData(strDescriptor, MIN(strDescriptor[0], wLength));\r
} else {\r
- AT91F_USB_SendStall(pUdp);\r
+ AT91F_USB_SendStall();\r
}\r
}\r
else\r
- AT91F_USB_SendStall(pUdp);\r
+ AT91F_USB_SendStall();\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
- 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
- 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
wStatus = 0; // Device is Bus powered, remote wakeup disabled\r
- AT91F_USB_SendData(pUdp, (char *) &wStatus, sizeof(wStatus));\r
+ AT91F_USB_SendData((char *) &wStatus, sizeof(wStatus));\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
+ AT91F_USB_SendData((char *) &wStatus, sizeof(wStatus));\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
- } 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
+ 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(pUdp);\r
+ AT91F_USB_SendStall();\r
break;\r
case STD_SET_FEATURE_ZERO:\r
- AT91F_USB_SendStall(pUdp);\r
+ AT91F_USB_SendStall();\r
break;\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
- pUdp->UDP_CSR[wIndex] = 0;\r
- AT91F_USB_SendZlp(pUdp);\r
+ AT91C_BASE_UDP->UDP_CSR[wIndex] = 0;\r
+ AT91F_USB_SendZlp(AT91C_EP_CONTROL);\r
} else\r
- AT91F_USB_SendStall(pUdp);\r
+ AT91F_USB_SendStall();\r
break;\r
case STD_CLEAR_FEATURE_ZERO:\r
- AT91F_USB_SendStall(pUdp);\r
+ AT91F_USB_SendStall();\r
break;\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
- 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
- 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
- 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
- AT91F_USB_SendStall(pUdp);\r
+ AT91F_USB_SendStall();\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
- AT91F_USB_SendZlp(pUdp);\r
+ AT91F_USB_SendZlp(AT91C_EP_CONTROL);\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
- AT91F_USB_SendZlp(pUdp);\r
+ AT91F_USB_SendZlp(AT91C_EP_CONTROL);\r
break;\r
default:\r
- AT91F_USB_SendStall(pUdp);\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
+ return length;\r
+}\r
+\r
+\r
//***************************************************************************\r
// Interface to the main program\r
//***************************************************************************\r
txcmd.d.asBytes[i] = ((uint8_t*)data)[i];\r
}\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