0

I am getting this error in my code.
Fatal exception 9(LoadStoreAlignmentCause):

Using core version 2.4.2

Stack trace

Decoding stack results
0x40213e9c: dns_gethostbyname at core/dns.c line 1472
0x40204c21: ESP8266WiFiGenericClass::hostByName(char const*, IPAddress&, unsigned int) at /home/tony/.arduino15/packages/esp8266/hardware/esp8266/2.4.2/libraries/ESP8266WiFi/src/ESP8266WiFiGeneric.cpp line 468
0x40205038: WiFiClient::connect(char const*, unsigned short) at /home/tony/.arduino15/packages/esp8266/hardware/esp8266/2.4.2/libraries/ESP8266WiFi/src/WiFiClient.cpp line 98
0x4020306b: setPostString(int, int, Array8Int, char*) at /home/tony/Arduino/sketches/tempo1_vs11e_Master_NodeMCU_Rain_Wind/tempo1_vs11e_Master_NodeMCU_Rain_Wind.ino line 516
0x40203f8a: uploadAgrigis(char const*) at /home/tony/Arduino/sketches/tempo1_vs11e_Master_NodeMCU_Rain_Wind/tempo1_vs11e_Master_NodeMCU_Rain_Wind.ino line 335
0x40209080: Print::println() at /home/tony/.arduino15/packages/esp8266/hardware/esp8266/2.4.2/cores/esp8266/Print.cpp line 178
0x402048e0: loop() at /home/tony/Arduino/sketches/tempo1_vs11e_Master_NodeMCU_Rain_Wind/tempo1_vs11e_Master_NodeMCU_Rain_Wind.ino line 243

struct declaration

struct Array8Int {
    int array[8];
};

loop

char post[300] = "";
int pulseChange05m = rainCheck(slaveData);
int now10 = floor(unixtimeServer/10)*10;
Array8Int slaveData = slaveCheck(now()); // update slaveData array
setPostString(now10, pulseChange05m, slaveData, post);

functions

void setPostString(int unixtimeEvent, int pulseChange, Array8Int postData, char* postStr) {
  bool flag = true;
  float rainHour = pulseChange*0.25;
  if (hour() == 10) { // 7 AM palmital
    rainCount = 0;
    rainCount05m = 0;
  }
  rainCountHour = rainCount;
  int humidityBme280HourAvg = 0;
  int pressureBme280HourAvg = 0;
  int temperatureBme280HourAvg = 0;
  int lightSensorHourAvg = 0;
  int pressureBme280HourMin = 100000;
  int temperatureBme280HourMin = 1000;
  int temperatureBme280HourMax = -1000;
  int counterLoop = counterHour;
  for(int i=0;i<counterLoop;i++) {
    lightSensorHourAvg += lightSensorHour[i];
    pressureBme280HourAvg += pressureBme280Hour[i];
    temperatureBme280HourAvg += temperatureBme280Hour[i];
    temperatureBme280HourMax= max(temperatureBme280HourMax,temperatureBme280Hour[i]);
    pressureBme280HourMin = min(pressureBme280HourMin,pressureBme280Hour[i]);
    temperatureBme280HourMin = min(temperatureBme280HourMin,temperatureBme280Hour[i]);
    humidityBme280HourAvg += humidityBme280Hour[i]; 
  }
  pressureBme280HourAvg = pressureBme280HourAvg/counterLoop;
  temperatureBme280HourAvg = temperatureBme280HourAvg/counterLoop;
  lightSensorHourAvg = (int)lightSensorHourAvg/counterLoop;

  strcat(postStr, stationId);
  strcat(postStr,"&3="); // chuva hora
  strcat(postStr, dtostrf(rainHour,6,2,charDummy));
  strcat(postStr,"&8=");
  strcat(postStr, dtostrf(temperatureBme280HourAvg,6,2,charDummy));
  strcat(postStr,"&9="); //temperature avg
  strcat(postStr, dtostrf(temperatureBme280HourAvg,6,2,charDummy));
  strcat(postStr,"&10="); //temperature avg
  strcat(postStr, dtostrf(temperatureBme280HourMax,6,2,charDummy));
  strcat(postStr,"&11="); //temperature avg
  strcat(postStr, dtostrf(temperatureBme280HourMin,6,2,charDummy));
  strcat(postStr,"&20=");
  strcat(postStr, dtostrf(pressureBme280HourAvg,10,0,charDummy));
  strcat(postStr,"&22=");
  strcat(postStr, dtostrf(pressureBme280HourMin,10,0,charDummy));
  strcat(postStr,"&101=");
  strcat(postStr, dtostrf(lightSensorHourAvg,6,0,charDummy));
  strcat(postStr,"&time=");
  strcat(postStr, dtostrf(unixtimeEvent,16,0,charDummy));
  strcat(postStr,"&endline=1");
  strcat(postStr, "\r\n\r\n\r\n\r\n\r\n\r\n\r\n\r\n");
}
int rainCheck(Array8Int slaveData) {
  int pulseChange = slaveData.array[0];
  return pulseChange;
}
  • 1
    your code and esp8266 core version, please – Juraj Jul 26 '19 at 4:42
  • 2000 lines? maybe narrow it down... i'd appreciate some help with the stack -> does it point to line 243 or 1472 as last executed? tks @Juraj – tony gil Jul 26 '19 at 10:02
  • 1
    at least the code around setPostString(int, int, Array8Int, char*) at /home/tony/Arduino/sketches/tempo1_vs11e_Master_NodeMCU_Rain_Wind/tempo1_vs11e_Master_NodeMCU_Rain_Wind.ino line 516 – Juraj Jul 26 '19 at 11:04
1

Fatal exception 9(LoadStoreAlignmentCause):

This is caused by you (or some code you are running) attempting to read or write a 32 bit value from memory that is not "aligned" to 32 bits. It is most often the result of trying to cast portions of a byte array (for instance a buffer received over UDP) to a 32-bit integer. The compiler is not clever enough to know that the byte array portion you are casting isn't at a 32-bit boundary and thus doesn't know to use the 8-bit load/store instructions to access the memory.

Mapping a struct over a buffer can also have similar issues where the buffer is 8-bit aligned and the struct contains 32-bit words.

You can combat it by forcing the alignment of any buffers which you treat in such a way with the __attribute__((aligned(4)) tag:

uint8_t __attribute__((aligned(4))) buffer[128];

You can also "pack" structures to tell the compiler "These variables are mis-aligned and should not be padded to 32-bits":

struct foo {
    uint8_t bar1;
    uint32_t bar2;
} __attribute__((packed));

If in doubt, though, just pull individual bytes out of buffers (or wherever) and build up your larger integers manually with bit shifting and OR operators (<< and |), and do the reverse for filling byte buffers.

|improve this answer|||||
  • yes, i am passing a struct. will attempt these solutions and get back here to ACCEPT or not. UPVOTED :) – tony gil Jul 26 '19 at 10:00
  • edited in code above, AFTER you pointed the culprit out. Good job! – tony gil Jul 26 '19 at 13:19

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for?Browse other questions tagged or ask your own question.