27 #include <libopencm3/stm32/gpio.h> 28 #include <libopencm3/stm32/rcc.h> 29 #include <libopencm3/stm32/usart.h> 42 #define BUSVOODOO_RS232_USART 2 46 static enum busvoodoo_rs232_setting_t { 47 BUSVOODOO_RS232_SETTING_NONE,
48 BUSVOODOO_RS232_SETTING_BAUDRATE,
49 BUSVOODOO_RS232_SETTING_DATABITS,
50 BUSVOODOO_RS232_SETTING_PARITY,
51 BUSVOODOO_RS232_SETTING_STOPBITS,
52 BUSVOODOO_RS232_SETTING_HWFLOWCTL,
53 BUSVOODOO_RS232_SETTING_DONE,
73 bool complete =
false;
78 case BUSVOODOO_RS232_SETTING_NONE:
83 case BUSVOODOO_RS232_SETTING_BAUDRATE:
84 if (NULL==line || 0==strlen(line)) {
87 uint32_t baudrate = atoi(line);
88 if (baudrate>0 && baudrate<=2000000) {
98 case BUSVOODOO_RS232_SETTING_DATABITS:
99 if (NULL==line || 0==strlen(line)) {
102 uint8_t databits = atoi(line);
103 if (8==databits || 9==databits) {
116 case BUSVOODOO_RS232_SETTING_PARITY:
117 if (NULL==line || 0==strlen(line)) {
119 }
else if (1==strlen(line)) {
123 }
else if (
'2'==line[0]) {
126 }
else if (
'3'==line[0]) {
140 case BUSVOODOO_RS232_SETTING_STOPBITS:
141 if (NULL==line || 0==strlen(line)) {
143 }
else if (1==strlen(line)) {
147 }
else if (
'2'==line[0]) {
150 }
else if (
'3'==line[0]) {
153 }
else if (
'4'==line[0]) {
159 printf(
"1) no flow control\n");
160 printf(
"2) RTS/CTS hardware flow control\n");
165 case BUSVOODOO_RS232_SETTING_HWFLOWCTL:
166 if (NULL==line || 0==strlen(line)) {
168 }
else if (1==strlen(line)) {
169 if (
'1'==line[0] ||
'2'==line[0]) {
175 rcc_periph_clock_enable(RCC_AFIO);
201 char* pinout_io[10] = {
"GND",
"5V",
"3V3",
"LV", NULL, NULL, NULL, NULL, NULL, NULL};
205 char* pinout_rscan[5] = {
"HV",
"Rx",
"Tx", NULL, NULL};
207 pinout_rscan[3] =
"RTS";
208 pinout_rscan[4] =
"CTS";
213 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]};
265 printf(
"write: '%c'/0x", value);
312 }
else if (error_framing) {
314 }
else if (error_parity) {
332 uint32_t length = strlen(action);
333 if (NULL==action || 0==length) {
337 if (1==length &&
'r'==action[0]) {
341 for (uint32_t i=0; i<repetition; i++) {
344 }
else if (1==length &&
'u'==action[0]) {
348 printf(
"wait for %u us\n", repetition);
350 }
else if (1==length &&
'm'==action[0]) {
354 printf(
"wait for %u ms\n", repetition);
356 }
else if (
'0'==action[0]) {
361 for (uint32_t i=0; i<repetition; i++) {
364 }
else if (
'x'==action[1] ||
'b'==action[1]) {
366 }
else if (action[1]>=
'0' && action[1]<=
'9') {
371 }
else if (
'x'==action[0] && length>1) {
372 for (uint32_t i=1; i<length; i++) {
373 if (!((action[i]>=
'0' && action[i]<=
'9') || (action[i]>=
'a' && action[i]<=
'f') || (action[i]>=
'A' && action[i]<=
'F'))) {
380 uint32_t value = strtol(&action[1], NULL, 16);
381 for (uint32_t i=0; i<repetition; i++) {
384 }
else if (
'b'==action[0] && length>1) {
385 for (uint32_t i=1; i<length; i++) {
386 if (action[i]<
'0' || action[i]>
'1') {
393 uint32_t value = strtol(&action[1], NULL, 2);
394 for (uint32_t i=0; i<repetition; i++) {
397 }
else if (action[0]>=
'1' && action[0]<=
'9') {
398 for (uint32_t i=1; i<length; i++) {
399 if (action[i]<
'0' || action[i]>
'9') {
406 uint32_t value = strtol(&action[0], NULL, 10);
407 for (uint32_t i=0; i<repetition; i++) {
410 }
else if (length>=2 && (
'"'==action[0] ||
'\''==action[0]) && (action[length-1]==action[0])) {
414 for (uint32_t r=0; r<repetition; r++) {
415 for (uint32_t i=1; i<length-1; i++) {
433 if (NULL==argument || 0==strlen(argument)) {
434 printf(
"available actions (separated by space or ,):\n");
435 printf(
"0\twrite decimal value\n");
436 printf(
"0x0\twrite hexadecimal value\n");
437 printf(
"0b0\twrite binary value\n");
438 printf(
"\"a\"/'a'\twrite ASCII characters\n");
439 printf(
"r\tread value\n");
440 printf(
"u/m\twait 1 us/ms\n");
441 printf(
":n\trepeat action n times\n");
446 char* copy = calloc(strlen(argument)+1,
sizeof(
char));
450 strncpy(copy, argument, strlen(argument)+1);
453 printf(
"malformed action(s)\n");
455 printf(
"press any key to exit\n");
469 if (NULL==argument || 0==strlen(argument)) {
472 printf(
"press any key to exit\n");
476 printf(
"%c", ((
char*)(argument))[i]);
484 if (strcmp(argument,
"\r\n")) {
494 bool display_hex =
false;
495 bool display_bin =
false;
496 if (NULL!=argument && strlen(argument)>0) {
497 if (0==strcmp(argument,
"h") || 0==strcmp(argument,
"hex")) {
499 }
else if (0==strcmp(argument,
"b") || 0==strcmp(argument,
"bin")) {
502 printf(
"malformed argument\n");
506 printf(
"press any key to exit\n");
531 }
else if (display_bin) {
560 printf(
"press 5 times escape to exit\n");
562 uint8_t esc_count = 0;
598 printf(
"press any key to exit\n");
610 .command_description =
"perform protocol actions",
612 .argument_description =
"[actions]",
618 .command_description =
"show incoming data [in hexadecimal or binary]",
620 .argument_description =
"[hex|bin]",
626 .command_description =
"transmit ASCII text (empty for CR+LF)",
628 .argument_description =
"[text]",
633 .name =
"transceive",
634 .command_description =
"transmit and receive data",
636 .argument_description = NULL,
642 .command_description =
"verify incoming transmission for errors",
644 .argument_description = NULL,
651 .description =
"Recommended Standard 232",
#define USART_TX_PORT(x)
get port for USART transmit pin based on USART identifier
#define RCC_USART(x)
get RCC for USART based on USART identifier
static void busvoodoo_rs232_command_actions(void *argument)
command to perform actions
static uint8_t busvoodoo_rs232_databits
RS-232 data bits.
#define BUSVOODOO_LED_PULSE
recommended duration in ms for pulsing LEDs to show activity
static uint32_t busvoodoo_rs232_parity
RS-232 parity setting.
BusVoodoo global definitions and methods (API)
static void busvoodoo_rs232_exit(void)
exit RS-232 mode
static const struct menu_command_t busvoodoo_rs232_commands[]
RS-232 menu commands.
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.
void busvoodoo_led_blue_pulse(uint32_t ms)
pulse blue LED for short duration
static void busvoodoo_rs232_command_transceive(void *argument)
command to transmit and receive data
#define BUSVOODOO_RS232_EN_PORT
RS-232 pin to enable receiver (active low, pulled up)
static void busvoodoo_rs232_command_error(void *argument)
command to verify incoming transmission for error
static void busvoodoo_rs232_write(uint16_t value)
write to RS-232
#define RCC_USART_PORT(x)
get RCC for USART port based on USART identifier
void busvoodoo_oled_update(void)
update OLED display RAM with current display buffer
const char * busvoodoo_global_pinout_rscan[5]
RS/CAN connector pinout.
static void busvoodoo_rs232_command_receive(void *argument)
command to receive data
global definitions and methods (API)
#define GPIO(x)
get GPIO based on GPIO identifier
static bool busvoodoo_rs232_setup(char **prefix, const char *line)
setup RS-232 mode
void sleep_us(uint32_t duration)
go to sleep for some microseconds
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
#define USART_TX_PIN(x)
get pin for USART transmit pin based on USART identifier
static enum busvoodoo_rs232_setting_t busvoodoo_rs232_setting
current mode setup stage
static void busvoodoo_rs232_command_transmit(void *argument)
command to transmit a string
#define USART_CTS_PORT(x)
get port for USART CTS pin based on USART identifier
#define USART(x)
get USART based on USART identifier
char shortcut
short command code (0 if not available)
const char * busvoodoo_global_pinout_io[10]
I/O connector pinout.
char user_input_get(void)
get user input
#define BUSVOODOO_RS232_EN_PIN
RS-232 pin to enable receiver (active low, pulled up)
static uint32_t busvoodoo_rs232_baudrate
RS-232 baud rate (in bps)
static bool busvoodoo_rs232_hwflowctl
RS-232 hardware flow control setting (true = with hardware flow control, false = without hardware flo...
char busvoodoo_global_string[64]
shared string buffer, i.e.
#define BUSVOODOO_RS232_USART
USART peripheral.
size_t snprintf(char *str, size_t size, const char *format,...)
print format string on string or user output
bool busvoodoo_global_actions(char *actions, bool perform, bool(*action_handler)(const char *action, uint32_t repetition, bool perform))
parse and perform actions
void sleep_ms(uint32_t duration)
go to sleep for some milliseconds
#define USART_RX_PORT(x)
get port for USART receive pin based on USART identifier
#define USART_RTS_PIN(x)
get pin for USART RTS pin based on USART identifier
size_t printf(const char *format,...)
print format string on user output
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
static void busvoodoo_rs232_read(void)
read from RS-232
printing utilities to replace the large printf from the standard library (API)
void busvoodoo_oled_text_left(char *text)
draw mode text on top (yellow) left side in display buffer
#define LENGTH(x)
get the length of an array
BusVoodoo RS-232 mode (API)
library to show BusVoodoo mode information on SSD1306 OLED display: name, activity, pinout (API)
static bool busvoodoo_rs232_action(const char *action, uint32_t repetition, bool perform)
perform RS-232 action
volatile bool user_input_available
flag set when user input is available
BusVoodoo mode interface.
static uint32_t busvoodoo_rs232_stopbits
RS-232 stop bits setting.
#define USART_RTS_PORT(x)
get port for USART RTS pin based on USART identifier
#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)