Receiving wrong values from four 32000 char arrays (Arduino Mega)

Hello,

I am trying to store and retrieve values from four 32000 char arrays stored in flash of an Arduino Mega 2560. Unfortunately, when I go to retrieve some values, the values printed out in the serial monitor are wrong.

I have looked at the PROGMEM section in the Arduino reference and at https://www.nongnu.org/avr-libc/user-manual/group__avr__pgmspace.html and am not able to figure out what is going wrong.

Currently, I am trying to use pgm_read_byte_near(), pgm_read_byte(), and pgm_read_byte_far() and all of them are giving the wrong values.

Interestingly, I get the right value when I store and retrieve values form only 1 array, so I'm guessing it has something to do with where and how the arrays are being stored.

Any help would be appreciated.

Code is too big to post here due to the arrays, so here is a link so others can replicate the issue:https://gofile.io/d/DgHjIF

Otherwise, here is the main snippet minus the arrays

#include <avr/pgmspace.h>
const char image1[32000] PROGMEM ={0x61, ...<large array>..., 0x31};
const char image2[32000] PROGMEM ={0x16, ...<large array>..., 0x31};
const char image3[32000] PROGMEM ={0x32, ...<large array>..., 0x31};
const char image4[32000] PROGMEM ={0x33, ...<large array>..., 0x31};



void setup() {
  // put your setup code here, to run once:
  Serial.begin(9600);

  Serial.println("I am working");
  delay(1000);

  for (int i = 0; i < 32000; i++) {
    Serial.print(pgm_read_byte_near(image1+i), HEX);
    Serial.print(" ");
    Serial.print(pgm_read_byte(image1+i), HEX);
    Serial.print(" ");
    Serial.print(pgm_read_byte_far(image1+i), HEX);
    Serial.print("\n");


    delay(1000);
  }

  for (int i = 0; i < 32000; i++) {
    Serial.print(pgm_read_byte_near(image2+i), HEX);
    Serial.print(" ");
    Serial.print(pgm_read_byte(image2+i), HEX);
    Serial.print(" ");
    Serial.print(pgm_read_byte_far(image2+i), HEX);
    Serial.print("\n");


    delay(1000);
  }

  for (int i = 0; i < 32000; i++) {
    Serial.print(pgm_read_byte_near(image3+i), HEX);
    Serial.print(" ");
    Serial.print(pgm_read_byte(image3+i), HEX);
    Serial.print(" ");
    Serial.print(pgm_read_byte_far(image3+i), HEX);
    Serial.print("\n");


    delay(1000);
  }

  for (int i = 0; i < 32000; i++) {
    Serial.print(pgm_read_byte_near(image4+i), HEX);
    Serial.print(" ");
    Serial.print(pgm_read_byte(image4+i), HEX);
    Serial.print(" ");
    Serial.print(pgm_read_byte_far(image4+i), HEX);
    Serial.print("\n");


    delay(1000);
  }

}

void loop() {
  // put your main code here, to run repeatedly:

}

Have a look at #9 and use GET_FAR_ADDRESS with pgm_read_byte_far.

try this, may that will work for you

const uint8_t image1[32000] PROGMEM = {0x11};
const uint8_t image2[32000] PROGMEM = {0x22};
const uint8_t image3[32000] PROGMEM = {0x33};
const uint8_t image4[32000] PROGMEM = {0x44};

void setup() {
  Serial.begin(115200); Serial.println();
  Serial.print("Image1 1st byte = 0x"); Serial.println(pgm_read_byte_far(pgm_get_far_address(image1)), HEX);
  Serial.print("Image2 1st byte = 0x"); Serial.println(pgm_read_byte_far(pgm_get_far_address(image2)), HEX);
  Serial.print("Image3 1st byte = 0x"); Serial.println(pgm_read_byte_far(pgm_get_far_address(image3)), HEX);
  Serial.print("Image4 1st byte = 0x"); Serial.println(pgm_read_byte_far(pgm_get_far_address(image4)), HEX);
}

void loop() {}

