[Top][All Lists]

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: [avr-gcc-list] storing values at a spcified location in FLASH..?

From: E. Weddington
Subject: Re: [avr-gcc-list] storing values at a spcified location in FLASH..?
Date: Thu, 13 Jan 2005 10:20:55 -0700
User-agent: Mozilla Thunderbird 0.7.3 (Windows/20040803)

Eivind Sivertsen wrote:

Hi guys,

Hi Eivind!

Are things calming down now for you? :-)

I am looking for a way to store values at specified locations in flash, for
instance calibration values, serial number...etc...
I know that other compilers may use preprocessor directives to do this, but
I cant find the combination that does the trick with WinAVR...
The solution below seems to just add the data at the flash location wherever
code ends.



// Test for defining location of var in flash
// 1. Declare the array as below
// 2. Add the following link flags:
// -Wl,--section-start=.calib=0x100,--cref
// 3. Generate Intel Hex file using avr-objcopy
// Use the following flags:
// -j .text -j .data -j .calib

#include <avr/io.h>
#include <avr/pgmspace.h>

const unsigned char calval[] __attribute__ ((section (".calib"))) = {0xcd,

int main(void){
unsigned char *calvalP;

calvalP = calval;

}// end

Actually you're very close.

Your declaration of calval with the attribute is correct.

You need to change your main() code to below:

int main(void)
   unsigned char mem_calval[2];

   mem_calval[0] = (unsigned char)pgm_read_byte(&(calval[0]));
   mem_calval[1] = (unsigned char)pgm_read_byte(&(calval[1]));


Remember that you are putting calval into the Program Memory, so the only way to read the value from there is to use the Program Space routines such as pgm_read_byte(). The argument is the *program space address* that you are reading from, hence: &(calval[0]). Technically you can also use pgm_read_word() to read out a word value. You can also use memcpy_P() to copy any size block of data from Program Memory to a RAM-based buffer.

I *highly* recommend that you use the WinAVR Makefile template (or MFile). That way you don't have to be concerned about the commands to generate the hex file.

Put this line in your Makefile, after the group of LDFLAGS variable settings:
LDFLAGS += -Wl,--section-start=.calib=0x100
Of course, change your address to the one desired.

After you build your software, open up the .map file and you can see the .calib section at the correct address. Then open up the .hex file and you can even see the correct values at the correct address.


PS. Many thanks to you and Erlend for getting the Freaks website back up!

reply via email to

[Prev in Thread] Current Thread [Next in Thread]