Atmel AVR and memory

For the past week, I have been puzzled by a problem I’ve been having with a program I’m making using an Arduino. Tonight, I solved it.

Because the Atmel AVR processor is a Harvard architecture device, program memory (Flash) and data memory (RAM) are two separate address spaces. I wanted my giant arrays to be placed in Flash, since they are never modified. In any other compiler I’ve used for embedded development, code like the following accomplishes that:

const uint8_t array[] = { ...

However, for Arduino, you have to do something like this:

#include <avr/pgmspace.h>
const uint8_t array[] PROGMEM = { ...

And then to read from it, you need to do something like this:

uint8_t data = pgm_read_byte(&array[0]);

Thanks, Atmel and Arduino, for a purely Harvard architecture processor and annoying compiler, and a few nights of confusion. At least avr-size was useful in diagnosing the problem. Also, I should probably get around to reading the AVR datasheet!

Leave a comment

Your email address will not be published. Required fields are marked *