Struct in flash....

Go To Last Post
7 posts / 0 new
Author
Message
#1
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Hi all,

 

I'm trying to setup a structure array in flash, using  __flash but am running into problems getting it to compile :

 

Code snippet to illustrate the problem.

const char Name8020[] PROGMEM = "8020";
const __flash char Name8021[] = "8021";
const __flash char Name8022[] = "8022";

typedef struct 
{
	char		*ChipName;		// Name of chip
	uint16_t	MaxAddr;		// Max rom address
} chipmodel_t;

const __flash chipmodel_t Mod8020 = { (char *)Name8020, 1024 };
const __flash chipmodel_t Mod8021 = { (char *)Name8021, 1024 };
const __flash chipmodel_t Mod8022 = { (char *)Name8022, 2048 };

 

I get no errors for Mod8020, which has Name8020 defined the old way with PROGMEM, but Mod8021 complains that it's not constant.

 

Compiling C: Dump804x.c
avr-gcc -c -mmcu=atmega2560 -I. -gdwarf-2 -DF_CPU=16000000UL -DSERIAL_STATUS -DCOOKED_SERIAL -DNOUSART1 -DSTDIOIN -DUSE_HEXDUMP  -Os -funsigned-char -funsigned-bitfields -fpack-struct -fshort-enums -Wall -Wstrict-prototypes -Wa,-adhlns=./Dump804x.lst  -std=gnu99 -MMD -MP -MF .dep/Dump804x.o.d Dump804x.c -o Dump804x.o 
Dump804x.c:50:1: error: initializer element is not constant
 const __flash chipmodel_t Mod8021 = { (char *)Name8021, 1024 };
 ^
Dump804x.c:50:1: error: (near initialization for 'Mod8021.ChipName')
Dump804x.c:51:1: error: initializer element is not constant
 const __flash chipmodel_t Mod8022 = { (char *)Name8022, 2048 };
 ^
Dump804x.c:51:1: error: (near initialization for 'Mod8022.ChipName')
make: *** [Dump804x.o] Error 1

> Process Exit Code: 2

 

I'm obviously doing something wrong but can't figure out what, as the strings are defined const.

 

Cheers.

 

Phill.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0
typedef struct 
{
	char		*ChipName;		// Name of chip
	uint16_t	MaxAddr;		// Max rom address
} chipmodel_t;

->

typedef struct 
{
	const char	*ChipName;		// Name of chip
	uint16_t	MaxAddr;		// Max rom address
} chipmodel_t;

And forget about the type casts in the initialization.

Stefan Ernst

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Ok I've changed it to

// Chip model constructs
const __flash char Name8020[] = "8020";
const __flash char Name8021[] = "8021";
const __flash char Name8022[] = "8022";

typedef struct 
{
	const char	*ChipName;		// Name of chip
	uint16_t	MaxAddr;		// Max rom address
} chipmodel_t;

const __flash chipmodel_t Mod8020 = { Name8020, 1024 };
const __flash chipmodel_t Mod8021 = { Name8021, 1024 };
const __flash chipmodel_t Mod8022 = { Name8022, 2048 };

 

And now getting :

 

avr-gcc -c -mmcu=atmega2560 -I. -gdwarf-2 -DF_CPU=16000000UL -DSERIAL_STATUS -DCOOKED_SERIAL -DNOUSART1 -DSTDIOIN -DUSE_HEXDUMP  -Os -funsigned-char -funsigned-bitfields -fpack-struct -fshort-enums -Wall -Wstrict-prototypes -Wa,-adhlns=./Dump804x.lst  -std=gnu99 -MMD -MP -MF .dep/Dump804x.o.d Dump804x.c -o Dump804x.o 
Dump804x.c:49:1: error: initializer element is not computable at load time
 const __flash chipmodel_t Mod8020 = { Name8020, 1024 };
 ^
Dump804x.c:49:1: error: (near initialization for 'Mod8020.ChipName')
Dump804x.c:50:1: error: initializer element is not computable at load time
 const __flash chipmodel_t Mod8021 = { Name8021, 1024 };
 ^
Dump804x.c:50:1: error: (near initialization for 'Mod8021.ChipName')
Dump804x.c:51:1: error: initializer element is not computable at load time
 const __flash chipmodel_t Mod8022 = { Name8022, 2048 };
 ^
Dump804x.c:51:1: error: (near initialization for 'Mod8022.ChipName')
make: *** [Dump804x.o] Error 1

Cheers.

 

Phill.

 

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Sorry, I forgot the __flash.

	const __flash char	*ChipName;		// Name of chip

 

Stefan Ernst

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

sternst wrote:

Sorry, I forgot the __flash.

	const __flash char	*ChipName;		// Name of chip

 

 

Bingo, that's done it

 

Thanks very much for the speedy reply :)

 

Cheers.

 

Phill.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Missing one __flash.

EDIT : oops, too late!

Last Edited: Fri. May 5, 2017 - 02:23 PM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

BTW: When using __flash (instead of PROGMEM) you don't really need the separate string definitions.

#define FSTR(X) ((const __flash char[]){X})

const __flash chipmodel_t Mod8020 = { FSTR("8020"), 1024 };
const __flash chipmodel_t Mod8021 = { FSTR("8021"), 1024 };
const __flash chipmodel_t Mod8022 = { FSTR("8022"), 2048 };

 

Stefan Ernst