STM32F4DoorControl/main.c

187 lines
4.4 KiB
C
Raw Normal View History

2021-02-13 19:39:00 +00:00
#include <libopencm3/cm3/nvic.h>
#include <libopencm3/cm3/systick.h>
#include <libopencm3/cm3/scb.h>
2021-01-31 18:26:40 +00:00
#include <libopencm3/stm32/rcc.h>
2021-02-13 19:39:00 +00:00
#include <stddef.h>
#include <stdio.h>
2021-01-31 18:26:40 +00:00
2021-02-13 19:39:00 +00:00
#include "adc.h"
2021-02-19 17:01:56 +00:00
#include "buttons.h"
2021-01-31 18:26:40 +00:00
#include "usb.h"
2021-02-13 19:39:00 +00:00
#include "ringbuffer.h"
#include "uart.h"
#include "encoder.h"
static const uint32_t button_time = 100;
2021-02-13 19:39:00 +00:00
volatile uint32_t tick = 0;
RINGBUFFER_STORAGE(usb_to_uart_buf, 64)
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;
2021-02-13 19:39:00 +00:00
static void sys_tick_setup(void);
static void fast_reset(void);
static void start_bootloader(void);
static void erase_app(void);
2021-02-13 19:39:00 +00:00
static void sys_tick_setup() {
systick_set_reload(168000/2-1);
2021-02-13 19:39:00 +00:00
systick_set_clocksource(STK_CSR_CLKSOURCE_AHB);
systick_counter_enable();
systick_interrupt_enable();
}
void sys_tick_handler() {
tick++;
}
2021-01-31 18:26:40 +00:00
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();
}
2021-01-31 18:26:40 +00:00
int main(void) {
2021-02-13 19:39:00 +00:00
#if 0
rcc_clock_setup_pll(&rcc_hse_25mhz_3v3[RCC_CLOCK_3V3_84MHZ]);
#else
2021-02-19 17:00:15 +00:00
rcc_clock_setup_pll(&rcc_hse_12mhz_3v3[RCC_CLOCK_3V3_84MHZ]);
2021-02-13 19:39:00 +00:00
#endif
RINGBUFFER_INIT(uart_to_usb_buf, 64);
RINGBUFFER_INIT(usb_to_uart_buf, 64);
RINGBUFFER_INIT(comm_in_buf, 64);
RINGBUFFER_INIT(comm_out_buf, 64);
2021-01-31 18:26:40 +00:00
sys_tick_setup();
while(tick < 100)
;
2021-02-13 19:39:00 +00:00
adc_setup();
uart_setup();
usb_setup();
2021-02-19 17:01:56 +00:00
buttons_setup();
encoder_setup();
2021-02-13 19:39:00 +00:00
nvic_enable_irq(NVIC_USART3_IRQ);
2021-01-31 18:26:40 +00:00
2021-02-19 17:01:56 +00:00
uint32_t last_tick = tick;
int last_pos = 0;
int watchdog = 1;
int watchdog_counter = 0;
int print_changes = 0;
2021-01-31 18:26:40 +00:00
while (1) {
2021-02-13 19:39:00 +00:00
/* Handle control messages through the comms CDC */
char buf[64];
unsigned buf_len = 0;
2021-02-19 17:01:56 +00:00
const bool no_comms = watchdog && ringbuffer_empty(usb_to_uart_buf) && ringbuffer_empty(comm_in_buf);
if (!no_comms) {
watchdog_counter = 0;
}
2021-02-19 17:01:56 +00:00
while (tick != last_tick) {
buttons_tick();
last_tick++;
watchdog_counter += no_comms;
}
if (watchdog_counter >= 35000) {
fast_reset();
2021-02-19 17:01:56 +00:00
}
for (char c; ringbuffer_get(comm_in_buf, (void *)&c, 1);) {
2021-02-13 19:39:00 +00:00
switch (c) {
case 'B': // Battery
2021-02-19 17:01:56 +00:00
printf("%lu", (unsigned long)adc_bat_voltage());
break;
case 'O': // Open
buttons_open(button_time);
2021-02-19 17:01:56 +00:00
break;
case 'C': // Close
buttons_close(button_time);
break;
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();
2021-02-19 17:01:56 +00:00
break;
2021-02-13 19:39:00 +00:00
}
}
int pos = encoder_get();
if (pos != last_pos && ringbuffer_empty(comm_out_buf)) {
if (print_changes) {
printf("pos: %d", pos);
}
last_pos = pos;
}
2021-02-13 19:39:00 +00:00
/* Send replies */
if ((buf_len = ringbuffer_peek(comm_out_buf, &buf[0], sizeof buf))) {
2021-02-19 17:01:56 +00:00
if (usb_write_cdcacm(ACM_COMM, (void *)buf, buf_len, 1)) {
2021-02-13 19:39:00 +00:00
ringbuffer_skip(comm_out_buf, buf_len);
}
}
/* Send any available data to the NFC CDC */
if (!ringbuffer_empty(uart_to_usb_buf)) {
unsigned len = ringbuffer_peek(uart_to_usb_buf, &buf[0], sizeof buf);
2021-02-19 17:01:56 +00:00
if (usb_write_cdcacm(ACM_NFC, &buf[0], len, 1)) {
2021-02-13 19:39:00 +00:00
ringbuffer_skip(uart_to_usb_buf, len);
}
}
if (!ringbuffer_empty(usb_to_uart_buf)) {
usart_enable_tx_interrupt(USART3);
}
usbd_poll(g_usbd_dev);
2021-01-31 18:26:40 +00:00
__asm__("wfi");
}
}
2021-02-13 19:39:00 +00:00
void usart3_isr() {
while (USART_SR(USART3) & USART_SR_RXNE) {
uint8_t c = usart_recv(USART3);
ringbuffer_add(uart_to_usb_buf, (void *)&c, 1);
}
while (USART_SR(USART3) & USART_SR_TXE) {
if (ringbuffer_empty(usb_to_uart_buf)) {
usart_disable_tx_interrupt(USART3);
break;
} else {
uint8_t c;
ringbuffer_get(usb_to_uart_buf, &c, 1);
usart_send(USART3, c);
}
}
}