#include "BoardCfg.h"

int InitBoard()
{
	
	DDPCONbits.JTAGEN = 0;
	
    HEARTBEAT_LED_1_PIN_DIR = PIN_OUTPUT;
	HEARTBEAT_LED_2_PIN_DIR = PIN_OUTPUT;
	
	HEARTBEAT_LED_2_PIN = LED_OFF;
	HEARTBEAT_LED_1_PIN = LED_ON;
    
//    LORA_ACTIVITY_LED_PIN_DIR = PIN_OUTPUT;
//    LORA_ACTIVITY_LED_PIN = LED_OFF;
    
    //    GP_DEBUG_1_PIN_DIR = PIN_OUTPUT;
    //    GP_DEBUG_1_PIN = 0;
    //    GP_DEBUG_2_PIN_DIR = PIN_OUTPUT;
    //    GP_DEBUG_2_PIN = 0;
    
    
    SPI_SDI_PIN_DIR	= PIN_INPUT;
    SD_SPI_SS_PIN_DIR = PIN_OUTPUT;
    SPI_SDO_PIN_DIR = PIN_OUTPUT;
    SPI_SCK_PIN_DIR	= PIN_OUTPUT;
    
    
    //////////////// SPI FLASH //////////////////
    
    FLASH_SS_PIN_DIR = PIN_OUTPUT;
    FLASH_SS_PIN = 1;
    
    ///////////////////////////  WIFI ///////////////////  
    //Wifi (WINC1500 module)
    WIFI_SPI_SS_PIN_DIR = PIN_OUTPUT;
    WIFI_SPI_SS_PIN = 1;
    WIFI_IRQ_PIN_DIR = PIN_INPUT;
    //    WIFI_SPI_CFG_PIN_DIR = PIN_OUTPUT;
    //    WIFI_SPI_CFG_PIN = 0;
    WIFI_CHP_EN_PIN_DIR = PIN_OUTPUT;
    WIFI_CHP_EN_PIN = 0;
    WIFI_CHP_RST_PIN_DIR = PIN_OUTPUT;
    WIFI_CHP_RST_PIN = 0;
    
    //Wifi chip IRQ
    IEC0bits.INT0IE = 0;
    IFS0bits.INT0IF = 0;
    INTCONbits.INT0EP = 0;  //Falling edge
    IPC0bits.INT0IP = 3;
    IPC0bits.INT0IS = 0;
    IEC0bits.INT0IE = 1;
    
    //Wifi chip SPI
    SPI2CON = 0;
    SPI2CONbits.MSTEN = 1;
    SPI2CONbits.CKE = 1;
    SPI2CONbits.SMP = 0;
    SPI2CONbits.CKP = 0;
    SPI2BRG = SPICalculateBRG(PERIPHERAL_FREQ, 1000000);
    //  SPI2BRG = SPICalculateBRG(PERIPHERAL_FREQ, 50000);
    SPI2CONbits.ON = 1;
    
    
    //////////////// INVERTER RELAY //////////////////   
    //Chalet inverter power relay
//    POWER_RELAY_ON_PIN_DIR = PIN_OUTPUT;
//    POWER_RELAY_OFF_PIN_DIR = PIN_OUTPUT;
//    POWER_RELAY_ON_PIN = 0;
//    POWER_RELAY_OFF_PIN = 0;
    
    
    //////////////// HARAKIRI RELAY //////////////////
//    //Harakiri relay
//    HARAKIRI_RELAY_ON_PIN_DIR = PIN_OUTPUT;
//    HARAKIRI_RELAY_ON_PIN= 0;
//    
//    //12V presence detection input
//    CHALET_12V_PRESENCE_PIN_DIR = PIN_INPUT;
//    
//    
//    //////////////// BATTERY MONITOR //////////////////
//    //Battery voltage measurement (analog input)
//    BATTERY_VOLTAGE_ANALOG_PIN_DIR = PIN_INPUT;
    
    //ADC Config
    
    AD1PCFG = 0xFFFF;	//Sart with I/O pins configured as digital I/O
    AD1PCFGbits.PCFG1 = 0;
	AD1PCFGbits.PCFG2 = 0;
    TRISBbits.TRISB1 = PIN_INPUT;
	TRISBbits.TRISB2 = PIN_INPUT;
    
    AD1CON1 = 0;
    AD1CON2 = 0;
    AD1CON3 = 0;
    
    AD1CHS = 0;
    AD1CHSbits.CH0SA = 1;   //AN1
    AD1CON3bits.ADCS = 0xF0;
    AD1CON3bits.SAMC = 0x01;
    AD1CON1bits.ON = 1;
    AD1CON1bits.SAMP = 1;
    
    
    //////////////// TEMPERATURE SENSOR ////////////////// 
    //Onboard temperature sensor (SPI)
//    TEMP_SENSOR_CS_PIN_DIR = PIN_OUTPUT;
//    TEMP_SENSOR_CS_PIN = 1;
//    TEMP_SENSOR_SPI_SDO_PIN_DIR = PIN_OUTPUT;
//    TEMP_SENSOR_SPI_SDI_PIN_DIR = PIN_INPUT;
//    TEMP_SENSOR_SPI_SCK_PIN_DIR = PIN_OUTPUT;
//    SPI3CON = 0;
//    SPI3CONbits.MSTEN = 1;
//    SPI3CONbits.CKE = 1;
//    SPI3CONbits.SMP = 0;
//    SPI3CONbits.CKP = 0;
//    SPI3BRG = SPICalculateBRG(PERIPHERAL_FREQ, 1000000);
//    SPI3CONbits.ON = 1;
    
    //////////////// CURRENT SENSOR ////////////////// 
    //Analog (Hall effect) current sensor
//    CURRENT_SENSOR_IN1_PIN_DIR = PIN_INPUT;     //AN2
//    CURRENT_SENSOR_IN2_PIN_DIR = PIN_INPUT;     //AN0
    
    //TODO
    //    AD1CHS = 0;
    //    AD1CHSbits.CH0SA = 1;   //AN2
    //    AD1CON3bits.ADCS = 0xF0;
    //    AD1CON3bits.SAMC = 0x01;
    //    AD1CON1bits.ON = 1;
    //    AD1CON1bits.SAMP = 1;
    
    
    //////////////// LORA ////////////////// 
//    LORA_MODULE_RELAY_PIN_DIR = PIN_OUTPUT;
//    LORA_MODULE_RELAY_PIN = 0;
    LORA_MODULE_M0_PIN_DIR = PIN_OUTPUT;
    LORA_MODULE_M0_PIN = 0;
    LORA_MODULE_M1_PIN_DIR = PIN_OUTPUT;
    LORA_MODULE_M1_PIN = 0;
//    LORA_MODULE_INT_PIN_DIR = PIN_INPUT;
//    LORA_MODULE_RX_LED_PIN_DIR = PIN_OUTPUT;
//    LORA_MODULE_RX_LED_PIN = LED_OFF;  
//    LORA_MODULE_TX_LED_PIN_DIR = PIN_OUTPUT;
//    LORA_MODULE_TX_LED_PIN = LED_OFF;
    
    //////////////// LCD SCREEN ////////////////// 
    //LCD Screen
//    LCD_RS_PIN_DIR = PIN_OUTPUT;
//    LCD_RS_PIN = 0;
//    LCD_RW_PIN_DIR = PIN_OUTPUT;
//    LCD_RW_PIN = 0;
//    LCD_E_PIN_DIR = PIN_OUTPUT;
//    LCD_E_PIN = 0;
//    LCD_DB4_PIN_DIR = PIN_OUTPUT;
//    LCD_DB4_PIN = 0;
//    LCD_DB5_PIN_DIR = PIN_OUTPUT;
//    LCD_DB5_PIN = 0;
//    LCD_DB6_PIN_DIR = PIN_OUTPUT;
//    LCD_DB6_PIN = 0;
//    LCD_DB7_PIN_DIR = PIN_OUTPUT;
//    LCD_DB7_PIN = 0;
//	
//    LCD_SCROLL_BTN_PIN_DIR = PIN_INPUT;
	
	
    
    
    
    
    ////// MISC UNUSED STUFF ///////////
    //    IEC0bits.INT1IE = 0;
    //    IFS0bits.INT1IF = 0;
    //    INTCONbits.INT1EP = 1;  //Rising edge
    //    IPC1bits.INT1IP = 2;
    //    IPC1bits.INT1IS = 1;
    //    IEC0bits.INT1IE = 1;
    
    
    //	OC3CON = 0;
    //	OC3R = 0;
    //	OC3RS = 2000;   //50% PWM
    //	OC3CONbits.OCTSEL = 0;
    //	OC3CONbits.OCM = 0b110;	//PWM mode, no fault protection
    
    //#ifdef __32MX330F064H__
    //    RPD2Rbits.RPD2R = 0b1011;   //RD2 peripheral selection = OC3
    //#endif
    //	
    //	T2CONbits.TON = 0; // Disable Timer
    //    //	T2CONbits.TCS = 0; // Select internal instruction cycle clock
    //	T2CONbits.TGATE = 0; // Disable Gated Timer mode
    //	T2CONbits.TCKPS = 0b011; // Select 1:1 Prescaler
    //	TMR2 = 0x00; // Clear timer register
    //	
    //
    //	IPC2bits.T2IP = 0x01; // Set Timer 2 Interrupt Priority Level
    //	IFS0bits.T2IF = 0; // Clear Timer 2 Interrupt Flag
    //	IEC0bits.T2IE = 0; // Disable Timer 2 interrupt
    //	T2CONbits.TON = 1; // Start Timer
    //    
    //    OC3CONbits.ON = 1;
    
    
//    RTCCONbits.ON = 1;
    
    return RET_OK;
}
