/**************************************
*  Arch Rock Corporation 2008 Copyright
*
**************************************/
#include <avr/io.h>
#include <inttypes.h>
#include "uart.h"

/*!
* Initialize UART to 9600 Baud Rate and only
* enable UART for transmission.
*/
void uart_init(void)
{
	uint8_t ubrr0;

	ubrr0 = 51;  // 9600 BAUD RATE @ 8Mhz using 2x == 0
	UBRR0L = ubrr0;
	UBRR0H = ubrr0 >> 8;
	UCSR0C = ( 3 << UCSZ00);

	// We only enable TX since we only
	// receive commands from Master
	// Enable TX
	UCSR0B |= (1 << TXEN0);
}

/*!
*  Send one byte over the uart.synchronously.
*/
static void uart_send_byte(uint8_t byte){
	UDR0 = byte;
	UCSR0A |= (1 << TXC0);	
	while(!(UCSR0A & (1 << TXC0)));
}

/*!
*  Send a preamble and then a stream of bytes 
*  synchronously over the UART.
*  
*/
void uart_send_buf(uint8_t * buf, uint8_t len){
	int index=0;
	uart_send_byte(START_PREM);
	uart_send_byte(len);

	while(index < len ){
		uart_send_byte(buf[index]);
		index++;
	}
}
