CuVoodoo STM32F1 firmware template
application.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 <string.h> // string utilities
24 
25 /* STM32 (including CM3) libraries */
26 #include <libopencmsis/core_cm3.h> // Cortex M3 utilities
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/dbgmcu.h> // debug utilities
30 
31 /* own libraries */
32 #include "global.h" // board definitions
33 #include "print.h" // printing utilities
34 #include "uart.h" // USART utilities
35 #include "usb_cdcacm.h" // USB CDC ACM utilities
36 #include "terminal.h" // handle the terminal interface
37 #include "menu.h" // menu utilities
38 #include "busvoodoo_global.h" // BusVoodoo definitions
39 #include "busvoodoo_oled.h" // OLED utilities
40 #include "busvoodoo_hiz.h" // BusVoodoo HiZ mode
41 #include "busvoodoo_uart.h" // BusVoodoo UART mode
42 #include "busvoodoo_i2c.h" // BusVoodoo I2C mode
43 #include "busvoodoo_spi.h" // BusVoodoo SPI mode
44 #include "busvoodoo_onewire.h" // BusVoodoo 1-wire mode
45 #include "busvoodoo_rs232.h" // BusVoodoo RS-232 mode
46 #include "busvoodoo_rs485.h" // BusVoodoo RS-485/422 mode
47 
49 static const struct busvoodoo_mode_t* busvoodoo_modes[] = {
57 };
58 
60 static struct busvoodoo_mode_t const * busvoodoo_mode = NULL;
62 static bool busvoodoo_mode_complete = false;
63 
64 size_t putc(char c)
65 {
66  size_t length = 0; // number of characters printed
67  static char last_c = 0; // to remember on which character we last sent
68  if ('\n' == c) { // send carriage return (CR) + line feed (LF) newline for each LF
69  if ('\r' != last_c) { // CR has not already been sent
70  uart_putchar_nonblocking('\r'); // send CR over USART
71  usb_cdcacm_putchar('\r'); // send CR over USB
72  length++; // remember we printed 1 character
73  }
74  }
75  uart_putchar_nonblocking(c); // send byte over USART
76  usb_cdcacm_putchar(c); // send byte over USB
77  length++; // remember we printed 1 character
78  last_c = c; // remember last character
79  return length; // return number of characters printed
80 }
81 
85 static void switch_mode(const struct busvoodoo_mode_t* mode)
86 {
87  if (busvoodoo_mode) {
88  (*busvoodoo_mode->exit)(); // exit current mode
89  }
90  busvoodoo_leds_off(); // switch off LEDs
91  busvoodoo_safe_state(); // return to safe state
92  // reset pinout
93  for (uint8_t i=0; i<LENGTH(busvoodoo_global_pinout_rscan); i++) {
95  }
96  for (uint8_t i=0; i<LENGTH(busvoodoo_global_pinout_io); i++) {
98  }
99  busvoodoo_oled_clear(); // clear OLED display buffer
100  busvoodoo_oled_update(); // update OLED display
101  if (NULL==mode) { // no mode provided
102  busvoodoo_mode = &busvoodoo_hiz_mode; // use default mode
103  } else { // mode provided
104  busvoodoo_mode = mode; // set provided mode a current mode
105  }
106  busvoodoo_mode_complete = (*busvoodoo_mode->setup)(&terminal_prefix, NULL); // start setup
107  terminal_send(0); // update the terminal prompt
108 }
109 
113 static void command_help(void* argument);
117 static void command_mode(void* argument);
121 static void command_quit(void* argument);
125 static void command_reset(void* argument);
131 static const struct menu_command_t menu_commands[] = {
132  {
133  .shortcut = 'm',
134  .name = "mode",
135  .command_description = "select mode",
136  .argument = MENU_ARGUMENT_STRING,
137  .argument_description = "[mode]",
138  .command_handler = &command_mode,
139  },
140  {
141  .shortcut = 'q',
142  .name = "quit",
143  .command_description = "quit current mode",
144  .argument = MENU_ARGUMENT_NONE,
145  .argument_description = NULL,
146  .command_handler = &command_quit,
147  },
148  {
149  .shortcut = 'R',
150  .name = "reset",
151  .command_description = "reset board",
152  .argument = MENU_ARGUMENT_NONE,
153  .argument_description = NULL,
154  .command_handler = &command_reset,
155  },
156  {
157  .shortcut = 'h',
158  .name = "help",
159  .command_description = "display help",
160  .argument = MENU_ARGUMENT_NONE,
161  .argument_description = NULL,
162  .command_handler = &command_help,
163  },
164 };
165 
166 static void command_help(void* argument)
167 {
168  (void)argument; // we won't use the argument
169  printf("available commands:\n");
170  menu_print_commands(menu_commands, LENGTH(menu_commands)); // print global commands
172  if (busvoodoo_full) {
174  }
176  menu_print_commands(busvoodoo_mode->commands, busvoodoo_mode->commands_nb); // print BusVoodoo mode commands
177  }
178 }
179 
180 static void command_mode(void* argument)
181 {
182  if (NULL==argument || 0==strlen(argument)) { // no mode provided: list all modes
183  printf("available modes:\n");
184  for (uint8_t i=0; i<LENGTH(busvoodoo_modes); i++) { // go through all modes
185  if (!busvoodoo_modes[i]->full_only || busvoodoo_full) {
186  printf("%s\t%s\n", busvoodoo_modes[i]->name, busvoodoo_modes[i]->description); // display mode information
187  }
188  }
189  } else { // mode provided
190  bool mode_found = false; // to know if we found the matching mode
191  for (uint8_t i=0; i<LENGTH(busvoodoo_modes); i++) { // go through all modes
192  if (0==strcmp(argument, busvoodoo_modes[i]->name)) { // check for corresponding mode
194  switch_mode(busvoodoo_modes[i]); // switch to mode
195  } else {
196  printf("mode only available for BusVoodoo full\n");
197  }
198  mode_found = true; // remember we found the mode
199  break; // stop searching for mode
200  }
201  }
202  if (!mode_found) {
203  printf("unknown mode: %s\n", argument);
204  }
205  }
206 }
207 
208 static void command_quit(void* argument)
209 {
210  (void)argument; // we won't use the argument
211  switch_mode(NULL); // switch do default mode
212 }
213 
214 static void command_reset(void* argument)
215 {
216  (void)argument; // we won't use the argument
217  scb_reset_system(); // reset device
218  while (true); // wait for the reset to happen
219 }
220 
224 static void process_command(char* str)
225 {
226  // ensure actions are available
227  if (NULL==menu_commands || 0==LENGTH(menu_commands)) {
228  return;
229  }
230  // handle user input
231  if (NULL==busvoodoo_mode) { // no mode set
232  switch_mode(NULL); // set default mode
233  }
234  if (!busvoodoo_mode_complete) { // mode setup is not complete
235  busvoodoo_mode_complete = (*busvoodoo_mode->setup)(&terminal_prefix, str); // continue setup
236  terminal_send(0); // update the terminal prompt
237  } else { // mode setup is complete
238  // don't handle empty lines
239  if (!str || 0==strlen(str)) {
240  return;
241  }
242  bool command_handled = false;
244  command_handled = menu_handle_command(str, busvoodoo_mode->commands, busvoodoo_mode->commands_nb); // try if the mode can handle this command
245  }
246  if (!command_handled && busvoodoo_full) {
247  command_handled = menu_handle_command(str, busvoodoo_global_full_commands, busvoodoo_global_full_commands_nb); // try if full BusVoodoo can handle this command
248  }
249  if (!command_handled) {
250  command_handled = menu_handle_command(str, busvoodoo_global_commands, busvoodoo_global_commands_nb); // try if the base BusVoodoo can handle this command
251  }
252  if (!command_handled) {
253  command_handled = menu_handle_command(str, menu_commands, LENGTH(menu_commands)); // try if this is not a global command
254  }
255  if (!command_handled) {
256  printf("command not recognized. enter help to list commands\n");
257  }
258  }
259 }
260 
264 void main(void);
265 void main(void)
266 {
267  rcc_clock_setup_in_hse_8mhz_out_72mhz(); // use 8 MHz high speed external clock to generate 72 MHz internal clock
268 
269 #if defined(DEBUG) && DEBUG
270  // enable functionalities for easier debug
271  DBGMCU_CR |= DBGMCU_CR_STANDBY; // allow debug also in standby mode (keep digital part and clock powered)
272  DBGMCU_CR |= DBGMCU_CR_STOP; // allow debug also in stop mode (keep clock powered)
273  DBGMCU_CR |= DBGMCU_CR_SLEEP; // allow debug also in sleep mode (keep clock powered)
274 #endif
275 
276  // setup board
277  board_setup(); // setup board
278  uart_setup(); // setup USART (for printing)
279  usb_cdcacm_setup(); // setup USB CDC ACM (for printing)
280  busvoodoo_setup(); // setup BusVoodoo board
281  printf("\nwelcome to \x1b[32mBus\x1b[35mVoodoo\x1b[0m (%s)\n", busvoodoo_full ? "full" : "light"); // print welcome message
282 
283  // setup terminal
284  terminal_prefix = "BV"; // set default prefix
285  terminal_process = &process_command; // set central function to process commands
286  terminal_setup(); // start terminal
287 
288  // setup OLED display
289  sleep_ms(10); // wait a bit until the display is ready
290  busvoodoo_oled_setup(); // setup OLED display
291 
292  // display version
294  busvoodoo_oled_text_left("BusVoodoo");
295  char str[20];
296  snprintf(str, sizeof(str), "fl: %s", busvoodoo_full ? "full" : "light");
297  busvoodoo_oled_text_pos(1, 16+(fonts[FONT_KING10].height+2)*1, FONT_KING10, str);
298  snprintf(str, sizeof(str), "hw: %c", busvoodoo_version);
299  busvoodoo_oled_text_pos(1, 16+(fonts[FONT_KING10].height+2)*2, FONT_KING10, str);
300  snprintf(str, sizeof(str), "fw: %04u-%02u-%02u\n", BUILD_YEAR, BUILD_MONTH, BUILD_DAY);
301  busvoodoo_oled_text_pos(1, 16+(fonts[FONT_KING10].height+2)*3, FONT_KING10, str);
302  snprintf(str, sizeof(str), "bus.cuvoodoo.info");
303  busvoodoo_oled_text_pos((127-((fonts[FONT_KING8].width+1)*strlen(str)))/2, 63, FONT_KING8, str);
305  sleep_ms(500);
307 
308  // setup default mode
309  switch_mode(NULL);
310 
311  // main loop
312  bool action = false; // if an action has been performed don't go to sleep
313  button_flag = false; // reset button flag
314  while (true) { // infinite loop
315  while (user_input_available) { // user input received
316  action = true; // action has been performed
317  char c = user_input_get(); // get user input
318  if (0x04==c) { // CTRL+D is used to quit the mode
319  printf("quit\n"); // acknowledge quitting
320  command_quit(NULL); // quit current mode
321  } else {
322  terminal_send(c); // send received character to terminal
323  }
324  }
325  if (action) { // go to sleep if nothing had to be done, else recheck for activity
326  action = false;
327  } else {
328  __WFI(); // go to sleep
329  }
330  } // main loop
331 }
void busvoodoo_leds_off(void)
switch off blue and red LEDs
static void process_command(char *str)
process user command
Definition: application.c:224
void board_setup(void)
setup board peripherals
Definition: global.c:169
const struct font_s fonts[FONT_MAX]
list of all available fonts
Definition: font.c:321
const struct menu_command_t * commands
list of menu commands provided by mode
command menu entry
Definition: menu.h:31
const bool full_only
if this mode is available only for BusVoodoo full flavor
library for UART communication (API)
const struct menu_command_t busvoodoo_global_commands[]
list of supported commands for base BusVoodoo
const struct busvoodoo_mode_t busvoodoo_onewire_mode
1-wire mode interface definition
BusVoodoo global definitions and methods (API)
void terminal_send(volatile char c)
send character to terminal
Definition: terminal.c:316
static void switch_mode(const struct busvoodoo_mode_t *mode)
switch BusVoddoo mode
Definition: application.c:85
static void command_reset(void *argument)
command to reset board
Definition: application.c:214
static const struct menu_command_t menu_commands[]
command to reboot into bootloader
Definition: application.c:131
void busvoodoo_oled_setup(void)
setup OLED display
void busvoodoo_oled_update(void)
update OLED display RAM with current display buffer
void uart_putchar_nonblocking(char c)
send character over UART (non-blocking)
Definition: uart.c:91
void main(void)
program entry point this is the firmware function started by the micro-controller ...
Definition: application.c:265
const char * busvoodoo_global_pinout_rscan[5]
RS/CAN connector pinout.
void usb_cdcacm_putchar(char c)
send character over USB (non-blocking)
Definition: usb_cdcacm.c:455
const struct busvoodoo_mode_t busvoodoo_spi_mode
SPI mode interface definition.
static const struct busvoodoo_mode_t * busvoodoo_modes[]
all supported BusVoodoo modes
Definition: application.c:49
global definitions and methods (API)
enum menu_argument_t argument
what kind of argument it accepts
Definition: menu.h:35
terminal prompt interface (API)
bool busvoodoo_full
is the BusVoodoo board fully populated (with HV voltage regulator, RS-232, RS-485, CAN transceiver on the back side)
const struct busvoodoo_mode_t busvoodoo_hiz_mode
HiZ mode interface definition.
void usb_cdcacm_setup(void)
setup USB CDC ACM peripheral
Definition: usb_cdcacm.c:436
static struct busvoodoo_mode_t const * busvoodoo_mode
current BusVoodoo mode
Definition: application.c:60
char * name
complete name of the command (space-free)
Definition: menu.h:33
custom 8x5 monospace font
Definition: font.h:24
void terminal_setup(void)
initialize terminal prompt
Definition: terminal.c:304
BusVoodoo SPI mode (API)
const struct busvoodoo_mode_t busvoodoo_uart_mode
UART mode interface definition.
volatile bool button_flag
flag set when board user button has been pressed/released
Definition: global.c:35
char shortcut
short command code (0 if not available)
Definition: menu.h:32
#define BUILD_MONTH
build month as number if known, or 0 if unknown
Definition: global.h:94
const char * busvoodoo_global_pinout_io[10]
I/O connector pinout.
static void command_quit(void *argument)
command to quit current BusVoodoo mode
Definition: application.c:208
BusVoodoo UART mode (API)
char user_input_get(void)
get user input
Definition: global.c:145
const uint8_t busvoodoo_global_commands_nb
number supported commands for base BusVoodoo
void(* exit)(void)
function to exit from mode (i.e.
static void command_help(void *argument)
command to show help
Definition: application.c:166
void sleep_ms(uint32_t duration)
go to sleep for some milliseconds
Definition: global.c:117
const uint8_t busvoodoo_global_full_commands_nb
number supported commands for BusVoodoo full only
const struct busvoodoo_mode_t busvoodoo_rs232_mode
RS-232 mode interface definition.
const uint8_t commands_nb
number of menu commands provided by mode
void busvoodoo_oled_text_left(char *text)
draw mode text on top (yellow) left side in display buffer
BusVoodoo 1-wire mode (API)
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 ...
library for USB CDC ACM communication (API)
static bool busvoodoo_mode_complete
is mode setup complete
Definition: application.c:62
#define LENGTH(x)
get the length of an array
Definition: global.h:26
BusVoodoo RS-232 mode (API)
const struct busvoodoo_mode_t busvoodoo_i2c_mode
I2C mode interface definition.
BusVoodoo high impedance (HiZ) default mode (API)
static void command_mode(void *argument)
command to select mode
Definition: application.c:180
void busvoodoo_oled_clear(void)
clear display buffer
const struct menu_command_t busvoodoo_global_full_commands[]
list of supported commands for BusVoodoo full only
library to show BusVoodoo mode information on SSD1306 OLED display: name, activity, pinout (API)
volatile bool user_input_available
flag set when user input is available
Definition: global.c:36
void busvoodoo_oled_text_pos(uint8_t column, uint8_t row, enum font_name font_name, const char *text)
draw text in display buffer
void busvoodoo_safe_state(void)
set safe state by disabling all outputs
#define BUILD_YEAR
build year as number if known, or 0 if unknown
Definition: global.h:92
char busvoodoo_version
version of the hardware board
BusVoodoo mode interface.
void busvoodoo_setup(void)
setup BusVoodoo board
void uart_setup(void)
setup UART peripheral
Definition: uart.c:50
BusVoodoo RS-485/422 mode (API)
#define BUILD_DAY
build day as number if known, or 0 if unknown
Definition: global.h:96
BusVoodoo I²C mode (API)
size_t putc(char c)
print a single character on user output
Definition: application.c:64
custom 10x6 monospace font
Definition: font.h:25
void(* terminal_process)(char *line)
called when a line is entered
Definition: terminal.c:33
char * terminal_prefix
terminal prompt prefix
Definition: terminal.c:32
const struct busvoodoo_mode_t busvoodoo_rs485_mode
RS-485 mode interface definition.