if all goes well (untested with a real MEGA, works in Wokwi) it should show

Image1 1st byte = 0x11
Image2 1st byte = 0x22
Image3 1st byte = 0x33
Image4 1st byte = 0x44

or to read multiple bytes

const uint8_t image1[32000] PROGMEM = {0x11, 0x12, 0x13};
const uint8_t image2[32000] PROGMEM = {0x21, 0x22, 0x23};
const uint8_t image3[32000] PROGMEM = {0x31, 0x32, 0x33};
const uint8_t image4[32000] PROGMEM = {0x41, 0x42, 0x43};

void setup() {
  Serial.begin(115200); Serial.println();

  Serial.print("Image1 1st byte = 0x"); Serial.println(pgm_read_byte_far(pgm_get_far_address(image1)), HEX);
  Serial.print("Image1 2nd byte = 0x"); Serial.println(pgm_read_byte_far(pgm_get_far_address(image1[1])), HEX);
  Serial.print("Image1 3rd byte = 0x"); Serial.println(pgm_read_byte_far(pgm_get_far_address(image1[2])), HEX);

  Serial.print("Image2 1st byte = 0x"); Serial.println(pgm_read_byte_far(pgm_get_far_address(image2)), HEX);
  Serial.print("Image3 2nd byte = 0x"); Serial.println(pgm_read_byte_far(pgm_get_far_address(image2[1])), HEX);
  Serial.print("Image2 3rd byte = 0x"); Serial.println(pgm_read_byte_far(pgm_get_far_address(image2[2])), HEX);

  Serial.print("Image3 1st byte = 0x"); Serial.println(pgm_read_byte_far(pgm_get_far_address(image3)), HEX);
  Serial.print("Image3 2nd byte = 0x"); Serial.println(pgm_read_byte_far(pgm_get_far_address(image3[1])), HEX);
  Serial.print("Image3 3rd byte = 0x"); Serial.println(pgm_read_byte_far(pgm_get_far_address(image3[2])), HEX);

  Serial.print("Image4 1st byte = 0x"); Serial.println(pgm_read_byte_far(pgm_get_far_address(image4)), HEX);
  Serial.print("Image4 2nd byte = 0x"); Serial.println(pgm_read_byte_far(pgm_get_far_address(image4[1])), HEX);
  Serial.print("Image4 3rd byte = 0x"); Serial.println(pgm_read_byte_far(pgm_get_far_address(image4[2])), HEX);

}

void loop() {}

➜

Image1 1st byte = 0x11
Image1 2nd byte = 0x12
Image1 3rd byte = 0x13
Image2 1st byte = 0x21
Image3 2nd byte = 0x22
Image2 3rd byte = 0x23
Image3 1st byte = 0x31
Image3 2nd byte = 0x32
Image3 3rd byte = 0x33
Image4 1st byte = 0x41
Image4 2nd byte = 0x42
Image4 3rd byte = 0x43

Here is my test from a while ago: Progmem_far.ino
Read the comment in the file and the added posts.

The Arduino Mega does not use a 32 bits pointer to data. The trick is to make a variable of 32 bits and put the address in it during runtime with the pgm_get_far_address() function. But you have to remember by yourself that it is a pointer.

The 'F()' macro may no longer be used, because it is not known in which segment it is. Also libraries that use the 'F()' macro can go wrong.

In my example I used pgm_get_far_address() and put those in a uint32_t variable. Then that variable is used as a pointer and only for special functions, such as pgm_read_byte_far().
J-M-L calls the pgm_get_far_address() and pgm_read_byte_far() in a single line. That should also work.

Solution: Buy a modern Arduino board (or compatible) with a normal processor.

Thanks for your replies.

@Whandall If I understood correctly, you are basically saying what @J-M-L has shown in their code.

I copied and pasted your code along with my arrays and it seems to work. However, I get a strange error while using a for loop to iterate over the array. Here is my code:

