Massimo M
Massimo M

Reputation: 11

How to share a single segment of flash memory between two programs on an STM32 microcontroller

I'm writing an embedded program, which is divided in two parts: bootloader and app. (I'm targeting the STM32 and doing my development in C using the Eclipse IDE.)

I'm using a display, so I wrote some functions and 3 different fonts. The idea is to use a sector of the microcontroller and share it. The font area is defined with a linker (ld) script like so:

 .mySegment start_of_FONT_segm : {KEEP(*(.Courier_New_Bold_20_Section))}
 .mySegment1 0x8011298 : {KEEP(*(.Terminal6x8_Section))}

Then, I use an array to write in:

const unsigned char __attribute__((section (".Terminal6x8_Section"))) Terminal6x8[] = {
   0x00,
   0x00,
   ...

But how do I read it from another program (in this case, the application)?

I tried with:

unsigned char *Terminal6x8 = (volatile unsigned char*)0x08011298;

but the compiler puts the Terminal6x8 into RAM.

I'll be glad to share some functions also, but I don't know how to declare those in ld and C syntax, either.

Upvotes: 1

Views: 763

Answers (2)

0___________
0___________

Reputation: 67476

The following line of code:

unsigned char *Terminal6x8 = (volatile unsigned char*)0x08011298;

is bad for many different reasons:

  1. It uses a fixed (hard-coded) address, which is not very good.
  2. It declares the data as volatile, which makes no sense in this context.
  3. It does not declare the data as constant.

If you want the pointer to be also placed in the flash memory, you should write:

const unsigned char * const Terminal6x8 = (const unsigned char * const) 0x08011298;

I'll be glad to share some functions also, but don't know how to declare in ld and C syntax too.

The proper way to do it is to declare a vector table (i.e., a table of pointers) containing the pointers to the data and the functions you want to share between the flash segments.

Upvotes: 1

Klas-Kenny
Klas-Kenny

Reputation: 259

Indeed the pointer *Terminal6x8 is put into RAM. If you want the pointer stored in flash, declare it const.

Although, the data pointed to by *Terminal6x8 is stored in flash at address 0x08011298 as you want. No matter where the pointer is stored.

Upvotes: 0

Related Questions