@ -4,10 +4,10 @@
# ifndef DS1337_h
# define DS1337_h
# define DS1337_DEBUG
// include types & constants of Wiring core API
# include <WConstants.h>
# include <inttypes.h>
# include "../global.h"
// include types & constants of Wire ic2 lib
# include <Wire/Wire.h>
@ -28,6 +28,21 @@
// Uncomment this to support alarms
# define DS1337_USE_ALARMS
# ifdef DS1337_USE_ALARMS
// Uncomment this to allow use external alarm interrupts
# define DS1337_USE_ALARM_INTERRUPTS
// Uncomment this to allow alarm functions callback
# define DS1337_USE_ALARMS_CALLBACK
# endif
// Uncomment this to use integrity check functions
//#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
# define DS1337_USE_SQW_OUTPUT
/**
* Define the position of the RTC buffer values
* */
@ -118,14 +133,31 @@
# define DS1337_ALARM_M3 B00000100
# define DS1337_ALARM_M4 B00001000
// Alarm interrupt bitmask
# define DS1337_ALARM_INT1 B00000001
# define DS1337_ALARM_INT2 B00000010
// Square Wave output masks
# define DS1337_SQW_INTCN B00000100
# define DS1337_SQW_RS1 B00001000
# define DS1337_SQW_RS2 B00010000
// Square Wave output modes
# define DS1337_SQW_RS B00011000
# define DS1337_SQW_1HZ B00000000
# define DS1337_SQW_4096KHZ DS1337_SQW_RS1
# define DS1337_SQW_8192KHZ DS1337_SQW_RS2
# define DS1337_SQW_OSC DS1337_SQW_RS1 | DS1337_SQW_RS2
/**
* Macros
* */
# define clockStart() unsetRegister(DS1337_SP, DS1337_SP_EOSC)
// 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() getRegisterSP(DS1337_SP)
# define getRegisterStatus() getRegisterStatus(DS1337_STATUS)
# define getRegisterSP() getRegister (DS1337_SP)
# define getRegisterStatus() getRegister (DS1337_STATUS)
# define getRegisterBit(reg, bitMask) (getRegister(reg) & bitMask) && bitMask
@ -182,6 +214,13 @@
# define clockGetRDow() clockGet(DS1337_DOW, true)
# if defined(DS1337_USE_ALARMS_CALLBACK) || defined(DS1337_USE_OSC_INTEGRITY)
/**
* Holds the pointer to callback functions
* */
volatile static voidFuncPtr DS1337callbackFunc [ 3 ] ;
# endif
// library interface description
class DS1337
{
@ -202,13 +241,20 @@ class DS1337
* If the I2C scan mod is available , it ' ll verify the RTC is reachable
* */
int8_t Init ( void ) ;
# ifdef DS1337_USE_OSC_INTEGRITY
/**
* ch eckClock: verifies the clock integrity
* Returns false when the integrity check fails
* ch ockSetIntegrityCallback: allow setting the callback function
* for the oscillator fault check .
* */
boolean checkClock ( ) ;
void clockIntegrityCallback ( void ( * ) ( void ) ) ;
# endif
# if defined(DS1337_USE_ALARMS_CALLBACK) || defined(DS1337_USE_OSC_INTEGRITY)
/**
* clockChecks : performs various clock checks such as integrity and alarms
* Will trigger the
* */
void clockChecks ( void ) ;
# endif
/**
* setRegister : sets a register bit fromt he register number and bitmask
* */
@ -260,12 +306,12 @@ class DS1337
* Use the clockSet macro to access this function !
* */
void clockSetWithUTS ( uint32_t , boolean ) ;
# ifdef DS1337_DEBUG
/**
* Prints all of the DS1337 registers
* */
void printRegisters ( void ) ;
# endif
# ifdef DS1337_USE_ALARMS
/**
* alarmSelect : allows selection of the DS1337 alarm 1 or 2
@ -284,6 +330,62 @@ class DS1337
* */
boolean alarmCheck ( boolean ) ;
boolean alarmCheck ( void ) ; // Same as above using false
# ifdef DS1337_USE_ALARMS_CALLBACK
/**
* alarmSetCallback : allows setting of a callback function associated with alarm
* The function will be passed a boolean indicating which of alarm1 ( false ) or alarm2 ( true )
* triggered the callback
* */
void alarmSetCallback ( void ( * ) ( void ) ) ;
/**
* alarmUnsetCallback : removes the callback function attached to the current alarm
* */
void alarmUnsetCallback ( void ) ;
/**
* alarmChecks : will trigger the callback function if an alarm is high
* This function need to be placed somewhere in the main loop
* */
void alarmChecks ( void ) ;
# endif
# ifdef DS1337_USE_ALARM_INTERRUPTS
/**
* alarmDisableInterrupts : disables all alarm interrupts
* */
void alarmDisableInterrupts ( void ) ;
/**
* alarmSetInterrupt : sets the alarm interrupt for the selected alarm
* */
void alarmSetInterrupt ( void ) ;
/**
* alarmUnsetInterrupt : disable interrupt for the select alarm
* */
void alarmUnsetInterrupt ( void ) ;
# endif
# endif
# ifdef DS1337_USE_SQW_OUTPUT
/**
* sqwEnable : Enable the square wave output on SQW / INTB
* If this is enabled and an interrupt is set for alarm 2
* the interrupt will INTA instead of SQW / INTB
* */
void sqwEnable ( void ) ;
/**
* sqwDisable : Disables the square wave output on SQW / INTB
* */
void sqwDisable ( void ) ;
/**
* sqwSetRate : Sets the square wave rate
* */
void sqwSetRate ( uint8_t sqwRate ) ;
# endif
private :
/**
@ -328,7 +430,6 @@ class DS1337
* Hold the buffer for alarm manipulation
* */
uint8_t rtc_alarm [ 4 ] ;
/**
* alarmId : keeps track of which alarm we are working with
* */