Voting resources, early voting, and poll worker information - VOTE. ... Adafruit is open and shipping.
0

RTC.adjust() or RTC.lostpower() hangs regularly (DS3231)
Moderators: adafruit_support_bill, adafruit

Please be positive and constructive with your questions and comments.

RTC.adjust() or RTC.lostpower() hangs regularly (DS3231)

by basrijn on Sun Apr 05, 2020 3:06 pm

Hi,

I initially thought this was power related (see https://forums.adafruit.com/viewtopic.php?f=63&t=163903), but it looks like a somewhat different issue.

The results are consistent between two different chronodots and a Adafruit DS3231 breakout board.

The unit is working fine, I upload a new sketch and it will hang on the first rtc.adjust().

From some googling it suggest that rtc.begin() ALWAYS returns true, I know that to be incorrect, because I have seen it return false :) But in my hang condition rtc.begin() did return true.

The code will hang on RTC.adjust() or RTC.lostpower() (and I assume other functions). I can get the controller to run again by:
- removing all the power
- pulling the + wire to GND
Let that sit for a few minutes, and usually it all comes back OK.

After it starts, I have no issue with any of the functions.

To exclude the power being an issue, I'm using a Adafruit PowerBoost 1000 to provide a current buffer on the power rail.

Anyone seen similar issues?

Bas

basrijn
 
Posts: 81
Joined: Tue Nov 28, 2017 11:24 pm

Re: RTC.adjust() or RTC.lostpower() hangs regularly (DS3231)

by adafruit_support_carter on Tue Apr 07, 2020 12:46 pm

Can you provide more information on your setup. What are you using as the main controller board?

adafruit_support_carter
 
Posts: 17179
Joined: Tue Nov 29, 2016 2:45 pm

Re: RTC.adjust() or RTC.lostpower() hangs regularly (DS3231)

by basrijn on Tue Apr 07, 2020 1:14 pm

Hi,

Metro M4 Grand Central, but seeing the same issue on a regular Metro M4. The Grand Central has a lora modem, tft, GPS in addition to the RTC.

The Metro has Lora only in addition to the RTC.

I found that I can re-upload the sketch and usually with one re-upload (sometimes 2) it will reset to the point it's fine. It's also very variable, I've had instances where 6 reboots all happened without a hang. But most of the time, it hangs every other reboot or so.

Must be timing with something else that triggers it, it's the first init I do in setup. Tried with delay()'s around it (and without.

I guess this is where a hardware debugger would be handy :) ?

Bas

basrijn
 
Posts: 81
Joined: Tue Nov 28, 2017 11:24 pm

Re: RTC.adjust() or RTC.lostpower() hangs regularly (DS3231)

by adafruit_support_carter on Tue Apr 07, 2020 1:43 pm

For the sake of troubleshooting, can you setup with just the Metro M4 and the RTC?

adafruit_support_carter
 
Posts: 17179
Joined: Tue Nov 29, 2016 2:45 pm

Re: RTC.adjust() or RTC.lostpower() hangs regularly (DS3231)

by basrijn on Wed Apr 08, 2020 12:35 pm

adafruit_support_carter wrote:For the sake of troubleshooting, can you setup with just the Metro M4 and the RTC?


Hmm, I just restarted the M4 (with Lora attached) about 20 times and it came up without issues every time.

The more complex setup with the Grand Central still shows the same issue.

Would a hardware debugger help with this type of issue? I'm assuming I would see where the code is sitting when it hangs?

Bas

basrijn
 
Posts: 81
Joined: Tue Nov 28, 2017 11:24 pm

Re: RTC.adjust() or RTC.lostpower() hangs regularly (DS3231)

by adafruit_support_carter on Wed Apr 08, 2020 5:50 pm

First just try simplifying the complex setup. Setup just the GC and the RTC and make sure basic functionality works. If it doesn't, then troubleshoot that setup. If it does work, then slowly add back complexity, checking functionality each step, until you find where it stops working. Then troubleshoot why with that setup.

adafruit_support_carter
 
Posts: 17179
Joined: Tue Nov 29, 2016 2:45 pm

Re: RTC.adjust() or RTC.lostpower() hangs regularly (DS3231)

by User_UMjT7KxnxP8YN8 on Wed Apr 08, 2020 10:44 pm

basrijn wrote:From some googling it suggest that rtc.begin() ALWAYS returns true, I know that to be incorrect, because I have seen it return false :) But in my hang condition rtc.begin() did return true.
Bas


I was one of those who noted that the DS3231 library's rtc.begin() always returned true. I fixed it and posted the new code; perhaps the version you're using incorporates my change.

Code: Select all | TOGGLE FULL SIZE
     Wire.beginTransmission (DS3231_ADDRESS);                  // added 7/24/2018 at 1:13pm
     if (Wire.endTransmission () == 0)                        //
      return true;                                    
     else                                              //
      return false;                                    //


Have you considered using battery backup with the ATSAMD51J19A and going into low-power mode if external power is lost? You could use the internal RTC and perhaps get other benefits from having the MCU 'always on', as it has the ability to wake up when external events occur.

User_UMjT7KxnxP8YN8
 
Posts: 170
Joined: Tue Jul 17, 2018 1:28 pm

Re: RTC.adjust() or RTC.lostpower() hangs regularly (DS3231)

