USB device stack with Nucleo F401RE support. NOTE: the default clock config needs to be changed to in order for USB to work.

Fork of USBDevice by Tomas Cerskus

Slightly modified original USBDevice library to support F401RE.

On F401RE the data pins of your USB connector should be attached to PA12 (D+) and PA11(D-). It is also required to connect the +5V USB line to PA9.

F401RE requires 48MHz clock for USB. Therefore in order for this to work you will need to change the default clock settings:

Clock settings for USB

#include "stm32f4xx_hal.h"

RCC_OscInitTypeDef RCC_OscInitStruct;
RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSI;
RCC_OscInitStruct.HSIState = RCC_HSI_ON;
RCC_OscInitStruct.HSICalibrationValue = 16;
RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON;
RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSI;
RCC_OscInitStruct.PLL.PLLM = 16;
RCC_OscInitStruct.PLL.PLLN = 336;
RCC_OscInitStruct.PLL.PLLP = RCC_PLLP_DIV4;
RCC_OscInitStruct.PLL.PLLQ = 7;
if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK) {
    error("RTC error: LSI clock initialization failed."); 
}

NOTE: Changing the clock frequency might affect the behavior of other libraries. I only tested the Serial library.

UPDATE: Clock settings should not to be changed anymore! Looks like the newer mbed library has the required clock enabled.

Revision:
8:335f2506f422
Parent:
3:6d85e04fb59f
Child:
11:eeb3cbbaa996
--- a/USBDevice/USBHAL_LPC11U.cpp	Mon Jan 21 10:41:28 2013 +0000
+++ b/USBDevice/USBHAL_LPC11U.cpp	Fri Mar 01 13:10:29 2013 +0000
@@ -22,7 +22,6 @@
 
 USBHAL * USBHAL::instance;
 
-
 // Valid physical endpoint numbers are 0 to (NUMBER_OF_PHYSICAL_ENDPOINTS-1)
 #define LAST_PHYSICAL_ENDPOINT (NUMBER_OF_PHYSICAL_ENDPOINTS-1)
 
@@ -129,6 +128,16 @@
 
 USBHAL::USBHAL(void) {
     NVIC_DisableIRQ(USB_IRQn);
+    
+    // fill in callback array
+    epCallback[0] = &USBHAL::EP1_OUT_callback;
+    epCallback[1] = &USBHAL::EP1_IN_callback;
+    epCallback[2] = &USBHAL::EP2_OUT_callback;
+    epCallback[3] = &USBHAL::EP2_IN_callback;
+    epCallback[4] = &USBHAL::EP3_OUT_callback;
+    epCallback[5] = &USBHAL::EP3_IN_callback;
+    epCallback[6] = &USBHAL::EP4_OUT_callback;
+    epCallback[7] = &USBHAL::EP4_IN_callback;
 
     // nUSB_CONNECT output
     LPC_IOCON->PIO0_6 = 0x00000001;
@@ -181,7 +190,6 @@
 USBHAL::~USBHAL(void) {
     // Ensure device disconnected (DCON not set)
     LPC_USB->DEVCMDSTAT = 0;
-
     // Disable USB interrupts
     NVIC_DisableIRQ(USB_IRQn);
 }
@@ -199,9 +207,11 @@
 }
 
 void USBHAL::configureDevice(void) {
+    // Not required
 }
 
 void USBHAL::unconfigureDevice(void) {
+    // Not required
 }
 
 void USBHAL::EP0setup(uint8_t *buffer) {
@@ -232,6 +242,11 @@
     return bytesRead;
 }
 
