Change register and bit names to agree with the datasheet. · httogo/MCP79412RTC@7c62cb4 · GitHub
Skip to content

Commit 7c62cb4

Browse files
committed
Change register and bit names to agree with the datasheet.
1 parent 15a3a0f commit 7c62cb4

4 files changed

Lines changed: 82 additions & 80 deletions

File tree

examples/SetSerial/SetSerial.ino

Lines changed: 0 additions & 1 deletion

keywords.txt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,3 +21,6 @@ out KEYWORD2
2121
alarmPolarity KEYWORD2
2222
isRunning KEYWORD2
2323
vbaten KEYWORD2
24+
dumpRegs KEYWORD2
25+
dumpSRAM KEYWORD2
26+
dumpEEPROM KEYWORD2

src/MCP79412RTC.cpp

Lines changed: 45 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -51,19 +51,19 @@ void MCP79412RTC::set(time_t t)
5151
bool MCP79412RTC::read(tmElements_t &tm)
5252
{
5353
i2cBeginTransmission(RTC_ADDR);
54-
i2cWrite(TIME_REG);
54+
i2cWrite(RTCSEC);
5555
if (i2cEndTransmission() != 0) {
5656
return false;
5757
}
5858
else {
5959
// request 7 bytes (secs, min, hr, dow, date, mth, yr)
6060
i2cRequestFrom(RTC_ADDR, static_cast<uint8_t>(tmNbrFields));
61-
tm.Second = bcd2dec(i2cRead() & ~_BV(ST));
61+
tm.Second = bcd2dec(i2cRead() & ~_BV(STOSC));
6262
tm.Minute = bcd2dec(i2cRead());
6363
tm.Hour = bcd2dec(i2cRead() & ~_BV(HR1224)); // assumes 24hr clock
64-
tm.Wday = i2cRead() & ~(_BV(OSCON) | _BV(VBAT) | _BV(VBATEN)); // mask off OSCON, VBAT, VBATEN bits
64+
tm.Wday = i2cRead() & ~(_BV(OSCRUN) | _BV(PWRFAIL) | _BV(VBATEN)); // mask off OSCRUN, PWRFAIL, VBATEN bits
6565
tm.Day = bcd2dec(i2cRead());
66-
tm.Month = bcd2dec(i2cRead() & ~_BV(LP)); // mask off the leap year bit
66+
tm.Month = bcd2dec(i2cRead() & ~_BV(LPYR)); // mask off the leap year bit
6767
tm.Year = y2kYearToTm(bcd2dec(i2cRead()));
6868
return true;
6969
}
@@ -73,8 +73,8 @@ bool MCP79412RTC::read(tmElements_t &tm)
7373
void MCP79412RTC::write(tmElements_t &tm)
7474
{
7575
i2cBeginTransmission(RTC_ADDR);
76-
i2cWrite(TIME_REG);
77-
i2cWrite(0x00); // stops the oscillator (Bit 7, ST == 0)
76+
i2cWrite(RTCSEC);
77+
i2cWrite(0x00); // stops the oscillator (Bit 7, STOSC == 0)
7878
i2cWrite(dec2bcd(tm.Minute));
7979
i2cWrite(dec2bcd(tm.Hour)); // sets 24 hour format (Bit 6 == 0)
8080
i2cWrite(tm.Wday | _BV(VBATEN)); // enable battery backup operation
@@ -84,8 +84,8 @@ void MCP79412RTC::write(tmElements_t &tm)
8484
i2cEndTransmission();
8585

8686
i2cBeginTransmission(RTC_ADDR);
87-
i2cWrite(TIME_REG);
88-
i2cWrite(dec2bcd(tm.Second) | _BV(ST)); // set the seconds and start the oscillator (Bit 7, ST == 1)
87+
i2cWrite(RTCSEC);
88+
i2cWrite(dec2bcd(tm.Second) | _BV(STOSC)); // set the seconds and start the oscillator (Bit 7, STOSC == 1)
8989
i2cEndTransmission();
9090
}
9191

@@ -251,13 +251,11 @@ uint8_t MCP79412RTC::eepromWait()
251251
uint8_t waitCount{0};
252252
uint8_t txStatus;
253253

254-
do
255-
{
254+
do {
256255
++waitCount;
257256
i2cBeginTransmission(EEPROM_ADDR);
258257
i2cWrite(0);
259258
txStatus = i2cEndTransmission();
260-
261259
} while (txStatus != 0);
262260

263261
return waitCount;
@@ -269,7 +267,7 @@ uint8_t MCP79412RTC::eepromWait()
269267
// it and return it to the caller as a regular twos-complement integer.
270268
int16_t MCP79412RTC::calibRead()
271269
{
272-
uint8_t val {ramRead(CALIB_REG)};
270+
uint8_t val {ramRead(OSCTRIM)};
273271

274272
if ( val & 0x80 ) return -(val & 0x7F);
275273
else return val;
@@ -283,7 +281,7 @@ void MCP79412RTC::calibWrite(int16_t value)
283281
if (value >= -127 && value <= 127) {
284282
uint8_t calibVal = abs(value);
285283
if (value < 0) calibVal += 128;
286-
ramWrite(CALIB_REG, calibVal);
284+
ramWrite(OSCTRIM, calibVal);
287285
}
288286
}
289287

@@ -322,7 +320,7 @@ void MCP79412RTC::getEUI64(uint8_t* uniqueID)
322320
// Check to see if a power failure has occurred. If so, returns TRUE
323321
// as the function value, and returns the power down and power up
324322
// timestamps. After returning the time stamps, the RTC's timestamp
325-
// registers are cleared and the VBAT bit which indicates a power
323+
// registers are cleared and the PWRFAIL bit which indicates a power
326324
// failure is reset.
327325
//
328326
// Note that the power down and power up timestamp registers do not
@@ -341,12 +339,12 @@ void MCP79412RTC::getEUI64(uint8_t* uniqueID)
341339
bool MCP79412RTC::powerFail(time_t* powerDown, time_t* powerUp)
342340
{
343341
uint8_t day, yr; // copies of the RTC Day and Year registers
344-
ramRead(DAY_REG, &day, 1);
345-
ramRead(YEAR_REG, &yr, 1);
342+
ramRead(RTCWKDAY, &day, 1);
343+
ramRead(RTCYEAR, &yr, 1);
346344
yr = y2kYearToTm(bcd2dec(yr));
347-
if ( day & _BV(VBAT) ) {
345+
if ( day & _BV(PWRFAIL) ) {
348346
i2cBeginTransmission(RTC_ADDR);
349-
i2cWrite(PWRDWN_TS_REG);
347+
i2cWrite(PWRDNMIN);
350348
i2cEndTransmission();
351349

352350
i2cRequestFrom(RTC_ADDR, TIMESTAMP_SIZE); // read both timestamp registers, 8 bytes total
@@ -367,16 +365,16 @@ bool MCP79412RTC::powerFail(time_t* powerDown, time_t* powerUp)
367365
*powerDown = makeTime(dn);
368366
*powerUp = makeTime(up);
369367

370-
// clear the VBAT bit, which causes the RTC hardware to clear the timestamps too.
368+
// clear the PWRFAIL bit, which causes the RTC hardware to clear the timestamps too.
371369
// I suppose there is a risk here that the day has changed since we read it,
372370
// but the Day of Week is actually redundant data and the makeTime() function
373371
// does not use it. This could be an issue if someone is reading the RTC
374372
// registers directly, but as this library is meant to be used with the Time library,
375373
// and also because we don't provide a method to read the RTC clock/calendar
376374
// registers directly, we won't lose any sleep about it at this point unless
377375
// some issue is actually brought to our attention ;-)
378-
day &= ~_BV(VBAT);
379-
ramWrite(DAY_REG, &day , 1);
376+
day &= ~_BV(PWRFAIL);
377+
ramWrite(RTCWKDAY, &day , 1);
380378

381379
// adjust the powerDown timestamp if needed (see notes above)
382380
if (*powerDown > *powerUp) {
@@ -393,29 +391,29 @@ bool MCP79412RTC::powerFail(time_t* powerDown, time_t* powerUp)
393391
void MCP79412RTC::squareWave(uint8_t freq)
394392
{
395393
uint8_t ctrlReg;
396-
ramRead(CTRL_REG, &ctrlReg, 1);
394+
ramRead(CONTROL, &ctrlReg, 1);
397395
if (freq > 3) {
398-
ctrlReg &= ~_BV(SQWE);
396+
ctrlReg &= ~_BV(SQWEN);
399397
}
400398
else {
401-
ctrlReg = (ctrlReg & 0xF8) | _BV(SQWE) | freq;
399+
ctrlReg = (ctrlReg & 0xF8) | _BV(SQWEN) | freq;
402400
}
403-
ramWrite(CTRL_REG, &ctrlReg, 1);
401+
ramWrite(CONTROL, &ctrlReg, 1);
404402
}
405403

406404
// Set an alarm time. Sets the alarm registers only, does not enable
407405
// the alarm. See enableAlarm().
408406
void MCP79412RTC::setAlarm(ALARM_NBR_t alarmNumber, time_t alarmTime)
409407
{
410408
uint8_t day; // need to preserve bits in the day (of week) register
411-
ramRead( ALM0_DAY + alarmNumber * (ALM1_REG - ALM0_REG) , &day, 1);
409+
ramRead( ALM0WKDAY + alarmNumber * (ALM1SEC - ALM0SEC) , &day, 1);
412410
tmElements_t tm;
413411
breakTime(alarmTime, tm);
414412
i2cBeginTransmission(RTC_ADDR);
415-
i2cWrite( ALM0_REG + alarmNumber * (ALM1_REG - ALM0_REG) );
413+
i2cWrite( ALM0SEC + alarmNumber * (ALM1SEC - ALM0SEC) );
416414
i2cWrite(dec2bcd(tm.Second));
417415
i2cWrite(dec2bcd(tm.Minute));
418-
i2cWrite(dec2bcd(tm.Hour)); // sets 24 hour format (Bit 6 == 0)
416+
i2cWrite(dec2bcd(tm.Hour)); // sets 24 hour format (Bit 6 == 0)
419417
i2cWrite( (day & 0xF8) + tm.Wday );
420418
i2cWrite(dec2bcd(tm.Day));
421419
i2cWrite(dec2bcd(tm.Month));
@@ -427,18 +425,18 @@ void MCP79412RTC::setAlarm(ALARM_NBR_t alarmNumber, time_t alarmTime)
427425
void MCP79412RTC::enableAlarm(ALARM_NBR_t alarmNumber, uint8_t alarmType)
428426
{
429427
uint8_t ctrl; // control register has alarm enable bits
430-
ramRead(CTRL_REG, &ctrl, 1);
428+
ramRead(CONTROL, &ctrl, 1);
431429
if (alarmType < ALM_DISABLE) {
432430
uint8_t day; // alarm day register has config & flag bits
433-
ramRead(ALM0_DAY + alarmNumber * (ALM1_REG - ALM0_REG), &day, 1);
431+
ramRead(ALM0WKDAY + alarmNumber * (ALM1SEC - ALM0SEC), &day, 1);
434432
day = ( day & 0x87 ) | alarmType << 4; // reset interrupt flag, OR in the config bits
435-
ramWrite(ALM0_DAY + alarmNumber * (ALM1_REG - ALM0_REG), &day, 1);
436-
ctrl |= _BV(ALM0 + alarmNumber); // enable the alarm
433+
ramWrite(ALM0WKDAY + alarmNumber * (ALM1SEC - ALM0SEC), &day, 1);
434+
ctrl |= _BV(ALM0EN + alarmNumber); // enable the alarm
437435
}
438436
else {
439-
ctrl &= ~(_BV(ALM0 + alarmNumber)); // disable the alarm
437+
ctrl &= ~(_BV(ALM0EN + alarmNumber)); // disable the alarm
440438
}
441-
ramWrite(CTRL_REG, &ctrl, 1);
439+
ramWrite(CONTROL, &ctrl, 1);
442440
}
443441

444442
// Returns true or false depending on whether the given alarm has been
@@ -447,10 +445,10 @@ void MCP79412RTC::enableAlarm(ALARM_NBR_t alarmNumber, uint8_t alarmType)
447445
bool MCP79412RTC::alarm(ALARM_NBR_t alarmNumber)
448446
{
449447
uint8_t day; // alarm day register has config & flag bits
450-
ramRead( ALM0_DAY + alarmNumber * (ALM1_REG - ALM0_REG), &day, 1);
451-
if (day & _BV(ALMIF)) {
452-
day &= ~_BV(ALMIF); // turn off the alarm "interrupt" flag
453-
ramWrite( ALM0_DAY + alarmNumber * (ALM1_REG - ALM0_REG), &day, 1);
448+
ramRead( ALM0WKDAY + alarmNumber * (ALM1SEC - ALM0SEC), &day, 1);
449+
if (day & _BV(ALMxIF)) {
450+
day &= ~_BV(ALMxIF); // turn off the alarm "interrupt" flag
451+
ramWrite( ALM0WKDAY + alarmNumber * (ALM1SEC - ALM0SEC), &day, 1);
454452
return true;
455453
}
456454
else
@@ -462,12 +460,12 @@ bool MCP79412RTC::alarm(ALARM_NBR_t alarmNumber)
462460
void MCP79412RTC::out(bool level)
463461
{
464462
uint8_t ctrlReg;
465-
ramRead(CTRL_REG, &ctrlReg, 1);
463+
ramRead(CONTROL, &ctrlReg, 1);
466464
if (level)
467465
ctrlReg |= _BV(OUT);
468466
else
469467
ctrlReg &= ~_BV(OUT);
470-
ramWrite(CTRL_REG, &ctrlReg, 1);
468+
ramWrite(CONTROL, &ctrlReg, 1);
471469
}
472470

473471
// Specifies the logic level on the Multi-Function Pin (MFP) when an
@@ -483,25 +481,25 @@ void MCP79412RTC::out(bool level)
483481
void MCP79412RTC::alarmPolarity(bool polarity)
484482
{
485483
uint8_t alm0Day;
486-
ramRead(ALM0_DAY, &alm0Day, 1);
484+
ramRead(ALM0WKDAY, &alm0Day, 1);
487485
if (polarity)
488486
alm0Day |= _BV(OUT);
489487
else
490488
alm0Day &= ~_BV(OUT);
491-
ramWrite(ALM0_DAY, &alm0Day, 1);
489+
ramWrite(ALM0WKDAY, &alm0Day, 1);
492490
}
493491

494-
// Check to see if the RTC's oscillator is started (ST bit in seconds
492+
// Check to see if the RTC's oscillator is started (STOSC bit in seconds
495493
// register). Returns true if started.
496494
bool MCP79412RTC::isRunning()
497495
{
498496
i2cBeginTransmission(RTC_ADDR);
499-
i2cWrite(TIME_REG);
497+
i2cWrite(RTCSEC);
500498
i2cEndTransmission();
501499

502500
// request just the seconds register
503501
i2cRequestFrom(RTC_ADDR, static_cast<uint8_t>(1));
504-
return i2cRead() & _BV(ST);
502+
return i2cRead() & _BV(STOSC);
505503
}
506504

507505
// Set or clear the VBATEN bit. Setting the bit powers the clock and
@@ -510,13 +508,13 @@ bool MCP79412RTC::isRunning()
510508
void MCP79412RTC::vbaten(bool enable)
511509
{
512510
uint8_t day;
513-
ramRead(DAY_REG, &day, 1);
511+
ramRead(RTCWKDAY, &day, 1);
514512
if (enable)
515513
day |= _BV(VBATEN);
516514
else
517515
day &= ~_BV(VBATEN);
518516

519-
ramWrite(DAY_REG, &day, 1);
517+
ramWrite(RTCWKDAY, &day, 1);
520518
return;
521519
}
522520

src/MCP79412RTC.h

Lines changed: 34 additions & 32 deletions

0 commit comments

Comments
 (0)