//----------------------------------------------------
/**
	\file		atsam4e16e/inc/uart.h
	\ingroup	serial
	\ingroup	cortex-m4

	\brief		Funkce pro seriovou komunikaci UART
				<<< CORTEX M4 >>>

	\author		ConTeK s.r.o., Bc. Jakub Kastner
	\version	01.01
	\date		18.10.2013
*/
//----------------------------------------------------


#ifndef _USART_H_
#define _USART_H_		//!< Definice pritomnosti knihovny

#include <libsets.h>
#include <cortex-m4/inc/ctktypes.h>
#include <comm.h>
#include <sam4e.h>

//----------------------------------------------------
//!	Struktura kontextu Usartu

typedef struct _USARTDESC
{
	COMMDESC	Parent;		//!< popisovac knihovny
	Usart *		UsartAddr;	//!< adresa fyzickeho USARTu
	UART_MODE	Mode;		//!< RS232 ci RS485

	Pio *		Pio;		//!< IO controler
	uint32_t	TxPin;		//!< Cislo vysilaciho pinu
	uint32_t	RxPin;		//!< Cislo prijmaciho pinu
} USARTDESC;


//--------------------------------------------------
//!	Konstanty pripustnych rychlosti UARTu

typedef enum _USART_SPD
{
	USARTSPD_2400	=	2400,
	USARTSPD_4800	=	4800,
	USARTSPD_9600	=	9600,
	USARTSPD_10400	=	10400,
	USARTSPD_14400	=	14400,
	USARTSPD_19200	=	19200,
	USARTSPD_28800	=	28800,
	USARTSPD_38400	=	38400,
	USARTSPD_57600	=	57600,
	USARTSPD_76800	=	76800,
	USARTSPD_115200	=	115200,
	USARTSPD_230400	=	230400,
	USARTSPD_250000	=	250000,
	USARTSPD_500000	=	500000,
	USARTSPD_1000000 = 	1000000
} USART_SPD;



//----------------------------------------------------
//!	\name	 Funkce pro ovladani
//@{

//----------------------------------------------------
//void UsartRxDisable(USARTDESC * _urt);
//void UsartRxEnable(USARTDESC * _urt);
// IMPLEMENTOVANO V COMM.H
//----------------------------------------------------
void UsartTxDisable(USARTDESC * _urt);
void UsartTxEnable(USARTDESC * _urt);
void UsartRxDisable(USARTDESC * _urt);
void UsartRxEnable(USARTDESC * _urt);

void UsartTxPinVal(USARTDESC * _urt, bool_t _val);
void UsartRxPinVal(USARTDESC * _urt, bool_t _val);

void UsartTxPinEnablePIO(USARTDESC * _urt);
void UsartTxPinDisablePIO(USARTDESC * _urt);
void UsartRxPinEnablePIO(USARTDESC * _urt);
void UsartRxPinDisablePIO(USARTDESC * _urt);


void UsartChangeBaudrate(USARTDESC * _urt, const USART_SPD _baudRate);
//@}

//----------------------------------------------------
/**
	\brief	Funkce pro inicializaci komunikacniho automatu

	\param	urt			Struktura kontextu automatu
	\param	port		cislo com portu
	\param	baudrate	rychlost prenosu [Bd]
	\param  dataBits  Pocet datovych bitu CMM_BITS_XXX
	\param	parity		parita CMM_PARITY_xxx
	\param	stopbits	stopbity CMM_STOPBIT_xxx
	\param	mode		mode RS485
	\param	timeout		neni-li prijat znak do xx tiku,
						je predan neuplne zaplneny buffer

	\return	TRUE		inicializace probehla v poradku
			FALSE		chyba
*/
//----------------------------------------------------

bool_t UsartInitialize(USARTDESC * _urt, uint32_t _port, const USART_SPD _baudRate,
		const DATA_BITS _dataBits, const PARITY_TYPE _parity, const STOP_BITS _stopBits, const ticks_t _timeout);


//----------------------------------------------------
/**
	\brief	Funkce pro ukonceni komunikacniho automatu

	\param	urt		Struktura kontextu vysilace a prijmace
*/
//----------------------------------------------------

bool_t UsartFinalize(USARTDESC * const _urt);


#endif // _UART_H_
