/************************************************************************/
/* Copyright 2003-2016 MBARI                                            */
/************************************************************************/
/* Summary  : Definitions for Real-time clock routines for OASIS5       */
/* 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 <FreeRTOSConfig.h>
#include <time.h>

/* From FreeRTOSConfig.h        */
#define SYSCLK      configCPU_CLOCK_HZ          /* System clock         */
#define PBCLK       configPERIPHERAL_CLOCK_HZ   /* Peripheral Bus Clock */
#define TICKS_PER_SEC 1000                      /* System tick rate     */
//#define TICKS_PER_SEC configTICK_RATE_HZ        /* System tick rate     */

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


#define DEBUG_TICKER
#ifdef  DEBUG_TICKER
#include "dig_io.h"
#define tickPinOn()     digTest0On()
#define tickPinOff()    digTest0Off()
#define rtcPinOn()      digTest1On()
#define rtcPinOff()     digTest1Off()
#define rtcTaskPinOn()  digTest2On()
#define rtcTaskPinOff() digTest2Off()
#else
#define tickPinOn()
#define tickPinOff()
#define rtcPinOn()
#define rtcPinOff()
#define rtcTaskPinOn()
#define rtcTaskPinOff()
#endif


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

#define tmrDelayTicks(ticks)    vTaskDelay((const TickType_t)ticks)
#define tmrGetTicks()           xTaskGetTickCount()       

void     tmrInit(void);
void    tmrDelayMs(UINT32 ms);
void    tmrBusyWaitUs(UINT32 us);
void    _tmrInterrupt(void);
int     clkInit(void);
time_t  clkTime(void);
time_t  clkGetTime(time_t *todp, UINT16 *ms);
int     clkSetTime(time_t newTod);
void    clkPrintTime(int port, time_t *tod);
void    ioSleep(UINT32 secs);

#endif  /* INCtimerh */
