/****************************************************************************/
/* Copyright 2009 - 2016 MBARI                                              */
/****************************************************************************/
/* Summary  : Definitions for system timer/ticker for OASIS5                */
/* Filename : sys_timer.h                                                   */
/* Author   : Mike Risi (mrisi), Robert Herlien (rah)                       */
/* Project  : OASIS Mooring Replacement (OASIS5)                            */
/* Revision: 1.0                                                            */
/* Created  : 2009 by Mike Risi, Adapted for Oasis5 10/29/2015 by Bob Herlien*/
/*                                                                          */
/* 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:                                                    */
/* 29oct2015 rah - adapted from Respirometer chamber controller             */
/****************************************************************************/

#ifndef SYS_TIMER_H
#define SYS_TIMER_H

#include <xc.h>

#ifdef O5_DB
#define SYSCLK      20000000L           /* Daughter board crystal freq  */
#else
#define SYSCLK      3686400L            /* Expander board crystal freq  */
#endif

#define PBCLK       (SYSCLK/2)          /* Peripheral Bus Clock         */
#define TICKS_PER_SEC 100               /* System tick rate             */

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


/********************************/
/*  Function Prototypes         */
/********************************/

void tmrInit();                         /* intialize the timer                  */
void tmrTask();                         /* perform any timer tasks in main loop */
void tmrDelayTicks(unsigned int ticks); /* delay for a specified number of timer ticks */
void tmrDelayMs(unsigned int ms);       /* delay for a specified number of milliseconds */
void tmrDelayUs(unsigned int us);       /* delay for a specified number of microseconds */
unsigned long tmrGetTicks();            /* get internal tick counts             */


/****  NO_IDLE  ****/
/* Macros to support doze mode when system is idle (no serial or modbus traffic)*/
/* Not sure where this logically belongs, but this is as good a place as any.	*/
/* If you DON'T want the CPU to doze when idle, either uncomment the definition */
/* below, OR add -DNO_IDLE to the gcc command line in MPLabX					*/

//#define NO_IDLE

#ifdef NO_IDLE
#define Doze()
#define NoDoze()
#else
void Doze(void);
void NoDoze(void);
#endif

#endif
