2

I'm trying to upload code to an STM32F103RET6 MCU and have a simple setup by now (see image below). I have attached a LED to pin PA1 as a GPIO OUTPUT and an external oscillator at 16 MHz. The other three pins are connected to the JTAG connector that goes to the STLINK v2. STM32 setup connections

When i generate the code to perform a simple blink I'm having trouble with the HAL_Delay function. The code is shown below.

#include "main.h"

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

int main(void)
{
  HAL_Init();

  SystemClock_Config();

  MX_GPIO_Init();

  while (1)
  {
      HAL_GPIO_WritePin(LED_G_GPIO_Port, LED_G_Pin, 1);
      HAL_Delay(500);
      HAL_GPIO_WritePin(LED_G_GPIO_Port, LED_G_Pin, 0);
      HAL_Delay(500);
  }
}

void SystemClock_Config(void)
{
  RCC_OscInitTypeDef RCC_OscInitStruct = {0};
  RCC_ClkInitTypeDef RCC_ClkInitStruct = {0};

  /** Initializes the RCC Oscillators according to the specified parameters
  * in the RCC_OscInitTypeDef structure.
  */
  RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSE;
  RCC_OscInitStruct.HSEState = RCC_HSE_ON;
  RCC_OscInitStruct.HSEPredivValue = RCC_HSE_PREDIV_DIV2;
  RCC_OscInitStruct.HSIState = RCC_HSI_ON;
  RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON;
  RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSE;
  RCC_OscInitStruct.PLL.PLLMUL = RCC_PLL_MUL9;
  if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK)
  {
    Error_Handler();
  }
  /** Initializes the CPU, AHB and APB buses clocks
  */
  RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_HCLK|RCC_CLOCKTYPE_SYSCLK
                              |RCC_CLOCKTYPE_PCLK1|RCC_CLOCKTYPE_PCLK2;
  RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_PLLCLK;
  RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1;
  RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV2;
  RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV1;

  if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_2) != HAL_OK)
  {
    Error_Handler();
  }
}

static void MX_GPIO_Init(void)
{
  GPIO_InitTypeDef GPIO_InitStruct = {0};

  /* GPIO Ports Clock Enable */
  __HAL_RCC_GPIOD_CLK_ENABLE();
  __HAL_RCC_GPIOA_CLK_ENABLE();
  __HAL_RCC_GPIOB_CLK_ENABLE();

  /*Configure GPIO pin Output Level */
  HAL_GPIO_WritePin(LED_G_GPIO_Port, LED_G_Pin, GPIO_PIN_RESET);

  /*Configure GPIO pin : LED_G_Pin */
  GPIO_InitStruct.Pin = LED_G_Pin;
  GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
  GPIO_InitStruct.Pull = GPIO_NOPULL;
  GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
  HAL_GPIO_Init(LED_G_GPIO_Port, &GPIO_InitStruct);

void Error_Handler(void)
{
  /* USER CODE BEGIN Error_Handler_Debug */
  /* User can add his own implementation to report the HAL error return state */
  __disable_irq();
  while (1)
  {
  }
  /* USER CODE END Error_Handler_Debug */
}

#ifdef  USE_FULL_ASSERT

void assert_failed(uint8_t *file, uint32_t line)
{

}
#endif /* USE_FULL_ASSERT */

The connection with the MCU is fine and the code starts running, so the first line that is to turn on the LED is executed correctly and the LED turns on. The problem is when the HAL_Delay(500) is executed, it stops the code and the following message pops up: No source available for "uwTickPrio() at 0x20000004" See image: uwTickPrio()

Setting NVIC priority as stated in: HAL_Delay stuck in infinite loop With the following parameters (as I understood from that post): NVIC preemption priority

And many other things commented in the forums but I cannot solve this problem. Any tip could be really useful.

Many thanks for taking your time.

1 Answers1

0

In order for HAL_Delay to work, the SysTick interrupt (or other timer interrupt) must be configured to run and it must call HAL_IncTick function at 1ms intervals. The reason to call HAL_IncTick at 1ms intervals is to increment an internal counter inside HAL that actually does the "counting" so that any time you call HAL_Delay it can actually "unlock" after specified time. If HAL_IncTick is not called from SysTick or similar interrupt, calling HAL_Delay will cause you to get "stuck" as it'll never exit which is likely the case you experience.

The easiest way to debug this would be the following:

  1. Find your SysTick interrupt and (while debugging) place a breakpoint inside of it and make sure it actually gets called. If not, configure SysTick to run at 1ms intervals. From the code you've posted, you don't configure SysTick anywhere.

  2. Make sure that HAL_IncTick gets called inside SysTick interrupt. If it's not there, place it there.

Jacek Ślimok
  • 2,627
  • 3
  • 11
  • 31
  • Thanks for answering. Just one question, where can i find my SysTick Interrupt? – Arnau Reyes Fernandez May 10 '21 at 19:35
  • Usually in their examples guys from ST put all interrupts inside a single file ending with `_it.c`, for example `stm32f4xx_it.c`. – Jacek Ślimok May 10 '21 at 20:06
  • After debugging line by line, i have seen that the function HAL_IncTick is not called, but it is defined in the SysTick Interrupt. I've seen that when entering the `HAL_Delay` the code gets stuck in the following line: `while ((HAL_GetTick() - tickstart) < wait)`, as it enters the while and keeps running this: `__weak uint32_t HAL_GetTick(void) {return uwTick;}` until it breaks. – Arnau Reyes Fernandez May 16 '21 at 18:14
  • If `HAL_IncTick` is not called even though it's placed inside SysTick, then the reason none of this works is because your SysTick interrupt is not configured to run. – Jacek Ślimok May 16 '21 at 21:48
  • And how can i make sure the SysTick interrupt is configured and works as it should? The weird thing is that i have not touched anything, this is what the STM32 CubeIDE generates, so I don't really understand what is happening – Arnau Reyes Fernandez May 17 '21 at 18:13