uart.c


1
#include "lpc2468_registers.h"
2
#define  CR  0x0D
3
#define IER_RBR    0x01
4
#define IER_THRE  0x02
5
#define IER_RLS    0x04
6
7
int SendChar(char c)
8
{
9
  if (c == '\n')
10
  {
11
    while (!(U0LSR & 0x20));
12
    U0THR = CR;
13
  }
14
  while (!(U0LSR & 0x20));
15
  return (U0THR = c);
16
}
17
18
void SendString(char *str)
19
{
20
  SendChar(*str);              // The first byte
21
  while (*str++)              // All further bytes
22
  {
23
     SendChar(*str);
24
  }
25
}
26
27
char ReadChar()
28
{
29
  while ((U0LSR & 1) == 0);        // Auf "Receiver Data Ready"-Bit (RDR) im "Line Status Register" (LSR) warten
30
  return U0RBR;              // Byte vom Stack einlesen ("Receiver Buffer Register", RBR)
31
}
32
33
void InitUART0()
34
{
35
  PCONP |= (1 << 3);             // UART0 power on
36
  PINSEL0 |= 0x00000050;          // P0.2 TXD0, P0.3 RXD0
37
  U0FDR = 0;                // Fractional divider not used
38
  U0LCR = 0x83;              // 8 bits, no Parity, 1 Stop bit and enable access to divisor latches
39
  U0DLL = 97;                // 9600 Baud Rate @ 15 MHz VPB Clock
40
  U0DLM = 0;                // High divisor latch = 0
41
  U0LCR = 0x03;              // DLAB = 0 and enable access to U0IER
42
  PCLKSEL0 &= 0xFFFFFF7F;         // clock selection for UART0
43
  U0FCR = 0x07;              // for UART0: RX and TX FIFO reset and FIFO enable
44
}
45
46
void UART0ISR (void)
47
{
48
  SendString("Character received!\n");
49
  VICVectAddr = 0;       // Acknowledge Interrupt
50
}
51
52
void InitUART0Interrupt()
53
{
54
  U0IER = 0x00;                           // disable all interrupts
55
  VICIntEnClr = 0xFFFFFFFF;         // delete all interrupts
56
  VICIntSelect |= 0x00000000;       // select uart0 interrupt source as IRQ (not FIQ)
57
  VICVectAddr0 = (unsigned long)UART0ISR; // address of the ISR
58
  VICVectCntl0 |= ((1<<5) | 6);       // VIC enable and channel 6 (weißt evntl. VICVectAdr0 den uart0-interrupt zu???)
59
  VICIntEnable |= (1<<6);          // select uart0 in interrupt enable clear register
60
61
  U0IER = (IER_RBR | IER_THRE | IER_RLS | 0x03); // Enable UART0 interrupt
62
}