lm3s1968: rename the function __delay() to lm3s_busyWait().
[bertos.git] / bertos / cpu / cortex-m3 / drv / kdebug_lm3s.c
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 LM3S debug support (implementation).
34  *
35  * \author Andrea Righi <arighi@develer.com>
36  */
37
38 #include <cfg/cfg_debug.h>
39 #include <cfg/macros.h> /* for BV() */
40 #include <drv/clock_lm3s.h> /* lm3s_busyWait() */
41 #include <drv/gpio_lm3s.h>
42 #include <io/lm3s.h>
43 #include "kdebug_lm3s.h"
44
45 INLINE void uart_disable(size_t base)
46 {
47         /* Disable the FIFO */
48         HWREG(base + UART_O_LCRH) &= ~UART_LCRH_FEN;
49         /* Disable the UART */
50         HWREG(base + UART_O_CTL) &=
51                 ~(UART_CTL_UARTEN | UART_CTL_TXE | UART_CTL_RXE);
52 }
53
54 INLINE void uart_enable(size_t base)
55 {
56         /* Enable the FIFO */
57         HWREG(base + UART_O_LCRH) |= UART_LCRH_FEN;
58         /* Enable RX, TX, and the UART */
59         HWREG(base + UART_O_CTL) |=
60                         UART_CTL_UARTEN | UART_CTL_TXE | UART_CTL_RXE;
61 }
62
63 INLINE void uart_config(size_t base, uint32_t baud, reg32_t config)
64 {
65         unsigned long div;
66         bool hi_speed;
67
68         if (baud * 16 > CPU_FREQ)
69         {
70                 hi_speed = true;
71                 baud /= 2;
72         }
73         div = (CPU_FREQ * 8 / baud + 1) / 2;
74
75         uart_disable(base);
76
77         if (hi_speed)
78                 HWREG(base + UART_O_CTL) |= UART_CTL_HSE;
79         else
80                 HWREG(base + UART_O_CTL) &= ~UART_CTL_HSE;
81
82         /* Set the baud rate */
83         HWREG(base + UART_O_IBRD) = div / 64;
84         HWREG(base + UART_O_FBRD) = div % 64;
85
86         /* Set parity, data length, and number of stop bits. */
87         HWREG(base + UART_O_LCRH) = config;
88
89         /* Clear the flags register */
90         HWREG(base + UART_O_FR) = 0;
91
92         uart_enable(base);
93 }
94
95 INLINE bool uart_putchar(size_t base, unsigned char ch)
96 {
97         if (!(HWREG(base + UART_O_FR) & UART_FR_TXFF))
98         {
99                 HWREG(base + UART_O_DR) = ch;
100                 return true;
101         }
102         return false;
103 }
104
105 #if CONFIG_KDEBUG_PORT == KDEBUG_PORT_DBGU
106 #define KDBG_WAIT_READY()     while (HWREG(UART0_BASE + UART_O_FR) & UART_FR_BUSY) {}
107 #define KDBG_WAIT_TXDONE()    while (!(HWREG(UART0_BASE + UART_O_FR) & UART_FR_TXFE)) {}
108
109 #define KDBG_WRITE_CHAR(c)    do { HWREG(UART0_BASE + UART_O_DR) = c; } while(0)
110
111 /* Debug unit is used only for debug purposes so does not generate interrupts. */
112 #define KDBG_MASK_IRQ(old)    do { (void)old; } while(0)
113
114 /* Debug unit is used only for debug purposes so does not generate interrupts. */
115 #define KDBG_RESTORE_IRQ(old) do { (void)old; } while(0)
116
117 typedef uint32_t kdbg_irqsave_t;
118
119 #else
120 #error CONFIG_KDEBUG_PORT should be KDEBUG_PORT_DBGU
121 #endif
122
123 INLINE void kdbg_hw_init(void)
124 {
125         /* Enable the peripheral clock */
126         SYSCTL_RCGC1_R |= SYSCTL_RCGC1_UART0;
127         SYSCTL_RCGC2_R |= SYSCTL_RCGC2_GPIOA;
128         lm3s_busyWait(512);
129
130         /* Set GPIO A0 and A1 as UART pins */
131         lm3s_gpio_pin_config(GPIO_PORTA_BASE, BV(0) | BV(1),
132                         GPIO_DIR_MODE_HW, GPIO_STRENGTH_2MA, GPIO_PIN_TYPE_STD);
133         /* 115.200, 8-bit word, no parity, one stop bit */
134         uart_config(UART0_BASE, CONFIG_KDEBUG_BAUDRATE, UART_LCRH_WLEN_8);
135 }