// Konfigurace portu
#ifndef _PIN_CONFIG_H
	#define _PIN_CONFIG_H
	
	#include "stm32f10x_gpio.h"
	#include "stm32f10x_rcc.h"
	
//	#define FINAL										// Odkomentovat u finalni verze - prehazuje pin LCD_D5 kvuli nefunkcnimu pinu na prototypove desce

	extern GPIO_InitTypeDef GPIO_InitStructure;
	
// Zapojeni napajeni pro vsechny GPIO porty a alternativni funkce
	#define ALL_PORTS_POWERED()	RCC->APB2ENR |= (RCC_APB2Periph_GPIOA | RCC_APB2Periph_GPIOB | \
													   RCC_APB2Periph_GPIOC |  RCC_APB2Periph_GPIOD | \
													   RCC_APB2Periph_AFIO);
	
/* Informacni LED diody na desce ---------------------------- */
	#define INFO_LED_PIN		GPIO_Pin_13	// Na finalni desce

	#define INFO_LED_PORT		GPIOC
	
	#define INFO2_LED_PIN		GPIO_Pin_9
	#define INFO2_LED_PORT		GPIOC
	
	#define INFO_LED_ON()		INFO_LED_PORT->BSRR	= INFO_LED_PIN;					// Rozsviceni LED
	#define INFO2_LED_ON()		INFO2_LED_PORT->BSRR= INFO2_LED_PIN;					// Rozsviceni LED
	#define INFO3_LED_ON()		INFO3_LED_PORT->BSRR= INFO3_LED_PIN;					// Rozsviceni LED
	#define INFO_LED_OFF()		INFO_LED_PORT->BRR	= INFO_LED_PIN;					// Zhasnuti LED
	#define INFO2_LED_OFF()		INFO2_LED_PORT->BRR	= INFO2_LED_PIN;					// Zhasnuti LED
	#define INFO3_LED_OFF()		INFO3_LED_PORT->BRR	= INFO3_LED_PIN;					// Zhasnuti LED
								
// Inicializace pinu na kterem je informacni LED jako vystupu --
	#define INFO_LED_Out()		GPIO_InitStructure.GPIO_Pin = INFO_LED_PIN;	\
								GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP; \
								GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; \
								GPIO_Init(INFO_LED_PORT, &GPIO_InitStructure);
	#define INFO2_LED_Out()		GPIO_InitStructure.GPIO_Pin = INFO2_LED_PIN;	\
								GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP; \
								GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; \
								GPIO_Init(INFO2_LED_PORT, &GPIO_InitStructure);
	#define INFO3_LED_Out()		GPIO_InitStructure.GPIO_Pin = INFO3_LED_PIN;	\
								GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP; \
								GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; \
								GPIO_Init(INFO3_LED_PORT, &GPIO_InitStructure);
	
/* LCD s radicem HD44780 s 4-bit sbernici --------------------- */
	#define LCD_E_PIN			GPIO_Pin_11
	#define LCD_RS_PIN			GPIO_Pin_12
	#define	LCD_CONROL_PORT		GPIOA
	
	#define LCD_D7_PIN			GPIO_Pin_6
	#define LCD_D6_PIN			GPIO_Pin_7
#ifdef FINAL	
	#define LCD_D5_PIN			GPIO_Pin_8		// GPIO_Pin_8 - na vyrobne desce na diplomku
#else
	#define LCD_D5_PIN			GPIO_Pin_10		// GPIO_Pin_8 - na vyrobne desce na diplomku
#endif	
	#define LCD_D4_PIN			GPIO_Pin_9
	#define	LCD_DATA_PORT		GPIOC
	
	#define LCD_D7_High()		LCD_DATA_PORT->BSRR = LCD_D7_PIN;
	#define LCD_D6_High()		LCD_DATA_PORT->BSRR = LCD_D6_PIN;
	#define LCD_D5_High()		LCD_DATA_PORT->BSRR = LCD_D5_PIN;
	#define LCD_D4_High()		LCD_DATA_PORT->BSRR = LCD_D4_PIN;
	
	#define LCD_D7_Low()		LCD_DATA_PORT->BRR = LCD_D7_PIN;
	#define LCD_D6_Low()		LCD_DATA_PORT->BRR = LCD_D6_PIN;
	#define LCD_D5_Low()		LCD_DATA_PORT->BRR = LCD_D5_PIN;
	#define LCD_D4_Low()		LCD_DATA_PORT->BRR = LCD_D4_PIN;
	
	
	#define LCD_RS_High()		LCD_CONROL_PORT->BSRR = LCD_RS_PIN;					// Nastaveni log "1" na pin RS u LCD			
	#define LCD_RS_Low()		LCD_CONROL_PORT->BRR = LCD_RS_PIN;					// Nastaveni log "0" na pin RS u LCD
	#define LCD_E_High()		LCD_CONROL_PORT->BSRR = LCD_E_PIN;					// Nastaveni log "1" na pin E u LCD			
	#define LCD_E_Low()			LCD_CONROL_PORT->BRR = LCD_E_PIN;					// Nastaveni log "0" na pin E u LCD
	
