#ifndef CONFIG_H
#define CONFIG_H

#include <rqstat.h>
#include "pinout.h"
#include <avr/interrupt.h>
#include <avr/wdt.h>
#include <avr/pgmspace.h>
#include <type.h>

#define SYS_CLK					(16000000)

#define APP_NAME				"FROG Handheld"

#define DEVICE_TYPE				ELZEL_FROGHANDHELD

#define VER_STR					"1.291\0"
#define INFO_STR				APP_NAME ", HW" HW_VER_STR ", FW" VER_STR
#define INFO_SIZE		35
#define VER_SIZE		5

#define TMR1_FREQ				1000

#define CFG_EEPROM_ADDR			0x20

#define EXTFLASH_SIZE			2097152
#define EXTFLASH_WRITEPAGE_SIZE	256
#define EXTFLASH_AUTO_ERASE		1

#define INTFLASH_SIZE			131072
#define LOG_BOTTOM_ADDR			INTFLASH_SIZE
#define LOG_TOP_ADDR			EXTFLASH_SIZE

#define USE_UART_C0
//#define UART_INDEX				C0
#define RS485_UART					C0
#define RS485_BAUD_RATE			57600
#define UART_C0_RXBUFFSIZE		200
#define UART_C0_TXBUFFSIZE		20
//#define UART_C0_DUPLEX			1
#define UART_C0_USE_TXEN		1
//#define UART_C0_RXCINTLVL_SET	1
//#define UART_C0_RXCINTLVL		USART_RXCINTLVL_MED_gc

// #if HW_VER == 11
// 
// #define USE_SPI_F
// #define SX_SPI					F
// #define SPI_F_MODE				SPI_MODE_0_gc | SPI_PRESCALER_DIV32
// 
// #define USE_SPI_C
// #define FLASH_SPI				C
// #define SPI_C_MODE				SPI_MODE_0_gc | SPI_PRESCALER_DIV4
// 
// #elif HW_VER == 10
// 
// #define USE_SPI_D
// #define SX_SPI					D
// #define SPI_D_MODE				SPI_MODE_0_gc | SPI_PRESCALER_DIV32
// 
// #define USE_SPI_F
// #define FLASH_SPI				F
// #define SPI_F_MODE				SPI_MODE_0_gc | SPI_PRESCALER_DIV4
// 
// #endif

#define USE_SPI_C
#define LCD_SPI					C
#define SPI_C_MODE				SPI_MODE_0_gc | SPI_PRESCALER_DIV16

#define NO_MULTIMASTER_MODE

#define COMM_ISR_FREQ	1000

#define RF_FREQUENCY			868300000
#define RF_FREQUENCY1			867800000
#define RF_FREQUENCY2			867300000

#define RF_BAUD					172800
#define SX_XTAL_FREQ			11059200
#define VCO_TRIM				VCO_TRIM_120

#define SX_USE_CRC
#define SYNCWORD				~0x12345678
#define SYNCWORD_LENGTH			4

#define MAX_OUTPACKET_DATA_SIZE	250
#define MAX_PACKET_DATA_SIZE	175
//#define NUM_REPEATS				3
#define UART_TX_TIMEOUT 		2		//timeout pro povoleni vysilani po prijmu
#define UART_NEXT_PCK_TIMEOUT	4		//timeout pro dalsi packet generovany timto zarizenim
#define UART_RESP_TIMEOUT		1		//po prijeti odpovedi (jakekoliv je tx timeout snizen na tuto hodnotu
#define UART_REQ_TIMEOUT		50		//timeout po prijeti jakehokoliv rq, bez ohledu na adresu

#define SX_DATA_SELECT()		NSS_DATA = 0/*, SPCR = (1 << SPE) | (1 << SPR0) | (1<<MSTR)*/		//fosc/8
#define SX_DATA_UNSELECT()		/*SPCR = (1 << SPE) | (1<<MSTR), */NSS_DATA = 1						//fosc/2

