I’m learning a lot programming in AVR C. There’s are a few subtleties to watch out for, and some of them have had me banging my head against the wall for days. This post is an attempt to prevent anyone else playing with AVR outside of AVR Studio (I’m using Linux and the command line) from having the same issues.
Sections for avr-objcopy
When you compile an AVR C program, you get object code, which you have to convert into HEX format to flash onto the chip. You do this using avr-objcopy, and there are apparently several valid ways of doing it. I recommend learning about what the -j and -R flags are doing when you come across tutorials with this command in them. I was using one from a tutorial that looked like this:
avr-objcopy -j .text -O ihex some.o some.hex
That’s all fine and dandy except it only works for programs that don’t end up using the .data section of the object code! Now, this wouldn’t be a big deal if there were any warnings about this, but there aren’t – you just have to know to add a -j .data. A better solution, posted here by clawson, is to use the -R flag to remove parts you know you do not want. Here’s my current strategy:
avr-objcopy -R .fuse -R .lock -R .eeprom some.o some.hex
This fixed 2 separate problems when I did it, one with struct initialization and one with my LCD displaying block characters for strings but working for single characters.