Arduino ver. 1.8.19 - Hardware see the attached images.
Problem with interrupt handling of RTClib and DS3231.
I used two ESP32, one that I need for the project is the 38 Pin ESP32, the other for any tests is the ESP32-TTGO.
I would like to use the RTClib with the DS3231 with alarms.
For this reason I used the SQW pin, connecting it to the free Pins, which in the case of the ESP32-TTGO are all, while in the case of the ESP32-38PIN they are only some.
Unfortunately every time I try to use one the ESP32 crashes, sometimes without the alarm being enabled but immediately after loading the program. At this point I excluded all the Pins that gave this problem imagining that the use of that Pin as an interrupt was not suitable.
The problem is that every time the alarm is started (even if I still don't understand how it works) the ESP32 crashes again.
Surely I must have done something wrong in the test program, but I don't understand where I made the mistake.
// Date and time functions using a DS3231 RTC connected via I2C and Wire lib
#include "RTClib.h"
RTC_DS3231 rtc;
// the pin that is connected to SQW
#define CLOCK_INTERRUPT_PIN 36
/*
DS3231 Alarm modes for alarm 1 ***********
DS3231_A1_PerSecond = 0x0F,
DS3231_A1_Second = 0x0E,
DS3231_A1_Minute = 0x0C,
DS3231_A1_Hour = 0x08,
DS3231_A1_Date = 0x00,
DS3231_A1_Day = 0x10
DS3231 Alarm modes for alarm 2 ***********
DS3231_A2_PerMinute = 0x7,
DS3231_A2_Minute = 0x6,
DS3231_A2_Hour = 0x4,
DS3231_A2_Date = 0x0,
DS3231_A2_Day = 0x8
*/
char daysOfTheWeek[7][12] = {"Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"};
int yy=0;
int MM=0;
int dd=0;
int hh=0;
int mm=0;
int ss=0;
bool Aa = false;
void setup () {
Serial.begin(115200);
delay(1000);
if (! rtc.begin()) {
Serial.println("Couldn't find RTC");
Serial.flush();
while (1) delay(10);
}
//we don't need the 32K Pin
rtc.disable32K();
// Making it so, that the alarm will trigger an interrupt
pinMode(CLOCK_INTERRUPT_PIN, INPUT_PULLUP);
attachInterrupt(digitalPinToInterrupt(CLOCK_INTERRUPT_PIN), onAlarm, FALLING);
// set alarm 1, 2 flag to false (so alarm 1, 2 didn't happen so far)
// if not done, this easily leads to problems, as both register aren't reset on reboot/recompile
rtc.clearAlarm(1);
rtc.clearAlarm(2);
// stop oscillating signals at SQW Pin
// otherwise setAlarm1 will fail
rtc.writeSqwPinMode(DS3231_OFF);
// if (rtc.lostPower()) {
rtc.adjust(DateTime(F(__DATE__), F(__TIME__)));
// }
DateTime now = rtc.now();
yy = now.year();
MM = now.month();
dd = now.day();
hh = now.hour();
mm = now.minute();
ss = now.second();
// rtc.adjust(DateTime(yy, MM, dd, hh, mm, ss));
}
void loop () {
DateTime now = rtc.now();
Serial.print("Adesso = ");
Serial.print(now.year(), DEC);
Serial.print('/');
Serial.print(now.month(), DEC);
Serial.print('/');
Serial.print(now.day(), DEC);
Serial.print(" - ");
Serial.print(now.hour(), DEC);
Serial.print(':');
Serial.print(now.minute(), DEC);
Serial.print(':');
Serial.print(now.second(), DEC);
Serial.println();
setAlarm();
// the stored alarm value + mode
DateTime alarm2 = rtc.getAlarm2();
Ds3231Alarm2Mode alarm2mode = rtc.getAlarm2Mode();
char alarm2Date[12] = "DD hh:mm:ss";
alarm2.toString(alarm2Date);
Serial.print(" [Alarm2: ");
Serial.print(alarm2Date);
Serial.print(", Mode: ");
switch (alarm2mode) {
case DS3231_A2_PerMinute: Serial.print("PerMinute"); break;
case DS3231_A2_Minute: Serial.print("Minute"); break;
case DS3231_A2_Hour: Serial.print("Hour"); break;
case DS3231_A2_Date: Serial.print("Date"); break;
case DS3231_A2_Day: Serial.print("Day"); break;
}
Serial.println();
delay(1000);
}
void setAlarm() {
if (Aa == false) {
rtc.clearAlarm(2);
rtc.writeSqwPinMode(DS3231_OFF);
if (!rtc.setAlarm2( rtc.now() + TimeSpan( 1 ), DS3231_A2_PerMinute )) {
Serial.println("Error, alarm wasn't set!");
} else {
Serial.println("Alarm will happen every 1 minute!");
}
}
Aa = true;
}
void onAlarm() {
Serial.println("Alarm occured!");
rtc.clearAlarm(2);
Serial.println("** Alarm cleared");
}
[Alarm2: 18 10:22:00, Mode: PerMinute
Alarm occured!
uru Meditation Error: Core 1 panic'ed (Interrupt wdt timeout on CPU1).
Core 1 register dump:
PC : 0x4008afe6 PS : 0x00060735 A0 : 0x80089f5e A1 : 0x3ffbf08c
A2 : 0x3ffb8d90 A3 : 0x3ffbce64 A4 : 0x00000004 A5 : 0x00060723
A6 : 0x00060723 A7 : 0x00000001 A8 : 0x3ffbce64 A9 : 0x00000019
A10 : 0x3ffbce64 A11 : 0x00000019 A12 : 0x3ffc2cdc A13 : 0x00060723
A14 : 0x007bf388 A15 : 0x003fffff SAR : 0x0000001c EXCCAUSE: 0x00000006
EXCVADDR: 0x00000000 LBEG : 0x400861f8 LEND : 0x4008620e LCOUNT : 0x00000000
Core 1 was running in ISR context:
EPC1 : 0x400dd96f EPC2 : 0x00000000 EPC3 : 0x00000000 EPC4 : 0x00000000
Backtrace: 0x4008afe3:0x3ffbf08c |<-CORRUPTED
Core 0 register dump:
PC : 0x4008b17b PS : 0x00060035 A0 : 0x80089b87 A1 : 0x3ffbeaac
A2 : 0x3ffbf388 A3 : 0xb33fffff A4 : 0x0000abab A5 : 0x00060023
A6 : 0x00060021 A7 : 0x0000cdcd A8 : 0x0000abab A9 : 0xffffffff
A10 : 0x3ffc2af8 A11 : 0x00000000 A12 : 0x3ffc2af4 A13 : 0x00000007
A14 : 0x007bf388 A15 : 0x003fffff SAR : 0x0000001d EXCCAUSE: 0x00000006
EXCVADDR: 0x00000000 LBEG : 0x00000000 LEND : 0x00000000 LCOUNT : 0x00000000
Backtrace: 0x4008b178:0x3ffbeaac |<-CORRUPTED
ELF file SHA256: 4cd5a81964b129d5