/* 
 * File:   DigitalIO_Chaletduino.h
 * Author: JF
 *
 * Created on November 24, 2018, 3:20 PM
 */

#ifndef DIGITALIO_CHALETDUINO_H
#define	DIGITALIO_CHALETDUINO_H


#ifdef	__cplusplus
extern "C" {
#endif
    
    
    /* ************************************************************************** */
    /* Includes */
#include "define.h"
    /* ************************************************************************** */
    /* Defines */
    //#include <plib.h>
#define SYS_FREQ 			(80000000L)		//Clock period = 12.5 ns
#define PERIPHERAL_FREQ		(80000000L)
    
    
    
    //Output pins hardware definitions
    //
#define HEARTBEAT_LED_2_PIN_DIR		TRISEbits.TRISE6
#define HEARTBEAT_LED_2_PIN			LATEbits.LATE6
#define HEARTBEAT_LED_2_TOGGLE_REG  LATEINV
#define HEARTBEAT_LED_2_SET_REG  	LATESET
#define HEARTBEAT_LED_2_CLEAR_REG   LATECLR
#define HEARTBEAT_LED_2_TOGGLE_MASK _LATG_LATE6_MASK
	
#define HEARTBEAT_LED_1_PIN_DIR		TRISEbits.TRISE5
#define HEARTBEAT_LED_1_PIN			LATEbits.LATE5
#define HEARTBEAT_LED_1_TOGGLE_REG  LATEINV
#define HEARTBEAT_LED_1_SET_REG  	LATESET
#define HEARTBEAT_LED_1_CLEAR_REG   LATECLR
#define HEARTBEAT_LED_1_TOGGLE_MASK _LATE_LATE5_MASK
    
#define LORA_ACTIVITY_LED_PIN_DIR		TRISFbits.TRISF1
#define LORA_ACTIVITY_LED_PIN			LATFbits.LATF1
#define LORA_ACTIVITY_LED_TOGGLE_REG  LATFINV
#define LORA_ACTIVITY_LED_SET_REG  	LATFSET
#define LORA_ACTIVITY_LED_CLEAR_REG   LATFCLR
#define LORA_ACTIVITY_LED_TOGGLE_MASK _LATE_LATF1_MASK
    
    //    #define GP_DEBUG_1_PIN_DIR  TRISEbits.TRISE5
    //    #define GP_DEBUG_1_PIN      LATEbits.LATE5
    //    #define GP_DEBUG_2_PIN_DIR  TRISEbits.TRISE6
    //    #define GP_DEBUG_2_PIN      LATEbits.LATE6
    
    
    //	#define SD_CARD_DETECT_PIN_DIR		TRISDbits.TRISD8
    //	#define SD_CARD_DETECT_PIN			LATDbits.LATD8
    
    //SPI port defs
#define SPI_SDO_PIN_DIR		TRISGbits.TRISG8
#define SPI_SDI_PIN_DIR		TRISGbits.TRISG7
#define SPI_SCK_PIN_DIR		TRISGbits.TRISG6
    
    
    
    
    //SD Card 
#define SD_SPI_SS_PIN_DIR		TRISBbits.TRISB13
#define SD_SPI_SS_PIN   		LATBbits.LATB13
    
    //SPI Flash
#define FLASH_SS_PIN_DIR   	TRISEbits.TRISE3
#define FLASH_SS_PIN       	LATEbits.LATE3
    
    
    //Wifi (WINC3400 module)
#define WIFI_SPI_SS_PIN_DIR		TRISEbits.TRISE0
#define WIFI_SPI_SS_PIN   		LATEbits.LATE0
#define WIFI_IRQ_PIN_DIR   		TRISDbits.TRISD0
#define WIFI_IRQ_PIN       		PORTDbits.RD0
    
    // #define WIFI_SPI_CFG_PIN_DIR   	TRISEbits.TRISE1
    // #define WIFI_SPI_CFG_PIN       	LATEbits.LATE1
#define WIFI_CHP_EN_PIN_DIR   	TRISEbits.TRISE2
#define WIFI_CHP_EN_PIN       	LATEbits.LATE2
#define WIFI_CHP_RST_PIN_DIR   	TRISEbits.TRISE4
#define WIFI_CHP_RST_PIN       	LATEbits.LATE4
    
    //Chalet power relay 
#define POWER_RELAY_ON_PIN_DIR	TRISEbits.TRISE7
#define POWER_RELAY_ON_PIN     	LATEbits.LATE7       //X2-
#define POWER_RELAY_OFF_PIN_DIR	TRISGbits.TRISG9
#define POWER_RELAY_OFF_PIN    	LATGbits.LATG9       //X3-
    
    //Harakiri relay
#define HARAKIRI_RELAY_ON_PIN_DIR	TRISDbits.TRISD6
#define HARAKIRI_RELAY_ON_PIN     	LATDbits.LATD6      
    
    //12V presence detection input
#define CHALET_12V_PRESENCE_PIN_DIR	TRISBbits.TRISB15
#define CHALET_12V_PRESENCE_PIN  PORTBbits.RB15       //X2-   
    
    //Battery voltage measurement (analog input)
#define BATTERY_VOLTAGE_ANALOG_PIN_DIR TRISBbits.TRISB1 //X2-7
    
    //    //Current sensor.  Those pins tristate is controlled by the I2C module
    //#define CURRENT_MODULE_I2C_SCL_PIN_DIR TRISDbits.TRISD3
    //#define CURRENT_MODULE_I2C_SDA_PIN_DIR TRISDbits.TRISD2    
    
    //Onboard temperature sensor (SPI)
#define TEMP_SENSOR_CS_PIN_DIR TRISDbits.TRISD4
#define TEMP_SENSOR_CS_PIN LATDbits.LATD4
#define TEMP_SENSOR_SPI_SDO_PIN_DIR     TRISDbits.TRISD3
#define TEMP_SENSOR_SPI_SDI_PIN_DIR     TRISDbits.TRISD2
#define TEMP_SENSOR_SPI_SCK_PIN_DIR     TRISDbits.TRISD1
    
    
    //Analog (Hall effect) current sensor
#define CURRENT_SENSOR_IN1_PIN_DIR TRISBbits.TRISB2
#define CURRENT_SENSOR_IN2_PIN_DIR TRISBbits.TRISB0    
    
    //LoRa
#define LORA_MODULE_RELAY_PIN_DIR   TRISDbits.TRISD11
#define LORA_MODULE_RELAY_PIN       LATDbits.LATD11
#define LORA_MODULE_M0_PIN_DIR   TRISEbits.TRISE7           //FUCK, double assignation avec Inverter ON
#define LORA_MODULE_M0_PIN       LATEbits.LATE7
#define LORA_MODULE_M1_PIN_DIR   TRISDbits.TRISD9
#define LORA_MODULE_M1_PIN       LATDbits.LATD9    
#define LORA_MODULE_INT_PIN_DIR   TRISDbits.TRISD8
#define LORA_MODULE_INT_PIN       PORTDits.RD8   
#define LORA_MODULE_RX_LED_PIN_DIR   TRISCbits.TRISC14
#define LORA_MODULE_RX_LED_PIN       LATCbits.LATC14  
#define LORA_MODULE_TX_LED_PIN_DIR   TRISCbits.TRISC13
#define LORA_MODULE_TX_LED_PIN       LATCbits.LATC13

    //LCD Screen
#define LCD_RS_PIN_DIR TRISDbits.TRISD5
#define LCD_RS_PIN LATDbits.LATD5
#define LCD_RW_PIN_DIR TRISBbits.TRISB13
#define LCD_RW_PIN LATBbits.LATB13
#define LCD_E_PIN_DIR TRISBbits.TRISB12
#define LCD_E_PIN LATBbits.LATB12
#define LCD_DB4_PIN_DIR TRISBbits.TRISB3
#define LCD_DB4_PIN LATBbits.LATB3
#define LCD_DB5_PIN_DIR TRISBbits.TRISB9
#define LCD_DB5_PIN LATBbits.LATB9
#define LCD_DB6_PIN_DIR TRISBbits.TRISB10
#define LCD_DB6_PIN LATBbits.LATB10
#define LCD_DB7_PIN_DIR TRISBbits.TRISB11
#define LCD_DB7_PIN LATBbits.LATB11
    
    //#define LCD_SCROLL_BTN_PIN_DIR TRISCbits.TRISC13
    //#define LCD_SCROLL_BTN_PIN PORTCbits.PORTC13
    
    /* ************************************************************************** */
    /* Prototypes */
    void InitDigitalIO(void);
    
    
#ifdef	__cplusplus
}
#endif

#endif	/* DIGITALIO_PINGUINO_H */