by basrijn on Wed Apr 15, 2020 1:18 am

Adafruit_UMjT7KxnxP8YN8 wrote:
basrijn wrote:From some googling it suggest that rtc.begin() ALWAYS returns true, I know that to be incorrect, because I have seen it return false :) But in my hang condition rtc.begin() did return true.
Bas


I was one of those who noted that the DS3231 library's rtc.begin() always returned true. I fixed it and posted the new code; perhaps the version you're using incorporates my change.

Code: Select all | TOGGLE FULL SIZE
     Wire.beginTransmission (DS3231_ADDRESS);                  // added 7/24/2018 at 1:13pm
     if (Wire.endTransmission () == 0)                        //
      return true;                                    
     else                                              //
      return false;                                    //


Have you considered using battery backup with the ATSAMD51J19A and going into low-power mode if external power is lost? You could use the internal RTC and perhaps get other benefits from having the MCU 'always on', as it has the ability to wake up when external events occur.


I just spent a little more time looking at the library, and noticed the code you listed above is indeed part of it.

I added the same error check in read_i2c_register, when I have the issue with the RTC, I do not get any serial output for the endTransmission() call.

My call is to
Code: Select all | TOGGLE FULL SIZE
rtc.lostPower()
, that translates to
Code: Select all | TOGGLE FULL SIZE
return (read_i2c_register(DS3231_ADDRESS, DS3231_STATUSREG) >> 7);


read_i2c_register has:
Code: Select all | TOGGLE FULL SIZE
Wire.beginTransmission(addr);
Wire._I2C_WRITE((byte)reg);
Wire.endTransmission();


So that must mean _I2C_WRITE() hangs? Following that down leads to stuff way past my skill level.

Is there any logical explanation for the _I2C_WRITE occasionally failing? If the unit boots OK, I don't seem to have issues further down the line

Bas

basrijn
 
Posts: 81
Joined: Tue Nov 28, 2017 11:24 pm

Re: RTC.adjust() or RTC.lostpower() hangs regularly (DS3231)

by User_UMjT7KxnxP8YN8 on Wed Apr 15, 2020 11:21 am

I haven't used the DS3231 for over a year, having ditched it in favor of the Metro M4's built-in RTC because it's accurate and included in the cost of the board. The only downside is that more effort is required to implement battery backup since Adafruit's done all the engineering for the DS3231 but not for the Metro M4.

But I dug through my old code today and found my DS3231 initialization code, which may help you. My code got an initial time setting from an NTP server, then updated the time every hour to keep the DS321 reasonably accurate (mine was driving a Nixie Clock).

Note that I don't try to communicate with the DS3231 unless RTC.begin() succeeds.

Settings is a global structure in which I keep my clock settings while operating; they're written to the Arduino Mega's EEPROM for persistence across resets. The point of the Settings.timeValid check is to determine whether the DS3231 has been initialized to a a valid time by a previous NTP response.

You can replace DEBUG_PRINT with Serial.Print if you want to see the return values.

Code: Select all | TOGGLE FULL SIZE
  // Start RTC. If RTC is present but time is not valid (e.g., first startup, no previous sync with NTP server, RTC power loss), time update will be initiated during first iteration of loop.
  // Time validity will be updated after sync with NTP server. If RTC is not detected, system time will be set to NTP server time so system functions, though with degraded time accuracy, and
  // NTP updates will be scheduled more frequently to compensate for system clock drift.
  RTCpresent = rtc.begin();
  DEBUG_PRINT(F("RTCpresent = "));
  DEBUG_PRINT(RTCpresent);
  DEBUG_PRINT(F(", rtc.lostPower() = "));
  DEBUG_PRINTLN(rtc.lostPower());

  if (RTCpresent && (!rtc.lostPower()) && Settings.timeValid) {  // if RTC detected and it hasn't lost power & time is valid...
    RTCtime = rtc.now();                                         //  get time from it...
    setTime(RTCtime.unixtime());                                 //  and set system time
  }

User_UMjT7KxnxP8YN8
 
Posts: 170
Joined: Tue Jul 17, 2018 1:28 pm

Re: RTC.adjust() or RTC.lostpower() hangs regularly (DS3231)

by basrijn on Wed Apr 15, 2020 11:34 am

Hi,

My projects is a timing system with two nodes connected via radio. A GPS is used to set the RTC on the main unit. With some trickery I set the RTC on the remote unit. I can consistently get them to within 5 ms of each other (as measured with a button press tied to both units).

Everything works very well, other then the random hangs on startup. Did more reading and I'm guessing the underlying cause is on the electrical level.

I'm going to pull my whole breadboard setup apart and build it back up. Maybe it's a poor connection somewhere on the board.

TX!
Bas

basrijn
 
Posts: 81
Joined: Tue Nov 28, 2017 11:24 pm

Re: RTC.adjust() or RTC.lostpower() hangs regularly (DS3231)

by adafruit_support_bill on Wed Apr 15, 2020 11:38 am

Is there any logical explanation for the _I2C_WRITE occasionally failing?

Intermittent hanging on i2c writes is a fairly common symptom of a marginal connection on the i2c bus. Recheck your solder joints and connections.

adafruit_support_bill
 
Posts: 79034
Joined: Sat Feb 07, 2009 10:11 am

Please be positive and constructive with your questions and comments.