
/*
 Description:
 This is a template file for standard C header file.
 
 */

/* ************************************************************************** */
/* Revision:
 ### 20120515 JFM
 Original version.
 
 ### YYYYMMDD Initial, Bug Identification
 Change description.
 */

//JFM WINC TODO


#include "define.h"


#include "Uart.h"
#include "BoardCfg.h"
#include "timer.h"
#include "Watchdog.h"
//#include "PWMCtrl.h"
//#include "KnobEncoderCtrl.h"
//#include "LedLightCtrl.h"
//#include "PrintfServer.h"

//#include "MasterCtrlInterface.h"
//#include "SDCardMgr.h"
//#include "FatFS/ff.h"
#include "main.h"
#include "NetworkProtocol.h"
#include "ChaletPowerRelay.h"
#include "BatteryMonitor.h"
#include "CurrentSensor.h"
#include "I2C.h"
#include "SPI_Flash.h"
#include "TemperatureSensor.h"
#include "LoraWatchdog.h"
#include "LCDCtrl.h"
#include "SIM7080GInterface.h"
#include "LTENetworkInterface.h"

#include "BootloaderInterface.h"

#include "hd44780.h"


//#define NO_WIFI
#ifndef NO_WIFI
#include "Terminal.h"
#include "WiFiCtrl.h"
#include "InternalUart.h"
#endif

#ifdef USE_PRINTF
void _mon_putc(char c);	//override from stdio to redirect stdout on uart 3B
#elif defined USE_SYSLOG
#include "Syslog.h"
#include "LoraNetworkInterface.h"
void _mon_putc(char c);	//override from stdio to redirect stdout on uart 3B
#endif

#define HEARTBEAT_LED_TIMEOUT 400
#define VOLTS_PER_BITS (float)3.3/1023

static void InitializeBoard(void);

#pragma config FPLLMUL = MUL_20, FPLLIDIV = DIV_2, FPLLODIV = DIV_1, FWDTEN = OFF, WDTPS = PS4096   //Watchdog timeout = 4,096s
#pragma config POSCMOD = EC, FNOSC = PRIPLL, FPBDIV = DIV_1
//#pragma config POSCMOD = XT, FNOSC = PRIPLL, FPBDIV = DIV_1
#pragma config ICESEL = ICS_PGx2, BWP = OFF
#pragma config FSOSCEN = OFF

int main(void)
{
	int mRetCode = 1;
    
	
	
	
	SYSTEMConfig(SYS_FREQ, SYS_CFG_WAIT_STATES | SYS_CFG_PCACHE);	//Use peripheral library to optimize configuration
	INTConfigureSystem(INT_SYSTEM_CONFIG_MULT_VECTOR);// configure for multi-vectored mode interrupts
	OSCCONbits.SOSCEN = 0;
    
#ifndef __32MX330F064H__
	AD1PCFG = 0xFFFF;	//Sart with I/O pins configured as digital I/O
#endif
    
    InitBoard();
    INTEnableInterrupts();
    
    TimerInit();
    InitWatchdog();
	// I2CInit();
    InitChaletPowerRelay();
    InitBatteryMonitor();
    InitHarakiriRelay();
	InitTempSensor();
	
    
    InitUart();
    LoraNetworkInterfaceInit();
	InitLoraWatchdog();
    
    
    
    InitSPIFlash();
	InitLCDCtrl();
    
    InitLTEModule(false);
	LTENetworkInterfaceInit();
	

#ifdef USE_SYSLOG
	InitSyslog();
#endif
	
    
    printf("ChaletDuino V2 Initialized\n");
    
    SPIFlashCheckAndConfigure();
	TempSensorCheckAndConfigure();
	
	BootloaderInterfaceInit();
	

//	SPIFlashErase64KSector(0x180000,1);
//    
//    unsigned char test[100];
//	int i = 0;
//	
//	for(i = 0; i < 100; i++)
//	{
//		test[i] = i;
//	}
//	
//	
//	SPIFlashWriteBuffer(test,100,0x180000);
//	
//	for(i = 0; i < 100; i++)
//	{
//		test[i] = 0;
//	}
//    
//	SPIFlashReadBuffer(test,100,0x180000);
//	
	
    
#ifndef NO_WIFI
    InitTerminal();

	InitWiFi();	

    
#endif
    
	//  unsigned int bw;
	
	//    FRESULT res;
	//    res = f_mount(&FatFs, "", 0);		/* Give a work area to the default drive */
	//    if(!res)
	//    {
	//        printf("Could not mount SD card\n");
	//    }
	//    else
	//    {
	//        printf("SD Card mounted successfuly");
	//    }
	//
	//	if (f_open(&File[0], "newfile.txt", FA_WRITE | FA_CREATE_ALWAYS) == FR_OK) /* Create a file */
	////    {	
	//
	//		res = f_write(&File[0], "It works!\r\n", 11, &bw);	/* Write data to the file */
	//
	//		res = f_close(&File[0]);								/* Close the file */
	//
	//	}
    
	// int res = OpenPrintfServer();
    
	TimerStart(HEARTBEAT_LED_TMR,HEARTBEAT_LED_TIMEOUT);
    
    
	
	
	//	printf("Lora Monitor Started\n");
    printf("test %d\n", 1);
    
	EnableWatchdog();
    KickWatchdog();
    mRetCode = 1;


	
	while(mRetCode == 1)
	{
		
        KickWatchdog();        
        
#ifndef NO_WIFI
        TickWiFi();
#endif        
        TickTerminal();
        UartTick();
        ChaletPowerRelayTick();
        BatteryMonitorTick();
		SyslogTick();
		TickTempSensor();
		BootloaderInterfaceTick();
		TickLoraWatchdog();
		TickLCDCtrl();
        TickLTEModule();
        TickLTENetworkInterface();
		
        if(IsTimerExpired(HEARTBEAT_LED_TMR))
        {
            HEARTBEAT_LED_2_PIN = ~HEARTBEAT_LED_2_PIN;
            TimerStart(HEARTBEAT_LED_TMR,HEARTBEAT_LED_TIMEOUT);
			
//			HD44780_DisplayClear();
//			HD44780_PositionXY(0, 0);
//			// send char
//			HD44780_DrawString("DISPLAY ON");
//			// display clear
//			HD44780_DisplayOn();
			
        }
	}
	
	return mRetCode;
}


void InitializeBoard()
{
    InitBoard();
	
}


void _mon_putc(char c)
{
//    U2TXREG = c;
//    while (U2STAbits.TRMT==0);
    UartTransmitData(NETWORK_UART_PORT,&c,1);
}
#ifdef USE_PRINTF
void _mon_putc(char c)
{
    U2TXREG = c;
    while (U2STAbits.TRMT==0);
}
//#elif defined USE_SYSLOG
//void _mon_putc(char c)
//{
//    SyslogNewByte(c);
//}

#endif

//EOF

