Putting data in a specific section in Flash

Go To Last Post
7 posts / 0 new
Author
Message
#1
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Hello,

 

I’m currently stuck on a topic with a M0 based ATSAM HA0 microcontroller.

I’m trying to place a value at a given address in the Flash (note : this value is GIT credential automatically generated during the compilation, to simplify my explanations a consider the variable “m”=1,2,3......).

 

I’ve tried several ways to do it, each time I’ve got a different problem :

 

static uint8_t __attribute__((__section__(".id_section")))  m[9] = {1;2 ;3 ;4 ;5 ;6 ;7 ;8 ;9}

  • It puts the value in the section at good adress (I checked the BIN), but sometimes I get an hardfault reset at the startup.

 

 

const uint8_t __attribute__((__section__(".id_section")))  m[9] = {1;2 ;3 ;4 ;5 ;6 ;7 ;8 ;9}

  • It puts the value in the section (I checked the BIN), but changes some other data at other adresses in the output bin => Why ?
  • It uses 4 bytes less flash, and 4 bytes less ram (not sure why)

 

Compilation in GCC is made with optimisations activated (o1).

In case 1) where I can have a hardfault reset at startup, in some cases if I made small changes in the code like adding a variable, removing a line.... the hardfault reset will disappear. (And may reappear the next time I made changes in the code)

Also, when putting the beginning of the custom section not at address 0x7FF4, but at address 0x7FF0, the hardfault reset seems also to disappear (see .ld file below for adresses)

 

 

For information, here is the custom section in the ld file :

 

MEMORY

{

  rom      (rx)  : ORIGIN = 0x00000000, LENGTH = 0x00007FF4

  custom_rom   (r)  : ORIGIN = 0x00007FF4, LENGTH = 0x0000000C

  ram      (rwx) : ORIGIN = 0x20000000, LENGTH = 0x00001000

}

 

.id_section :

       {

        . = ALIGN(4);

        _s_id_section = .;          /* start of mysection */

       KEEP(*(.id_section*));

        . = ALIGN(4);

        _e_id_section = .;          /* End of mysection */

        } > custom_rom

 

I hope someone could explain me the best way to do it....

Thank you very much

This topic has a solution.
Last Edited: Tue. Nov 2, 2021 - 10:57 AM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

lludo wrote:
It uses 4 bytes less flash, and 4 bytes less ram (not sure why)

if it's purely in Flash, then there's no need to copy to RAM ?

 

lludo wrote:
In case 1) where I can have a hardfault reset at startup, in some cases if I made small changes in the code like adding a variable, removing a line.... the hardfault reset will disappear. (And may reappear the next time I made changes in the code)

doesn't sound like it's really related to (just) this variable.

 

You need to find what's actually causing the HardFault

 

https://community.arm.com/developer/ip-products/system/f/embedded-forum/3257/debugging-a-cortex-m0-hard-fault

 

https://www.embedded.com/debugging-hard-faults-in-arm-cortex-m0-based-socs/

Top Tips:

  1. How to properly post source code - see: https://www.avrfreaks.net/comment... - also how to properly include images/pictures
  2. "Garbage" characters on a serial terminal are (almost?) invariably due to wrong baud rate - see: https://learn.sparkfun.com/tutorials/serial-communication
  3. Wrong baud rate is usually due to not running at the speed you thought; check by blinking a LED to see if you get the speed you expected
  4. Difference between a crystal, and a crystal oscillatorhttps://www.avrfreaks.net/comment...
  5. When your question is resolved, mark the solution: https://www.avrfreaks.net/comment...
  6. Beginner's "Getting Started" tips: https://www.avrfreaks.net/comment...
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

I need access in both Flash and RAM, but I would like that at each compilation, there is only a specific section of the BIN which change. The customized section which I placed at the end of the flash section. Everything else should remain the same.

 

You will ask me why : its simple. I'm using it in order to store GIT information such as Tag number and hash number inside the code. But I need to be certain that after a merge (for example after a merge to master branch), the new code beeing compilated is exactly the same than the code being tested (exception being the few bytes used to store the GIT information).

 

 

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

lludo wrote:
I need access in both Flash and RAM

What do you mean by that?

 

You will ask me why : its simple. I'm using it in order to store GIT information such as Tag number and hash number inside the code. But I need to be certain that after a merge (for example after a merge to master branch), the new code beeing compilated is exactly the same than the code being tested (exception being the few bytes used to store the GIT information).

That's a common enough requirement - but why does it need to be in a specific section?

 

Surely, so long as it's somewhere in Flash you can either inspect it in the debugger, or "print" it to some debug stream or display - none of which require it to be in a specific sction.

Top Tips:

  1. How to properly post source code - see: https://www.avrfreaks.net/comment... - also how to properly include images/pictures
  2. "Garbage" characters on a serial terminal are (almost?) invariably due to wrong baud rate - see: https://learn.sparkfun.com/tutorials/serial-communication
  3. Wrong baud rate is usually due to not running at the speed you thought; check by blinking a LED to see if you get the speed you expected
  4. Difference between a crystal, and a crystal oscillatorhttps://www.avrfreaks.net/comment...
  5. When your question is resolved, mark the solution: https://www.avrfreaks.net/comment...
  6. Beginner's "Getting Started" tips: https://www.avrfreaks.net/comment...
This reply has been marked as the solution. 
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 1

most hard faults are from missalignment of memory...

try an alignment of 8 bytes, maybe this helps.

It is not so trivial to check what the compiler exactly does in your case:

As you said the const variant is different from the static one. What may happen is that your array is stored in your section,

but the pointer to your array is not located there. Due to its static behaviour it may be initialized first by 0 and afterwards by the pointer value pointing to your array. (so it needs a bit more memory)

Maybe the missalignment happens here. (https://infocenter.nordicsemi.co... ;

Unaligned read/write Cortex-M4F allows unaligned reads/writes, but on Cortex-M0 this leads to a HardFault. [....] In Cortex-M4F the default stack alignment is 4 bytes, while in Cortex-M0 it is 8 bytes.  )

 

So try changing to 8 byte alignment and fit the length to 8 byte alignment, this may help.

 

Flo1991

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Hello,

After additional tests and tries those last days, the problem seems solved with the 8 bytes alignment.

I had to wait a little bit before confirmation... I already observed this behavior, but I'm very happy that someone managed to find a written explanation somewhere :)

 

Thank you very much

 

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

lludo wrote:
the problem seems solved with the 8 bytes alignment.

Good to hear. Please mark the solution - see Tip #5 in my signature, below:

Top Tips:

  1. How to properly post source code - see: https://www.avrfreaks.net/comment... - also how to properly include images/pictures
  2. "Garbage" characters on a serial terminal are (almost?) invariably due to wrong baud rate - see: https://learn.sparkfun.com/tutorials/serial-communication
  3. Wrong baud rate is usually due to not running at the speed you thought; check by blinking a LED to see if you get the speed you expected
  4. Difference between a crystal, and a crystal oscillatorhttps://www.avrfreaks.net/comment...
  5. When your question is resolved, mark the solution: https://www.avrfreaks.net/comment...
  6. Beginner's "Getting Started" tips: https://www.avrfreaks.net/comment...