diff --git a/configs/ds1337/ds1337.h b/configs/ds1337/ds1337.h index 0c14c42..8ca56cd 100644 --- a/configs/ds1337/ds1337.h +++ b/configs/ds1337/ds1337.h @@ -28,7 +28,7 @@ #endif // Uncomment this to use integrity check functions - //#define DS1337_USE_OSC_INTEGRITY + #define DS1337_USE_OSC_INTEGRITY // Uncomment this to support square wave output // If this is enabled, alarm 2 will output it's interrupt on INTA diff --git a/ds1337/ds1337.cpp b/ds1337/ds1337.cpp index ad1507d..c5453b5 100644 --- a/ds1337/ds1337.cpp +++ b/ds1337/ds1337.cpp @@ -35,6 +35,8 @@ DS1337 RTC = DS1337(); #ifdef WIRE_LIB_SCAN_MOD int8_t DS1337::Init(void) { + delay(500); //Account for the crystal startup time + // Check address and returns false is there is an error if (Wire.checkAddress(DS1337_WADDR)) { // Possibly set the default registers here @@ -42,17 +44,13 @@ int8_t DS1337::Init(void) clockExists = true; // Start the oscillator if need - if (getRegisterBit(DS1337_SP, DS1337_SP_EOSC)) + if (getRegisterBit(DS1337_SP, DS1337_SP_EOSC) || getRegisterBit(DS1337_STATUS, DS1337_STATUS_OSF)) { - if (getRegisterBit(DS1337_STATUS, DS1337_STATUS_OSF)) - { - unsetRegister(DS1337_STATUS, DS1337_STATUS_OSF); - } + clockStart(); + #if defined(DS1337_USE_ALARM_INTERRUPTS) && !defined(DS1337_USE_SQW_OUTPUT) setRegister(DS1337_SP, DS1337_SQW_INTCN); #endif - - clockStart(); } return DS1337_WADDR; @@ -63,25 +61,17 @@ int8_t DS1337::Init(void) #else int8_t DS1337::Init(void) { - // Account for the crystal power up! - delay(250); + delay(500); //Account for the crystal startup time clockExists = true; // Start the oscillator if need - if (getRegisterBit(DS1337_SP, DS1337_SP_EOSC)) - { - if (getRegisterBit(DS1337_STATUS, DS1337_STATUS_OSF)) - { - unsetRegister(DS1337_STATUS, DS1337_STATUS_OSF); - } - + if (getRegisterBit(DS1337_SP, DS1337_SP_EOSC) || getRegisterBit(DS1337_STATUS, DS1337_STATUS_OSF)) + { clockStart(); - - return DS1337_WADDR; } - return -1; + return DS1337_WADDR; } #endif @@ -122,6 +112,15 @@ uint8_t DS1337::getRegister(uint8_t registerNumber) return Wire.receive(); } +void DS1337::clockStart(void) +{ + // Start the oscillator + unsetRegister(DS1337_SP, DS1337_SP_EOSC); + + // Reset the status register + writeRegister(DS1337_STATUS, B00000000); +} + void DS1337::clockRead(void) { if (!clockExists) return; @@ -144,6 +143,9 @@ void DS1337::clockSave(void) { if (!clockExists) return; + // Stop the clock + clockStop(); + Wire.beginTransmission(DS1337_WADDR); Wire.send(0x00); @@ -154,7 +156,8 @@ void DS1337::clockSave(void) Wire.endTransmission(); - delay(10); + // Restart the oscillator + clockStart(); } void DS1337::clockGet(uint16_t *rtc) @@ -267,7 +270,7 @@ void DS1337::clockSet(uint8_t timeSection, uint16_t timeValue) } break; } // end switch - + clockSave(); } #endif @@ -452,14 +455,8 @@ void DS1337::clockSetWithUTS(uint32_t unixTimeStamp, boolean correctedTime) // Serial.print(tt, DEC); // SPrint(" "); - // Stop the clock - //clockStop(); // Uneeded with the ds1337 - // Save buffer to the RTC clockSave(); - - // Restart the oscillator - //clockStart(); // Uneeded with the ds1337 } #endif @@ -498,13 +495,13 @@ void DS1337::alarmChecks(void) if (getRegisterBit(DS1337_STATUS, DS1337_STATUS_A1F)) { unsetRegister(DS1337_STATUS, DS1337_STATUS_A1F); delay(10); - if (DS1337callbackFunc) DS1337callbackFunc[0](); + if (DS1337callbackFunc[0]) DS1337callbackFunc[0](); } if (getRegisterBit(DS1337_STATUS, DS1337_STATUS_A2F)) { unsetRegister(DS1337_STATUS, DS1337_STATUS_A2F); delay(10); - if (DS1337callbackFunc) DS1337callbackFunc[1](); + if (DS1337callbackFunc[1]) DS1337callbackFunc[1](); } return; @@ -522,28 +519,28 @@ void DS1337::alarmSet(uint8_t timeSection, uint8_t timeValue) case DS1337_SEC: if(alarmId == false && timeValue<60) { - rtc_alarm[DS1337_SEC] = binToBcd(timeValue) & ~DS1337_ALARM_MASK | (rtc_alarm[DS1337_SEC] & DS1337_ALARM_MASK); + rtc_alarm[DS1337_SEC] = (binToBcd(timeValue) & ~DS1337_ALARM_MASK) | (rtc_alarm[DS1337_SEC] & DS1337_ALARM_MASK); } break; case DS1337_MIN: if(timeValue < 60) { - rtc_alarm[DS1337_MIN] = binToBcd(timeValue) & ~DS1337_ALARM_MASK | (rtc_alarm[DS1337_MIN] & DS1337_ALARM_MASK); + rtc_alarm[DS1337_MIN] = (binToBcd(timeValue) & ~DS1337_ALARM_MASK) | (rtc_alarm[DS1337_MIN] & DS1337_ALARM_MASK); } break; case DS1337_HR: if(timeValue < 24) { - rtc_alarm[DS1337_HR] = binToBcd(timeValue) & ~DS1337_ALARM_MASK | (rtc_alarm[DS1337_HR] & DS1337_ALARM_MASK); + rtc_alarm[DS1337_HR] = (binToBcd(timeValue) & ~DS1337_ALARM_MASK) | (rtc_alarm[DS1337_HR] & DS1337_ALARM_MASK); } break; case DS1337_DOW: if(timeValue < 31) { - rtc_alarm[DS1337_DOW] = binToBcd(timeValue) & ~DS1337_ALARM_MASK & ~DS1337_ALARM_DT_MASK | (rtc_alarm[DS1337_DOW] & DS1337_ALARM_MASK) | (rtc_alarm[DS1337_DOW] & DS1337_ALARM_DT_MASK); + rtc_alarm[DS1337_DOW] = (binToBcd(timeValue) & ~DS1337_ALARM_MASK & ~DS1337_ALARM_DT_MASK) | (rtc_alarm[DS1337_DOW] & DS1337_ALARM_MASK) | (rtc_alarm[DS1337_DOW] & DS1337_ALARM_DT_MASK); } break; @@ -552,15 +549,19 @@ void DS1337::alarmSet(uint8_t timeSection, uint8_t timeValue) if (alarmId) { timeValue = (timeValue>>1)<<1; } else + // A1M1 rtc_alarm[DS1337_SEC] = (rtc_alarm[DS1337_SEC] & ~DS1337_ALARM_MASK) | DS1337_ALARM_MASK & timeValue<<7; + // AM2 rtc_alarm[DS1337_MIN] = (rtc_alarm[DS1337_MIN] & ~DS1337_ALARM_MASK) | DS1337_ALARM_MASK & timeValue<<6; + // AM3 rtc_alarm[DS1337_HR] = (rtc_alarm[DS1337_HR] & ~DS1337_ALARM_MASK) | DS1337_ALARM_MASK & timeValue<<5; + // AM4 rtc_alarm[DS1337_DOW] = (rtc_alarm[DS1337_DOW] & ~DS1337_ALARM_MASK) | DS1337_ALARM_MASK & timeValue<<4; - if (timeValue == DS1337_ALARM_MCH_DOWHRMINSEC) + if (timeValue == DS1337_ALARM_MCH_DOWHRMINSEC || timeValue == DS1337_ALARM_MCH_DOWHRMIN) { - rtc_alarm[DS1337_DOW] = rtc_alarm[DS1337_DOW] & ~DS1337_ALARM_DT_MASK | DS1337_ALARM_DT_MASK; + rtc_alarm[DS1337_DOW] = (rtc_alarm[DS1337_DOW] & ~DS1337_ALARM_DT_MASK) | DS1337_ALARM_DT_MASK; } } @@ -568,7 +569,7 @@ void DS1337::alarmSet(uint8_t timeSection, uint8_t timeValue) case DS1337_ALARM_DT: { - rtc_alarm[DS1337_DOW] = rtc_alarm[DS1337_DOW] & ~DS1337_ALARM_DT_MASK | (timeValue == 1 ? DS1337_ALARM_DT_MASK : 0); + rtc_alarm[DS1337_DOW] = (rtc_alarm[DS1337_DOW] & ~DS1337_ALARM_DT_MASK) | (timeValue == 1 ? DS1337_ALARM_DT_MASK : 0); } break; } // end switch @@ -664,12 +665,9 @@ void DS1337::clockIntegrityCallback(void (*userFunc)(void)) void DS1337::clockChecks(void) { #ifdef DS1337_USE_OSC_INTEGRITY - if (getRegisterBit(DS1337_STATUS, DS1337_STATUS_OSF)) + if (getRegisterBit(DS1337_STATUS, DS1337_STATUS_OSF) && !getRegisterBit(DS1337_SP, DS1337_SP_EOSC)) { clockStop(); - unsetRegister(DS1337_STATUS, DS1337_STATUS_OSF); - unsetRegister(DS1337_STATUS, DS1337_STATUS_A1F); - unsetRegister(DS1337_STATUS, DS1337_STATUS_A2F); if (DS1337callbackFunc[2]) DS1337callbackFunc[2](); diff --git a/ds1337/ds1337.h b/ds1337/ds1337.h index a03dd20..e4bf7a1 100644 --- a/ds1337/ds1337.h +++ b/ds1337/ds1337.h @@ -90,13 +90,23 @@ #define DS1337_ALARM_DT_DOW true #define DS1337_ALARM_DT_DATE false -#define DS1337_ALARM_PER_SEC B00001111 -#define DS1337_ALARM_PER_MIN B00000111 /* Used for alarm 2 only*/ -#define DS1337_ALARM_MCH_SEC B00001110 /* Used for alarm 1 only */ -#define DS1337_ALARM_MCH_MINSEC B00001100 -#define DS1337_ALARM_MCH_HRMINSEC B00001000 /* Used for alarm 1 only */ -#define DS1337_ALARM_MCH_DATEHRMINSEC B00000000 -#define DS1337_ALARM_MCH_DOWHRMINSEC B10000000 +#define DS1337_ALARM_PERA B00001111 +#define DS1337_ALARM_PER_SEC DS1337_ALARM_PERA /* Used for alarm 1 only*/ +#define DS1337_ALARM_PER_MIN DS1337_ALARM_PERA /* Used for alarm 2 only*/ + +#define DS1337_ALARM_MCH_SEC B00001110 /* Used for alarm 1 only */ +#define DS1337_ALARM_MCH_MIN B00001100 /* Used for alarm 2 only */ + +#define DS1337_ALARM_MCH_MINSEC B00001100 /* Used for alarm 1 only */ +#define DS1337_ALARM_MCH_HRMIN B00001000 /* Used for alarm 2 only */ + +#define DS1337_ALARM_MCH_HRMINSEC B00001000 /* Used for alarm 1 only */ + +#define DS1337_ALARM_MCH_DATEHRMINSEC B00000000 /* Used for alarm 1 only */ +#define DS1337_ALARM_MCH_DATEHRMIN B00000000 /* Used for alarm 2 only */ + +#define DS1337_ALARM_MCH_DOWHRMINSEC B10000000 /* Used for alarm 1 only */ +#define DS1337_ALARM_MCH_DOWHRMIN B10000000 /* Used for alarm 2 only */ // Alarm mode masks #define DS1337_ALARM2_MODE_MASK B00001000 @@ -124,8 +134,6 @@ /** * Macros **/ -// Starts the oscillator and delay a little to account for long oscillator startup times -#define clockStart() unsetRegister(DS1337_SP, DS1337_SP_EOSC); delay(500); #define clockStop() setRegister(DS1337_SP, DS1337_SP_EOSC) #define getRegisterSP() getRegister(DS1337_SP) @@ -213,6 +221,11 @@ class DS1337 * If the I2C scan mod is available, it'll verify the RTC is reachable **/ int8_t Init(void); + + /** + * clockStart: starts the oscillator and reset the fault flag + **/ + void clockStart(void); #ifdef DS1337_USE_OSC_INTEGRITY /** * chockSetIntegrityCallback: allow setting the callback function diff --git a/global.h b/global.h index 82e5c0c..a5c261e 100644 --- a/global.h +++ b/global.h @@ -17,25 +17,21 @@ extern "C" { #include "SPrint/staticPrint.h" +#ifndef CI + #define CI(reg,bit) (reg & ~(bit)) +#endif +#ifndef SI + #define SI(reg,bit) (reg | bit) +#endif #ifndef BV #define BV(bit) (1<<(bit)) #endif - #ifndef cbi #define cbi(reg,bit) reg &= ~(BV(bit)) #endif #ifndef sbi #define sbi(reg,bit) reg |= (BV(bit)) #endif - -#ifndef CI - #define CI(reg,bit) (reg & ~(bit)) -#endif -#ifndef SI - #define SI(reg,bit) (reg | bit) -#endif - - #ifndef cli #define cli() __asm__ __volatile__ ("cli" ::) #endif diff --git a/twiLCD/twiLCD.cpp b/twiLCD/twiLCD.cpp index 5a01c40..aaef520 100644 --- a/twiLCD/twiLCD.cpp +++ b/twiLCD/twiLCD.cpp @@ -425,7 +425,7 @@ void twiLCD::printInteger(int16_t integer) #endif #ifdef LCD_USE_CLRSCREEN_CALLBACK -void twiLCD::setClrscreenCallback(void (*userFunc)(void)) +void twiLCD::clrsSetCallback(void (*userFunc)(void)) { twiLCDcallbackFunc[0] = userFunc; } diff --git a/twiLCD/twiLCD.h b/twiLCD/twiLCD.h index 476f107..d6c093a 100644 --- a/twiLCD/twiLCD.h +++ b/twiLCD/twiLCD.h @@ -74,7 +74,7 @@ void printString_P(const char *); #ifdef LCD_USE_CLRSCREEN_CALLBACK - void setClrscreenCallback(void (*)(void)); + void clrsSetCallback(void (*)(void)); #endif /** * These function are only available on the mega168 for space reasons