Hi,
Does anyone know how to place a variable on a exact location in ROM?
I tried the following methods:
volatile int bootStatus __attribute__((at(0xC0))) = 0x66;
int variable2 __attribute__((section(".id_firmware.__at_0xe2"))) = 10;
const int x1 __attribute__((at(0x200))) = 10; /* RO */
__attribute__ ((section(".id_firmware"))) uint32_t aap = 0x11223344;
this one doesn't write the memory on itself but when referring to it in the main on this way:
aap = aap;
it will place the data in opposite order (44332211) and the program gets a hard_fault interrupt.
memory layout in linker file is as follows:
MEMORY {
m_interrupts (RX) : ORIGIN = 0x00000000, LENGTH = 0x000000C0
my_id_firmware (RX) : ORIGIN = 0x000000c0, LENGTH = 0x00000340
m_text (RX) : ORIGIN = 0x00000410, LENGTH = 0x00002BF0
m_data (RW) : ORIGIN = 0x1FFFF000, LENGTH = 0x00004000
m_cfmprotrom (RX) : ORIGIN = 0x00000400, LENGTH = 0x00000010
}
/* Define output sections */
SECTIONS
{
/* The startup code goes first into INTERNAL_FLASH */
.interrupts :
{
__vector_table = .;
. = ALIGN(4);
KEEP(*(.vectortable)) /* Startup code */
. = ALIGN(4);
} > m_interrupts
/* Section created to relocate code in specific Flash address */
.id_firmware :
{
. = ALIGN(4);
*(.myROM)
. = ALIGN(4);
} > my_id_firmware
The only thing I want is to hardcode a few variables in ROM so that I can access them from a bootloader program as well as the actual firmware program.
I am using MKE06Z128LK4 Microcontroller
I would be really great if someone can help me!
解決済! 解決策の投稿を見る。
Hello Mark:
Sorry, actually the hardfault was easy to figure out from your first post. The code aap = aap is both a read and a write access, but you are placing this variable in flash memory space, which cannot be written with a simple C code instruction. So you need to remove that line of code.
However you are right that if the aap variable is not referenced from your code then the linker discards it. To avoid this you can use the command KEEP, like this:
/* Section created to relocate code in specific Flash address */
.id_firmware :
{
. = ALIGN(4);
KEEP(*(.id_firmware))
. = ALIGN(4);
} > my_id_firmware
Then any symbols (functions or variables) you place in the input section .id_firmware will not be optimized out.
I hope this helps you move forward.
Regards!
Jorge Gonzalez
Hello Mark de Groot:
This guide should be what you are looking for: Defining Variables at Absolute Addresses with gcc | MCU on Eclipse
There is also an extensive document about this topic here: Relocating Code and Data Using the KDS GCC Linker File for Kinetis
The correct syntax is the one you have for aap, but in the linker file the input section should be called .id_firmware instead of .myROM, like next:
.id_firmware :
{
. = ALIGN(4);
*(.id_firmware)
. = ALIGN(4);
} > my_id_firmware
Let me know if you still have issues.
Best Regards!
Jorge Gonzalez
-----------------------------------------------------------------------------------------------------------------------
Note: If this post answers your question, please click the Correct Answer button. Thank you!
-----------------------------------------------------------------------------------------------------------------------
Thank you for your answer!
Unfortunately, I still get a hard_fault interrupt when changing .myROM to .id_firmware.
also, thanks for the document, the whole thing makes a bit more sense to me now.
Hi Mark:
In your case the placement of the variables should not cause any hardfault. If you are still having this issue please share your project with me and I can give it a check.
Regards!
Jorge Gonzalez
Hello Mark:
Sorry, actually the hardfault was easy to figure out from your first post. The code aap = aap is both a read and a write access, but you are placing this variable in flash memory space, which cannot be written with a simple C code instruction. So you need to remove that line of code.
However you are right that if the aap variable is not referenced from your code then the linker discards it. To avoid this you can use the command KEEP, like this:
/* Section created to relocate code in specific Flash address */
.id_firmware :
{
. = ALIGN(4);
KEEP(*(.id_firmware))
. = ALIGN(4);
} > my_id_firmware
Then any symbols (functions or variables) you place in the input section .id_firmware will not be optimized out.
I hope this helps you move forward.
Regards!
Jorge Gonzalez
This is working!
Thank you very much for your help!