How to allocate a data segment to static address in program memory

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

Hello,

 

Making use of atmel studio 6.2 for an ATxmega128D4, I'm trying to put a data table in the program memory at a static address. I'v followed these steps:

 

1: Definition in Project properties > Toolchain > AVR/GNU Linker > Memory Settings: FLASH segment

.datasegment = 0xF800

(This is the word address as I want the byte adress at 0x1F000)

 

2: Definition of the table in the code:

  const __flash  DATA_type   DATA_table   __attribute__  ((section (".datasegment "))) = { ... }

 

 

When debugging, I notice that the table is placed at some random address in program memory, not where it is supposed to be.

 

What did I wrong or what have I forgotten?

 

Pieter

 

 

 

 

 

This topic has a solution.
Last Edited: Thu. Nov 20, 2014 - 03:51 PM
This reply has been marked as the solution. 
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

What did I wrong or what have I forgotten?

Why do you think "__flash" is involved in this? __flash is the modern replacement for "PROGMEM" and PROGMEM was effectively "__attribute__((section(".progmem.data")))". So you are telling the linker you want this data to be in both ".progmem" and ".datasegment". Whether it's because you listed __flash first or something else it looks like __flash won (and by default the linker places .progmem stuff down low, immediately after the vector table).

 

So simply try it with everything else but dropping the "__flash" from that.

 

BTW just semantics but personally I think I'd prefer ".rodatasegement" rather than just ".datasegment" as it's more documentary to the maintainer as to what your intention is.

 

Oh and you posted in the wrong forum - "Tutorials" is for gurus to post articles that aim to teach others. I think "Compilers" is a more appropriate place for this so I'll move the thread as moderator.

Last Edited: Thu. Nov 20, 2014 - 03:51 PM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Thank you for the quick response.

I used datasegment as example, it is not really written so.

 

I've tried it again with the following line:

 

const FACTORY_type FACTORY __attribute__ ((section (".factory_segment"))) = { 

 

I recieve this compilation error:

 

Error    10    conflicting named address spaces (generic vs __flash) for 'FACTORY'   

 

I suppose it conflicts with the setting in the FLASH_segment of the project properties.

How can I define it otherwise?

 

 

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

What is "FACTORY_type"? It's not a typedef with an __attribute__ attached by any chance is it?

 

I can't test this but try:
 

#include <avr/io.h>

const int foo __attribute__((section(".foo"))) = 12345;

int main(void) {
}

then at the linker stage:

-Wl,-section-start=.foo=0x3456

I would be very surprised if this didn't put 0x3039 in the flash image at location 0x3456!

 

EDIT: OK I had to try that so I switched PCs and tried:

flight@ws-czc1138h0b:/windows$ cat avr.c
#include <avr/io.h>

const int foo __attribute__((section(".foo"))) = 12345;

int main(void) {
}

flight@ws-czc1138h0b:/windows$ avr-gcc -mmcu=atmega16 -Os -g -save-temps -Wl,-section-start=.foo=0x3456 avr.c -o avr.elf
flight@ws-czc1138h0b:/windows$ avr-objcopy -j .text -j .foo -O ihex avr.elf avr.hex
flight@ws-czc1138h0b:/windows$ cat avr.hex
:100000000C942A000C9434000C9434000C943400AA
:100010000C9434000C9434000C9434000C94340090
:100020000C9434000C9434000C9434000C94340080
:100030000C9434000C9434000C9434000C94340070
:100040000C9434000C9434000C9434000C94340060
:100050000C94340011241FBECFE5D4E0DEBFCDBF29
:100060000E9436000C9437000C9400000895F89418
:02007000FFCFC0
:0234560039300B
:00000001FF

Notice the record in that .hex file that says:

:0234560039300B

That is placing 0x3039 (12345) at location 0x3456 just as planned.

Last Edited: Thu. Nov 20, 2014 - 04:08 PM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

FACTORY_type is simply defined as a struct with several other struct definitions which all end with the defintions of several unsigned chars and longs.

 

Anyway, it functions now. I was forgotten to omit the __flash in the definition that was written in the header file, this caused the Error 10. Your suggestion was directly OK.

 

Thanks a lot for your support

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

clawson wrote:
What is "FACTORY_type"? It's not a typedef with an __attribute__ attached by any chance is it?
My recollection is that attributes do not attach to typedef types.

That they used to was a bug.

Qualifiers. e.g. const, volatile and namespace qualifiers like __flash do attach to typedef types.

That, as noted later, was the problem.

International Theophysical Year seems to have been forgotten..
Anyone remember the song Jukebox Band?