1 /******************************************************************************
6 * This module provides interface routines to the LPC ARM UARTs.
7 * Copyright 2004, R O SoftWare
8 * No guarantees, warrantees, or promises, implied or otherwise.
9 * May be used for hobby or commercial purposes provided copyright
10 * notice remains intact.
12 * reduced to see what has to be done for minimum UART-support by mthomas
13 *****************************************************************************/
15 // #warning "this is a reduced version of the R O Software code"
19 /* on LPC210x: UART0 TX-Pin=P0.2, RX-Pin=P0.1
20 PINSEL0 has to be set to "UART-Function" = Function "01"
21 for Pin 0.0 and 0.1 */
23 #define PINSEL_BITPIN0 0
24 #define PINSEL_BITPIN1 2
25 #define PINSEL_BITPIN2 4
26 #define PINSEL_FIRST_ALT_FUNC 1
27 #define PINSEL_SECOND_ALT_FUNC 2
29 // Values of Bits 0-3 in PINSEL to activate UART0
30 #define UART0_PINSEL ((PINSEL_FIRST_ALT_FUNC<<PINSEL_BITPIN0)|(PINSEL_FIRST_ALT_FUNC<<PINSEL_BITPIN1))
32 #define UART0_PINMASK (0x0000000F) /* PINSEL0 Mask for UART0 */
34 // U0_LCR devisor latch bit
35 #define UART0_LCR_DLAB 7
37 /* baudrate divisor - use UART_BAUD macro
38 * mode - see typical modes (uart.h)
39 * fmode - see typical fmodes (uart.h)
40 * NOTE: uart0Init(UART_BAUD(9600), UART_8N1, UART_FIFO_8);
42 void uart0Init(uint16_t baud, uint8_t mode, uint8_t fmode)
46 // setup Pin Function Select Register (Pin Connect Block)
47 // make sure old values of Bits 0-4 are masked out and
48 // set them according to UART0-Pin-Selection
49 PINSEL0 = (PINSEL0 & ~UART0_PINMASK) | UART0_PINSEL;
51 U0IER = 0x00; // disable all interrupts
52 U0IIR = 0x00; // clear interrupt ID register
53 U0LSR = 0x00; // clear line status register
55 // set the baudrate - DLAB must be set to access DLL/DLM
56 U0LCR = (1<<UART0_LCR_DLAB); // set divisor latches (DLAB)
57 U0DLL = (uint8_t)baud; // set for baud low byte
58 U0DLM = (uint8_t)(baud >> 8); // set for baud high byte
60 // set the number of characters and other
61 // user specified operating parameters
62 // Databits, Parity, Stopbits - Settings in Line Control Register
63 U0LCR = (mode & ~(1<<UART0_LCR_DLAB)); // clear DLAB "on-the-fly"
64 // setup FIFO Control Register (fifo-enabled + xx trig)
70 int uart0Putch(int ch)
72 while (!(U0LSR & ULSR_THRE)) // wait for TX buffer to empty
73 continue; // also either WDOG() or swap()
75 U0THR = (uint8_t)ch; // put char to Transmit Holding Register
76 return (uint8_t)ch; // return char ("stdio-compatible"?)
79 const char *uart0Puts(const char *string)
83 while ((ch = *string)) {
84 if (uart0Putch(ch)<0) break;
91 int uart0TxEmpty(void)
93 return (U0LSR & (ULSR_THRE | ULSR_TEMT)) == (ULSR_THRE | ULSR_TEMT);
96 void uart0TxFlush(void)
98 U0FCR |= UFCR_TX_FIFO_RESET; // clear the TX fifo
102 /* Returns: character on success, -1 if no character is available */
105 if (U0LSR & ULSR_RDR) // check if character is available
106 return U0RBR; // return character
111 /* Returns: character on success, waits */
112 int uart0GetchW(void)
114 while ( !(U0LSR & ULSR_RDR) ); // wait for character
115 return U0RBR; // return character