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 20:d38b72fed893, committed 2014-02-27
- Comitter:
- mbed_official
- Date:
- Thu Feb 27 09:45:46 2014 +0000
- Parent:
- 19:fcb63a105965
- Child:
- 21:ee9f2fa6bdbc
- Commit message:
- Synchronized with git revision 6b57b5237463a65d25f0ffa5efa3c8505553da4b
Full URL: https://github.com/mbedmicro/mbed/commit/6b57b5237463a65d25f0ffa5efa3c8505553da4b/
add D0-D13 and A0-A5 for Arch V1.1 and Arch Pro
Changed in this revision
USBDevice/USBEndpoints.h | Show annotated file Show diff for this revision Revisions of this file |
USBDevice/USBHAL_KL25Z.cpp | Show annotated file Show diff for this revision Revisions of this file |
--- a/USBDevice/USBEndpoints.h Thu Feb 20 11:15:58 2014 +0000 +++ b/USBDevice/USBEndpoints.h Thu Feb 27 09:45:46 2014 +0000 @@ -41,7 +41,7 @@ #include "USBEndpoints_LPC17_LPC23.h" #elif defined(TARGET_LPC11UXX) || defined(TARGET_LPC1347) #include "USBEndpoints_LPC11U.h" -#elif defined(TARGET_KL25Z) +#elif defined(TARGET_KL25Z) | defined(TARGET_KL46Z) | defined(TARGET_K20D5M) #include "USBEndpoints_KL25Z.h" #elif defined (TARGET_STM32F4XX) #include "USBEndpoints_STM32F4.h"
--- a/USBDevice/USBHAL_KL25Z.cpp Thu Feb 20 11:15:58 2014 +0000 +++ b/USBDevice/USBHAL_KL25Z.cpp Thu Feb 27 09:45:46 2014 +0000 @@ -16,7 +16,7 @@ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ -#if defined(TARGET_KL25Z) +#if defined(TARGET_KL25Z) | defined(TARGET_KL46Z) | defined(TARGET_K20D5M) #include "USBHAL.h" @@ -59,7 +59,7 @@ uint8_t dummy; // RSVD: BD[8:15] uint16_t byte_count; // BD[16:32] uint32_t address; // Addr -} BDT; +} BDT; // there are: @@ -82,10 +82,10 @@ return 0; } -USBHAL::USBHAL(void) { +USBHAL::USBHAL(void) { // Disable IRQ NVIC_DisableIRQ(USB0_IRQn); - + // fill in callback array epCallback[0] = &USBHAL::EP1_OUT_callback; epCallback[1] = &USBHAL::EP1_IN_callback; @@ -117,11 +117,11 @@ epCallback[27] = &USBHAL::EP14_IN_callback; epCallback[28] = &USBHAL::EP15_OUT_callback; epCallback[29] = &USBHAL::EP15_IN_callback; - - + + // choose usb src as PLL SIM->SOPT2 |= (SIM_SOPT2_USBSRC_MASK | SIM_SOPT2_PLLFLLSEL_MASK); - + // enable OTG clock SIM->SCGC4 |= SIM_SCGC4_USBOTG_MASK; @@ -129,12 +129,12 @@ instance = this; NVIC_SetVector(USB0_IRQn, (uint32_t)&_usbisr); NVIC_EnableIRQ(USB0_IRQn); - + // USB Module Configuration // Reset USB Module USB0->USBTRC0 |= USB_USBTRC0_USBRESET_MASK; while(USB0->USBTRC0 & USB_USBTRC0_USBRESET_MASK); - + // Set BDT Base Register USB0->BDTPAGE1=(uint8_t)((uint32_t)bdt>>8); USB0->BDTPAGE2=(uint8_t)((uint32_t)bdt>>16); @@ -144,14 +144,14 @@ USB0->ISTAT = 0xff; // USB Interrupt Enablers - USB0->INTEN |= USB_INTEN_TOKDNEEN_MASK | - USB_INTEN_SOFTOKEN_MASK | + USB0->INTEN |= USB_INTEN_TOKDNEEN_MASK | + USB_INTEN_SOFTOKEN_MASK | USB_INTEN_ERROREN_MASK | USB_INTEN_USBRSTEN_MASK; - - // Disable weak pull downs - USB0->USBCTRL &= ~(USB_USBCTRL_PDE_MASK | USB_USBCTRL_SUSP_MASK); - + + // Disable weak pull downs + USB0->USBCTRL &= ~(USB_USBCTRL_PDE_MASK | USB_USBCTRL_SUSP_MASK); + USB0->USBTRC0 |= 0x40; } @@ -296,9 +296,9 @@ uint32_t n, sz, idx, setup = 0; uint8_t not_iso; uint8_t * ep_buf; - + uint32_t log_endpoint = PHY_TO_LOG(endpoint); - + if (endpoint > NUMBER_OF_PHYSICAL_ENDPOINTS - 1) { return EP_INVALID; } @@ -335,7 +335,7 @@ if (((Data1 >> endpoint) & 1) == ((bdt[idx].info >> 6) & 1)) { if (setup && (buffer[6] == 0)) // if no setup data stage, Data1 &= ~1UL; // set DATA0 - else + else Data1 ^= (1 << endpoint); } @@ -345,7 +345,7 @@ else { bdt[idx].info = BD_DTS_MASK | BD_OWN_MASK; } - + USB0->CTL &= ~USB_CTL_TXSUSPENDTOKENBUSY_MASK; *bytesRead = sz; @@ -368,27 +368,27 @@ idx = EP_BDT_IDX(PHY_TO_LOG(endpoint), TX, 0); bdt[idx].byte_count = size; - - + + // non iso endpoint if (USB0->ENDPOINT[PHY_TO_LOG(endpoint)].ENDPT & USB_ENDPT_EPHSHK_MASK) { ep_buf = endpoint_buffer[idx]; } else { ep_buf = endpoint_buffer_iso[2]; } - + for (n = 0; n < size; n++) { ep_buf[n] = data[n]; } - + if ((Data1 >> endpoint) & 1) { bdt[idx].info = BD_OWN_MASK | BD_DTS_MASK; } else { bdt[idx].info = BD_OWN_MASK | BD_DTS_MASK | BD_DATA01_MASK; } - + Data1 ^= (1 << endpoint); - + return EP_PENDING; } @@ -429,7 +429,7 @@ uint8_t istat = USB0->ISTAT; // reset interrupt - if (istat & USB_ISTAT_USBRST_MASK) { + if (istat & USB_ISTAT_USBRST_MASK) { // disable all endpt for(i = 0; i < 16; i++) { USB0->ENDPOINT[i].ENDPT = 0x00; @@ -457,11 +457,11 @@ // SOF interrupt if (istat & USB_ISTAT_SOFTOK_MASK) { - USB0->ISTAT = USB_ISTAT_SOFTOK_MASK; + USB0->ISTAT = USB_ISTAT_SOFTOK_MASK; // SOF event, read frame number SOF(frameNumber()); } - + // stall interrupt if (istat & 1<<7) { if (USB0->ENDPOINT[0].ENDPT & USB_ENDPT_EPSTALL_MASK) @@ -483,7 +483,7 @@ // EP0 SETUP event (SETUP data received) EP0setupCallback(); - + } else { // OUT packet if (TOK_PID((EP_BDT_IDX(num, dir, ev_odd))) == OUT_TOKEN) { @@ -517,11 +517,11 @@ USB0->ISTAT = USB_ISTAT_TOKDNE_MASK; } - + // sleep interrupt if (istat & 1<<4) { USB0->ISTAT |= USB_ISTAT_SLEEP_MASK; - } + } // error interrupt if (istat & USB_ISTAT_ERROR_MASK) {