// Inicializace pinu pro LCD --------------------------------
	#define LCD_Out()			GPIO_InitStructure.GPIO_Pin = LCD_D7_PIN | LCD_D6_PIN | LCD_D5_PIN | LCD_D4_PIN;	\
								GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP; \
								GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; \
								GPIO_Init(LCD_DATA_PORT, &GPIO_InitStructure); \
								\
								GPIO_InitStructure.GPIO_Pin = LCD_E_PIN|LCD_RS_PIN; \
								GPIO_Init(LCD_CONROL_PORT, &GPIO_InitStructure);
	
/* PWM kanaly pro rizeni vystupniho proudu spinanych zdroju, ventilatoru a podsviceni LCD */
	#define PWM_LED_1_PIN		GPIO_Pin_3			//5x LED 645nm => Inom = 1 400 mA, cerne kulate
	#define PWM_LED_1_PORT		GPIOA
	#define PWM_LED_2_PIN		GPIO_Pin_2			//1x LED 660nm => Inom = 1 000 mA, cerna ctverata
	#define PWM_LED_2_PORT		GPIOA
	#define PWM_LED_3_PIN		GPIO_Pin_1			//1x LED 735nm => Inom = 1 000 mA, bila ctverata
	#define PWM_LED_3_PORT		GPIOA
	#define PWM_LED_4_PIN		GPIO_Pin_0			//1x LED bila => Inom = 350 mA, stribrna
	#define PWM_LED_4_PORT		GPIOA
	#define PWM_LED_1_4_PORT	GPIOA
	#define PWM_LED_5_PIN		GPIO_Pin_0			//2x LED 455nm => Inom = 350 mA, bila kulata
	#define PWM_LED_5_PORT		GPIOB

	#define PWM_LED_6_PIN		GPIO_Pin_2
	#define PWM_LED_6_PORT		GPIOD
		
	#define PWM_Set_LED_1_OFF();		PWM_LED_1_PORT->BSRR = PWM_LED_1_PIN;
	#define PWM_Set_LED_2_OFF();		PWM_LED_2_PORT->BSRR = PWM_LED_2_PIN;
	#define PWM_Set_LED_3_OFF();		PWM_LED_3_PORT->BSRR = PWM_LED_3_PIN;
	#define PWM_Set_LED_4_OFF();		PWM_LED_4_PORT->BSRR = PWM_LED_4_PIN;
	#define PWM_Set_LED_5_OFF();		PWM_LED_5_PORT->BSRR = PWM_LED_5_PIN;
	
	#define PWM_Set_LED_6_ON();			PWM_LED_6_PORT->BSRR = PWM_LED_6_PIN;
	#define PWM_Set_LED_6_OFF();		PWM_LED_6_PORT->BRR = PWM_LED_6_PIN;
	
	#define PWM_Get_LED6()				((PWM_LED_6_PORT->ODR & PWM_LED_6_PIN) != 0)

	#define FAN_PIN				GPIO_Pin_1
	#define FAN_PORT			GPIOB
	
	#define BACKLIGHT_PIN		GPIO_Pin_8
	#define BACKLIGHT_PORT		GPIOA
	
