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
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.
Diff: USBSerial/USBCDC.cpp
- Revision:
- 8:335f2506f422
- Parent:
- 6:d0945750af57
- Child:
- 11:eeb3cbbaa996
--- a/USBSerial/USBCDC.cpp Mon Jan 21 10:41:28 2013 +0000 +++ b/USBSerial/USBCDC.cpp Fri Mar 01 13:10:29 2013 +0000 @@ -56,8 +56,7 @@ terminal_connected = true; break; case CDC_SET_CONTROL_LINE_STATE: - if (terminal_connected) - terminal_connected = false; + terminal_connected = false; success = true; break; default: @@ -147,12 +146,13 @@ } -#define CONFIG1_DESC_SIZE (9+9+5+5+4+5+7+9+7+7) +#define CONFIG1_DESC_SIZE (9+8+9+5+5+4+5+7+9+7+7) uint8_t * USBCDC::configurationDesc() { static uint8_t configDescriptor[] = { - 9, // bLength; - 2, // bDescriptorType; + // configuration descriptor + 9, // bLength + 2, // bDescriptorType LSB(CONFIG1_DESC_SIZE), // wTotalLength MSB(CONFIG1_DESC_SIZE), 2, // bNumInterfaces @@ -160,6 +160,16 @@ 0, // iConfiguration 0x80, // bmAttributes 50, // bMaxPower + + // IAD to associate the two CDC interfaces + 0x08, // bLength + 0x0b, // bDescriptorType + 0x00, // bFirstInterface + 0x02, // bInterfaceCount + 0x02, // bFunctionClass + 0x02, // bFunctionSubClass + 0, // bFunctionProtocol + 0, // iFunction // interface descriptor, USB spec 9.6.5, page 267-269, Table 9-12 9, // bLength @@ -211,33 +221,33 @@ // interface descriptor, USB spec 9.6.5, page 267-269, Table 9-12 - 9, // bLength - 4, // bDescriptorType - 1, // bInterfaceNumber - 0, // bAlternateSetting - 2, // bNumEndpoints - 0x0A, // bInterfaceClass - 0x00, // bInterfaceSubClass - 0x00, // bInterfaceProtocol - 0, // iInterface + 9, // bLength + 4, // bDescriptorType + 1, // bInterfaceNumber + 0, // bAlternateSetting + 2, // bNumEndpoints + 0x0A, // bInterfaceClass + 0x00, // bInterfaceSubClass + 0x00, // bInterfaceProtocol + 0, // iInterface // endpoint descriptor, USB spec 9.6.6, page 269-271, Table 9-13 - 7, // bLength - 5, // bDescriptorType - PHY_TO_DESC(EPBULK_IN), // bEndpointAddress - 0x02, // bmAttributes (0x02=bulk) - LSB(MAX_PACKET_SIZE_EPBULK), // wMaxPacketSize (LSB) - MSB(MAX_PACKET_SIZE_EPBULK), // wMaxPacketSize (MSB) - 0, // bInterval + ENDPOINT_DESCRIPTOR_LENGTH, // bLength + ENDPOINT_DESCRIPTOR, // bDescriptorType + PHY_TO_DESC(EPBULK_IN), // bEndpointAddress + E_BULK, // bmAttributes (0x02=bulk) + LSB(MAX_PACKET_SIZE_EPBULK),// wMaxPacketSize (LSB) + MSB(MAX_PACKET_SIZE_EPBULK),// wMaxPacketSize (MSB) + 0, // bInterval // endpoint descriptor, USB spec 9.6.6, page 269-271, Table 9-13 - 7, // bLength - 5, // bDescriptorType - PHY_TO_DESC(EPBULK_OUT),// bEndpointAddress - 0x02, // bmAttributes (0x02=bulk) - LSB(MAX_PACKET_SIZE_EPBULK), // wMaxPacketSize (LSB) - MSB(MAX_PACKET_SIZE_EPBULK), // wMaxPacketSize (MSB) - 0 // bInterval + ENDPOINT_DESCRIPTOR_LENGTH, // bLength + ENDPOINT_DESCRIPTOR, // bDescriptorType + PHY_TO_DESC(EPBULK_OUT), // bEndpointAddress + E_BULK, // bmAttributes (0x02=bulk) + LSB(MAX_PACKET_SIZE_EPBULK),// wMaxPacketSize (LSB) + MSB(MAX_PACKET_SIZE_EPBULK),// wMaxPacketSize (MSB) + 0 // bInterval }; return configDescriptor; }