/***************************************************************************/
/*  Description      : UART driver for the NXP LPC13xx microcontrollers.   */
/*                                                                         */
/*                  (C) Copyright 2010 NXP Semiconductors                  */
/*                                                                         */
/***************************************************************************/

#include <LPC13Uxx.h>                              // LPC13xx definitions
#include "uart.h"

const char ascii[] = "0123456789ABCDEF";


void UART_Init(LONG baudrate)
{
  LONG brd = (SystemCoreClock / (baudrate << 4));

    LPC_IOCON->PIO0_18 = 0x00000081;               // UART RXD
    LPC_IOCON->PIO0_19 = 0x00000081;               // UART TXD

    LPC_SYSCON->SYSAHBCLKCTRL |= (1<<12);          // Enable UART clock
    LPC_SYSCON->UARTCLKDIV     = 1;                // divided by 1

    LPC_USART->FCR = 0x07;                         // Enable and reset TX and RX FIFO
    LPC_USART->LCR = 0x83;                         // 8 bits, no Parity, 1 Stop bit
    LPC_USART->DLL = (brd & 0xFF);                 // Set baud rate dividers
    LPC_USART->DLM = (brd >> 8);                   //
    LPC_USART->LCR = 0x03;                         // DLAB = 0
}

char ua_inchar(void)	                             // look if there is a character available
{
    if (LPC_USART->LSR & 1)
        return LPC_USART->RBR & 0x7F;              // yes, return character from input buffer
    else
        return 0xFF;                               // no, return -1
}

char ua_getchar(void)                              // this function waits for a character
{
    while (!(LPC_USART->LSR & 0x1));
    return LPC_USART->RBR & 0x7F;                  // Read character from input buffer
}

void ua_outchar(char c)		
{
    LPC_USART->THR = c;
    while(!(LPC_USART->LSR & 0x40));
}

void PrintByte(unsigned char b)
{
    ua_outchar(ascii[b >> 4]);
    ua_outchar(ascii[b & 0x0f]);
}

void PrintWord(unsigned short w)
{
    PrintByte((unsigned char)(w>>8));
    PrintByte((unsigned char)w);
}

//void PrintLong(long l)
//{
//    PrintWord((WORD)(l>>16));
//    PrintWord((WORD)l);
//}

void PrintBDecimal(unsigned char b)
{
  unsigned char p;

    p = ascii[b / 10];
    if (p != '0')
        ua_outchar(p);
    ua_outchar(ascii[b % 10]);
}

void PrintWDecimal(unsigned short w)
{
  BYTE p[5];

    p[4] = ascii[w / 10000];
    w %= 10000;
    p[3] = ascii[w / 1000];
    w %= 1000;
    p[2] = ascii[w / 100];
    w %= 100;
    p[1] = ascii[w / 10];
    p[0] = ascii[w % 10];

    if (p[4] == '0')
    {
        p[4] = ' ';
        if (p[3] == '0')
        {
            p[3] = ' ';
            if (p[2] == '0')
            {
                p[2] = ' ';
                if (p[1] == '0')
                    p[1] = ' ';
            }
        }
    }
    ua_outchar(p[4]);
    ua_outchar(p[3]);
    ua_outchar(p[2]);
    ua_outchar(p[1]);
    ua_outchar(p[0]);
}

void PrintString(const char *s)
{
    while (*s)
    {
        if (*s == '\n')
            ua_outchar('\r');                      // output a '\r' first
        ua_outchar(*s);
	s++;
    }
}