// Inicializace pinu PWM jako alternativni funkce - PWM
	#define PWM_OutPWM()		GPIO_InitStructure.GPIO_Pin =  PWM_LED_1_PIN | PWM_LED_2_PIN | PWM_LED_3_PIN | \
															   PWM_LED_4_PIN; \
							    GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP; \
								GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; \
								GPIO_Init(PWM_LED_1_4_PORT, &GPIO_InitStructure); \
								\
								GPIO_InitStructure.GPIO_Pin =  PWM_LED_5_PIN | FAN_PIN; \
								GPIO_Init(PWM_LED_5_PORT, &GPIO_InitStructure); \
								\
								GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP; \
								GPIO_InitStructure.GPIO_Pin =  PWM_LED_6_PIN;  \
								GPIO_Init(PWM_LED_6_PORT, &GPIO_InitStructure);
	
	#define PWM_OutLED_Disable()	GPIO_InitStructure.GPIO_Pin =  PWM_LED_1_PIN | PWM_LED_2_PIN | PWM_LED_3_PIN | \
															   	   PWM_LED_4_PIN; \
							    	GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP; \
									GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; \
									GPIO_Init(PWM_LED_1_4_PORT, &GPIO_InitStructure); \
									\
									GPIO_InitStructure.GPIO_Pin =  PWM_LED_5_PIN; \
									GPIO_Init(PWM_LED_5_PORT, &GPIO_InitStructure);
	
	#define PWM_OutLED_ReEnable()	GPIO_InitStructure.GPIO_Pin =  PWM_LED_1_PIN | PWM_LED_2_PIN | PWM_LED_3_PIN | \
															   	   PWM_LED_4_PIN; \
							    	GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP; \
									GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; \
									GPIO_Init(PWM_LED_1_4_PORT, &GPIO_InitStructure); \
									\
									GPIO_InitStructure.GPIO_Pin =  PWM_LED_5_PIN; \
									GPIO_Init(PWM_LED_5_PORT, &GPIO_InitStructure);

	#define Backlight_Out()		GPIO_InitStructure.GPIO_Pin =   BACKLIGHT_PIN; \
							    GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP; \
								GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; \
								GPIO_Init(BACKLIGHT_PORT, &GPIO_InitStructure);

	#define Backlight_ON()		BACKLIGHT_PORT->BSRR = BACKLIGHT_PIN;					// Nastaveni log "1"	
	#define Backlight_OFF()		BACKLIGHT_PORT->BRR = BACKLIGHT_PIN;					// Nastaveni log "0"
	

/* Ethernet controler ENC28J60 --------------------------------------------- */	
	#define ETH_SPI_Port	GPIOA
	#define ETH_SI_Port		GPIOA
	#define ETH_SO_Port		GPIOA
	#define ETH_SCLK_Port	GPIOA
	#define ETH_CS_Port		GPIOA
	#define ETH_INT_Port	GPIOB
	
	#define ETH_SI_Pin		GPIO_Pin_7
	#define ETH_SO_Pin		GPIO_Pin_6
	#define ETH_CLK_Pin		GPIO_Pin_5
	#define ETH_CS_Pin		GPIO_Pin_4
	#define ETH_INT_Pin		GPIO_Pin_10

/* AD prevodnik ------------------------------------------------------------ */
	#define ADC_LED_1_PIN		GPIO_Pin_3
	#define ADC_LED_2_PIN		GPIO_Pin_2
	#define ADC_LED_3_PIN		GPIO_Pin_1
	#define ADC_LED_4_PIN		GPIO_Pin_0
	#define ADC_LED_5_PIN		GPIO_Pin_4
	#define ADC_VBAT_PIN		GPIO_Pin_5
	#define ADC_PORT			GPIOC
	
	#define ADC_IN()			GPIO_InitStructure.GPIO_Pin =  ADC_LED_1_PIN | ADC_LED_2_PIN | ADC_LED_3_PIN | \
															   ADC_LED_4_PIN | ADC_LED_5_PIN | ADC_VBAT_PIN; \
							    GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING; \
								GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; \
								GPIO_Init(ADC_PORT, &GPIO_InitStructure);

/* USART - seriova linka ---------------------------------------------------- */
	// Definice pinu pro UASRT1
	#define USART1_TX_Pin		GPIO_Pin_9
	#define USART1_RX_Pin		GPIO_Pin_10
	#define UASRT1_Port			GPIOA

	#define USART1_ON()			GPIO_InitStructure.GPIO_Pin = 	USART1_TX_Pin; \
    							GPIO_InitStructure.GPIO_Mode = 	GPIO_Mode_AF_PP; \
    							GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; \
    							GPIO_Init(UASRT1_Port, &GPIO_InitStructure); \
								\
								GPIO_InitStructure.GPIO_Pin = 	USART1_RX_Pin; \
    							GPIO_InitStructure.GPIO_Mode = 	GPIO_Mode_IN_FLOATING; \
    							GPIO_Init(UASRT1_Port, &GPIO_InitStructure); 