const uint8_t image1[32000] PROGMEM = {0x11, 0x12, 0x13};
const uint8_t image2[32000] PROGMEM = {0x21, 0x22, 0x23};
const uint8_t image3[32000] PROGMEM = {0x31, 0x32, 0x33};
const uint8_t image4[32000] PROGMEM = {0x41, 0x42, 0x43};
void setup() {
  Serial.begin(115200);
  Serial.println();


  for(int i =0;i<32000;i++){
    Serial.print("Image1 byte = 0x"); 
    Serial.println(pgm_read_byte_far(pgm_get_far_address(image1[i])), HEX);
  }
//
//  Serial.print("Image1 1st byte = 0x"); Serial.println(pgm_read_byte_far(pgm_get_far_address(image1)), HEX);
//  Serial.print("Image1 2nd byte = 0x"); Serial.println(pgm_read_byte_far(pgm_get_far_address(image1[1])), HEX);
//  Serial.print("Image1 3rd byte = 0x"); Serial.println(pgm_read_byte_far(pgm_get_far_address(image1[1000])), HEX);
//
//  Serial.print("Image2 1st byte = 0x"); Serial.println(pgm_read_byte_far(pgm_get_far_address(image2)), HEX);
//  Serial.print("Image3 2nd byte = 0x"); Serial.println(pgm_read_byte_far(pgm_get_far_address(image2[1])), HEX);
//  Serial.print("Image2 3rd byte = 0x"); Serial.println(pgm_read_byte_far(pgm_get_far_address(image2[2])), HEX);
//
//  Serial.print("Image3 1st byte = 0x"); Serial.println(pgm_read_byte_far(pgm_get_far_address(image3)), HEX);
//  Serial.print("Image3 2nd byte = 0x"); Serial.println(pgm_read_byte_far(pgm_get_far_address(image3[1])), HEX);
//  Serial.print("Image3 3rd byte = 0x"); Serial.println(pgm_read_byte_far(pgm_get_far_address(image3[2])), HEX);
//
//  Serial.print("Image4 1st byte = 0x"); Serial.println(pgm_read_byte_far(pgm_get_far_address(image4)), HEX);
//  Serial.print("Image4 2nd byte = 0x"); Serial.println(pgm_read_byte_far(pgm_get_far_address(image4[1])), HEX);
//  Serial.print("Image4 3rd byte = 0x"); Serial.println(pgm_read_byte_far(pgm_get_far_address(image4[2])), HEX);

}

void loop() {}

This is the error that I get when I try to compile this.

<removed>In function `setup':
<removed>/anothermemtest.ino:16: undefined reference to `r30'
<removed>/anothermemtest.ino:16: undefined reference to `r30'
<removed>/anothermemtest.ino:16: undefined reference to `r30'
collect2.exe: error: ld returned 1 exit status
exit status 1
Error compiling for board Arduino Mega or Mega 2560.

@Koepel thanks for replying. I'll check out your solution in a bit and get back to you

Hi

you need to do that math yourself. In my case I was using known indexes so the compiler was optimising this for me.

You need to go to what @Koepel said, and use a uint32_t

const uint8_t image1[32000] PROGMEM = {0x11, 0x12, 0x13};
const uint8_t image2[32000] PROGMEM = {0x21, 0x22, 0x23};
const uint8_t image3[32000] PROGMEM = {0x31, 0x32, 0x33};
const uint8_t image4[32000] PROGMEM = {0x41, 0x42, 0x43};


void setup() {
  Serial.begin(115200);
  Serial.println();

  const uint32_t im1Address = (uint32_t) pgm_get_far_address(image1);
  const uint32_t im2Address = (uint32_t) pgm_get_far_address(image2);
  const uint32_t im3Address = (uint32_t) pgm_get_far_address(image3);
  const uint32_t im4Address = (uint32_t) pgm_get_far_address(image4);

  for (int i = 0; i < 3; i++) {
    Serial.print("Image1 byte = 0x");
    Serial.println(pgm_read_byte_far(im1Address + i), HEX);
  }

  for (int i = 0; i < 3; i++) {
    Serial.print("Image1 byte = 0x");
    Serial.println(pgm_read_byte_far(im2Address + i), HEX);
  }

  for (int i = 0; i < 3; i++) {
    Serial.print("Image1 byte = 0x");
    Serial.println(pgm_read_byte_far(im3Address + i), HEX);
  }

  for (int i = 0; i < 3; i++) {
    Serial.print("Image1 byte = 0x");
    Serial.println(pgm_read_byte_far(im4Address + i), HEX);
  }
}

