BeRTOS
kdebug_stm32.c
Go to the documentation of this file.
00001 
00038 #include "kdebug_stm32.h"
00039 
00040 #include <cfg/cfg_debug.h>
00041 
00042 #include <drv/gpio_stm32.h>
00043 #include <drv/clock_stm32.h> /* RCC */
00044 
00045 #include <io/stm32.h>
00046 
00047 
00048 
00049 #if CONFIG_KDEBUG_PORT == 0
00050     #define UART_BASE ((struct stm32_usart *)USART1_BASE)
00051 #elif CONFIG_KDEBUG_PORT == 1
00052     #define UART_BASE ((struct stm32_usart *)USART2_BASE)
00053 #elif CONFIG_KDEBUG_PORT == 2
00054     #define UART_BASE ((struct stm32_usart *)USART3_BASE)
00055 #else
00056     #error "UART port not supported in this board"
00057 #endif
00058 
00059 #define KDBG_WAIT_READY()   while (!(UART_BASE->SR & USART_FLAG_TXE))
00060 #define KDBG_WAIT_TXDONE()  while (!(UART_BASE->SR & USART_FLAG_TC))
00061 
00062 #define KDBG_WRITE_CHAR(c)  do { UART_BASE->DR = (c) & 0x1ff; } while(0)
00063 
00064 /* Debug unit is used only for debug purposes so does not generate interrupts. */
00065 #define KDBG_MASK_IRQ(old)  do { (void)old; } while(0)
00066 
00067 /* Debug unit is used only for debug purposes so does not generate interrupts. */
00068 #define KDBG_RESTORE_IRQ(old)   do { (void)old; } while(0)
00069 
00070 typedef uint32_t kdbg_irqsave_t;
00071 
00072 
00073 /* Initialize UART debug port */
00074 INLINE void kdbg_hw_init(void)
00075 {
00076     /* Enable clocking on AFIO */
00077     RCC->APB2ENR |= RCC_APB2_AFIO;
00078     /* Configure USART pins */
00079 #if CONFIG_KDEBUG_PORT == 0
00080     RCC->APB2ENR |= RCC_APB2_GPIOA;
00081     RCC->APB2ENR |= RCC_APB2_USART1;
00082     stm32_gpioPinConfig((struct stm32_gpio *)GPIOA_BASE, GPIO_USART1_TX_PIN,
00083                 GPIO_MODE_AF_PP, GPIO_SPEED_50MHZ);
00084 #elif CONFIG_KDEBUG_PORT == 1
00085     RCC->APB2ENR |= RCC_APB2_GPIOA;
00086     RCC->APB1ENR |= RCC_APB1_USART2;
00087     stm32_gpioPinConfig((struct stm32_gpio *)GPIOA_BASE, GPIO_USART2_TX_PIN,
00088                 GPIO_MODE_AF_PP, GPIO_SPEED_50MHZ);
00089 #elif  CONFIG_KDEBUG_PORT == 2
00090     RCC->APB2ENR |= RCC_APB2_GPIOB;
00091     RCC->APB1ENR |= RCC_APB1_USART3;
00092     stm32_gpioPinConfig((struct stm32_gpio *)GPIOB_BASE, GPIO_USART3_TX_PIN,
00093                 GPIO_MODE_AF_PP, GPIO_SPEED_50MHZ);
00094 #else
00095     #error "UART port not supported in this board"
00096 #endif
00097     /* Enable the USART by writing the UE bit */
00098     UART_BASE->CR1 |= CR1_RUN_SET;
00099     /* Configure the desired baud rate */
00100     UART_BASE->BRR = (uint16_t)evaluate_brr(UART_BASE, CPU_FREQ, CONFIG_KDEBUG_BAUDRATE);
00101     /* Set the Transmitter Enable bit in CR1 */
00102     UART_BASE->CR1 |= USART_MODE_TX;
00103 }