BV UART: add timer for baud rate detection

This commit is contained in:
King Kévin 2018-07-13 18:31:39 +02:00
parent ae3621faba
commit 847e7d189c
1 changed files with 22 additions and 0 deletions

View File

@ -24,6 +24,7 @@
#include <string.h> // string utilities
/* STM32 (including CM3) libraries */
#include <libopencm3/cm3/nvic.h> // interrupt definitions
#include <libopencm3/stm32/gpio.h> // general purpose input output library
#include <libopencm3/stm32/rcc.h> // real-time control clock library
#include <libopencm3/stm32/usart.h> // USART utilities
@ -44,6 +45,9 @@
#define BUSVOODOO_UART_USART 3 /**< USART peripheral */
/** @} */
#define BUSVOODOO_UART_RX_TIMER 2 /**< timer ID to capture RX edges */
#define BUSVOODOO_UART_RX_CHANNEL 4 /**< channel ID used as input capture to capture RX edges */
/** UART specific methods that will be called by the generic methods */
static const struct busvoodoo_uart_generic_specific_t busvoodoo_uart_generic_uart = {
.usart = USART(BUSVOODOO_UART_USART),
@ -67,6 +71,18 @@ static const struct busvoodoo_uart_generic_specific_t busvoodoo_uart_generic_uar
.cts_port = USART_CTS_PORT(BUSVOODOO_UART_USART),
.cts_pin = USART_CTS_PIN(BUSVOODOO_UART_USART),
.cts_rcc = RCC_USART_PORT(BUSVOODOO_UART_USART),
.timer = TIM(BUSVOODOO_UART_RX_TIMER),
.timer_rcc = RCC_TIM(BUSVOODOO_UART_RX_TIMER),
.timer_port = TIM_CH_PORT(BUSVOODOO_UART_RX_TIMER, BUSVOODOO_UART_RX_CHANNEL),
.timer_port_rcc = RCC_TIM_CH(BUSVOODOO_UART_RX_TIMER, BUSVOODOO_UART_RX_CHANNEL),
.timer_pin = TIM_CH_PIN(BUSVOODOO_UART_RX_TIMER, BUSVOODOO_UART_RX_CHANNEL),
.timer_ic = TIM_IC(BUSVOODOO_UART_RX_CHANNEL),
.timer_ic_in_ti = TIM_IC_IN_TI(BUSVOODOO_UART_RX_CHANNEL),
.timer_sr_ccif = TIM_SR_CCIF(BUSVOODOO_UART_RX_CHANNEL),
.timer_sr_ccof = TIM_SR_CCOF(BUSVOODOO_UART_RX_CHANNEL),
.timer_ccr = &TIM_CCR(BUSVOODOO_UART_RX_TIMER, BUSVOODOO_UART_RX_CHANNEL),
.timer_dier_ccie = TIM_DIER_CCIE(BUSVOODOO_UART_RX_CHANNEL),
.timer_nvic_irq = NVIC_TIM_IRQ(BUSVOODOO_UART_RX_TIMER),
};
/** setup UART mode
@ -83,6 +99,7 @@ static bool busvoodoo_uart_setup(char** prefix, const char* line)
complete = busvoodoo_uart_generic_setup(prefix, line); // configure underlying generic UART
if (complete) { // generic configuration finished
busvoodoo_led_blue_off(); // disable blue LED because there is no activity
gpio_primary_remap(AFIO_MAPR_SWJ_CFG_JTAG_OFF_SW_ON, AFIO_MAPR_TIM2_REMAP_PARTIAL_REMAP2); // remap timer 2 channel 4 to RX to be able to measure the edge timing
*prefix = "UART"; // display mode
busvoodoo_oled_text_left(*prefix); // set mode title on OLED display
const char* pinout_io[10] = {"GND", "5V", "3V3", "LV", "Rx", "Tx", "RTS", "CTS", NULL, NULL}; // UART mode pinout
@ -106,6 +123,11 @@ static bool busvoodoo_uart_setup(char** prefix, const char* line)
static void busvoodoo_uart_exit(void)
{
busvoodoo_uart_generic_exit(); // exiting the underlying generic UART does everything we need
// disable timer 2 remapping set during configuration
uint32_t remap = AFIO_MAPR; // get the remap setting
remap &= ~AFIO_MAPR_SWJ_MASK; // mask the SWJ setting since they are read only
remap &= ~AFIO_MAPR_TIM2_REMAP_FULL_REMAP; // clear the timer 2 remap
gpio_primary_remap(AFIO_MAPR_SWJ_CFG_JTAG_OFF_SW_ON, remap); // disable timer 2 remapping set during configuration
}
const struct busvoodoo_mode_t busvoodoo_uart_mode = {