AnsweredAssumed Answered

STM32L432 - User Data Flash Write

Question asked by Nolan L on Feb 10, 2017
Latest reply on Sep 28, 2017 by sean green

Hi,

I am new to this forum so bear with me.

We are attempting to store user data in flash on STM32L432 for things such as calibration values for different sensors or CAN ID's so that these values don't need to be hard coded. This would ideally be done through UART.

 

The issue I have is that my write to flash hangs on the following line if I place the Flash_write() in a lower part of main():

     if(HAL_FLASHEx_Erase(&EraseInitStruct, &PAGEError) != HAL_OK)

The only difference from where it is successfully being written to flash (function call near top of main() ) and when it fails (function call near bottom of main() ) is a bunch of initialization functions (ADC, DMA, GPIO, I2C, Timers, etc.).

 

Is there something that is happening in-between that time that I am not accounting for that may hang on HAL_Flash_ex_Erase()?

 

The following steps are shown to highlight what has been done to successfully write to flash:

Modified Linker Script

 

/* Specify the memory areas */
MEMORY
{
   RAM (xrw) : ORIGIN = 0x20000000, LENGTH = 48K
   RAM2 (xrw) : ORIGIN = 0x10000000, LENGTH = 16K
   FLASH (rx) : ORIGIN = 0x8000000, LENGTH = 255K
   DATA (rwx) : ORIGIN = 0x0803F800, LENGTH = 2K /*User Defined Data*/
}
/* Define output sections */
SECTIONS
{
.user_data :
{
   . = ALIGN(4);
   *(.user_data)
   . = ALIGN(4);
} > DATA

Source Code in C to Write to Flash

#include "stm32l4xx_hal.h"

#include "modules/flash/inc/flash.h"

const uint8_t userData[2048] __attribute__((section(".user_data")));

void FLASH_write(FLASH_CONTENT_e content, uint64_t data)
{
     FLASH_EraseInitTypeDef EraseInitStruct;
     uint32_t FirstPage = 0, BankNumber = 0, NbOfPages = 0, PAGEError = 0;

     HAL_FLASH_Unlock();

     __HAL_FLASH_CLEAR_FLAG(FLASH_FLAG_OPTVERR);

     switch(content)
     {
     case FLASH_CAN_ID_e:
          FirstPage = 127;
          NbOfPages = 1;
          BankNumber = FLASH_BANK_1;
          break;
     default:
          HAL_FLASH_Lock();
          return;
     }

     EraseInitStruct.TypeErase   = FLASH_TYPEERASE_PAGES;
     EraseInitStruct.Banks       = BankNumber;
     EraseInitStruct.Page        = FirstPage;
     EraseInitStruct.NbPages     = NbOfPages;

     if(HAL_FLASHEx_Erase(&EraseInitStruct, &PAGEError) != HAL_OK)
     {
          //TODO: REMOVE and handle check properly
          while(1);
     }

     if (HAL_FLASH_Program(FLASH_TYPEPROGRAM_DOUBLEWORD, (uint32_t)&userData[0], data) != HAL_OK)
     {
          //TODO: REMOVE and handle check properly
          while(1);
     }
     HAL_FLASH_Lock();
}

 

Main Call Function

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

  /* Configure the system clock */
  SystemClock_Config();

  FLASH_write(FLASH_CAN_ID_e, 0xABCDEFAB3DEFABCE);

 

Notes:

I am using FreeRTOS in this system!

Outcomes