uart.c


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