/*
 * uart.c
 *
 * Created: 20.02.2019 20:50:13
 *  Author: jens
 */ 

#include <stdio.h>
#include <avr/io.h>
#include <avr/interrupt.h>
#include "uart.h"
#include "message.h"

static Fifo *g_pFifo = NULL;
ISR(USART_UDRE_vect)
{
	
}

ISR(USART_TX_vect)
{
	
}

ISR(USART_RX_vect)
{
	uint8_t status = UCSR0A;
	if (status & (1 << RXC0))
	{
		uint8_t rx_data = UDR0;
		Msg_t msg = {Uart, rx_data};
		fifo_push(g_pFifo, &msg);
	}
}

void uart_init(Fifo *pFifo, uint16_t BAUD_PRESCALE)
{
	g_pFifo = pFifo;

	// Set baud rate
	UBRR0L  = (uint8_t)BAUD_PRESCALE;
	UBRR0H = (uint8_t)(BAUD_PRESCALE >> 8);
	
	// Enable receiver and transmitter
	UCSR0B = (1<<TXEN0)|(1<<RXEN0);
	
	// Enable RX interrupt
	UCSR0B |= (1<<RXCIE0);
	
}

void uart_putc(char c)
{
	while((UCSR0A & (1<<UDRE0)) == 0)
	{
	}
	UDR0 = c;
}

int uart_putchar(char c, FILE *stream)
{
	uart_putc(c);
	if (c == 0x0A)
	{
		uart_putc(0x0D);
	}
	return 0;
}

void uart_puts(char const *str)
{
	while(*str)
	{
		char c = *(str++);
		uart_putc(c);
		if (c == 0x0A)
		{
			uart_putc(0x0D);
		}
	}
}

