Skip to content

pgm_read_word can cause exception if return type doesn't match size of pgm_read type #2028

@Makuna

Description

@Makuna

Basic Infos

Description

Problem description
If the type size that pgm_read_word() result does not match, an exception is thrown.
In the sample below, it demonstrates the issue; but if the result is stored in a uint16_t type; which matches the pgm_read_word(), then it works correctly.

Sketch

const uint32_t  pwmTable[] PROGMEM  = {42,43};
unsigned int i = 0;

void setup() {
 Serial.begin(115200);
 while (!Serial);
 Serial.println("Startup...");
}

void loop() {
  delay(5000);
  Serial.print(i);
  Serial.print(" = ");

  // uint16_t readvalue = pgm_read_word(pwmTable + i);  // this line won't cause exception
  uint32_t readvalue = pgm_read_word(pwmTable + i); // exception here
  i = (i+1) % (sizeof(pwmTable) / sizeof(pwmTable[0]));

  Serial.println(readvalue);
  Serial.flush();
}

Debug Messages


Exception (3):
epc1=0x40201c3d epc2=0x00000000 epc3=0x00000000 excvaddr=0x4022dfc4 depc=0x00000000

ctx: cont 
sp: 3ffef850 end: 3ffefa20 offset: 01a0

Want to back this issue? Post a bounty on it! We accept bounties via Bountysource.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions