In order to use a custom section, you must define its start address in a custom linker script. Copy your device's linker script and add the new section into its SECTIONS
block:
/* in custom.gld */
mysection 0x2000 :
{
*(mysection);
} >data
To make your objects go into this section, use the section attribute:
/* mycode1.c: */
#include "mystruct.h"
mystruct __attribute__((section("mysection"))) instance1 = { 1, 2 };
/* mycode2.c: */
#include "mystruct.h"
mystruct __attribute__((section("mysection"))) instance2 = { 3, 4 };
Now, to get the boundaries of your custom section, you can use the .startof.(section_name)
and .sizeof.(section_name)
assembler operators:
#include "mystruct.h"
char *mysection_start;
char *mysection_end;
size_t mysection_size;
int main(void)
{
asm("mov #.startof.(mysection), W12");
asm("mov #.sizeof.(mysection), W13");
asm("mov W12, _mysection_start");
asm("mov W13, _mysection_size");
mysection_end = mysection_start + mysection_size;
mystruct *p = (mystruct *)mysection_start;
for ( ; (char *)p < mysection_end ; p++)
{
// do stuff using p->a and p->b
}
}