#define DEV_HANDLER_NEW
#define DEV_BROADCAST_HANDLER_NEW
//#define ANSWER_HANDLER

// #define USE_EXT_FLASH
// #define EXTFLASH_WAIT_FOR_WRITE			1
// #define EXTFLASH_WAIT_FOR_ERASE			0
// #define EXTFLASH_PAGE_SIZE				65536

// #define USE_BOOTLOADER
// #define SYSTEM_EXT
// #define SYSTEM_EXT_USE_EXTFLASH
// #define BOOTLOADMEM_CS			EXTFLASH_CS
// #define BOOTLOADER_SPI			FLASH_SPI
// #define BOOTLOADER_LED			RLED

#define WDT_RESET() wdt_reset()

#define RTC_TICK_FREQ			1000

#define GLCD_DRIVER				<eadog.h>
#define IMAGES_FLASH_ADDR		INTFLASH_SIZE
#define FLASHING_TOP			INTFLASH_SIZE //+ EXTFLASH_PAGE_SIZE
#define LCD_STDOUT
#define GLCD_INTERNAL_SKIN

#define LCD_LED_LIGHT_TIME		5000		//[ms]
#define LCD_TEXT_LIGHT_TIME		20			//[s]
#define FROG_ONLINE_TIMEOUT		3			//[s]

// #define sda()		SDA_IN
// #define sda_high()	TRIS_SDA = 0
// #define sda_low()	TRIS_SDA = 1, SDA_OUT = 0
// #define scl_high()	TRIS_SCL = 0
// #define scl_low()	TRIS_SCL = 1, SCL = 0

/*#define BATT_VOLT_DIVIDER		2
#define ADC_REFERENCE			1.875			//[V] Vcc/1.6 = 3V/1.6
#define ADC_FULLSCALE			4095
#define BATT_VOLT_CONSTANT		0.000916		//DIVIDER * ADC_REFERENCE/ADC_FULLSCALE
#define ADC_OFFSET_REAL			0.09375			//[V] ADC_REFERENCE * 0.05
#define ADC_OFFSET				204.75
*/
#define SLEEP_TIMEOUT			120				//[s]
#define OFF_MS_TIMEOUT			1500			//[ms]
//#define LCDLED_ONOFF_TIMEOUT	1500			//[ms]
#define SWITCH_PRESSED_TIMEOUT	20				//[ms]

#define ADC_FULLSCALE			4095.0
#define ADC_REF					1000.0	//[V]
#define R1						470.0		//[kOhm]
#define R2						120.0		//[kOhm]
#define BIAS					180

#define IMAGES_CNT				9
//#define TEST_FW
#define LCD_ROTATE180
#define LCD_WIDTH				128
#define LCD_HEIGHT				64

#define STATE_WAIT_TIMEOUT		2				//[s]
#define MAX_FROGS_NEAR			10

#define ROW_HEIGHT				11
#define FROG_NEAR_TIMEOUT		10				//[s]

#define STATE_ITEMS				17
#define CONFIG_ITEMS			15
#define BODY_STATE_ITEMS		7
#define ERRORS_ITEMS			13
#define INFO_ITEMS				6
#define FROGMENU_ITEMS			5

#define CONFIG_CHARS			15
#define SYSINFO_CHARS			11

#define DISPLAY_ITEMS			6

#define COLOR_ITEMS				4
#define COLOR_LEN				7
#define FREQ_ITEMS				3
#define FREQ_LEN				6
#define POUT_ITEMS				8
#define POUT_LEN				3
#define IFGAIN_ITEMS			4
#define IFGAIN_LEN				6
#define BOOL_ITEMS				2
#define BOOL_LEN				4
#define POS_ITEMS				2
#define POS_LEN					6

#define SEND_TRIES 				5

#define GSM_LEON_G100

#define MAX_CHARGE_FAULTS			5
#define TEMP_FAULT_TIMEOUT			10	//[s]

#endif
