Compare commits

...

11 Commits

Author SHA1 Message Date
Valentin Ochs bb32eb6614 Bootloader, speed calculation, better position detection 2021-08-15 02:01:04 +02:00
Valentin Ochs e2ce72200e Fix buttons 2021-07-17 00:52:42 +02:00
Valentin Ochs d0545ec844 Use default linking stuff for now 2021-07-17 00:52:10 +02:00
Valentin Ochs 8a03399dc3 Enable SYSCLK to set up EXTI... 2021-07-17 00:18:18 +02:00
Valentin Ochs c76c95c1bd Do not ignore linker files 2021-07-16 21:22:20 +02:00
Valentin Ochs 4afb598453 Allow reinitializing USB 2021-07-16 21:22:13 +02:00
Valentin Ochs e121ecd896 Make encoder setup easier to change 2021-07-16 21:21:54 +02:00
Valentin Ochs 2f78a78ed8 Make buttons setup easier to change 2021-07-16 21:21:45 +02:00
Valentin Ochs aa653aecea Report battery state in mV 2021-07-16 21:21:22 +02:00
Valentin Ochs 51a95e7ffb 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
2021-07-16 21:20:33 +02:00
Valentin Ochs 7ea4494345 Add options for generating UF2 image for bootloader, and set up linker symbol for bootloader config 2021-07-16 21:18:10 +02:00
11 changed files with 422 additions and 83 deletions

1
.gitignore vendored
View File

@ -1,4 +1,3 @@
*.ld
*.o
*.d
*.elf

View File

@ -3,15 +3,34 @@ OOCD_INTERFACE ?= stlink-v2
OOCD_TARGET ?= stm32f4x
BMP_PORT ?= /dev/ttyBmpGdb
COMFLAGS=-g -Os -pedantic -Wall
COMFLAGS=-g -Og -pedantic -Wall
CFLAGS=$(COMFLAGS) -std=c99 -D_POSIX_C_SOURCE=200809L
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
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
View File

@ -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);
bat_voltage_mv = adc_read_regular(ADC1) * 3300 / 2048;
}
return bat_voltage_mv;
}

View File

@ -4,28 +4,35 @@
#include <stdio.h>
#include "buttons.h"
#define BUTTON_RCC RCC_GPIOA
#define BUTTON_PORT GPIOA
#define OPEN_BUTTON GPIO6
#define CLOSE_BUTTON GPIO7
#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
void buttons_setup() {
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);
#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();
}
static uint32_t open_ticks = 0;
static uint32_t close_ticks = 0;
void buttons_open(uint32_t ticks) {
gpio_clear(BUTTON_PORT, OPEN_BUTTON);
gpio_clear(OPEN_PORT, OPEN_BUTTON);
printf("Open %lu", ticks);
open_ticks = ticks;
}
void buttons_close(uint32_t ticks) {
gpio_clear(BUTTON_PORT, CLOSE_BUTTON);
gpio_clear(CLOSE_PORT, CLOSE_BUTTON);
printf("Close %lu", ticks);
close_ticks = ticks;
}
@ -33,14 +40,15 @@ void buttons_close(uint32_t ticks) {
void buttons_release(void) {
open_ticks = close_ticks = 0;
printf("Release");
gpio_set(BUTTON_PORT, OPEN_BUTTON | CLOSE_BUTTON);
gpio_set(OPEN_PORT, OPEN_BUTTON);
gpio_set(CLOSE_PORT, CLOSE_BUTTON);
}
void buttons_tick(void) {
if (open_ticks) {
open_ticks--;
if (!open_ticks) {
gpio_set(BUTTON_PORT, OPEN_BUTTON);
gpio_set(OPEN_PORT, OPEN_BUTTON);
printf("Release open");
}
}
@ -48,7 +56,7 @@ void buttons_tick(void) {
close_ticks--;
if (!close_ticks) {
printf("Release close");
gpio_set(BUTTON_PORT, CLOSE_BUTTON);
gpio_set(CLOSE_PORT, CLOSE_BUTTON);
}
}
}

224
encoder.c
View File

@ -2,73 +2,203 @@
#include <libopencm3/stm32/exti.h>
#include <libopencm3/stm32/gpio.h>
#include <libopencm3/stm32/rcc.h>
#include <stdint.h>
#include <stddef.h>
#include <stdint.h>
#include <stdio.h>
#include "encoder.h"
#include "systick.h"
static volatile int pos;
#define USE_CORRECTION
#define FIXED_OFFSET 20
// #define DEBUG_ENCODER_TICKS
#define SPEED_AVG_NUM 4
#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);
#define PIN1 GPIO4
#define PIN1_IRQ NVIC_EXTI4_IRQ
#define PIN2 GPIO5
#define PIN2_IRQ NVIC_EXTI9_5_IRQ
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
void encoder_setup(void) {
rcc_periph_clock_enable(RCC_GPIOA);
rcc_periph_clock_enable(PIN_RCC);
rcc_periph_clock_enable(RCC_SYSCFG);
gpio_mode_setup(GPIOA, GPIO_MODE_INPUT, GPIO_PUPD_NONE, PIN1 | PIN2);
gpio_mode_setup(PIN_PORT, GPIO_MODE_INPUT, GPIO_PUPD_NONE, PIN_OPEN | 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);
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);
nvic_enable_irq(PIN1_IRQ);
nvic_enable_irq(PIN2_IRQ);
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);
}
int encoder_get() {
return pos;
}
static int prev = 0;
void check(void) {
int pp = pos;
int now = GPIO_IDR(GPIOA);
now = (!!(now & PIN1))<<0 | (!!(now & PIN2)) << 1;
switch (prev << 2 | now) {
case 0 * 4 + 2:
case 1 * 4 + 0:
case 2 * 4 + 3:
case 3 * 4 + 1:
pos++;
break;
case 0 * 4 + 1:
case 1 * 4 + 3:
case 2 * 4 + 0:
case 3 * 4 + 2:
pos--;
break;
default:
prev = now;
return;
}
prev = now;
if (pos < MIN_POS)
pos = MIN_POS;
if (pos > MAX_POS)
pos = MAX_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);
}
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;
#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;
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;
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;
}
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();
}