void loop() {}

Careful on the math, it's not a pointer so adding i is just adding a number of bytes value. Here it works because we are pointing to bytes anyway. if you were pointing to larger types, adding 1 to a pointer would skip the size of the data to get to the next entry and would not work in our case (need to add sizeof the data type pointed)

Thanks for your insights everyone. I was able to get the code that @Koepel and @J-M-L posted to store and retrieve these values.

However, the intended purpose of this was to store image data to be sent to a e-paper display via SPI, which still isn't working. I still believe it is a storage related issue as the code works when I comment out a certain (seemingly harmless) block of statements.

In the code posted below, I have marked a section in the function EPDInit(). The Mega finishes the setup() function ("I finished the program" is printed out) when the code statements in the section are commented out. However, the program stalls somewhere when the full code is run. The last thing printed out is "Done reset. Sending init commands to epd", so we are not even reaching the for loops. Again, any help would be much appreciated.

Complete code for anyone who wishes to replicate the issue: https://gofile.io/d/acG9hW

Code with the arrays removed:

#include <SPI.h>

#define RST_PIN         8
#define DC_PIN          9
#define CS_PIN          10
#define BUSY_PIN        7
#define WIDTH           640
#define HEIGHT          400

const char image1[32000] PROGMEM = { 0x11, 0x12 ... <Large array> ... 0x16, 0x61};
const char image2[32000] PROGMEM = { 0x21, 0x22 ... <Large array> ... 0x16, 0x61};
const char image3[32000] PROGMEM = { 0x31, 0x32 ... <Large array> ... 0x16, 0x61};
const char image4[32000] PROGMEM = { 0x41, 0x42 ... <Large array> ... 0x16, 0x61};


uint32_t image1block, image2block, image3block, far_block4, image4block;

void createFarPointers()
{
  image1block = pgm_get_far_address( image1);
  image2block = pgm_get_far_address( image2);
  image3block = pgm_get_far_address( image3);
  image4block = pgm_get_far_address( image4);
}

void SendCommand(unsigned char command) {
  digitalWrite(DC_PIN, LOW);
  digitalWrite(CS_PIN, LOW);
  SPI.transfer(command);
  digitalWrite(CS_PIN, HIGH);
}

void SendData(unsigned char command) {
  digitalWrite(DC_PIN, HIGH);
  digitalWrite(CS_PIN, LOW);
  SPI.transfer(command);
  digitalWrite(CS_PIN, HIGH);

}

void EPD_4IN01F_BusyHigh() {
  while (!(digitalRead(BUSY_PIN)));
}

void EPD_4IN01F_BusyLow() {
  while ((digitalRead(BUSY_PIN)));
}

void Reset(void) {
  digitalWrite(RST_PIN, HIGH);
  delay(200);
  digitalWrite(RST_PIN, LOW);
  delay(1);
  digitalWrite(RST_PIN, HIGH);
  delay(200);
}

