/****************************************************************************/
/* Copyright 2015 MBARI                                                     */
/****************************************************************************/
/* Summary  : Main Module for OASIS5 on PIC32MX470F512L                     */
/* Filename : main.c                                                        */
/* Author   : Robert Herlien (rah)                                          */
/* Project  : OASIS Mooring Replacement (OASIS5)                            */
/* Revision: 1.0                                                            */
/* Created  : 07/13/2015                                                    */
/*                                                                          */
/* 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:                                                    */
/* 13jul2015 rah - created                                                  */
/****************************************************************************/

#include <xc.h>
#include <GenericTypeDefs.h>

#include <oasis.h>
#include <debug.h>
#include <serial.h>
#include "dig_io.h"
#include "picConfig.h"
#include "timer.h"
#include "adc.h"
#include "o5debug.h"
#include <otask.h>
#include <syslog.h>

#include <FreeRTOS.h>
#include <task.h>
#include <timers.h>

#include <stdint.h>
#include <string.h>
#include <time.h>

Global time_t			radio_time;		/* Last time user connected on radio*/
const char * const		signon = "OASIS5 $Revision$";


/************************************************************************/
/* Function    : dbgTask                                                */
/* Purpose     : Thread to run debugMenu                                */
/* Inputs      : Serial port as a void *parm                            */
/* Outputs     : None, never exits                                      */
/************************************************************************/
static void dbgTask(void *parm)
{
    if (clkInit() != 0)
        serPutString(SER_CONSOLE, "RTC needs to be set!!!\r\n");

	sysLogInit();
	sysLog("Test message");

    digPwrAllOff();
    digBlinkyOff();
    digTx0En();
    tmrDelayMs(10);                     /* Wait for charge pumps        */

    while(TRUE)
    {
        debugMenu((UINT)parm);
        printf("Program exited, restarting\r\n");
    }
}


/************************************************************************/
/* Function    : blinkyTimerCallback                                    */
/* Purpose     : Callback func to timer, to toggle the Blinky LED       */
/* Inputs      : Timer Handle, unused                                   */
/* Outputs     : None                                                   */
/************************************************************************/
static void blinkyTimerCallback(TimerHandle_t xTimer)
{
    digToggleBlinky();
	serCheckXoffStatus();
}


/************************************************************************/
/* Function    : toggleTask                                             */
/* Purpose     : Thread to quickly toggle digTest3                      */
/* Inputs      : None                                                   */
/* Outputs     : None, never exits                                      */
/************************************************************************/
static void toggleTask(void *parm)
{
	int		i;

    while(TRUE)
    {
		for (i = 0; i < 10000; i++)
			digTest3Toggle();

		tmrDelayMs(100);
    }
}


/************************************************************************/
/* Function    : _mon_putc                                              */
/* Purpose     : Function to allow printf(), putc(), etc to work in PIC32*/
/* Inputs      : Character to put to console                            */
/* Outputs     : None                                                   */
/************************************************************************/
void _mon_putc(char c)
{
    ser_putc(SER_CONSOLE, c);
}


/************************************************************************/
/* Function    : main                                                   */
/* Purpose     : Main Module                                            */
/* Inputs      : None                                                   */
/* Outputs     : None, never exits                                      */
/************************************************************************/
int main()
{
    UINT32      tick, lastTick;
    UINT8       b;
    TimerHandle_t xTimer;

    /* Call initializers from FreeRTOS PIC32 support files      */
    vHardwareConfigurePerformance();
    vHardwareUseMultiVectoredInterrupts();

    digInit();
    picPeriphInit();
    serInit();
    rtcInit();
    adcInit();
	semLibInit();
	file_init();

    lastTick = 0;

    /* spit out the banner			*/
	/* xprintf et al won't work yet	*/
#if 0
	serPutString(SER_CONSOLE, "\r\nOASIS5 Software Built on");
	serPutString(SER_CONSOLE, __DATE__);
	serPutString(SER_CONSOLE, " at ");
	serPutString(SER_CONSOLE, __TIME__);
	serPutString(SER_CONSOLE, "\r\n");
	tmrBusyWaitUs(500000);

    dprintf("\r\nOASIS5 Prototype Board Built on %s at %s\r\n",
            __DATE__, __TIME__);
#endif

	task_create(dbgTask, (void *)SER_CONSOLE, "o5Debug",
                DRV_STACK_WORDS, DRVR_PRIO);

#if 0
	task_create(toggleTask, NULL, "toggle", 
                configMINIMAL_STACK_SIZE, 1);
#endif

    xTimer = xTimerCreate("Blinky",
                          TICKS_PER_SEC/2,
                          pdTRUE,
                          (void *) 0,
                          blinkyTimerCallback);

    if(xTimer != NULL)
        xTimerStart(xTimer, 0);
    
    vTaskStartScheduler();

    // Should never arrive here!!
    // debugMenu(SER_CONSOLE);     /* Loops forever until user exits       */

}

