#include "uart.h"

void serial_init()
{
    P1SEL = BIT1 + BIT2;            // Set P1.1 to RXD and P1.2 to TXD
    P1SEL2 = BIT1 + BIT2;           //

    UCA0CTL1 |= UCSSEL_2;           // Have USCI use SMCLK AKA main CLK
    UCA0BR0 = BR0_BD_4800;          // 4800 baud
    UCA0BR1 = BR1_BD_4800;
    UCA0MCTL = UCBRS_1;             // Modulation UCBRSx = 1
    UCA0CTL1 &= ~UCSWRST;           // Start USCI
    IE2 |= UCA0RXIE;                // Enable USCI0 RX interrupt
}

void serial_putch(char c) {
    while ( !(IFG2 & UCA0TXIFG) ) ;     // Wait if there is a transmission ongoing
    UCA0TXBUF = c;                      // Put the char into the UART TX buffer
}

void serial_puts(const char *ptr) {
    while(*ptr) {                       // Go until we are at the NULL
        serial_putch(*(ptr++));         // put out the single char
    }
}

unsigned char uart_getc()
{
    while (!(IFG2&UCA0RXIFG)); // USCI_A0 RX buffer ready?
    return UCA0RXBUF;
}


