STM32: fix GPIO settings in kdebug module.
[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 <cfg/macros.h> /* for BV() */
39 #include <cfg/cfg_debug.h>
40 #include <drv/gpio_stm32.h>
41 #include <drv/clock_stm32.h> /* RCC */
42 #include <io/stm32.h>
43 #include "kdebug_stm32.h"
44
45 #define CR1_RUN_SET               ((uint16_t)0x2000)  /* USART Enable MASK */
46 #define CR1_RUN_RESET             ((uint16_t)0xDFFF)  /* USART Disable MASK */
47 #define CR1_RWU_SET               ((uint16_t)0x0002)  /* USART mute mode Enable MASK */
48 #define CR1_RWU_RESET             ((uint16_t)0xFFFD)  /* USART mute mode Enable MASK */
49 #define CR1_SBK_SET               ((uint16_t)0x0001)  /* USART Break Character send MASK */
50 #define CR1_CLEAR_MASK            ((uint16_t)0xE9F3)  /* USART CR1 MASK */
51
52 #define CR2_MASK                  ((uint16_t)0xFFF0)  /* USART address MASK */
53 #define CR2_LINE_SET              ((uint16_t)0x4000)  /* USART LIN Enable MASK */
54 #define CR2_LINE_RESET            ((uint16_t)0xBFFF)  /* USART LIN Disable MASK */
55 #define CR2_CLEAR_MASK            ((uint16_t)0xC0FF)  /* USART CR2 MASK */
56
57 #define CR3_SCEN_SET              ((uint16_t)0x0020)  /* USART SC Enable MASK */
58 #define CR3_SCEN_RESET            ((uint16_t)0xFFDF)  /* USART SC Disable MASK */
59 #define CR3_NACK_SET              ((uint16_t)0x0010)  /* USART SC NACK Enable MASK */
60 #define CR3_NACK_RESET            ((uint16_t)0xFFEF)  /* USART SC NACK Disable MASK */
61 #define CR3_HDSEL_SET             ((uint16_t)0x0008)  /* USART Half-Duplex Enable MASK */
62 #define CR3_HDSEL_RESET           ((uint16_t)0xFFF7)  /* USART Half-Duplex Disable MASK */
63 #define CR3_IRLP_MASK             ((uint16_t)0xFFFB)  /* USART IrDA LowPower mode MASK */
64 #define CR3_LBDL_MASK             ((uint16_t)0xFFDF)  /* USART LIN Break detection MASK */
65 #define CR3_WAKE_MASK             ((uint16_t)0xF7FF)  /* USART WakeUp Method MASK */
66 #define CR3_IREN_SET              ((uint16_t)0x0002)  /* USART IrDA Enable MASK */
67 #define CR3_IREN_RESET            ((uint16_t)0xFFFD)  /* USART IrDA Disable MASK */
68 #define CR3_CLEAR_MASK            ((uint16_t)0xFCFF)  /* USART CR3 MASK */
69
70 #define GTPR_LSB_MASK             ((uint16_t)0x00FF)  /* Guard Time Register LSB MASK */
71 #define GTPR_MSB_MASK             ((uint16_t)0xFF00)  /* Guard Time Register MSB MASK */
72
73 #define USART_IT_MASK             ((uint16_t)0x001F)  /* USART Interrupt MASK */
74
75 /* USART flags */
76 #define USART_FLAG_CTS            ((uint16_t)0x0200)
77 #define USART_FLAG_LBD            ((uint16_t)0x0100)
78 #define USART_FLAG_TXE            ((uint16_t)0x0080)
79 #define USART_FLAG_TC             ((uint16_t)0x0040)
80 #define USART_FLAG_RXNE           ((uint16_t)0x0020)
81 #define USART_FLAG_IDLE           ((uint16_t)0x0010)
82 #define USART_FLAG_ORE            ((uint16_t)0x0008)
83 #define USART_FLAG_NE             ((uint16_t)0x0004)
84 #define USART_FLAG_FE             ((uint16_t)0x0002)
85 #define USART_FLAG_PE             ((uint16_t)0x0001)
86
87 /* USART registers */
88 struct stm32_usart
89 {
90         reg16_t SR;
91         uint16_t _RESERVED0;
92         reg16_t DR;
93         uint16_t _RESERVED1;
94         reg16_t BRR;
95         uint16_t _RESERVED2;
96         reg16_t CR1;
97         uint16_t _RESERVED3;
98         reg16_t CR2;
99         uint16_t _RESERVED4;
100         reg16_t CR3;
101         uint16_t _RESERVED5;
102         reg16_t GTPR;
103         uint16_t _RESERVED6;
104 };
105
106 /* USART mode */
107 #define USART_MODE_RX             ((uint16_t)0x0004)
108 #define USART_MODE_TX             ((uint16_t)0x0008)
109
110 /* USART last bit */
111 #define USART_LASTBIT_DISABLE     ((uint16_t)0x0000)
112 #define USART_LASTBIT_ENABLE      ((uint16_t)0x0100)
113
114 #if CONFIG_KDEBUG_PORT == KDEBUG_PORT_DBGU
115
116 #if CONFIG_KDEBUG_PORT == 0
117         #define UART_BASE ((struct stm32_usart *)USART1_BASE)
118 #elif CONFIG_KDEBUG_PORT == 1
119         #define UART_BASE ((struct stm32_usart *)USART2_BASE)
120 #elif CONFIG_KDEBUG_PORT == 2
121         #define UART_BASE ((struct stm32_usart *)USART3_BASE)
122 #else
123         #error "UART port not supported in this board"
124 #endif
125
126 #define KDBG_WAIT_READY()       while (!(UART_BASE->SR & USART_FLAG_TXE))
127 #define KDBG_WAIT_TXDONE()      while (!(UART_BASE->SR & USART_FLAG_TC))
128
129 #define KDBG_WRITE_CHAR(c)      do { UART_BASE->DR = (c) & 0x1ff; } while(0)
130
131 /* Debug unit is used only for debug purposes so does not generate interrupts. */
132 #define KDBG_MASK_IRQ(old)      do { (void)old; } while(0)
133
134 /* Debug unit is used only for debug purposes so does not generate interrupts. */
135 #define KDBG_RESTORE_IRQ(old)   do { (void)old; } while(0)
136
137 typedef uint32_t kdbg_irqsave_t;
138
139 #else
140 #error CONFIG_KDEBUG_PORT should be KDEBUG_PORT_DBGU
141 #endif
142
143 #define GPIO_USART1_TX_PIN      (1 << 9)
144 #define GPIO_USART1_RX_PIN      (1 << 10)
145
146 #define GPIO_USART2_TX_PIN      (1 << 2)
147 #define GPIO_USART2_RX_PIN      (1 << 3)
148
149 #define GPIO_USART3_TX_PIN      (1 << 13)
150 #define GPIO_USART3_RX_PIN      (1 << 14)
151
152 INLINE uint16_t evaluate_brr(void)
153 {
154         uint32_t freq, reg, div, frac;
155
156         /* NOTE: PCLK1 has been configured as CPU_FREQ / 2 */
157         freq = (CONFIG_KDEBUG_PORT == 0) ? CPU_FREQ : CPU_FREQ / 2;
158         div = (0x19 * freq) / (0x04 * CONFIG_KDEBUG_BAUDRATE);
159         reg = (div / 0x64) << 0x04;
160         frac = div - (0x64 * (reg >> 0x04));
161         reg |= ((frac * 0x10 + 0x32) / 0x64) & 0x0f;
162
163         return (uint16_t)reg;
164 }
165
166 /* Initialize UART debug port */
167 INLINE void kdbg_hw_init(void)
168 {
169         /* Enable clocking on AFIO */
170         RCC->APB2ENR |= RCC_APB2_AFIO;
171         /* Configure USART pins */
172 #if CONFIG_KDEBUG_PORT == 0
173         RCC->APB2ENR |= RCC_APB2_GPIOA;
174         RCC->APB2ENR |= RCC_APB2_USART1;
175         stm32_gpioPinConfig((struct stm32_gpio *)GPIOA_BASE, GPIO_USART1_TX_PIN,
176                                 GPIO_MODE_AF_PP, GPIO_SPEED_50MHZ);
177 #elif CONFIG_KDEBUG_PORT == 1
178         RCC->APB2ENR |= RCC_APB2_GPIOA;
179         RCC->APB1ENR |= RCC_APB1_USART2;
180         stm32_gpioPinConfig((struct stm32_gpio *)GPIOA_BASE, GPIO_USART2_TX_PIN,
181                                 GPIO_MODE_AF_PP, GPIO_SPEED_50MHZ);
182 #elif  CONFIG_KDEBUG_PORT == 2
183         RCC->APB2ENR |= RCC_APB2_GPIOB;
184         RCC->APB2ENR |= RCC_APB1_USART3;
185         stm32_gpioPinConfig((struct stm32_gpio *)GPIOB_BASE, GPIO_USART3_TX_PIN,
186                                 GPIO_MODE_AF_PP, GPIO_SPEED_50MHZ);
187 #else
188         #error "UART port not supported in this board"
189 #endif
190         /* Enable the USART by writing the UE bit */
191         UART_BASE->CR1 |= CR1_RUN_SET;
192         /* Configure the desired baud rate */
193         UART_BASE->BRR = evaluate_brr();
194         /* Set the Transmitter Enable bit in CR1 */
195         UART_BASE->CR1 |= USART_MODE_TX;
196 }