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.

Files at this revision

API Documentation at this revision

Comitter:
mbed_official
Date:
Tue Feb 18 11:00:19 2014 +0000
Parent:
17:bbd6dac92961
Child:
19:fcb63a105965
Commit message:
Synchronized with git revision 3c8f1c0c59bc1b665e9e1b1b3dc00e5fcdde6400

Full URL: https://github.com/mbedmicro/mbed/commit/3c8f1c0c59bc1b665e9e1b1b3dc00e5fcdde6400/

Allow USBDevice::connect() to be non-blocking

Changed in this revision

USBDevice/USBDevice.cpp Show annotated file Show diff for this revision Revisions of this file
USBDevice/USBDevice.h Show annotated file Show diff for this revision Revisions of this file
USBMSD/USBMSD.cpp Show annotated file Show diff for this revision Revisions of this file
USBMSD/USBMSD.h Show annotated file Show diff for this revision Revisions of this file
--- a/USBDevice/USBDevice.cpp	Thu Jan 23 17:30:20 2014 +0000
+++ b/USBDevice/USBDevice.cpp	Tue Feb 18 11:00:19 2014 +0000
@@ -703,12 +703,15 @@
     return (device.state == CONFIGURED);
 }
 
-void USBDevice::connect(void)
+void USBDevice::connect(bool blocking)
 {
     /* Connect device */
     USBHAL::connect();
-    /* Block if not configured */
-    while (!configured());
+    
+    if (blocking) {
+        /* Block if not configured */
+        while (!configured());
+    }
 }
 
 void USBDevice::disconnect(void)
--- a/USBDevice/USBDevice.h	Thu Jan 23 17:30:20 2014 +0000
+++ b/USBDevice/USBDevice.h	Tue Feb 18 11:00:19 2014 +0000
@@ -37,8 +37,10 @@
     
     /*
     * Connect a device
+    * 
+    * @param blocking: block if not configured
     */
-    void connect(void);
+    void connect(bool blocking = true);
     
     /*
     * Disconnect a device
--- a/USBMSD/USBMSD.cpp	Thu Jan 23 17:30:20 2014 +0000
+++ b/USBMSD/USBMSD.cpp	Tue Feb 18 11:00:19 2014 +0000
@@ -103,8 +103,7 @@
 }
 
 
-bool USBMSD::connect() {
-
+bool USBMSD::connect(bool blocking) {
     //disk initialization
     if (disk_status() & NO_INIT) {
         if (disk_initialize()) {
@@ -131,7 +130,7 @@
     }
 
     //connect the device
-    USBDevice::connect();
+    USBDevice::connect(blocking);
     return true;
 }
 
--- a/USBMSD/USBMSD.h	Thu Jan 23 17:30:20 2014 +0000
+++ b/USBMSD/USBMSD.h	Tue Feb 18 11:00:19 2014 +0000
@@ -70,9 +70,10 @@
     /**
     * Connect the USB MSD device. Establish disk initialization before really connect the device.
     *
+    * @param blocking if not configured
     * @returns true if successful
     */
-    bool connect();
+    bool connect(bool blocking = true);
 
     /**
     * Disconnect the USB MSD device.