lm3s1968: generic UART driver.
[bertos.git] / bertos / cpu / cortex-m3 / drv / ser_lm3s.h
1 /**
2  * \file
3  * <!--
4  * This file is part of BeRTOS.
5  *
6  * Bertos is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; either version 2 of the License, or
9  * (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program; if not, write to the Free Software
18  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
19  *
20  * As a special exception, you may use this file as part of a free software
21  * library without restriction.  Specifically, if other files instantiate
22  * templates or use macros or inline functions from this file, or you compile
23  * this file and link it with other files to produce an executable, this
24  * file does not by itself cause the resulting executable to be covered by
25  * the GNU General Public License.  This exception does not however
26  * invalidate any other reasons why the executable file might be covered by
27  * the GNU General Public License.
28  *
29  * Copyright 2010 Develer S.r.l. (http://www.develer.com/)
30  *
31  * -->
32  *
33  * \brief LM3S1968 UART interface driver.
34  *
35  * \author Andrea Righi <arighi@develer.com>
36  */
37
38 #ifndef SER_LM3S_H
39 #define SER_LM3S_H
40
41 #include <cfg/cfg_debug.h>
42 #include <cpu/power.h> /* cpu_relax() */
43 #include <io/lm3s.h>
44
45 /* Serial hardware numbers */
46 enum
47 {
48         SER_UART0,
49         SER_UART1,
50         SER_UART2,
51
52         SER_CNT //< Number of serial ports
53 };
54
55 /* Software errors */
56 #define SERRF_RXFIFOOVERRUN  BV(0) //< Rx FIFO buffer overrun
57 #define SERRF_RXTIMEOUT      BV(1) //< Receive timeout
58 #define SERRF_TXTIMEOUT      BV(2) //< Transmit timeout
59
60 /*
61  * Hardware errors.
62  */
63 #define SERRF_RXSROVERRUN    0 //< Input overrun
64 #define SERRF_FRAMEERROR     0 //< Stop bit missing
65 #define SERRF_PARITYERROR    0 //< Parity error
66 #define SERRF_NOISEERROR     0 //< Noise error
67
68 /* Serial error/status flags */
69 typedef uint32_t serstatus_t;
70
71 INLINE void lm3s_uartDisable(uint32_t base)
72 {
73         /* Disable the hardware FIFO */
74         HWREG(base + UART_O_LCRH) &= ~UART_LCRH_FEN;
75
76         /* Disable the UART */
77         HWREG(base + UART_O_CTL) &=
78                 ~(UART_CTL_UARTEN | UART_CTL_TXE | UART_CTL_RXE);
79 }
80
81 INLINE void lm3s_uartEnable(uint32_t base)
82 {
83         /* Enable the hardware FIFO */
84         HWREG(base + UART_O_LCRH) |= UART_LCRH_FEN;
85
86         /* Enable RX, TX, and the UART */
87         HWREG(base + UART_O_CTL) |=
88                         UART_CTL_UARTEN | UART_CTL_TXE | UART_CTL_RXE;
89 }
90
91 INLINE bool lm3s_uartTxDone(uint32_t base)
92 {
93         return HWREG(base + UART_O_FR) & UART_FR_TXFE ? true : false;
94 }
95
96 INLINE bool lm3s_uartTxReady(uint32_t base)
97 {
98         return HWREG(base + UART_O_FR) & UART_FR_TXFF ? false : true;
99 }
100
101 INLINE bool lm3s_uartRxReady(uint32_t base)
102 {
103         return HWREG(base + UART_O_FR) & UART_FR_RXFE ? false : true;
104 }
105
106 INLINE bool lm3s_uartReady(uint32_t base)
107 {
108         return HWREG(base + UART_O_FR) & UART_FR_BUSY ? false : true;
109 }
110
111 INLINE int lm3s_uartPutCharNonBlocking(uint32_t base, unsigned char c)
112 {
113         if (!lm3s_uartTxReady(base))
114                 return EOF;
115         HWREG(base + UART_O_DR) = c;
116         return c;
117 }
118
119 INLINE int lm3s_uartPutChar(uint32_t base, unsigned char c)
120 {
121         while (!lm3s_uartTxReady(base))
122                 cpu_relax();
123         HWREG(base + UART_O_DR) = c;
124         return c;
125 }
126
127 INLINE int lm3s_uartGetCharNonBlocking(uint32_t base)
128 {
129         if (!lm3s_uartRxReady(base))
130                 return EOF;
131         return HWREG(base + UART_O_DR);
132 }
133
134 INLINE int lm3s_uartGetChar(uint32_t base)
135 {
136         while (!lm3s_uartRxReady(base))
137                 cpu_relax();
138         return HWREG(base + UART_O_DR);
139 }
140
141 void lm3s_uartSetBaudRate(uint32_t base, unsigned long baud);
142 void lm3s_uartSetParity(uint32_t base, int parity);
143 void lm3s_uartInit(int port);
144
145 #endif /* SER_LM3S_H */