Uart Setup

C

#include <arch\philips\lpc2103_k9supd.h>
#include "coridium.h"
 
#define TEMT            (1<<6)
#define CARRAIGE_RET    0xD
void Initialize(void);
 
int main()
{
    int i;
    char c[] = "Hello World";
 
    Initialize();
    while (1) {
        i = 0;
 
        /* write c[] into FIFO byte by byte, until c[i] == '\0' */
        while (c[i]) {    
            U0THR = c[i];
            i++;
        }
 
        /* write a '\n' into FIFO buffer (ASCII 0x0D is "carriage return") */
        U0THR = 0xD;        
 
        /* Wait until U0THR and U0TSR are both empty */ 
        while ((U0LSR & (1 << 6)) == 0) {}
    }
}
 
void Initialize()
{
    /* Initialize Pin Select Block for Tx and Rx */ 
    PINSEL0 = 0x5;
 
    /* Enable FIFO's and reset them */ 
    U0FCR = 0x7;
 
    /* Set DLAB and word length set to 8bits */ 
    U0LCR = 0x83;
 
    /* Baud rate set to 9600 */ 
    U0DLL=0x10;              
    U0DLM=0x00;         
 
    /* Clear DLAB, U0DLL and U0THR are mapped to the same address 0xE000C000
    , when we want to write data into U0THR, DLAB should be 0; when we want to set the frequency devider in U0DLL,     
    DLAB should be 1*/     
    U0LCR=0x03;  
}

ARM Assembly

        .text
        .equ    PINSEL0, 0xE002C000
        .equ    U0FCR, 0xE000C008
        .equ    U0LCR, 0xE000C00C
        .equ    U0DLL, 0xE000C000
        .equ    U0DLM, 0xE000C004
        .equ    U0THR, 0xE000C000
        .equ    U0LSR, 0xE000C014
 
_start: .global _start
        .global main
        .global UNDEF_Routine
        .global SWI_Routine
        .global PAbt_Routine
        .global DAbt_Routine
        .global FIQ_Routine
        b        main
 
UNDEF_Routine:
SWI_Routine:
PAbt_Routine:
DAbt_Routine:
FIQ_Routine:
 
main:
        bl     INITIALIZE
infinite:
        ldr    r0, =hello
        ldr    r2, =U0THR
write:
        ldrb   r1, [r0], #1            // ldrb get a byte and increment the pointer (stored in r0)
        cmp    r1, #0
        beq    done_write
        str    r1, [r2, #0] 
        b      write
done_write:
 
        mov    r1, #0x0D
        str    r1, [r2, #0]
 
        ldr    r1, =U0LSR
waitempty:     
        ldr    r2, [r1, #0]
        mov       r3, #1
        and    r2, r3, LSL #6
        cmp    r2, #0
        bne    infinite
        b      waitempty
 
INITIALIZE: 
        ldr    r2, =PINSEL0
        mov    r1, #0x05
        str    r1, [r2, #0]
 
        ldr    r2, =U0FCR
        mov    r1, #0x07
        str    r1, [r2, #0]
 
        ldr    r2, =U0LCR
        mov    r1, #0x83
        str    r1, [r2, #0]
 
        ldr    r2, =U0DLL
        mov    r1, #0x10
        str    r1, [r2, #0]
 
        ldr    r2, =U0DLM
        mov    r1, #0x00
        str    r1, [r2, #0]
 
        ldr    r2, =U0LCR
        mov    r1, #0x03
        str    r1, [r2, #0]
 
        mov     pc, lr
 
        .data
hello:    .asciz "Hello World!"
Unless otherwise stated, the content of this page is licensed under Creative Commons Attribution-ShareAlike 3.0 License