cancel
Showing results for 
Search instead for 
Did you mean: 

STM32L452CE CubeIDE default settings causes HardFault when setting flash latency

jeremyh
Associate II

Hi,

I have created a project using CubeIDE 1.14.0 for the STM32L452CE. Inside the IOC editor, I changed the following things:

  • SYS => Debug set to Serial Wire
  • Project Manager => Advanced Settings => Set both RCC and GPIO to use LL instead of HAL

I then generated the code and fixed the broken linker script as outlined here: https://community.st.com/t5/stm32cubeide-mcus/stm32l452ce-cubeide-linker-script-causes-hardfault-inside-reset/m-p/651426#M25207

When I build and flash the code, I get a hardfault when executing the line:

 

LL_FLASH_SetLatency(LL_FLASH_LATENCY_0);

 

The full code is below, but it is just the default code that is generated...

 

 

 

/* USER CODE BEGIN Header */
/**
  ******************************************************************************
  * @file           : main.c
  * @brief          : Main program body
  ******************************************************************************
  * @attention
  *
  * Copyright (c) 2024 STMicroelectronics.
  * All rights reserved.
  *
  * This software is licensed under terms that can be found in the LICENSE file
  * in the root directory of this software component.
  * If no LICENSE file comes with this software, it is provided AS-IS.
  *
  ******************************************************************************
  */
/* USER CODE END Header */
/* Includes ------------------------------------------------------------------*/
#include "main.h"

/* Private includes ----------------------------------------------------------*/
/* USER CODE BEGIN Includes */

/* 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 ---------------------------------------------------------*/

/* USER CODE BEGIN PV */

/* USER CODE END PV */

/* Private function prototypes -----------------------------------------------*/
void SystemClock_Config(void);
static void MX_GPIO_Init(void);
/* USER CODE BEGIN PFP */

/* USER CODE END PFP */

/* Private user code ---------------------------------------------------------*/
/* USER CODE BEGIN 0 */

/* 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. */
  LL_APB2_GRP1_EnableClock(LL_APB2_GRP1_PERIPH_SYSCFG);
  LL_APB1_GRP1_EnableClock(LL_APB1_GRP1_PERIPH_PWR);

  /* System interrupt init*/
  NVIC_SetPriorityGrouping(NVIC_PRIORITYGROUP_4);

  /* SysTick_IRQn interrupt configuration */
  NVIC_SetPriority(SysTick_IRQn, NVIC_EncodePriority(NVIC_GetPriorityGrouping(),15, 0));

  /* 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();
  /* USER CODE BEGIN 2 */

  /* USER CODE END 2 */

  /* Infinite loop */
  /* USER CODE BEGIN WHILE */
  while (1)
  {
    /* USER CODE END WHILE */

    /* USER CODE BEGIN 3 */
  }
  /* USER CODE END 3 */
}

/**
  * @brief System Clock Configuration
  * @retval None
  */
void SystemClock_Config(void)
{
  LL_FLASH_SetLatency(LL_FLASH_LATENCY_0);
  while(LL_FLASH_GetLatency()!= LL_FLASH_LATENCY_0)
  {
  }
  LL_PWR_SetRegulVoltageScaling(LL_PWR_REGU_VOLTAGE_SCALE1);
  while (LL_PWR_IsActiveFlag_VOS() != 0)
  {
  }
  LL_RCC_MSI_Enable();

   /* Wait till MSI is ready */
  while(LL_RCC_MSI_IsReady() != 1)
  {

  }
  LL_RCC_MSI_EnableRangeSelection();
  LL_RCC_MSI_SetRange(LL_RCC_MSIRANGE_6);
  LL_RCC_MSI_SetCalibTrimming(0);
  LL_RCC_SetSysClkSource(LL_RCC_SYS_CLKSOURCE_MSI);

   /* Wait till System clock is ready */
  while(LL_RCC_GetSysClkSource() != LL_RCC_SYS_CLKSOURCE_STATUS_MSI)
  {

  }
  LL_RCC_SetAHBPrescaler(LL_RCC_SYSCLK_DIV_1);
  LL_RCC_SetAPB1Prescaler(LL_RCC_APB1_DIV_1);
  LL_RCC_SetAPB2Prescaler(LL_RCC_APB2_DIV_1);

  LL_Init1msTick(4000000);

  LL_SetSystemCoreClock(4000000);
}

/**
  * @brief GPIO Initialization Function
  * @PAram None
  * @retval None
  */
static void MX_GPIO_Init(void)
{
/* USER CODE BEGIN MX_GPIO_Init_1 */
/* USER CODE END MX_GPIO_Init_1 */

  /* GPIO Ports Clock Enable */
  LL_AHB2_GRP1_EnableClock(LL_AHB2_GRP1_PERIPH_GPIOA);

/* USER CODE BEGIN MX_GPIO_Init_2 */
/* USER CODE END MX_GPIO_Init_2 */
}

/* USER CODE BEGIN 4 */

/* USER CODE END 4 */

/**
  * @brief  This function is executed in case of error occurrence.
  * @retval None
  */
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
/**
  * @brief  Reports the name of the source file and the source line number
  *         where the assert_param error has occurred.
  * @PAram  file: pointer to the source file name
  * @PAram  line: assert_param error line source number
  * @retval None
  */
void assert_failed(uint8_t *file, uint32_t line)
{
  /* USER CODE BEGIN 6 */
  /* User can add his own implementation to report the file name and line number,
     ex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */
  /* USER CODE END 6 */
}
#endif /* USE_FULL_ASSERT */

 

 

 

 

1 ACCEPTED SOLUTION

Accepted Solutions

I ended up resolving this today - I lifted and replaced the chip with a new one, and it worked first time. So I'm going to put this one down to the ESD boogeyman I suppose. All of the flash latency modes were failing on the old one.

View solution in original post

6 REPLIES 6
AScha.3
Principal III

So set to using HAL only - and try.

+

>

  • SYS => Debug set to Serial Wire      ok.
  • Set both RCC and GPIO to use LL instead of HAL

Why this ??

If you feel a post has answered your question, please click "Accept as Solution".

Is there something wrong with using the LL API?

IF not working...seems so. 

I try at first, to get something running , as it should. And HAL usually making this on the shortest time.

Playing around or improve speed is next - not first step. (Just my point of view - ymmv.)

If you feel a post has answered your question, please click "Accept as Solution".

I tried the HAL version, same problem. There is a hardfault at

  __HAL_FLASH_SET_LATENCY(latency);

which roughly translates to the same code.

So - dont get me wrong: i dont "love" the HAL - use the HAL (at first) and also look, is there a update, because sometimes errors in HAL are corrected (if this is an error in HAL and not some strange setup you try to start).

(Just from thinking: you are the only one, that reports this basic error, but for sure you are not the first and only, that use this cpu; so 99% this is something in your setup (hard-and software)....)

Then begin to find out, whats wrong: without new "generate code", just writing it in actual main, set latency to ( 0-> 1 , -> 2 ) and see, what comes out; then comment out this call, to check...

And check from rm, that you have a valid setting of selected supply (smps, LDO) and core voltage and speed setting.

If you feel a post has answered your question, please click "Accept as Solution".

I ended up resolving this today - I lifted and replaced the chip with a new one, and it worked first time. So I'm going to put this one down to the ESD boogeyman I suppose. All of the flash latency modes were failing on the old one.