STM32F303RET’s core is always halted after programming

clockhal-librarystm32stm32cubemxstm32f3

I have an STM32F303RET MCU with Eclipse, Cross ARM GCC and HAL library environment. I am using an STM32F4Discovery board as SWD programmer. My problem is when I download the hex file to the MCU I got the following message:

Connected via SWD.
Target voltage = 2.9 V.
Connection mode :
Normal.
Device ID:0x446
Device flash Size : 512 Kbytes
Device family :STM32F302xE/F303xE/F398xx

Loading file…
Flash Programming:
File :
Line_Sensors2.hex
Address : 0x08000000
Memory
programming…
0% …………………………… 100%
Memory programmed in 1s and 358ms.
Programming Complete.

MCU Reset.

The core is halted!

My current configuration in STM32CUBEMX with an external 8MHz crystal is:

enter image description here

Soure files, generated by STM32CubeMX:

main.c

/* Includes ------------------------------------------------------------------*/
#include "stm32f3xx_hal.h"
#include "mxconstants.h"

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


int main(void)
{
  /* MCU Configuration----------------------------------------------------------*/

  /* 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();

  while (1)
  {
      HAL_GPIO_WritePin(GPIOB, D42_LED_Pin, GPIO_PIN_SET);
      HAL_GPIO_WritePin(GPIOB, D43_LED_Pin, GPIO_PIN_SET);
  }
}

/** System Clock Configuration
*/
void SystemClock_Config(void)
{
    RCC_OscInitTypeDef RCC_OscInitStruct;
    RCC_ClkInitTypeDef RCC_ClkInitStruct;

    RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSE;
    RCC_OscInitStruct.HSEState = RCC_HSE_ON;
    RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON;
    RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSE;
    RCC_OscInitStruct.PLL.PLLMUL = RCC_PLL_MUL2;
    RCC_OscInitStruct.PLL.PREDIV = RCC_PREDIV_DIV1;
    HAL_RCC_OscConfig(&RCC_OscInitStruct);

    RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_SYSCLK|RCC_CLOCKTYPE_PCLK1;
    RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_PLLCLK;
    RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1;
    RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV2;
    RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV1;
    HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_2);

    HAL_SYSTICK_Config(HAL_RCC_GetHCLKFreq()/1000);

    HAL_SYSTICK_CLKSourceConfig(SYSTICK_CLKSOURCE_HCLK);

    /* SysTick_IRQn interrupt configuration */
    HAL_NVIC_SetPriority(SysTick_IRQn, 0, 0);
}

/** Configure pins as 
        * Analog 
        * Input 
        * Output
        * EVENT_OUT
        * EXTI
*/
void MX_GPIO_Init(void)
{
  GPIO_InitTypeDef GPIO_InitStruct;

  /* GPIO Ports Clock Enable */
  __GPIOF_CLK_ENABLE();
  __GPIOA_CLK_ENABLE();
  __GPIOB_CLK_ENABLE();

  /*Configure GPIO pins : D42_LED_Pin D43_LED_Pin */
  GPIO_InitStruct.Pin = D42_LED_Pin|D43_LED_Pin;
  GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
  GPIO_InitStruct.Pull = GPIO_NOPULL;
  GPIO_InitStruct.Speed = GPIO_SPEED_LOW;
  HAL_GPIO_Init(GPIOB, &GPIO_InitStruct);   
}

stm32f3xx_hal_msp.c:

/* Includes ------------------------------------------------------------------*/
#include "stm32f3xx.h"
#include "stm32f3xx_hal.h"
#include "stm32f3xx_hal_cortex.h"
/**
  * Initializes the Global MSP.
  */
void HAL_MspInit(void)
{
  __SYSCFG_CLK_ENABLE();

  HAL_NVIC_SetPriorityGrouping(NVIC_PRIORITYGROUP_0);

  /* System interrupt init*/
  /* SysTick_IRQn interrupt configuration */
  HAL_NVIC_SetPriority(SysTick_IRQn, 0, 0);
}

stm32f3xx_it.c:

/* Includes ------------------------------------------------------------------*/
#include "stm32f3xx_hal.h"
#include "stm32f3xx.h"
#include "stm32f3xx_it.h"

/**
* @brief This function handles System tick timer.
*/
void SysTick_Handler(void)
{
  HAL_IncTick();
  HAL_SYSTICK_IRQHandler();
}

I have tried with the internal oscillator (calibrated to 8MHz) as PLL source, and without PLL. Also tried with different PLLMUL and PLLDIV values. The results are all the same, core is halted after programming.

I do not have any breakpoints, I do not even use debug session, I just simply download the hex file to the controller. The voltage on the reset pin is 2.96V.

I am flashing the device with ST-LINK_CLI, arguments are the following:

-c SWD -p  ${project_name}.hex  -Rst -Run

The -Run always fails as the core is halted.

I am out of ideas, I do not know what can be the problem here. Something is missing from the HAL project, but I do not know what since CubeMX should generate all necessary initialization.

Update

I was able to successfully program the MCU using SPL library so the problem must be HAL library specific. The programmer/debugger should be OK.

Best Answer

Have you seen this settings page? This is why my debugger halts before debugging:

enter image description here