/**************************************************************************
Name            - init_uart 
Parameters      - Nothing
Responsibility  - Set-up the onboard UART to send data that will be written
                    to the UDR (Uart Data Register)
                
Notes           - 
                 
Returns         - Nothing
Autor           - wje
                  Based on Atmel application note - AVR313
**************************************************************************/
#include <stdio.h>
#include <avr/pgmspace.h>
#include <avr/interrupt.h>

#include "uart_putchar.h"
#include "uart_getchar.h"
void init_uart(void) {
  // set the BAUD rate 
    UBRRH = (MYUBRR >>8);
    // UBRRH = (unsigned char) 0x00;
    UBRRL = MYUBRR;
    // UBRRL = (unsigned char) 0x33;

/*
  **************** Set-up  UCSRA **************
  The default values are ok
  This is the way it should look for this application.

  |   7   |   6   |   5   |   4   |   3   |   2   |   1   |   0   |  Bit
  |  RXC  |  TXC  |  UDRE |   FE  |  DOR  |   PE  |  U2X  | MPCM  |  Name
      0       0       1       0       0       0       0       0      Value
      R      R/W      R       R       R       R      R/W     R/W     Operations

*/

 /*
  **************** Set-up  UCSRB **************
   Enable the receier and transmitter
   This transfers control of the pins
   to the UART function
*/

   UCSRB |= _BV(RXEN) | _BV(TXEN);

/*
  The default values are ok for the rest of UCSRB
  This is the way it should look for this application.

  |   7   |   6   |   5   |   4   |   3   |   2   |   1   |   0   |  Bit
  | RXCIE | TXCIE | UDRIE | RXEN  | TXEN  | UCSZ2 | RXB8  | TXB8  |  Name
      0       0       0       1       1       0       0       0      Value
     R/W    R/W     R/W     R/W     R/W      R/W      R      R/W     Operations

   This set-up results on no parity and 1 stop bit.
   Baud rate is determined by BAUD #define & is set in init_uart().
*/


/*
  **************** Set-up  UCSRC **************
  The default values are ok 
  This is the way it should look for this application.

  |   7   |   6   |   5   |   4   |   3   |   2   |   1   |   0   |  Bit
  | URSEL | UMSEL |  UPM1 |  UPM0 |  USBS | UCSZ1 | UCSZ0 | UCPOL |  Name
      1       0       0       0       0       1       1       0      Value
     R/W    R/W     R/W     R/W     R/W      R/W     R/W     R/W     Operations
*/

    // initialize the receive buffer pointer
    rx_ptr = rx_buffer;

    UCSRB |= _BV(RXCIE);
}
