#ifndef __HWDEFS
#define __HWDEFS

#include "stm32f10x_conf.h"

//*********************** HARDWARE I/O DEFINES ********************
//*****************************************************************


/*
------- Power supply backup subsystem --------
----------------------------------------------
*/

//Analog input for measure supply voltage
#define SUPPLY_MES_PORT 	GPIOA
#define SUPPLY_MES_PIN  	GPIO_Pin_4

//Analog input for measure battery voltage
#define BATT_MES_PORT 		GPIOA
#define BATT_MES_PIN  		GPIO_Pin_5

//Output switching battery measurement
#define BATT_MES_EN_PORT 	GPIOC
#define BATT_MES_EN_PIN  	GPIO_Pin_6

//Output enabling battery charger
#define BATT_CHRG_EN_PORT 	GPIOC
#define BATT_CHRG_EN_PIN  	GPIO_Pin_7

//Battery charger - status input 1
#define BATT_CHRG_ST1_PORT 	GPIOC
#define BATT_CHRG_ST1_PIN  	GPIO_Pin_9

//Battery charger - status input 2
#define BATT_CHRG_ST2_PORT 	GPIOC
#define BATT_CHRG_ST2_PIN  	GPIO_Pin_8

//Output enabling DC/DC backup power supply
#define BATT_POWER_EN_PORT 	GPIOA
#define BATT_POWER_EN_PIN  	GPIO_Pin_8


/*
---------------- Audio output ----------------
----------------------------------------------
*/

//Output switching audio amplifier
#define AMP_EN_PORT 		GPIOA
#define AMP_EN_PIN  		GPIO_Pin_12

//Output driving audio amplifier with PWM signal
#define BUZZ_OUT_PORT 		GPIOA
#define BUZZ_OUT_PIN  		GPIO_Pin_11


/*
------------------- UARTS --------------------
----------------------------------------------
*/

//UART for connection with HOST system - TX
#define HOST_UART_TX_PORT 		GPIOA
#define HOST_UART_TX_PIN  		GPIO_Pin_9

//UART for connection with HOST system - RX
#define HOST_UART_RX_PORT 		GPIOA
#define HOST_UART_RX_PIN  		GPIO_Pin_10

#define HOST_UART 				USART1

//UART for connection with SM621 Finferprint reader - TX
#define FPR_UART_TX_PORT 		GPIOA
#define FPR_UART_TX_PIN  		GPIO_Pin_2

//UART for connection with SM621 Finferprint reader - RX
#define FPR_UART_RX_PORT 		GPIOA
#define FPR_UART_RX_PIN  		GPIO_Pin_3

#define FPR_UART				USART2

//UART for connection with EM-18 RFID card reader - RX
#define RFID_UART_RX_PORT 		GPIOD
#define RFID_UART_RX_PIN  		GPIO_Pin_2

#define RFID_UART				UART5

//UART providing external RS485 connectivity - TX
#define RS485_UART_TX_PORT 		GPIOC
#define RS485_UART_TX_PIN  		GPIO_Pin_10

//UART providing external RS485 connectivity - RX
#define RS485_UART_RX_PORT 		GPIOC
#define RS485_UART_RX_PIN  		GPIO_Pin_11

//UART providing external RS485 connectivity - DIR
#define RS485_UART_DIR_PORT 	GPIOC
#define RS485_UART_DIR_PIN  	GPIO_Pin_12

#define RS485_UART				USART3



/*
------------- EXTERNAL BINARY I/O  -----------
----------------------------------------------
*/

//External binary relay output 1
#define EXT_OUT1_PORT 		GPIOA
#define EXT_OUT1_PIN  		GPIO_Pin_15

//External binary relay output 2
#define EXT_OUT2_PORT 		GPIOB
#define EXT_OUT2_PIN  		GPIO_Pin_9

//External binary input 1
#define EXT_IN1_PORT 		GPIOC
#define EXT_IN1_PIN  		GPIO_Pin_0

//External binary input 2
#define EXT_IN2_PORT 		GPIOC
#define EXT_IN2_PIN  		GPIO_Pin_1

//External binary input 3
#define EXT_IN3_PORT 		GPIOC
#define EXT_IN3_PIN  		GPIO_Pin_2

//External binary input 4
#define EXT_IN4_PORT 		GPIOC
#define EXT_IN4_PIN  		GPIO_Pin_3


/*
-------- CAPACITIVE KEYBOARD INTERFACE -------
----------------------------------------------
*/

//External binary input 4
#define KEYB_SCL_PORT 		GPIOB
#define KEYB_SCL_PIN  		GPIO_Pin_10

//External binary input 4
#define KEYB_SDA_PORT 		GPIOB
#define KEYB_SDA_PIN  		GPIO_Pin_11

//External binary input 4
#define KEYB_IRQ_PORT 		GPIOB
#define EXT_IRQ_PIN  		GPIO_Pin_2

