cancel
Showing results for 
Search instead for 
Did you mean: 

Why isn't HAL_Init() exiting and why am stuck at an infinite loop at HAL_TIM_PeriodElapsedCallback()?

CDyer.1
Senior

Hi guys,

My code is acting strange, when it seems to intermittently get stuck during the HAL_Init() function at the start of the code. The issue never throws an error ( but the debugger constantly pauses at HAL_TIM_PeriodElapsedCallback() despite there being no breakpoint:

void HAL_TIM_PeriodElapsedCallback(TIM_HandleTypeDef *htim)
{
  /* USER CODE BEGIN Callback 0 */
 
  /* USER CODE END Callback 0 */
  if (htim->Instance == TIM6) {
    HAL_IncTick();
  }
  /* USER CODE BEGIN Callback 1 */
 
 

This is the systick increment function. Now, this should be running every 1ms anyway, I understand that but it keeps calling this function and the program doesn't progress past this and Hal_Init() doesn't exit. What's strange is that this is intermittent and I can't consistently repeat this error unless I add a breakpoint to the code above it.

void ADC_DMAConvCplt(DMA_HandleTypeDef *hdma)
{
	HAL_ADC_Stop_DMA(&hadc1);
	while(1);
}
/*
void adc_polling_task(void)
{
	for(uint8_t i = 0; i<= 50; i++)
	{
		if(HAL_ADC_PollForConversion(&hadc1, 5) == HAL_OK)
		{
			ADC_DMA_val[i] = HAL_ADC_GetValue(&hadc1);
		}
	}
}*/
/* USER CODE END 4 */
 
 /**
  * @brief  Period elapsed callback in non blocking mode
  * @note   This function is called  when TIM6 interrupt took place, inside
  * HAL_TIM_IRQHandler(). It makes a direct call to HAL_IncTick() to increment
  * a global variable "uwTick" used as application time base.
  * @param  htim : TIM handle
  * @retval None
  */
void HAL_TIM_PeriodElapsedCallback(TIM_HandleTypeDef *htim)
{
  /* USER CODE BEGIN Callback 0 */
 
  /* USER CODE END Callback 0 */
  if (htim->Instance == TIM6) {
    HAL_IncTick();
  }

Adding a breakpoint inside the ADC_DMAConvCplt() function always causes the HAL_TIM_PeriodElapsedCallback() to break and then I'm stuck in that function.

For the sake of completeness here's my code before I get to my while loop:

/* USER CODE END Header */
 
/* Includes ------------------------------------------------------------------*/
#include "main.h"
 
/* Private includes ----------------------------------------------------------*/
/* USER CODE BEGIN Includes */
#include "stdint.h"
/* USER CODE END Includes */
 
/* Private typedef -----------------------------------------------------------*/
/* USER CODE BEGIN PTD */
 
/* USER CODE END PTD */
 
/* Private define ------------------------------------------------------------*/
/* USER CODE BEGIN PD */
/* USER CODE END PD */
 
/* Private macro -------------------------------------------------------------*/
/* USER CODE BEGIN PM */
 
/* USER CODE END PM */
 
/* Private variables ---------------------------------------------------------*/
ADC_HandleTypeDef hadc1;
DMA_HandleTypeDef hdma_adc1;
 
TIM_HandleTypeDef htim2;
 
UART_HandleTypeDef huart3;
 
/* USER CODE BEGIN PV */
 
/* USER CODE END PV */
 
/* Private function prototypes -----------------------------------------------*/
void SystemClock_Config(void);
static void MX_GPIO_Init(void);
static void MX_DMA_Init(void);
static void MX_USART3_UART_Init(void);
static void MX_TIM2_Init(void);
static void MX_ADC1_Init(void);
/* USER CODE BEGIN PFP */
void HAL_TIM_PWM_PulseFinishedCallback(TIM_HandleTypeDef *htim);
 //void ADC_DMAConvCplt(DMA_HandleTypeDef *hdma);
void adc_polling_task(void);
/* USER CODE END PFP */
 
/* Private user code ---------------------------------------------------------*/
/* USER CODE BEGIN 0 */
uint32_t ADC_DMA_val[100] = {0};
uint8_t newline[] = "\n";
/* USER CODE END 0 */
 
/**
  * @brief  The application entry point.
  * @retval int
  */
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_DMA_Init();
  MX_USART3_UART_Init();
  MX_TIM2_Init();
  MX_ADC1_Init();
  /* USER CODE BEGIN 2 */
	uint8_t first_msg[] = "\rWelcome.\nThe Digital Metalarm has been reset.\n";
	HAL_UART_Transmit(&huart3, first_msg, sizeof(first_msg), HAL_MAX_DELAY);
	if(HAL_TIM_PWM_Start(&htim2, TIM_CHANNEL_1) != HAL_OK)
	{
		Error_Handler();
	}
	if(HAL_TIM_PWM_Start_IT(&htim2, TIM_CHANNEL_2) != HAL_OK)
	{
		Error_Handler();
	}
//	if(HAL_ADC_Start(&hadc1)!= HAL_OK)
//	{
//		Error_Handler();
//	}
  /* USER CODE END 2 */
 
  /* Infinite loop */
  /* USER CODE BEGIN WHILE */
  while (1)
  {
 
 
 
//	  HAL_UART_Transmit(&huart3, ADC_val, sizeof(ADC_val), HAL_MAX_DELAY);
	  //HAL_UART_Transmit(&huart3, (uint8_t*)newline, sizeof(newline), HAL_MAX_DELAY);
	  //HAL_Delay(500);
    /* USER CODE END WHILE */
 
    /* USER CODE BEGIN 3 */
  }
  /* USER CODE END 3 */
}

So, is there any obvious reason as to why my code intermittently breaks at HAL_TIM_PeriodElapsedCallback() despite there being no break point and why it forms an infinte loop, never allowing Hal_Init() to exit? I also find it strange that a break point in the function above it will always guarantee this error? I'm using an stm32F767ZI nucleo and STM32Cubeide and never had this issue previously.

1 REPLY 1
S.Ma
Principal

A callback is usually within an interrupt routine. If the configuration is bogus or there is a high frequency external signal triggering at high rate interrupts, it could happen. Also Interrupt Priority is user settings and this could cause race conditions. Try to put all interrupts at same priority, then test. If no clues, enable interrupts one by one.