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