/****************************************************************************/
/* 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 <stdio.h>
#include <stdint.h>
#include <string.h>

#include <serial.h>
#include "dig_io.h"
#include "periph.h"
#include "spi.h"
#include "sys_timer.h"
#include "adc.h"
#include "o5debug.h"

//#include "actions.h"
//#include "parser.h"
//#include "commands.h"
//#include "errors.h"
//#include "sys_timer.h"
//#include "rtcc.h"


#define MAX_CHARS   80  /* maximum command line entry length */


/************************************************************************/
/* Function    : exceptInit                                             */
/* Purpose     : Initialize interrupt handlint                          */
/* Inputs      : None                                                   */
/* Outputs     : None                                                   */
/************************************************************************/
static void exceptInit()
{
    unsigned int    temp_CP0;

    __builtin_disable_interrupts();
   
    _CP0_SET_EBASE(0x9fc01000);
    temp_CP0 = _CP0_GET_CAUSE();
    temp_CP0 |= 0x00800000;
    _CP0_SET_CAUSE(temp_CP0);

    INTCONSET = _INTCON_MVEC_MASK;

    temp_CP0 = _CP0_GET_STATUS();
    _CP0_SET_STATUS(temp_CP0 & ~_CP0_STATUS_IPL_MASK);

    __builtin_enable_interrupts();
}


/************************************************************************/
/* Function    : main                                                   */
/* Purpose     : Main Module                                            */
/* Inputs      : None                                                   */
/* Outputs     : None, never exits                                      */
/************************************************************************/
int main()
{
    UINT8       b;
    UINT16      i;
    char        cmd_buff[MAX_CHARS];
    UINT32      tick, lastTick;

    SYS_Initialize();           //Harmony initializer

    digInit();
    periphInit();
    serInit();
    tmrInit();
    spiInit();
    adcInit();
    rtcInit();
    exceptInit();

    digBlinkyOn();
    digTx0En();
    tmrDelayMs(10);             /* Wait for charge pumps */
    lastTick = 0;

    /* spit out the banner */
    sprintf(cmd_buff, "\r\nOASIS5 Prototype Board Built on %s at %s\r\n",
            __DATE__, __TIME__);
    serPutString(SER_CONSOLE, cmd_buff);
    serPutString(SER_PORT_1, "\r\nOASIS5 Prototype Board Serial port 1\r\n");

#if 0
    for (i = 0; i < 10; i++)
    {
        sprintf(cmd_buff, "Testing serial Tx Interrupts on console port, loop %u\r\n", i);
        serPutString(SER_CONSOLE, cmd_buff);
    }
#endif
    
    debugMenu(SER_CONSOLE);     /* Loops forever until user exits       */


    digTx1En();
    digPwrOn(1);

    for(;;)                     /* After user exits, go back to test loop*/
    {
        LATGINV = 0x8000;

        if (serGetByte(SER_CONSOLE, &b))
            serPutByte(SER_CONSOLE, b);

        if (serGetByte(SER_PORT_1, &b))
            serPutByte(SER_PORT_1, b);

        tick = tmrGetTicks();
        if ((tick - lastTick) >= TICKS_PER_SEC/2)
        {
            digToggleBlinky();
            lastTick = tick;
        }
    }
}

