#ifndef _init_h
#define _init_h   


//DEFINICE

#define WAKEUP_IN_PORT      GPIOA    
#define WAKEUP_IN_PIN       GPIO_PIN_0    
#define WAKEUP_IN           WAKEUP_IN_PORT,WAKEUP_IN_PIN

#define NAPETI_IN_PORT      GPIOA    
#define NAPETI_IN_PIN       GPIO_PIN_1    
#define NAPETI_IN           NAPETI_IN_PORT,NAPETI_IN_PIN

#define USB_CONNECT_PORT    GPIOA    
#define USB_CONNECT_PIN     GPIO_PIN_2    
#define USB_CONNECT         USB_CONNECT_PORT,USB_CONNECT_PIN

#define CHARGE_STAT_PORT    GPIOA    
#define CHARGE_STAT_PIN     GPIO_PIN_3    
#define CHARGE_STAT         CHARGE_STAT_PORT,CHARGE_STAT_PIN

#define CS_FLASH_PORT       GPIOA    
#define CS_FLASH_PIN        GPIO_PIN_4    
#define CS_FLASH            CS_FLASH_PORT,CS_FLASH_PIN

#define SCLK_PORT           GPIOA    
#define SCLK_PIN            GPIO_PIN_5    
#define SCLK                SCLK_PORT,SCLK_PIN

#define MISO_PORT           GPIOA    
#define MISO_PIN            GPIO_PIN_6    
#define MISO                MISO_PORT,MISO_PIN

#define MOSI_PORT           GPIOA    
#define MOSI_PIN            GPIO_PIN_7    
#define MOSI                MOSI_PORT,MOSI_PIN

#define MOT_ONOFF_PORT      GPIOA    
#define MOT_ONOFF_PIN       GPIO_PIN_8    
#define MOT_ONOFF           MOT_ONOFF_PORT,MOT_ONOFF_PIN

#define PER_ONOFF_PORT      GPIOA    
#define PER_ONOFF_PIN       GPIO_PIN_9    
#define PER_ONOFF           PER_ONOFF_PORT,PER_ONOFF_PIN

#define CS_AKC_PORT         GPIOA    
#define CS_AKC_PIN          GPIO_PIN_10    
#define CS_AKC              CS_AKC_PORT,CS_AKC_PIN

#define NC2_PORT            GPIOA    
#define NC2_PIN             GPIO_PIN_11    
#define NC2                 NC2_PORT,NC2_PIN

#define NC3_PORT            GPIOA    
#define NC3_PIN             GPIO_PIN_12    
#define NC3                 NC3_PORT,NC3_PIN

#define SWDIO_PORT          GPIOA    
#define SWDIO_PIN           GPIO_PIN_13    
#define SWDIO               SWDIO_PORT,SWDIO_PIN

#define SWCLK_PORT          GPIOA    
#define SWCLK_PIN           GPIO_PIN_14    
#define SWCLK               SWCLK_PORT,SWCLK_PIN

#define NC4_PORT            GPIOA    
#define NC4_PIN             GPIO_PIN_12    
#define NC4                 NC4_PORT,NC4_PIN

//PORT B

#define INT1_AKC_PORT       GPIOB    
#define INT1_AKC_PIN        GPIO_PIN_0    
#define INT1_AKC           	NC1_PORT,NC1_PIN

#define CS_GYRO_PORT       	GPIOB                                              
#define CS_GYRO_PIN         GPIO_PIN_1                      // GPIO_PIN_1 - puvodni
#define CS_GYRO             CS_GYRO_PORT,CS_GYRO_PIN

#define INT2_AKC_PORT       GPIOB    
#define INT2_AKC_PIN        GPIO_PIN_2                      // GPIO_PIN_2
#define INT2_AKC            INT2_AKC_PORT,INT2_AKC_PIN

#define PWM_PULZ_PORT       GPIOB    
#define PWM_PULZ_PIN        GPIO_PIN_3   
#define PWM_PULZ            PWM_PULZ_PORT,PWM_PULZ_PIN

#define NC5_PORT            GPIOB    
#define NC5_PIN             GPIO_PIN_4    
#define NC5                 NC5_PORT,NC5_PIN

#define LED_CERVENA_PORT    GPIOB    
#define LED_CERVENA_PIN     GPIO_PIN_5    
#define LED_CERVENA         LED_CERVENA_PORT,LED_CERVENA_PIN

#define TX_DATA_PORT        GPIOB    
#define TX_DATA_PIN         GPIO_PIN_6    
#define TX_DATA             TX_DATA_PORT,TX_DATA_PIN

#define RX_DATA_PORT        GPIOB    
#define RX_DATA_PIN         GPIO_PIN_7    
#define RX_DATA             RX_DATA_PORT,RX_DATA_PIN

#define BAT_MEAS_ONOFF_PORT GPIOB    
#define BAT_MEAS_ONOFF_PIN  GPIO_PIN_8    
#define BAT_MEAS_ONOFF      BAT_MEAS_ONOFF_PORT,BAT_MEAS_ONOFF_PIN

