
/*
 * serial.h
 *
 *  Created on: 4 Jan 2016
 *      Author: Mike
 */

#ifndef INC_SERIAL_H_
#define INC_SERIAL_H_

#include "main.h"

typedef struct {
	USART_TypeDef *Instance;

	volatile uint8_t tx_usart_buff[TX_USART_BUFF_SIZ];
	volatile unsigned int tx_usart_in_Ptr;
	volatile unsigned int tx_usart_out_Ptr;
	volatile uint8_t tx_usart_running;
	volatile uint16_t tx_usart_count;

	volatile uint8_t rx_usart_buff[RX_USART_BUFF_SIZ];
	volatile unsigned int rx_usart_in_Ptr;
	volatile unsigned int rx_usart_out_Ptr;
	volatile uint8_t rx_usart_buffer_full;
} usart_ctl;

#if defined SERIAL_UART1
extern usart_ctl uc1;
#endif
#if defined SERIAL_UART2
extern usart_ctl uc2;
#endif
#if defined SERIAL_UART3
extern usart_ctl uc3;
#endif






/* returns the number of characters received by the Rx USART */
extern uint16_t SerialCharsReceived(usart_ctl * instance);

extern uint8_t PollSerial(usart_ctl * instance);
extern uint8_t GetCharSerial(usart_ctl * instance);
extern void EnableSerialRxInterrupt(usart_ctl * instance);

extern void UART_IRQHandler(usart_ctl * instance);

extern void PutCharSerial(usart_ctl * instance,uint8_t c);
extern void ResetTxBuffer(usart_ctl * instance);
extern void ResetRxBuffer(usart_ctl * instance);
extern void FlushSerial(usart_ctl * instance);
extern uint8_t  TxBufferEmpty(usart_ctl * instance);
///@brief  wait until the USART buffer is empty and all characters are sent
extern void TxWaitEmpty(usart_ctl *instance);


extern void init_usart_ctl(usart_ctl * instance,
			   USART_TypeDef * usart);



#endif /* INC_SERIAL_H_ */
