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:
emilmont
Date:
Thu Aug 08 15:56:46 2013 +0100
Parent:
11:eeb3cbbaa996
Child:
13:16731886c049
Commit message:
Synch with latest https://github.com/mbedmicro/mbed changes

Changed in this revision

USBDevice/USBHAL_LPC40.cpp Show annotated file Show diff for this revision Revisions of this file
--- a/USBDevice/USBHAL_LPC40.cpp	Mon Aug 05 14:13:36 2013 +0300
+++ b/USBDevice/USBHAL_LPC40.cpp	Thu Aug 08 15:56:46 2013 +0100
@@ -32,8 +32,9 @@
 #define PCUSB      (1UL<<31)
 
 // USB Clock Control register
-#define DEV_CLK_EN (1UL<<1)
-#define AHB_CLK_EN (1UL<<4)
+#define DEV_CLK_EN  (1UL<<1)
+#define PORT_CLK_EN (1UL<<3)
+#define AHB_CLK_EN  (1UL<<4)
 
 // USB Clock Status register
 #define DEV_CLK_ON (1UL<<1)
@@ -364,21 +365,23 @@
     LPC_SC->PCONP |= PCUSB;
 
     // Enable USB clocks
-    LPC_USB->USBClkCtrl |= DEV_CLK_EN | AHB_CLK_EN;
-    while ((LPC_USB->USBClkSt & (DEV_CLK_EN | AHB_CLK_EN)) != (DEV_CLK_ON | AHB_CLK_ON));
+    LPC_USB->USBClkCtrl |= DEV_CLK_EN | AHB_CLK_EN | PORT_CLK_EN;
+    while ((LPC_USB->USBClkSt & (DEV_CLK_EN | AHB_CLK_EN | PORT_CLK_EN)) != (DEV_CLK_ON | AHB_CLK_ON | PORT_CLK_EN));
+    
+    // Select port USB2
+    LPC_USB->StCtrl |= 3;
 
-    // Configure pins P0.29 and P0.30 to be USB D+ and USB D-
-    LPC_IOCON->P0_29 &= ~0x07;
-    LPC_IOCON->P0_29 |= 0x01;
-    LPC_IOCON->P0_30 &= ~0x07;
-    LPC_IOCON->P0_30 |= 0x01;    
+
+    // Configure pin P0.31 to be USB2
+    LPC_IOCON->P0_31 &= ~0x07;
+    LPC_IOCON->P0_31 |= 0x01;
     
     // Disconnect USB device
     SIEdisconnect();
 
-    // Configure pin P2.9 to be Connect
-    LPC_IOCON->P2_9 &= ~0x07;
-    LPC_IOCON->P2_9 |= 0x01;    
+    // Configure pin P0.14 to be Connect
+    LPC_IOCON->P0_14 &= ~0x07;
+    LPC_IOCON->P0_14 |= 0x03;    
 
     // Connect must be low for at least 2.5uS
     wait(0.3);