A fully-Android-compatible two joysticks USB driver for LPC1768. The joysticks have 1 hat, 6 buttons, and there are 1P, 2P buttons.

Dependencies:   mbed

Fork of app-board-Joystick by Chris Styles

Revision:
3:f1a8ec4659f8
Parent:
1:76c47d2ba442
--- a/usbdc.cpp	Sat Dec 17 13:13:59 2016 +0000
+++ b/usbdc.cpp	Sat Dec 17 15:10:12 2016 +0000
@@ -143,7 +143,7 @@
     enableEvents();
 }
 
-void usbdc::connect(void)
+void usbdc::connect()
 {
     /* Connect USB device */
     unsigned char status;
@@ -152,7 +152,7 @@
     setDeviceStatus(status | SIE_DS_CON);
 }
 
-void usbdc::disconnect(void)
+void usbdc::disconnect()
 {
     /* Disconnect USB device */
     unsigned char status;
@@ -193,7 +193,7 @@
     SIEWriteData(status);
 }
 
-unsigned char usbdc::getDeviceStatus(void)
+unsigned char usbdc::getDeviceStatus()
 {
     /* Read SIE device status register */
     SIECommand(SIE_CMD_GET_DEVICE_STATUS);
@@ -233,14 +233,14 @@
 }
 #endif
 
-unsigned char usbdc::clearBuffer(void)
+unsigned char usbdc::clearBuffer()
 {
     /* SIE clear buffer command */
     SIECommand(SIE_CMD_CLEAR_BUFFER);
     return SIEReadData(SIE_CMD_CLEAR_BUFFER);
 }
 
