1 | /* |
---|
2 | * Uart driver for Lattice Mico32 (lm32) UART |
---|
3 | */ |
---|
4 | |
---|
5 | /* |
---|
6 | * COPYRIGHT (c) 1989-1999. |
---|
7 | * On-Line Applications Research Corporation (OAR). |
---|
8 | * |
---|
9 | * The license and distribution terms for this file may be |
---|
10 | * found in the file LICENSE in this distribution or at |
---|
11 | * http://www.rtems.org/license/LICENSE. |
---|
12 | * |
---|
13 | * Jukka Pietarinen <jukka.pietarinen@mrf.fi>, 2008, |
---|
14 | * Micro-Research Finland Oy |
---|
15 | */ |
---|
16 | |
---|
17 | #include "../include/system_conf.h" |
---|
18 | #include "uart.h" |
---|
19 | #include <bsp.h> |
---|
20 | #include <rtems/libio.h> |
---|
21 | |
---|
22 | static inline int uartread(unsigned int reg) |
---|
23 | { |
---|
24 | return *((int*)(UART_BASE_ADDRESS + reg)); |
---|
25 | } |
---|
26 | |
---|
27 | static inline void uartwrite(unsigned int reg, int value) |
---|
28 | { |
---|
29 | *((int*)(UART_BASE_ADDRESS + reg)) = value; |
---|
30 | } |
---|
31 | |
---|
32 | void BSP_uart_init(int baud) |
---|
33 | { |
---|
34 | /* Disable UART interrupts */ |
---|
35 | uartwrite(LM32_UART_IER, 0); |
---|
36 | |
---|
37 | /* Line control 8 bit, 1 stop, no parity */ |
---|
38 | uartwrite(LM32_UART_LCR, LM32_UART_LCR_8BIT); |
---|
39 | |
---|
40 | /* Modem control, DTR = 1, RTS = 1 */ |
---|
41 | uartwrite(LM32_UART_MCR, LM32_UART_MCR_DTR | LM32_UART_MCR_RTS); |
---|
42 | |
---|
43 | /* Set baud rate */ |
---|
44 | uartwrite(LM32_UART_DIV, CPU_FREQUENCY/baud); |
---|
45 | } |
---|
46 | |
---|
47 | void BSP_uart_polled_write(char ch) |
---|
48 | { |
---|
49 | /* Insert CR before LF */ |
---|
50 | if (ch == '\n') |
---|
51 | BSP_uart_polled_write('\r'); |
---|
52 | /* Wait until THR is empty. */ |
---|
53 | while (!(uartread(LM32_UART_LSR) & LM32_UART_LSR_THRE)); |
---|
54 | uartwrite(LM32_UART_RBR, ch); |
---|
55 | } |
---|
56 | |
---|
57 | int BSP_uart_polled_read( void ) |
---|
58 | { |
---|
59 | /* Wait until there is a byte in RBR */ |
---|
60 | while (!(uartread(LM32_UART_LSR) & LM32_UART_LSR_DR)); |
---|
61 | return (int) uartread(LM32_UART_RBR); |
---|
62 | } |
---|
63 | |
---|
64 | char BSP_uart_is_character_ready(char *ch) |
---|
65 | { |
---|
66 | if (uartread(LM32_UART_LSR) & LM32_UART_LSR_DR) |
---|
67 | { |
---|
68 | *ch = (char) uartread(LM32_UART_RBR); |
---|
69 | return true; |
---|
70 | } |
---|
71 | *ch = '0'; |
---|
72 | return false; |
---|
73 | } |
---|