/****************************************************************************
 Title	:	TERMINAL FOR VISUALIZATION AND CONTROL CONDITIONS IN HOUSE
 Author	:	Patrik Szalay <xszala01@stud.feec.vutbr.cz>
 File	:	IO.h
 Software:	CodeWarrior Development Studio Version: 10.6 Build Id:140329
 Hardware:	MC9S08LH64
*****************************************************************************/

#ifndef IO_H_
#define IO_H_
#include <hidef.h>
#include "derivative.h"

/* LED pins definitions */
#define LED1	PTCD_PTCD2	/**< pin for the LED1 */
#define LED2	PTCD_PTCD3	/**< pin for the LED2 */
#define LED3	PTCD_PTCD4	/**< pin for the LED3 */
#define LED4	PTCD_PTCD5	/**< pin for the LED4 */
#define LED1_MASK 	PTCD_PTCD2_MASK	/**< mask for the LED1 pin */
#define LED2_MASK 	PTCD_PTCD3_MASK	/**< mask for the LED2 pin */
#define LED3_MASK 	PTCD_PTCD4_MASK	/**< mask for the LED3 pin */
#define LED4_MASK 	PTCD_PTCD5_MASK	/**< mask for the LED4 pin */
#define LED  	PTCD	/**< port for LEDs */

#define LED_DDir PTCDD |= 0b00111100; /**< LED DataDirection = OUT */
#define LED_Data PTCD |= 0b00111100; /**< init the LED pins */
#define LED_Init LED_DDir; LED_Data; /* macro for run all the macros above */


/* SW pins definitions */
#define SW1		PTAD_PTAD6	/**< pin for the SW1 */
#define SW2		PTAD_PTAD7	/**< pin for the SW2 */
#define SW3 	PTAD_PTAD4	/**< pin for the SW3 */
#define SW4 	PTCD_PTCD7	/**< pin for the SW4 */
#define SW_UP	PTCD_PTCD7	/**< pin for the SWUP */
#define SW_DN	PTAD_PTAD4	/**< pin for the SWDN */
#define SW_RGHT	PTAD_PTAD7	/**< pin for the SWRGHT */
#define SW_LEFT	PTAD_PTAD2	/**< pin for the SWLEFT */
#define SW_ENT	PTAD_PTAD6	/**< pin for the SWENT */

#define SW_PUpEn PTAPE = 0b11010100;  /**< Pull-Ups for buttons*/
#define SW_En PTADD &= 0b00101011; PTCDD &= 0b01111111; /**< Buttons DataDirection = IN; */
#define SW_Init	SW_PUpEn; SW_En; /* macro for run all the macros above */

/* Relay pins definitions */
#define HEATER_RE1 PTAD_PTAD1	/**< pin for the RE1 */
#define PUMP_RE2 PTAD_PTAD3		/**< pin for the RE2 */
#define RE_DDir PTADD |= 0b00001010; 	/**< RE pin data direction */
#define RE_Data PTAD &= 0b11111111;		/**< init the RE pins */
#define RE_Init RE_DDir; RE_Data; SOPT2_SPIPS = 1;	/* macro for run all the macros above */

/* Disable peripherals */
#define SPI_Disable SPIC1_SPE = 0; 	/* Disable the SPI module */
#define ACIC_Disable SOPT2_ACIC = 0;	/* Disable the AC module */
#define CLKSRC_Disable SCGC1 &= 0b10001101; SCGC2 &= 0b11110110;	/* Disable clock source for the unused modules */
#define Init_Peripheral SPI_Disable; ACIC_Disable; CLKSRC_Disable; /* macro for run all the macros above */

#define KBI_DIS KBISC_KBIE = 0; /**< KBI Disable */
#define KBI_Edge KBIES &= 0b00101011; /**< KBI on Falling Edge */ 
#define KBI_PEn KBIPE |= 0b11010100; /**< KBI enable for SW1-SW3 */
#define KBI_KBF KBISC_KBACK = 1; /**< Reset KBF bit*/
#define KBI_EN  KBISC_KBIE = 1; /**< KBI Enable */
#define KBI_Init KBI_DIS; KBI_Edge; KBI_PEn; KBI_KBF; KBI_EN;	/* macro for run all the macros above */

#define IRQ_DIS IRQSC_IRQIE = 0;	/**< IRQI Disable */
#define IRQ_Edge IRQSC_IRQEDG = 0;	/**< IRQI on Falling Edge */ 
#define IRQ_PEn IRQSC_IRQPE = 1; 	/**< IRQI enable for pin */
#define IRQ_ACK IRQSC_IRQACK = 1;	/**< Reset IRQACK bit*/
#define IRQ_EN  IRQSC_IRQIE = 1;	/**< IRQ Enable */
#define IRQI_Init IRQ_DIS; IRQ_Edge; IRQ_PEn; IRQ_ACK; IRQ_EN;	/* macro for run all the macros above */

