CuVoodoo STM32F1 firmware template
busvoodoo_rs232.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 utilities
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 "busvoodoo_global.h" // BusVoodoo definitions
37 #include "busvoodoo_oled.h" // OLED utilities
38 #include "busvoodoo_uart_generic.h" // generic UART mode
39 #include "busvoodoo_rs232.h" // own definitions
40 
44 #define BUSVOODOO_RS232_USART 2
47 #define BUSVOODOO_RS232_RX_TIMER 2
48 #define BUSVOODOO_RS232_RX_CHANNEL 4
51 static const struct busvoodoo_uart_generic_specific_t busvoodoo_uart_generic_rs232 = {
52  .usart = USART(BUSVOODOO_RS232_USART),
53  .usart_rcc = RCC_USART(BUSVOODOO_RS232_USART),
54  .usart_rst = RST_USART(BUSVOODOO_RS232_USART),
55  .multidrive = false,
59  .tx_pre = NULL,
60  .tx_post = NULL,
64  .rx_pre = NULL,
65  .rx_post = NULL,
66  .hwflowctl = true,
73  .timer = TIM(BUSVOODOO_RS232_RX_TIMER),
74  .timer_rcc = RCC_TIM(BUSVOODOO_RS232_RX_TIMER),
79  .timer_ic_in_ti = TIM_IC_IN_TI(BUSVOODOO_RS232_RX_CHANNEL),
80  .timer_sr_ccif = TIM_SR_CCIF(BUSVOODOO_RS232_RX_CHANNEL),
81  .timer_sr_ccof = TIM_SR_CCOF(BUSVOODOO_RS232_RX_CHANNEL),
83  .timer_dier_ccie = TIM_DIER_CCIE(BUSVOODOO_RS232_RX_CHANNEL),
84  .timer_nvic_irq = NVIC_TIM_IRQ(BUSVOODOO_RS232_RX_TIMER),
85 };
86 
92 static bool busvoodoo_rs232_setup(char** prefix, const char* line)
93 {
94  bool complete = false; // is the setup complete
95  if (NULL==line) { // first call
96  busvoodoo_uart_generic_configure(&busvoodoo_uart_generic_rs232); // provide the RS-232 specific information
97  }
98  complete = busvoodoo_uart_generic_setup(prefix, line); // configure underlying generic UART
99  if (complete) { // generic configuration finished
100  gpio_clear(GPIO(BUSVOODOO_RS232_EN_PORT), GPIO(BUSVOODOO_RS232_EN_PIN)); // set low to enable RS-232 transceiver receiver
101  gpio_set_mode(GPIO(BUSVOODOO_RS232_EN_PORT), GPIO_MODE_OUTPUT_10_MHZ, GPIO_CNF_OUTPUT_PUSHPULL, GPIO(BUSVOODOO_RS232_EN_PIN)); // setup RS-232 enable GPIO as output to enable the receiver (pulled high to disable by default)
102  gpio_set(GPIO(BUSVOODOO_RS232_SHDN_PORT), GPIO(BUSVOODOO_RS232_SHDN_PIN)); // set high to enable RS-232 transceiver transmitter
103  gpio_set_mode(GPIO(BUSVOODOO_RS232_SHDN_PORT), GPIO_MODE_OUTPUT_10_MHZ, GPIO_CNF_OUTPUT_PUSHPULL, GPIO(BUSVOODOO_RS232_SHDN_PIN)); // setup RS-232 shutdwon GPIO as output to enable the transmitter (pulled low to disable by default)
104  busvoodoo_led_blue_off(); // disable blue LED because there is no activity
105  *prefix = "RS-232"; // display mode
106  busvoodoo_oled_text_left(*prefix); // set mode title on OLED display
107  const char* pinout_io[10] = {"GND", "5V", "3V3", "LV", NULL, NULL, NULL, NULL, NULL, NULL}; // RS-232 mode I/O pinout
108  for (uint8_t i=0; i<LENGTH(pinout_io) && i<LENGTH(busvoodoo_global_pinout_io); i++) {
109  busvoodoo_global_pinout_io[i] = pinout_io[i]; // set pin names
110  }
111  const char* pinout_rscan[5] = {"HV", "Rx", "Tx", "CTS", "RTS"}; // RS-232 mode RS/CAN pinout
112  for (uint8_t i=0; i<LENGTH(pinout_rscan) && i<LENGTH(busvoodoo_global_pinout_rscan); i++) {
113  busvoodoo_global_pinout_rscan[i] = pinout_rscan[i]; // set pin names
114  }
115  const char* pinout[10] = {pinout_rscan[0], pinout_io[0], pinout_rscan[1], pinout_io[2], pinout_rscan[2], pinout_io[4], pinout_rscan[3], pinout_io[6], pinout_rscan[4], pinout_io[8]}; // pinout to display
116  busvoodoo_oled_text_pinout(pinout, false); // set pinout on display
117  busvoodoo_oled_update(); // update display to show text and pinout
118  }
119  return complete;
120 }
121 
124 static void busvoodoo_rs232_exit(void)
125 {
126  busvoodoo_uart_generic_exit(); // exiting the underlying generic UART does everything we need
127  gpio_set(GPIO(BUSVOODOO_RS232_EN_PORT), GPIO(BUSVOODOO_RS232_EN_PIN)); // set high to disable RS-232 transceiver receiver
128  gpio_clear(GPIO(BUSVOODOO_RS232_SHDN_PORT), GPIO(BUSVOODOO_RS232_SHDN_PIN)); // set low to disable RS-232 transceiver transmitter
129 }
130 
132  .name = "rs232",
133  .description = "Recommended Standard 232",
134  .full_only = true,
135  .setup = &busvoodoo_rs232_setup,
137  .commands_nb = busvoodoo_uart_generic_commands_nb,
138  .exit = &busvoodoo_rs232_exit,
139 };
#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)
static void busvoodoo_rs232_exit(void)
exit RS-232 mode
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 BUSVOODOO_RS232_EN_PORT
RS-232 pin to enable receiver (active low, pulled up)
#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 GPIO(x)
get GPIO based on GPIO identifier
Definition: global.h:103
static bool busvoodoo_rs232_setup(char **prefix, const char *line)
setup RS-232 mode
#define TIM_CCR(x, y)
get TIM_CCRy register based on TIMx_CHy identifier
Definition: global.h:154
#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
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 RST_USART(x)
get RST for USART based on USART identifier
Definition: global.h:188
#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
#define BUSVOODOO_RS232_RX_TIMER
timer ID to capture RX edges
const char * busvoodoo_global_pinout_io[10]
I/O connector pinout.
static const struct busvoodoo_uart_generic_specific_t busvoodoo_uart_generic_rs232
RS-232 specific methods that will be called by the generic methods.
#define BUSVOODOO_RS232_EN_PIN
RS-232 pin to enable receiver (active low, pulled up)
#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 BUSVOODOO_RS232_USART
USART peripheral.
#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
const struct busvoodoo_mode_t busvoodoo_rs232_mode
RS-232 mode interface definition.
#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
#define LENGTH(x)
get the length of an array
Definition: global.h:26
BusVoodoo RS-232 mode (API)
#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)
BusVoodoo mode interface.
#define USART_RTS_PORT(x)
get port for USART RTS pin based on USART identifier
Definition: global.h:198
#define BUSVOODOO_RS232_SHDN_PORT
RS-232 pin to enable transmitter (active high, pulled low)
#define BUSVOODOO_RS232_SHDN_PIN
RS-232 pin to enable transmitter (active high, pulled low)
#define BUSVOODOO_RS232_RX_CHANNEL
channel ID used as input capture to capture RX edges