USB watchdog, resets, reporting

USB watchdog: Restart if no comms for too long (toggleable)
Resets: Allow various resets to communicate with the bootloader
Reporting: Allow toggling instant reporting of position changes
main
Valentin Ochs 2021-07-16 21:19:53 +02:00
parent 7ea4494345
commit 51a95e7ffb
1 changed files with 64 additions and 8 deletions

72
main.c
View File

@ -1,5 +1,6 @@
#include <libopencm3/cm3/nvic.h>
#include <libopencm3/cm3/systick.h>
#include <libopencm3/cm3/scb.h>
#include <libopencm3/stm32/rcc.h>
#include <stddef.h>
#include <stdio.h>
@ -19,10 +20,15 @@ RINGBUFFER_STORAGE(uart_to_usb_buf, 64)
RINGBUFFER_STORAGE(comm_in_buf, 64)
RINGBUFFER_STORAGE(comm_out_buf, 64)
extern uint32_t *_board_dfu_dbl_tap;
static void sys_tick_setup(void);
static void fast_reset(void);
static void start_bootloader(void);
static void erase_app(void);
static void sys_tick_setup() {
systick_set_reload(168000);
systick_set_reload(168000/2-1);
systick_set_clocksource(STK_CSR_CLKSOURCE_AHB);
systick_counter_enable();
systick_interrupt_enable();
@ -32,6 +38,19 @@ void sys_tick_handler() {
tick++;
}
void fast_reset() {
*_board_dfu_dbl_tap = 0xf02669ef;
scb_reset_system();
}
void start_bootloader() {
*_board_dfu_dbl_tap = 0xf01669ef;
scb_reset_system();
}
void erase_app() {
*_board_dfu_dbl_tap = 0xf5e80ab4;
scb_reset_system();
}
int main(void) {
#if 0
rcc_clock_setup_pll(&rcc_hse_25mhz_3v3[RCC_CLOCK_3V3_84MHZ]);
@ -44,10 +63,16 @@ int main(void) {
RINGBUFFER_INIT(comm_in_buf, 64);
RINGBUFFER_INIT(comm_out_buf, 64);
usb_setup();
sys_tick_setup();
while(tick < 100)
;
adc_setup();
uart_setup();
sys_tick_setup();
usb_setup();
buttons_setup();
encoder_setup();
@ -55,36 +80,67 @@ int main(void) {
uint32_t last_tick = tick;
int last_pos = 0;
int watchdog = 1;
int watchdog_counter = 0;
int print_changes = 0;
while (1) {
/* Handle control messages through the comms CDC */
char buf[64];
unsigned buf_len = 0;
const bool no_comms = watchdog && ringbuffer_empty(usb_to_uart_buf) && ringbuffer_empty(comm_in_buf);
if (!no_comms) {
watchdog_counter = 0;
}
while (tick != last_tick) {
buttons_tick();
last_tick++;
watchdog_counter += no_comms;
}
if (watchdog_counter >= 35000) {
fast_reset();
}
for (char c; ringbuffer_get(comm_in_buf, (void *)&c, 1);) {
switch (c) {
case 'B':
case 'B': // Battery
printf("%lu", (unsigned long)adc_bat_voltage());
break;
case 'O':
case 'O': // Open
buttons_open(button_time);
break;
case 'C':
case 'C': // Close
buttons_close(button_time);
break;
case 'R':
case 'R': // Report
printf("pos: %d", encoder_get());
printf("tick: %lu", tick);
break;
case 'r': // toggle reporting
print_changes = !print_changes;
printf("pos reporting: %d", print_changes);
break;
case 'W': // toggle watchdog
watchdog = !watchdog;
printf("watchdog: %d", watchdog);
break;
case 'F': // Flash
start_bootloader();
break;
case 'E': // Erase
erase_app();
break;
case 'S': // reStart
fast_reset();
break;
}
}
int pos = encoder_get();
if (pos != last_pos && ringbuffer_empty(comm_out_buf)) {
printf("pos: %d", pos);
if (print_changes) {
printf("pos: %d", pos);
}
last_pos = pos;
}