From be4eeeb9433c187eafabcd5e4048dc53e78ce194 Mon Sep 17 00:00:00 2001 From: hathach Date: Mon, 22 Jul 2019 18:35:22 +0700 Subject: [PATCH] refactor stm32f407 to use st hal api --- hw/bsp/stm32f407disco/board.mk | 10 +- hw/bsp/stm32f407disco/board_stm32f407disco.c | 141 +++++++++++++------ hw/bsp/stm32f411disco/board.mk | 4 +- 3 files changed, 105 insertions(+), 50 deletions(-) diff --git a/hw/bsp/stm32f407disco/board.mk b/hw/bsp/stm32f407disco/board.mk index 18223399c..bc1fd7c70 100644 --- a/hw/bsp/stm32f407disco/board.mk +++ b/hw/bsp/stm32f407disco/board.mk @@ -13,14 +13,20 @@ CFLAGS += \ LD_FILE = hw/bsp/stm32f407disco/STM32F407VGTx_FLASH.ld SRC_C += \ - hw/mcu/st/system-init/system_stm32f4xx.c + hw/mcu/st/system-init/system_stm32f4xx.c \ + hw/mcu/st/stm32lib/STM32F4xx_HAL_Driver/Src/stm32f4xx_hal.c \ + hw/mcu/st/stm32lib/STM32F4xx_HAL_Driver/Src/stm32f4xx_hal_cortex.c \ + hw/mcu/st/stm32lib/STM32F4xx_HAL_Driver/Src/stm32f4xx_hal_rcc.c \ + hw/mcu/st/stm32lib/STM32F4xx_HAL_Driver/Src/stm32f4xx_hal_gpio.c SRC_S += \ hw/mcu/st/startup/stm32f4/startup_stm32f407xx.s INC += \ + $(TOP)/hw/mcu/st/cmsis \ $(TOP)/hw/mcu/st/stm32lib/CMSIS/STM32F4xx/Include \ - $(TOP)/hw/mcu/st/cmsis + $(TOP)/hw/mcu/st/stm32lib/STM32F4xx_HAL_Driver/Inc \ + $(TOP)/hw/bsp/stm32f407disco # For TinyUSB port source VENDOR = st diff --git a/hw/bsp/stm32f407disco/board_stm32f407disco.c b/hw/bsp/stm32f407disco/board_stm32f407disco.c index c5695733c..7ef1c16de 100644 --- a/hw/bsp/stm32f407disco/board_stm32f407disco.c +++ b/hw/bsp/stm32f407disco/board_stm32f407disco.c @@ -27,39 +27,13 @@ #include "../board.h" #include "stm32f4xx.h" +#include "stm32f4xx_hal_conf.h" + +#define LED_PORT GPIOD +#define LED_PIN GPIO_PIN_14 void board_init(void) { - // 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 @@ -69,21 +43,100 @@ void board_init(void) //NVIC_SetPriority(USB0_IRQn, configLIBRARY_MAX_SYSCALL_INTERRUPT_PRIORITY ); #endif - RCC->AHB2ENR |= RCC_AHB2ENR_OTGFSEN; +/** 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 + */ + RCC_ClkInitTypeDef RCC_ClkInitStruct; + RCC_OscInitTypeDef RCC_OscInitStruct; + + /* Enable Power Control clock */ + __HAL_RCC_PWR_CLK_ENABLE(); + + /* The voltage scaling allows optimizing the power consumption when the device is + clocked below the maximum system frequency, to update the voltage scaling value + regarding system frequency refer to product datasheet. */ + __HAL_PWR_VOLTAGESCALING_CONFIG(PWR_REGULATOR_VOLTAGE_SCALE1); + + /* 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_HSE; + RCC_OscInitStruct.PLL.PLLM = 8; + RCC_OscInitStruct.PLL.PLLN = 336; + RCC_OscInitStruct.PLL.PLLP = RCC_PLLP_DIV2; + 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 */ + 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_DIV4; + RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV2; + HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_5); + + // Notify runtime of frequency change. + SystemCoreClockUpdate(); + + // Enable USB OTG clock + __HAL_RCC_USB_OTG_FS_CLK_ENABLE(); + + GPIO_InitTypeDef GPIO_InitStruct; // 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); + __HAL_RCC_GPIOA_CLK_ENABLE(); - // Pullup required on ID, despite the manual claiming there's an - // internal pullup already (page 1245, Rev 17) - GPIOA->PUPDR |= GPIO_PUPDR_PUPD10_0; + /* 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; + GPIO_InitStruct.Speed = GPIO_SPEED_FAST; + HAL_GPIO_Init(LED_PORT, &GPIO_InitStruct); + + board_led_write(false); + + // TODO Button } //--------------------------------------------------------------------+ @@ -92,11 +145,7 @@ void board_init(void) void board_led_write(bool state) { - if (!state) { - GPIOD->BSRR = GPIO_BSRR_BR14; - } else { - GPIOD->BSRR = GPIO_BSRR_BS14; - } + HAL_GPIO_WritePin(LED_PORT, LED_PIN, state); } uint32_t board_button_read(void) diff --git a/hw/bsp/stm32f411disco/board.mk b/hw/bsp/stm32f411disco/board.mk index a79c2695b..8154545f7 100644 --- a/hw/bsp/stm32f411disco/board.mk +++ b/hw/bsp/stm32f411disco/board.mk @@ -23,10 +23,10 @@ SRC_S += \ hw/mcu/st/startup/stm32f4/startup_stm32f411xe.s INC += \ - $(TOP)/hw/bsp/stm32f411disco \ + $(TOP)/hw/mcu/st/cmsis \ $(TOP)/hw/mcu/st/stm32lib/CMSIS/STM32F4xx/Include \ $(TOP)/hw/mcu/st/stm32lib/STM32F4xx_HAL_Driver/Inc \ - $(TOP)/hw/mcu/st/cmsis + $(TOP)/hw/bsp/stm32f411disco # For TinyUSB port source VENDOR = st