9485b1f8a7197215f564b6a97ca5517cac92a21b
[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 <drv/clock_lm3s.h> /* lm3s_busyWait() */
44 #include <io/lm3s.h>
45
46 /* Serial hardware numbers */
47 enum
48 {
49         SER_UART0,
50         SER_UART1,
51         SER_UART2,
52
53         SER_CNT //< Number of serial ports
54 };
55
56 /* Software errors */
57 #define SERRF_RXFIFOOVERRUN  BV(0) //< Rx FIFO buffer overrun
58 #define SERRF_RXTIMEOUT      BV(1) //< Receive timeout
59 #define SERRF_TXTIMEOUT      BV(2) //< Transmit timeout
60
61 /*
62  * Hardware errors.
63  */
64 #define SERRF_RXSROVERRUN    0 //< Input overrun
65 #define SERRF_FRAMEERROR     0 //< Stop bit missing
66 #define SERRF_PARITYERROR    0 //< Parity error
67 #define SERRF_NOISEERROR     0 //< Noise error
68
69 /* Serial error/status flags */
70 typedef uint32_t serstatus_t;
71
72 INLINE void lm3s_uartDisable(uint32_t base)
73 {
74         /* Disable the hardware FIFO */
75         HWREG(base + UART_O_LCRH) &= ~UART_LCRH_FEN;
76
77         /* Disable the UART */
78         HWREG(base + UART_O_CTL) &=
79                 ~(UART_CTL_UARTEN | UART_CTL_TXE | UART_CTL_RXE);
80         lm3s_busyWait(512);
81 }
82
83 INLINE void lm3s_uartEnable(uint32_t base)
84 {
85         /* Enable the hardware FIFO */
86         HWREG(base + UART_O_LCRH) |= UART_LCRH_FEN;
87
88         /* Enable RX, TX, and the UART */
89         HWREG(base + UART_O_CTL) |=
90                         UART_CTL_UARTEN | UART_CTL_TXE | UART_CTL_RXE;
91         lm3s_busyWait(512);
92 }
93
94 INLINE bool lm3s_uartTxDone(uint32_t base)
95 {
96         return HWREG(base + UART_O_FR) & UART_FR_TXFE ? true : false;
97 }
98
99 INLINE bool lm3s_uartTxReady(uint32_t base)
100 {
101         return HWREG(base + UART_O_FR) & UART_FR_TXFF ? false : true;
102 }
103
104 INLINE bool lm3s_uartRxReady(uint32_t base)
105 {
106         return HWREG(base + UART_O_FR) & UART_FR_RXFE ? false : true;
107 }
108
109 INLINE bool lm3s_uartReady(uint32_t base)
110 {
111         return HWREG(base + UART_O_FR) & UART_FR_BUSY ? false : true;
112 }
113
114 INLINE int lm3s_uartPutCharNonBlocking(uint32_t base, unsigned char c)
115 {
116         if (!lm3s_uartTxReady(base))
117                 return EOF;
118         HWREG(base + UART_O_DR) = c;
119         return c;
120 }
121
122 INLINE int lm3s_uartPutChar(uint32_t base, unsigned char c)
123 {
124         while (!lm3s_uartTxReady(base))
125                 cpu_relax();
126         HWREG(base + UART_O_DR) = c;
127         return c;
128 }
129
130 INLINE int lm3s_uartGetCharNonBlocking(uint32_t base)
131 {
132         if (!lm3s_uartRxReady(base))
133                 return EOF;
134         return HWREG(base + UART_O_DR);
135 }
136
137 INLINE int lm3s_uartGetChar(uint32_t base)
138 {
139         while (!lm3s_uartRxReady(base))
140                 cpu_relax();
141         return HWREG(base + UART_O_DR);
142 }
143
144 void lm3s_uartSetBaudRate(uint32_t base, unsigned long baud);
145 void lm3s_uartSetParity(uint32_t base, int parity);
146 void lm3s_uartInit(int port);
147
148 #endif /* SER_LM3S_H */