Skip to content

Commit 7b21c50

Browse files
author
JChristensen
committed
Add support for TinyWireM and ATtiny44/45/84/85.
1 parent b6dbb0a commit 7b21c50

File tree

3 files changed

+73
-46
lines changed

3 files changed

+73
-46
lines changed

MCP79412RTC.cpp

Lines changed: 49 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -22,14 +22,15 @@
2222
*----------------------------------------------------------------------*/
2323

2424
#include <Wire.h>
25+
#include <TinyWireM.h>
2526
#include "MCP79412RTC.h"
2627

2728
/*----------------------------------------------------------------------*
2829
* Constructor. *
2930
*----------------------------------------------------------------------*/
3031
MCP79412RTC::MCP79412RTC()
3132
{
32-
Wire.begin();
33+
i2cBegin();
3334
}
3435

3536
/*----------------------------------------------------------------------*
@@ -63,14 +64,14 @@ void MCP79412RTC::set(time_t t)
6364
*----------------------------------------------------------------------*/
6465
boolean MCP79412RTC::read(tmElements_t &tm)
6566
{
66-
Wire.beginTransmission(RTC_ADDR);
67+
i2cBeginTransmission(RTC_ADDR);
6768
i2cWrite((uint8_t)TIME_REG);
68-
if (Wire.endTransmission() != 0) {
69+
if (i2cEndTransmission() != 0) {
6970
return false;
7071
}
7172
else {
7273
//request 7 bytes (secs, min, hr, dow, date, mth, yr)
73-
Wire.requestFrom(RTC_ADDR, tmNbrFields);
74+
i2cRequestFrom(RTC_ADDR, tmNbrFields);
7475
tm.Second = bcd2dec(i2cRead() & ~_BV(ST));
7576
tm.Minute = bcd2dec(i2cRead());
7677
tm.Hour = bcd2dec(i2cRead() & ~_BV(HR1224)); //assumes 24hr clock
@@ -87,7 +88,7 @@ boolean MCP79412RTC::read(tmElements_t &tm)
8788
*----------------------------------------------------------------------*/
8889
void MCP79412RTC::write(tmElements_t &tm)
8990
{
90-
Wire.beginTransmission(RTC_ADDR);
91+
i2cBeginTransmission(RTC_ADDR);
9192
i2cWrite((uint8_t)TIME_REG);
9293
i2cWrite((uint8_t)0x00); //stops the oscillator (Bit 7, ST == 0)
9394
i2cWrite(dec2bcd(tm.Minute));
@@ -96,12 +97,12 @@ void MCP79412RTC::write(tmElements_t &tm)
9697
i2cWrite(dec2bcd(tm.Day));
9798
i2cWrite(dec2bcd(tm.Month));
9899
i2cWrite(dec2bcd(tmYearToY2k(tm.Year)));
99-
Wire.endTransmission();
100+
i2cEndTransmission();
100101

101-
Wire.beginTransmission(RTC_ADDR);
102+
i2cBeginTransmission(RTC_ADDR);
102103
i2cWrite((uint8_t)TIME_REG);
103104
i2cWrite(dec2bcd(tm.Second) | _BV(ST)); //set the seconds and start the oscillator (Bit 7, ST == 1)
104-
Wire.endTransmission();
105+
i2cEndTransmission();
105106
}
106107

107108
/*----------------------------------------------------------------------*
@@ -121,10 +122,10 @@ void MCP79412RTC::ramWrite(byte addr, byte value)
121122
*----------------------------------------------------------------------*/
122123
void MCP79412RTC::ramWrite(byte addr, byte *values, byte nBytes)
123124
{
124-
Wire.beginTransmission(RTC_ADDR);
125+
i2cBeginTransmission(RTC_ADDR);
125126
i2cWrite(addr);
126127
for (byte i=0; i<nBytes; i++) i2cWrite(values[i]);
127-
Wire.endTransmission();
128+
i2cEndTransmission();
128129
}
129130

130131
/*----------------------------------------------------------------------*
@@ -147,10 +148,10 @@ byte MCP79412RTC::ramRead(byte addr)
147148
*----------------------------------------------------------------------*/
148149
void MCP79412RTC::ramRead(byte addr, byte *values, byte nBytes)
149150
{
150-
Wire.beginTransmission(RTC_ADDR);
151+
i2cBeginTransmission(RTC_ADDR);
151152
i2cWrite(addr);
152-
Wire.endTransmission();
153-
Wire.requestFrom( (uint8_t)RTC_ADDR, nBytes );
153+
i2cEndTransmission();
154+
i2cRequestFrom( (uint8_t)RTC_ADDR, nBytes );
154155
for (byte i=0; i<nBytes; i++) values[i] = i2cRead();
155156
}
156157

@@ -174,7 +175,11 @@ void MCP79412RTC::sramWrite(byte addr, byte value)
174175
*----------------------------------------------------------------------*/
175176
void MCP79412RTC::sramWrite(byte addr, byte *values, byte nBytes)
176177
{
178+
#if defined(__AVR_ATtiny44__) || defined(__AVR_ATtiny84__) || defined(__AVR_ATtiny45__) || defined(__AVR_ATtiny85__)
179+
if (nBytes >= 1 && (addr + nBytes) <= SRAM_SIZE) {
180+
#else
177181
if (nBytes >= 1 && nBytes <= (BUFFER_LENGTH - 1) && (addr + nBytes) <= SRAM_SIZE) {
182+
#endif
178183
ramWrite( (addr & (SRAM_SIZE - 1) ) + SRAM_START_ADDR, values, nBytes );
179184
}
180185
}
@@ -202,7 +207,11 @@ byte MCP79412RTC::sramRead(byte addr)
202207
*----------------------------------------------------------------------*/
203208
void MCP79412RTC::sramRead(byte addr, byte *values, byte nBytes)
204209
{
210+
#if defined(__AVR_ATtiny44__) || defined(__AVR_ATtiny84__) || defined(__AVR_ATtiny45__) || defined(__AVR_ATtiny85__)
211+
if (nBytes >= 1 && (addr + nBytes) <= SRAM_SIZE) {
212+
#else
205213
if (nBytes >= 1 && nBytes <= BUFFER_LENGTH && (addr + nBytes) <= SRAM_SIZE) {
214+
#endif
206215
ramRead((addr & (SRAM_SIZE - 1) ) + SRAM_START_ADDR, values, nBytes);
207216
}
208217
}
@@ -215,11 +224,11 @@ void MCP79412RTC::sramRead(byte addr, byte *values, byte nBytes)
215224
*----------------------------------------------------------------------*/
216225
void MCP79412RTC::eepromWrite(byte addr, byte value)
217226
{
218-
Wire.beginTransmission(EEPROM_ADDR);
219-
i2cWrite( addr & (EEPROM_SIZE - 1) );
220-
i2cWrite(value);
221-
Wire.endTransmission();
222-
eepromWait();
227+
i2cBeginTransmission(EEPROM_ADDR);
228+
i2cWrite( addr & (EEPROM_SIZE - 1) );
229+
i2cWrite(value);
230+
i2cEndTransmission();
231+
eepromWait();
223232
}
224233

225234
/*----------------------------------------------------------------------*
@@ -232,10 +241,10 @@ void MCP79412RTC::eepromWrite(byte addr, byte value)
232241
void MCP79412RTC::eepromWrite(byte addr, byte *values, byte nBytes)
233242
{
234243
if (nBytes >= 1 && nBytes <= EEPROM_PAGE_SIZE) {
235-
Wire.beginTransmission(EEPROM_ADDR);
244+
i2cBeginTransmission(EEPROM_ADDR);
236245
i2cWrite( addr & ~(EEPROM_PAGE_SIZE - 1) & (EEPROM_SIZE - 1) );
237246
for (byte i=0; i<nBytes; i++) i2cWrite(values[i]);
238-
Wire.endTransmission();
247+
i2cEndTransmission();
239248
eepromWait();
240249
}
241250
}
@@ -263,11 +272,15 @@ byte MCP79412RTC::eepromRead(byte addr)
263272
*----------------------------------------------------------------------*/
264273
void MCP79412RTC::eepromRead(byte addr, byte *values, byte nBytes)
265274
{
275+
#if defined(__AVR_ATtiny44__) || defined(__AVR_ATtiny84__) || defined(__AVR_ATtiny45__) || defined(__AVR_ATtiny85__)
276+
if (nBytes >= 1 && (addr + nBytes) <= EEPROM_SIZE) {
277+
#else
266278
if (nBytes >= 1 && nBytes <= BUFFER_LENGTH && (addr + nBytes) <= EEPROM_SIZE) {
267-
Wire.beginTransmission(EEPROM_ADDR);
279+
#endif
280+
i2cBeginTransmission(EEPROM_ADDR);
268281
i2cWrite( addr & (EEPROM_SIZE - 1) );
269-
Wire.endTransmission();
270-
Wire.requestFrom( (uint8_t)EEPROM_ADDR, nBytes );
282+
i2cEndTransmission();
283+
i2cRequestFrom( (uint8_t)EEPROM_ADDR, nBytes );
271284
for (byte i=0; i<nBytes; i++) values[i] = i2cRead();
272285
}
273286
}
@@ -283,9 +296,9 @@ byte MCP79412RTC::eepromWait(void)
283296
do
284297
{
285298
++waitCount;
286-
Wire.beginTransmission(EEPROM_ADDR);
299+
i2cBeginTransmission(EEPROM_ADDR);
287300
i2cWrite((uint8_t)0);
288-
txStatus = Wire.endTransmission();
301+
txStatus = i2cEndTransmission();
289302

290303
} while (txStatus != 0);
291304

@@ -328,10 +341,10 @@ void MCP79412RTC::calibWrite(int value)
328341
*----------------------------------------------------------------------*/
329342
void MCP79412RTC::idRead(byte *uniqueID)
330343
{
331-
Wire.beginTransmission(EEPROM_ADDR);
344+
i2cBeginTransmission(EEPROM_ADDR);
332345
i2cWrite(UNIQUE_ID_ADDR);
333-
Wire.endTransmission();
334-
Wire.requestFrom( EEPROM_ADDR, UNIQUE_ID_SIZE );
346+
i2cEndTransmission();
347+
i2cRequestFrom( EEPROM_ADDR, UNIQUE_ID_SIZE );
335348
for (byte i=0; i<UNIQUE_ID_SIZE; i++) uniqueID[i] = i2cRead();
336349
}
337350

@@ -365,11 +378,11 @@ boolean MCP79412RTC::powerFail(time_t *powerDown, time_t *powerUp)
365378
ramRead(YEAR_REG, &yr, 1);
366379
yr = y2kYearToTm(bcd2dec(yr));
367380
if ( day & _BV(VBAT) ) {
368-
Wire.beginTransmission(RTC_ADDR);
381+
i2cBeginTransmission(RTC_ADDR);
369382
i2cWrite(PWRDWN_TS_REG);
370-
Wire.endTransmission();
383+
i2cEndTransmission();
371384

372-
Wire.requestFrom(RTC_ADDR, TIMESTAMP_SIZE); //read both timestamp registers, 8 bytes total
385+
i2cRequestFrom(RTC_ADDR, TIMESTAMP_SIZE); //read both timestamp registers, 8 bytes total
373386
dn.Second = 0;
374387
dn.Minute = bcd2dec(i2cRead());
375388
dn.Hour = bcd2dec(i2cRead() & ~_BV(HR1224)); //assumes 24hr clock
@@ -437,15 +450,15 @@ void MCP79412RTC::setAlarm(uint8_t alarmNumber, time_t alarmTime)
437450
alarmNumber &= 0x01; //ensure a valid alarm number
438451
ramRead( ALM0_DAY + alarmNumber * (ALM1_REG - ALM0_REG) , &day, 1);
439452
breakTime(alarmTime, tm);
440-
Wire.beginTransmission(RTC_ADDR);
453+
i2cBeginTransmission(RTC_ADDR);
441454
i2cWrite( ALM0_REG + alarmNumber * (ALM1_REG - ALM0_REG) );
442455
i2cWrite(dec2bcd(tm.Second));
443456
i2cWrite(dec2bcd(tm.Minute));
444457
i2cWrite(dec2bcd(tm.Hour)); //sets 24 hour format (Bit 6 == 0)
445458
i2cWrite( (day & 0xF8) + tm.Wday );
446459
i2cWrite(dec2bcd(tm.Day));
447460
i2cWrite(dec2bcd(tm.Month));
448-
Wire.endTransmission();
461+
i2cEndTransmission();
449462
}
450463

451464
/*----------------------------------------------------------------------*
@@ -537,12 +550,12 @@ void MCP79412RTC::alarmPolarity(boolean polarity)
537550
*----------------------------------------------------------------------*/
538551
boolean MCP79412RTC::isRunning(void)
539552
{
540-
Wire.beginTransmission(RTC_ADDR);
553+
i2cBeginTransmission(RTC_ADDR);
541554
i2cWrite((uint8_t)TIME_REG);
542-
Wire.endTransmission();
555+
i2cEndTransmission();
543556

544557
//request just the seconds register
545-
Wire.requestFrom(RTC_ADDR, 1);
558+
i2cRequestFrom(RTC_ADDR, 1);
546559
return i2cRead() & _BV(ST);
547560
}
548561

MCP79412RTC.h

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,10 +32,25 @@
3232
#endif
3333

3434
//define release-independent I2C functions
35-
#if ARDUINO >= 100
35+
#if defined(__AVR_ATtiny44__) || defined(__AVR_ATtiny84__) || defined(__AVR_ATtiny45__) || defined(__AVR_ATtiny85__)
36+
#define i2cBegin TinyWireM.begin
37+
#define i2cBeginTransmission TinyWireM.beginTransmission
38+
#define i2cEndTransmission TinyWireM.endTransmission
39+
#define i2cRequestFrom TinyWireM.requestFrom
40+
#define i2cRead TinyWireM.receive
41+
#define i2cWrite TinyWireM.send
42+
#elif ARDUINO >= 100
43+
#define i2cBegin Wire.begin
44+
#define i2cBeginTransmission Wire.beginTransmission
45+
#define i2cEndTransmission Wire.endTransmission
46+
#define i2cRequestFrom Wire.requestFrom
3647
#define i2cRead Wire.read
3748
#define i2cWrite Wire.write
3849
#else
50+
#define i2cBegin Wire.begin
51+
#define i2cBeginTransmission Wire.beginTransmission
52+
#define i2cEndTransmission Wire.endTransmission
53+
#define i2cRequestFrom Wire.requestFrom
3954
#define i2cRead Wire.receive
4055
#define i2cWrite Wire.send
4156
#endif

ReadMe.txt

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -17,19 +17,18 @@ Michael Margolis that is supplied with the Arduino Time library above. To change
1717
from using a DS1307 RTC to an MCP79412 RTC, it is only necessary to change the
1818
#include statement to include MCP79412RTC.h instead of DS1307RTC.h.
1919

20-
This library also implements functions to support the additional
21-
features of the MCP79412.
20+
This library also implements methods to support the additional features
21+
of the MCP79412.
2222

2323
--------------------------------------------------------------------------------
2424
To use the library:
25-
(1) Go to https://github.com/JChristensen/MCP79412RTC/downloads and download the
26-
file in the compressed format of your choice (zip or tar.gz) to a convenient
27-
location on your PC.
28-
(2) Uncompress the downloaded file. This will result in a folder containing all
29-
the files for the library, that has a name similar to "JChristensen-
30-
MCP79412-42e98a7".
25+
(1) Go to https://github.com/JChristensen/MCP79412RTC and click the
26+
Download ZIP button and save the ZIP file to a convenient location on your PC.
27+
(2) Uncompress the downloaded file. This will result in a folder containing
28+
the library files, which has a name that includes the branch name,
29+
e.g. MCP79412-master.
3130
(3) Rename the folder to just "MCP79412".
32-
(4) Copy the renamed folder to the Arduino sketchbook\libraries folder.
31+
(4) Move the renamed folder to the Arduino sketchbook\libraries folder.
3332

3433
--------------------------------------------------------------------------------
3534
The following example sketches are included with the Timezone library:

0 commit comments

Comments
 (0)