/****************************************************************************/
/* COPYRIGHT: 2012 MBARI.                                                   */
/* MBARI Proprietary Information. All rights reserved.                      */
/* AUTHORS: JAINE PEROTTI, ROLAND QUIROS										*/	
/* DESCRIPTION: Main source code for the Shallow Water FOCE's low voltage	*/
/*				load switcher.  This is designed to be compiled with the	*/
/*				MPLAB C30 Compiler, for use with the PIC24FJ256GA106.		*/
/****************************************************************************/
#include <p24fxxxx.h>
#include <stdio.h>
#include <string.h>

#include "card.h"
#include "sys_defs.h"
#include "actions.h"
#include "parser.h"
#include "commands.h"
#include "errors.h"
#include "serial.h"
#include "spi1.h"
#include "spi2.h"
#include "dig_io.h"
#include "sys_timer.h"
#include "analog.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 & ICS_PGx2)
/* 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_WPEN & WPFP_WPFP2)

#define MAX_CHARS   80  /* maximum command line entry length */
#define SER_CONSOLE 1

int getCommand(char* cmd);

int main()
{
    int ret_val;
    char cmd_buff[MAX_CHARS];
    char err_buff[32];     
    char* cmd_ptr;
    unsigned int reg;
    unsigned int spiCount = 0;
    Timer Timer1;
    Timer Timer2;
     
    /* Map UART pins */
	//updated --JP
    RPINR18bits.U1RXR = 3;  // Make Pin RP3 U1RX 
    RPOR2bits.RP4R = 3;     // Make Pin RP4 U1TX 
    
    /* Set B7 high to disable UART2_SHUTDOWN 
    TRISBbits.TRISB7 = 0;
    LATBbits.LATB7 = 1;
*/
    
    /* Map SPI #1 Pins */
	//*updated* --JP
    RPINR20bits.SDI1R = 21; // SDI1 (MISO) RP21 
    RPOR13bits.RP26R = 7;   // SDO1 (MOSI) RP26 
    RPOR9bits.RP19R = 8;    // SCK1 (SCK)  RP19 
	RPINR21bits.SS1R = 27;	// SS1  (CS)   RP27

	/* initialize the shizzle */
	digInit();
    serInit();
    tmrInit();
    spiInit();
	spi2Init();
    cardInit();
    
    /* set ADCs to external clock 
	*NO INTERNAL ADC USED ON CURRENT BOARD, HOWEVER CSAddSet() WILL STILL BE USEFUL*  --JP

    CSAddSet(8);
    spiReadWrite2(0b10000111);
    CSAddSet(99);
    
    CSAddSet(9);
    spiReadWrite2(0b10000111);
    CSAddSet(99);
*/
    
    
    /* spit out the banner */
    sprintf(cmd_buff, 
            "\r\nFOCE CHASSIS LOW VOLTAGE LOAD SWITCHER BOARD Built on %s at %s\r\n", 
            __DATE__, __TIME__);

    serPutString(SER_CONSOLE, cmd_buff);
    
    tmrClear(&Timer1);
    tmrStart(&Timer1);
    tmrClear(&Timer2);
    tmrStart(&Timer2);

    /* enter the main loop */
    for(;;)
    {

		LATGbits.LATG3 = 1;
		
		int test;
		
		for(test = 0; test <= 1000; test++)
		{

		}

		LATGbits.LATG3 = 0;

		for(test = 0; test <= 1000; test++)
		{

		}


/*
        // get command 
        if ( !getCommand(cmd_buff) )
        {
            ret_val = ERR_NO_LINE;
//							serPutString(SER_CONSOLE, "ERR_NO_LINE\r\n");	
        }
        else
        {
            // Skip white space here 
            cmd_ptr = prsSkip(cmd_buff, "     ");
            
            // If the cmd from getCommand was empty then return here. 
            if ( !strlen(cmd_ptr) ) {
                ret_val = ERR_EMPTY_LINE;
				            serPutString(SER_CONSOLE, "ERR_EMPTY_LINE\r\n");
            }
			else {
                ret_val = prsParse(cmd_ptr, cmdsRoot, ERR_NO_COMMAND);
							serPutString(SER_CONSOLE, "ERR_NO_COMMAND\r\n");
			}
        }

        // 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");
        }

        cardPoll();
        spiUpdate();
*/
    }

    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');

//					serPutString(SER_CONSOLE, "ECHO MODE TRUE\r\n");	
        }

        if ( b != TERM )
        {
            if ( (b != '\b') && (char_count < MAX_CHARS) ){
                line[char_count++] = b;
//					serPutString(SER_CONSOLE, "b != '\b' and char_count < MAX_CHARS\r\n");
				}
           else if ( char_count > 0 ) {
                --char_count;
//					serPutString(SER_CONSOLE, "char_count > 0\r\n");
			}

            cmd[0] = '\0';

//					serPutString(SER_CONSOLE, "b != TERM\r\n");
        } 
        else
        {
            got_line = TRUE;
            line[char_count] = '\0';
            for (i = 0; i <= char_count; i++)
                cmd[i] = line[i];
            char_count = 0;
//					serPutString(SER_CONSOLE, "b = TERM\r\n");
        }
    }

    return got_line;
}
