From 6d6a67b44bda78663c1a46d03a2b5fc1cc9b6e87 Mon Sep 17 00:00:00 2001 From: Uwe Bonnes Date: Sun, 25 Jul 2021 23:48:35 +0200 Subject: [PATCH] timeout: Make sure we wait at least the period requested (#900, #902) Fixes bug introduced with last commit( Recover from bad AP access) Let STM32 timers run at 100 Hz against 10 Hz before. Programming STM32F103 failed random (#900) with 20 ms timeout requested against the 100 ms timeout granularity provided up to now. STM32 Firmware only ticked at 10 hertz, so the sequence "low_access", "set timeout", "send out 8 bit command", "read 3 bit result" when reading "wait" and timer increment tick happening during that sequence will already hits the timeout even so only mininal time has elapsed and not the requested timeout. --- src/include/general.h | 6 ++++++ src/platforms/hosted/platform.h | 2 ++ src/platforms/launchpad-icdi/platform.c | 3 --- src/platforms/stm32/timing_stm32.c | 18 ++++++++++++------ src/timing.c | 4 +++- 5 files changed, 23 insertions(+), 10 deletions(-) diff --git a/src/include/general.h b/src/include/general.h index ba329d21..25f83a4b 100644 --- a/src/include/general.h +++ b/src/include/general.h @@ -164,5 +164,11 @@ static inline void DEBUG_WIRE(const char *format, ...) #undef MAX #define MAX(x, y) (((x) > (y)) ? (x) : (y)) +#if !defined(SYSTICKHZ) +# define SYSTICKHZ 100 +#endif +#define SYSTICKMS (1000 / SYSTICKHZ) +#define MORSECNT ((SYSTICKHZ / 10) - 1) + #endif diff --git a/src/platforms/hosted/platform.h b/src/platforms/hosted/platform.h index bf3fa141..339fec85 100644 --- a/src/platforms/hosted/platform.h +++ b/src/platforms/hosted/platform.h @@ -10,6 +10,8 @@ void platform_buffer_flush(void); #define SET_IDLE_STATE(x) #define SET_RUN_STATE(x) +#define SYSTICKHZ 1000 + #define VENDOR_ID_BMP 0x1d50 #define PRODUCT_ID_BMP_BL 0x6017 #define PRODUCT_ID_BMP 0x6018 diff --git a/src/platforms/launchpad-icdi/platform.c b/src/platforms/launchpad-icdi/platform.c index d62a8cda..83189192 100644 --- a/src/platforms/launchpad-icdi/platform.c +++ b/src/platforms/launchpad-icdi/platform.c @@ -25,9 +25,6 @@ #include #include -#define SYSTICKHZ 100 -#define SYSTICKMS (1000 / SYSTICKHZ) - #define PLL_DIV_80MHZ 5 #define PLL_DIV_25MHZ 16 diff --git a/src/platforms/stm32/timing_stm32.c b/src/platforms/stm32/timing_stm32.c index dbf5cc3a..9f9c6984 100644 --- a/src/platforms/stm32/timing_stm32.c +++ b/src/platforms/stm32/timing_stm32.c @@ -27,12 +27,14 @@ uint8_t running_status; static volatile uint32_t time_ms; uint32_t swd_delay_cnt = 0; +static int morse_tick; + void platform_timing_init(void) { /* Setup heartbeat timer */ systick_set_clocksource(STK_CSR_CLKSOURCE_AHB_DIV8); /* Interrupt us at 10 Hz */ - systick_set_reload(rcc_ahb_frequency / (8 * 10) ); + systick_set_reload(rcc_ahb_frequency / (8 * SYSTICKHZ) ); /* SYSTICK_IRQ with low priority */ nvic_set_priority(NVIC_SYSTICK_IRQ, 14 << 4); systick_interrupt_enable(); @@ -48,12 +50,16 @@ void platform_delay(uint32_t ms) void sys_tick_handler(void) { - if(running_status) - gpio_toggle(LED_PORT, LED_IDLE_RUN); - - time_ms += 100; + time_ms += SYSTICKMS; - SET_ERROR_STATE(morse_update()); + if (morse_tick >= MORSECNT) { + if(running_status) + gpio_toggle(LED_PORT, LED_IDLE_RUN); + SET_ERROR_STATE(morse_update()); + morse_tick = 0; + } else { + morse_tick++; + } } uint32_t platform_time_ms(void) diff --git a/src/timing.c b/src/timing.c index 1717bf5a..6c9a6da9 100644 --- a/src/timing.c +++ b/src/timing.c @@ -21,10 +21,12 @@ void platform_timeout_set(platform_timeout *t, uint32_t ms) { + if (ms <= SYSTICKMS) + ms = SYSTICKMS; t->time = platform_time_ms() + ms; } bool platform_timeout_is_expired(platform_timeout *t) { - return platform_time_ms() >= t->time; + return platform_time_ms() > t->time; }