View File

@ -8,4 +8,6 @@
void encoder_setup(void);
int encoder_get(void);
int encoder_current_speed(void);
int encoder_speed(void);
#endif

78
linker.ld Normal file
View File

@ -0,0 +1,78 @@
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));

99
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>
@ -10,6 +11,7 @@
#include "ringbuffer.h"
#include "uart.h"
#include "encoder.h"
#include "systick.h"
static const uint32_t button_time = 100;
@ -21,8 +23,17 @@ 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(168000);
systick_set_reload(rcc_ahb_frequency / SYSTICK_FREQUENCY - 1);
systick_set_clocksource(STK_CSR_CLKSOURCE_AHB);
systick_counter_enable();
systick_interrupt_enable();
@ -32,6 +43,24 @@ 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]);
@ -44,10 +73,12 @@ int main(void) {
RINGBUFFER_INIT(comm_in_buf, 64);
RINGBUFFER_INIT(comm_out_buf, 64);
usb_setup();
sys_tick_setup();
adc_setup();
uart_setup();
sys_tick_setup();
usb_setup();
buttons_setup();
encoder_setup();
@ -55,40 +86,82 @@ 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':
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':
printf("pos: %d", encoder_get());
case 'R': // Report
printf("pos: %d %d", encoder_get(), encoder_current_speed());
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 && ringbuffer_empty(comm_out_buf)) {
printf("pos: %d", pos);
last_pos = pos;
if (pos != last_pos) {
if (ringbuffer_empty(comm_out_buf)) {
if (print_changes) {
printf("pos: %d %d", pos, encoder_speed());
}
last_pos = pos;
}
}
/* 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)) {
@ -108,7 +181,7 @@ int main(void) {
}
usbd_poll(g_usbd_dev);
__asm__("wfi");
// __asm__("wfi");
}
}

13
systick.h Normal file
View File

@ -0,0 +1,13 @@
#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

27
usb.c
View File

@ -40,6 +40,8 @@ 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. */
@ -173,6 +175,14 @@ 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);
@ -314,15 +324,20 @@ void usb_setup() {
}
/* Initialize the device... */
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_set_config_callback(g_usbd_dev, cdcacm_set_config);
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);
}
//void __attribute__((weak)) otg_fs_isr() { }

2
usb.h
View File

@ -6,7 +6,9 @@
extern usbd_device *g_usbd_dev;
void usb_setup(void);
void usb_reinit(void);
int usb_write_cdcacm(uint8_t acm, void *data, size_t len, int tries);
bool usb_got_reset(void);
#define ACM_NFC 1
#define ACM_COMM 0