#define KEYB_I2C			I2C2

/*
------------------ OTHER I/Os ----------------
----------------------------------------------
*/

//RED LED Diode output
#define LED_R_PORT 				GPIOB
#define LED_R_PIN  				GPIO_Pin_0

//GREEN LED Diode output
#define LED_G_PORT 				GPIOB
#define LED_G_PIN  				GPIO_Pin_1

//PIR Motion sensor input
#define PIR_IN_PORT 			GPIOA
#define PIR_IN_PIN  			GPIO_Pin_6

//FPR SM621 Enable output
#define FPR_EN_PORT 			GPIOA
#define FPR_EN_PIN  			GPIO_Pin_1

#define AMP_ON()					AMP_EN_PORT->BSRR = AMP_EN_PIN
#define AMP_OFF()					AMP_EN_PORT->BRR = AMP_EN_PIN

#define BEEP_ON()					AMP_ON();  BUZZ_OUT_PORT->CRH &= ~(0xF000); BUZZ_OUT_PORT->CRH |= (0xA000); Delay_ms(5); TIM_Cmd(TIM1, ENABLE)
#define BEEP_OFF()					AMP_OFF(); BUZZ_OUT_PORT->CRH &= ~(0xF000); BUZZ_OUT_PORT->CRH |= (0x4000); Delay_ms(5); TIM_Cmd(TIM1, DISABLE)

#define BEEP_ON_S()					 BUZZ_OUT_PORT->CRH &= ~(0xF000); BUZZ_OUT_PORT->CRH |= (0xA000); Delay_ms(5); TIM_Cmd(TIM1, ENABLE)
#define BEEP_OFF_S()				 BUZZ_OUT_PORT->CRH &= ~(0xF000); BUZZ_OUT_PORT->CRH |= (0x4000); Delay_ms(5); TIM_Cmd(TIM1, DISABLE)

#define RED_LED_ON()				LED_R_PORT->BSRR = LED_R_PIN; ledstat &= (~RED_LED_BLINK_STAT)
#define RED_LED_OFF()				LED_R_PORT->BRR = LED_R_PIN;  ledstat &= (~RED_LED_BLINK_STAT)
#define RED_LED_TOGGLE()			LED_R_PORT->ODR ^= LED_R_PIN

#define GREEN_LED_ON()				LED_G_PORT->BSRR = LED_G_PIN; ledstat &= (~GREEN_LED_BLINK_STAT)
#define GREEN_LED_OFF()				LED_G_PORT->BRR = LED_G_PIN; ledstat &= (~GREEN_LED_BLINK_STAT)
#define GREEN_LED_TOGGLE()			LED_G_PORT->ODR ^= LED_G_PIN

#define FPR_ON()					FPR_EN_PORT->BSRR = FPR_EN_PIN
#define FPR_OFF()					FPR_EN_PORT->BRR = FPR_EN_PIN

#define BATT_MEAS_ON()				BATT_MES_EN_PORT->BSRR = BATT_MES_EN_PIN
#define BATT_MEAS_OFF()				BATT_MES_EN_PORT->BRR = BATT_MES_EN_PIN
#define BATT_VOLTAGE_READ()			(((ADC1->JDR2)*(33*1454))/40960)


//Ledkoviny

#define LED_TIMER					TIM2

#define RED_LED_BLINK_STAT			0x01
#define GREEN_LED_BLINK_STAT		0x02

#define RED_LED_TOFF_STAT			0x04
#define GREEN_LED_TOFF_STAT			0x08

#define GREEN_LED_PULSED_STAT		0x10
#define RED_LED_PULSED_STAT			0x20

#define RED_LED_BLFAST()			ledstat |= RED_LED_BLINK_STAT; (LED_TIMER->ARR = 130);  (LED_TIMER->CNT=0)
#define RED_LED_BLSLOW()			ledstat |= RED_LED_BLINK_STAT; (LED_TIMER->ARR = 500)

#define GREEN_LED_BLFAST()			ledstat |= GREEN_LED_BLINK_STAT; LED_TIMER->ARR = 130;  (LED_TIMER->CNT=0)
#define GREEN_LED_BLSLOW()			ledstat |= GREEN_LED_BLINK_STAT; (LED_TIMER->ARR = 500);

#define RED_LED_PULSE()				ledstat |= RED_LED_TOFF_STAT;
#define GREEN_LED_PULSE()			ledstat |= GREEN_LED_TOFF_STAT;

//----------------- HOSTOVINY ------------------------


#define STAT_PIR_SIG	(1<<0)
#define STAT_TERMINAL_ACTIVE	(1<<1)
#define STAT_BATTERY_POWERED (1<<2)
#define STAT_AMP_PERMANENT_ACTIVE (1<<3)
#define STAT_RFID_READ (1<<4)

#endif