#define TOD_DIS TODC_TODEN = 0; 	/**< TOD Disable */
#define TOD_Src TODC_TODCLKS = 0b00; /**< TOD Clock Source */ 
#define TOD_PRSc TODC_TODPS = 0b001; /**< TOD Prescaler 32.768 kHz*/
#define TOD_QSEC TODSC_QSECIE = 1; /**< Quarter-Second Interrupt Enable */
#define TOD_SEC TODSC_SECIE = 1; /**< Second Interrupt Enable */
#define TOD_En  TODC_TODEN = 1;; /**< TOD Enable */
#define TOD_Init TOD_DIS; TOD_Src; TOD_PRSc; TOD_QSEC; TOD_SEC; TOD_En;	/* macro for run all the macros above */

/* Enumeration for SCI Device Addresses */
typedef enum 
{
	NA = 0x00,
	Terminal = 0x10,
	GUI = 0x20,
	MATLAB = 0x30
}SCIDevAddress;

/* Enumeration for SCI Tasks */
typedef enum 
{
	RING = 0x01,
	ACK,
	NACK,
	RTX,
	ERR,
	GET,
	SET,
	ANS,
	RST,
	LOCK,
	DISCONN,
	FORBIDDEN,
	SERGET,
	SERANS, 
	SERSET,
}SCITask;

/* Enumeration for SCI Registers Addresses */
typedef enum 
{
	TERM = 0x01,
	PC,
	MAT,
	LM75_1,
	LM75_2,
	MCP9808_1,
	MCP9808_2,
	MCP9808_3,
	HTU21D_TEMP,
	HTU21D_HUM,
	ROOM_TEMP,
	WATER_OUT_TEMP,
	WATER_RETURN_TEMP,
	AVG_TEMP,
	OUTDOOR_TEMP,
	OUTDOOR_HUM,
	REGULATE_DAY_TEMP,
	REGULATE_NIGHT_TEMP,
	REGULATE_TEMP_HYST,
	REGULATE_WATER_MAX_TEMP,
	REGULATE_WATER_DIFF,
	ON_AVG_TEMP,
	OFF_AVG_TEMP,
	SYST_TYPE,
	HEATER,
	PUMP,
	AUTOSW,
	DAY_START_TIME,
	DAY_END_TIME,
	PUMP_DELAY_TIME,
	CLOCK,
	SERIALIZED
}SCIRegAddress;

/** TOD - TOD Status Register;  **/
typedef union {
  byte Byte;
  struct {
    byte TODQSEC     :1;                                        /*TOD Status Register Quarter Second Flag Bit */
    byte TODHSEC     :1;                                        /*TOD Status Register Half Second Flag Bit */
    byte TODSEC      :1;                                        /*TOD Status Register Second Flag Bit */
    byte TOD2SEC     :1;                                        /*TOD Status Register 2 Seconds Flag Bit*/ 
    byte TOD5SEC     :1;                                        /*TOD Status Register 5 Seconds Flag Bit */
    byte TOD10SEC    :1;                                        /*TOD Status Register 10 Seconds Flag Bit */
    byte TOD30SEC    :1;                                        /*TOD Status Register 30 Seconds Flag Bit */
    byte TOD1MIN     :1;                                        /*TOD Status Register 1 Minute Flag Bit */
  } Bits;
} TODSTR;
extern volatile TODSTR TOD;
#define TODReg                         TOD.Byte
#define TOD_TODQSEC                    TOD.Bits.TODQSEC
#define TOD_TODHSEC                    TOD.Bits.TODHSEC
#define TOD_TODSEC                     TOD.Bits.TODSEC
#define TOD_TOD2SEC                    TOD.Bits.TOD2SEC
#define TOD_TOD5SEC                    TOD.Bits.TOD5SEC
#define TOD_TOD10SEC                   TOD.Bits.TOD10SEC
#define TOD_TOD30SEC                   TOD.Bits.TOD30SEC
#define TOD_TOD1MIN                    TOD.Bits.TOD1MIN

#define TOD_TODQSEC_MASK               1U
#define TOD_TODHSEC_MASK               2U
#define TOD_TODSEC_MASK                4U
#define TOD_TOD2SEC_MASK               8U
#define TOD_TOD5SEC_MASK               16U
#define TOD_TOD10SEC_MASK              32U
#define TOD_TOD30SEC_MASK              64U
#define TOD_TOD1MIN_MASK               128U

/* Time structure */
typedef struct time{
	unsigned char seconds;
	unsigned char minutes;
	unsigned char hours;
	unsigned char dayOfWeek;
	unsigned char day;
	unsigned char month;
	unsigned int year;
} time;

/* WeekDays definitions */
#define MO 1
#define TU 2
#define WE 3
#define TH 4
#define FR 5
#define SA 6
#define SU 7

/* variable for the TOD tick */
extern unsigned TOD_tick;

/* Custom type definitions */
typedef long long int decimal; /* decimal number (2 decimal places)*/
typedef unsigned char ADDRESS;
typedef enum { false, true } bool;
typedef unsigned char uint8_t;

/**
 @brief    Delay code for waiting
 @param    void
 @return   none
*/
void delay(unsigned int time);


#endif /* IO_H_ */
