/*C**************************************************************************
* $RCSfile: uart_lib.c,v $
*----------------------------------------------------------------------------
* Copyright (c) 2003 Atmel.
*----------------------------------------------------------------------------
* RELEASE:      $Name:  $
* REVISION:     $Revision: 1.20.4.6 $
* FILE_CVSID:   $Id: uart_lib.c,v 1.20.4.6 2008/01/02 14:19:54 raubree Exp $
*----------------------------------------------------------------------------
* PURPOSE:
* This file provides a minimal VT100 terminal access through UART
* and compatibility with Custom I/O support
*****************************************************************************/

/*_____ I N C L U D E S ____________________________________________________*/
#include "config.h"
#include "usart_driver.h"

/*_____ G L O B A L    D E F I N I T I O N _________________________________*/


/*_____ D E F I N I T I O N ________________________________________________*/
#ifdef DB101
  #define USART       USARTD0
  #define BR_SCALE    3329
  #define BR_SEL      10
#else
  #define USART       USARTF0
  #define BR_SCALE    3329
  #define BR_SEL      10
#endif
/*_____ M A C R O S ________________________________________________________*/


bit uart_test_hit (void)
{
  //return Uart_rx_ready();
  return USART_IsRXComplete(&USART);
}


bit uart_init (void)
{
#ifdef DB101
  // PD3 (TXD0) as output. 
  PORTD.DIRSET = PIN3_bm;

  // PD2 (RXD0) as input. 
  PORTD.DIRCLR = PIN2_bm;   
#else
  // PF3 (TXD0) as output. 
  PORTF.DIRSET = PIN3_bm;

  // PF2 (RXD0) as input. 
  PORTF.DIRCLR = PIN2_bm;  
#endif
  // USARTxx, 8 Data bits, No Parity, 1 Stop bit. 
  USART_Format_Set(&USART, USART_CHSIZE_8BIT_gc, USART_PMODE_DISABLED_gc, false);

  USART_Baudrate_Set(&USART, BR_SCALE , BR_SEL);

  // Enable both RX and TX. 
  USART_Rx_Enable(&USART);
  USART_Tx_Enable(&USART);  
  
  return TRUE;

}


r_uart_ptchar uart_putchar (p_uart_ptchar ch)
{
  do{
  }while(!USART_IsTXDataRegisterEmpty(&USART));
  USART_PutChar(&USART, ch);	
  return ch;
}

r_uart_ptchar uart_putcharD101 (p_uart_ptchar ch)
{
  do{
  }while(!USART_IsTXDataRegisterEmpty(&USART));
  USART_PutChar(&USART, ch);	
  return ch;
}



r_uart_gtchar uart_getchar (void)
{
  uint8_t receivedData;  
  receivedData = USART_GetChar(&USART);
  return receivedData; 
}


