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:
1:80ab0d068708
--- a/USBAudio/USBAudio.cpp	Thu May 30 17:16:57 2013 +0100
+++ b/USBAudio/USBAudio.cpp	Mon Aug 05 14:13:36 2013 +0300
@@ -400,8 +400,8 @@
         MSB(TERMINAL_USB_STREAMING),            // wTerminalType
         0x00,                                   // bAssocTerminal
         channel_nb_in,                          // bNrChannels
-        LSB(channel_config_in),                 // wChannelConfig
-        MSB(channel_config_in),                 // wChannelConfig
+        (uint8_t)(LSB(channel_config_in)),                 // wChannelConfig
+        (uint8_t)(MSB(channel_config_in)),                 // wChannelConfig
         0x00,                                   // iChannelNames
         0x00,                                   // iTerminal
 
@@ -438,8 +438,8 @@
         MSB(TERMINAL_MICROPHONE),               // wTerminalType
         0x00,                                   // bAssocTerminal
         channel_nb_out,                         // bNrChannels
-        LSB(channel_config_out),                // wChannelConfig
-        MSB(channel_config_out),                // wChannelConfig
+        (uint8_t)(LSB(channel_config_out)),                // wChannelConfig
+        (uint8_t)(MSB(channel_config_out)),                // wChannelConfig
         0x00,                                   // iChannelNames
         0x00,                                   // iTerminal
 
@@ -499,17 +499,17 @@
         0x02,                                   // bSubFrameSize
         16,                                     // bBitResolution
         0x01,                                   // bSamFreqType
-        LSB(FREQ_IN),                           // tSamFreq
-        (FREQ_IN >> 8) & 0xff,                  // tSamFreq
-        (FREQ_IN >> 16) & 0xff,                 // tSamFreq
+        (uint8_t)(LSB(FREQ_IN)),                           // tSamFreq
+        (uint8_t)((FREQ_IN >> 8) & 0xff),                  // tSamFreq
+        (uint8_t)((FREQ_IN >> 16) & 0xff),                 // tSamFreq
 
         // Endpoint - Standard Descriptor
         ENDPOINT_DESCRIPTOR_LENGTH + 2,         // bLength
         ENDPOINT_DESCRIPTOR,                    // bDescriptorType
         PHY_TO_DESC(EPISO_OUT),                 // bEndpointAddress
         E_ISOCHRONOUS,                          // bmAttributes
-        LSB(PACKET_SIZE_ISO_IN),                   // wMaxPacketSize
-        MSB(PACKET_SIZE_ISO_IN),                   // wMaxPacketSize
+        (uint8_t)(LSB(PACKET_SIZE_ISO_IN)),                   // wMaxPacketSize
+        (uint8_t)(MSB(PACKET_SIZE_ISO_IN)),                   // wMaxPacketSize
         0x01,                                   // bInterval
         0x00,                                   // bRefresh
         0x00,                                   // bSynchAddress
@@ -569,17 +569,17 @@
         0x02,                                   // bSubFrameSize
         0x10,                                   // bBitResolution
         0x01,                                   // bSamFreqType
-        LSB(FREQ_OUT),                          // tSamFreq
-        (FREQ_OUT >> 8) & 0xff,                 // tSamFreq
-        (FREQ_OUT >> 16) & 0xff,                // tSamFreq
+        (uint8_t)(LSB(FREQ_OUT)),                          // tSamFreq
+        (uint8_t)((FREQ_OUT >> 8) & 0xff),                 // tSamFreq
+        (uint8_t)((FREQ_OUT >> 16) & 0xff),                // tSamFreq
 
         // Endpoint - Standard Descriptor
         ENDPOINT_DESCRIPTOR_LENGTH + 2,         // bLength
         ENDPOINT_DESCRIPTOR,                    // bDescriptorType
         PHY_TO_DESC(EPISO_IN),                  // bEndpointAddress
         E_ISOCHRONOUS,                          // bmAttributes
-        LSB(PACKET_SIZE_ISO_OUT),                   // wMaxPacketSize
-        MSB(PACKET_SIZE_ISO_OUT),                   // wMaxPacketSize
+        (uint8_t)(LSB(PACKET_SIZE_ISO_OUT)),                   // wMaxPacketSize
+        (uint8_t)(MSB(PACKET_SIZE_ISO_OUT)),                   // wMaxPacketSize
         0x01,                                   // bInterval
         0x00,                                   // bRefresh
         0x00,                                   // bSynchAddress