From 3f5dc472965e6a5b8efe60e33c7b6c2d18d92008 Mon Sep 17 00:00:00 2001 From: Stefan Kerkmann Date: Mon, 11 Jul 2022 15:17:05 +0200 Subject: [Core] Use polled waiting on ChibiOS platforms that support it (#17607) * Use polled waiting on platforms that support it Due to context switching overhead waiting a very short amount of time on a sleeping thread is often not accurate and in fact not usable for timing critical usage i.e. in a driver. Thus we use polled waiting for ranges in the us range on platforms that support it instead. The fallback is the thread sleeping mechanism. This includes: * ARM platforms with CYCCNT register (ARMv7, ARMv8) this is incremented at CPU clock frequency * GD32VF103 RISC-V port with CSR_MCYCLE register this is incremented at CPU clock frequency * RP2040 ARMv6 port which uses the integrated timer peripheral which is incremented with a fixed 1MHz frequency * Use wait_us() instead of chSysPolledDelayX ...as it is powered by busy waiting now. * Add chibios waiting methods test bench --- platforms/chibios/_wait.h | 5 +++++ platforms/chibios/bootloaders/rp2040.c | 5 ++--- platforms/chibios/chibios_config.h | 12 ++++++++++++ platforms/chibios/drivers/serial.c | 3 ++- platforms/chibios/drivers/vendor/RP/RP2040/serial_vendor.c | 2 +- 5 files changed, 22 insertions(+), 5 deletions(-) (limited to 'platforms') diff --git a/platforms/chibios/_wait.h b/platforms/chibios/_wait.h index 2f36c64a2e..21cdffe11a 100644 --- a/platforms/chibios/_wait.h +++ b/platforms/chibios/_wait.h @@ -30,6 +30,11 @@ #ifdef WAIT_US_TIMER void wait_us(uint16_t duration); +#elif PORT_SUPPORTS_RT == TRUE +# define wait_us(us) \ + do { \ + chSysPolledDelayX(US2RTC(REALTIME_COUNTER_CLOCK, us)); \ + } while (0) #else # define wait_us(us) \ do { \ diff --git a/platforms/chibios/bootloaders/rp2040.c b/platforms/chibios/bootloaders/rp2040.c index 13a54036ef..bedc00f32e 100644 --- a/platforms/chibios/bootloaders/rp2040.c +++ b/platforms/chibios/bootloaders/rp2040.c @@ -43,9 +43,8 @@ void __late_init(void) { if (magic_location != magic_token) { magic_location = magic_token; // ChibiOS is not initialized at this point, so sleeping is only - // possible via busy waiting. The internal timer peripheral is running - // at this point with a precision of 1us. - chSysPolledDelayX(MS2RTC(1 * MHZ, RP2040_BOOTLOADER_DOUBLE_TAP_RESET_TIMEOUT)); + // possible via busy waiting. + wait_us(RP2040_BOOTLOADER_DOUBLE_TAP_RESET_TIMEOUT * 1000U); magic_location = 0; return; } diff --git a/platforms/chibios/chibios_config.h b/platforms/chibios/chibios_config.h index e5aa8e3d05..c7a3a98fb0 100644 --- a/platforms/chibios/chibios_config.h +++ b/platforms/chibios/chibios_config.h @@ -21,6 +21,9 @@ #if defined(MCU_RP) # define CPU_CLOCK RP_CORE_CLK +// ChibiOS uses the RP2040 timer peripheral as its real time counter, this timer +// is monotonic and running at 1MHz. +# define REALTIME_COUNTER_CLOCK 1000000 # define USE_GPIOV1 # define PAL_OUTPUT_TYPE_OPENDRAIN _Static_assert(0, "RP2040 has no Open Drain GPIO configuration, setting this is not possible"); @@ -102,6 +105,11 @@ # endif #endif +#if defined(MCU_MIMXRT1062) +# include "clock_config.h" +# define CPU_CLOCK BOARD_BOOTCLOCKRUN_CORE_CLOCK +#endif + #if defined(HT32) # define CPU_CLOCK HT32_CK_SYS_FREQUENCY # define PAL_MODE_ALTERNATE PAL_HT32_MODE_AF @@ -109,3 +117,7 @@ # define PAL_OUTPUT_TYPE_PUSHPULL PAL_HT32_MODE_DIR # define PAL_OUTPUT_SPEED_HIGHEST 0 #endif + +#if !defined(REALTIME_COUNTER_CLOCK) +# define REALTIME_COUNTER_CLOCK CPU_CLOCK +#endif diff --git a/platforms/chibios/drivers/serial.c b/platforms/chibios/drivers/serial.c index 3fae5cd3a4..0dd8e71ae8 100644 --- a/platforms/chibios/drivers/serial.c +++ b/platforms/chibios/drivers/serial.c @@ -20,7 +20,8 @@ # error "chSysPolledDelayX method not supported on this platform" #else # undef wait_us -# define wait_us(x) chSysPolledDelayX(US2RTC(CPU_CLOCK, x)) +// Force usage of polled waiting - in case WAIT_US_TIMER is activated +# define wait_us(us) chSysPolledDelayX(US2RTC(REALTIME_COUNTER_CLOCK, us)) #endif #ifndef SELECT_SOFT_SERIAL_SPEED diff --git a/platforms/chibios/drivers/vendor/RP/RP2040/serial_vendor.c b/platforms/chibios/drivers/vendor/RP/RP2040/serial_vendor.c index d933dc1f5d..338146dadd 100644 --- a/platforms/chibios/drivers/vendor/RP/RP2040/serial_vendor.c +++ b/platforms/chibios/drivers/vendor/RP/RP2040/serial_vendor.c @@ -152,7 +152,7 @@ static inline void enter_rx_state(void) { } // Wait for ~11 bits, 1 start bit + 8 data bits + 1 stop bit + 1 bit // headroom. - chSysPolledDelayX(US2RTC(1 * MHZ, (1000000U * 11 / SERIAL_USART_SPEED))); + wait_us(1000000U * 11U / SERIAL_USART_SPEED); // Disable tx state machine to not interfere with our tx pin manipulation pio_sm_set_enabled(pio, tx_state_machine, false); gpio_set_drive_strength(SERIAL_USART_TX_PIN, GPIO_DRIVE_STRENGTH_2MA); -- cgit v1.2.3