-void usbdc::validateBuffer(void)
+void usbdc::validateBuffer()
 {
     /* SIE validate buffer command */
     SIECommand(SIE_CMD_VALIDATE_BUFFER);
@@ -283,7 +283,7 @@
 void usbdc::stallEndpoint(unsigned char endpoint)
 {
     /* Stall an endpoint */
-    if ( (endpoint==EP0IN) || (endpoint==EP0OUT) )
+    if ( (endpoint == EP0IN) || (endpoint == EP0OUT) )
     {
         /* Conditionally stall both control endpoints */
         setEndpointStatus(EP0OUT, SIE_SES_CND_ST);
@@ -312,14 +312,14 @@
     return endpointStallState & EP(endpoint);
 }
 
-void usbdc::configureDevice(void)
+void usbdc::configureDevice()
 {
     /* SIE Configure device command */
     SIECommand(SIE_CMD_CONFIGURE_DEVICE);
     SIEWriteData(SIE_CONF_DEVICE);
 }
 
-void usbdc::unconfigureDevice(void)
+void usbdc::unconfigureDevice()
 {
     /* SIE Configure device command */
     SIECommand(SIE_CMD_CONFIGURE_DEVICE);
@@ -329,35 +329,29 @@
 unsigned long usbdc::endpointRead(unsigned char endpoint, unsigned char *buffer)
 {
     /* Read from an OUT endpoint */
-    unsigned long size;
-    unsigned long i;
-    unsigned long data;
-    unsigned char offset;
-
     LPC_USB->USBCtrl = LOG_ENDPOINT(endpoint) | RD_EN;
-    while (!(LPC_USB->USBRxPLen & PKT_RDY));
+    while(!(LPC_USB->USBRxPLen & PKT_RDY)) ;
 
-    size = LPC_USB->USBRxPLen & PKT_LNGTH_MASK;
-
-    offset = 0;
-
-    for (i=0; i<size; i++)
+    unsigned long size = LPC_USB->USBRxPLen & PKT_LNGTH_MASK;
+    unsigned char offset = 0;
+    unsigned long data;
+    for(unsigned long i = 0; i < size; i++)
     {
-        if (offset==0)
+        if (offset == 0)
         {
             /* Fetch up to four bytes of data as a word */
             data = LPC_USB->USBRxData;
         }
 
         /* extract a byte */
-        *buffer++ = data>>offset;
+        *buffer++ = data >> offset;
 
         /* move on to the next byte */
         offset = (offset + 8) % 32;
     }
 
     /* Clear RD_EN to cover zero length packet case */
-    LPC_USB->USBCtrl=0;
+    LPC_USB->USBCtrl = 0;
 
     selectEndpoint(endpoint);
     clearBuffer();
@@ -368,21 +362,17 @@
 void usbdc::endpointWrite(unsigned char endpoint, unsigned char *buffer, unsigned long size)
 {
     /* Write to an IN endpoint */
-    unsigned long temp, data;
-    unsigned char offset;
-
     LPC_USB->USBCtrl = LOG_ENDPOINT(endpoint) | WR_EN;
-
     LPC_USB->USBTxPLen = size;
-    offset = 0;
-    data = 0;
-
-    if (size>0)
+    
+    unsigned char offset = 0;
+    unsigned long data = 0;
+    if (size > 0)
     {
         do
         {
             /* Fetch next data byte into a word-sized temporary variable */
-            temp = *buffer++;
+            unsigned long temp = *buffer++;
 
             /* Add to current data word */
             temp = temp << offset;
@@ -392,35 +382,35 @@
             offset = (offset + 8) % 32;
             size--;
 
-            if ((offset==0) || (size==0))
+            if ((offset == 0) || (size == 0))
             {
                 /* Write the word to the endpoint */
                 LPC_USB->USBTxData = data;
                 data = 0;
             }
-        } while (size>0);
+        } while(size > 0);
     }
 
     /* Clear WR_EN to cover zero length packet case */
-    LPC_USB->USBCtrl=0;
+    LPC_USB->USBCtrl = 0;
 
     selectEndpoint(endpoint);
     validateBuffer();
 }
 
-void usbdc::enableEvents(void)
+void usbdc::enableEvents()
 {
     /* Enable interrupt sources */
     LPC_USB->USBDevIntEn = EP_SLOW | DEV_STAT;
 }
 
-void usbdc::disableEvents(void)
+void usbdc::disableEvents()
 {
     /* Disable interrupt sources */
     LPC_USB->USBDevIntClr = EP_SLOW | DEV_STAT;
 }
 
-void usbdc::usbisr(void)
+void usbdc::usbisr()
 {
     unsigned char devStat;
 
@@ -461,9 +451,7 @@
                 endpointEventEP0Setup();
             }
             else
-            {
                 endpointEventEP0Out();
-            }
         }
 
         if (LPC_USB->USBEpIntSt & EP(EP0IN))
@@ -502,44 +490,17 @@
     }
 }
 
-
-void usbdc::_usbisr(void)
+void usbdc::_usbisr()
 {
     instance->usbisr();
 }
 
-void usbdc::deviceEventReset(void)
-{
-}
-
-void usbdc::deviceEventFrame(void)
-{
-}
-
-void usbdc::endpointEventEP0Setup(void)
-{
-}
-
-void usbdc::endpointEventEP0In(void)
-{
-}
-
-void usbdc::endpointEventEP0Out(void)
-{
-}
-
-void usbdc::endpointEventEP1In(void)
-{
-}
-
-void usbdc::endpointEventEP1Out(void)
-{
-}
-
-void usbdc::endpointEventEP2In(void)
-{
-}
-
-void usbdc::endpointEventEP2Out(void)
-{
-}
+void usbdc::deviceEventReset()      { }
+void usbdc::deviceEventFrame()      { }
+void usbdc::endpointEventEP0Setup() { }
+void usbdc::endpointEventEP0In()    { }
+void usbdc::endpointEventEP0Out()   { }
+void usbdc::endpointEventEP1In()    { }
+void usbdc::endpointEventEP1Out()   { }
+void usbdc::endpointEventEP2In()    { }
+void usbdc::endpointEventEP2Out()   { }