JaffaMicrobrain
JaffaMicrobrain

Reputation: 71

How to create array in FLASH that can be overwritten with code? Using STM32CubeIDE 1.9.0

I am fairly new to STM32 but worked on Microchip MPASM and C for a long time.

My code declares a two dimensional array in an include file to be stored in FLASH. The array is referenced within my application during run time. It works fine. However, I need the ability to write over the existing FLASH array occasionally.

I understand the principals of STM32 erase/write cycles and have HAL based routines to do that. However, I don't know how to declare my original array to start at a sector start address. My research has uncovered conflicting suggestions and I am now confused.

I assume that I must use a compiler directive (as I have done many times with Microchip projects) such as:

__attribute__ ((section ("FLASH_SECTOR_11))) ArrayRecord_t FlashArray[] {
/* Here I define my array records */
{1, 1600},
{10, 3200},
etc
};

Is there a way to do this? Am I barking up the wrong tree?

Thanks.

Upvotes: 0

Views: 1122

Answers (1)

JaffaMicrobrain
JaffaMicrobrain

Reputation: 71

So, I found the answer:

The multi-dimension array must be declared using a linker table token, then the token must be defined in the linker file. For example:

static const MyArrayRecord_t MyArray[] __attribute__ ((section(".MySectionName"))) = {
{10, 100},
{20, 112},
{31, 156}
};

Then in the linker file (Eg: MyProject_FLASH.ld) add the corresponding section definition:

.MySectionName 0x8060000 :
{
. = ALIGN(4);
KEEP(*(.MySectionName))
. = ALIGN(4);
} > FLASH

On the chip I am using the address 0x8060000 corresponds to the start of a FLASH segment address. To overwrite this FLASH array it is first necessary to unlock FLASH writes (see manual or HAL_FLASH_Unlock()). Then erase the entire sector. After that the FLASH array can be overwritten. Note that this assumes the FLASH array is smaller than the FLASH segment.

Within your code the FLASH array can be referenced just the same as a RAM array.

HTH

Upvotes: 2

Related Questions