]> rtime.felk.cvut.cz Git - lincan.git/blob - embedded/board/arm/ul_usb1/libs/bspbase/uart.c
Update of system-less architecture and board support code to actual uLAN.sf.net version.
[lincan.git] / embedded / board / arm / ul_usb1 / 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 "uart.h"
18
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 */
22    
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
28
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))
31 // Mask of Bits 0-4
32 #define UART0_PINMASK      (0x0000000F)    /* PINSEL0 Mask for UART0 */
33
34 // U0_LCR devisor latch bit 
35 #define UART0_LCR_DLAB  7
36
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); 
41  */
42 void uart0Init(uint16_t baud, uint8_t mode, uint8_t fmode)
43 {
44   volatile int i;
45
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; 
50
51   U0IER = 0x00;             // disable all interrupts
52   U0IIR = 0x00;             // clear interrupt ID register
53   U0LSR = 0x00;             // clear line status register
54
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
59   
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) 
65   U0FCR = fmode;
66
67   for(i=0;i<65000;i++);
68 }
69
70 int uart0Putch(int ch)
71 {
72   while (!(U0LSR & ULSR_THRE))          // wait for TX buffer to empty
73     continue;                           // also either WDOG() or swap()
74
75   U0THR = (uint8_t)ch;  // put char to Transmit Holding Register
76   return (uint8_t)ch;      // return char ("stdio-compatible"?)
77 }
78
79 const char *uart0Puts(const char *string)
80 {
81         char ch;
82         
83         while ((ch = *string)) {
84                 if (uart0Putch(ch)<0) break;
85                 string++;
86         }
87         
88         return string;
89 }
90
91 int uart0TxEmpty(void)
92 {
93   return (U0LSR & (ULSR_THRE | ULSR_TEMT)) == (ULSR_THRE | ULSR_TEMT);
94 }
95
96 void uart0TxFlush(void)
97 {
98   U0FCR |= UFCR_TX_FIFO_RESET;          // clear the TX fifo
99 }
100
101
102 /* Returns: character on success, -1 if no character is available */
103 int uart0Getch(void)
104 {
105   if (U0LSR & ULSR_RDR)                 // check if character is available
106     return U0RBR;                       // return character
107
108   return -1;
109 }
110
111 /* Returns: character on success, waits */
112 int uart0GetchW(void)
113 {
114         while ( !(U0LSR & ULSR_RDR) ); // wait for character 
115         return U0RBR;                // return character
116 }
117