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:
bogdanm
Date:
Mon Aug 05 14:13:36 2013 +0300
Parent:
10:1e3d126a322b
Child:
12:6030a12b6c62
Commit message:
Bug fixes, added suppor for LPC1347

Author: Samuel Mokrani

Changed in this revision

USBAudio/USBAudio.cpp Show annotated file Show diff for this revision Revisions of this file
USBDevice/USBDevice.cpp Show annotated file Show diff for this revision Revisions of this file
USBDevice/USBEndpoints.h Show annotated file Show diff for this revision Revisions of this file
USBDevice/USBHAL.h Show annotated file Show diff for this revision Revisions of this file
USBDevice/USBHAL_LPC11U.cpp Show annotated file Show diff for this revision Revisions of this file
USBHID/USBHID.cpp Show annotated file Show diff for this revision Revisions of this file
USBHID/USBKeyboard.cpp Show annotated file Show diff for this revision Revisions of this file
USBHID/USBMouse.cpp Show annotated file Show diff for this revision Revisions of this file
USBMIDI/MIDIMessage.h Show annotated file Show diff for this revision Revisions of this file
USBMIDI/USBMIDI.cpp 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
USBSerial/USBCDC.cpp 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/USBAudio/USBAudio.cpp	Thu May 30 17:16:57 2013 +0100
+++ b/USBAudio/USBAudio.cpp	Mon Aug 05 14:13:36 2013 +0300
@@ -400,8 +400,8 @@
         MSB(TERMINAL_USB_STREAMING),            // wTerminalType
         0x00,                                   // bAssocTerminal
         channel_nb_in,                          // bNrChannels
-        LSB(channel_config_in),                 // wChannelConfig
-        MSB(channel_config_in),                 // wChannelConfig
+        (uint8_t)(LSB(channel_config_in)),                 // wChannelConfig
+        (uint8_t)(MSB(channel_config_in)),                 // wChannelConfig
         0x00,                                   // iChannelNames
         0x00,                                   // iTerminal
 
@@ -438,8 +438,8 @@
         MSB(TERMINAL_MICROPHONE),               // wTerminalType
         0x00,                                   // bAssocTerminal
         channel_nb_out,                         // bNrChannels
-        LSB(channel_config_out),                // wChannelConfig
-        MSB(channel_config_out),                // wChannelConfig
+        (uint8_t)(LSB(channel_config_out)),                // wChannelConfig
+        (uint8_t)(MSB(channel_config_out)),                // wChannelConfig
         0x00,                                   // iChannelNames
         0x00,                                   // iTerminal
 
@@ -499,17 +499,17 @@
         0x02,                                   // bSubFrameSize
         16,                                     // bBitResolution
         0x01,                                   // bSamFreqType
-        LSB(FREQ_IN),                           // tSamFreq
-        (FREQ_IN >> 8) & 0xff,                  // tSamFreq
-        (FREQ_IN >> 16) & 0xff,                 // tSamFreq
+        (uint8_t)(LSB(FREQ_IN)),                           // tSamFreq
+        (uint8_t)((FREQ_IN >> 8) & 0xff),                  // tSamFreq
+        (uint8_t)((FREQ_IN >> 16) & 0xff),                 // tSamFreq
 
         // Endpoint - Standard Descriptor
         ENDPOINT_DESCRIPTOR_LENGTH + 2,         // bLength
         ENDPOINT_DESCRIPTOR,                    // bDescriptorType
         PHY_TO_DESC(EPISO_OUT),                 // bEndpointAddress
         E_ISOCHRONOUS,                          // bmAttributes
-        LSB(PACKET_SIZE_ISO_IN),                   // wMaxPacketSize
-        MSB(PACKET_SIZE_ISO_IN),                   // wMaxPacketSize
+        (uint8_t)(LSB(PACKET_SIZE_ISO_IN)),                   // wMaxPacketSize
+        (uint8_t)(MSB(PACKET_SIZE_ISO_IN)),                   // wMaxPacketSize
         0x01,                                   // bInterval
         0x00,                                   // bRefresh
         0x00,                                   // bSynchAddress
@@ -569,17 +569,17 @@
         0x02,                                   // bSubFrameSize
         0x10,                                   // bBitResolution
         0x01,                                   // bSamFreqType
