#ifndef USART_TOOLS_H
#define USART_TOOLS_H

#include "stm32f4xx_hal.h"

//#define TIMEOUT_ON
#define TIMEOUT_EDGE	250	// msec

#define Modbus_LEN 0x000080
#define Modbus ((WORDE *)modbus)

#define HAL_UART_STATE_RECEIVED 0x28U

#define UNIVERSAL_ADDR 0x10U

#define Timeout_counter_len	1
#define In_Buffer_Start		(Timeout_counter_len)
#define In_Buffer_Len		32
#define Out_Buffer_Start	(Timeout_counter_len+In_Buffer_Len)
#define Out_Buffer_Len		128  
#define All_Buffer_Len		(Timeout_counter_len+In_Buffer_Len+Out_Buffer_Len)

#define	Timeout_counter	pRxBuffPtr[0]
#define	Buffer_in   	(huart->pRxBuffPtr + In_Buffer_Start)
#define	Buffer_out  	(huart->pRxBuffPtr + Out_Buffer_Start)

#define RS232	0
#define RS485	1

#define Package_type	0
#define Array_type		1

uint8_t UART_get_command(UART_HandleTypeDef *huart);
void HAL_UART_Receive_IT_user_handler(UART_HandleTypeDef *huart);
void HAL_UART_user_setup(void);
void HAL_UART_timeout_check(void);

void peek(UART_HandleTypeDef *huart);
void poke(UART_HandleTypeDef *huart);
void initload(UART_HandleTypeDef *huart); 
void load(UART_HandleTypeDef *huart);
void extendbios(UART_HandleTypeDef *huart);
void ReceiveCommandModbus6(UART_HandleTypeDef *huart);
void ReceiveCommandModbus3(UART_HandleTypeDef *huart);

enum
{
	CMD_MODBUS_3=3, ANS_MODBUS_3, CMD_MODBUS_15, CMD_MODBUS_6, ANS_MODBUS_6, 
	CMD_MODBUS_16=16,
	CMD_LOAD=51, CMD_UPLOAD, CMD_RUN, CMD_XFLASH, CMD_TFLASH, CMD_PEEK, CMD_POKE, CMD_INITLOAD, CMD_INIT, CMD_EXTEND,
	CMD_VECTOR=61, CMD_IMPULSE,
	CMD_STD=65, CMD_STD_ANS
};

typedef struct
{
	unsigned char Address;	// ????? ???????????
	unsigned char Number;	// ????? ???????

	unsigned char byte0;
	unsigned char byte1;
	unsigned char byte2;
	unsigned char byte3;
	unsigned char byte4;
	unsigned char byte5;
	unsigned char byte6;
	unsigned char byte7;

	unsigned char crc_lo;
	unsigned char crc_hi;
	unsigned char add_byte;
} CMD_TO_TMS;


extern uint16_t modbus[];//,archiv[],county[],espero[];

//extern uint16_t Maska[][8];

void Save_params(void);
void Load_params(void);
void Default_params(void);

#endif

