From 1594a9de71c7fedd4c924051a6c09d041154e5b1 Mon Sep 17 00:00:00 2001 From: hathach Date: Mon, 22 Jul 2019 18:53:52 +0700 Subject: [PATCH] stm32f411 usb work with example --- hw/bsp/stm32f303disco/board_stm32f303disco.c | 2 +- hw/bsp/stm32f407disco/board_stm32f407disco.c | 61 ++++---- hw/bsp/stm32f411disco/board_stm32f411disco.c | 148 ++++++++++--------- 3 files changed, 110 insertions(+), 101 deletions(-) diff --git a/hw/bsp/stm32f303disco/board_stm32f303disco.c b/hw/bsp/stm32f303disco/board_stm32f303disco.c index cb370b4f..c5e1108e 100644 --- a/hw/bsp/stm32f303disco/board_stm32f303disco.c +++ b/hw/bsp/stm32f303disco/board_stm32f303disco.c @@ -68,7 +68,7 @@ void board_init(void) __HAL_RCC_GPIOE_CLK_ENABLE(); /* -2- Configure PE.8 to PE.15 IOs in output push-pull mode to drive external LEDs */ - static GPIO_InitTypeDef GPIO_InitStruct; + GPIO_InitTypeDef GPIO_InitStruct; GPIO_InitStruct.Pin = LED_PIN; GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP; GPIO_InitStruct.Pull = GPIO_PULLUP; diff --git a/hw/bsp/stm32f407disco/board_stm32f407disco.c b/hw/bsp/stm32f407disco/board_stm32f407disco.c index 7ef1c16d..6936f2b1 100644 --- a/hw/bsp/stm32f407disco/board_stm32f407disco.c +++ b/hw/bsp/stm32f407disco/board_stm32f407disco.c @@ -32,34 +32,28 @@ #define LED_PORT GPIOD #define LED_PIN GPIO_PIN_14 -void board_init(void) -{ - -#if CFG_TUSB_OS == OPT_OS_NONE - // 1ms tick timer - SysTick_Config(SystemCoreClock / 1000); -#elif CFG_TUSB_OS == OPT_OS_FREERTOS - // If freeRTOS is used, IRQ priority is limit by max syscall ( smaller is higher ) - //NVIC_SetPriority(USB0_IRQn, configLIBRARY_MAX_SYSCALL_INTERRUPT_PRIORITY ); -#endif - -/** System Clock Configuration - * The system Clock is configured as follow : - * System Clock source = PLL (HSE) - * SYSCLK(Hz) = 168000000 - * HCLK(Hz) = 168000000 - * AHB Prescaler = 1 - * APB1 Prescaler = 4 - * APB2 Prescaler = 2 - * HSE Frequency(Hz) = 8000000 - * PLL_M = 8 - * PLL_N = 336 - * PLL_P = 2 - * PLL_Q = 7 - * VDD(V) = 3.3 - * Main regulator output voltage = Scale1 mode - * Flash Latency(WS) = 5 +/** + * @brief System Clock Configuration + * The system Clock is configured as follow : + * System Clock source = PLL (HSE) + * SYSCLK(Hz) = 168000000 + * HCLK(Hz) = 168000000 + * AHB Prescaler = 1 + * APB1 Prescaler = 4 + * APB2 Prescaler = 2 + * HSE Frequency(Hz) = 8000000 + * PLL_M = 8 + * PLL_N = 336 + * PLL_P = 2 + * PLL_Q = 7 + * VDD(V) = 3.3 + * Main regulator output voltage = Scale1 mode + * Flash Latency(WS) = 5 + * @param None + * @retval None */ +static void SystemClock_Config(void) +{ RCC_ClkInitTypeDef RCC_ClkInitStruct; RCC_OscInitTypeDef RCC_OscInitStruct; @@ -90,6 +84,19 @@ void board_init(void) RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV4; RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV2; HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_5); +} + +void board_init(void) +{ +#if CFG_TUSB_OS == OPT_OS_NONE + // 1ms tick timer + SysTick_Config(SystemCoreClock / 1000); +#elif CFG_TUSB_OS == OPT_OS_FREERTOS + // If freeRTOS is used, IRQ priority is limit by max syscall ( smaller is higher ) + //NVIC_SetPriority(USB0_IRQn, configLIBRARY_MAX_SYSCALL_INTERRUPT_PRIORITY ); +#endif + + SystemClock_Config(); // Notify runtime of frequency change. SystemCoreClockUpdate(); diff --git a/hw/bsp/stm32f411disco/board_stm32f411disco.c b/hw/bsp/stm32f411disco/board_stm32f411disco.c index 4871e138..cb598376 100644 --- a/hw/bsp/stm32f411disco/board_stm32f411disco.c +++ b/hw/bsp/stm32f411disco/board_stm32f411disco.c @@ -32,14 +32,28 @@ #define LED_PORT GPIOD #define LED_PIN GPIO_PIN_13 -void board_init(void) +/** + * @brief System Clock Configuration + * The system Clock is configured as follow : + * System Clock source = PLL (HSE) + * SYSCLK(Hz) = 84000000 + * HCLK(Hz) = 84000000 + * AHB Prescaler = 1 + * APB1 Prescaler = 2 + * APB2 Prescaler = 1 + * HSE Frequency(Hz) = 8000000 + * PLL_M = 8 + * PLL_N = 336 + * PLL_P = 4 + * PLL_Q = 7 + * VDD(V) = 3.3 + * Main regulator output voltage = Scale2 mode + * Flash Latency(WS) = 2 + * @param None + * @retval None + */ +static void SystemClock_Config(void) { - #if CFG_TUSB_OS == OPT_OS_NONE - // 1ms tick timer - SysTick_Config(SystemCoreClock / 1000); - #endif - - /* Configure the system clock to 100 MHz */ RCC_ClkInitTypeDef RCC_ClkInitStruct; RCC_OscInitTypeDef RCC_OscInitStruct; @@ -51,33 +65,76 @@ void board_init(void) regarding system frequency refer to product datasheet. */ __HAL_PWR_VOLTAGESCALING_CONFIG(PWR_REGULATOR_VOLTAGE_SCALE2); - /* Enable HSI Oscillator and activate PLL with HSI as source */ - RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSI; - RCC_OscInitStruct.HSIState = RCC_HSI_ON; - RCC_OscInitStruct.HSICalibrationValue = 0x10; + /* Enable HSE Oscillator and activate PLL with HSE as source */ + RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSE; + RCC_OscInitStruct.HSEState = RCC_HSE_ON; RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON; - RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSI; - RCC_OscInitStruct.PLL.PLLM = 16; - RCC_OscInitStruct.PLL.PLLN = 400; + RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSE; + RCC_OscInitStruct.PLL.PLLM = 8; + RCC_OscInitStruct.PLL.PLLN = 336; RCC_OscInitStruct.PLL.PLLP = RCC_PLLP_DIV4; RCC_OscInitStruct.PLL.PLLQ = 7; HAL_RCC_OscConfig(&RCC_OscInitStruct); - /* Select PLL as system clock source and configure the HCLK, PCLK1 and PCLK2 clocks dividers */ + /* Select PLL as system clock source and configure the HCLK, PCLK1 and PCLK2 + clocks dividers */ RCC_ClkInitStruct.ClockType = (RCC_CLOCKTYPE_SYSCLK | RCC_CLOCKTYPE_HCLK | RCC_CLOCKTYPE_PCLK1 | RCC_CLOCKTYPE_PCLK2); RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_PLLCLK; RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1; RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV2; RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV1; - HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_3); + HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_2); +} + +void board_init(void) +{ +#if CFG_TUSB_OS == OPT_OS_NONE + // 1ms tick timer + SysTick_Config(SystemCoreClock / 1000); +#elif CFG_TUSB_OS == OPT_OS_FREERTOS + // If freeRTOS is used, IRQ priority is limit by max syscall ( smaller is higher ) + //NVIC_SetPriority(USB0_IRQn, configLIBRARY_MAX_SYSCALL_INTERRUPT_PRIORITY ); +#endif + + SystemClock_Config(); // Notify runtime of frequency change. SystemCoreClockUpdate(); - /* Configure the GPIO_LED pin */ - __HAL_RCC_GPIOD_CLK_ENABLE(); + // Enable USB OTG clock + __HAL_RCC_USB_OTG_FS_CLK_ENABLE(); + + + /* Configure USB FS GPIOs */ + __HAL_RCC_GPIOA_CLK_ENABLE(); GPIO_InitTypeDef GPIO_InitStruct; + + /* Configure DM DP Pins */ + GPIO_InitStruct.Pin = GPIO_PIN_11 | GPIO_PIN_12; + GPIO_InitStruct.Speed = GPIO_SPEED_HIGH; + GPIO_InitStruct.Mode = GPIO_MODE_AF_PP; + GPIO_InitStruct.Pull = GPIO_NOPULL; + GPIO_InitStruct.Alternate = GPIO_AF10_OTG_FS; + HAL_GPIO_Init(GPIOA, &GPIO_InitStruct); + + /* Configure VBUS Pin */ + GPIO_InitStruct.Pin = GPIO_PIN_9; + GPIO_InitStruct.Mode = GPIO_MODE_INPUT; + GPIO_InitStruct.Pull = GPIO_NOPULL; + HAL_GPIO_Init(GPIOA, &GPIO_InitStruct); + + /* This for ID line debug */ + GPIO_InitStruct.Pin = GPIO_PIN_10; + GPIO_InitStruct.Mode = GPIO_MODE_AF_OD; + GPIO_InitStruct.Pull = GPIO_PULLUP; + GPIO_InitStruct.Speed = GPIO_SPEED_HIGH; + GPIO_InitStruct.Alternate = GPIO_AF10_OTG_FS; + HAL_GPIO_Init(GPIOA, &GPIO_InitStruct); + + // Init the LED + __HAL_RCC_GPIOD_CLK_ENABLE(); + GPIO_InitStruct.Pin = LED_PIN; GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP; GPIO_InitStruct.Pull = GPIO_PULLUP; @@ -86,62 +143,7 @@ void board_init(void) board_led_write(false); -#if 0 - // Init the LED on PD14 - RCC->AHB1ENR |= RCC_AHB1ENR_GPIODEN; - GPIOD->MODER |= GPIO_MODER_MODE14_0; - // TODO Button - - // USB Clock init - // PLL input- 8 MHz (External oscillator clock; HSI clock tolerance isn't - // tight enough- 1%, need 0.25%) - // VCO input- 1 to 2 MHz (2 MHz, M = 4) - // VCO output- 100 to 432 MHz (144 MHz, N = 72) - // Main PLL out- <= 180 MHz (18 MHz, P = 3- divides by 8) - // USB PLL out- 48 MHz (Q = 3) - RCC->PLLCFGR = RCC_PLLCFGR_PLLSRC_HSE | (3 << RCC_PLLCFGR_PLLQ_Pos) | \ - (3 << RCC_PLLCFGR_PLLP_Pos) | (72 << RCC_PLLCFGR_PLLN_Pos) | \ - (4 << RCC_PLLCFGR_PLLM_Pos); - - // Wait for external clock to become ready - RCC->CR |= RCC_CR_HSEON; - while(!(RCC->CR & RCC_CR_HSERDY_Msk)); - - // Wait for PLL to become ready - RCC->CR |= RCC_CR_PLLON; - while(!(RCC->CR & RCC_CR_PLLRDY_Msk)); - - // Switch clocks! - RCC->CFGR |= RCC_CFGR_SW_1; - - // Notify runtime of frequency change. - SystemCoreClockUpdate(); - -#if CFG_TUSB_OS == OPT_OS_NONE - // 1ms tick timer - SysTick_Config(SystemCoreClock / 1000); -#elif CFG_TUSB_OS == OPT_OS_FREERTOS - // If freeRTOS is used, IRQ priority is limit by max syscall ( smaller is higher ) - //NVIC_SetPriority(USB0_IRQn, configLIBRARY_MAX_SYSCALL_INTERRUPT_PRIORITY ); -#endif - - RCC->AHB2ENR |= RCC_AHB2ENR_OTGFSEN; - - // USB Pin Init - // PA9- VUSB, PA10- ID, PA11- DM, PA12- DP - // PC0- Power on - RCC->AHB1ENR |= RCC_AHB1ENR_GPIOAEN; - GPIOA->MODER |= GPIO_MODER_MODE9_1 | GPIO_MODER_MODE10_1 | \ - GPIO_MODER_MODE11_1 | GPIO_MODER_MODE12_1; - GPIOA->AFR[1] |= (10 << GPIO_AFRH_AFSEL9_Pos) | \ - (10 << GPIO_AFRH_AFSEL10_Pos) | (10 << GPIO_AFRH_AFSEL11_Pos) | \ - (10 << GPIO_AFRH_AFSEL12_Pos); - - // Pullup required on ID, despite the manual claiming there's an - // internal pullup already (page 1245, Rev 17) - GPIOA->PUPDR |= GPIO_PUPDR_PUPD10_0; -#endif } //--------------------------------------------------------------------+