/****************************************************************************/
/* Copyright 2003-2018 MBARI												*/
/****************************************************************************/
/* Summary	: Main Routine for BEDS2 on PIC32MX470							*/
/* Filename : beds.c														*/
/* Author	: Robert Herlien (rah)											*/
/* Project	: BEDS (Benthic Event Detection System)							*/	
/* Revision : 2.0															*/
/* Created	: 02/13/2018 from beds.c on Persistor CF2						*/
/*																			*/
/* 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:													*/
/* 18apr2012 rah - created for first version of BEDS, on Persistor CF2		*/
/* 13feb2018 rah - modified for BEDS2 on PIC32MX470							*/
/* $Log$
 */
/****************************************************************************/

#include <mbariTypes.h>			/* MBARI type definitions				*/
#include <beds.h>				/* BEDS general definitions				*/		
#include <dig_io.h>				/* BEDS digital I/O definitions			*/
#include <serial.h>				/* BEDS serial I/O definitions			*/
#include <file.h>				/* BEDS file I/O definitions			*/
#include <config.h>				/* BEDS configuration					*/
//#include <bedsCmd.h>			/* BEDS Command line processor			*/
#include <syslog.h>				/* System logger						*/
#include <adc.h>				/* BEDS Analog Module					*/
#include <watch.h>				/* BEDS watch cycle						*/
#include <event.h>				/* BEDS event cycle						*/
#include <spi.h>				/* BEDS SPI module						*/
#include <clock.h>				/* BEDS Clock module					*/
#include <modem.h>				/* BEDS Acoustic modem routines			*/
#include <ConfigPerformance.h>	/* PIC32MX wait state and caching from FreeRTOS*/

#include <stddef.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>

/********************************/
/*		External Data			*/
/********************************/

Extern time_t	startTime;				/* Start time from config file	*/
Extern MBool	wdtOn;					/* Use watchdog timer			*/


/************************************************************************/
/* Function	   : platform_init											*/
/* Purpose	   : Initialize hardware-specific platform stuff			*/
/* Inputs	   : None													*/
/* Outputs	   : None													*/
/************************************************************************/
MLocal void platform_init(void)
{
	digInit();					/* Initialize digital I/O				*/
    picPeriphInit();			/* Set PMDs								*/
	spiInit();					/* Initialize the SPI module			*/
	clkInit();					/* Initialize the clock module			*/
	serInit();					/* Initialize serial I/O module			*/

	/* Now we should be able to print the signon message	*/
	printf("\r\nProgram: %s: %s %s \r\n", __FILE__, __DATE__, __TIME__);

	if (fileInit() == OK)		/* Initialize file I/O module			*/
	{
		sysLogInit();			/* Initialize sysLogger					*/
		parseConfigFile(CFG_FILE);	/* Need user vars for some of below	*/
	}

	adcInit();					/* Initialize the ADC module			*/
	watchInit();				/* Initialize the watch cycle module	*/
//	eventInit();				/* Initialize the event cycle module	*/
	modemInit();

#if 0
	if (wdtOn)
	{
		wdtEnable();			/* Start the watchdog timer				*/
		wdtPing();				/* And ping it							*/
	}
	else
		wdtDisable();
#else
	wdtDisable();
#endif
	
} /* platform_init() */


/************************************************************************/
/* Function	   : main													*/
/* Purpose	   : Main entry point										*/
/* Inputs	   : argc, argv												*/
/* Outputs	   : None													*/
/* Comments	   : Never returns											*/
/************************************************************************/
int main(int argc, char **argv)
{
    /* Call initializers from FreeRTOS PIC32 support files      */
    vHardwareConfigurePerformance();
    vHardwareUseMultiVectoredInterrupts();

	platform_init();

	if (clkTime() < startTime)
	{
		modemOff();
		sysLogPrintf("Sleeping until %s", asctime(gmtime(&startTime)));
	}

	serRxFlush(1);
	serRxFlush(2);
	while(clkTime() < startTime)
	{
		if (serRxCount(1) || serRxCount(2))
		{
			sysLogPrintf("Woke due to keyboard char");
			break;
		}

		clkSleep(5);					/* Sleep until start time*/
		if (wdtOn)
			wdtPing();					/* Keep watchdog running */
	}

	//Initialize and start MPU6000
	imuInit();
	sysLog("IMU Started");

	sysLog("Entering main processing loop");

	watchCycle();

	/* Should never get here	*/
	picSoftReset();						/* full hardware reset	*/
	return(0);

} //____ main() ____//
