568920ecc713d77926413c187a880693e0dae2d1
[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 <drv/ser_lm3s.h>
43 #include "kdebug_lm3s.h"
44
45 #if CONFIG_KDEBUG_PORT == KDEBUG_PORT_DBGU
46
47 #if CONFIG_KDEBUG_PORT == 0
48         #define UART_BASE UART0_BASE
49         #define UART_GPIO_BASE GPIO_PORTA_BASE
50         #define UART_PINS (BV(1) | BV(0))
51         #define UART_REG_SYSCTL SYSCTL_RCGC2_GPIOA
52 #elif CONFIG_KDEBUG_PORT == 1
53         #define UART_BASE UART1_BASE
54         #define UART_GPIO_BASE GPIO_PORTD_BASE
55         #define UART_PINS (BV(3) | BV(2))
56         #define UART_REG_SYSCTL SYSCTL_RCGC2_GPIOD
57 #elif CONFIG_KDEBUG_PORT == 2
58         #define UART_BASE UART2_BASE
59         #define UART_GPIO_BASE GPIO_PORTG_BASE
60         #define UART_PINS (BV(1) | BV(0))
61         #define UART_REG_SYSCTL SYSCTL_RCGC2_GPIOG
62 #else
63         #error "UART port not supported in this board"
64 #endif
65
66 #define KDBG_WAIT_READY()     while (!lm3s_uartReady(UART_BASE)) {}
67 #define KDBG_WAIT_TXDONE()    while (!lm3s_uartTxDone(UART_BASE)) {}
68
69 #define KDBG_WRITE_CHAR(c)    do { lm3s_uartPutCharNonBlocking(UART_BASE, c); } while(0)
70
71 /* Debug unit is used only for debug purposes so does not generate interrupts. */
72 #define KDBG_MASK_IRQ(old)    do { (void)old; } while(0)
73
74 /* Debug unit is used only for debug purposes so does not generate interrupts. */
75 #define KDBG_RESTORE_IRQ(old) do { (void)old; } while(0)
76
77 typedef uint32_t kdbg_irqsave_t;
78
79 #else
80 #error CONFIG_KDEBUG_PORT should be KDEBUG_PORT_DBGU
81 #endif
82
83 INLINE void uart_hw_config(void)
84 {
85         unsigned long div, baud = CONFIG_KDEBUG_BAUDRATE;
86         bool hi_speed = false;
87
88         if (baud * 16 > CPU_FREQ)
89         {
90                 hi_speed = true;
91                 baud /= 2;
92         }
93         div = (CPU_FREQ * 8 / baud + 1) / 2;
94
95         lm3s_uartDisable(UART_BASE);
96         if (hi_speed)
97                 HWREG(UART_BASE + UART_O_CTL) |= UART_CTL_HSE;
98         else
99                 HWREG(UART_BASE + UART_O_CTL) &= ~UART_CTL_HSE;
100         /* Set the baud rate */
101         HWREG(UART_BASE + UART_O_IBRD) = div / 64;
102         HWREG(UART_BASE + UART_O_FBRD) = div % 64;
103         /* Set word lenght and parity */
104         HWREG(UART_BASE + UART_O_LCRH) = UART_LCRH_WLEN_8;
105         lm3s_uartClear(UART_BASE);
106         lm3s_uartEnable(UART_BASE);
107 }
108
109 INLINE void kdbg_hw_init(void)
110 {
111         uint32_t reg_clock = 1 << CONFIG_KDEBUG_PORT;
112
113         /* Enable the peripheral clock */
114         SYSCTL_RCGC1_R |= reg_clock;
115         SYSCTL_RCGC2_R |= UART_REG_SYSCTL;
116         lm3s_busyWait(512);
117
118         /* Configure GPIO pins to work as UART pins */
119         lm3s_gpioPinConfig(UART_GPIO_BASE, UART_PINS,
120                         GPIO_DIR_MODE_HW, GPIO_STRENGTH_2MA, GPIO_PIN_TYPE_STD);
121
122         /* Low-level UART configuration */
123         uart_hw_config();
124 }