void EPDInit() {
  pinMode(CS_PIN, OUTPUT);
  pinMode(RST_PIN, OUTPUT);
  pinMode(DC_PIN, OUTPUT);
  pinMode(BUSY_PIN, INPUT);
  SPI.begin();
  SPI.beginTransaction(SPISettings(2000000, MSBFIRST, SPI_MODE0));
  Serial.println("Initialized SPI");
  Reset();
  EPD_4IN01F_BusyHigh();
  Serial.println("Done reset. Sending init commands to epd");
  SendCommand(0x00);
  SendData(0x2f);
  SendData(0x00);
  SendCommand(0x01);
  SendData(0x37);
  SendData(0x00);
  SendData(0x05);
  SendData(0x05);
  SendCommand(0x03);
  SendData(0x00);
  SendCommand(0x06);
  SendData(0xC7);
  SendData(0xC7);
  SendData(0x1D);
  SendCommand(0x41);
  SendData(0x00);
  SendCommand(0x50);
  SendData(0x37);
  SendCommand(0x60);
  SendData(0x22);
  SendCommand(0x61);
  SendData(0x02);
  SendData(0x80);
  SendData(0x01);
  SendData(0x90);
  SendCommand(0xE3);
  SendData(0xAA);
  Serial.println("EPD init commands done. Begin clear");


  // *** CODE WORKS WHEN THIS BLOCK IS COMMENTED OUT ***

  
//  SendCommand(0x61);//Set Resolution setting
//  SendData(0x02);
//  SendData(0x80);
//  SendData(0x01);
//  SendData(0x90);
//  SendCommand(0x10);
//  for (int i = 0; i < WIDTH / 2; i++) {
//    for (int j = 0; j < HEIGHT; j++) {
//      SendData(0x11);
//    }
//  }
//  Serial.println("Sent all clear commands. Refreshing screen");
//  SendCommand(0x04);
//  EPD_4IN01F_BusyHigh();
//  SendCommand(0x12);
//  EPD_4IN01F_BusyHigh();
//  SendCommand(0x02);
//  EPD_4IN01F_BusyLow();
//  delay(500);

  
  //*** BLOCK ENDS ***


}

void setup()
{
  Serial.begin(9600);

  EPDInit();
  Serial.println("Cleared Display");

  createFarPointers();
  Serial.println("I finished the program");
}

void loop()
{
}

Add some flush commands after each debug print and add more prints.

Okay, I added more Serial.println() functions and Serial.flush() after them. The last message printed out on the Serial monitor is "Done reset. Sending init commands to epd" when the mentioned code block is uncommented.

The program completes when the code block is commented, so the issue is still there.

Here is the code with the debug and flush statements:

#include <SPI.h>

#define RST_PIN         8
#define DC_PIN          9
#define CS_PIN          10
#define BUSY_PIN        7
#define WIDTH           640
#define HEIGHT          400

const char image1[32000] PROGMEM = { 0x11, 0x12 ... <Large array> ... 0x16, 0x61};
const char image2[32000] PROGMEM = { 0x21, 0x22 ... <Large array> ... 0x16, 0x61};
const char image3[32000] PROGMEM = { 0x31, 0x32 ... <Large array> ... 0x16, 0x61};
const char image4[32000] PROGMEM = { 0x41, 0x42 ... <Large array> ... 0x16, 0x61};

uint32_t image1block, image2block, image3block, image4block;

void createFarPointers()
{
  image1block = pgm_get_far_address( image1);
  image2block = pgm_get_far_address( image2);
  image3block = pgm_get_far_address( image3);
  image4block = pgm_get_far_address( image4);
}

void SendCommand(unsigned char command) {
  digitalWrite(DC_PIN, LOW);
  digitalWrite(CS_PIN, LOW);
  SPI.transfer(command);
  digitalWrite(CS_PIN, HIGH);
}

void SendData(unsigned char command) {
  digitalWrite(DC_PIN, HIGH);
  digitalWrite(CS_PIN, LOW);
  SPI.transfer(command);
  digitalWrite(CS_PIN, HIGH);

}

void EPD_4IN01F_BusyHigh() {
  while (!(digitalRead(BUSY_PIN)));
}

void EPD_4IN01F_BusyLow() {
  while ((digitalRead(BUSY_PIN)));
}

void Reset(void) {
  digitalWrite(RST_PIN, HIGH);
  delay(200);
  digitalWrite(RST_PIN, LOW);
  delay(1);
  digitalWrite(RST_PIN, HIGH);
  delay(200);
}

