APP 1 S5.

Dependencies:   mbed

UART.cpp

Committer:
vinbel93
Date:
2016-01-10
Revision:
6:ccdbd5923e37
Parent:
5:c6f70b61aea5
Child:
8:e38cd9e16055
Child:
9:f35a5cde61a3

File content as of revision 6:ccdbd5923e37:

#include "UART.h"
#include "APP.h"


void UARTInit(uint32_t baudrate)
{
    uint32_t Fdiv;
    uint32_t pclkdiv, pclk;
    
    LPC_SC->PCONP |= 1 << 25; // Activate UART3 in PCONP register.
    pclkdiv = (LPC_SC->PCLKSEL1 >> 18) & 0x03; // Bits 18~19 are for UART3
    
    switch (pclkdiv)
    {
        case 0x00:
        default:
            pclk = SYSTEM_CLOCK_FREQUENCY/4;
            break;
        case 0x01:
            pclk = SYSTEM_CLOCK_FREQUENCY;
            break;
        case 0x02:
            pclk = SYSTEM_CLOCK_FREQUENCY/2;
            break;
        case 0x03:
            pclk = SYSTEM_CLOCK_FREQUENCY/8;
            break;
    }

    LPC_PINCON->PINSEL0 &= ~0x00000003;
    LPC_PINCON->PINSEL0 |= 0x00000002;
    
    LPC_UART3->LCR = 0x83;         // 8 bits, no Parity, 1 Stop bit, DLAB = 1
    Fdiv = (pclk / 16) / baudrate; // baud rate
    LPC_UART3->DLM = Fdiv / 256;
    LPC_UART3->DLL = Fdiv % 256;
    LPC_UART3->LCR = 0x03;         // DLAB = 0
}

void UARTSend(uint8_t byte)
{
    LPC_UART3->THR = byte;
}

void UARTSend(uint8_t *buffer, uint32_t length)
{
    while (length != 0)
    {
        LPC_UART3->THR = *buffer;
        buffer++;
        length--;
    }
}