forked from igkov/bcomp11
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathuart0.c
78 lines (68 loc) · 1.64 KB
/
uart0.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
#if !defined( WIN32 )
#include "LPC11xx.h"
#endif
#include "uart0.h"
#include "dbg.h"
#if __UART_IRQ
void UART_IRQHandler(void) {
uint8_t ch;
uint8_t IIR_val = (LPC_UART->IIR >> 1) & 0x07;
// Ïðèåìêà:
if (IIR_val == 0x03) {
if (LPC_UART->LSR & 0x01) {
ch = LPC_UART->RBR;
// Âûçûâàåì îáðàáîò÷èê ïðèøåäøåãî ñèìâîëà:
UART_DISPATCHER(ch);
} else {
return;
}
} else
if (IIR_val == 0x02) {
ch = LPC_UART->RBR;
// Âûçûâàåì îáðàáîò÷èê ïðèøåäøåãî ñèìâîëà:
UART_DISPATCHER(ch);
}
return;
}
#endif
void uart0_init(uint32_t baudrate) {
uint32_t Fdiv;
#if __UART_IRQ
NVIC_DisableIRQ(UART_IRQn);
#endif
// UART power on:
LPC_SYSCON->SYSAHBCLKCTRL |= (1<<16) ;
// UART I/O Config:
LPC_IOCON->PIO1_6 &= ~0x07;
LPC_IOCON->PIO1_6 |= 0x01;
LPC_IOCON->PIO1_7 &= ~0x07;
LPC_IOCON->PIO1_7 |= 0x01;
// Enable UART clock:
LPC_SYSCON->SYSAHBCLKCTRL |= (1<<12);
LPC_SYSCON->UARTCLKDIV = 0x1;
LPC_UART->LCR = 0x83; // 8 bits, no Parity, 1 Stop bit
Fdiv = ((SystemCoreClock/LPC_SYSCON->UARTCLKDIV)/16)/baudrate;
LPC_UART->DLM = Fdiv / 256;
LPC_UART->DLL = Fdiv % 256;
LPC_UART->LCR = 0x03;
#if 1
LPC_UART->FCR = 0x07; // Enable and reset TX and RX FIFO.
#endif
#if __UART_IRQ
LPC_UART->IER = 0x01 | 0x04; // IER_RBR | IER_RLS
NVIC_EnableIRQ(UART_IRQn);
#endif
}
void uart0_putchar (uint8_t ch) {
while (!(LPC_UART->LSR & 0x20));
LPC_UART->THR = ch;
}
uint8_t uart0_getchar (void) {
while (!(LPC_UART->LSR & 0x01));
return (LPC_UART->RBR);
}
void uart0_puts(const uint8_t * str) {
while ((*str) != 0) {
uart0_putchar(*str++);
}
}