/* Tlacitka ----------------------------------------------------------------- */
	#define TL_1_Pin			GPIO_Pin_6
	#define TL_1_Port			GPIOB
	#define TL_2_Pin			GPIO_Pin_5
	#define TL_2_Port			GPIOB
	#define TL_3_Pin			GPIO_Pin_8
	#define TL_3_Port			GPIOB
	#define TL_4_Pin			GPIO_Pin_9
	#define TL_4_Port			GPIOB
	#define TL_Port				GPIOB

	#define TLACITKA_IN()		GPIO_InitStructure.GPIO_Pin = 	TL_1_Pin | TL_2_Pin | TL_3_Pin | \
																TL_4_Pin; \
    							GPIO_InitStructure.GPIO_Mode = 	GPIO_Mode_IN_FLOATING; \
    							GPIO_Init(TL_Port, &GPIO_InitStructure); 
	// Makra pro cteni stau tlacitka - vraci platnou podminku pokud je na pinu log "1" => nestisknute tlacitko
	#define TL_1_Read_1()		((TL_Port->IDR & TL_1_Pin) != (uint32_t)Bit_RESET)
	#define TL_2_Read_1()		((TL_Port->IDR & TL_2_Pin) != (uint32_t)Bit_RESET)
	#define TL_3_Read_1()		((TL_Port->IDR & TL_3_Pin) != (uint32_t)Bit_RESET)
	#define TL_4_Read_1()		((TL_Port->IDR & TL_4_Pin) != (uint32_t)Bit_RESET)
	// Makra pro cteni stau tlacitka - vraci platnou podminku pokud je na pinu log "0" => stisknute tlacitko
	#define TL_1_Read_0()		((TL_Port->IDR & TL_1_Pin) == (uint32_t)Bit_RESET)
	#define TL_2_Read_0()		((TL_Port->IDR & TL_2_Pin) == (uint32_t)Bit_RESET)
	#define TL_3_Read_0()		((TL_Port->IDR & TL_3_Pin) == (uint32_t)Bit_RESET)
	#define TL_4_Read_0()		((TL_Port->IDR & TL_4_Pin) == (uint32_t)Bit_RESET)

	#define TL_OK_Read_1()		TL_2_Read_1()
	#define TL_OK_Read_0()		TL_2_Read_0()
	#define TL_MENU_Read_1()	TL_1_Read_1()
	#define TL_MENU_Read_0()	TL_1_Read_0()
	#define TL_ZPET_Read_1()	TL_1_Read_1()
	#define TL_ZPET_Read_0()	TL_1_Read_0()
	#define TL_NAHORU_Read_1()	TL_4_Read_1()
	#define TL_NAHORU_Read_0()	TL_4_Read_0()
	#define TL_DOLU_Read_1()	TL_3_Read_1()
	#define TL_DOLU_Read_0()	TL_3_Read_0()
	
/* 1-Wire sbernice pro teplotni cidla DS18B20 ------------------------------- */
	#define OW_PIN				GPIO_Pin_12	// GPIO_Pin_3
	#define OW_PORT				GPIOC

/* Vstup pro vlhkostni cidlo ------------------------------------------------ */
	#define HUM_PIN				GPIO_Pin_7
	#define HUM_PORT			GPIOB
	#define HUM_IN()			GPIO_InitStructure.GPIO_Pin =  HUM_PIN; \
							    GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING; \
								GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; \
								GPIO_Init(HUM_PORT, &GPIO_InitStructure);

	#define PIEZO_PIN			GPIO_Pin_11
	#define PIEZO_PORT			GPIOB
	#define PEZO_ON()			PIEZO_PORT->BSRR = PIEZO_PIN;
	#define PEZO_OFF()			PIEZO_PORT->BRR = PIEZO_PIN;
	#define PEZO_BEEP()			PEZO_ON(); \
								vTaskDelay(20*portTICK_FOR_1MS); \
								PEZO_OFF();
	#define PEZO_BEEP_NoTask()	PEZO_ON(); \
								for(del=0;del<65530;del++); \
								PEZO_OFF();
	
	#define PIEZO_OUT()			GPIO_InitStructure.GPIO_Pin =   PIEZO_PIN; \
							    GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP; \
								GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; \
								GPIO_Init(PIEZO_PORT, &GPIO_InitStructure);

#endif

// Konec souboru ===========================================================
