Skip to main content
Senior III
May 5, 2025
Solved

SysTick_Handler triggering before main initialization is complete

  • May 5, 2025
  • 2 replies
  • 497 views

I am using custom STM32G4 board and generated code using the MCSDK 6.2.0. I am observing that the SysTick_Handler(void) function is called before the peripheral initializations, and it is leading to hardware fault.

int main(void)
{
 /* USER CODE BEGIN 1 */

 /* USER CODE END 1 */

 /* MCU Configuration--------------------------------------------------------*/

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

 /* USER CODE BEGIN Init */

 /* USER CODE END Init */

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

 /* USER CODE BEGIN SysInit */

 /* USER CODE END SysInit */

 /* Initialize all configured peripherals */
 MX_GPIO_Init();
 MX_ADC1_Init();
 MX_ADC2_Init();
#ifdef DACENABLED
 MX_DAC1_Init();
#endif

 MX_CORDIC_Init();
 
 MX_OPAMP1_Init();
 MX_OPAMP2_Init();
 MX_OPAMP3_Init();
 MX_TIM1_Init();
 MX_TIM4_Init();
 MX_FDCAN1_Init();
 /* USER CODE BEGIN 2 */
 MX_MotorControl_Init();
 /* Initialize interrupts */
 MX_NVIC_Init();
 HAL_FDCAN_Start(&hfdcan1);
 system_ready = true;
#ifdef DACENABLED
 HAL_DAC_Start(&hdac1,DAC_CHANNEL_1);
#endif

 /* USER CODE BEGIN 2 */
 /* USER CODE END 2 */

 /* Infinite loop */
 /* USER CODE BEGIN WHILE */
 while (1)
 {
 /* USER CODE END WHILE */
	 // HAL_GPIO_TogglePin(GPIOB, GPIO_PIN_12);
 /* USER CODE BEGIN 3 */
 }
 /* USER CODE END 3 */
}

But the Systick interrupt is enabled only in MX_MotorControl_Init(). Then why it is called sometime after the HAL_Init();

 

Best answer by LCE

"...is leading to hardware fault"

Do you mean that HardFaultHandler() is called ? 

I'm pretty sure that the SysTick is enabled in HAL_init(), because some SystemClock_Config() parts usually have timeouts using SysTick.

Debug-step through your code to find out what's exactly going wrong.

2 replies

LCE
LCEBest answer
Principal II
May 5, 2025

"...is leading to hardware fault"

Do you mean that HardFaultHandler() is called ? 

I'm pretty sure that the SysTick is enabled in HAL_init(), because some SystemClock_Config() parts usually have timeouts using SysTick.

Debug-step through your code to find out what's exactly going wrong.

STuser2Author
Senior III
May 5, 2025

I could solve the problem because the pointer was not initialized, but the main question is 

MC_RunMotorControlTasks(); is called in void SysTick_Handler(void) which is expected to be called after the 

MX_MotorControl_Init();

but the sequence is not followed, that is the main issue. Is it ok to use some strategy to overcome this issue? which i have implemented using the variable 

 system_ready = true;

 

LCE
Principal II
May 22, 2025

MC_RunMotorControlTasks(); is called in void SysTick_Handler(void)

Bad idea to call anything in SysTickHandler.

If your really need to call any "bigger" function in an interrupt handler (bad idea...), use at least some extra timer for that.