-        LSB(FREQ_OUT),                          // tSamFreq
-        (FREQ_OUT >> 8) & 0xff,                 // tSamFreq
-        (FREQ_OUT >> 16) & 0xff,                // tSamFreq
+        (uint8_t)(LSB(FREQ_OUT)),                          // tSamFreq
+        (uint8_t)((FREQ_OUT >> 8) & 0xff),                 // tSamFreq
+        (uint8_t)((FREQ_OUT >> 16) & 0xff),                // tSamFreq
 
         // Endpoint - Standard Descriptor
         ENDPOINT_DESCRIPTOR_LENGTH + 2,         // bLength
         ENDPOINT_DESCRIPTOR,                    // bDescriptorType
         PHY_TO_DESC(EPISO_IN),                  // bEndpointAddress
         E_ISOCHRONOUS,                          // bmAttributes
-        LSB(PACKET_SIZE_ISO_OUT),                   // wMaxPacketSize
-        MSB(PACKET_SIZE_ISO_OUT),                   // wMaxPacketSize
+        (uint8_t)(LSB(PACKET_SIZE_ISO_OUT)),                   // wMaxPacketSize
+        (uint8_t)(MSB(PACKET_SIZE_ISO_OUT)),                   // wMaxPacketSize
         0x01,                                   // bInterval
         0x00,                                   // bRefresh
         0x00,                                   // bSynchAddress
--- a/USBDevice/USBDevice.cpp	Thu May 30 17:16:57 2013 +0100
+++ b/USBDevice/USBDevice.cpp	Mon Aug 05 14:13:36 2013 +0300
@@ -758,7 +758,7 @@
     if (wTotalLength <= (CONFIGURATION_DESCRIPTOR_LENGTH+2))
     /* +2 is for bLength and bDescriptorType of next descriptor */
     {
-        return false;
+        return NULL;
     }
 
     /* Start at first descriptor after the configuration descriptor */
@@ -908,12 +908,12 @@
         0x00,                           /* bDeviceSubClass */
         0x00,                           /* bDeviceprotocol */
         MAX_PACKET_SIZE_EP0,            /* bMaxPacketSize0 */
-        LSB(VENDOR_ID),                 /* idVendor (LSB) */
-        MSB(VENDOR_ID),                 /* idVendor (MSB) */
-        LSB(PRODUCT_ID),                /* idProduct (LSB) */
-        MSB(PRODUCT_ID),                /* idProduct (MSB) */
-        LSB(PRODUCT_RELEASE),           /* bcdDevice (LSB) */
-        MSB(PRODUCT_RELEASE),           /* bcdDevice (MSB) */
+        (uint8_t)(LSB(VENDOR_ID)),                 /* idVendor (LSB) */
+        (uint8_t)(MSB(VENDOR_ID)),                 /* idVendor (MSB) */
+        (uint8_t)(LSB(PRODUCT_ID)),                /* idProduct (LSB) */
+        (uint8_t)(MSB(PRODUCT_ID)),                /* idProduct (MSB) */
+        (uint8_t)(LSB(PRODUCT_RELEASE)),           /* bcdDevice (LSB) */
+        (uint8_t)(MSB(PRODUCT_RELEASE)),           /* bcdDevice (MSB) */
         STRING_OFFSET_IMANUFACTURER,    /* iManufacturer */
         STRING_OFFSET_IPRODUCT,         /* iProduct */
         STRING_OFFSET_ISERIAL,          /* iSerialNumber */
--- a/USBDevice/USBEndpoints.h	Thu May 30 17:16:57 2013 +0100
+++ b/USBDevice/USBEndpoints.h	Mon Aug 05 14:13:36 2013 +0300
@@ -39,7 +39,7 @@
 /* Include configuration for specific target */
 #if defined(TARGET_LPC1768) || defined(TARGET_LPC2368) || defined(TARGET_LPC4088)
 #include "USBEndpoints_LPC17_LPC23.h"
-#elif defined(TARGET_LPC11U24)
+#elif defined(TARGET_LPC11U24) || defined(TARGET_LPC1347)
 #include "USBEndpoints_LPC11U.h"
 #elif defined(TARGET_KL25Z)
 #include "USBEndpoints_KL25Z.h"
--- a/USBDevice/USBHAL.h	Thu May 30 17:16:57 2013 +0100
+++ b/USBDevice/USBHAL.h	Mon Aug 05 14:13:36 2013 +0300
@@ -21,10 +21,11 @@
 
 #include "mbed.h"
 #include "USBEndpoints.h"
