cancel
Showing results for 
Search instead for 
Did you mean: 

STM32L151ZETx: Optimizing Initialization Time (Power-On to while(1))

AnJung
Visitor

Target MCU: STM32L151ZETx (LQFP144)
Main Clock Frequency: 32 MHz

When the MCU is first powered on, it takes about 41 ms to reach the while(1) loop in the main() function, based on the code shown below.

For my product requirements, I would like this startup time (from power-on to reaching while(1)) to be reduced to around 16 ms.

Is there any way to achieve this reduction? Any suggestions or optimization techniques would be greatly appreciated.

 

/* Includes ------------------------------------------------------------------*/
#include "main.h"
#include "adc.h"
#include "dma.h"
#include "rtc.h"
#include "tim.h"
#include "usart.h"
#include "gpio.h"

/* Private function prototypes -----------------------------------------------*/
void SystemClock_Config(void);

int main(void)
{
/* Reset of all peripherals, Initializes the Flash interface and the Systick. */
HAL_Init();

/* Configure the system clock */
SystemClock_Config();

/* Initialize all configured peripherals */
MX_GPIO_Init();
MX_DMA_Init();
MX_ADC_Init();
MX_TIM5_Init();
MX_TIM2_Init();
MX_TIM3_Init();
MX_TIM4_Init();
MX_TIM6_Init();
MX_RTC_Init();
MX_USART1_UART_Init();
MX_USART3_UART_Init();

/* Start timers */
HAL_TIM_Base_Start_IT(&htim2); // TIMER2(100us)
HAL_TIM_Base_Start_IT(&htim5); // TIMER5(1ms)
HAL_TIM_Base_Start_IT(&htim3); // TIMER3(10ms)
HAL_TIM_Base_Start_IT(&htim4); // TIMER4(20ms)
HAL_TIM_Base_Start_IT(&htim6); // TIMER6(1440Hz)
HAL_TIM_PWM_Start(&htim5, TIM_CHANNEL_1);

/* Infinite loop */
while (1)
{
}

1 REPLY 1
TDK
Super User

Toggle pins at various points during startup to see where the most time can be saved. Toggle immediately after Reset_Handler to see the minimum time.

A few things in particular will have large effects:

  • Set the clock speed high immediately after Reset_Handler, before CopyDataInit and FillZerobss sections.
  • Unroll loops in CopyDataInit/FillZerobss and ensure they operate operating on uint32_t and not uint8_t. Aligning these sections to 32-bytes and using 4x uint32_t writes is close to optimal.
  • Use higher compiler optimization settings. The Release configuration is a good start.
If you feel a post has answered your question, please click "Accept as Solution".