Locating a struct at a fixed RAM location

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

I use a structure to communicate between my application and the bootloader.  I won't go into all the reasons why they need to communicate but one reason is so that the application can notify the bootloader that he wants to be replaced by a new firmware image (which will be loaded across the CAN bus).

 

In both the bootloader and application I currently define the area thus:

__attribute__((address(RAMEND-sizeof(BootInterface_t))))
char __stack[1];

#define BootInterface (*(BootInterface_t*) &__stack[1])

and access it thus:

		//
		//	Initialize values used by the application
		//
		BootInterface.Contents.mcucsr = Saved_MCUSR;
		BootInterface.Contents.RestartWasForced = false;
		BootInterface_SetRestartInfo( RM_Processor, RC_PowerOn_Init, 0 );

This works fine for the purpose.  On an ATMega1284p the structure gets allocated at 0x40ED (the size of the struct is 19 [0x13] bytes).

 

This seems like a bit of a kludge and there must be a better way to locate a struct at the top of RAM and locate the stack just below it.

 

The other problem is that the debugger apparently can't figure out the symbol linkage so I don't have symbolic access to it (debugger says "BootInterface" is unknown).

 

I tried this awhile ago but it didn't work (I forget the exact reasons):

BootInterface_t BootInterface __attribute__((address(RAMEND-sizeof(BootInterface_t))));

Thanks in advance ....

 

Regards,

 

Chuck Hackett

This topic has a solution.
Last Edited: Thu. Dec 26, 2019 - 04:09 PM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

The other way would be to put it in a named section, and modify the linker script to place it below the stack...
.
What does the studio debugger show? It shouldn't have an issue with that placement ...

:: Morten

 

(yes, I work for Microchip, yes, I do this in my spare time, now stop sending PMs)

 

The postings on this site are my own and do not represent Microchip’s positions, strategies, or opinions.

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

meolsen wrote:
The other way would be to put it in a named section, and modify the linker script to place it below the stack...

 

Maybe we are not talking the same "below".  I need this structure at the end (high address) of RAM and the stack growing immediately 'below' it - i.e.: starting at the location just below the structure and growing down - where "below" and "down" are towards lower addresses. 

 

If the linker script can't be modified by using Atmel Studio project "properties" I'd rather not modify the generated script.

 

meolsen wrote:
What does the studio debugger show? It shouldn't have an issue with that placement ...

 

The debugger says:

        BootInterface:  Unknown identifier
 

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

I wanted to do something very similar and struggled like you are doing here.

I ended up simply calling my boot-loader just like it was a regular function with parameters to control the operation mode.

 

These were the essentials:

void BootloaderJump (void)
{
    void (*bootFn) (bool shouldAwaitConnection);

	bootFn = (void (*) (bool))(0x7c0c / 2);
	bootFn(true);
}

 

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

        BootInterface:  Unknown identifier

Ah, that's because you're using a #define for it, so it is not a symbol... 

 

Either, make it a real variable, or watch the expression itself, 

(*(BootInterface_t*) &__stack[1])

Or, change the definition of __stack to not be a char, but 

__attribute__((address(RAMEND-sizeof(BootInterface_t))))
BootInterface_t BootInterface;

or, compile with -g3 under debugging, which normally adds information to the .elf file about how to interpret defines...

:: Morten

 

(yes, I work for Microchip, yes, I do this in my spare time, now stop sending PMs)

 

The postings on this site are my own and do not represent Microchip’s positions, strategies, or opinions.

This reply has been marked as the solution. 
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

meolsen wrote:

__attribute__((address(RAMEND-sizeof(BootInterface_t))))
BootInterface_t BootInterface;

 

Thanks!  That worked great!  Wish I had pursued this months ago smiley

 

I had to make one small change to get it located at the right spot (added 1 to address):

__attribute__((address(RAMEND-sizeof(BootInterface_t)+1)))
BootInterface_t BootInterface;

Thanks much!

 

Regards,

 

Chuck Hackett

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

Why would you want it in RAM?