/* flaneth - flash and ethernet
   Copyright (C) 2007-2012 Stefan Schuermans <stefan@blinkenarea.org>
   Copyleft: GNU public license V2 - http://www.gnu.org/copyleft/gpl.html
   a BlinkenArea project - http://www.blinkenarea.org/ */

#include <stdio.h>
#include <avr/io.h>
#include <avr/wdt.h>

#include "macros.h"
#include "uart.h"

#define BAUD_RATE 38400

// initialize
void UartInit(void)     // (extern)
{
  // enable transmission
  UCSR0B = 1 << TXEN;
  // set baudrate (16MHz crystal) 
  UBRR0L = 16000000 / (BAUD_RATE * 16L) - 1;

  // set STDOUT to use UartPutchar
  fdevopen(UartPutchar, NULL);
}

// write a character
int UartPutchar(char c, FILE * file)    // (extern)
{
  (void)file;

  // wait until last character was sent
  while (bit_is_clear(UCSR0A, UDRE))
    wdt_reset();

  // send character
  UDR0 = c;

  return 0;

  file = NULL;  // keep compiler happy
}
