STM32F4DoorControl/main.c

116 lines
2.7 KiB
C

#include <libopencm3/cm3/nvic.h>
#include <libopencm3/cm3/systick.h>
#include <libopencm3/stm32/rcc.h>
#include <stddef.h>
#include <stdio.h>
#include "adc.h"
#include "buttons.h"
#include "usb.h"
#include "ringbuffer.h"
#include "uart.h"
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)
static void sys_tick_setup(void);
static void sys_tick_setup() {
systick_set_reload(168000);
systick_set_clocksource(STK_CSR_CLKSOURCE_AHB);
systick_counter_enable();
systick_interrupt_enable();
}
void sys_tick_handler() {
tick++;
}
int main(void) {
#if 0
rcc_clock_setup_pll(&rcc_hse_25mhz_3v3[RCC_CLOCK_3V3_84MHZ]);
#else
rcc_clock_setup_pll(&rcc_hse_12mhz_3v3[RCC_CLOCK_3V3_84MHZ]);
#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);
usb_setup();
adc_setup();
uart_setup();
sys_tick_setup();
buttons_setup();
nvic_enable_irq(NVIC_USART3_IRQ);
uint32_t last_tick = tick;
while (1) {
/* Handle control messages through the comms CDC */
char buf[64];
unsigned buf_len = 0;
while (tick != last_tick) {
buttons_tick();
last_tick++;
}
for (char c; ringbuffer_get(comm_in_buf, (void *)&c, 1);) {
switch (c) {
case 'B':
printf("%lu", (unsigned long)adc_bat_voltage());
break;
case 'O':
buttons_open(15);
break;
case 'C':
buttons_close(15);
break;
}
}
/* Send replies */
if ((buf_len = ringbuffer_peek(comm_out_buf, &buf[0], sizeof buf))) {
if (usb_write_cdcacm(ACM_COMM, (void *)buf, buf_len, 1)) {
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);
if (usb_write_cdcacm(ACM_NFC, &buf[0], len, 1)) {
ringbuffer_skip(uart_to_usb_buf, len);
}
}
if (!ringbuffer_empty(usb_to_uart_buf)) {
usart_enable_tx_interrupt(USART3);
}
usbd_poll(g_usbd_dev);
__asm__("wfi");
}
}
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);
}
}
}