application.c
Go to the documentation of this file.
/* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
*/
/* standard libraries */
#include <stdint.h> // standard integer types
#include <string.h> // string utilities
/* STM32 (including CM3) libraries */
#include <libopencmsis/core_cm3.h> // Cortex M3 utilities
#include <libopencm3/stm32/gpio.h> // general purpose input output library
#include <libopencm3/stm32/rcc.h> // real-time control clock library
#include <libopencm3/stm32/dbgmcu.h> // debug utilities
/* own libraries */
#include "global.h" // board definitions
#include "print.h" // printing utilities
#include "uart.h" // USART utilities
#include "usb_cdcacm.h" // USB CDC ACM utilities
#include "terminal.h" // handle the terminal interface
#include "menu.h" // menu utilities
#include "busvoodoo_global.h" // BusVoodoo definitions
#include "busvoodoo_oled.h" // OLED utilities
#include "busvoodoo_hiz.h" // BusVoodoo HiZ mode
#include "busvoodoo_uart.h" // BusVoodoo UART mode
#include "busvoodoo_i2c.h" // BusVoodoo I2C mode
#include "busvoodoo_spi.h" // BusVoodoo SPI mode
#include "busvoodoo_onewire.h" // BusVoodoo 1-wire mode
#include "busvoodoo_rs232.h" // BusVoodoo RS-232 mode
#include "busvoodoo_rs485.h" // BusVoodoo RS-485/422 mode
static const struct busvoodoo_mode_t* busvoodoo_modes[] = {
&busvoodoo_hiz_mode,
&busvoodoo_uart_mode,
&busvoodoo_i2c_mode,
&busvoodoo_spi_mode,
&busvoodoo_onewire_mode,
&busvoodoo_rs232_mode,
&busvoodoo_rs485_mode,
};
static struct busvoodoo_mode_t const * busvoodoo_mode = NULL;
static bool busvoodoo_mode_complete = false;
size_t putc(char c)
{
size_t length = 0; // number of characters printed
static char last_c = 0; // to remember on which character we last sent
if ('\n' == c) { // send carriage return (CR) + line feed (LF) newline for each LF
if ('\r' != last_c) { // CR has not already been sent
uart_putchar_nonblocking('\r'); // send CR over USART
usb_cdcacm_putchar('\r'); // send CR over USB
length++; // remember we printed 1 character
}
}
uart_putchar_nonblocking(c); // send byte over USART
usb_cdcacm_putchar(c); // send byte over USB
length++; // remember we printed 1 character
last_c = c; // remember last character
return length; // return number of characters printed
}
static void switch_mode(const struct busvoodoo_mode_t* mode)
{
if (busvoodoo_mode) {
(*busvoodoo_mode->exit)(); // exit current mode
}
busvoodoo_leds_off(); // switch off LEDs
busvoodoo_safe_state(); // return to safe state
// reset pinout
for (uint8_t i=0; i<LENGTH(busvoodoo_global_pinout_rscan); i++) {
busvoodoo_global_pinout_rscan[i] = NULL;
}
for (uint8_t i=0; i<LENGTH(busvoodoo_global_pinout_io); i++) {
busvoodoo_global_pinout_io[i] = NULL;
}
busvoodoo_oled_clear(); // clear OLED display buffer
busvoodoo_oled_update(); // update OLED display
if (NULL==mode) { // no mode provided
busvoodoo_mode = &busvoodoo_hiz_mode; // use default mode
} else { // mode provided
busvoodoo_mode = mode; // set provided mode a current mode
}
busvoodoo_mode_complete = (*busvoodoo_mode->setup)(&terminal_prefix, NULL); // start setup
terminal_send(0); // update the terminal prompt
}
static void command_help(void* argument);
static void command_mode(void* argument);
static void command_quit(void* argument);
static void command_reset(void* argument);
static const struct menu_command_t menu_commands[] = {
{
.shortcut = 'm',
.name = "mode",
.command_description = "select mode",
.argument = MENU_ARGUMENT_STRING,
.argument_description = "[mode]",
.command_handler = &command_mode,
},
{
.shortcut = 'q',
.name = "quit",
.command_description = "quit current mode",
.argument = MENU_ARGUMENT_NONE,
.argument_description = NULL,
.command_handler = &command_quit,
},
{
.shortcut = 'R',
.name = "reset",
.command_description = "reset board",
.argument = MENU_ARGUMENT_NONE,
.argument_description = NULL,
.command_handler = &command_reset,
},
{
.shortcut = 'h',
.name = "help",
.command_description = "display help",
.argument = MENU_ARGUMENT_NONE,
.argument_description = NULL,
.command_handler = &command_help,
},
};
static void command_help(void* argument)
{
(void)argument; // we won't use the argument
printf("available commands:\n");
menu_print_commands(menu_commands, LENGTH(menu_commands)); // print global commands
menu_print_commands(busvoodoo_global_commands, busvoodoo_global_commands_nb); // print BusVoodoo global commands
if (busvoodoo_full) {
menu_print_commands(busvoodoo_global_full_commands, busvoodoo_global_full_commands_nb); // print BusVoodoo global commands
}
if (!busvoodoo_mode->full_only || busvoodoo_full) {
menu_print_commands(busvoodoo_mode->commands, busvoodoo_mode->commands_nb); // print BusVoodoo mode commands
}
}
static void command_mode(void* argument)
{
if (NULL==argument || 0==strlen(argument)) { // no mode provided: list all modes
printf("available modes:\n");
for (uint8_t i=0; i<LENGTH(busvoodoo_modes); i++) { // go through all modes
if (!busvoodoo_modes[i]->full_only || busvoodoo_full) {
printf("%s\t%s\n", busvoodoo_modes[i]->name, busvoodoo_modes[i]->description); // display mode information
}
}
} else { // mode provided
bool mode_found = false; // to know if we found the matching mode
for (uint8_t i=0; i<LENGTH(busvoodoo_modes); i++) { // go through all modes
if (0==strcmp(argument, busvoodoo_modes[i]->name)) { // check for corresponding mode
if (!busvoodoo_mode->full_only || busvoodoo_full) {
switch_mode(busvoodoo_modes[i]); // switch to mode
} else {
printf("mode only available for BusVoodoo full\n");
}
mode_found = true; // remember we found the mode
break; // stop searching for mode
}
}
if (!mode_found) {
printf("unknown mode: %s\n", argument);
}
}
}
static void command_quit(void* argument)
{
(void)argument; // we won't use the argument
switch_mode(NULL); // switch do default mode
}
static void command_reset(void* argument)
{
(void)argument; // we won't use the argument
scb_reset_system(); // reset device
while (true); // wait for the reset to happen
}
static void process_command(char* str)
{
// ensure actions are available
if (NULL==menu_commands || 0==LENGTH(menu_commands)) {
return;
}
// handle user input
if (NULL==busvoodoo_mode) { // no mode set
switch_mode(NULL); // set default mode
}
if (!busvoodoo_mode_complete) { // mode setup is not complete
busvoodoo_mode_complete = (*busvoodoo_mode->setup)(&terminal_prefix, str); // continue setup
terminal_send(0); // update the terminal prompt
} else { // mode setup is complete
// don't handle empty lines
if (!str || 0==strlen(str)) {
return;
}
bool command_handled = false;
if (!busvoodoo_mode->full_only || busvoodoo_full) {
command_handled = menu_handle_command(str, busvoodoo_mode->commands, busvoodoo_mode->commands_nb); // try if the mode can handle this command
}
if (!command_handled && busvoodoo_full) {
command_handled = menu_handle_command(str, busvoodoo_global_full_commands, busvoodoo_global_full_commands_nb); // try if full BusVoodoo can handle this command
}
if (!command_handled) {
command_handled = menu_handle_command(str, busvoodoo_global_commands, busvoodoo_global_commands_nb); // try if the base BusVoodoo can handle this command
}
if (!command_handled) {
command_handled = menu_handle_command(str, menu_commands, LENGTH(menu_commands)); // try if this is not a global command
}
if (!command_handled) {
printf("command not recognized. enter help to list commands\n");
}
}
}
void main(void);
void main(void)
{
rcc_clock_setup_in_hse_8mhz_out_72mhz(); // use 8 MHz high speed external clock to generate 72 MHz internal clock
#if defined(DEBUG) && DEBUG
// enable functionalities for easier debug
DBGMCU_CR |= DBGMCU_CR_STANDBY; // allow debug also in standby mode (keep digital part and clock powered)
DBGMCU_CR |= DBGMCU_CR_STOP; // allow debug also in stop mode (keep clock powered)
DBGMCU_CR |= DBGMCU_CR_SLEEP; // allow debug also in sleep mode (keep clock powered)
#endif
// setup board
board_setup(); // setup board
uart_setup(); // setup USART (for printing)
usb_cdcacm_setup(); // setup USB CDC ACM (for printing)
busvoodoo_setup(); // setup BusVoodoo board
printf("\nwelcome to \x1b[32mBus\x1b[35mVoodoo\x1b[0m (%s)\n", busvoodoo_full ? "full" : "light"); // print welcome message
// setup terminal
terminal_prefix = "BV"; // set default prefix
terminal_process = &process_command; // set central function to process commands
terminal_setup(); // start terminal
// setup OLED display
sleep_ms(10); // wait a bit until the display is ready
busvoodoo_oled_setup(); // setup OLED display
// setup default mode
switch_mode(NULL);
// main loop
bool action = false; // if an action has been performed don't go to sleep
button_flag = false; // reset button flag
while (true) { // infinite loop
while (user_input_available) { // user input received
action = true; // action has been performed
char c = user_input_get(); // get user input
if (0x04==c) { // CTRL+D is used to quit the mode
printf("quit\n"); // acknowledge quitting
command_quit(NULL); // quit current mode
} else {
terminal_send(c); // send received character to terminal
}
}
if (action) { // go to sleep if nothing had to be done, else recheck for activity
action = false;
} else {
__WFI(); // go to sleep
}
} // main loop
}
busvoodoo_leds_off
void busvoodoo_leds_off(void)
switch off blue and red LEDs
Definition: busvoodoo_global.c:447
process_command
static void process_command(char *str)
process user command
Definition: application.c:224
board_setup
void board_setup(void)
setup board peripherals
Definition: global.c:169
busvoodoo_mode_t::commands
const struct menu_command_t * commands
list of menu commands provided by mode
Definition: busvoodoo_global.h:118
menu_command_t
command menu entry
Definition: menu.h:31
busvoodoo_mode_t::full_only
const bool full_only
if this mode is available only for BusVoodoo full flavor
Definition: busvoodoo_global.h:116
uart.h
library for UART communication (API)
busvoodoo_global_commands
const struct menu_command_t busvoodoo_global_commands[]
list of supported commands for base BusVoodoo
Definition: busvoodoo_global.c:880
busvoodoo_onewire_mode
const struct busvoodoo_mode_t busvoodoo_onewire_mode
1-wire mode interface definition
Definition: busvoodoo_onewire.c:382
busvoodoo_global.h
BusVoodoo global definitions and methods (API)
terminal_send
void terminal_send(volatile char c)
send character to terminal
Definition: terminal.c:316
switch_mode
static void switch_mode(const struct busvoodoo_mode_t *mode)
switch BusVoddoo mode
Definition: application.c:85
command_reset
static void command_reset(void *argument)
command to reset board
Definition: application.c:214
menu_commands
static const struct menu_command_t menu_commands[]
command to reboot into bootloader
Definition: application.c:131
busvoodoo_oled_setup
void busvoodoo_oled_setup(void)
setup OLED display
Definition: busvoodoo_oled.c:334
busvoodoo_oled_update
void busvoodoo_oled_update(void)
update OLED display RAM with current display buffer
Definition: busvoodoo_oled.c:589
uart_putchar_nonblocking
void uart_putchar_nonblocking(char c)
send character over UART (non-blocking)
Definition: uart.c:91
main
void main(void)
program entry point this is the firmware function started by the micro-controller ...
Definition: application.c:265
busvoodoo_global_pinout_rscan
const char * busvoodoo_global_pinout_rscan[5]
RS/CAN connector pinout.
Definition: busvoodoo_global.c:63
usb_cdcacm_putchar
void usb_cdcacm_putchar(char c)
send character over USB (non-blocking)
Definition: usb_cdcacm.c:455
busvoodoo_spi_mode
const struct busvoodoo_mode_t busvoodoo_spi_mode
SPI mode interface definition.
Definition: busvoodoo_spi.c:499
busvoodoo_modes
static const struct busvoodoo_mode_t * busvoodoo_modes[]
all supported BusVoodoo modes
Definition: application.c:49
global.h
global definitions and methods (API)
menu_command_t::argument
enum menu_argument_t argument
what kind of argument it accepts
Definition: menu.h:35
terminal.h
terminal prompt interface (API)
busvoodoo_full
bool busvoodoo_full
is the BusVoodoo board fully populated (with HV voltage regulator, RS-232, RS-485, CAN transceiver on the back side)
Definition: busvoodoo_global.c:70
busvoodoo_hiz_mode
const struct busvoodoo_mode_t busvoodoo_hiz_mode
HiZ mode interface definition.
Definition: busvoodoo_hiz.c:986
usb_cdcacm_setup
void usb_cdcacm_setup(void)
setup USB CDC ACM peripheral
Definition: usb_cdcacm.c:436
busvoodoo_mode
static struct busvoodoo_mode_t const * busvoodoo_mode
current BusVoodoo mode
Definition: application.c:60
menu_command_t::name
char * name
complete name of the command (space-free)
Definition: menu.h:33
terminal_setup
void terminal_setup(void)
initialize terminal prompt
Definition: terminal.c:304
busvoodoo_spi.h
BusVoodoo SPI mode (API)
busvoodoo_uart_mode
const struct busvoodoo_mode_t busvoodoo_uart_mode
UART mode interface definition.
Definition: busvoodoo_uart.c:686
button_flag
volatile bool button_flag
flag set when board user button has been pressed/released
Definition: global.c:35
menu_command_t::shortcut
char shortcut
short command code (0 if not available)
Definition: menu.h:32
busvoodoo_global_pinout_io
const char * busvoodoo_global_pinout_io[10]
I/O connector pinout.
Definition: busvoodoo_global.c:62
command_quit
static void command_quit(void *argument)
command to quit current BusVoodoo mode
Definition: application.c:208
busvoodoo_uart.h
BusVoodoo UART mode (API)
user_input_get
char user_input_get(void)
get user input
Definition: global.c:145
busvoodoo_global_commands_nb
const uint8_t busvoodoo_global_commands_nb
number supported commands for base BusVoodoo
Definition: busvoodoo_global.c:907
busvoodoo_mode_t::exit
void(* exit)(void)
function to exit from mode (i.e.
Definition: busvoodoo_global.h:120
command_help
static void command_help(void *argument)
command to show help
Definition: application.c:166
sleep_ms
void sleep_ms(uint32_t duration)
go to sleep for some milliseconds
Definition: global.c:117
printf
size_t printf(const char *format,...)
print format string on user output
Definition: print.c:448
busvoodoo_global_full_commands_nb
const uint8_t busvoodoo_global_full_commands_nb
number supported commands for BusVoodoo full only
Definition: busvoodoo_global.c:920
busvoodoo_rs232_mode
const struct busvoodoo_mode_t busvoodoo_rs232_mode
RS-232 mode interface definition.
Definition: busvoodoo_rs232.c:649
busvoodoo_mode_t::commands_nb
const uint8_t commands_nb
number of menu commands provided by mode
Definition: busvoodoo_global.h:119
print.h
printing utilities to replace the large printf from the standard library (API)
busvoodoo_onewire.h
BusVoodoo 1-wire mode (API)
busvoodoo_mode_t::setup
bool(* setup)(char **prefix, const char *line)
function to setup mode (menu prefix can be used to ask parameter, and line will be the user provided ...
Definition: busvoodoo_global.h:117
usb_cdcacm.h
library for USB CDC ACM communication (API)
busvoodoo_mode_complete
static bool busvoodoo_mode_complete
is mode setup complete
Definition: application.c:62
LENGTH
#define LENGTH(x)
get the length of an array
Definition: global.h:26
busvoodoo_rs232.h
BusVoodoo RS-232 mode (API)
busvoodoo_i2c_mode
const struct busvoodoo_mode_t busvoodoo_i2c_mode
I2C mode interface definition.
Definition: busvoodoo_i2c.c:550
busvoodoo_hiz.h
BusVoodoo high impedance (HiZ) default mode (API)
command_mode
static void command_mode(void *argument)
command to select mode
Definition: application.c:180
busvoodoo_oled_clear
void busvoodoo_oled_clear(void)
clear display buffer
Definition: busvoodoo_oled.c:348
busvoodoo_global_full_commands
const struct menu_command_t busvoodoo_global_full_commands[]
list of supported commands for BusVoodoo full only
Definition: busvoodoo_global.c:909
busvoodoo_oled.h
library to show BusVoodoo mode information on SSD1306 OLED display: name, activity, pinout (API)
user_input_available
volatile bool user_input_available
flag set when user input is available
Definition: global.c:36
busvoodoo_safe_state
void busvoodoo_safe_state(void)
set safe state by disabling all outputs
Definition: busvoodoo_global.c:171
busvoodoo_mode_t
BusVoodoo mode interface.
Definition: busvoodoo_global.h:113
busvoodoo_setup
void busvoodoo_setup(void)
setup BusVoodoo board
Definition: busvoodoo_global.c:74
uart_setup
void uart_setup(void)
setup UART peripheral
Definition: uart.c:50
busvoodoo_rs485.h
BusVoodoo RS-485/422 mode (API)
busvoodoo_i2c.h
BusVoodoo I²C mode (API)
putc
size_t putc(char c)
print a single character on user output
Definition: application.c:64
terminal_process
void(* terminal_process)(char *line)
called when a line is entered
Definition: terminal.c:33
terminal_prefix
char * terminal_prefix
terminal prompt prefix
Definition: terminal.c:32
busvoodoo_rs485_mode
const struct busvoodoo_mode_t busvoodoo_rs485_mode
RS-485 mode interface definition.
Definition: busvoodoo_rs485.c:640