void EPDInit() {
  pinMode(CS_PIN, OUTPUT);
  pinMode(RST_PIN, OUTPUT);
  pinMode(DC_PIN, OUTPUT);
  pinMode(BUSY_PIN, INPUT);
  SPI.begin();
  SPI.beginTransaction(SPISettings(2000000, MSBFIRST, SPI_MODE0));
  Serial.println("Initialized SPI");
  Serial.flush();
  Reset();
  Serial.println("Sent reset commands");
  Serial.flush();
  EPD_4IN01F_BusyHigh();
  Serial.println("Busy pin is now high");
  Serial.flush();
  Serial.println("Done reset. Sending init commands to epd");
  Serial.flush();
  SendCommand(0x00);
  SendData(0x2f);
  SendData(0x00);
  Serial.println("Sent 0x00 command");
  Serial.flush();
  SendCommand(0x01);
  SendData(0x37);
  SendData(0x00);
  SendData(0x05);
  SendData(0x05);
  Serial.println("Sent 0x01 command");
  Serial.flush();
  SendCommand(0x03);
  SendData(0x00);
  Serial.println("Sent 0x03 command");
  Serial.flush();
  SendCommand(0x06);
  SendData(0xC7);
  SendData(0xC7);
  SendData(0x1D);
  Serial.println("Sent 0x06 command");
  Serial.flush();
  SendCommand(0x41);
  SendData(0x00);
  Serial.println("Sent 0x41 command");
  Serial.flush();
  SendCommand(0x50);
  SendData(0x37);
  Serial.println("Sent 0x50 command");
  Serial.flush();
  SendCommand(0x60);
  SendData(0x22);
  Serial.println("Sent 0x60 command");
  Serial.flush();
  SendCommand(0x61);
  SendData(0x02);
  SendData(0x80);
  SendData(0x01);
  SendData(0x90);
  Serial.println("Sent 0x61 command");
  Serial.flush();
  SendCommand(0xE3);
  SendData(0xAA);
  Serial.println("EPD init commands done. Begin clear");
  Serial.flush();


  // *** CODE WORKS WHEN THIS BLOCK IS COMMENTED OUT ***


  SendCommand(0x61);//Set Resolution setting
  SendData(0x02);
  SendData(0x80);
  SendData(0x01);
  SendData(0x90);
  Serial.println("Sent 0x61 command");
  Serial.flush();

  SendCommand(0x10);

  Serial.println("Sent 0x10 command. Writing to screen");
  Serial.flush();
  for (int i = 0; i < WIDTH / 2; i++) {
    for (int j = 0; j < HEIGHT; j++) {
      SendData(0x11);
    }
  }
  Serial.println("Sent all clear commands. Refreshing screen");
  Serial.flush();
  SendCommand(0x04);
  EPD_4IN01F_BusyHigh();
  SendCommand(0x12);
  EPD_4IN01F_BusyHigh();
  SendCommand(0x02);
  EPD_4IN01F_BusyLow();
  delay(500);


  //*** BLOCK ENDS ***


}

void setup()
{
  Serial.begin(9600);

  EPDInit();
  Serial.println("Cleared Display");
  Serial.flush();

  createFarPointers();
  Serial.println("I finished the program");
  Serial.flush();
}

void loop()
{
}

Welcome,

Try this

#define PROGMEM_LATE __attribute__ (( __section__(".fini1") ))

const char image1[32000] PROGMEM_LATE = { 0x11, 0x12 ... <Large array> ... 0x16, 0x61};
const char image2[32000] PROGMEM_LATE = { 0x21, 0x22 ... <Large array> ... 0x16, 0x61};
const char image3[32000] PROGMEM_LATE = { 0x31, 0x32 ... <Large array> ... 0x16, 0x61};
const char image4[32000] PROGMEM_LATE = { 0x41, 0x42 ... <Large array> ... 0x16, 0x61};

This topic, amongst others, may be useful : Maximum PROGMEM data size - Arduino Mega

Definitely got things working. Thanks so much for your help.

I went through the thread you linked, but couldn't figure out what PROGMEM_LATE did. Perhaps you could explain or point me to the some other literature that could make things more clear?