STM32 Ecosystem workshop T.O.M.A.S Team
2 • Before adding a new code let’s go with some theory explaining the Cube Library structure and what is generated by STM32CubeMX
Goal of this part 4 Understand the structure of the code generated by STM32CubeMX Know the role of the library files Know the role of the functions executed before main Understand interrupt handling process
STM32CubeMX generated code
STM32CubeMX generated code 6 code structure, main operations after the reset Located in: system_stm32l4xx.c SystemInit(); main() Called from: startup_stm32l4xx.s (reset vector before main()) { STM32CubeMX HAL_Init() { It performs the following operations: HAL_MspInit(); • Enables Floating Point Unit ( FPU ) inside the core } • Resets the Clock Configuration to the default reset state • Disables all Interrupts SystemClockConfig(); • Configures Vector Table location and its offset MX_PPP_Init() { It does NOT configure the clock system (as it was done in HAL_PPP_Init() { Standard Peripherals Library (SPL)) HAL_PPP_MspInit(); } } User HAL_PPP_Action(); while(1); }
STM32CubeMX generated code 7 code structure, main operations after the reset Located in: stm32l4xx_hal.c SystemInit(); main() Called from: main.c { STM32CubeMX HAL_Init() { It performs the following operations: HAL_MspInit(); • Configures FLASH accelerator } • Configures NVIC priority (group and sub-priorities split) • Configures SysTick for 1ms tick (based on HSI clock) SystemClockConfig(); • Initializes Low-level hardware selected in STM32CubeMX MX_PPP_Init() (call to HAL_MspInit() function) { HAL_PPP_Init() { HAL_PPP_MspInit(); } } User HAL_PPP_Action(); while(1); }
STM32CubeMX generated code 8 code structure, main operations after the reset Located in: stm32l4xx_hal_msp.c SystemInit(); main() Called from: stm32l4xx_hal.c { STM32CubeMX HAL_Init() { It performs the following operations: HAL_MspInit(); • Configures Interrupts Priorities } (for each peripheral selected in STM32CubeMX) SystemClockConfig(); MX_PPP_Init() { HAL_PPP_Init() { HAL_PPP_MspInit(); } } User HAL_PPP_Action(); while(1); }
STM32CubeMX generated code 9 code structure, main operations after the reset Located in: main.c SystemInit(); main() Called from: main.c { STM32CubeMX HAL_Init() { It performs the following operations: HAL_MspInit(); • Configures System & Buses clocks } (based on STM32CubeMX clock settings) SystemClockConfig(); MX_PPP_Init() { HAL_PPP_Init() { HAL_PPP_MspInit(); } } User HAL_PPP_Action(); while(1); }
STM32CubeMX generated code 10 code structure, main operations after the reset Located in: main.c SystemInit(); main() Called from: main.c { STM32CubeMX HAL_Init() { It performs the following operations: HAL_MspInit(); • Initializes PPP peripheral based on STM32CubeMX } configuration • Uses structures and dedicated initialization functions type SystemClockConfig(); HAL_PPP_Init() MX_PPP_Init() • Within those functions this is possible to tune a peripheral { configuration HAL_PPP_Init() { HAL_PPP_MspInit(); } } User HAL_PPP_Action(); while(1); }
STM32CubeMX generated code 11 code structure, main operations after the reset Located in: stm32l4xx_hal_PPP.c SystemInit(); main() Called from: main.c { STM32CubeMX HAL_Init() { It performs the following operations: HAL_MspInit(); • Initializes the PPP peripherial mode ( according to parameters } specified in the PPP_InitTypeDef structure) and the associated SystemClockConfig(); handle MX_PPP_Init() { HAL_PPP_Init() { HAL_PPP_MspInit(); } } User HAL_PPP_Action(); while(1); }
STM32CubeMX generated code 12 code structure, main operations after the reset Located in: stm32l4xx_hal_msp.c SystemInit(); main() Called from: stm32l4xx_hal_PPP.c { STM32CubeMX HAL_Init() { It performs the following operations: HAL_MspInit(); • Configures Clocks related to PPP peripheral } • Configures GPIO lines assigned to PPP peripheral • SystemClockConfig(); Configures DMA channel assigned to PPP peripheral (except source and destination address and number of the MX_PPP_Init() data to be transferred – this is done by HAL_PPP_Action() { function) HAL_PPP_Init() • { Configures Interrupts selected for PPP peripheral HAL_PPP_MspInit(); } } User HAL_PPP_Action(); while(1); }
STM32CubeMX generated code 13 peripherals initialization (DAC example) • HAL_DAC_Init() All HAL_DAC_Init() functions void HAL_DAC_MspInit(DAC_HandleTypeDef* hdac) are calling the same function { function details GPIO_InitTypeDef GPIO_InitStruct; HAL_DAC_MspInit() if(hdac->Instance==DAC) { /* USER CODE BEGIN DAC_MspInit 0 */ Handler instance parameter HAL_DAC_Init() start /* USER CODE END DAC_MspInit 0 */ is used to determine which /* Peripheral clock enable */ __HAL_RCC_DAC_CLK_ENABLE(); DAC needs to be initialized HAL_DAC_MspInit() start /**DAC GPIO Configuration PA4 ------> DAC_OUT1 Initialize GPIO lines */ GPIO_InitStruct.Pin = GPIO_PIN_4; selected in GPIO and linked DMA initialization GPIO_InitStruct.Mode = GPIO_MODE_ANALOG; GPIO_InitStruct.Pull = GPIO_NOPULL; STM32CubeMX HAL_GPIO_Init(GPIOA, &GPIO_InitStruct); /* Peripheral DMA init*/ HAL_DAC_MspInit() end Initialize DMA channel hdma_dac_ch1.Instance = DMA1_Channel2; selected in hdma_dac_ch1.Init.Request = DMA_REQUEST_9; hdma_dac_ch1.Init.Direction = DMA_MEMORY_TO_PERIPH; STM32CubeMX hdma_dac_ch1.Init.PeriphInc = DMA_PINC_DISABLE; HAL_DAC_Init() end hdma_dac_ch1.Init.MemInc = DMA_MINC_ENABLE; hdma_dac_ch1.Init.PeriphDataAlignment = DMA_PDATAALIGN_HALFWORD; hdma_dac_ch1.Init.MemDataAlignment = DMA_MDATAALIGN_HALFWORD; hdma_dac_ch1.Init.Mode = DMA_CIRCULAR; hdma_dac_ch1.Init.Priority = DMA_PRIORITY_LOW; Store Init structure into DAC if (HAL_DMA_Init(&hdma_dac_ch1) != HAL_OK) registers { Error_Handler(); } __HAL_LINKDMA(hdac,DMA_Handle1,hdma_dac_ch1); /* USER CODE BEGIN DAC_MspInit 1 */ /* USER CODE END DAC_MspInit 1 */ } }
STM32CubeMX generated code 14 code structure, main operations after the reset Located in: main.c SystemInit(); main() Called from: main.c { STM32CubeMX HAL_Init() { It performs the following operations: HAL_MspInit(); • PPP peripherals control (i.e. Start, Stop, Calibration) } • DMA configuration if needed (buffer location, source and SystemClockConfig(); destination addresses, number of data to be transferred) MX_PPP_Init() { HAL_PPP_Init() { HAL_PPP_MspInit(); } } User HAL_PPP_Action(); while(1); }
STM32CubeMX generated code code structure, main operations after the reset - summary 15 Operation Function Origin SystemInit(); • called from main() • source location { • SystemInit() Enable Floating Point Unit (FPU) inside the core HAL_Init() • Reset the clock configuration to the default reset state • startup_stm32xxxx.s { STM32CubeMX • Disable all interrupts • system_stm32xxxx.c HAL_MspInit(); • Configure vector table location and its offset } • HAL_Init() Configure FLASH accelerator SystemClockConfig(); • Configure NVIC priority (group and sub-priorities split) • main.c • Configure SysTick for 1ms tick (based on HSI clock) • STM32CubeMX stm32xxxx_hal.c MX_PPP_Init() • Initialization of low level hardware selected in STM32CubeMX (HAL_MspInit() { function) HAL_PPP_Init() • HAL_MspInit() Configure Interrupts priorities (for each peripheral selected in { STM32CubeMX) • STM32CubeMX stm32xxxx_hal_msp.c HAL_PPP_MspInit(); • stm32xxxx_hal_msp.c } } • SystemClockConfig() Configure System & Buses clocks based on STM32CubeMX clock settings STM32CubeMX • main.c HAL_PPP_Action(); • main.c while(1); • MX_PPP_Init() } Initialize PPP peripheral based on STM32CubeMX configuration:.PPP related STM32CubeMX peripheral configuration (GPIO lines, DMA channel) in function • main.c HAL_PPP_MspInit() • main.c or PPP.c • HAL_PPP_MspInit() Connect Clock to the peripheral • Configure GPIO lines • STM32CubeMX stm32xxxx_hal_msp.c • Configure DMA channels (based on STM32CubeMX settings) • stm32xxxx_hal_msp.c • HAL_PPP_Action() Activate PPP peripherals control (i.e. Start, Stop, Calibration), • Configure DMA if needed (buffer location, source and destination addresses, • User main.c number of data to be transferred) • user_code.c
STM32CubeMX generated code 16 code structure - interrupts operation Function Origin • called from • source location PPP_IRQHandler() • Check all possible flags and interrupt triggers PPP_IRQHandler() { • Clear all interrupts flags • STM32CubeMX stm32xxxx_it.c HAL_PPP_Callback(); • Call proper callback functions assigned to the particular interrupt source/trigger • stm32xxxx_hal_ppp.c } • Perform an action related to the specific interrupt. HAL_PPP_Callback() • User user_code.c • user_code.c
HAL libraries basic information
Recommend
More recommend