APP 1 S5.

Dependencies:   mbed

Revision:
6:ccdbd5923e37
Parent:
5:c6f70b61aea5
Child:
8:e38cd9e16055
Child:
9:f35a5cde61a3
diff -r c6f70b61aea5 -r ccdbd5923e37 UART.cpp
--- a/UART.cpp	Sun Jan 10 18:21:11 2016 +0000
+++ b/UART.cpp	Sun Jan 10 22:24:00 2016 +0000
@@ -1,222 +1,53 @@
-#include <UART.h>
-#include <stdio.h>
- 
-//#define BAUD_RATE       9600
-//#define IN_BUF_SIZE     256
- 
-//#define TRUE 1
-//#define FALSE 0
- 
-//static unsigned char        inbuf[IN_BUF_SIZE];
-//static unsigned char        qin = 0;
-//static unsigned char        qout = 0;
- 
-//static char             flag_rx_waiting_for_stop_bit;
-//static char             flag_rx_off;
-//static char             rx_mask;
-//static char             flag_rx_ready;
-//static char             flag_tx_ready;
-//static char             timer_rx_ctr;
-//static char             timer_tx_ctr;
-//static char             bits_left_in_rx;
-//static char             bits_left_in_tx;
-//static char             rx_num_of_bits;
-//static char             tx_num_of_bits;
-//static int              internal_rx_buffer;
-//static int              internal_tx_buffer;
-//static int              user_tx_buffer;
+#include "UART.h"
+#include "APP.h"
+
 
-UART::UART (int baud_rate)
+void UARTInit(uint32_t baudrate)
 {
-    baudRate = 9600;
-}
- 
-//void UART::initUart()
-//{
-//    flag_tx_ready = FALSE;
-//    flag_rx_ready = FALSE;
-//    flag_rx_waiting_for_stop_bit = FALSE;
-//    flag_rx_off = FALSE;
-//    rx_num_of_bits = 10;
-//    tx_num_of_bits = 10;
+    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
     
-//    set_tx_pin_low();
-    //ticker.attach_us(&timer_isr, 1000000.0 / (BAUD_RATE * 3.0));
-//}
- 
- 
-//DigitalOut TX(p25);
-//DigitalIn RX(p26);
-//Ticker ticker;
- 
-//Serial pc(USBTX, USBRX);
- 
- 
-//Background functions to execute while waiting for input.
-//void idle(){
-//    wait_us(1.0);
-//}
- 
-//Sets the transmit pin to the high state.
-//void set_tx_pin_high() {
-//    TX = 1;
-//}
- 
-//Sets the transmit pin to the low state.
-//void UART::set_tx_pin_low()
-//{
-//    TX = 0;
-//}
- 
-//Returns 0 or 1 dependent on whether the receive pin is high or low
-//int get_rx_pin_status() {
-//    return RX.read();
-//}
- 
-//void timer_isr(void)
-//    {
-//    char            mask, start_bit, flag_in;
-// 
- 
-// Transmitter Section
-//    if ( flag_tx_ready )
-//        {
-//        
-//        if ( --timer_tx_ctr<=0 )
-//            {
-//            mask = internal_tx_buffer&1;
-//            internal_tx_buffer >>= 1;
-//            if ( mask )
-//                {
-//                set_tx_pin_high();          
-//                }
-//            else
-//                {
-//                set_tx_pin_low();
-//                }
-//            timer_tx_ctr = 3;
-//            if ( --bits_left_in_tx<=0 )
-//                {
-//                flag_tx_ready = FALSE;
-//                }
-//            }
-//        }
-// Receiver Section
-//    if ( flag_rx_off==FALSE )
-//        {
-//        if ( flag_rx_waiting_for_stop_bit )
-//            {
-//            if ( --timer_rx_ctr<=0 )
-//                {
-//                flag_rx_waiting_for_stop_bit = FALSE;
-//                flag_rx_ready = FALSE;
-//                internal_rx_buffer &= 0xFF;
-//                if ( internal_rx_buffer!=0xC2 )
-//                    {
-//                    inbuf[qin] = internal_rx_buffer;
-//                    if ( ++qin>=IN_BUF_SIZE )
-//                        {
-//                        qin = 0;
-//                        }
-//                    }
-//                }
-//            }
-//        else        // rx_test_busy
-//            {
-//            if ( flag_rx_ready==FALSE )
-//                {
-//                start_bit = get_rx_pin_status();
-// Test for Start Bit
-//                if ( start_bit==0 )
-//                    {
-//                    flag_rx_ready = TRUE;
-//                    internal_rx_buffer = 0;
-//                    timer_rx_ctr = 4;
-//                    bits_left_in_rx = rx_num_of_bits;
-//                    rx_mask = 1;
-//                    }
-//                }
-//            else    // rx_busy
-//                {
-//                if ( --timer_rx_ctr<=0 )
-//                    {               // rcv
-//                    timer_rx_ctr = 3;
-//                    flag_in = get_rx_pin_status();
-//                    if ( flag_in )
-//                        {
-//                        internal_rx_buffer |= rx_mask;
-//                        }
-//                    rx_mask <<= 1;
-//                    if ( --bits_left_in_rx<=0 )
-//                        {
-//                        flag_rx_waiting_for_stop_bit = TRUE;
-//                        }
-//                    }
-//                }
-//            }
-//        }
-//    }
- 
-//char _getchar( void )
-//    {
-//    char        ch;
- 
-//    do
-//        {
-//        while ( qout==qin )
-//            {
-//            idle();
-//            }
-//        ch = inbuf[qout] & 0xFF;
-//        if ( ++qout>=IN_BUF_SIZE )
-//            {
-//            qout = 0;
-//            }
-//        }
-//    while ( ch==0x0A || ch==0xC2 );
-//    return( ch );
-//    }
- 
-//void _putchar( int ch )
-//    {
-//    while ( flag_tx_ready );
-//    user_tx_buffer = ch;
- 
-// invoke_UART_transmit
-//    timer_tx_ctr = 3;
-//    bits_left_in_tx = tx_num_of_bits;
-//    internal_tx_buffer = (user_tx_buffer<<1) | 0x200;;
-//    flag_tx_ready = TRUE;
-//    }
- 
-//void flush_input_buffer( void )
-//    {
-//    qin = 0;
-//    qout = 0;
-//    }
- 
-//char kbhit( void )
-//    {
-//    return( qin!=qout );
-//    }
- 
-//void turn_rx_on( void )
-//    {
-//    flag_rx_off = FALSE;
-//    }
- 
-//void turn_rx_off( void )
-//    {
-//    flag_rx_off = TRUE;
-//    }
+    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;
     
-//void printStr(char* str)
-//{
-//    int i = 0;
-//    int len = strlen(str); 
-//    for(i = 0; i<len; i++)
-//    {
-//     wait(0.01);
-//        _putchar(str[i]);
-//    }
-//}
\ No newline at end of file
+    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--;
+    }
+}