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 
46 static struct busvoodoo_mode_t* busvoodoo_modes[] = {
51 };
52 
54 static struct busvoodoo_mode_t* busvoodoo_mode = NULL;
56 static bool busvoodoo_mode_complete = false;
57 
58 size_t putc(char c)
59 {
60  size_t length = 0; // number of characters printed
61  static char last_c = 0; // to remember on which character we last sent
62  if ('\n' == c) { // send carriage return (CR) + line feed (LF) newline for each LF
63  if ('\r' != last_c) { // CR has not already been sent
64  uart_putchar_nonblocking('\r'); // send CR over USART
65  usb_cdcacm_putchar('\r'); // send CR over USB
66  length++; // remember we printed 1 character
67  }
68  }
69  uart_putchar_nonblocking(c); // send byte over USART
70  usb_cdcacm_putchar(c); // send byte over USB
71  length++; // remember we printed 1 character
72  last_c = c; // remember last character
73  return length; // return number of characters printed
74 }
75 
79 static void switch_mode(struct busvoodoo_mode_t* mode)
80 {
81  if (busvoodoo_mode) {
82  (*busvoodoo_mode->exit)(); // exit current mode
83  }
84  led_off(); // switch off LEDs
85  busvoodoo_safe_state(); // return to safe state
86  // reset pinout
87  for (uint8_t i=0; i<LENGTH(busvoodoo_global_pinout_rscan); i++) {
89  }
90  for (uint8_t i=0; i<LENGTH(busvoodoo_global_pinout_io); i++) {
92  }
93  busvoodoo_oled_clear(); // clear OLED display buffer
94  busvoodoo_oled_update(); // update OLED display
95  if (NULL==mode) { // no mode provided
96  busvoodoo_mode = &busvoodoo_hiz_mode; // use default mode
97  } else { // mode provided
98  busvoodoo_mode = mode; // set provided mode a current mode
99  }
100  busvoodoo_mode_complete = (*busvoodoo_mode->setup)(&terminal_prefix, NULL); // start setup
101  terminal_send(0); // update the terminal prompt
102 }
103 
107 static void command_help(void* argument);
111 static void command_mode(void* argument);
115 static void command_quit(void* argument);
119 static void command_reset(void* argument);
125 static const struct menu_command_t menu_commands[] = {
126  {
127  'm',
128  "mode",
129  "select mode",
131  "[mode]",
132  &command_mode,
133  },
134  {
135  'q',
136  "quit",
137  "quit current mode",
139  NULL,
140  &command_quit,
141  },
142  {
143  'r',
144  "reset",
145  "reset board",
147  NULL,
148  &command_reset,
149  },
150  {
151  'h',
152  "help",
153  "display help",
155  NULL,
156  &command_help,
157  },
158 };
159 
160 static void command_help(void* argument)
161 {
162  (void)argument; // we won't use the argument
163  printf("available commands:\n");
164  menu_print_commands(menu_commands, LENGTH(menu_commands)); // print global commands
166  menu_print_commands(busvoodoo_mode->commands, busvoodoo_mode->commands_nb); // print BusVoodoo mode commands
167 }
168 
169 static void command_mode(void* argument)
170 {
171  if (NULL==argument || 0==strlen(argument)) { // no mode provided: list all modes
172  printf("available modes:\n");
173  for (uint8_t i=0; i<LENGTH(busvoodoo_modes); i++) { // go through all modes
174  printf("%s\t%s\n", busvoodoo_modes[i]->name, busvoodoo_modes[i]->description); // display mode information
175  }
176  } else { // mode provided
177  bool mode_found = false; // to know if we found the matching mode
178  for (uint8_t i=0; i<LENGTH(busvoodoo_modes); i++) { // go through all modes
179  if (0==strcmp(argument, busvoodoo_modes[i]->name)) { // check for corresponding mode
180  switch_mode(busvoodoo_modes[i]); // switch to mode
181  mode_found = true; // remember we found the mode
182  break; // stop searching for mode
183  }
184  }
185  if (!mode_found) {
186  printf("unknown mode: %s\n", argument);
187  }
188  }
189 }
190 
191 static void command_quit(void* argument)
192 {
193  (void)argument; // we won't use the argument
194  switch_mode(NULL); // switch do default mode
195 }
196 
197 static void command_reset(void* argument)
198 {
199  (void)argument; // we won't use the argument
200  scb_reset_system(); // reset device
201  while (true); // wait for the reset to happen
202 }
203 
207 static void process_command(char* str)
208 {
209  // ensure actions are available
210  if (NULL==menu_commands || 0==LENGTH(menu_commands)) {
211  return;
212  }
213  // handle user input
214  if (NULL==busvoodoo_mode) { // no mode set
215  switch_mode(NULL); // set default mode
216  }
217  if (!busvoodoo_mode_complete) { // mode setup is not complete
218  busvoodoo_mode_complete = (*busvoodoo_mode->setup)(&terminal_prefix, str); // continue setup
219  terminal_send(0); // update the terminal prompt
220  } else {
221  // don't handle empty lines
222  if (!str || 0==strlen(str)) {
223  return;
224  }
228  printf("command not recognized. enter help to list commands\n");
229  }
230  }
231  }
232  }
233 }
234 
238 void main(void);
239 void main(void)
240 {
241  rcc_clock_setup_in_hse_8mhz_out_72mhz(); // use 8 MHz high speed external clock to generate 72 MHz internal clock
242 
243  // enable functionalities for easier debug
244  DBGMCU_CR |= DBGMCU_CR_STANDBY; // allow debug also in standby mode (keep digital part and clock powered)
245  DBGMCU_CR |= DBGMCU_CR_STOP; // allow debug also in stop mode (keep clock powered)
246  DBGMCU_CR |= DBGMCU_CR_SLEEP; // allow debug also in sleep mode (keep clock powered)
247 
248  board_setup(); // setup board
249  uart_setup(); // setup USART (for printing)
250  usb_cdcacm_setup(); // setup USB CDC ACM (for printing)
251 
252  busvoodoo_setup(); // setup BusVoodoo board
253  printf("\nwelcome to BusVoodoo ("); // print welcome message
254  if (busvoodoo_full) {
255  printf("full");
256  } else {
257  printf("light");
258  }
259  printf(")\n");
260 
261  // setup terminal
262  terminal_prefix = "BV"; // set default prefix
263  terminal_process = &process_command; // set central function to process commands
264  terminal_setup(); // start terminal
265 
266  // setup OLED display
267  sleep_ms(10); // wait a bit until the display is ready
268  busvoodoo_oled_setup(); // setup OLED display
269 
270  // setup default mode
271  switch_mode(NULL);
272 
273  // main loop
274  bool action = false; // if an action has been performed don't go to sleep
275  button_flag = false; // reset button flag
276  while (true) { // infinite loop
277  while (user_input_available) { // user input received
278  action = true; // action has been performed
279  char c = user_input_get(); // get user input
280  if (0x04==c) { // CTRL+D is used to quit the mode
281  printf("quit\n"); // acknowledge quitting
282  command_quit(NULL); // quit current mode
283  } else {
284  terminal_send(c); // send received character to terminal
285  }
286  }
287  if (action) { // go to sleep if nothing had to be done, else recheck for activity
288  action = false;
289  } else {
290  __WFI(); // go to sleep
291  }
292  } // main loop
293 }
static void process_command(char *str)
process user command
Definition: application.c:207
void board_setup(void)
setup board peripherals
Definition: global.c:211
const struct menu_command_t * commands
list of menu commands provided by mode
command menu entry
Definition: menu.h:31
library for UART communication (API)
const struct menu_command_t busvoodoo_global_commands[]
list of supported commands
BusVoodoo global definitions and methods (API)
struct busvoodoo_mode_t busvoodoo_i2c_mode
I2C mode interface definition.
void terminal_send(volatile char c)
send character to terminal
Definition: terminal.c:316
static void command_reset(void *argument)
command to reset board
Definition: application.c:197
static const struct menu_command_t menu_commands[]
command to reboot into bootloader
Definition: application.c:125
void busvoodoo_oled_setup(void)
setup OLED display
void led_off(void)
switch off board LED
Definition: global.c:82
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:239
struct busvoodoo_mode_t busvoodoo_hiz_mode
HiZ mode interface definition.
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:463
global definitions and methods (API)
enum menu_argument_t argument
what kind of argument it accepts
Definition: menu.h:35
terminal prompt interface (API)
static struct busvoodoo_mode_t * busvoodoo_mode
current BusVoodoo mode
Definition: application.c:54
static void switch_mode(struct busvoodoo_mode_t *mode)
switch BusVoddoo mode
Definition: application.c:79
bool busvoodoo_full
is the BusVoodoo board fully populated (with HV voltage regulator, RS-232, RS-485, CAN transceiver on the back side)
void usb_cdcacm_setup(void)
setup USB CDC ACM peripheral
Definition: usb_cdcacm.c:443
char * name
complete name of the command (space-free)
Definition: menu.h:33
void terminal_setup(void)
initialize terminal prompt
Definition: terminal.c:304
BusVoodoo SPI mode (API)
volatile bool button_flag
flag set when board user button has been pressed/released
Definition: global.c:36
struct busvoodoo_mode_t busvoodoo_spi_mode
SPI mode interface definition.
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:191
BusVoodoo UART mode (API)
char user_input_get(void)
get user input
Definition: global.c:187
const uint8_t busvoodoo_global_commands_nb
number supported commands
struct busvoodoo_mode_t busvoodoo_uart_mode
UART mode interface definition.
void(* exit)(void)
function to exit from mode (i.e.
static void command_help(void *argument)
command to show help
Definition: application.c:160
void sleep_ms(uint32_t duration)
go to sleep for some milliseconds
Definition: global.c:159
const uint8_t commands_nb
number of menu commands provided by mode
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:56
#define LENGTH(x)
get the length of an array
Definition: global.h:26
static struct busvoodoo_mode_t * busvoodoo_modes[]
all supported BusVoodoo modes
Definition: application.c:46
BusVoodoo high impedance (HiZ) default mode (API)
static void command_mode(void *argument)
command to select mode
Definition: application.c:169
void busvoodoo_oled_clear(void)
clear display buffer
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:37
void busvoodoo_safe_state(void)
set safe state by disabling all outputs
BusVoodoo mode interface.
void busvoodoo_setup(void)
setup BusVoodoo board
void uart_setup(void)
setup UART peripheral
Definition: uart.c:50
BusVoodoo I²C mode (API)
size_t putc(char c)
print a single character on user output
Definition: application.c:58
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