Flash Data

Using Flash memory with AVRGCC

Like a number of microcontrollers, the AVR carries its code in non-volatile flash memory and has a relatively limited amount of available RAM.

Data in your program which is constant, such as tables and strings, are most conveniently located in flash. Some compilers recognise the standard C reserved word const and place the corresponding data in flash.

Unfortunately, this is not the case for GCCAVR. The AVR needs special instructions to access code memory so normal data access will not work with items located in flash. The compiler does in fact place the data in flash but then copies it to RAM at program startup. You can imagine that this can result in a rapid consumption of available RAM and unnecessary duplication of the data.

An extension to the language in GCC allows the programmer to specify that data items are to live in flash and for the compiler to generate the special code needed to access them. These directives are properly described in the avr-libc documentation. Here are some examples of their use:

Create a type that describes a data structure to be held in flash:

typedef struct
{
  uint8_t length;
  uint8_t width;
} PROGMEM const sBox_t;

This has the same effect:

typedef struct PROGMEM
{
  uint8_t length;
  uint8_t width;
} const sBox_t;

Simple variables:

uint16_t  datavalue PROGMEM = 0xffe4;
uint8_t   string[] PROGMEM = "harry";

 

Comments are closed.