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.
Revision 14:d495202c90f4, committed 2013-09-12
- Comitter:
- bogdanm
- Date:
- Thu Sep 12 14:45:27 2013 +0100
- Parent:
- 13:16731886c049
- Child:
- 15:849c0c0f2769
- Commit message:
- Synchronized with git revision b9d52bda50a692c05a4587bcc8d3219997444f58
Changed in this revision
--- a/USBDevice/USBHAL_KL25Z.cpp Tue Sep 10 15:14:55 2013 +0300
+++ b/USBDevice/USBHAL_KL25Z.cpp Thu Sep 12 14:45:27 2013 +0100
@@ -169,6 +169,16 @@
USB0->CTL &= ~USB_CTL_USBENSOFEN_MASK;
// Pull up disable
USB0->CONTROL &= ~USB_CONTROL_DPPULLUPNONOTG_MASK;
+
+ //Free buffers if required:
+ for (int i = 0; i<(NUMBER_OF_PHYSICAL_ENDPOINTS - 2) * 2; i++) {
+ free(endpoint_buffer[i]);
+ endpoint_buffer[i] = NULL;
+ }
+ free(endpoint_buffer_iso[2]);
+ endpoint_buffer_iso[2] = NULL;
+ free(endpoint_buffer_iso[0]);
+ endpoint_buffer_iso[0] = NULL;
}
void USBHAL::configureDevice(void) {
@@ -200,18 +210,22 @@
if ((flags & ISOCHRONOUS) == 0) {
handshake_flag = USB_ENDPT_EPHSHK_MASK;
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];
+ if (endpoint_buffer[EP_BDT_IDX(log_endpoint, TX, ODD)] == NULL)
+ 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 {
- 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];
+ if (endpoint_buffer[EP_BDT_IDX(log_endpoint, RX, ODD)] == NULL)
+ 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)) {
- endpoint_buffer_iso[2] = (uint8_t *) malloc (1023*2);
+ if (endpoint_buffer_iso[2] == NULL)
+ endpoint_buffer_iso[2] = (uint8_t *) malloc (1023*2);
buf = &endpoint_buffer_iso[2][0];
} else {
- endpoint_buffer_iso[0] = (uint8_t *) malloc (1023*2);
+ if (endpoint_buffer_iso[0] == NULL)
+ endpoint_buffer_iso[0] = (uint8_t *) malloc (1023*2);
buf = &endpoint_buffer_iso[0][0];
}
}
--- a/USBMSD/USBMSD.cpp Tue Sep 10 15:14:55 2013 +0300
+++ b/USBMSD/USBMSD.cpp Thu Sep 12 14:45:27 2013 +0100
@@ -67,8 +67,12 @@
stage = READ_CBW;
memset((void *)&cbw, 0, sizeof(CBW));
memset((void *)&csw, 0, sizeof(CSW));
+ page = NULL;
}
+USBMSD::~USBMSD() {
+ disconnect();
+}
// Called in ISR context to process a class specific request
@@ -117,6 +121,7 @@
if (BlockCount > 0) {
BlockSize = MemorySize / BlockCount;
if (BlockSize != 0) {
+ free(page);
page = (uint8_t *)malloc(BlockSize * sizeof(uint8_t));
if (page == NULL)
return false;
@@ -130,6 +135,12 @@
return true;
}
+void USBMSD::disconnect() {
+ //De-allocate MSD page size:
+ free(page);
+ page = NULL;
+ USBDevice::disconnect();
+}
void USBMSD::reset() {
stage = READ_CBW;
--- a/USBMSD/USBMSD.h Tue Sep 10 15:14:55 2013 +0300
+++ b/USBMSD/USBMSD.h Thu Sep 12 14:45:27 2013 +0100
@@ -74,6 +74,15 @@
*/
bool connect();
+ /**
+ * Disconnect the USB MSD device.
+ */
+ void disconnect();
+
+ /**
+ * Destructor
+ */
+ ~USBMSD();
protected:
--- a/USBSerial/CircBuffer.h Tue Sep 10 15:14:55 2013 +0300
+++ b/USBSerial/CircBuffer.h Thu Sep 12 14:45:27 2013 +0100
@@ -29,6 +29,10 @@
buf = (T *)malloc(size * sizeof(T));
};
+ ~CircBuffer() {
+ free(buf);
+ }
+
bool isFull() {
return ((write + 1) % size == read);
};
