Skip to content

Commit 7c62cb4

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

File tree

4 files changed

+82
-80
lines changed

4 files changed

+82
-80
lines changed

examples/SetSerial/SetSerial.ino

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,6 @@ void setup()
4141
Serial.begin(115200);
4242
Serial << F( "\n" __FILE__ "\n" __DATE__ " " __TIME__ "\n" );
4343
myRTC.begin();
44-
myRTC.dumpRegs();
4544

4645
// setSyncProvider() causes the Time library to synchronize with the
4746
// external RTC by calling myRTC.get() every five minutes by default.

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
Original file line numberDiff line numberDiff line change
@@ -88,54 +88,56 @@ class MCP79412RTC
8888

8989
// MCP7941x Register Addresses
9090
static constexpr uint8_t
91-
TIME_REG {0x00}, // 7 registers, Seconds, Minutes, Hours, DOW, Date, Month, Year
92-
DAY_REG {0x03}, // the RTC Day register contains the OSCON, VBAT, and VBATEN bits
93-
YEAR_REG {0x06}, // RTC year register
94-
CTRL_REG {0x07}, // control register
95-
CALIB_REG {0x08}, // calibration register
96-
UNLOCK_ID_REG {0x09}, // unlock ID register
97-
ALM0_REG {0x0A}, // alarm 0, 6 registers, Seconds, Minutes, Hours, DOW, Date, Month
98-
ALM1_REG {0x11}, // alarm 1, 6 registers, Seconds, Minutes, Hours, DOW, Date, Month
99-
ALM0_DAY {0x0D}, // DOW register has alarm config/flag bits
100-
PWRDWN_TS_REG {0x18}, // power-down timestamp, 4 registers, Minutes, Hours, Date, Month
101-
PWRUP_TS_REG {0x1C}, // power-up timestamp, 4 registers, Minutes, Hours, Date, Month
91+
RTCSEC {0x00}, // 7 registers, Seconds, Minutes, Hours, DOW, Date, Month, Year
92+
RTCWKDAY {0x03}, // the RTC Day register contains the OSCRUN, PWRFAIL, and VBATEN bits
93+
RTCYEAR {0x06}, // RTC year register
94+
CONTROL {0x07}, // control register
95+
OSCTRIM {0x08}, // oscillator calibration register
96+
EEUNLOCK {0x09}, // protected eeprom unlock register
97+
ALM0SEC {0x0A}, // alarm 0, 6 registers, Seconds, Minutes, Hours, DOW, Date, Month
98+
ALM1SEC {0x11}, // alarm 1, 6 registers, Seconds, Minutes, Hours, DOW, Date, Month
99+
ALM0WKDAY {0x0D}, // DOW register has alarm config/flag bits
100+
PWRDNMIN {0x18}, // power-down timestamp, 4 registers, Minutes, Hours, Date, Month
101+
PWRUPMIN {0x1C}, // power-up timestamp, 4 registers, Minutes, Hours, Date, Month
102102
TIMESTAMP_SIZE {8}, // number of bytes in the two timestamp registers
103103
SRAM_START_ADDR {0x20}, // first SRAM address
104104
SRAM_SIZE {64}, // number of bytes of SRAM
105105
EEPROM_SIZE {128}, // number of bytes of EEPROM
106106
EEPROM_PAGE_SIZE{8}, // number of bytes on an EEPROM page
107-
UNIQUE_ID_ADDR {0xF0}, // starting address for unique ID
107+
UNIQUE_ID_ADDR {0xF0}, // starting address for unique ID in EEPROM
108108
UNIQUE_ID_SIZE {8}; // number of bytes in unique ID
109109

