
/*
 * serial.h
 *
 *  Created on: 4 Jan 2016
 *      Author: Mike
 */

#pragma once 

#include "main.h"
typedef struct
{
	UART_HandleTypeDef *Handle;

	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 unsigned int tx_usart_overruns; 
	volatile uint8_t tx_usart_running;
	
	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
#if defined SERIAL_UART4
extern usart_ctl uc4;
#endif
#if defined SERIAL_UART5
extern usart_ctl uc5;
#endif

///@brief  returns the number of characters in the recieve buffer
extern uint16_t SerialCharsReceived(usart_ctl *instance);
///@brief Get the amount of free space in the transmit buffer.
extern uint16_t SerialTransmitSpace(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,
						   UART_HandleTypeDef *usart);

extern void setBaud(usart_ctl *instance, uint32_t baud);
