PROGMEM question

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

I have a table that I've put in program memory, the table looks like

const byte ext_seq[] PROGMEM = { 0x00 0x01 0x02 0x03 0x00 0x01 0x02 0x03 ....... };

the C data structure

typedef struct MyStruct
{
   byte field1;
   byte field2;
   word field3;
} my_struct;

byte is defined as unsigned char and word is unsigned short
so as can be seen the data structure is repeated in memory every 4 bytes.
Now my question if I store a pointer to the start of the data so;

byte* ptr = ext_seq;

and increment the pointer;
ptr += sizeof(my_struct);

and access the data;

my_struct ms;
memcpy_P(&ms, ptr, sizeof(my_struct));

is this correct or am I missing something?

I've put identical tables in regular memory and one in progmem and the one in reg. mem. works but the one in promem goes off into lala land. When I run it in debugger works fine but not in -Os. Went back and added static volatile to all appropriate variable with no success.
Got me stumped.

Any help appreciated

Happy Trails,

Mike

JaxCoder.com

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

Have you read the PROGMEM tutorial?

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

Very good tutorial!
Yes I did read it and what I've got looks good that's why I don't understand why it's not working.
I'm at the point where I'm putting trace msgs at strategic points. Just posted to see if anyone could tell me if there was a nuance or if I was just plain wrong in my thinking.

Thamks

Happy Trails,

Mike

JaxCoder.com

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

I think you're skipping over some important information in the tutorial. Check out the avr-libc manual and re-read the tutorial [ pgm_read_byte() ].

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

Just out of interest, why define the data as a stream of bytes in the first place? Why not an array of struct?

typedef struct 
{
   byte field1;
   byte field2;
   word field3;
} my_struct; 

const my_struct ext_seq[] PROGMEM = { 
  ( 0x00, 0x01, 0x0302 },
  { 0x03, 0x00, 0x0201 },
  { 0x02, 0x03 ....... }
}; 

my_struct ms;

memcpy_P(&ms, &ext_seq[N], sizeof(ms));

BTW I edited [code] tags into the original post to make it more readable and I'll move this to GCC Forum as it's specifically about GCC.

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

atomicdog
Thanks for your help I will go back and reread the tutorials.

Clawson
The information I provided was simplified greatly. The actual data stored is not a repeating 4 byte sequence as I provided but figured the mechanics would be the same.
What I actually have is more like the following;

typedef struct
{
    byte       value;
    byte       mask;
    word       time_offset;
} sequence_item;

typedef struct
{
     word	external_sequence_id;
     word	item_cnt;
     sequence_item* pItem;
} external_sequence;

I copy the external_sequence_id and item_cnt fro structure in PROGMEM and pItem is the pointer I maintain to the data in PROGMEM.

Also thanks for cleaning up formatting and moving to appropriate forum.

Happy Trails,

Mike

JaxCoder.com

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

Quote:

     sequence_item* pItem;

Quote:
pItem is the pointer I maintain to the data in PROGMEM.

Quote:
[...]and increment the pointer;
ptr += sizeof(my_struct);

Okay, so if you are doing

xxx.pItem += sizeof(sequence_item); 

then your problem is called pointer arithmetics and has nothing to do with PROGMEM.

JW

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

Agreed....I'm embarrassed to say that's all it was, my logic was correct but my math not. Do I get partial credit. :)

Thanks all

Happy Trails,

Mike

JaxCoder.com

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

mike32217 wrote:
Do I get partial credit?
A friend of a friend was in an electrical engineering program but wanted to take a tensors course (no clue why - maybe he was a masochist). The only such course being taught when he wanted to take it was out of the civil engineering department.

On the first day of class he found the instructor was a wizened Japanese gentleman with moderate mastery of the English language. The isntructor started talking about grading on exams:

"90 and above, A. 80 to 89, B. 70 to 79, C. ...." After the familiar litany, the friend raised his hand and asked, "What about partial credit?"

The instructor replied, "You build bridge. Bridge fall down. No partial credit."

:twisted:

Stu

Engineering seems to boil down to: Cheap. Fast. Good. Choose two. Sometimes choose only one.

Newbie? Be sure to read the thread Newbie? Start here!

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

stu_san,

HaHaHa very good story.
It's odd but I had a PhD as a supervisor once that didn't know anything about programming but he was the best person to get get help from. Although he knew nothing about the programming aspect he knew what my end goal was and he knew how to ask question that would make me answer my own.
And he had patience which is a good thing with me because I have A.A.A.D.D. (Advanced Age Attention Deficit Disorder) :)

Thanks for the story
Mike

Happy Trails,

Mike

JaxCoder.com