]> rtime.felk.cvut.cz Git - sysless.git/blob - board/arm/lpc178x-common/libs/bspbase/uart.c
LPC178x: Update board support for new header files and pin configuration.
[sysless.git] / board / arm / lpc178x-common / libs / bspbase / uart.c
1 /******************************************************************************
2  *
3  * $RCSfile$
4  * $Revision$
5  *
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.
11  *
12  * reduced to see what has to be done for minimum UART-support by mthomas
13  *****************************************************************************/
14
15 // #warning "this is a reduced version of the R O Software code"
16
17 #include <hal_machperiph.h>
18 #include "uart.h"
19 #include "serial_reg.h"
20 #include "hal_gpio.h"
21
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 */
25
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
30
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))
33 // Mask of Bits 4-7
34 #define UART0_PINMASK   ((3<<PINSEL_BIT_TXD0)|(3<<PINSEL_BIT_RXD0))
35
36 // U0_LCR devisor latch bit 
37 #define UART0_LCR_DLAB  7
38
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); 
43  */
44 void uart0Init(uint32_t baud, uint8_t mode, uint8_t fmode)
45 {
46   volatile int i;
47   uint32_t baud_div;
48
49   hal_pin_conf(RXD0_PIN);
50   hal_pin_conf(TXD0_PIN);
51
52   LPC_UART0->IER = 0x00;             // disable all interrupts
53   //UART0->IIR = 0x00;             // clear interrupt ID register
54   //UART0->LSR = 0x00;             // clear line status register
55
56   baud *= 16;
57   baud_div = (PCLK + baud / 4) / baud;
58
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
63
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;
70
71   for(i=0;i<65000;i++);
72 }
73
74 int uart0Putch(int ch)
75 {
76   while (!(LPC_UART0->LSR & UART_LSR_THRE)) // wait for TX buffer to empty
77     continue;                           // also either WDOG() or swap()
78
79   LPC_UART0->THR = (uint8_t)ch;  // put char to Transmit Holding Register
80   return (uint8_t)ch;      // return char ("stdio-compatible"?)
81 }
82
83 int uart0PutchNW(int ch)
84 {
85   if (!(LPC_UART0->LSR & UART_LSR_THRE)) // wait for TX buffer to empty
86     return -1;                           // also either WDOG() or swap()
87
88   LPC_UART0->THR = (uint8_t)ch;  // put char to Transmit Holding Register
89   return (uint8_t)ch;      // return char ("stdio-compatible"?)
90 }
91
92 const char *uart0Puts(const char *string)
93 {
94         char ch;
95         
96         while ((ch = *string)) {
97                 if (uart0Putch(ch)<0) break;
98                 string++;
99         }
100         
101         return string;
102 }
103
104 int uart0TxEmpty(void)
105 {
106   return (LPC_UART0->LSR & (UART_LSR_THRE | UART_LSR_TEMT)) == (UART_LSR_THRE | UART_LSR_TEMT);
107 }
108
109 void uart0TxFlush(void)
110 {
111   LPC_UART0->FCR |= UART_FCR_CLEAR_XMIT;         // clear the TX fifo
112 }
113
114
115 /* Returns: character on success, -1 if no character is available */
116 int uart0Getch(void)
117 {
118   if (LPC_UART0->LSR & UART_LSR_DR)              // check if character is available
119     return LPC_UART0->RBR;                       // return character
120
121   return -1;
122 }
123
124 /* Returns: character on success, waits */
125 int uart0GetchW(void)
126 {
127         while ( !(LPC_UART0->LSR & UART_LSR_DR) ); // wait for character 
128         return LPC_UART0->RBR;                // return character
129 }
130