27 #include <libopencm3/cm3/nvic.h> 28 #include <libopencm3/stm32/gpio.h> 29 #include <libopencm3/stm32/rcc.h> 30 #include <libopencm3/stm32/usart.h> 31 #include <libopencm3/stm32/timer.h> 45 BUSVOODOO_UART_SETTING_NONE,
46 BUSVOODOO_UART_SETTING_BAUDRATE,
47 BUSVOODOO_UART_SETTING_DATABITS,
48 BUSVOODOO_UART_SETTING_PARITY,
49 BUSVOODOO_UART_SETTING_STOPBITS,
50 BUSVOODOO_UART_SETTING_HWFLOWCTL,
51 BUSVOODOO_UART_SETTING_DRIVE,
52 BUSVOODOO_UART_SETTING_DONE,
93 bool complete =
false;
98 case BUSVOODOO_UART_SETTING_NONE:
103 case BUSVOODOO_UART_SETTING_BAUDRATE:
104 if (NULL==line || 0==strlen(line)) {
107 uint32_t baudrate = atoi(line);
108 if (baudrate>0 && baudrate<=2000000) {
118 case BUSVOODOO_UART_SETTING_DATABITS:
119 if (NULL==line || 0==strlen(line)) {
121 }
else if (1==strlen(line)) {
122 uint8_t databits = atoi(line);
123 if (databits>=5 && databits<=8) {
138 case BUSVOODOO_UART_SETTING_PARITY:
139 if (NULL==line || 0==strlen(line)) {
141 }
else if (1==strlen(line)) {
142 uint8_t parity = atoi(line);
143 if (parity>0 && parity<6) {
157 case BUSVOODOO_UART_SETTING_STOPBITS:
158 if (NULL==line || 0==strlen(line)) {
160 }
else if (1==strlen(line)) {
164 }
else if (
'2'==line[0]) {
167 }
else if (
'3'==line[0]) {
170 }
else if (
'4'==line[0]) {
180 printf(
"1) no flow control\n");
181 printf(
"2) RTS/CTS hardware flow control\n");
186 case BUSVOODOO_UART_SETTING_HWFLOWCTL:
187 if (NULL==line || 0==strlen(line)) {
189 }
else if (1==strlen(line)) {
190 if (
'1'==line[0] ||
'2'==line[0]) {
202 printf(
"1) push-pull (3.3V)\n");
203 printf(
"2) open-drain, with embedded pull-up resistors (2kO)\n");
204 printf(
"3) open-drain, with external pull-up resistors\n");
209 case BUSVOODOO_UART_SETTING_DRIVE:
210 if (NULL==line || 0==strlen(line)) {
212 }
else if (1==strlen(line)) {
213 uint8_t drive = atoi(line);
214 if (1==drive || 2==drive || 3==drive) {
222 rcc_periph_clock_enable(RCC_AFIO);
259 printf(
"use LV to set pull-up voltage\n");
310 printf(
"write: '%c'/0x%02x\n", value, value);
340 printf(
"'%c'/0x%02x", input, input);
345 }
else if (error_framing) {
371 uint32_t length = strlen(action);
372 if (NULL==action || 0==length) {
376 if (1==length &&
'r'==action[0]) {
380 for (uint32_t i=0; i<repetition; i++) {
383 }
else if (1==length &&
'u'==action[0]) {
387 printf(
"wait for %u us\n", repetition);
389 }
else if (1==length &&
'm'==action[0]) {
393 printf(
"wait for %u ms\n", repetition);
395 }
else if (
'0'==action[0]) {
400 for (uint32_t i=0; i<repetition; i++) {
403 }
else if (
'x'==action[1] ||
'b'==action[1]) {
405 }
else if (action[1]>=
'0' && action[1]<=
'9') {
410 }
else if (
'x'==action[0] && length>1) {
411 for (uint32_t i=1; i<length; i++) {
412 if (!((action[i]>=
'0' && action[i]<=
'9') || (action[i]>=
'a' && action[i]<=
'f') || (action[i]>=
'A' && action[i]<=
'F'))) {
419 uint32_t value = strtol(&action[1], NULL, 16);
420 for (uint32_t i=0; i<repetition; i++) {
423 }
else if (
'b'==action[0] && length>1) {
424 for (uint32_t i=1; i<length; i++) {
425 if (action[i]<
'0' || action[i]>
'1') {
432 uint32_t value = strtol(&action[1], NULL, 2);
433 for (uint32_t i=0; i<repetition; i++) {
436 }
else if (action[0]>=
'1' && action[0]<=
'9') {
437 for (uint32_t i=1; i<length; i++) {
438 if (action[i]<
'0' || action[i]>
'9') {
445 uint32_t value = strtol(&action[0], NULL, 10);
446 for (uint32_t i=0; i<repetition; i++) {
449 }
else if (length>=2 && (
'"'==action[0] ||
'\''==action[0]) && (action[length-1]==action[0])) {
453 for (uint32_t r=0; r<repetition; r++) {
454 for (uint32_t i=1; i<length-1; i++) {
472 if (NULL==argument || 0==strlen(argument)) {
473 printf(
"available actions (separated by space or ,):\n");
474 printf(
"0\twrite decimal value\n");
475 printf(
"0x0\twrite hexadecimal value\n");
476 printf(
"0b0\twrite binary value\n");
477 printf(
"\"a\"/'a'\twrite ASCII characters\n");
478 printf(
"r\tread value\n");
479 printf(
"u/m\twait 1 us/ms\n");
480 printf(
":n\trepeat action n times\n");
485 char* copy = calloc(strlen(argument)+1,
sizeof(
char));
489 strncpy(copy, argument, strlen(argument)+1);
492 printf(
"malformed action(s)\n");
494 printf(
"press any key to exit\n");
514 if (NULL==argument || 0==strlen(argument)) {
517 printf(
"press any key to exit\n");
521 printf(
"%c", ((
char*)(argument))[i]);
530 if (strcmp(argument,
"\r\n")) {
543 bool display_hex =
false;
544 bool display_bin =
false;
545 if (NULL!=argument && strlen(argument)>0) {
546 if (0==strcmp(argument,
"h") || 0==strcmp(argument,
"hex")) {
548 }
else if (0==strcmp(argument,
"b") || 0==strcmp(argument,
"bin")) {
551 printf(
"malformed argument\n");
561 printf(
"press any key to exit\n");
568 }
else if (display_bin) {
594 printf(
"press 5 times escape to exit\n");
596 uint8_t esc_count = 0;
648 printf(
"press any key to exit\n");
659 .command_description =
"perform protocol actions",
661 .argument_description =
"[actions]",
667 .command_description =
"show incoming data [in hexadecimal or binary]",
669 .argument_description =
"[hex|bin]",
675 .command_description =
"transmit ASCII text (empty for CR+LF)",
677 .argument_description =
"[text]",
682 .name =
"transceive",
683 .command_description =
"transmit and receive data",
685 .argument_description = NULL,
691 .command_description =
"verify incoming transmission for errors",
693 .argument_description = NULL,
static uint32_t busvoodoo_uart_generic_baudrate
UART baud rate (in bps)
static enum busvoodoo_uart_generic_setting_t busvoodoo_uart_generic_setting
current mode setup stage
bool busvoodoo_uart_generic_setup(char **prefix, const char *line)
setup generic UART mode
#define BUSVOODOO_LED_PULSE
recommended duration in ms for pulsing LEDs to show activity
void usart_enhanced_send(uint32_t usart, uint8_t data)
send data over the enhanced USART using the configuration
static bool busvoodoo_uart_generic_hwflowctl
UART hardware flow control setting (true = with hardware flow control, false = without hardware flow ...
BusVoodoo global definitions and methods (API)
static bool busvoodoo_uart_generic_action(const char *action, uint32_t repetition, bool perform)
perform UART action
busvoodoo_uart_generic_setting_t
mode setup stage
void(* tx_post)(void)
method to be called after transmitting data
void busvoodoo_led_blue_pulse(uint32_t ms)
pulse blue LED for short duration
uint32_t rx_pin
GPIO pin address of receive pin.
global definitions and methods (API)
uint32_t rts_port
GPIO port address of request to send pin.
void sleep_us(uint32_t duration)
go to sleep for some microseconds
static void busvoodoo_uart_generic_command_actions(void *argument)
command to perform actions
static const struct busvoodoo_uart_generic_specific_t * busvoodoo_uart_generic_specific
the USART mode specific information
static void busvoodoo_uart_generic_write(uint8_t value)
write to UART
usart_enhanced_parity_t
enhanced USART setting for the additional parity bit
static void busvoodoo_uart_generic_command_error(void *argument)
command to verify incoming transmission for error
void busvoodoo_led_blue_off(void)
switch off blue LED
void(* rx_pre)(void)
method to be called before receiving data
uint32_t usart
USART peripheral base address.
uint32_t rx_rcc
GPIO RCC address of receive pin.
bool multidrive
if multiple drive modes are supported (push-pull, open-drain with internal resistors, open-drain with external resistors), or just push-pull
uint32_t rx_port
GPIO port address of receive pin.
uint32_t tx_pin
GPIO pin address of transmit pin.
void(* rx_post)(void)
method to be called after receiving data
void busvoodoo_uart_generic_exit(void)
exit genetic UART mode
char shortcut
short command code (0 if not available)
uint32_t tx_rcc
GPIO RCC address of transmit pin.
UART specific methods that will be called by the generic methods.
void(* tx_pre)(void)
method to be called before transmitting data
static bool busvoodoo_uart_generic_pullup
if embedded pull-up resistors are used
uint32_t rts_rcc
GPIO RCC address of request to send pin.
static void busvoodoo_uart_generic_command_transmit(void *argument)
command to transmit a string
uint32_t cts_port
GPIO port address of clear to send pin.
static enum usart_enhanced_parity_t busvoodoo_uart_generic_parity
UART parity setting.
char user_input_get(void)
get user input
char busvoodoo_global_string[64]
shared string buffer, i.e.
#define busvoodoo_uart_generic_commands_nb
number of commands supported by the generic UART mode
uint32_t usart_rst
USART RST address.
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
uint32_t rts_pin
GPIO pin address of request to send pin.
bool busvoodoo_uart_generic_configure(const struct busvoodoo_uart_generic_specific_t *conf)
provide the generic USART with mode specific information
uint32_t usart_rcc
USART RCC address.
size_t printf(const char *format,...)
print format string on user output
bool usart_enhanced_config(uint32_t usart, uint8_t databits, enum usart_enhanced_parity_t parity)
configure enhanced USART
static void busvoodoo_uart_generic_read(void)
read from UART
BusVoodoo generic UART mode (API)
static uint32_t busvoodoo_uart_generic_stopbits
UART stop bits setting.
static void busvoodoo_uart_generic_command_receive(void *argument)
command to receive data
printing utilities to replace the large printf from the standard library (API)
uint32_t cts_rcc
GPIO RCC address of clear to send pin.
uint8_t usart_enhanced_recv(uint32_t usart)
receive data over the enhanced USART using the configuration
const struct menu_command_t busvoodoo_uart_generic_commands[busvoodoo_uart_generic_commands_nb]
commands supported by the generic UART mode
float busvoodoo_embedded_pullup(bool on)
enable embedded pull-up resistors
static bool busvoodoo_uart_generic_drive
pin drive mode (true = push-pull, false = open-drain)
#define LENGTH(x)
get the length of an array
bool hwflowctl
if RTC/CTS hardware flow control is supported
static void busvoodoo_uart_generic_command_transceive(void *argument)
command to transmit and receive data
volatile bool user_input_available
flag set when user input is available
static uint8_t busvoodoo_uart_generic_databits
UART data bits.
uint32_t tx_port
GPIO port address of transmit pin.
bool usart_enhanced_parity_error(uint32_t usart)
get the parity status of the received data
library for enhanced USART communication (API)
uint32_t cts_pin
GPIO pin address of clear to send pin.