0

I currently have a C project where I have to write a specific value (0xaa) to a specific address (0x3fec0), and I would like to write it during the programming sequence. The solution I have found is the following :

I write another section in the linker file :

/* Custom flag section in linker file */
_flag_start = 0x3fec0;
.flag _flag_start :
{
    KEEP(*(.flag));
}

I write the value as a static variable :

static uint8_t flag __attribute__((section (".flag"))) __attribute__((__used__)) = 0xaa;

It works well, and I manage to write the right value at the right address. However it also generates a very heavy binary : because I set this section at 0x3fec0, my binary will be 261824 Bytes (0x3fec0 in decimal), meaning approx 255KB, as if it was filling with empty data until it reaches this address.

My question is : how to perform the same thing, but without impacting the size of the binary file ?

8
  • *((*unsigned char)0x3fec0) = 0xaaU; Commented Sep 30, 2016 at 8:42
  • Well, unfortunately that doesn't even pass the compilation process Commented Sep 30, 2016 at 8:51
  • @user1943797 *((unsigned char*)0x3fec0) = 0xaaU; works here. Commented Sep 30, 2016 at 8:58
  • This statement works in functions, but not in the global scope. Writing constant values at a specific addresses relies on the linker file. Commented Sep 30, 2016 at 9:04
  • 1
    @tofro It's ROM - read the title of the question. A program can't directly write to read-only memory. The OP is trying to configure the linker script to do that for him. Commented Sep 30, 2016 at 9:51

2 Answers 2

1

You can try to output it to another format such as Motorola s-record (srec) or intel HEX. The program you're using to flash your device may support these formats and skip the useless data between the different address ranges to speed-up the transfer.

Sign up to request clarification or add additional context in comments.

5 Comments

That indeed might be a much more simple solution. How would you create your HEX file to write a specific value at a specific address ?
@user1943797 GNU objcopy should be capable of transforming your linked binary into S-Record format. Whether you get it to ignore (exclude) the gaps, is currently beyond my knowledge. The --onlysection option might be of use here (you might need to give the gap a section name that you explicitly exclude from copying into the S-Record file).
I am thinking of the problem in another way : I want to write a specific byte at a specific address. Maybe I could just general an HEX file and program my MCU with this HEX file, to write the 0xAA at the address I specified. Would it be possible ?
@user1943797 In that case you should merge the change with the rest of the binary, so that there's only one file. Programming the same MCU several times from several hex files will only lead to problems. Editing a hex or srecord file to include an extra line should be pretty easy to do though.
When you link your program, I guess you'll output an elf file. Then you can just do a objcopy -O srec "out.elf" "out.srec" and it will output a s-rec file (or hex if you change the output type to "hex"), with the gaps excluded.
0

I've found a solution much more easy to understand, implement, and that works well.

I simply created a .bin file containing the byte "0xAA" and wrote it with my MCU-specific programmer to my device (If you want to know the specs, it's an Atmel MCU and I used the atprogram program). I wrote the content of this binary file on my MCU with the correct offset (0x3fec0) so it would be at the right place.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.