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 Jan 21 10:41:28 2013 +0000
Parent:
6:d0945750af57
Child:
8:335f2506f422
Commit message:
modify USBMSD prototypes to fit latest sdfilesystem lib

Changed in this revision

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/USBMSD/USBMSD.cpp	Thu Dec 20 17:05:37 2012 +0000
+++ b/USBMSD/USBMSD.cpp	Mon Jan 21 10:41:28 2013 +0000
@@ -114,7 +114,7 @@
     // get memory size
     MemorySize = disk_size();
 
-    if (BlockCount >= 0) {
+    if (BlockCount > 0) {
         BlockSize = MemorySize / BlockCount;
         if (BlockSize != 0) {
             page = (uint8_t *)malloc(BlockSize * sizeof(uint8_t));
@@ -222,7 +222,7 @@
     // if the array is filled, write it in memory
     if (!((addr + size)%BlockSize)) {
         if (!(disk_status() & WRITE_PROTECT)) {
-            disk_write((const char *)page, addr/BlockSize);
+            disk_write(page, addr/BlockSize);
         }
     }
 
@@ -247,7 +247,7 @@
 
     // beginning of a new block -> load a whole block in RAM
     if (!(addr%BlockSize))
-        disk_read((char *)page, addr/BlockSize);
+        disk_read(page, addr/BlockSize);
 
     // info are in RAM -> no need to re-read memory
     for (n = 0; n < size; n++) {
@@ -495,7 +495,7 @@
 
     // we read an entire block
     if (!(addr%BlockSize))
-        disk_read((char *)page, addr/BlockSize);
+        disk_read(page, addr/BlockSize);
 
     // write data which are in RAM
     writeNB(EPBULK_IN, &page[addr%BlockSize], n, MAX_PACKET_SIZE_EPBULK);
--- a/USBMSD/USBMSD.h	Thu Dec 20 17:05:37 2012 +0000
+++ b/USBMSD/USBMSD.h	Mon Jan 21 10:41:28 2013 +0000
@@ -84,7 +84,7 @@
     * @param block block number
     * @returns 0 if successful
     */
-    virtual int disk_read(char * data, int block) = 0;
+    virtual int disk_read(uint8_t * data, uint64_t block) = 0;
 
     /*
     * write a block on a storage chip
@@ -93,7 +93,7 @@
     * @param block block number
     * @returns 0 if successful
     */
-    virtual int disk_write(const char * data, int block) = 0;
+    virtual int disk_write(const uint8_t * data, uint64_t block) = 0;
 
     /*
     * Disk initilization
@@ -105,14 +105,14 @@
     *
     * @returns number of blocks
     */
-    virtual int disk_sectors() = 0;
+    virtual uint64_t disk_sectors() = 0;
 
     /*
     * Return memory size
     *
     * @returns memory size
     */
-    virtual int disk_size() = 0;
+    virtual uint64_t disk_size() = 0;
 
 
     /*
@@ -216,8 +216,8 @@
     uint8_t * page;
 
     int BlockSize;
-    int MemorySize;
-    int BlockCount;
+    uint64_t MemorySize;
+    uint64_t BlockCount;
 
     void CBWDecode(uint8_t * buf, uint16_t size);
     void sendCSW (void);