@@ -34,13 +34,16 @@ MCP79412RTC::MCP79412RTC()
3434
3535/* ----------------------------------------------------------------------*
3636 * Read the current time from the RTC and return it as a time_t value. *
37+ * Returns a zero value if RTC not present (I2C I/O error). *
3738 *----------------------------------------------------------------------*/
3839time_t MCP79412RTC::get ()
3940{
4041 tmElements_t tm;
4142
42- read (tm);
43- return (makeTime (tm));
43+ if ( read (tm) )
44+ return ( makeTime (tm) );
45+ else
46+ return 0 ;
4447}
4548
4649/* ----------------------------------------------------------------------*
@@ -56,23 +59,27 @@ void MCP79412RTC::set(time_t t)
5659
5760/* ----------------------------------------------------------------------*
5861 * Read the current time from the RTC and return it in a tmElements_t *
59- * structure. *
62+ * structure. Returns false if RTC not present (I2C I/O error). *
6063 *----------------------------------------------------------------------*/
61- void MCP79412RTC::read (tmElements_t &tm)
64+ boolean MCP79412RTC::read (tmElements_t &tm)
6265{
6366 Wire.beginTransmission (RTC_ADDR);
6467 i2cWrite (TIME_REG);
65- Wire.endTransmission ();
66-
67- // request 7 bytes (secs, min, hr, dow, date, mth, yr)
68- Wire.requestFrom (RTC_ADDR, tmNbrFields);
69- tm.Second = bcd2dec (i2cRead () & ~_BV (ST));
70- tm.Minute = bcd2dec (i2cRead ());
71- tm.Hour = bcd2dec (i2cRead () & ~_BV (HR1224)); // assumes 24hr clock
72- tm.Wday = i2cRead () & ~(_BV (OSCON) | _BV (VBAT) | _BV (VBATEN)); // mask off OSCON, VBAT, VBATEN bits
73- tm.Day = bcd2dec (i2cRead ());
74- tm.Month = bcd2dec (i2cRead () & ~_BV (LP)); // mask off the leap year bit
75- tm.Year = y2kYearToTm (bcd2dec (i2cRead ()));
68+ if (Wire.endTransmission () != 0 ) {
69+ return false ;
70+ }
71+ else {
72+ // request 7 bytes (secs, min, hr, dow, date, mth, yr)
73+ Wire.requestFrom (RTC_ADDR, tmNbrFields);
74+ tm.Second = bcd2dec (i2cRead () & ~_BV (ST));
75+ tm.Minute = bcd2dec (i2cRead ());
76+ tm.Hour = bcd2dec (i2cRead () & ~_BV (HR1224)); // assumes 24hr clock
77+ tm.Wday = i2cRead () & ~(_BV (OSCON) | _BV (VBAT) | _BV (VBATEN)); // mask off OSCON, VBAT, VBATEN bits
78+ tm.Day = bcd2dec (i2cRead ());
79+ tm.Month = bcd2dec (i2cRead () & ~_BV (LP)); // mask off the leap year bit
80+ tm.Year = y2kYearToTm (bcd2dec (i2cRead ()));
81+ return true ;
82+ }
7683}
7784
7885/* ----------------------------------------------------------------------*
0 commit comments