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"
17 #include <hal_machperiph.h>
19 #include "serial_reg.h"
22 /* on LPC17xx: UART0 TX-Pin=P0.2, RX-Pin=P0.3
23 PINSEL0 has to be set to "UART-Function" = Function "01"
24 for Pin 0.2 and 0.3 */
26 #define PINSEL_BIT_TXD0 (2*2)
27 #define PINSEL_BIT_RXD0 (3*2)
28 #define PINSEL_FIRST_ALT_FUNC 1
29 #define PINSEL_SECOND_ALT_FUNC 2
31 // Values of Bits 4-7 in PINSEL to activate UART0
32 #define UART0_PINSEL ((PINSEL_FIRST_ALT_FUNC<<PINSEL_BIT_TXD0)|(PINSEL_FIRST_ALT_FUNC<<PINSEL_BIT_RXD0))
34 #define UART0_PINMASK ((3<<PINSEL_BIT_TXD0)|(3<<PINSEL_BIT_RXD0))
36 // U0_LCR devisor latch bit
37 #define UART0_LCR_DLAB 7
39 /* baudrate divisor - use UART_BAUD macro
40 * mode - see typical modes (uart.h)
41 * fmode - see typical fmodes (uart.h)
42 * NOTE: uart0Init(UART_BAUD(9600), UART_8N1, UART_FIFO_8);
44 void uart0Init(uint32_t baud, uint8_t mode, uint8_t fmode)
49 hal_pin_conf(RXD0_PIN);
50 hal_pin_conf(TXD0_PIN);
52 LPC_UART0->IER = 0x00; // disable all interrupts
53 //UART0->IIR = 0x00; // clear interrupt ID register
54 //UART0->LSR = 0x00; // clear line status register
57 baud_div = (PCLK + baud / 4) / baud;
59 // set the baudrate - DLAB must be set to access DLL/DLM
60 LPC_UART0->LCR = (1<<UART0_LCR_DLAB); // set divisor latches (DLAB)
61 LPC_UART0->DLL = (uint8_t)baud_div; // set for baud low byte
62 LPC_UART0->DLM = (uint8_t)(baud_div >> 8); // set for baud high byte
64 // set the number of characters and other
65 // user specified operating parameters
66 // Databits, Parity, Stopbits - Settings in Line Control Register
67 LPC_UART0->LCR = (mode & ~(1<<UART0_LCR_DLAB)); // clear DLAB "on-the-fly"
68 // setup FIFO Control Register (fifo-enabled + xx trig)
69 LPC_UART0->FCR = fmode;
74 int uart0Putch(int ch)
76 while (!(LPC_UART0->LSR & UART_LSR_THRE)) // wait for TX buffer to empty
77 continue; // also either WDOG() or swap()
79 LPC_UART0->THR = (uint8_t)ch; // put char to Transmit Holding Register
80 return (uint8_t)ch; // return char ("stdio-compatible"?)
83 int uart0PutchNW(int ch)
85 if (!(LPC_UART0->LSR & UART_LSR_THRE)) // wait for TX buffer to empty
86 return -1; // also either WDOG() or swap()
88 LPC_UART0->THR = (uint8_t)ch; // put char to Transmit Holding Register
89 return (uint8_t)ch; // return char ("stdio-compatible"?)
92 const char *uart0Puts(const char *string)
96 while ((ch = *string)) {
97 if (uart0Putch(ch)<0) break;
104 int uart0TxEmpty(void)
106 return (LPC_UART0->LSR & (UART_LSR_THRE | UART_LSR_TEMT)) == (UART_LSR_THRE | UART_LSR_TEMT);
109 void uart0TxFlush(void)
111 LPC_UART0->FCR |= UART_FCR_CLEAR_XMIT; // clear the TX fifo
115 /* Returns: character on success, -1 if no character is available */
118 if (LPC_UART0->LSR & UART_LSR_DR) // check if character is available
119 return LPC_UART0->RBR; // return character
124 /* Returns: character on success, waits */
125 int uart0GetchW(void)
127 while ( !(LPC_UART0->LSR & UART_LSR_DR) ); // wait for character
128 return LPC_UART0->RBR; // return character