hi,
my problem comes by using a variable with " attribute ((section (".noinit")))".
To locate hang ups of my program I activated the watchdop of my Mega2560 and set the time out period to 8 seconds. Then I defined a variable "prog_state" which is set to different values during various steps of my program. When a hang up occures the content of prog_state should tell me where it happened. To avoid initialization of prog_state during program run up (after watchdog reset) the variable is defined with attribute ((section (".noinit"))).
To allow resetting the watchdog also within some external functions the necessary includes for the watchdog are contained within a file GSM_GP.h with following code:
#ifndef __GSM_GP_H
#define __GSM_GP_H
#include <Arduino.h>
#include <avr/wdt.h>
...
To allow setting of prog_state also within some further external functions the file EPD_grapp.h contains the following code:
#ifndef _EPD_GRAPP_H_
#define _EPD_GRAPP_H_
...
extern uint8_t prog_state;
...
The code of the sketch, reduced to the essential parts, is as follows:
...
#include "GSM_GP.h"
#include "EPD_grapp.h"
...
uint8_t prog_state __attribute__ ((section (".noinit")));
word loopcnt=0;
...
void setup() { // ----------------------------------------------
wdt_disable(); // watchdog ausschalten
wdt_reset();
// Initialize serial port for communication with IDE.
Serial.begin(38400); // for Serial Monitor
delay(100);
Serial.print("\nWatchdog at ");
Serial.println(prog_state);
digitalWrite(41,LOW); // special test
...
wdt_enable(WDTO_8S);
wdt_reset();
} // setup()
void loop() { // ------------------------------------------------------<<
loopcnt++;
// testing the effect of changing pin41:
wdt_reset();
if (loopcnt>7) {
digitalWrite(41,HIGH);
prog_state=99;
// Serial.print(F("GSM-PWR off: "));Serial.println(prog_state);
delay(9000); // here the watchdog will reset the program!!
}
// a lot of code in the original sketch with many wdt_reset() statements and prog_state settings
// here is replaced by the following code
wdt_reset();
prog_state=30;
delay(1000);
wdt_reset();
prog_state=40;
delay(2000);
wdt_reset();
prog_state=50;
delay(3000);
wdt_reset();
prog_state=70;
delay(4000);
}
After 7 loops the program always is resetted by the watchdog.
The problem now is the following:
- With the code as shown here prog_state never is printed in setup() as 99
- Only when the line with "...Serial.println(prog_state)" is uncommented then prog_state will be 99 after watchdog reset.
remark: if the whole statement if (loopcnt>7) { } is commented then there is never a program reset from the watchdog.
It seems to me that the statement "prog_state=99;" within the if statement is optimized out. How can I avoid this, or what could be the reason?
SupArdu