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"
21 /* on LPC17xx: UART0 TX-Pin=P0.2, RX-Pin=P0.3
22 PINSEL0 has to be set to "UART-Function" = Function "01"
23 for Pin 0.2 and 0.3 */
25 #define PINSEL_BIT_TXD0 (2*2)
26 #define PINSEL_BIT_RXD0 (3*2)
27 #define PINSEL_FIRST_ALT_FUNC 1
28 #define PINSEL_SECOND_ALT_FUNC 2
30 // Values of Bits 4-7 in PINSEL to activate UART0
31 #define UART0_PINSEL ((PINSEL_FIRST_ALT_FUNC<<PINSEL_BIT_TXD0)|(PINSEL_FIRST_ALT_FUNC<<PINSEL_BIT_RXD0))
33 #define UART0_PINMASK ((3<<PINSEL_BIT_TXD0)|(3<<PINSEL_BIT_RXD0))
35 // U0_LCR devisor latch bit
36 #define UART0_LCR_DLAB 7
38 /* baudrate divisor - use UART_BAUD macro
39 * mode - see typical modes (uart.h)
40 * fmode - see typical fmodes (uart.h)
41 * NOTE: uart0Init(UART_BAUD(9600), UART_8N1, UART_FIFO_8);
43 void uart0Init(uint32_t baud, uint8_t mode, uint8_t fmode)
48 // setup Pin Function Select Register (Pin Connect Block)
49 // make sure old values of Bits 0-4 are masked out and
50 // set them according to UART0-Pin-Selection
51 PINCON->PINSEL0 = (PINCON->PINSEL0 & ~UART0_PINMASK) | UART0_PINSEL;
53 UART0->IER = 0x00; // disable all interrupts
54 //UART0->IIR = 0x00; // clear interrupt ID register
55 //UART0->LSR = 0x00; // clear line status register
58 baud_div = (PCLK + baud / 4) / baud;
60 // set the baudrate - DLAB must be set to access DLL/DLM
61 UART0->LCR = (1<<UART0_LCR_DLAB); // set divisor latches (DLAB)
62 UART0->DLL = (uint8_t)baud_div; // set for baud low byte
63 UART0->DLM = (uint8_t)(baud_div >> 8); // set for baud high byte
65 // set the number of characters and other
66 // user specified operating parameters
67 // Databits, Parity, Stopbits - Settings in Line Control Register
68 UART0->LCR = (mode & ~(1<<UART0_LCR_DLAB)); // clear DLAB "on-the-fly"
69 // setup FIFO Control Register (fifo-enabled + xx trig)
75 int uart0Putch(int ch)
77 while (!(UART0->LSR & UART_LSR_THRE)) // wait for TX buffer to empty
78 continue; // also either WDOG() or swap()
80 UART0->THR = (uint8_t)ch; // put char to Transmit Holding Register
81 return (uint8_t)ch; // return char ("stdio-compatible"?)
84 int uart0PutchNW(int ch)
86 if (!(UART0->LSR & UART_LSR_THRE)) // wait for TX buffer to empty
87 return -1; // also either WDOG() or swap()
89 UART0->THR = (uint8_t)ch; // put char to Transmit Holding Register
90 return (uint8_t)ch; // return char ("stdio-compatible"?)
93 const char *uart0Puts(const char *string)
97 while ((ch = *string)) {
98 if (uart0Putch(ch)<0) break;
105 int uart0TxEmpty(void)
107 return (UART0->LSR & (UART_LSR_THRE | UART_LSR_TEMT)) == (UART_LSR_THRE | UART_LSR_TEMT);
110 void uart0TxFlush(void)
112 UART0->FCR |= UART_FCR_CLEAR_XMIT; // clear the TX fifo
116 /* Returns: character on success, -1 if no character is available */
119 if (UART0->LSR & UART_LSR_DR) // check if character is available
120 return UART0->RBR; // return character
125 /* Returns: character on success, waits */
126 int uart0GetchW(void)
128 while ( !(UART0->LSR & UART_LSR_DR) ); // wait for character
129 return UART0->RBR; // return character