+#include "toolchain.h"
 
-#ifdef __GNUC__
-#define __packed __attribute__ ((__packed__))
-#endif
+//#ifdef __GNUC__
+//#define __packed __attribute__ ((__packed__))
+//#endif
 
 class USBHAL {
 public:
--- a/USBDevice/USBHAL_LPC11U.cpp	Thu May 30 17:16:57 2013 +0100
+++ b/USBDevice/USBHAL_LPC11U.cpp	Mon Aug 05 14:13:36 2013 +0300
@@ -16,7 +16,13 @@
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
 */
 
-#ifdef TARGET_LPC11U24
+#if defined(TARGET_LPC11U24) || defined(TARGET_LPC1347)
+
+#if defined(TARGET_LPC1347)
+#define USB_IRQ USB_IRQ_IRQn
+#elif defined(TARGET_LPC11U24)
+#define USB_IRQ USB_IRQn
+#endif
 
 #include "USBHAL.h"
 
@@ -81,22 +87,22 @@
 // One entry for a double-buffered logical endpoint in the endpoint
 // command/status list. Endpoint 0 is single buffered, out[1] is used
 // for the SETUP packet and in[1] is not used
-typedef __packed struct {
+typedef struct {
     uint32_t out[2];
     uint32_t in[2];
-} EP_COMMAND_STATUS;
+} PACKED EP_COMMAND_STATUS;
 
-typedef __packed struct {
+typedef struct {
     uint8_t out[MAX_PACKET_SIZE_EP0];
     uint8_t in[MAX_PACKET_SIZE_EP0];
     uint8_t setup[SETUP_PACKET_SIZE];
-} CONTROL_TRANSFER;
+} PACKED CONTROL_TRANSFER;
 
-typedef __packed struct {
+typedef struct {
     uint32_t    maxPacket;
     uint32_t    buffer[2];
     uint32_t    options;
-} EP_STATE;
+} PACKED EP_STATE;
 
 static volatile EP_STATE endpointState[NUMBER_OF_PHYSICAL_ENDPOINTS];
 
@@ -127,7 +133,7 @@
 
 
 USBHAL::USBHAL(void) {
-    NVIC_DisableIRQ(USB_IRQn);
+    NVIC_DisableIRQ(USB_IRQ);
     
     // fill in callback array
     epCallback[0] = &USBHAL::EP1_OUT_callback;
@@ -184,24 +190,24 @@
     instance = this;
 
     //attach IRQ handler and enable interrupts
-    NVIC_SetVector(USB_IRQn, (uint32_t)&_usbisr);
+    NVIC_SetVector(USB_IRQ, (uint32_t)&_usbisr);
 }
 
 USBHAL::~USBHAL(void) {
     // Ensure device disconnected (DCON not set)
     LPC_USB->DEVCMDSTAT = 0;
     // Disable USB interrupts
-    NVIC_DisableIRQ(USB_IRQn);
+    NVIC_DisableIRQ(USB_IRQ);
 }
 
 void USBHAL::connect(void) {
-    NVIC_EnableIRQ(USB_IRQn);
+    NVIC_EnableIRQ(USB_IRQ);
     devCmdStat |= DCON;
     LPC_USB->DEVCMDSTAT = devCmdStat;
 }
 
 void USBHAL::disconnect(void) {
-    NVIC_DisableIRQ(USB_IRQn);
+    NVIC_DisableIRQ(USB_IRQ);
     devCmdStat &= ~DCON;
     LPC_USB->DEVCMDSTAT = devCmdStat;
 }
--- a/USBHID/USBHID.cpp	Thu May 30 17:16:57 2013 +0100
+++ b/USBHID/USBHID.cpp	Mon Aug 05 14:13:36 2013 +0300
@@ -250,8 +250,8 @@
         0x00,                           // bCountryCode
         0x01,                           // bNumDescriptors
         REPORT_DESCRIPTOR,              // bDescriptorType
-        LSB(this->reportDescLength()),  // wDescriptorLength (LSB)
-        MSB(this->reportDescLength()),  // wDescriptorLength (MSB)
+        (uint8_t)(LSB(this->reportDescLength())),  // wDescriptorLength (LSB)
+        (uint8_t)(MSB(this->reportDescLength())),  // wDescriptorLength (MSB)
 
         ENDPOINT_DESCRIPTOR_LENGTH,     // bLength
         ENDPOINT_DESCRIPTOR,            // bDescriptorType
--- a/USBHID/USBKeyboard.cpp	Thu May 30 17:16:57 2013 +0100
+++ b/USBHID/USBKeyboard.cpp	Mon Aug 05 14:13:36 2013 +0300
@@ -530,8 +530,8 @@
         0x00,                           // bCountryCode
         0x01,                           // bNumDescriptors
         REPORT_DESCRIPTOR,              // bDescriptorType
-        LSB(reportDescLength()),  // wDescriptorLength (LSB)
-        MSB(reportDescLength()),  // wDescriptorLength (MSB)
+        (uint8_t)(LSB(reportDescLength())),  // wDescriptorLength (LSB)
+        (uint8_t)(MSB(reportDescLength())),  // wDescriptorLength (MSB)
 
         ENDPOINT_DESCRIPTOR_LENGTH,     // bLength
         ENDPOINT_DESCRIPTOR,            // bDescriptorType
--- a/USBHID/USBMouse.cpp	Thu May 30 17:16:57 2013 +0100
+++ b/USBHID/USBMouse.cpp	Mon Aug 05 14:13:36 2013 +0300
@@ -222,8 +222,8 @@
         0x00,                           // bCountryCode
         0x01,                           // bNumDescriptors
         REPORT_DESCRIPTOR,              // bDescriptorType
-        LSB(reportDescLength()),        // wDescriptorLength (LSB)
-        MSB(reportDescLength()),        // wDescriptorLength (MSB)
+        (uint8_t)(LSB(reportDescLength())),        // wDescriptorLength (LSB)
+        (uint8_t)(MSB(reportDescLength())),        // wDescriptorLength (MSB)
 
         ENDPOINT_DESCRIPTOR_LENGTH,     // bLength
         ENDPOINT_DESCRIPTOR,            // bDescriptorType
--- a/USBMIDI/MIDIMessage.h	Thu May 30 17:16:57 2013 +0100
+++ b/USBMIDI/MIDIMessage.h	Mon Aug 05 14:13:36 2013 +0300
@@ -45,7 +45,8 @@
     MIDIMessage() {}
     
     MIDIMessage(uint8_t *buf) {
-        *((uint32_t *)data) = *((uint32_t *)buf);
+        for (int i = 0; i < 4; i++)
+            data[i] = buf[i];
     }
     
     // create messages
@@ -245,6 +246,6 @@
     }
     
     uint8_t data[4];
