CuVoodoo STM32F1 firmware template
busvoodoo_uart.c
Go to the documentation of this file.
1 /* This program is free software: you can redistribute it and/or modify
2  * it under the terms of the GNU General Public License as published by
3  * the Free Software Foundation, either version 3 of the License, or
4  * (at your option) any later version.
5  *
6  * This program is distributed in the hope that it will be useful,
7  * but WITHOUT ANY WARRANTY; without even the implied warranty of
8  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
9  * GNU General Public License for more details.
10  *
11  * You should have received a copy of the GNU General Public License
12  * along with this program. If not, see <http://www.gnu.org/licenses/>.
13  *
14  */
21 /* standard libraries */
22 #include <stdint.h> // standard integer types
23 #include <stdlib.h> // standard utilities
24 #include <string.h> // string utilities
25 
26 /* STM32 (including CM3) libraries */
27 #include <libopencm3/cm3/nvic.h> // interrupt definitions
28 #include <libopencm3/stm32/gpio.h> // general purpose input output library
29 #include <libopencm3/stm32/rcc.h> // real-time control clock library
30 #include <libopencm3/stm32/usart.h> // USART utilities
31 
32 /* own libraries */
33 #include "global.h" // board definitions
34 #include "print.h" // printing utilities
35 #include "menu.h" // menu definitions
36 #include "usart_enhanced.h" // utilities for USART enhancements
37 #include "busvoodoo_global.h" // BusVoodoo definitions
38 #include "busvoodoo_oled.h" // OLED utilities
39 #include "busvoodoo_uart_generic.h" // generic UART mode
40 #include "busvoodoo_uart.h" // own definitions
41 
45 #define BUSVOODOO_UART_USART 3
48 #define BUSVOODOO_UART_RX_TIMER 2
49 #define BUSVOODOO_UART_RX_CHANNEL 4
52 static const struct busvoodoo_uart_generic_specific_t busvoodoo_uart_generic_uart = {
53  .usart = USART(BUSVOODOO_UART_USART),
54  .usart_rcc = RCC_USART(BUSVOODOO_UART_USART),
55  .usart_rst = RST_USART(BUSVOODOO_UART_USART),
56  .multidrive = true,
60  .tx_pre = NULL,
61  .tx_post = NULL,
65  .rx_pre = NULL,
66  .rx_post = NULL,
67  .hwflowctl = true,
74  .timer = TIM(BUSVOODOO_UART_RX_TIMER),
75  .timer_rcc = RCC_TIM(BUSVOODOO_UART_RX_TIMER),
79  .timer_ic = TIM_IC(BUSVOODOO_UART_RX_CHANNEL),
80  .timer_ic_in_ti = TIM_IC_IN_TI(BUSVOODOO_UART_RX_CHANNEL),
81  .timer_sr_ccif = TIM_SR_CCIF(BUSVOODOO_UART_RX_CHANNEL),
82  .timer_sr_ccof = TIM_SR_CCOF(BUSVOODOO_UART_RX_CHANNEL),
84  .timer_dier_ccie = TIM_DIER_CCIE(BUSVOODOO_UART_RX_CHANNEL),
85  .timer_nvic_irq = NVIC_TIM_IRQ(BUSVOODOO_UART_RX_TIMER),
86 };
87 
93 static bool busvoodoo_uart_setup(char** prefix, const char* line)
94 {
95  bool complete = false; // is the setup complete
96  if (NULL==line) { // first call
97  busvoodoo_uart_generic_configure(&busvoodoo_uart_generic_uart); // provide the UART specific information
98  }
99  complete = busvoodoo_uart_generic_setup(prefix, line); // configure underlying generic UART
100  if (complete) { // generic configuration finished
101  busvoodoo_led_blue_off(); // disable blue LED because there is no activity
102  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
103  *prefix = "UART"; // display mode
104  busvoodoo_oled_text_left(*prefix); // set mode title on OLED display
105  const char* pinout_io[10] = {"GND", "5V", "3V3", "LV", "Rx", "Tx", "RTS", "CTS", NULL, NULL}; // UART mode pinout
106  for (uint8_t i=0; i<LENGTH(pinout_io) && i<LENGTH(busvoodoo_global_pinout_io); i++) {
107  busvoodoo_global_pinout_io[i] = pinout_io[i]; // set pin names
108  }
109  if (busvoodoo_full) {
110  const char* pinout_rscan[5] = {"HV", NULL, NULL, NULL, NULL}; // HiZ mode RS/CAN pinout
111  for (uint8_t i=0; i<LENGTH(pinout_rscan) && i<LENGTH(busvoodoo_global_pinout_rscan); i++) {
112  busvoodoo_global_pinout_rscan[i] = pinout_rscan[i]; // set pin names
113  }
114  }
115  busvoodoo_oled_text_pinout((const char**)pinout_io, true); // set pinout on display
116  busvoodoo_oled_update(); // update display to show text and pinout
117  }
118  return complete;
119 }
120 
123 static void busvoodoo_uart_exit(void)
124 {
125  busvoodoo_uart_generic_exit(); // exiting the underlying generic UART does everything we need
126  // disable timer 2 remapping set during configuration
127  uint32_t remap = AFIO_MAPR; // get the remap setting
128  remap &= ~AFIO_MAPR_SWJ_MASK; // mask the SWJ setting since they are read only
129  remap &= ~AFIO_MAPR_TIM2_REMAP_FULL_REMAP; // clear the timer 2 remap
130  gpio_primary_remap(AFIO_MAPR_SWJ_CFG_JTAG_OFF_SW_ON, remap); // disable timer 2 remapping set during configuration
131 }
132 
134  .name = "uart",
135  .description = "Universal Asynchronous Receiver-Transmitter",
136  .full_only = false,
137  .setup = &busvoodoo_uart_setup,
139  .commands_nb = busvoodoo_uart_generic_commands_nb,
140  .exit = &busvoodoo_uart_exit,
141 };
#define USART_TX_PORT(x)
get port for USART transmit pin based on USART identifier
Definition: global.h:194
#define RCC_USART(x)
get RCC for USART based on USART identifier
Definition: global.h:186
#define RCC_TIM_CH(x, y)
get RCC for port based on TIMx_CHy identifier
Definition: global.h:119
bool busvoodoo_uart_generic_setup(char **prefix, const char *line)
setup generic UART mode
BusVoodoo global definitions and methods (API)
void busvoodoo_oled_text_pinout(const char *pins[10], bool io_connector)
draw pin names on bottom (blue) part in display buffer
const char * name
name of the mode (i.e.
#define TIM_SR_CCOF(x)
get TIM_SR_CCxOF based on CHx identifier
Definition: global.h:150
#define NVIC_TIM_IRQ(x)
get NVIC IRQ for timer base on TIM identifier
Definition: global.h:111
#define RCC_USART_PORT(x)
get RCC for USART port based on USART identifier
Definition: global.h:210
void busvoodoo_oled_update(void)
update OLED display RAM with current display buffer
#define TIM_SR_CCIF(x)
get TIM_SR_CCxIF based on CHx identifier
Definition: global.h:148
const char * busvoodoo_global_pinout_rscan[5]
RS/CAN connector pinout.
#define TIM_DIER_CCIE(x)
get TIM_DIER_CCxIE based on CHx identifier
Definition: global.h:152
global definitions and methods (API)
#define TIM_CCR(x, y)
get TIM_CCRy register based on TIMx_CHy identifier
Definition: global.h:154
#define BUSVOODOO_UART_RX_TIMER
timer ID to capture RX edges
bool busvoodoo_full
is the BusVoodoo board fully populated (with HV voltage regulator, RS-232, RS-485, CAN transceiver on the back side)
#define RCC_TIM(x)
get RCC for timer based on TIM identifier
Definition: global.h:109
#define TIM_CH_PIN(x, y)
get pin based on TIMx_CHy identifier
Definition: global.h:117
static bool busvoodoo_uart_setup(char **prefix, const char *line)
setup UART mode
void busvoodoo_led_blue_off(void)
switch off blue LED
#define USART_RX_PIN(x)
get pin for USART receive pin based on USART identifier
Definition: global.h:204
#define USART_TX_PIN(x)
get pin for USART transmit pin based on USART identifier
Definition: global.h:202
#define TIM_IC(x)
get TIM_IC based on CHx identifier
Definition: global.h:144
#define USART_CTS_PORT(x)
get port for USART CTS pin based on USART identifier
Definition: global.h:200
#define BUSVOODOO_UART_USART
USART peripheral.
#define RST_USART(x)
get RST for USART based on USART identifier
Definition: global.h:188
const struct busvoodoo_mode_t busvoodoo_uart_mode
UART mode interface definition.
#define USART(x)
get USART based on USART identifier
Definition: global.h:184
void busvoodoo_uart_generic_exit(void)
exit genetic UART mode
#define TIM_IC_IN_TI(x)
get TIM_IC_IN_TI based on CHx identifier
Definition: global.h:146
const char * busvoodoo_global_pinout_io[10]
I/O connector pinout.
BusVoodoo UART mode (API)
#define busvoodoo_uart_generic_commands_nb
number of commands supported by the generic UART mode
#define TIM_CH_PORT(x, y)
get port based on TIMx_CHy identifier
Definition: global.h:115
#define USART_RX_PORT(x)
get port for USART receive pin based on USART identifier
Definition: global.h:196
bool busvoodoo_uart_generic_configure(const struct busvoodoo_uart_generic_specific_t *conf)
provide the generic USART with mode specific information
#define USART_RTS_PIN(x)
get pin for USART RTS pin based on USART identifier
Definition: global.h:206
#define USART_CTS_PIN(x)
get pin for USART CTS pin based on USART identifier
Definition: global.h:208
BusVoodoo generic UART mode (API)
void busvoodoo_oled_text_left(char *text)
draw mode text on top (yellow) left side in display buffer
const struct menu_command_t busvoodoo_uart_generic_commands[busvoodoo_uart_generic_commands_nb]
commands supported by the generic UART mode
static const struct busvoodoo_uart_generic_specific_t busvoodoo_uart_generic_uart
UART specific methods that will be called by the generic methods.
#define BUSVOODOO_UART_RX_CHANNEL
channel ID used as input capture to capture RX edges
#define LENGTH(x)
get the length of an array
Definition: global.h:26
#define TIM(x)
get TIM based on TIM identifier
Definition: global.h:107
library to show BusVoodoo mode information on SSD1306 OLED display: name, activity, pinout (API)
static void busvoodoo_uart_exit(void)
exit UART mode
BusVoodoo mode interface.
#define USART_RTS_PORT(x)
get port for USART RTS pin based on USART identifier
Definition: global.h:198
library for enhanced USART communication (API)