#include <stdio.h>
#include "xc.h"
#include "UART.h"
#include "config.h"


void InitU1(void)
{
    U1BRG = BRATE2;
    U1MODE = U_ENABLE_L | U_NOFLOWCONTROL;  //Enable in low speed mode without flow control
    U1STA = U_TX;
    RTS1 = 1;  //set TRS default status
    TRTS1 = 0;  // make RTS output
    _U1RXIF = 0; //Clear Interrupt
    _U1RXIE = 1;  //Enable Interrupt

    RTS1 = 0;  //Accept characters.
}  //InitU1

void InitU2(void)
{
    U2BRG = BRATE;
    U2MODE = U_ENABLE_H | U_NOFLOWCONTROL;  //Enable in high speed mode without flow control
    U2STA = U_TX;
    RTS2 = 1;  //set TRS default status
    TRTS2 = 0;  // make RTS output
    _U2RXIF = 0; //Clear Interrupt
    _U2RXIE = 1;  //Enable Interrupt
    RTS2 = 0;  //Accept characters.
}  //InitU2

int putU1( int c)
{
    while(U1STAbits.UTXBF);  //This needs a timeout.
    U1TXREG = c;
    return c;
}  //putU1

void putsU1(char *s)
{
    while(*s)
        putU1(*s++);  // No protection for non null-terminated arguments.
}

char getU1(void) {
    RTS1 = 0;
    while (!U1STAbits.URXDA);
    RTS1 = 1;
    return U1RXREG;
} //getU1


int putU2( int c)
{
    //while (CTS);
    while(U2STAbits.UTXBF);  //This needs a timeout.
    U2TXREG = c;
    return c;
}  //putU2

void putsU2(char *s)
{
    while(*s)
        putU2(*s++);  // No protection for non null-terminated arguments.
}

char getU2(void) {
    RTS2 = 0;
    while (!U2STAbits.URXDA);
    RTS2 = 1;
    return U2RXREG;
} //getU2