//PORT C

#define NC6_PORT            GPIOC    
#define NC6_PIN             GPIO_PIN_14    
#define NC6                 NC6_PORT,NC6_PIN

#define NC7_PORT            GPIOC    
#define NC7_PIN             GPIO_PIN_15    
#define NC7                 NC7_PORT,NC7_PIN

//KONSTANTY

#define LED_ON              DISABLE
#define LED_OFF             ENABLE

#define MOT_ON              ENABLE
#define MOT_OFF             DISABLE

#define BAT_MEAS_ON         DISABLE
#define BAT_MEAS_OFF        ENABLE

#define TRUE                1    
#define FALSE             	0    

#define HIGH                1    
#define LOW               	0    

#define System_1ms          (SystemCoreClock/960)    
#define System_10ms         (SystemCoreClock/96)    
#define System_100ms        (SystemCoreClock*10)/96

#define PRIP_NO             0    
#define PRIP_UP             1    
#define PRIP_DOWN           2    
#define PRIP_STOP           3    



//FUNKCE

//Inicializace CLK pro periferije - 1Mhz
void CLK_1MHz_Init(void);

//Inicializace CLK pro komunikaci s PC - 32MHz
void CLK_32MHz_Init(void); 

//Deinicializace hodin
void CLK_Deinit(void);

//Inicializace GPIO 
void GPIO_Init(void);

//Deinicializace GPIO
void GPIO_Deinit(void);

void Periph_Deinit(void);

//Inicializace SPI
void SPI_Init(void);

//Deinicialializace SPI
void SPI_Deinit(void);

//SPI cteni a zapis bytu
uint8_t SPI_TransmitReceive(uint8_t data);

//SPI zapis pole
void SPI_TransmitField(uint8_t *Data ,uint16_t number);

// SPI cteni pole
void SPI_ReceiveField(uint8_t *Data ,uint16_t number); 

//RTC Init a nastavit na 0
void RTC_Init_0(void);

//RTC Init NEnastavovat na 0
void RTC_Init(void);

//RTC cas a datum
void RTC_TellTime(uint8_t *Year, uint8_t *WeekDay, uint8_t *Month, uint8_t * Date, uint8_t * Hr, uint8_t * Min, uint8_t * Sec);

//RTC sekundy
uint8_t RTC_TellSec(void);

//RTC nastavit cas a datum
void RTC_SetTime(uint8_t Year, uint8_t WeekDay, uint8_t Month, uint8_t Date, uint8_t Hr, uint8_t Min, uint8_t Sec);

//Inicializace a nastaveni PWM pro FET civky
void PWM_FET_Set(uint32_t F_Delka_periody, uint32_t F_Delka_pulzu);

//Stop PWM
void PWM_FET_Stop(void);

//Start PWM
void PWM_FET_Start(void);

//Start mereni baterie
void BAT_MEAS_Start(void);

//Stop mereni baterie 
void BAT_MEAS_Stop(void);

//Prepocet merene hodnoty na milivolty
uint32_t BAT_VOLTAGE(uint32_t adcArray);

//Prepocet merene hodnoty na miliprocenta
uint32_t BAT_CAPACITY(uint32_t adcArray);

//ON perieferie
void PERIFERIE_ON(void);

//OFF periferie
void PERIFERIE_OFF(void);

//Uspani MCU
void SLEEP(void);

//Pokud je zmerene napeti mensi nez 3V, uspi MCU
void SLEEP_IfBatLow(uint32_t adcValue);

//Inicializace a start watchdogu;
void WATCH_Init(void);

//refresh watchdogu
void WATCH_Refresh(void);

//UART inicializace
void UART1_Init(void);

//UART deinicializace
void UART1_Deinit(void);

//UART vyslani dat
void UART1_DMA_SEND (unsigned int delka_dat);

//LED ON
void Led_ON(void);

//LED OFF
void Led_OFF(void);

//vypocet teploty
int32_t ComputeTemperature(uint32_t measure, uint32_t VDDref);

//Zvuk zapinani
void SOUND_TurnOn(void);

//Zvuk vypinani
void SOUND_TurnOff(void);

//Zvuk slaba baterie
void SOUND_LowBat(void);

//Zvuk pripojeni USB
void SOUND_USBConnect(void);

//Zvuk odpojeni USB
void SOUND_USBDisconnect(void);

//Init CRC
void mgmCRC32_Init(void);


//Vypocet CRC
uint32_t mgmCRC32(uint8_t pBuffer[], uint32_t startIndex, uint32_t lastIndex);

//nacteni z ADC
void 		ADC_NAP_Init		(void);
uint16_t    ADC_NAP_IN_Read     (void);
uint16_t    ADC_NAP_OUT_Read    (void);

void        ADC_NAP_Start       (void);
void        ADC_NAP_Stop        (void);

void        ADC_Set_channel     (uint32_t adc_channel);

void 		Error_Handler		(void);

#endif