110110
// Control Register bits
111111
static constexpr uint8_t
112112
OUT {7}, // sets logic level on MFP when not used as square wave output
113-
SQWE {6}, // set to enable square wave output
114-
ALM1 {5}, // alarm 1 is active
115-
ALM0 {4}, // alarm 0 is active
116-
EXTOSC {3}, // set to drive the RTC registers from an external oscillator instead of a crystal
117-
RS2 {2}, // RS2:0 set square wave output frequency: 0==1Hz, 1==4096Hz, 2==8192Hz, 3=32768Hz
118-
RS1 {1},
119-
RS0 {0};
113+
SQWEN {6}, // set to enable square wave output
114+
ALM1EN {5}, // alarm 1 is active
115+
ALM0EN {4}, // alarm 0 is active
116+
EXTOSC {3}, // enable external oscillator instead of a crystal
117+
CRSTRIM {2}, // coarse trim mode enable
118+
SQWFS1 {1}, // SQWFS1:0 square wave output freq: 0==1Hz, 1==4096Hz, 2==8192Hz, 3=32768Hz
119+
SQWFS0 {0};
120120

121121
// Other Control Bits
122122
static constexpr uint8_t
123-
ST {7}, // Seconds register (TIME_REG) oscillator start/stop bit, 1==Start, 0==Stop
124-
HR1224 {6}, // Hours register (TIME_REG+2) 12 or 24 hour mode (24 hour mode==0)
125-
AMPM {5}, // Hours register (TIME_REG+2) AM/PM bit for 12 hour mode
126-
OSCON {5}, // Day register (TIME_REG+3) oscillator running (set and cleared by hardware)
127-
VBAT {4}, // Day register (TIME_REG+3) set by hardware when Vcc fails and RTC runs on battery.
128-
// VBAT is cleared by software, clearing VBAT also clears the timestamp registers
129-
VBATEN {3}, // Day register (TIME_REG+3) VBATEN==1 enables backup battery, VBATEN==0 disconnects the VBAT pin (e.g. to save battery)
130-
LP {5}; // Month register (TIME_REG+5) leap year bit
123+
STOSC {7}, // Seconds register (RTCSEC) oscillator start/stop bit, 1==Start, 0==Stop
124+
HR1224 {6}, // Hours register (RTCHOUR) 12 or 24 hour mode (24 hour mode==0)
125+
AMPM {5}, // Hours register (RTCHOUR) AM/PM bit for 12 hour mode
126+
OSCRUN {5}, // Day register (RTCWKDAY) oscillator running (set and cleared by hardware)
127+
PWRFAIL {4}, // Day register (RTCWKDAY) set by hardware when Vcc fails and RTC runs on battery.
128+
// PWRFAIL is cleared by software, clearing PWRFAIL also
129+
// clears the timestamp registers
130+
VBATEN {3}, // Day register (RTCWKDAY) VBATEN==1 enables backup
131+
// battery, VBATEN==0 disconnects the VBAT pin (e.g. to save battery)
132+
LPYR {5}; // Month register (RTCMTH) leap year bit
131133

132134
// Alarm Control Bits
133135
static constexpr uint8_t
134-
ALMPOL {7}, // Alarm Polarity: Defines the logic level for the MFP when an alarm is triggered.
135-
ALMC2 {6}, // Alarm configuration bits determine how alarms match. See ALM_MATCH defines below.
136-
ALMC1 {5},
137-
ALMC0 {4},
138-
ALMIF {3}; // Alarm Interrupt Flag: Set by hardware when an alarm was triggered, cleared by software.
136+
ALMPOL {7}, // Alarm Polarity: Defines the logic level for the MFP when an alarm is triggered.
137+
ALMxMSK2 {6}, // Alarm configuration bits determine how alarms match. See ALARM_TYPES_t enum.
138+
ALMxMSK1 {5},
139+
ALMxMSK0 {4},
140+
ALMxIF {3}; // Alarm Interrupt Flag: Set by hardware when an alarm was triggered, cleared by software.
139141

140142
MCP79412RTC() {};
141143
MCP79412RTC(bool initI2C) { (void)initI2C; } // undocumented for backward compatibility

0 commit comments

Comments
 (0)