cancel
Showing results for 
Search instead for 
Did you mean: 

HAL_Delay does not work

PMind
Associate II

I built my first board with a STM32F030CC mcu. I downloaded STM32CubeIDE and created a new project. I set PA8 as a GPIO output port.

All I want to achive is to enable/disable the LED connected to the PA8 port .

My problem is, that HAL_Delay does not work since HAL_GetTick seems not to increase.

I suspect, that the interrupt is not called.

Do I need do configure something special to enable Sysclock for HAL_GetTick?

Or should it just work after creating a new project?

5 REPLIES 5
S.Ma
Principal

I think the Systick management changed depending on the version of STM32CubeIDE.

Otherwise, make your own to be free from uncontrollable changes, use your own timer, and at the same time, implement microsecond delays, which is quite handy.

Here is a simple example from STM32L4R (48MHz SYSCLK) using Low Power Timer to make delays (which could have a callback). One shot mode.

//=================== LPTIM2 to replace the NOPs() by interrupt events (master mode only)
extern LPTIM_HandleTypeDef hlptim2;
 
void SPIP_LPTIM2_Init(void) {
  LPTIM_HandleTypeDef* hlptim = &hlptim2;
  // Set the LPTIM2 clock to PCLK because HSI16 is off
//  RCC->CCIPR &= ~(3<<20);
//  RCC->CCIPR |=  (0<<20); 48 MHz = MSI = APB
  HAL_NVIC_SetPriority(LPTIM2_IRQn, 0, 0);
  HAL_NVIC_EnableIRQ(LPTIM2_IRQn);
 
  /* Set WAVE bit to enable the set once mode */
  hlptim->Instance->CFGR |= LPTIM_CFGR_WAVE;
 
  /* Enable Autoreload match interrupt */
  hlptim->Instance->ICR |= 0x0002; // ARRMCF clear flag  
  __HAL_LPTIM_ENABLE_IT(hlptim, LPTIM_IT_ARRM);
 
  /* Enable Compare match interrupt */
//  __HAL_LPTIM_ENABLE_IT(hlptim, LPTIM_IT_CMPM);
 
  /* Enable the Peripheral */
  __HAL_LPTIM_ENABLE(hlptim);
}
 
void SPIP_LPTIM2_SetDelay_us(uint32_t delay_us) {
  LPTIM_HandleTypeDef* hlptim = &hlptim2;
  
  if(delay_us*48>0xFFFF) TrapError(); // change the clock prescaler if looking at more than one milisecond
  if(delay_us==0) return; // done
 
  /* Enable Autoreload match interrupt */
  __HAL_LPTIM_ENABLE_IT(hlptim, LPTIM_IT_ARRM);
 
  /* Enable Compare match interrupt */
//  __HAL_LPTIM_ENABLE_IT(hlptim, LPTIM_IT_CMPM);
 
  /* Enable the Peripheral */
  __HAL_LPTIM_ENABLE(hlptim);
 
  /* Load the period value in the autoreload register */
  __HAL_LPTIM_AUTORELOAD_SET(hlptim, delay_us * 48);
 
  /* Start timer in single shot mode */
  __HAL_LPTIM_START_SINGLE(hlptim);
}
 
 
void HAL_LPTIM_AutoReloadMatchCallback(LPTIM_HandleTypeDef *hlptim) { 
  // Timer is one shot mode, so it should trigger only once after kitstarted
  __HAL_LPTIM_DISABLE_IT(hlptim, LPTIM_IT_ARRM);
 
  NOPs(1);
}
 
void LPTIM2_IRQHandler(void) { 
  HAL_LPTIM_IRQHandler(&hlptim2); // ==>   HAL_LPTIM_AutoReloadMatchCallback(&hlptim2); 
}

PMind
Associate II

So the STM32CubeIDE does not work? Or at least the SysTick does not work? Or can't I use interrupts while debugging.

S.Ma
Principal

Check out similar post, this is not the first one about "HAL_Delay() is broken"

PMind
Associate II

Yes I know, but non has a working solution.

adding HAL_SYSTICK_Callback(); in SysTck_Handler did nothing.

If I set up a new bare metal project for the STM32F030CC do I need to add any additional clock setup steps? Or is the internal clock automatically selected?

Also I found some threads mentioning changing the interrupt priority? Do I need to? If yes would 14 be okay?

turboscrew
Senior III

HAL usually uses timer6 for its timings. Systick is "reserved" for OSes, but F030 doesn't seem to have timer6. And CubeMX seems to use Systick by default. Have you checked the start.S if there is a systick vector? and is Systick enabled? Usually HAL timer is started in HAL_init().