/****************************************************************************/
/* Copyright 2013 MBARI.                                                    */
/* MBARI Proprietary Information. All rights reserved.                      */
/****************************************************************************/
#include <p24fxxxx.h>
#include <stdio.h>
#include <string.h>

#include "sys_defs.h"
#include "actions.h"
#include "parser.h"
#include "commands.h"
#include "errors.h"
#include "serial.h"
#include "dig_io.h"
#include "spi_io.h"
#include "sys_timer.h"
#include "rtcc.h"


/* FLASH Configuration */

/* JTAG/Code Protect/Write Protect/Clip-on Emulation mode
Watchdog Timer/ICD pins select */
_CONFIG1(JTAGEN_OFF & GCP_OFF & GWRP_OFF & FWDTEN_OFF & BKBUG_OFF & ICS_PGx1 & COE_OFF)
/* Enable CLK switch, Disable CLK monitor, OSCO or Fosc/2, 
Primary Oscillator Mode: Disabled, Internal Fast RC oscillator */
_CONFIG2(FCKSM_CSDCMD & OSCIOFNC_OFF & POSCMOD_XT & FNOSC_PRI)
/* Disable CLK switch and CLK monitor, OSCO or Fosc/2, HS oscillator,
Primary oscillator */
_CONFIG3(WPEND_WPSTARTMEM & WPCFG_WPCFGEN & WPDIS_WPDIS & WPFP_WPFP2)


#define TOG_DELAY  70000
#define MAX_CHARS   80  /* maximum command line entry length */
#define ChipSelect 1
#define TIMER_100 100
#define TIMER_1000 1000

int getCommand(char* cmd);

int main()
{
    int ret_val;
    char cmd_buff[MAX_CHARS];
    char err_buff[32];
    int loop_cnt = 0;


    char* cmd_ptr;
    
        /* Map UARTs to pins */

    __builtin_write_OSCCONL(OSCCON & 0xbf);


    	/* Map UART #1 pins */
	RPINR18bits.U1RXR = 22;  /* Make Pin RP22 U1RX */
	RPOR10bits.RP20R = 3;     /* Make Pin RP20 U1TX */

	/* Map UART #2 pins */
	RPINR19bits.U2RXR = 23; // Make Pin RP23 U2RX
	RPOR12bits.RP25R = 5;   // Make Pin RP25 U2TX

        /* Map UART #3 pins */
	RPINR17bits.U3RXR = 24; // Make Pin RP24 U3RX
	RPOR5bits.RP11R = 28;   // Make Pin RP11 U3TX

         __builtin_write_OSCCONL(OSCCON | 0x40);




    /* initialize the shizzle */
    digInit();
    serInit();
    rtcInit();
    spiInit();
    tmrInit();


    Timer Timer1;
    Timer Timer2;
	
    /* spit out the banner */
    sprintf(cmd_buff, "\r\nswFOCE Medium Voltage Converter Board Built on %s at %s\r\n",
            __DATE__, __TIME__);
    serPutString(SER_CONSOLE, cmd_buff);
    serPutString(SER_MODBUS1, "\r\nMVConv Board serial port #1\r\n");
    serPutString(SER_MODBUS2, "\r\nMVConv Board serial port #2\r\n");
    tmrClear(&Timer1);
    tmrStart(&Timer1);
    tmrClear(&Timer2);
    tmrStart(&Timer2);


    /* enter the main loop */
    for(;;)
    {

        if (loop_cnt <2000) // 250ms
        {
            digSetOutBit(27,1); // Test LED On
        }
        if (loop_cnt >=2000)
        {
            digSetOutBit(27,0); // Test LED Off
        }
        if (loop_cnt >4000)
        {
             loop_cnt = 0;
        }
        loop_cnt++;


    /*
        if (tmrRead(&Timer1) > TIMER_1000)
	{

	}

        if (tmrRead(&Timer2) > TIMER_100)
        {
            // do other stuff
            tmrClear(&Timer2);
        }
    */


	/* get command */
	if ( !getCommand(cmd_buff) )
	{
	    ret_val = ERR_NO_LINE;
	}
	else
	{
	    /* Skip white space here */
	    cmd_ptr = prsSkip(cmd_buff, " \t");
	    
	    /* If the cmd from getCommand was empty then return here. */
	    if ( !strlen(cmd_ptr) )
	    {
		ret_val = ERR_EMPTY_LINE;
	    }
	    else
	    {
		ret_val = prsParse(cmd_ptr, cmdsRoot, ERR_NO_COMMAND);
	    }
	}

	/* display ERR code or RDY prompt */
	if ( ret_val != ERR_NO_LINE)
	{

	    if ((ret_val != ERR_SUCCESS) && (ret_val != ERR_EMPTY_LINE))
	    {
		sprintf(err_buff,"ERR %04d\r\n", ret_val);
		serPutString(SER_CONSOLE, err_buff);
	    }

	    serPutString(SER_CONSOLE, "RDY\r\n");
	}
    }

        return 0;
}





    #define ECHO_MODE   1
    #define TERM        '\r'

    int getCommand(char* cmd)
    {
        static int char_count = 0;
        static char line[MAX_CHARS] = "";
        int got_line;
        unsigned char b;
        int i;

        got_line = FALSE;


        if ( serGetByte(SER_CONSOLE, &b) )
        {
            if ( ECHO_MODE )
            {
                serPutByte(SER_CONSOLE, b);
                if (b == '\r') serPutByte(SER_CONSOLE, '\n');
            }

            if ( b != TERM )
            {
                if ( (b != '\b') && (char_count < MAX_CHARS) )
                    line[char_count++] = b;
                else if ( char_count > 0 )
                    --char_count;

                cmd[0] = '\0';
            }
            else
            {
                got_line = TRUE;
                line[char_count] = '\0';
                for (i = 0; i <= char_count; i++)
                    cmd[i] = line[i];
                char_count = 0;
            }
        }

        return got_line;
    }


