Compare commits
No commits in common. "bb32eb661467856faf9872097f39abee9fb342d5" and "1c9ac7382dc7d8fbfed76ed265f6ef8f44757b4c" have entirely different histories.
bb32eb6614
...
1c9ac7382d
1
.gitignore
vendored
1
.gitignore
vendored
|
@ -1,3 +1,4 @@
|
|||
*.ld
|
||||
*.o
|
||||
*.d
|
||||
*.elf
|
||||
|
|
25
Makefile
25
Makefile
|
@ -3,34 +3,15 @@ OOCD_INTERFACE ?= stlink-v2
|
|||
OOCD_TARGET ?= stm32f4x
|
||||
BMP_PORT ?= /dev/ttyBmpGdb
|
||||
|
||||
COMFLAGS=-g -Og -pedantic -Wall
|
||||
COMFLAGS=-g -Os -pedantic -Wall
|
||||
|
||||
BOOTLOADER ?= no
|
||||
|
||||
ifeq ($(BOOTLOADER),yes)
|
||||
LIBNAME = opencm3_stm32f4
|
||||
FP_FLAGS ?= -mfloat-abi=hard -mfpu=fpv4-sp-d16
|
||||
ARCH_FLAGS = -mthumb -mcpu=cortex-m4 $(FP_FLAGS)
|
||||
DEVDEFS = -DSTM32F4 -DSTM32F4CCM -DSTM32F405RG -D_ROM=1008K -D_RAM=128K -D_CCM=64K -D_CCM_OFF=0x10000000 -D_ROM_OFF=0x08004000 -D_RAM_OFF=0x20000000 -DBOOTLOADER
|
||||
LDSCRIPT = linker.ld
|
||||
else
|
||||
DEVICE = STM32F405RG
|
||||
endif
|
||||
CFLAGS=$(COMFLAGS) $(DEVDEFS) -std=c99 -D_POSIX_C_SOURCE=200809L
|
||||
CFLAGS=$(COMFLAGS) -std=c99 -D_POSIX_C_SOURCE=200809L
|
||||
CXXFLAGS=$(COMFLAGS) -std=c++17
|
||||
|
||||
|
||||
OBJS = usb.o adc.o ringbuffer.o uart.o buttons.o printf.o encoder.o
|
||||
BINARY ?= main
|
||||
DEVICE=STM32F405RG
|
||||
|
||||
printf.o: CFLAGS:=$(CFLAGS) -Wno-parentheses -Wno-char-subscripts -Wno-sign-compare -Wno-implicit-fallthrough
|
||||
|
||||
include ../rules.mk
|
||||
|
||||
uf2: main.uf2
|
||||
|
||||
main.uf2: main.bin
|
||||
uf2conv.py -c -b 0x4000 -o $@ $<
|
||||
|
||||
|
||||
.PHONY: uf2
|
||||
|
|
2
adc.c
2
adc.c
|
@ -20,7 +20,7 @@ void adc_setup(void) {
|
|||
|
||||
uint32_t adc_bat_voltage(void) {
|
||||
if (adc_get_flag(ADC1, ADC_SR_EOC)) {
|
||||
bat_voltage_mv = adc_read_regular(ADC1) * 3300 / 2048;
|
||||
bat_voltage_mv = adc_read_regular(ADC1);
|
||||
}
|
||||
return bat_voltage_mv;
|
||||
}
|
||||
|
|
32
buttons.c
32
buttons.c
|
@ -4,35 +4,28 @@
|
|||
#include <stdio.h>
|
||||
#include "buttons.h"
|
||||
|
||||
#define OPEN_RCC RCC_GPIOB
|
||||
#define OPEN_PORT GPIOB
|
||||
#define OPEN_BUTTON GPIO9
|
||||
#define CLOSE_RCC RCC_GPIOC
|
||||
#define CLOSE_PORT GPIOC
|
||||
#define CLOSE_BUTTON GPIO3
|
||||
#define BUTTON_RCC RCC_GPIOA
|
||||
#define BUTTON_PORT GPIOA
|
||||
#define OPEN_BUTTON GPIO6
|
||||
#define CLOSE_BUTTON GPIO7
|
||||
|
||||
void buttons_setup() {
|
||||
#define SETUP(BTN) \
|
||||
rcc_periph_clock_enable(BTN ## _RCC); \
|
||||
gpio_mode_setup(BTN ## _PORT, GPIO_MODE_OUTPUT, GPIO_PUPD_NONE, BTN ## _BUTTON); \
|
||||
gpio_set_output_options(BTN ## _PORT, GPIO_OTYPE_OD, GPIO_OSPEED_2MHZ, BTN ## _BUTTON);
|
||||
SETUP(OPEN);
|
||||
SETUP(CLOSE);
|
||||
#undef SETUP
|
||||
buttons_release();
|
||||
rcc_periph_clock_enable(BUTTON_RCC);
|
||||
gpio_mode_setup(BUTTON_PORT, GPIO_MODE_OUTPUT, GPIO_PUPD_NONE, OPEN_BUTTON | CLOSE_BUTTON);
|
||||
gpio_set_output_options(BUTTON_PORT, GPIO_OTYPE_OD, GPIO_OSPEED_2MHZ, OPEN_BUTTON | CLOSE_BUTTON);
|
||||
}
|
||||
|
||||
static uint32_t open_ticks = 0;
|
||||
static uint32_t close_ticks = 0;
|
||||
|
||||
void buttons_open(uint32_t ticks) {
|
||||
gpio_clear(OPEN_PORT, OPEN_BUTTON);
|
||||
gpio_clear(BUTTON_PORT, OPEN_BUTTON);
|
||||
printf("Open %lu", ticks);
|
||||
open_ticks = ticks;
|
||||
}
|
||||
|
||||
void buttons_close(uint32_t ticks) {
|
||||
gpio_clear(CLOSE_PORT, CLOSE_BUTTON);
|
||||
gpio_clear(BUTTON_PORT, CLOSE_BUTTON);
|
||||
printf("Close %lu", ticks);
|
||||
close_ticks = ticks;
|
||||
}
|
||||
|
@ -40,15 +33,14 @@ void buttons_close(uint32_t ticks) {
|
|||
void buttons_release(void) {
|
||||
open_ticks = close_ticks = 0;
|
||||
printf("Release");
|
||||
gpio_set(OPEN_PORT, OPEN_BUTTON);
|
||||
gpio_set(CLOSE_PORT, CLOSE_BUTTON);
|
||||
gpio_set(BUTTON_PORT, OPEN_BUTTON | CLOSE_BUTTON);
|
||||
}
|
||||
|
||||
void buttons_tick(void) {
|
||||
if (open_ticks) {
|
||||
open_ticks--;
|
||||
if (!open_ticks) {
|
||||
gpio_set(OPEN_PORT, OPEN_BUTTON);
|
||||
gpio_set(BUTTON_PORT, OPEN_BUTTON);
|
||||
printf("Release open");
|
||||
}
|
||||
}
|
||||
|
@ -56,7 +48,7 @@ void buttons_tick(void) {
|
|||
close_ticks--;
|
||||
if (!close_ticks) {
|
||||
printf("Release close");
|
||||
gpio_set(CLOSE_PORT, CLOSE_BUTTON);
|
||||
gpio_set(BUTTON_PORT, CLOSE_BUTTON);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
204
encoder.c
204
encoder.c
|
@ -2,203 +2,73 @@
|
|||
#include <libopencm3/stm32/exti.h>
|
||||
#include <libopencm3/stm32/gpio.h>
|
||||
#include <libopencm3/stm32/rcc.h>
|
||||
#include <stddef.h>
|
||||
#include <stdint.h>
|
||||
#include <stdio.h>
|
||||
#include <stddef.h>
|
||||
#include "encoder.h"
|
||||
#include "systick.h"
|
||||
|
||||
#define USE_CORRECTION
|
||||
#define FIXED_OFFSET 20
|
||||
// #define DEBUG_ENCODER_TICKS
|
||||
#define SPEED_AVG_NUM 4
|
||||
static volatile int pos;
|
||||
|
||||
#ifdef DEBUG_ENCODER_TICKS
|
||||
#define LOG_ENCODER_DEBUG_TICKS(...) printf(__VA_ARGS__)
|
||||
#else
|
||||
#define LOG_ENCODER_DEBUG_TICKS(...)
|
||||
#endif
|
||||
|
||||
static volatile int pos = 0;
|
||||
static int offset =
|
||||
#ifdef FIXED_OFFSET
|
||||
FIXED_OFFSET
|
||||
#else
|
||||
-1
|
||||
#endif
|
||||
;
|
||||
static volatile int speed = 0;
|
||||
static void check(void);
|
||||
|
||||
static uint32_t last_tick = 0;
|
||||
static struct tick_pos {
|
||||
uint32_t tick;
|
||||
int pos;
|
||||
} speed_ticks[SPEED_AVG_NUM + 1] = {0};
|
||||
static uint32_t ticks[3] = {0};
|
||||
|
||||
#define MARKS 96
|
||||
#define PIN_OPEN GPIO1
|
||||
#define PIN_OPEN_IRQ NVIC_EXTI1_IRQ
|
||||
#define PIN_CLOSE GPIO2
|
||||
#define PIN_CLOSE_IRQ NVIC_EXTI2_IRQ
|
||||
|
||||
#define PIN_PORT GPIOC
|
||||
#define PIN_RCC RCC_GPIOC
|
||||
#define PIN1 GPIO4
|
||||
#define PIN1_IRQ NVIC_EXTI4_IRQ
|
||||
#define PIN2 GPIO5
|
||||
#define PIN2_IRQ NVIC_EXTI9_5_IRQ
|
||||
|
||||
void encoder_setup(void) {
|
||||
rcc_periph_clock_enable(PIN_RCC);
|
||||
rcc_periph_clock_enable(RCC_SYSCFG);
|
||||
rcc_periph_clock_enable(RCC_GPIOA);
|
||||
|
||||
gpio_mode_setup(PIN_PORT, GPIO_MODE_INPUT, GPIO_PUPD_NONE, PIN_OPEN | PIN_CLOSE);
|
||||
gpio_mode_setup(GPIOA, GPIO_MODE_INPUT, GPIO_PUPD_NONE, PIN1 | PIN2);
|
||||
|
||||
exti_select_source(PIN_OPEN, PIN_PORT);
|
||||
exti_select_source(PIN_CLOSE, PIN_PORT);
|
||||
exti_set_trigger(PIN_OPEN, EXTI_TRIGGER_BOTH);
|
||||
exti_set_trigger(PIN_CLOSE, EXTI_TRIGGER_BOTH);
|
||||
exti_enable_request(PIN_OPEN);
|
||||
exti_enable_request(PIN_CLOSE);
|
||||
/* Set up exti on A0 & A1 */
|
||||
exti_select_source(PIN1 | PIN2, GPIOA);
|
||||
exti_set_trigger(PIN1 | PIN2, EXTI_TRIGGER_BOTH);
|
||||
exti_enable_request(PIN1 | PIN2);
|
||||
|
||||
nvic_enable_irq(PIN_OPEN_IRQ);
|
||||
nvic_enable_irq(PIN_CLOSE_IRQ);
|
||||
|
||||
// We've configured it, now we can turn it off again
|
||||
rcc_periph_clock_disable(RCC_SYSCFG);
|
||||
nvic_enable_irq(PIN1_IRQ);
|
||||
nvic_enable_irq(PIN2_IRQ);
|
||||
}
|
||||
|
||||
int encoder_get() {
|
||||
return pos;
|
||||
}
|
||||
|
||||
int encoder_speed() {
|
||||
return speed;
|
||||
}
|
||||
|
||||
int encoder_current_speed() {
|
||||
int dt = ticks_ms(tick - speed_ticks[SPEED_AVG_NUM - 1].tick);
|
||||
if (dt > 200) {
|
||||
printf("dt: %d", dt);
|
||||
return 0;
|
||||
}
|
||||
return 1000
|
||||
* (speed_ticks[SPEED_AVG_NUM-1].pos - speed_ticks[0].pos)
|
||||
/ (int)ticks_ms(tick - speed_ticks[0].tick);
|
||||
}
|
||||
|
||||
static int prev = 0;
|
||||
void check(void) {
|
||||
static int prev = 0;
|
||||
int now = GPIO_IDR(PIN_PORT);
|
||||
int diff = 0;
|
||||
now = (!!(now & PIN_OPEN))<<0 | (!!(now & PIN_CLOSE)) << 1;
|
||||
int pp = pos;
|
||||
int now = GPIO_IDR(GPIOA);
|
||||
now = (!!(now & PIN1))<<0 | (!!(now & PIN2)) << 1;
|
||||
|
||||
#define NEITHER 0
|
||||
#define JUST_OPEN 1
|
||||
#define JUST_CLOSE 2
|
||||
#define BOTH 3
|
||||
#define WAS(x) ((x)<<2)
|
||||
#define IS(x) (x)
|
||||
|
||||
switch (WAS(prev) + IS(now)) {
|
||||
case WAS(NEITHER) + IS(JUST_CLOSE):
|
||||
case WAS(JUST_OPEN) + IS(NEITHER):
|
||||
case WAS(JUST_CLOSE) + IS(BOTH):
|
||||
case WAS(BOTH) + IS(JUST_OPEN):
|
||||
diff = -1;
|
||||
switch (prev << 2 | now) {
|
||||
case 0 * 4 + 2:
|
||||
case 1 * 4 + 0:
|
||||
case 2 * 4 + 3:
|
||||
case 3 * 4 + 1:
|
||||
pos++;
|
||||
break;
|
||||
case WAS(NEITHER) + IS(JUST_OPEN):
|
||||
case WAS(JUST_OPEN) + IS(BOTH):
|
||||
case WAS(BOTH) + IS(JUST_CLOSE):
|
||||
case WAS(JUST_CLOSE) + IS(NEITHER):
|
||||
diff = +1;
|
||||
case 0 * 4 + 1:
|
||||
case 1 * 4 + 3:
|
||||
case 2 * 4 + 0:
|
||||
case 3 * 4 + 2:
|
||||
pos--;
|
||||
break;
|
||||
default:
|
||||
prev = now;
|
||||
return;
|
||||
}
|
||||
|
||||
speed_ticks[SPEED_AVG_NUM].pos += diff;
|
||||
pos += diff;
|
||||
|
||||
const int tick_diff = tick - last_tick;
|
||||
if (now == BOTH) {
|
||||
ticks[0] = tick_diff;
|
||||
} else if (prev == BOTH) {
|
||||
ticks[1] = tick_diff;
|
||||
} else if (now == NEITHER) {
|
||||
for (int i = 0; i < SPEED_AVG_NUM; i++) {
|
||||
speed_ticks[i] = speed_ticks[i+1];
|
||||
}
|
||||
speed_ticks[SPEED_AVG_NUM - 1].tick = tick;
|
||||
speed = 1000
|
||||
* (speed_ticks[SPEED_AVG_NUM-1].pos - speed_ticks[0].pos)
|
||||
/ (int)ticks_ms(speed_ticks[SPEED_AVG_NUM-1].tick - speed_ticks[0].tick);
|
||||
|
||||
#ifdef USE_CORRECTION
|
||||
ticks[2] = tick_diff;
|
||||
|
||||
LOG_ENCODER_DEBUG_TICKS("%3d %4lu %4lu %4lu (%lu)", pos, ticks[0], ticks[1], ticks[2], offset);
|
||||
|
||||
const uint32_t mn = us_ticks(4500),
|
||||
mx = us_ticks(7000);
|
||||
if (ticks[0] >= mn &&
|
||||
ticks[0] <= mx &&
|
||||
ticks[2] >= mn &&
|
||||
ticks[2] <= mx &&
|
||||
ticks[1] >= (ticks[0] + ticks[2]) &&
|
||||
ticks[1] <= (ticks[0] + ticks[2]) * 3 / 2) {
|
||||
if (offset == -1) {
|
||||
#ifndef FIXED_OFFSET
|
||||
pos = 2*diff;
|
||||
offset = 0;
|
||||
#else
|
||||
pos = FIXED_OFFSET;
|
||||
#endif
|
||||
} else {
|
||||
int pp = pos;
|
||||
pos -= 2*diff + offset;
|
||||
pos = (pos + (diff > 0 ? MARKS - 1 : 0)) / MARKS * MARKS;
|
||||
pos += 2*diff + offset;
|
||||
|
||||
if (pp != pos) {
|
||||
LOG_ENCODER_DEBUG_TICKS("changed pos: %d -> %d", pp, pos);
|
||||
} else {
|
||||
LOG_ENCODER_DEBUG_TICKS("match");
|
||||
}
|
||||
}
|
||||
}
|
||||
#endif
|
||||
}
|
||||
|
||||
if (pos < -1) {
|
||||
#ifndef FIXED_OFFSET
|
||||
offset = (offset - pos + 1) % MARKS;
|
||||
pos = -1;
|
||||
#else
|
||||
pos = FIXED_OFFSET;
|
||||
#endif
|
||||
}
|
||||
|
||||
last_tick = tick;
|
||||
|
||||
#undef NEITHER
|
||||
#undef JUST_OPEN
|
||||
#undef JUST_CLOSE
|
||||
#undef BOTH
|
||||
#undef WAS
|
||||
#undef IS
|
||||
|
||||
prev = now;
|
||||
if (pos < MIN_POS)
|
||||
pos = MIN_POS;
|
||||
if (pos > MAX_POS)
|
||||
pos = MAX_POS;
|
||||
}
|
||||
|
||||
void exti1_isr(void) {
|
||||
exti_reset_request(EXTI1);
|
||||
check();
|
||||
}
|
||||
void exti2_isr(void) {
|
||||
exti_reset_request(EXTI2);
|
||||
check();
|
||||
}
|
||||
void exti4_isr(void) {
|
||||
exti_reset_request(EXTI4);
|
||||
check();
|
||||
}
|
||||
void exti9_5_isr(void) {
|
||||
exti_reset_request(EXTI5);
|
||||
check();
|
||||
}
|
||||
|
|
|
@ -8,6 +8,4 @@
|
|||
|
||||
void encoder_setup(void);
|
||||
int encoder_get(void);
|
||||
int encoder_current_speed(void);
|
||||
int encoder_speed(void);
|
||||
#endif
|
||||
|
|
78
linker.ld
78
linker.ld
|
@ -1,78 +0,0 @@
|
|||
EXTERN(vector_table)
|
||||
ENTRY(reset_handler)
|
||||
|
||||
_estack = ORIGIN(ram) + LENGTH(ram);
|
||||
_board_dfu_dbl_tap = _estack;
|
||||
|
||||
MEMORY
|
||||
{
|
||||
ram (rwx) : ORIGIN = 0x20000000, LENGTH = 128K - 4
|
||||
rom (rx) : ORIGIN = 0x08004000, LENGTH = 1024K - 16K
|
||||
ccm (rwx) : ORIGIN = 0x10000000, LENGTH = 64K
|
||||
}
|
||||
SECTIONS
|
||||
{
|
||||
.text : {
|
||||
*(.vectors)
|
||||
*(.text*)
|
||||
. = ALIGN(4);
|
||||
*(.rodata*)
|
||||
. = ALIGN(4);
|
||||
} >rom
|
||||
.preinit_array : {
|
||||
. = ALIGN(4);
|
||||
__preinit_array_start = .;
|
||||
KEEP (*(.preinit_array))
|
||||
__preinit_array_end = .;
|
||||
} >rom
|
||||
.init_array : {
|
||||
. = ALIGN(4);
|
||||
__init_array_start = .;
|
||||
KEEP (*(SORT(.init_array.*)))
|
||||
KEEP (*(.init_array))
|
||||
__init_array_end = .;
|
||||
} >rom
|
||||
.fini_array : {
|
||||
. = ALIGN(4);
|
||||
__fini_array_start = .;
|
||||
KEEP (*(.fini_array))
|
||||
KEEP (*(SORT(.fini_array.*)))
|
||||
__fini_array_end = .;
|
||||
} >rom
|
||||
.ARM.extab : {
|
||||
*(.ARM.extab*)
|
||||
} >rom
|
||||
.ARM.exidx : {
|
||||
__exidx_start = .;
|
||||
*(.ARM.exidx*)
|
||||
__exidx_end = .;
|
||||
} >rom
|
||||
. = ALIGN(4);
|
||||
_etext = .;
|
||||
.noinit (NOLOAD) : {
|
||||
*(.noinit*)
|
||||
} >ram
|
||||
. = ALIGN(4);
|
||||
.data : {
|
||||
_data = .;
|
||||
*(.data*)
|
||||
*(.ramtext*)
|
||||
. = ALIGN(4);
|
||||
_edata = .;
|
||||
} >ram AT >rom
|
||||
_data_loadaddr = LOADADDR(.data);
|
||||
.bss : {
|
||||
*(.bss*)
|
||||
*(COMMON)
|
||||
. = ALIGN(4);
|
||||
_ebss = .;
|
||||
} >ram
|
||||
.ccm : {
|
||||
*(.ccmram*)
|
||||
. = ALIGN(4);
|
||||
} >ccm
|
||||
/DISCARD/ : { *(.eh_frame) }
|
||||
. = ALIGN(4);
|
||||
end = .;
|
||||
}
|
||||
PROVIDE(_stack = ORIGIN(ram) + LENGTH(ram));
|
97
main.c
97
main.c
|
@ -1,6 +1,5 @@
|
|||
#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>
|
||||
|
@ -11,7 +10,6 @@
|
|||
#include "ringbuffer.h"
|
||||
#include "uart.h"
|
||||
#include "encoder.h"
|
||||
#include "systick.h"
|
||||
|
||||
static const uint32_t button_time = 100;
|
||||
|
||||
|
@ -23,17 +21,8 @@ RINGBUFFER_STORAGE(comm_out_buf, 64)
|
|||
|
||||
static void sys_tick_setup(void);
|
||||
|
||||
#if defined(BOOTLOADER)
|
||||
extern uint32_t *_board_dfu_dbl_tap;
|
||||
|
||||
static void start_bootloader(void);
|
||||
static void erase_app(void);
|
||||
#endif
|
||||
|
||||
static void fast_reset(void);
|
||||
|
||||
static void sys_tick_setup() {
|
||||
systick_set_reload(rcc_ahb_frequency / SYSTICK_FREQUENCY - 1);
|
||||
systick_set_reload(168000);
|
||||
systick_set_clocksource(STK_CSR_CLKSOURCE_AHB);
|
||||
systick_counter_enable();
|
||||
systick_interrupt_enable();
|
||||
|
@ -43,24 +32,6 @@ void sys_tick_handler() {
|
|||
tick++;
|
||||
}
|
||||
|
||||
void fast_reset() {
|
||||
#if defined(BOOTLOADER)
|
||||
*_board_dfu_dbl_tap = 0xf02669ef;
|
||||
#endif
|
||||
scb_reset_system();
|
||||
}
|
||||
|
||||
#if defined(BOOTLOADER)
|
||||
void start_bootloader() {
|
||||
*_board_dfu_dbl_tap = 0xf01669ef;
|
||||
scb_reset_system();
|
||||
}
|
||||
void erase_app() {
|
||||
*_board_dfu_dbl_tap = 0xf5e80ab4;
|
||||
scb_reset_system();
|
||||
}
|
||||
#endif
|
||||
|
||||
int main(void) {
|
||||
#if 0
|
||||
rcc_clock_setup_pll(&rcc_hse_25mhz_3v3[RCC_CLOCK_3V3_84MHZ]);
|
||||
|
@ -73,12 +44,10 @@ int main(void) {
|
|||
RINGBUFFER_INIT(comm_in_buf, 64);
|
||||
RINGBUFFER_INIT(comm_out_buf, 64);
|
||||
|
||||
sys_tick_setup();
|
||||
|
||||
usb_setup();
|
||||
adc_setup();
|
||||
uart_setup();
|
||||
usb_setup();
|
||||
|
||||
sys_tick_setup();
|
||||
buttons_setup();
|
||||
encoder_setup();
|
||||
|
||||
|
@ -86,81 +55,39 @@ int main(void) {
|
|||
|
||||
uint32_t last_tick = tick;
|
||||
int last_pos = 0;
|
||||
int watchdog = 1;
|
||||
uint32_t watchdog_counter = 0;
|
||||
int print_changes = 0;
|
||||
|
||||
while (tick < 5000) {
|
||||
usbd_poll(g_usbd_dev);
|
||||
}
|
||||
|
||||
if (!usb_got_reset()) {
|
||||
fast_reset();
|
||||
}
|
||||
|
||||
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 >= s_ticks(35)) {
|
||||
fast_reset();
|
||||
}
|
||||
|
||||
for (char c; ringbuffer_get(comm_in_buf, (void *)&c, 1);) {
|
||||
switch (c) {
|
||||
case 'B': // Battery
|
||||
case 'B':
|
||||
printf("%lu", (unsigned long)adc_bat_voltage());
|
||||
break;
|
||||
case 'O': // Open
|
||||
case 'O':
|
||||
buttons_open(button_time);
|
||||
break;
|
||||
case 'C': // Close
|
||||
case 'C':
|
||||
buttons_close(button_time);
|
||||
break;
|
||||
case 'R': // Report
|
||||
printf("pos: %d %d", encoder_get(), encoder_current_speed());
|
||||
case 'R':
|
||||
printf("pos: %d", encoder_get());
|
||||
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 'S': // reStart
|
||||
fast_reset();
|
||||
break;
|
||||
#if defined(BOOTLOADER)
|
||||
case 'F': // Flash
|
||||
start_bootloader();
|
||||
break;
|
||||
case 'E': // Erase
|
||||
erase_app();
|
||||
break;
|
||||
#endif
|
||||
}
|
||||
}
|
||||
|
||||
int pos = encoder_get();
|
||||
if (pos != last_pos) {
|
||||
if (ringbuffer_empty(comm_out_buf)) {
|
||||
if (print_changes) {
|
||||
printf("pos: %d %d", pos, encoder_speed());
|
||||
}
|
||||
if (pos != last_pos && ringbuffer_empty(comm_out_buf)) {
|
||||
printf("pos: %d", pos);
|
||||
last_pos = pos;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/* Send replies */
|
||||
if ((buf_len = ringbuffer_peek(comm_out_buf, &buf[0], sizeof buf))) {
|
||||
|
@ -181,7 +108,7 @@ int main(void) {
|
|||
}
|
||||
|
||||
usbd_poll(g_usbd_dev);
|
||||
// __asm__("wfi");
|
||||
__asm__("wfi");
|
||||
}
|
||||
}
|
||||
|
||||
|
|
13
systick.h
13
systick.h
|
@ -1,13 +0,0 @@
|
|||
#ifndef SYSTICK_H
|
||||
#define SYSTICK_H
|
||||
#include <stdint.h>
|
||||
#define SYSTICK_FREQUENCY 10000
|
||||
|
||||
static inline uint32_t us_ticks(uint32_t us) { return us * SYSTICK_FREQUENCY / 1000000; }
|
||||
static inline uint32_t ms_ticks(uint32_t ms) { return ms * SYSTICK_FREQUENCY / 1000; }
|
||||
static inline uint32_t s_ticks(uint32_t s) { return s * SYSTICK_FREQUENCY; }
|
||||
static inline uint32_t ticks_ms(uint32_t ticks) { return ticks * 1000 / SYSTICK_FREQUENCY; }
|
||||
|
||||
extern volatile uint32_t tick;
|
||||
|
||||
#endif
|
21
usb.c
21
usb.c
|
@ -40,8 +40,6 @@ static char const *usb_strings[] = {
|
|||
[3 + ACM_NFC] = "RFID",
|
||||
};
|
||||
|
||||
static int got_reset = 0;
|
||||
|
||||
usbd_device *g_usbd_dev;
|
||||
|
||||
/* The descriptor for our device. We need only one of these. */
|
||||
|
@ -175,14 +173,6 @@ static void cdcacm_set_config(usbd_device *usbd_dev, uint16_t wValue) {
|
|||
cdcacm_control_request);
|
||||
}
|
||||
|
||||
void usb_reset_callback(void) {
|
||||
got_reset = 1;
|
||||
}
|
||||
|
||||
bool usb_got_reset(void) {
|
||||
return got_reset;
|
||||
}
|
||||
|
||||
void usb_setup() {
|
||||
rcc_periph_clock_enable(RCC_GPIOA);
|
||||
rcc_periph_clock_enable(RCC_OTGFS);
|
||||
|
@ -324,20 +314,15 @@ void usb_setup() {
|
|||
}
|
||||
|
||||
/* Initialize the device... */
|
||||
usb_reinit();
|
||||
|
||||
/* And use interrupts instead of polling. */
|
||||
// nvic_enable_irq(NVIC_OTG_FS_IRQ);
|
||||
}
|
||||
|
||||
void usb_reinit() {
|
||||
g_usbd_dev = usbd_init(&otgfs_usb_driver, &dev, &config,
|
||||
(char const * const *)usb_strings,
|
||||
(sizeof usb_strings) / sizeof(usb_strings[0]),
|
||||
usbd_control_buffer, sizeof(usbd_control_buffer));
|
||||
|
||||
usbd_register_reset_callback(g_usbd_dev, &usb_reset_callback);
|
||||
usbd_register_set_config_callback(g_usbd_dev, cdcacm_set_config);
|
||||
|
||||
/* And use interrupts instead of polling. */
|
||||
// nvic_enable_irq(NVIC_OTG_FS_IRQ);
|
||||
}
|
||||
|
||||
//void __attribute__((weak)) otg_fs_isr() { }
|
||||
|
|
Loading…
Reference in New Issue
Block a user