+
+void USBHAL::EP0readStage(void) {
+    // Not required
+}
+
 void USBHAL::EP0write(uint8_t *buffer, uint32_t size) {
     // Start and endpoint 0 write
 
@@ -303,10 +318,7 @@
 }
 
 void USBHAL::EP0getWriteResult(void) {
-    // Complete an endpoint 0 write
-
-    // Nothing required for this target
-    return;
+    // Not required
 }
 
 void USBHAL::EP0stall(void) {
@@ -385,8 +397,8 @@
 
 EP_STATUS USBHAL::endpointWriteResult(uint8_t endpoint) {
     uint32_t bf;
+    
     // Validate parameters
-
     if (endpoint > LAST_PHYSICAL_ENDPOINT) {
         return EP_INVALID;
     }
@@ -422,8 +434,7 @@
 
 void USBHAL::stallEndpoint(uint8_t endpoint) {
 
-    // TODO: should this clear active bit?
-
+    // FIX: should this clear active bit?
     if (IN_EP(endpoint)) {
         ep[PHY_TO_LOG(endpoint)].in[0] |= CMDSTS_S;
         ep[PHY_TO_LOG(endpoint)].in[1] |= CMDSTS_S;
@@ -441,26 +452,26 @@
             ep[PHY_TO_LOG(endpoint)].in[1] = 0; // S = 0
 
             if (LPC_USB->EPINUSE & EP(endpoint)) {
-                ep[PHY_TO_LOG(endpoint)].in[1] = CMDSTS_TR; // S =0, TR=1, TV = 0
+                ep[PHY_TO_LOG(endpoint)].in[1] = CMDSTS_TR; // S = 0, TR = 1, TV = 0
             } else {
-                ep[PHY_TO_LOG(endpoint)].in[0] = CMDSTS_TR; // S =0, TR=1, TV = 0
+                ep[PHY_TO_LOG(endpoint)].in[0] = CMDSTS_TR; // S = 0, TR = 1, TV = 0
             }
         } else {
             ep[PHY_TO_LOG(endpoint)].out[0] = 0; // S = 0
             ep[PHY_TO_LOG(endpoint)].out[1] = 0; // S = 0
 
             if (LPC_USB->EPINUSE & EP(endpoint)) {
-                ep[PHY_TO_LOG(endpoint)].out[1] = CMDSTS_TR; // S =0, TR=1, TV = 0
+                ep[PHY_TO_LOG(endpoint)].out[1] = CMDSTS_TR; // S = 0, TR = 1, TV = 0
             } else {
-                ep[PHY_TO_LOG(endpoint)].out[0] = CMDSTS_TR; // S =0, TR=1, TV = 0
+                ep[PHY_TO_LOG(endpoint)].out[0] = CMDSTS_TR; // S = 0, TR = 1, TV = 0
             }
         }
     } else {
         // Single buffered
         if (IN_EP(endpoint)) {
-            ep[PHY_TO_LOG(endpoint)].in[0] = CMDSTS_TR; // S=0, TR=1, TV = 0
+            ep[PHY_TO_LOG(endpoint)].in[0] = CMDSTS_TR;     // S = 0, TR = 1, TV = 0
         } else {
-            ep[PHY_TO_LOG(endpoint)].out[0] = CMDSTS_TR; // S=0, TR=1, TV = 0
+            ep[PHY_TO_LOG(endpoint)].out[0] = CMDSTS_TR;    // S = 0, TR = 1, TV = 0
         }
     }
 }
@@ -658,55 +669,15 @@
         // EP0IN ACK event (IN data sent)
         EP0in();
     }
-
-    if (LPC_USB->INTSTAT & EP(EP1IN)) {
-        // Clear EP1IN interrupt
-        LPC_USB->INTSTAT = EP(EP1IN);
-        epComplete |= EP(EP1IN);
-        if (EP1_IN_callback())
-            epComplete &= ~EP(EP1IN);
-    }
-
-    if (LPC_USB->INTSTAT & EP(EP1OUT)) {
-        // Clear EP1OUT interrupt
-        LPC_USB->INTSTAT = EP(EP1OUT);
-        epComplete |= EP(EP1OUT);
-        if (EP1_OUT_callback())
-            epComplete &= ~EP(EP1OUT);
-    }
-
-    if (LPC_USB->INTSTAT & EP(EP2IN)) {
-        // Clear EPBULK_IN interrupt
-        LPC_USB->INTSTAT = EP(EP2IN);
-        epComplete |= EP(EP2IN);
-        if (EP2_IN_callback())
-            epComplete &= ~EP(EP2IN);
-    }
-
-    if (LPC_USB->INTSTAT & EP(EP2OUT)) {
-        // Clear EPBULK_OUT interrupt
-        LPC_USB->INTSTAT = EP(EP2OUT);
-        epComplete |= EP(EP2OUT);
-        //Call callback function. If true, clear epComplete
-        if (EP2_OUT_callback())
-            epComplete &= ~EP(EP2OUT);
-    }
-
-    if (LPC_USB->INTSTAT & EP(EP3IN)) {
-        // Clear EP3_IN interrupt
-        LPC_USB->INTSTAT = EP(EP3IN);
-        epComplete |= EP(EP3IN);
-        if (EP3_IN_callback())
-            epComplete &= ~EP(EP3IN);
-    }
-
-    if (LPC_USB->INTSTAT & EP(EP3OUT)) {
-        // Clear EP3_OUT interrupt
-        LPC_USB->INTSTAT = EP(EP3OUT);
-        epComplete |= EP(EP3OUT);
-        //Call callback function. If true, clear epComplete
-        if (EP3_OUT_callback())
-            epComplete &= ~EP(EP3OUT);
+    
+    for (uint8_t num = 2; num < 5*2; num++) {
+        if (LPC_USB->INTSTAT & EP(num)) {
+            LPC_USB->INTSTAT = EP(num);
+            epComplete |= EP(num);
+            if ((instance->*(epCallback[num - 2]))()) {
+                epComplete &= ~EP(num);
+            }
+        }
     }
 }