Compare commits

...

3 Commits

4 changed files with 73 additions and 18 deletions

View File

@ -5,6 +5,7 @@
#include <stddef.h> #include <stddef.h>
#include <stdint.h> #include <stdint.h>
#include <stdio.h> #include <stdio.h>
#include <string.h>
#include "encoder.h" #include "encoder.h"
#include "systick.h" #include "systick.h"
@ -77,8 +78,8 @@ int encoder_speed() {
int encoder_current_speed() { int encoder_current_speed() {
int dt = ticks_ms(tick - speed_ticks[SPEED_AVG_NUM - 1].tick); int dt = ticks_ms(tick - speed_ticks[SPEED_AVG_NUM - 1].tick);
if (dt > 200) { if (dt > 200) {
printf("dt: %d", dt); memmove(&speed_ticks[0], &speed_ticks[1], SPEED_AVG_NUM * sizeof speed_ticks[0]);
return 0; speed_ticks[SPEED_AVG_NUM].tick = tick;
} }
return 1000 return 1000
* (speed_ticks[SPEED_AVG_NUM-1].pos - speed_ticks[0].pos) * (speed_ticks[SPEED_AVG_NUM-1].pos - speed_ticks[0].pos)

68
main.c
View File

@ -4,6 +4,7 @@
#include <libopencm3/stm32/rcc.h> #include <libopencm3/stm32/rcc.h>
#include <stddef.h> #include <stddef.h>
#include <stdio.h> #include <stdio.h>
#include <string.h>
#include "adc.h" #include "adc.h"
#include "buttons.h" #include "buttons.h"
@ -31,6 +32,8 @@ static void erase_app(void);
#endif #endif
static void fast_reset(void); static void fast_reset(void);
static void set_watchdog(int w);
static void set_print_changes(int p);
static void sys_tick_setup() { static void sys_tick_setup() {
systick_set_reload(rcc_ahb_frequency / SYSTICK_FREQUENCY - 1); systick_set_reload(rcc_ahb_frequency / SYSTICK_FREQUENCY - 1);
@ -61,6 +64,19 @@ void erase_app() {
} }
#endif #endif
int watchdog = 0;
int print_changes = 0;
void set_watchdog(int w) {
watchdog = w;
printf("watchdog: %d", watchdog);
}
void set_print_changes(int p) {
print_changes = p;
printf("pos reporting: %d", print_changes);
}
int main(void) { int main(void) {
#if 0 #if 0
rcc_clock_setup_pll(&rcc_hse_25mhz_3v3[RCC_CLOCK_3V3_84MHZ]); rcc_clock_setup_pll(&rcc_hse_25mhz_3v3[RCC_CLOCK_3V3_84MHZ]);
@ -86,18 +102,20 @@ int main(void) {
uint32_t last_tick = tick; uint32_t last_tick = tick;
int last_pos = 0; int last_pos = 0;
int watchdog = 1;
uint32_t watchdog_counter = 0; uint32_t watchdog_counter = 0;
int print_changes = 0;
while (tick < 5000) { while (tick < 5000 && !usb_connected()) {
usbd_poll(g_usbd_dev); usbd_poll(g_usbd_dev);
} }
if (!usb_got_reset()) { if (!usb_connected()) {
fast_reset(); fast_reset();
} }
char cmd_buf[64];
uint8_t cmd_len = 0;
bool cmd = false;
while (1) { while (1) {
/* Handle control messages through the comms CDC */ /* Handle control messages through the comms CDC */
char buf[64]; char buf[64];
@ -117,7 +135,37 @@ int main(void) {
} }
for (char c; ringbuffer_get(comm_in_buf, (void *)&c, 1);) { for (char c; ringbuffer_get(comm_in_buf, (void *)&c, 1);) {
switch (c) { if (cmd) {
if (c == '\r' || c == '\n') {
cmd_buf[cmd_len] = 0;
if (cmd_buf[0] == 'r' && cmd_len == 2 && (cmd_buf[1] == '0' || cmd_buf[1] == '1')) {
set_print_changes(cmd_buf[1] == '1');
} else if(cmd_buf[0] == 'w' && cmd_len == 2 && (cmd_buf[1] == '0' || cmd_buf[1] == '1')) {
set_watchdog(cmd_buf[1] == '1');
} else if (!strcmp(cmd_buf, "reset")) {
fast_reset();
}
#if defined(BOOTLOADER)
else if (!strcmp(cmd_buf, "flash")) {
start_bootloader();
} else if (!strcmp(cmd_buf, "erase")) {
erase_app();
}
#endif
else {
printf("unknown command: %s", cmd_buf);
}
cmd = false;
} else if (cmd_len < sizeof cmd_buf - 1) {
cmd_buf[cmd_len++] = c;
}
}
else switch (c) {
case '>':
cmd = true;
cmd_len = 0;
break;
case 'B': // Battery case 'B': // Battery
printf("%lu", (unsigned long)adc_bat_voltage()); printf("%lu", (unsigned long)adc_bat_voltage());
break; break;
@ -131,12 +179,10 @@ int main(void) {
printf("pos: %d %d", encoder_get(), encoder_current_speed()); printf("pos: %d %d", encoder_get(), encoder_current_speed());
break; break;
case 'r': // toggle reporting case 'r': // toggle reporting
print_changes = !print_changes; set_print_changes(!print_changes);
printf("pos reporting: %d", print_changes);
break; break;
case 'W': // toggle watchdog case 'W': // toggle watchdog
watchdog = !watchdog; set_watchdog(!watchdog);
printf("watchdog: %d", watchdog);
break; break;
case 'S': // reStart case 'S': // reStart
fast_reset(); fast_reset();
@ -164,7 +210,7 @@ int main(void) {
/* Send replies */ /* Send replies */
if ((buf_len = ringbuffer_peek(comm_out_buf, &buf[0], sizeof buf))) { if ((buf_len = ringbuffer_peek(comm_out_buf, &buf[0], sizeof buf))) {
if (usb_write_cdcacm(ACM_COMM, (void *)buf, buf_len, 1)) { if (!usb_connected() || usb_write_cdcacm(ACM_COMM, (void *)buf, buf_len, 1)) {
ringbuffer_skip(comm_out_buf, buf_len); ringbuffer_skip(comm_out_buf, buf_len);
} }
} }
@ -172,7 +218,7 @@ int main(void) {
/* Send any available data to the NFC CDC */ /* Send any available data to the NFC CDC */
if (!ringbuffer_empty(uart_to_usb_buf)) { if (!ringbuffer_empty(uart_to_usb_buf)) {
unsigned len = ringbuffer_peek(uart_to_usb_buf, &buf[0], sizeof buf); unsigned len = ringbuffer_peek(uart_to_usb_buf, &buf[0], sizeof buf);
if (usb_write_cdcacm(ACM_NFC, &buf[0], len, 1)) { if (!usb_connected() || usb_write_cdcacm(ACM_NFC, &buf[0], len, 1)) {
ringbuffer_skip(uart_to_usb_buf, len); ringbuffer_skip(uart_to_usb_buf, len);
} }
} }

16
usb.c
View File

@ -11,6 +11,9 @@
extern ringbuffer *usb_to_uart_buf, *comm_in_buf; extern ringbuffer *usb_to_uart_buf, *comm_in_buf;
static void usb_reset_callback(void);
static void usb_suspend_callback(void);
/* Number of serial devices we want to provide. */ /* Number of serial devices we want to provide. */
#define NUM_SERIAL 2 #define NUM_SERIAL 2
@ -40,7 +43,7 @@ static char const *usb_strings[] = {
[3 + ACM_NFC] = "RFID", [3 + ACM_NFC] = "RFID",
}; };
static int got_reset = 0; static int connected = 0;
usbd_device *g_usbd_dev; usbd_device *g_usbd_dev;
@ -176,11 +179,15 @@ static void cdcacm_set_config(usbd_device *usbd_dev, uint16_t wValue) {
} }
void usb_reset_callback(void) { void usb_reset_callback(void) {
got_reset = 1; connected = 1;
} }
bool usb_got_reset(void) { void usb_suspend_callback(void) {
return got_reset; connected = 0;
}
bool usb_connected(void) {
return connected;
} }
void usb_setup() { void usb_setup() {
@ -337,6 +344,7 @@ void usb_reinit() {
usbd_control_buffer, sizeof(usbd_control_buffer)); usbd_control_buffer, sizeof(usbd_control_buffer));
usbd_register_reset_callback(g_usbd_dev, &usb_reset_callback); usbd_register_reset_callback(g_usbd_dev, &usb_reset_callback);
usbd_register_suspend_callback(g_usbd_dev, &usb_suspend_callback);
usbd_register_set_config_callback(g_usbd_dev, cdcacm_set_config); usbd_register_set_config_callback(g_usbd_dev, cdcacm_set_config);
} }

2
usb.h
View File

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