adafruit / RTClib

A fork of Jeelab's fantastic RTC Arduino library
MIT License
795 stars 705 forks source link

Arduino restarts when it tries to read RTC_DS3231::now() #261

Closed rtek1000 closed 2 years ago

rtek1000 commented 2 years ago

I tried to use this function to read the RTC, but the Arduino Pro Mini crashes constantly.

I was only able to isolate the problem using WDT. I was isolating each part of the code until I got to this library function. If I comment out this line, there are no more crashes:

  i2c_dev->write_then_read(buffer, 1, buffer, 7);

Full function:

DateTime RTC_DS3231::now() {
  uint8_t buffer[7];
  buffer[0] = 0;
  i2c_dev->write_then_read(buffer, 1, buffer, 7);

  return DateTime(bcd2bin(buffer[6]) + 2000U, bcd2bin(buffer[5] & 0x7F),
                  bcd2bin(buffer[4]), bcd2bin(buffer[2]), bcd2bin(buffer[1]),
                  bcd2bin(buffer[0] & 0x7F));
}

In another project with RTC DS3231 I've been using this other library for a long time without having this kind of problem, in case someone wants to compare what might be happening: https://github.com/JChristensen/DS3232RTC

// Read the current time from the RTC and return it in a tmElements_t
// structure. Returns the I2C status (zero if successful).
uint8_t DS3232RTC::read(tmElements_t &tm)
{
    i2cBeginTransmission(DS32_ADDR);
    i2cWrite(DS32_SECONDS);
    if ( uint8_t e = i2cEndTransmission() ) { errCode = e; return e; }
    // request 7 bytes (secs, min, hr, dow, date, mth, yr)
    i2cRequestFrom(DS32_ADDR, static_cast<uint8_t>(tmNbrFields));
    tm.Second = bcd2dec(i2cRead() & ~_BV(DS1307_CH));
    tm.Minute = bcd2dec(i2cRead());
    tm.Hour = bcd2dec(i2cRead() & ~_BV(DS32_HR1224));   // assumes 24hr clock
    tm.Wday = i2cRead();
    tm.Day = bcd2dec(i2cRead());
    tm.Month = bcd2dec(i2cRead() & ~_BV(DS32_CENTURY)); // don't use the Century bit
    tm.Year = y2kYearToTm(bcd2dec(i2cRead()));
    return 0;
}
rtek1000 commented 2 years ago

Too soon, I replaced this library with the other one, but it's still crashing, so I'll leave this notification closed, in case I manage to find the problem, I'll try to comment later.

rtek1000 commented 2 years ago

I found the problem, the other library that makes the display work doesn't allow the I2C port to work properly.

A library for driving the Freetronics 512 pixel dot matrix LED display "DMD", a 32 x 16 layout.

Library that does not support I2C: https://github.com/freetronics/DMD2

Library that accepts I2C: https://github.com/freetronics/DMD

rtek1000 commented 2 years ago

https://github.com/freetronics/DMD2/issues/67