cancel
Showing results for 
Search instead for 
Did you mean: 

STM32L432 - User Data Flash Write

Nolan L
Associate III
Posted on February 10, 2017 at 18:44

Hi,

I am new to this forum so bearwith 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 onthe 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!

10 REPLIES 10
Posted on September 28, 2017 at 19:14

found this little gem in the hall init

  /* Use systick as time base source and configure 1ms tick (default clock after Reset is HSI) */

  HAL_InitTick(TICK_INT_PRIORITY);

I was able to disable it using

SysTick->CTRL &= ~0x00000002;

and re enabling it

HAL_InitTick(TICK_INT_PRIORITY);

all working well now.