Flashticles

I am now following the Learning Tutorials and Reference information concerning reading and writing to Flash. I tried the sample code provided and have unsatisfactory results. Here is the code:

#include <avr/pgmspace.h>

// save some unsigned ints

const int test1[]  PROGMEM = { 2, 121, 111, 65535};
/* int test1; */
int displayInt;
int k = 0;    // counter variable

void setup() {
  Serial.begin(9600);
  
  // read back a 2-byte int
  for (k = 0; k < 4; k++)
  {
    displayInt = pgm_read_word_near(test1 + k);
    Serial.println(displayInt);
  }
  } 

void loop() {
}

When I compile and run I get what appears to be the correct result of reading the flash. To test further I eliminate the write step: const int test1 PROGMEM = { 2, 121, 111, 65535}; and add: int test1; to define the variable independent of the PROGMEM line. The result is numbers apparently unrelated to the original entered using the PROGMEM line. I suspect the program is reading back at different addresses because the read data is unaffected by the original PROGMEM line data values. If this is true, how do I define the read addresses?

I have not found any help in the tutorials or Reference yet.

If this is true, how do I define the read addresses?

You don't need to. The compiler KNOWS where it allocated memory for the int. Just use it's name in places where you want the value, and the compiler-generated code will do the address lookup.

You only need to use a different approach if data is NOT stored in SRAM.

To test further I eliminate the write step: const int test1 PROGMEM = { 2, 121, 111, 65535};

That is not a "write step". That is a data definition. It creates an array of the four values placed in Flash. Removing that definition removes the array. Forcing your code to compile without that definition creates a situation where the behaviour is undefined.

What are you trying to accomplish?

so... you are learning about pointers...

pgm_read_word_near() takes a pointer as its argument. Your for loop then does some pointer math as it works through, incrementing the pointer to the next address.

You substitute the pointer to test1[]

(test1 is a pointer to the first element of the test1 array) which you have not initialized! As you increment that pointer, you get garbage... you don't know what is in those addresses.

yes, you are learning about pointers!!!

Coding badly:

I am right now just trying to learn how to write some data to flash and read it back at another time using another program. Is it possible to specify the data address range when writing it? That way I can confirm I succeeded in my goal to write to flash.

edkost:
Coding badly:

I am right now just trying to learn how to write some data to flash and read it back at another time using another program. Is it possible to specify the data address range when writing it? That way I can confirm I succeeded in my goal to write to flash.

Consider using EEPROM vs flash.

BulldogLowell:
The Due does not seem to have any EEPROM so I suppose you are suggesting attaching an external serial EEPROM? I suppose that would work for me. Something else to learn since defining the flash address seems to be a background thing and not under my control.

edkost:
The Due does not seem to have any EEPROM...

there are FLASH/EEPROM libraries out there that may work for Due.

here is one, though I have no experience with it.

Another winner thread title...

This is related to your post about generating sine waves, isn't it?

You can certainly attach I2C EEPROM devices, but if you want to store an array of numbers you can probably use PROGMEM, the same as on the AVR processors.

aarg:
Another winner thread title...

Yes, it would be courteous for the thread title to reflect what you are asking, not something like "Flashticles" or "Loopus Doopus". A lot of the regular posters here are professional programmers or electronics engineers. Consider talking to them in the same way you might if you met them in person.

Nick:
Yes this is somewhat related, but not directly. As you can see from my posts this I am studying PROGMEM and found the tutorials and references lacking. Bummer. The goal is to write data to specific address so I can dig that data up later. So far PROGMEM does not seem to allow me to control where the data goes or at least tell me where it is.
You obviously don't know me or you would not suggest I would not talking in person to professionals in such a casual way. I am and have been an electronics analog hardware engineer for decades now. Being such a professional does not impress me too much. In any event I will try and keep my subject titles more professional.

edkost:
As you can see from my posts this I am studying PROGMEM and found the tutorials and references lacking.

http://www.gammon.com.au/progmem

edkost:
You obviously don’t know me or you would not suggest I would not talking in person to professionals in such a casual way.

How could I? I only know you from what you write here.

Thanks. I’ll study this.

edkost:
So far PROGMEM does not seem to allow me to control where the data goes or at least tell me where it is.

You probably know that pgm_read_<byte/word>_near() will access it, no matter where it is. So what do you need to do, that requires that you know “where it is”, or control that? It’s not a C/C++ philosophy to facilitate control of specific memory allocations at the application level. For well established software engineering reasons.