-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathSerial.c
78 lines (59 loc) · 2.88 KB
/
Serial.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
/*----------------------------------------------------------------------------
* Name: Serial.c
* Purpose: Low level serial routines
* Note(s):
*----------------------------------------------------------------------------
* This file is part of the uVision/ARM development tools.
* This software may only be used under the terms of a valid, current,
* end user licence from KEIL for a compatible version of KEIL software
* development tools. Nothing else gives you the right to use this software.
*
* This software is supplied "AS IS" without warranties of any kind.
*
* Copyright (c) 2010-2013 Keil - An ARM Company. All rights reserved.
*----------------------------------------------------------------------------*/
#include "LPC11xx.h" /* LPC11xx definitions */
/*----------------------------------------------------------------------------
Initialize UART pins, Baudrate
*----------------------------------------------------------------------------*/
void SER_init (void)
{
/* configure PINs GPIO1.6, GPIO1.7 for UART */
LPC_SYSCON->SYSAHBCLKCTRL |= ((1UL << 6) | /* enable clock for GPIO */
(1UL << 16) ); /* enable clock for IOCON */
LPC_IOCON->PIO1_6 = (1UL << 0); /* P1.6 is RxD */
LPC_IOCON->PIO1_7 = (1UL << 0); /* P1.7 is TxD */
/* configure UART0 */
LPC_SYSCON->SYSAHBCLKCTRL |= (1UL << 12); /* Enable clock to UART */
LPC_SYSCON->UARTCLKDIV = (4UL << 0); /* UART clock = CCLK / 4 */
LPC_UART->LCR = 0x83; /* 8 bits, no Parity, 1 Stop bit */
LPC_UART->DLL = 4; /* 115200 Baud Rate @ 12.0 MHZ PCLK */
LPC_UART->DLL = 12; /* 38400 Baud Rate @ 12.0 MHZ PCLK */
LPC_UART->FDR = 0x85; /* FR 1.627, DIVADDVAL 5, MULVAL 8 */
LPC_UART->DLM = 0; /* High divisor latch = 0 */
LPC_UART->LCR = 0x03; /* DLAB = 0 */
LPC_UART->IER = 0x1; /* enable UART int */
LPC_UART->FCR=0x01; //enable FIFO 1 byte
// LPC_UART->MCR=0x40; //enable RTS Flow Control
//must be set BEFORE enabling the IRQ
NVIC_SetPriority (UART_IRQn,0x80);
//enable UART in NVIC
NVIC_EnableIRQ(UART_IRQn);
}
/*----------------------------------------------------------------------------
Write character to Serial Port
*----------------------------------------------------------------------------*/
int sendchar (int c)
{
while (!(LPC_UART->LSR & 0x20));
LPC_UART->THR = c;
return (c);
}
/*----------------------------------------------------------------------------
Read character from Serial Port (blocking read)
*----------------------------------------------------------------------------*/
int getkey (void)
{
while (!(LPC_UART->LSR & 0x01));
return (LPC_UART->RBR);
}