]> rtime.felk.cvut.cz Git - lincan.git/blob - embedded/board/arm/lpc17xx-common/libs/bspbase/uart.c
Contributed support for LCP17xx devices and PiKRON's LMC1 board.
[lincan.git] / embedded / board / arm / lpc17xx-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
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 */
24
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
29
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))
32 // Mask of Bits 4-7
33 #define UART0_PINMASK   ((3<<PINSEL_BIT_TXD0)|(3<<PINSEL_BIT_RXD0))
34
35 // U0_LCR devisor latch bit 
36 #define UART0_LCR_DLAB  7
37
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); 
42  */
43 void uart0Init(uint32_t baud, uint8_t mode, uint8_t fmode)
44 {
45   volatile int i;
46   uint32_t baud_div;
47
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; 
52
53   UART0->IER = 0x00;             // disable all interrupts
54   //UART0->IIR = 0x00;             // clear interrupt ID register
55   //UART0->LSR = 0x00;             // clear line status register
56
57   baud *= 16;
58   baud_div = (PCLK + baud / 4) / baud;
59
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
64
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) 
70   UART0->FCR = fmode;
71
72   for(i=0;i<65000;i++);
73 }
74
75 int uart0Putch(int ch)
76 {
77   while (!(UART0->LSR & UART_LSR_THRE)) // wait for TX buffer to empty
78     continue;                           // also either WDOG() or swap()
79
80   UART0->THR = (uint8_t)ch;  // put char to Transmit Holding Register
81   return (uint8_t)ch;      // return char ("stdio-compatible"?)
82 }
83
84 int uart0PutchNW(int ch)
85 {
86   if (!(UART0->LSR & UART_LSR_THRE)) // wait for TX buffer to empty
87     return -1;                           // also either WDOG() or swap()
88
89   UART0->THR = (uint8_t)ch;  // put char to Transmit Holding Register
90   return (uint8_t)ch;      // return char ("stdio-compatible"?)
91 }
92
93 const char *uart0Puts(const char *string)
94 {
95         char ch;
96         
97         while ((ch = *string)) {
98                 if (uart0Putch(ch)<0) break;
99                 string++;
100         }
101         
102         return string;
103 }
104
105 int uart0TxEmpty(void)
106 {
107   return (UART0->LSR & (UART_LSR_THRE | UART_LSR_TEMT)) == (UART_LSR_THRE | UART_LSR_TEMT);
108 }
109
110 void uart0TxFlush(void)
111 {
112   UART0->FCR |= UART_FCR_CLEAR_XMIT;         // clear the TX fifo
113 }
114
115
116 /* Returns: character on success, -1 if no character is available */
117 int uart0Getch(void)
118 {
119   if (UART0->LSR & UART_LSR_DR)              // check if character is available
120     return UART0->RBR;                       // return character
121
122   return -1;
123 }
124
125 /* Returns: character on success, waits */
126 int uart0GetchW(void)
127 {
128         while ( !(UART0->LSR & UART_LSR_DR) ); // wait for character 
129         return UART0->RBR;                // return character
130 }
131