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/stm32/gpio.h> // general purpose input output library
28 #include <libopencm3/stm32/rcc.h> // real-time control clock library
29 #include <libopencm3/stm32/usart.h> // USART utilities
30 
31 /* own libraries */
32 #include "global.h" // board definitions
33 #include "print.h" // printing utilities
34 #include "menu.h" // menu definitions
35 #include "busvoodoo_global.h" // BusVoodoo definitions
36 #include "busvoodoo_oled.h" // OLED utilities
37 #include "busvoodoo_uart_generic.h" // generic UART mode
38 #include "busvoodoo_rs232.h" // own definitions
39 
43 #define BUSVOODOO_RS232_USART 2
47 static const struct busvoodoo_uart_generic_specific_t busvoodoo_uart_generic_rs232 = {
48  .usart = USART(BUSVOODOO_RS232_USART),
49  .usart_rcc = RCC_USART(BUSVOODOO_RS232_USART),
50  .usart_rst = RST_USART(BUSVOODOO_RS232_USART),
51  .multidrive = false,
55  .tx_pre = NULL,
56  .tx_post = NULL,
60  .rx_pre = NULL,
61  .rx_post = NULL,
62  .hwflowctl = true,
69 };
70 
76 static bool busvoodoo_rs232_setup(char** prefix, const char* line)
77 {
78  bool complete = false; // is the setup complete
79  if (NULL==line) { // first call
80  busvoodoo_uart_generic_configure(&busvoodoo_uart_generic_rs232); // provide the RS-232 specific information
81  }
82  complete = busvoodoo_uart_generic_setup(prefix, line); // configure underlying generic UART
83  if (complete) { // generic configuration finished
84  gpio_clear(GPIO(BUSVOODOO_RS232_EN_PORT), GPIO(BUSVOODOO_RS232_EN_PIN)); // set low to enable RS-232 transceiver receiver
85  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)
86  gpio_set(GPIO(BUSVOODOO_RS232_SHDN_PORT), GPIO(BUSVOODOO_RS232_SHDN_PIN)); // set high to enable RS-232 transceiver transmitter
87  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)
88  busvoodoo_led_blue_off(); // disable blue LED because there is no activity
89  *prefix = "RS-232"; // display mode
90  busvoodoo_oled_text_left(*prefix); // set mode title on OLED display
91  const char* pinout_io[10] = {"GND", "5V", "3V3", "LV", NULL, NULL, NULL, NULL, NULL, NULL}; // RS-232 mode I/O pinout
92  for (uint8_t i=0; i<LENGTH(pinout_io) && i<LENGTH(busvoodoo_global_pinout_io); i++) {
93  busvoodoo_global_pinout_io[i] = pinout_io[i]; // set pin names
94  }
95  const char* pinout_rscan[5] = {"HV", "Rx", "Tx", "CTS", "RTS"}; // RS-232 mode RS/CAN pinout
96  for (uint8_t i=0; i<LENGTH(pinout_rscan) && i<LENGTH(busvoodoo_global_pinout_rscan); i++) {
97  busvoodoo_global_pinout_rscan[i] = pinout_rscan[i]; // set pin names
98  }
99  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
100  busvoodoo_oled_text_pinout(pinout, false); // set pinout on display
101  busvoodoo_oled_update(); // update display to show text and pinout
102  }
103  return complete;
104 }
105 
108 static void busvoodoo_rs232_exit(void)
109 {
110  busvoodoo_uart_generic_exit(); // exiting the underlying generic UART does everything we need
111  gpio_set(GPIO(BUSVOODOO_RS232_EN_PORT), GPIO(BUSVOODOO_RS232_EN_PIN)); // set high to disable RS-232 transceiver receiver
112  gpio_clear(GPIO(BUSVOODOO_RS232_SHDN_PORT), GPIO(BUSVOODOO_RS232_SHDN_PIN)); // set low to disable RS-232 transceiver transmitter
113 }
114 
116  .name = "rs232",
117  .description = "Recommended Standard 232",
118  .full_only = true,
119  .setup = &busvoodoo_rs232_setup,
121  .commands_nb = busvoodoo_uart_generic_commands_nb,
122  .exit = &busvoodoo_rs232_exit,
123 };
#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
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 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
const char * busvoodoo_global_pinout_rscan[5]
RS/CAN connector pinout.
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
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 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
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 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)
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)