/************************************************************************/
/* Copyright 2003-2016 MBARI                                            */
/************************************************************************/
/* Summary  : System Ticker and RTC Definitions for OASIS5 on PIC32MX470F512L*/
/* Filename : timer.h                                                   */
/* Author   : Robert Herlien (rah)                                      */
/* Project  : OASIS Mooring Replacement (OASIS5)                        */
/* Revision : 1.0                                                       */
/* Created  : 01/13/2003                                                */
/*                                                                          */
/* 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:                                                */
/* 13jan2003 rah - created                                              */
/* 20apr2016 rah - modified for OASIS5                                  */
/************************************************************************/

#ifndef INCtimerh
#define INCtimerh       1

#include "GenericTypeDefs.h"
#include <FreeRTOS.h>
#include <portmacro.h>
#include <time.h>
#include <mbariTypes.h>
#include <otask.h>
#include <dig_io.h>
#include <debug.h>

/* Following are defined in FreeRTOSConfig.h	*/
#define SYSCLK      configCPU_CLOCK_HZ          /* System clock         */
#define PBCLK       configPERIPHERAL_CLOCK_HZ   /* Peripheral Bus Clock */
#define TICKS_PER_SEC	 TICK_RATE_HZ			/* System tick rate     */
#define TICKS_PER_SECOND TICKS_PER_SEC			/* Alias				*/


#define TMR2PRD         (PBCLK/(8*TICKS_PER_SEC))-1
#define MS_PER_TICK     (1000/TICKS_PER_SEC)

#define WAIT_FOREVER	portMAX_DELAY


#ifdef  DEBUG_TICKER							/* defined in debug.h	*/
#define tickPinOn()     digTest0On()
#define tickPinOff()    digTest0Off()
#else
#define tickPinOn()
#define tickPinOff()
#endif

#ifdef  DEBUG_RTC								/* defined in debug.h	*/
#define rtcPinOn()      digTest0On()
#define rtcPinOff()     digTest0Off()
#define rtcTaskPinOn()  digTest1On()
#define rtcTaskPinOff() digTest1Off()
#else
#define rtcPinOn()
#define rtcPinOff()
#define rtcTaskPinOn()
#define rtcTaskPinOff()
#endif

#ifdef  DEBUG_SLEEP								/* defined in debug.h	*/
#define sleepPinOn()     digTest0On()
#define sleepPinOff()    digTest0Off()
#define wakeupPinOn()    digTest1On()
#define wakeupPinOff()   digTest1Off()
#else
#define sleepPinOn()
#define sleepPinOff()
#define wakeupPinOn()
#define wakeupPinOff()
#endif


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

#define tmrDelayTicks(ticks)    task_delay((TickType_t)ticks)
#define tmrGetTicks()           xTaskGetTickCount()       
#define tmrDelayMs(ms)			task_delay(ms/MS_PER_TICK)
#define task_delay_ms(ms)		task_delay(ms/MS_PER_TICK)

void    tmrBusyWaitUs(UINT32 us);
int     clkInit(void);
time_t  clkTime(void);
time_t  clkGetTime(time_t *todp, UINT16 *ms);
int     clkSetTime(time_t newTod);
Nat16	clkLocalHour(void);
void	clkPrintTime(time_t *tod);
void    tmrSleep(UINT32 secs);
void	tmrRtosSleep(TickType_t slpTicks);

#endif  /* INCtimerh */
