*/\r
\r
#include "usb_cdc.h"\r
+#include "util.h"\r
\r
#define MIN(a, b) (((a) < (b)) ? (a) : (b))\r
#define MAX(a, b) (((a) > (b)) ? (a) : (b))\r
0x00, // bDeviceSubclass: CDC class sub code\r
0x00, // bDeviceProtocol: CDC Device protocol\r
0x08, // bMaxPacketSize0\r
- 0xeb,0x03, // Vendor ID (random numbers)\r
- 0x25,0x61, // Product ID (random numbers)\r
+ 0x2d,0x2d, // Vendor ID (--)\r
+ 0x4d,0x50, // Product ID (PM), transmitted in reverse\r
0x01,0x00, // Device release number (0001)\r
0x01, // iManufacturer // 0x01\r
0x00, // iProduct\r
byte_t btConnection = 0;\r
byte_t btReceiveBank = AT91C_UDP_RX_DATA_BK0;\r
\r
+//*----------------------------------------------------------------------------\r
+//* \fn AT91F_USB_Disable\r
+//* \brief This function deactivates the USB device\r
+//*----------------------------------------------------------------------------\r
+void usb_disable() {\r
+ // Disconnect and reconnect USB controller for 100ms\r
+ AT91C_BASE_PIOA->PIO_ODR = AT91C_PIO_PA24;\r
+ SpinDelay(100);\r
+ \r
+ // Clear all lingering interrupts\r
+ if(pUdp->UDP_ISR & AT91C_UDP_ENDBUSRES) {\r
+ pUdp->UDP_ICR = AT91C_UDP_ENDBUSRES;\r
+ }\r
+}\r
+\r
//*----------------------------------------------------------------------------\r
//* \fn AT91F_USB_Enable\r
//* \brief This function Activates the USB device\r
//*----------------------------------------------------------------------------\r
-void usb_enable()\r
-{\r
+void usb_enable() {\r
// Set the PLL USB Divider\r
AT91C_BASE_CKGR->CKGR_PLLR |= AT91C_CKGR_USBDIV_1 ;\r
\r
\r
// Clear for set the Pul up resistor\r
AT91C_BASE_PIOA->PIO_CODR = AT91C_PIO_PA16;\r
+ \r
+ // Disconnect and USB device\r
+ usb_disable();\r
+ \r
+ // Wait for a short while\r
+ SpinDelay(100);\r
+\r
+ // Reconnect USB reconnect\r
+ AT91C_BASE_PIOA->PIO_SODR = AT91C_PIO_PA24;\r
+ AT91C_BASE_PIOA->PIO_OER = AT91C_PIO_PA24;\r
}\r
\r
//*----------------------------------------------------------------------------\r
//* \fn AT91F_UDP_IsConfigured\r
//* \brief Test if the device is configured and handle enumeration\r
//*----------------------------------------------------------------------------\r
-bool usb_check()\r
-{\r
+bool usb_check() {\r
AT91_REG isr = pUdp->UDP_ISR;\r
\r
if (isr & AT91C_UDP_ENDBUSRES) {\r
//* \fn AT91F_UDP_Read\r
//* \brief Read available data from Endpoint OUT\r
//*----------------------------------------------------------------------------\r
-uint32_t usb_read(byte_t* data, size_t len)\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
//* \fn AT91F_CDC_Write\r
//* \brief Send through endpoint 2\r
//*----------------------------------------------------------------------------\r
-uint32_t usb_write(const byte_t* data, const size_t len)\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
unsigned int csrTab[100];\r
unsigned char csrIdx = 0;\r
\r
-static void AT91F_USB_SendData(AT91PS_UDP pUdp, const char *pData, uint32_t length)\r
-{\r
+static void AT91F_USB_SendData(AT91PS_UDP pUdp, const char *pData, uint32_t length) {\r
uint32_t cpt = 0;\r
AT91_REG csr;\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
-{\r
+void AT91F_USB_SendZlp(AT91PS_UDP pUdp) {\r
pUdp->UDP_CSR[0] |= AT91C_UDP_TXPKTRDY;\r
while ( !(pUdp->UDP_CSR[0] & AT91C_UDP_TXCOMP) );\r
pUdp->UDP_CSR[0] &= ~(AT91C_UDP_TXCOMP);\r
//* \fn AT91F_USB_SendStall\r
//* \brief Stall the control endpoint\r
//*----------------------------------------------------------------------------\r
-void AT91F_USB_SendStall(AT91PS_UDP pUdp)\r
-{\r
+void AT91F_USB_SendStall(AT91PS_UDP pUdp) {\r
pUdp->UDP_CSR[0] |= AT91C_UDP_FORCESTALL;\r
while ( !(pUdp->UDP_CSR[0] & AT91C_UDP_ISOERROR) );\r
pUdp->UDP_CSR[0] &= ~(AT91C_UDP_FORCESTALL | AT91C_UDP_ISOERROR);\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
-{\r
+void AT91F_CDC_Enumerate() {\r
byte_t bmRequestType, bRequest;\r
uint16_t wValue, wIndex, wLength, wStatus;\r
\r