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:
11:eeb3cbbaa996
Parent:
8:335f2506f422
Child:
17:bbd6dac92961
--- a/USBDevice/USBHAL_LPC11U.cpp	Thu May 30 17:16:57 2013 +0100
+++ b/USBDevice/USBHAL_LPC11U.cpp	Mon Aug 05 14:13:36 2013 +0300
@@ -16,7 +16,13 @@
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
 */
 
-#ifdef TARGET_LPC11U24
+#if defined(TARGET_LPC11U24) || defined(TARGET_LPC1347)
+
+#if defined(TARGET_LPC1347)
+#define USB_IRQ USB_IRQ_IRQn
+#elif defined(TARGET_LPC11U24)
+#define USB_IRQ USB_IRQn
+#endif
 
 #include "USBHAL.h"
 
@@ -81,22 +87,22 @@
 // One entry for a double-buffered logical endpoint in the endpoint
 // command/status list. Endpoint 0 is single buffered, out[1] is used
 // for the SETUP packet and in[1] is not used
-typedef __packed struct {
+typedef struct {
     uint32_t out[2];
     uint32_t in[2];
-} EP_COMMAND_STATUS;
+} PACKED EP_COMMAND_STATUS;
 
-typedef __packed struct {
+typedef struct {
     uint8_t out[MAX_PACKET_SIZE_EP0];
     uint8_t in[MAX_PACKET_SIZE_EP0];
     uint8_t setup[SETUP_PACKET_SIZE];
-} CONTROL_TRANSFER;
+} PACKED CONTROL_TRANSFER;
 
-typedef __packed struct {
+typedef struct {
     uint32_t    maxPacket;
     uint32_t    buffer[2];
     uint32_t    options;
-} EP_STATE;
+} PACKED EP_STATE;
 
 static volatile EP_STATE endpointState[NUMBER_OF_PHYSICAL_ENDPOINTS];
 
@@ -127,7 +133,7 @@
 
 
 USBHAL::USBHAL(void) {
-    NVIC_DisableIRQ(USB_IRQn);
+    NVIC_DisableIRQ(USB_IRQ);
     
     // fill in callback array
     epCallback[0] = &USBHAL::EP1_OUT_callback;
@@ -184,24 +190,24 @@
     instance = this;
 
     //attach IRQ handler and enable interrupts
-    NVIC_SetVector(USB_IRQn, (uint32_t)&_usbisr);
+    NVIC_SetVector(USB_IRQ, (uint32_t)&_usbisr);
 }
 
 USBHAL::~USBHAL(void) {
     // Ensure device disconnected (DCON not set)
     LPC_USB->DEVCMDSTAT = 0;
     // Disable USB interrupts
-    NVIC_DisableIRQ(USB_IRQn);
+    NVIC_DisableIRQ(USB_IRQ);
 }
 
 void USBHAL::connect(void) {
-    NVIC_EnableIRQ(USB_IRQn);
+    NVIC_EnableIRQ(USB_IRQ);
     devCmdStat |= DCON;
     LPC_USB->DEVCMDSTAT = devCmdStat;
 }
 
 void USBHAL::disconnect(void) {
-    NVIC_DisableIRQ(USB_IRQn);
+    NVIC_DisableIRQ(USB_IRQ);
     devCmdStat &= ~DCON;
     LPC_USB->DEVCMDSTAT = devCmdStat;
 }