/************************************************************************/
/* Copyright 2003-2012 MBARI											*/
/************************************************************************/
/* Summary	: Real-time Clock support for BEDS System					*/
/* Filename : clock.c													*/
/* Author	: Robert Herlien (rah)										*/
/* Project	: Benthic Event Detection System (BEDS)						*/
/* Revision : 1.0														*/
/* Created	: 10/18/2011 from Oasis4 clock.c							*/
/*																			*/
/* MBARI provides this documentation and code "as is", with no warranty,	*/
/* express or implied, of its quality or consistency. It is provided without*/
/* support and without obligation on the part of the Monterey Bay Aquarium	*/
/* Research Institute to assist in its use, correction, modification, or	*/
/* enhancement. This information should not be published or distributed to	*/
/* third parties without specific written permission from MBARI.			*/
/*																			*/
/************************************************************************/
/* Modification History:												*/
/* 18oct2011 rah - created												*/
/************************************************************************/

#ifndef INCclockh
#define INCclockh		1

#include <xc.h>
#include <beds.h>
#include <time.h>


#define SYSCLK			24000000L		/* Clock frequency = 24 MHz			*/
#define PBCLK			(SYSCLK/2)		/* If FPBDIV is DIV_2, see picConfig.c*/

#define TICKS_PER_SECOND 100			/* Frequency of software time base	*/
#define MS_PER_TICK		(1000/TICKS_PER_SECOND)
#define TICKS_PER_SEC	TICKS_PER_SECOND


#define enableTickInt()		IEC0SET = _IEC0_T1IE_MASK;
#define disableTickInt()	IEC0CLR = _IEC0_T1IE_MASK;

#define clkIntsOn()			IEC0SET = (_IEC0_T1IE_MASK | _IEC0_INT4IE_MASK);
#define clkIntsOff()		IEC0CLR = (_IEC0_T1IE_MASK | _IEC0_INT4IE_MASK);


/****************************************/
/* Function Declarations				*/
/****************************************/

int 		clkInit(void);
time_t		clkTime(void);
time_t		clkGetTime(time_t *todp, UINT16 *ms);
int    		clkSetTime(time_t newTod);
void		clkPrintTime(time_t ptime);
Nat32		clkGetTick(void);
void		clkDelayUs(Nat32 us);
void		clkDelayMs(Nat32 ms);
void    	clkSleep(Nat32 secs);
void		clkIdle(Nat32 ticks);
MBool		clkSleeping(void);
void		clkSetWakeupVec(Nat32 bitVec);
void		RTCack(void);
void		tickIntHandler(void);

CmdRtn		uptimeCmd(int argc, char **argv);

#endif	/* INCclockh */
