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:
Mon Mar 04 13:37:51 2013 +0000
Parent:
8:335f2506f422
Child:
10:1e3d126a322b
Commit message:
kl25z: use malloc to allocate only endpoints needed

Changed in this revision

USBDevice/USBHAL_KL25Z.cpp Show annotated file Show diff for this revision Revisions of this file
--- a/USBDevice/USBHAL_KL25Z.cpp	Fri Mar 01 13:10:29 2013 +0000
+++ b/USBDevice/USBHAL_KL25Z.cpp	Mon Mar 04 13:37:51 2013 +0000
@@ -66,8 +66,8 @@
 //    * 16 bidirectionnal endpt -> 32 physical endpt
 //    * as there are ODD and EVEN buffer -> 32*2 bdt
 __attribute__((__aligned__(512))) BDT bdt[NUMBER_OF_PHYSICAL_ENDPOINTS * 2];
-uint8_t endpoint_buffer[(NUMBER_OF_PHYSICAL_ENDPOINTS - 2) * 2][64];
-uint8_t endpoint_buffer_iso[2*2][1023];
+uint8_t * endpoint_buffer[(NUMBER_OF_PHYSICAL_ENDPOINTS - 2) * 2];
+uint8_t * endpoint_buffer_iso[2*2];
 
 static uint8_t set_addr = 0;
 static uint8_t addr = 0;
@@ -199,15 +199,21 @@
 
     if ((flags & ISOCHRONOUS) == 0) {
         handshake_flag = USB_ENDPT_EPHSHK_MASK;
-        if (IN_EP(endpoint))
+        if (IN_EP(endpoint)) {
+            endpoint_buffer[EP_BDT_IDX(log_endpoint, TX, ODD )] = (uint8_t *) malloc (64*2);
             buf = &endpoint_buffer[EP_BDT_IDX(log_endpoint, TX, ODD )][0];
-        else
+        } else {
+            endpoint_buffer[EP_BDT_IDX(log_endpoint, RX, ODD )] = (uint8_t *) malloc (64*2);
             buf = &endpoint_buffer[EP_BDT_IDX(log_endpoint, RX, ODD )][0];
+        }
     } else {
-        if (IN_EP(endpoint))
+        if (IN_EP(endpoint)) {
+            endpoint_buffer_iso[2] = (uint8_t *) malloc (1023*2);
             buf = &endpoint_buffer_iso[2][0];
-        else
+        } else {
+            endpoint_buffer_iso[0] = (uint8_t *) malloc (1023*2);
             buf = &endpoint_buffer_iso[0][0];
+        }
     }
 
     // IN endpt -> device to host (TX)