Refactor to use new protocol module and sipo.
[bertos.git] / bertos / cpu / cortex-m3 / drv / kdebug_stm32.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 STM32 debug support (implementation).
34  *
35  * \author Andrea Righi <arighi@develer.com>
36  */
37
38 #include "kdebug_stm32.h"
39
40 #include <cfg/cfg_debug.h>
41
42 #include <drv/gpio_stm32.h>
43 #include <drv/clock_stm32.h> /* RCC */
44
45 #include <io/stm32.h>
46
47
48
49 #if CONFIG_KDEBUG_PORT == 0
50         #define UART_BASE ((struct stm32_usart *)USART1_BASE)
51 #elif CONFIG_KDEBUG_PORT == 1
52         #define UART_BASE ((struct stm32_usart *)USART2_BASE)
53 #elif CONFIG_KDEBUG_PORT == 2
54         #define UART_BASE ((struct stm32_usart *)USART3_BASE)
55 #else
56         #error "UART port not supported in this board"
57 #endif
58
59 #define KDBG_WAIT_READY()       while (!(UART_BASE->SR & USART_FLAG_TXE))
60 #define KDBG_WAIT_TXDONE()      while (!(UART_BASE->SR & USART_FLAG_TC))
61
62 #define KDBG_WRITE_CHAR(c)      do { UART_BASE->DR = (c) & 0x1ff; } while(0)
63
64 /* Debug unit is used only for debug purposes so does not generate interrupts. */
65 #define KDBG_MASK_IRQ(old)      do { (void)old; } while(0)
66
67 /* Debug unit is used only for debug purposes so does not generate interrupts. */
68 #define KDBG_RESTORE_IRQ(old)   do { (void)old; } while(0)
69
70 typedef uint32_t kdbg_irqsave_t;
71
72
73 /* Initialize UART debug port */
74 INLINE void kdbg_hw_init(void)
75 {
76         /* Enable clocking on AFIO */
77         RCC->APB2ENR |= RCC_APB2_AFIO;
78         /* Configure USART pins */
79 #if CONFIG_KDEBUG_PORT == 0
80         RCC->APB2ENR |= RCC_APB2_GPIOA;
81         RCC->APB2ENR |= RCC_APB2_USART1;
82         stm32_gpioPinConfig((struct stm32_gpio *)GPIOA_BASE, GPIO_USART1_TX_PIN,
83                                 GPIO_MODE_AF_PP, GPIO_SPEED_50MHZ);
84 #elif CONFIG_KDEBUG_PORT == 1
85         RCC->APB2ENR |= RCC_APB2_GPIOA;
86         RCC->APB1ENR |= RCC_APB1_USART2;
87         stm32_gpioPinConfig((struct stm32_gpio *)GPIOA_BASE, GPIO_USART2_TX_PIN,
88                                 GPIO_MODE_AF_PP, GPIO_SPEED_50MHZ);
89 #elif  CONFIG_KDEBUG_PORT == 2
90         RCC->APB2ENR |= RCC_APB2_GPIOB;
91         RCC->APB1ENR |= RCC_APB1_USART3;
92         stm32_gpioPinConfig((struct stm32_gpio *)GPIOB_BASE, GPIO_USART3_TX_PIN,
93                                 GPIO_MODE_AF_PP, GPIO_SPEED_50MHZ);
94 #else
95         #error "UART port not supported in this board"
96 #endif
97         /* Enable the USART by writing the UE bit */
98         UART_BASE->CR1 |= CR1_RUN_SET;
99         /* Configure the desired baud rate */
100         UART_BASE->BRR = (uint16_t)evaluate_brr(UART_BASE, CPU_FREQ, CONFIG_KDEBUG_BAUDRATE);
101         /* Set the Transmitter Enable bit in CR1 */
102         UART_BASE->CR1 |= USART_MODE_TX;
103 }