2222 *----------------------------------------------------------------------*/
2323
2424#include < Wire.h>
25+ #include < TinyWireM.h>
2526#include " MCP79412RTC.h"
2627
2728/* ----------------------------------------------------------------------*
2829 * Constructor. *
2930 *----------------------------------------------------------------------*/
3031MCP79412RTC::MCP79412RTC ()
3132{
32- Wire. begin ();
33+ i2cBegin ();
3334}
3435
3536/* ----------------------------------------------------------------------*
@@ -63,14 +64,14 @@ void MCP79412RTC::set(time_t t)
6364 *----------------------------------------------------------------------*/
6465boolean 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 *----------------------------------------------------------------------*/
8889void 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 *----------------------------------------------------------------------*/
122123void 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 *----------------------------------------------------------------------*/
148149void 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 *----------------------------------------------------------------------*/
175176void 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 *----------------------------------------------------------------------*/
203208void 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 *----------------------------------------------------------------------*/
216225void 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)
232241void 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 *----------------------------------------------------------------------*/
264273void 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 *----------------------------------------------------------------------*/
329342void 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 *----------------------------------------------------------------------*/
538551boolean 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
0 commit comments