4 * This file is part of BeRTOS.
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.
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.
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
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.
29 * Copyright 2010 Develer S.r.l. (http://www.develer.com/)
33 * \brief LM3S1968 UART interface driver.
35 * \author Andrea Righi <arighi@develer.com>
38 #include <cfg/macros.h> /* for BV() */
39 #include <drv/clock_lm3s.h> /* lm3s_busyWait() */
40 #include <drv/gpio_lm3s.h>
41 #include <drv/ser_p.h>
43 #include <drv/irq_cm3.h>
44 #include "cfg/cfg_ser.h"
47 /* From the high-level serial driver */
48 extern struct Serial *ser_handles[SER_CNT];
52 struct SerialHardware hw;
58 /* Forward declaration */
59 static struct CM3Serial UARTDesc[SER_CNT];
61 /* GPIO descriptor for UART pins */
66 /* GPIO base address register */
72 /* Table to retrieve GPIO pins configuration to work as UART pins */
73 static const struct gpio_uart_info gpio_uart[SER_CNT] =
77 .base = GPIO_PORTA_BASE,
78 .pins = BV(1) | BV(0),
79 .sysctl = SYSCTL_RCGC2_GPIOA,
83 .base = GPIO_PORTD_BASE,
84 .pins = BV(3) | BV(2),
85 .sysctl = SYSCTL_RCGC2_GPIOD,
89 .base = GPIO_PORTG_BASE,
90 .pins = BV(1) | BV(0),
91 .sysctl = SYSCTL_RCGC2_GPIOG,
95 /* Clear the flags register */
96 INLINE void lm3s_uartClear(uint32_t base)
98 HWREG(base + UART_O_FR) = 0;
101 void lm3s_uartSetBaudRate(uint32_t base, unsigned long baud)
106 if (baud * 16 > CPU_FREQ)
111 div = (CPU_FREQ * 8 / baud + 1) / 2;
113 lm3s_uartDisable(base);
115 HWREG(base + UART_O_CTL) |= UART_CTL_HSE;
117 HWREG(base + UART_O_CTL) &= ~UART_CTL_HSE;
118 /* Set the baud rate */
119 HWREG(base + UART_O_IBRD) = div / 64;
120 HWREG(base + UART_O_FBRD) = div % 64;
121 lm3s_uartClear(base);
122 lm3s_uartEnable(base);
125 void lm3s_uartSetParity(uint32_t base, int parity)
127 /* Set 8-bit word, one stop bit by default */
128 uint32_t config = UART_LCRH_WLEN_8;
132 case SER_PARITY_NONE:
135 config |= UART_LCRH_PEN;
137 case SER_PARITY_EVEN:
138 config |= UART_LCRH_EPS | UART_LCRH_PEN;
144 lm3s_uartDisable(base);
145 HWREG(base + UART_O_LCRH) = config;
146 lm3s_uartClear(base);
147 lm3s_uartEnable(base);
150 void lm3s_uartInit(int port)
152 uint32_t reg_clock, base;
154 ASSERT(port >= 0 && port < SER_CNT);
156 base = UARTDesc[port].base;
157 reg_clock = 1 << port;
159 /* Enable the peripheral clock */
160 SYSCTL_RCGC1_R |= reg_clock;
161 SYSCTL_RCGC2_R |= gpio_uart[port].sysctl;
164 /* Configure GPIO pins to work as UART pins */
165 lm3s_gpioPinConfig(gpio_uart[port].base, gpio_uart[port].pins,
166 GPIO_DIR_MODE_HW, GPIO_STRENGTH_2MA, GPIO_PIN_TYPE_STD);
168 /* Set serial param: 115.200 bps, no parity */
169 lm3s_uartSetBaudRate(base, 115200);
170 lm3s_uartSetParity(base, SER_PARITY_NONE);
173 static bool tx_sending(struct SerialHardware *_hw)
175 struct CM3Serial *hw = (struct CM3Serial *)_hw;
179 static void uart_irq_rx(int port)
181 struct FIFOBuffer *rxfifo = &ser_handles[port]->rxfifo;
182 uint32_t base = UARTDesc[port].base;
185 while (lm3s_uartRxReady(base))
187 c = HWREG(base + UART_O_DR);
188 if (fifo_isfull(rxfifo))
189 ser_handles[port]->status |= SERRF_RXFIFOOVERRUN;
191 fifo_push(rxfifo, c);
195 static void uart_irq_tx(int port)
197 struct FIFOBuffer *txfifo = &ser_handles[port]->txfifo;
198 uint32_t base = UARTDesc[port].base;
200 while (lm3s_uartTxReady(base))
202 if (fifo_isempty(txfifo)) {
204 * Disable TX empty interrupts if there're no more
205 * characters to transmit.
207 HWREG(base + UART_O_IM) &= ~UART_IM_TXIM;
208 UARTDesc[port].sending = false;
211 HWREG(base + UART_O_DR) = fifo_pop(txfifo);
215 static void uart_common_irq_handler(int port)
217 uint32_t base = UARTDesc[port].base;
220 /* Read and clear the IRQ status */
221 status = HWREG(base + UART_O_RIS);
223 /* Process the IRQ */
224 if (status & (UART_RIS_RXRIS | UART_RIS_RTRIS))
226 if (status & UART_RIS_TXRIS)
231 lm3s_uartIRQEnable(int port, sysirq_handler_t handler)
233 uint32_t base = UARTDesc[port].base;
234 sysirq_t irq = UARTDesc[port].irq;
236 /* Register the IRQ handler */
237 sysirq_setHandler(irq, handler);
238 /* Enable RX interrupt in the UART interrupt mask register */
239 HWREG(base + UART_O_IM) |= UART_IM_RXIM | UART_IM_RTIM;
242 static void lm3s_uartIRQDisable(int port)
244 uint32_t base = UARTDesc[port].base;
246 HWREG(base + UART_O_IM) &=
247 ~(UART_IM_TXIM | UART_IM_RXIM | UART_IM_RTIM);
250 /* UART class definition */
251 #define UART_PORT(port) \
252 /* UART TX and RX buffers */ \
253 static unsigned char \
254 uart ## port ## _txbuffer[CONFIG_UART ## port ## _TXBUFSIZE]; \
255 static unsigned char \
256 uart ## port ## _rxbuffer[CONFIG_UART ## port ## _RXBUFSIZE]; \
258 /* UART interrupt handler */ \
259 static DECLARE_ISR(uart ## port ## _irq_handler) \
261 uart_common_irq_handler(port); \
264 /* UART public methods */ \
266 uart ## port ## _txStart(struct SerialHardware *_hw) \
268 struct FIFOBuffer *txfifo = &ser_handles[port]->txfifo; \
269 struct CM3Serial *hw = (struct CM3Serial *)_hw; \
273 lm3s_uartPutChar(UART ## port ## _BASE, fifo_pop(txfifo)); \
274 if (!fifo_isempty(txfifo)) \
276 HWREG(UART ## port ## _BASE + UART_O_IM) |= \
278 hw->sending = true; \
283 uart ## port ## _setbaudrate(UNUSED_ARG(struct SerialHardware *, hw), \
284 unsigned long baud) \
286 lm3s_uartSetBaudRate(UART ## port ## _BASE, baud); \
290 uart ## port ## _setparity(UNUSED_ARG(struct SerialHardware *, hw), \
293 lm3s_uartSetParity(UART ## port ## _BASE, parity); \
297 uart ## port ## _cleanup(struct SerialHardware *_hw) \
299 struct CM3Serial *hw = (struct CM3Serial *)_hw; \
301 hw->sending = false; \
302 lm3s_uartIRQDisable(port); \
303 lm3s_uartClear(UART ## port ## _BASE); \
304 lm3s_uartDisable(UART ## port ## _BASE); \
308 uart ## port ## _init(UNUSED_ARG(struct SerialHardware *, hw), \
309 UNUSED_ARG(struct Serial *, ser)) \
311 lm3s_uartInit(port); \
312 lm3s_uartEnable(UART ## port ## _BASE); \
313 lm3s_uartIRQEnable(port, uart ## port ## _irq_handler); \
316 /* UART operations */ \
317 static const struct SerialHardwareVT UART ## port ## _VT = \
319 .init = uart ## port ## _init, \
320 .cleanup = uart ## port ## _cleanup, \
321 .setBaudrate = uart ## port ## _setbaudrate, \
322 .setParity = uart ## port ## _setparity, \
323 .txStart = uart ## port ## _txStart, \
324 .txSending = tx_sending, \
327 /* UART port instances */
332 static struct CM3Serial UARTDesc[SER_CNT] =
337 .txbuffer = uart0_txbuffer,
338 .rxbuffer = uart0_rxbuffer,
339 .txbuffer_size = sizeof(uart0_txbuffer),
340 .rxbuffer_size = sizeof(uart0_rxbuffer),
349 .txbuffer = uart1_txbuffer,
350 .rxbuffer = uart1_rxbuffer,
351 .txbuffer_size = sizeof(uart1_txbuffer),
352 .rxbuffer_size = sizeof(uart1_rxbuffer),
361 .txbuffer = uart2_txbuffer,
362 .rxbuffer = uart2_rxbuffer,
363 .txbuffer_size = sizeof(uart2_txbuffer),
364 .rxbuffer_size = sizeof(uart2_rxbuffer),
372 struct SerialHardware *ser_hw_getdesc(int port)
374 ASSERT(port >= 0 && port < SER_CNT);
375 return &UARTDesc[port].hw;