add LPC1549, add FRDM-KL46Z, fix epComplete bit operation.

Dependents:   XBee-ExplorerLite USBLocalFileSystem USB-to-UART-bridge USBLocalFileSystem

Fork of USBDevice by mbed official

Revision:
23:554b76408e69
Parent:
17:bbd6dac92961
--- a/USBDevice/USBHAL_LPC11U.cpp	Tue May 13 15:31:04 2014 +0900
+++ b/USBDevice/USBHAL_LPC11U.cpp	Sat Jun 21 07:25:16 2014 +0900
@@ -16,17 +16,20 @@
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
 */
 
-#if defined(TARGET_LPC11U24) || defined(TARGET_LPC11U35_401) || defined(TARGET_LPC1347)
+#if defined(TARGET_LPC11U24) || defined(TARGET_LPC11U35_401) || defined(TARGET_LPC1347) || defined(TARGET_LPC11U35_501) || defined(TARGET_LPC11U68) || defined(TARGET_LPC1549)
 
-#if defined(TARGET_LPC1347)
+#if defined(TARGET_LPC1347) || defined(TARGET_LPC1549)
 #define USB_IRQ USB_IRQ_IRQn
-#elif defined(TARGET_LPC11U24) || defined(TARGET_LPC11U35_401)
+#elif defined(TARGET_LPC11U24) || defined(TARGET_LPC11U35_401) || defined(TARGET_LPC11U35_501) || defined(TARGET_LPC11U68)
 #define USB_IRQ USB_IRQn
 #endif
 
 #include "USBHAL.h"
 
 USBHAL * USBHAL::instance;
+#if defined(TARGET_LPC1549)
+static uint8_t usbmem[2048] __attribute__((aligned(2048)));
+#endif
 
 // Valid physical endpoint numbers are 0 to (NUMBER_OF_PHYSICAL_ENDPOINTS-1)
 #define LAST_PHYSICAL_ENDPOINT (NUMBER_OF_PHYSICAL_ENDPOINTS-1)
@@ -42,12 +45,21 @@
 #define OUT_EP(endpoint)    ((endpoint) & 1U ? false : true)
 
 // USB RAM
+#if defined(TARGET_LPC1549)
+#define USB_RAM_START ((uint32_t)usbmem)
+#define USB_RAM_SIZE  sizeof(usbmem)
+#else
 #define USB_RAM_START (0x20004000)
 #define USB_RAM_SIZE  (0x00000800)
+#endif
 
 // SYSAHBCLKCTRL
+#if defined(TARGET_LPC1549)
+#define CLK_USB     (1UL<<23)
+#else
 #define CLK_USB     (1UL<<14)
 #define CLK_USBRAM  (1UL<<27)
+#endif
 
 // USB Information register
 #define FRAME_NR(a)     ((a) & 0x7ff)   // Frame number
@@ -134,7 +146,7 @@
 
 USBHAL::USBHAL(void) {
     NVIC_DisableIRQ(USB_IRQ);
-    
+
     // fill in callback array
     epCallback[0] = &USBHAL::EP1_OUT_callback;
     epCallback[1] = &USBHAL::EP1_IN_callback;
@@ -145,11 +157,42 @@
     epCallback[6] = &USBHAL::EP4_OUT_callback;
     epCallback[7] = &USBHAL::EP4_IN_callback;
 
-    #if defined(TARGET_LPC11U35_401)
+#if defined(TARGET_LPC1549)
+    /* Set USB PLL input to system oscillator */
+    LPC_SYSCON->USBPLLCLKSEL = 0x01;
+
+    /* Setup USB PLL  (FCLKIN = 12MHz) * 4 = 48MHz
+       MSEL = 3 (this is pre-decremented), PSEL = 1 (for P = 2)
+       FCLKOUT = FCLKIN * (MSEL + 1) = 12MHz * 4 = 48MHz
+       FCCO = FCLKOUT * 2 * P = 48MHz * 2 * 2 = 192MHz (within FCCO range) */
+    LPC_SYSCON->USBPLLCTRL = (0x3 | (1UL << 6));
+
+    /* Powerup USB PLL */  
+    LPC_SYSCON->PDRUNCFG &= ~(CLK_USB);
+
+    /* Wait for PLL to lock */
+    while(!(LPC_SYSCON->USBPLLSTAT & 0x01));
+
+    /* enable USB main clock */
+    LPC_SYSCON->USBCLKSEL = 0x02;
+    LPC_SYSCON->USBCLKDIV = 1;
+
+    /* Enable AHB clock to the USB block. */
+    LPC_SYSCON->SYSAHBCLKCTRL1 |= CLK_USB;
+
+    /* power UP USB Phy */
+    LPC_SYSCON->PDRUNCFG &= ~(1UL << 9);
+
+    /* Reset USB block */
+    LPC_SYSCON->PRESETCTRL1 |= (CLK_USB);
+    LPC_SYSCON->PRESETCTRL1 &= ~(CLK_USB);
+
+#else
+    #if defined(TARGET_LPC11U35_401) || defined(TARGET_LPC11U35_501)
     // USB_VBUS input with pull-down
     LPC_IOCON->PIO0_3 = 0x00000009;
     #endif
-    
+
     // nUSB_CONNECT output
     LPC_IOCON->PIO0_6 = 0x00000001;
 
@@ -158,7 +201,7 @@
 
     // Ensure device disconnected (DCON not set)
     LPC_USB->DEVCMDSTAT = 0;
-
+#endif
     // to ensure that the USB host sees the device as
     // disconnected if the target CPU is reset.
     wait(0.3);
@@ -287,13 +330,13 @@
             bf = 0;
         }
     }
-    
+
     // if isochronous endpoint, T = 1
     if(endpointState[endpoint].options & ISOCHRONOUS)
     {
         flags |= CMDSTS_T;
     }
-        
+
     //Active the endpoint for reading
     ep[PHY_TO_LOG(endpoint)].out[bf] = CMDSTS_A | CMDSTS_NBYTES(maximumSize) \
                                        | CMDSTS_ADDRESS_OFFSET((uint32_t)ct->out) | flags;
@@ -408,7 +451,7 @@
 
 EP_STATUS USBHAL::endpointWriteResult(uint8_t endpoint) {
     uint32_t bf;
-    
+
     // Validate parameters
     if (endpoint > LAST_PHYSICAL_ENDPOINT) {
         return EP_INVALID;
@@ -680,7 +723,7 @@
         // EP0IN ACK event (IN data sent)
         EP0in();
     }
-    
+
     for (uint8_t num = 2; num < 5*2; num++) {
         if (LPC_USB->INTSTAT & EP(num)) {
             LPC_USB->INTSTAT = EP(num);