As you can see from the log below, bar3
and ok4
messages are missing,
so it is crashing on Serial.println(*foo);
when foo
is passed to a function.
It works if the var is not PSTR
(PROGMEM
/FLASH
) and BEFORE passing it to the bar()
function.
The question is : why? Is it a general bug or am I doing something wrong?
Actually, I stumbled upon this problem when I tried to pass PSTR
-ed string literal to WiFi.begin()
(ssid
arg):
#include <Arduino.h>
#include <ESP8266WiFi.h>
void bar(const char* foo) {
Serial.println("bar1");
Serial.println(foo);
Serial.println("bar2");
Serial.println(*foo);
Serial.println("bar3");
}
void setup() {
Serial.begin(115200);
delay(2000);
while (!Serial && millis() < 5000);
Serial.println("OK Serial started");
// just `"Hello"` works
// `PSTR("Hello")` crashes at `*foo` WHEN PASSED to `bar()`
const char* foo = PSTR("Hello");
Serial.println("ok1");
Serial.println(foo);
Serial.println("ok2");
Serial.println(*foo);
Serial.println("ok3");
bar(foo);
Serial.println("ok4");
}
void loop() {
}
18:31:15.468 > OK Serial started
18:31:15.468 > ok1
18:31:15.468 > Hello
18:31:15.468 > ok2
18:31:15.468 > H
18:31:15.468 > ok3
18:31:15.468 > bar1
18:31:15.468 > Hello
18:31:15.468 > bar2
18:31:15.468 >
18:31:15.473 > --------------- CUT HERE FOR EXCEPTION DECODER ---------------
18:31:15.480 >
18:31:15.480 > Exception (3):
18:31:15.480 > epc1=0x40201053 epc2=0x00000000 epc3=0x00000000 excvaddr=0x4023ae24 depc=0x00000000
18:31:15.523 >
18:31:15.523 > LoadStoreError: Processor internal physical address or data error during load or store
18:31:15.523 > epc1=0x40201053 in bar(char const*) at ??:?
18:31:15.523 > excvaddr=0x4023ae24 in etharp_output at ??:?
18:31:15.523 >
18:31:15.523 > >>>stack>>>
18:31:15.523 >
18:31:15.523 > ctx: cont
18:31:15.635 > sp: 3ffffdf0 end: 3fffffc0 offset: 0190
18:31:15.635 > 3fffff80: 40202189 4023ae24 3ffee508 40201053
18:31:15.635 > 3fffff90: 3fffdad0 4023ae24 3ffee508 402010fc
18:31:15.635 > 3fffffa0: feefeffe 00000000 3ffee55c 40201bf4
18:31:15.635 > 3fffffb0: feefeffe feefeffe 3ffe85d8 40100c25
18:31:15.635 > <<<stack<<<
18:31:15.635 >
18:31:15.635 > 0x40202189 in __delay at ??:?
18:31:15.635 > 0x4023ae24 in etharp_output at ??:?
18:31:15.635 > 0x40201053 in bar(char const*) at ??:?
18:31:15.635 > 0x4023ae24 in etharp_output at ??:?
18:31:15.635 > 0x402010fc in setup at ??:?
18:31:15.635 > 0x40201bf4 in loop_wrapper() at core_esp8266_main.cpp:?
18:31:15.635 > 0x40100c25 in cont_wrapper at ??:?
18:31:15.635 >
18:31:15.635 >
18:31:15.635 > --------------- CUT HERE FOR EXCEPTION DECODER ---------------
18:31:15.636 >
18:31:15.636 > ets Jan 8 2013,rst cause:2, boot mode:(3,6)