-};    
+};
 
 #endif
--- a/USBMIDI/USBMIDI.cpp	Thu May 30 17:16:57 2013 +0100
+++ b/USBMIDI/USBMIDI.cpp	Mon Aug 05 14:13:36 2013 +0300
@@ -41,7 +41,7 @@
     readEP(EPBULK_OUT, buf, &len, 64);
 
     if (midi_evt != NULL) {
-        for (int i=0; i<len; i+=4) {
+        for (uint32_t i=0; i<len; i+=4) {
             midi_evt(MIDIMessage(buf+i));
         }
     }
--- a/USBMSD/USBMSD.cpp	Thu May 30 17:16:57 2013 +0100
+++ b/USBMSD/USBMSD.cpp	Mon Aug 05 14:13:36 2013 +0300
@@ -192,16 +192,16 @@
             sendCSW();
             break;
 
-            // an error has occured
-        case ERROR:
+        // the host has received the CSW -> we wait a CBW
+        case WAIT_CSW:
+            stage = READ_CBW;
+            break;
+
+        // an error has occured
+        default:
             stallEndpoint(EPBULK_IN);
             sendCSW();
             break;
-
-            // the host has received the CSW -> we wait a CBW
-        case WAIT_CSW:
-            stage = READ_CBW;
-            break;
     }
     return true;
 }
@@ -284,15 +284,15 @@
 
 bool USBMSD::readFormatCapacity() {
     uint8_t capacity[] = { 0x00, 0x00, 0x00, 0x08,
-                           (BlockCount >> 24) & 0xff,
-                           (BlockCount >> 16) & 0xff,
-                           (BlockCount >> 8) & 0xff,
-                           (BlockCount >> 0) & 0xff,
+                           (uint8_t)((BlockCount >> 24) & 0xff),
+                           (uint8_t)((BlockCount >> 16) & 0xff),
+                           (uint8_t)((BlockCount >> 8) & 0xff),
+                           (uint8_t)((BlockCount >> 0) & 0xff),
 
                            0x02,
-                           (BlockSize >> 16) & 0xff,
-                           (BlockSize >> 8) & 0xff,
-                           (BlockSize >> 0) & 0xff,
+                           (uint8_t)((BlockSize >> 16) & 0xff),
+                           (uint8_t)((BlockSize >> 8) & 0xff),
+                           (uint8_t)((BlockSize >> 0) & 0xff),
                          };
     if (!write(capacity, sizeof(capacity))) {
         return false;
@@ -303,15 +303,15 @@
 
 bool USBMSD::readCapacity (void) {
     uint8_t capacity[] = {
-        ((BlockCount - 1) >> 24) & 0xff,
-        ((BlockCount - 1) >> 16) & 0xff,
-        ((BlockCount - 1) >> 8) & 0xff,
-        ((BlockCount - 1) >> 0) & 0xff,
+        (uint8_t)(((BlockCount - 1) >> 24) & 0xff),
+        (uint8_t)(((BlockCount - 1) >> 16) & 0xff),
+        (uint8_t)(((BlockCount - 1) >> 8) & 0xff),
+        (uint8_t)(((BlockCount - 1) >> 0) & 0xff),
 
-        (BlockSize >> 24) & 0xff,
-        (BlockSize >> 16) & 0xff,
-        (BlockSize >> 8) & 0xff,
-        (BlockSize >> 0) & 0xff,
+        (uint8_t)((BlockSize >> 24) & 0xff),
+        (uint8_t)((BlockSize >> 16) & 0xff),
+        (uint8_t)((BlockSize >> 8) & 0xff),
+        (uint8_t)((BlockSize >> 0) & 0xff),
     };
     if (!write(capacity, sizeof(capacity))) {
         return false;
--- a/USBMSD/USBMSD.h	Thu May 30 17:16:57 2013 +0100
+++ b/USBMSD/USBMSD.h	Mon Aug 05 14:13:36 2013 +0300
@@ -184,7 +184,7 @@
         uint8_t  LUN;
         uint8_t  CBLength;
         uint8_t  CB[16];
-    } __packed CBW;
+    } PACKED CBW;
 
     // Bulk-only CSW
     typedef struct {
@@ -192,7 +192,7 @@
         uint32_t Tag;
         uint32_t DataResidue;
         uint8_t  Status;
-    } __packed CSW;
+    } PACKED CSW;
 
     //state of the bulk-only state machine
     Stage stage;
--- a/USBSerial/USBCDC.cpp	Thu May 30 17:16:57 2013 +0100
+++ b/USBSerial/USBCDC.cpp	Mon Aug 05 14:13:36 2013 +0300
@@ -116,8 +116,8 @@
         0,                    // bDeviceSubClass
         0,                    // bDeviceProtocol
         MAX_PACKET_SIZE_EP0,  // bMaxPacketSize0
-        LSB(VENDOR_ID), MSB(VENDOR_ID),  // idVendor
-        LSB(PRODUCT_ID), MSB(PRODUCT_ID),// idProduct
+        (uint8_t)(LSB(VENDOR_ID)), (uint8_t)(MSB(VENDOR_ID)),  // idVendor
+        (uint8_t)(LSB(PRODUCT_ID)), (uint8_t)(MSB(PRODUCT_ID)),// idProduct
         0x00, 0x01,           // bcdDevice
         1,                    // iManufacturer
         2,                    // iProduct
--- a/USBSerial/USBSerial.cpp	Thu May 30 17:16:57 2013 +0100
+++ b/USBSerial/USBSerial.cpp	Mon Aug 05 14:13:36 2013 +0300
@@ -27,7 +27,7 @@
 }
 
 int USBSerial::_getc() {
-    uint8_t c;
+    uint8_t c = 0;
     while (buf.isEmpty());
     buf.dequeue(&c);
     return c;
@@ -52,7 +52,7 @@
 
     //we read the packet received and put it on the circular buffer
     readEP(c, &size);
-    for (int i = 0; i < size; i++) {
+    for (uint32_t i = 0; i < size; i++) {
         buf.queue(c[i]);
     }