
//#include <stdio.h>

#include "UART.h"

BYTE ReceivePuffer[RX_BUF_LEN];		// Receive puffer
BYTE  m_RxInPointer = 0;			// Receive adat hova kerül
BYTE  m_RxOutPointer = 0;			// Receive adatot honnan olvassuk


void Init_UATRT(unsigned int ubrr)
{
	// Set baud rate
	UBRRH = (unsigned char)(ubrr>>8);
	UBRRL = (unsigned char)ubrr;
  	
	UCSRB = (1<<RXEN) | (1<<TXEN) | (1<<RXCIE);// | (1<<TXCIE); // Enable receiver and transmitter and Rx tx int
	
	UCSRC = (1<<UCSZ0) | (1<<UCSZ1);							// Set frame format: 8data, 1stop bit 

}


BYTE GetChar(void)
{
	BYTE Data = 0;
	Data = ReceivePuffer[m_RxOutPointer++];
	if(m_RxOutPointer == RX_BUF_LEN)m_RxOutPointer = 0;
	return Data;
}

BYTE RxReady(void)
{
	if(m_RxInPointer != m_RxOutPointer)return TRUE;
	else return FALSE;
}

BYTE WaitSerialData(void)
{
	BYTE m_DataDelay = 0;
	while(!RxReady())
	{
		m_DataDelay++;
		Delay(100);
		if(m_DataDelay > 100)return FALSE;
	}
	return TRUE;
}

//---------------------------------[ interrupts ]---------------------------------//


SIGNAL(SIG_USART0_RECV)	// RX Complete
{
	ReceivePuffer[m_RxInPointer++] = UDR;
	if(m_RxInPointer == RX_BUF_LEN)m_RxInPointer = 0;
}

