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:
samux
Date:
Thu Dec 20 17:05:37 2012 +0000
Parent:
5:d27e4c226965
Child:
7:f8f057664123
Commit message:
USBSerial: detect when a terminal is connected

Changed in this revision

USBSerial/USBCDC.cpp Show annotated file Show diff for this revision Revisions of this file
USBSerial/USBCDC.h Show annotated file Show diff for this revision Revisions of this file
USBSerial/USBSerial.cpp Show annotated file Show diff for this revision Revisions of this file
--- a/USBSerial/USBCDC.cpp	Sun Oct 14 15:08:52 2012 +0000
+++ b/USBSerial/USBCDC.cpp	Thu Dec 20 17:05:37 2012 +0000
@@ -30,6 +30,7 @@
 #define MAX_CDC_REPORT_SIZE MAX_PACKET_SIZE_EPBULK
 
 USBCDC::USBCDC(uint16_t vendor_id, uint16_t product_id, uint16_t product_release): USBDevice(vendor_id, product_id, product_release) {
+    terminal_connected = false;
     USBDevice::connect();
 }
 
@@ -52,8 +53,11 @@
             case CDC_SET_LINE_CODING:
                 transfer->remaining = 7;
                 success = true;
+                terminal_connected = true;
                 break;
             case CDC_SET_CONTROL_LINE_STATE:
+                if (terminal_connected)
+                    terminal_connected = false;
                 success = true;
                 break;
             default:
--- a/USBSerial/USBCDC.h	Sun Oct 14 15:08:52 2012 +0000
+++ b/USBSerial/USBCDC.h	Thu Dec 20 17:05:37 2012 +0000
@@ -99,9 +99,11 @@
     * @returns true if successful
     */
     bool readEP_NB(uint8_t * buffer, uint32_t * size);
-
+    
+protected:
     virtual bool USBCallback_request();
     virtual bool USBCallback_setConfiguration(uint8_t configuration);
+    volatile bool terminal_connected;
 
 };
 
--- a/USBSerial/USBSerial.cpp	Sun Oct 14 15:08:52 2012 +0000
+++ b/USBSerial/USBSerial.cpp	Thu Dec 20 17:05:37 2012 +0000
@@ -20,6 +20,8 @@
 #include "USBSerial.h"
 
 int USBSerial::_putc(int c) {
+    if (!terminal_connected)
+        return 0;
     send((uint8_t *)&c, 1);
     return 1;
 }