_RODATA() works only on arrays

Showing results for 
Search instead for 
Did you mean: 

_RODATA() works only on arrays

Contributor III

#include <cr_section_macros.h>

static __RODATA(Flash_SN) const uint32_t sn_flash = 0x01020304;

It doesn't work, the linker put sn_flash variable in the main flash block (see below my memory layout). I need to write something similar to:

#include <cr_section_macros.h>

static __RODATA(Flash_SN) const uint8_t sn_flash[] = { 0x01, 0x02, 0x03, 0x04 };

What is the reason to this behaviour?

This is the memory layout in linking script auto-generated by MCUXpresso:

  /* Define each memory region */
  MFlash512 (rx) : ORIGIN = 0x4000, LENGTH = 0x7bff0 /* 507888 bytes (alias Flash) */
  Flash_SN (rx) : ORIGIN = 0x7fff0, LENGTH = 0x8 /* 8 bytes (alias Flash2) */
  Flash_MAC (rx) : ORIGIN = 0x7fff8, LENGTH = 0x8 /* 8 bytes (alias Flash3) */
  RamLoc32 (rwx) : ORIGIN = 0x10000000, LENGTH = 0x8000 /* 32K bytes (alias RAM) */
  RamAHB32 (rwx) : ORIGIN = 0x2007c000, LENGTH = 0x8000 /* 32K bytes (alias RAM2) */

MFlash512 starts from 0x4000 because I have a bootloader at the beginning of the Flash.

Labels (1)
Tags (1)
0 Kudos
1 Reply

NXP Employee
NXP Employee

This is due to the way that GCC handles rodata - basically treating your const int as a literal pool item. This means that the section you create is unused (as the literal pool is used instead by your code) and is optimised away.

If you are using an MCU based on ARM architecture v7-M core  (such as Cortex-M3/M4), rather than v6-M (such as Cortex-M0/M0+) then adding the compiler option -mpure-code should achieve what you are trying to do.


MCUXpresso IDE Support

0 Kudos