From 9a1429ae9020e559f0b7ecf67e196362b1da623c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Benjamin=20Cab=C3=A9?= Date: Wed, 14 Oct 2026 18:36:30 +0200 Subject: [PATCH] fix(platforms): sleep with nanosleep() in px4_usleep() (#29218) * fix(platforms/common): sleep with nanosleep() in px4_usleep() Outside of the SITL lockstep build, px4_usleep() was usleep(). POSIX only requires usleep() to accept less than 1000000 microseconds and lets it fail with EINVAL from there; POSIX.1-2001 marked it obsolescent and POSIX.1-2008 removed it in favour of nanosleep(). glibc, NuttX and macOS sleep for longer anyway, but other implementations return EINVAL at once, and as no caller checks the result, the sleep would silently not happen. More than twenty call sites ask for a second or more, such as uorb top, mavlink, the ESC calibration or voxl2_io, and some durations are computed at run time, such as the one-second idle period of the POSIX HRT thread. Outside of the lockstep build, px4_usleep() is now a px4_platform function that converts the microseconds to the timespec of nanosleep(), so the call sites keep passing microseconds of any duration. It is not inline: expanding the conversion at every call site grew px4_fmu-v6xrt_default by 4 kB of flash. QURT has no nanosleep(), which PX4 only stubs there, but its usleep() accepts a second or more, so px4_usleep() remains usleep() there. system_usleep() remains usleep(), for the wall-clock sleeps that bypass the lockstep scheduler. A signal still ends the sleep early, with -1 and EINTR, rather than restarting it for the remaining time: on POSIX, the HRT and work queue threads sleep in px4_usleep(), and hrt_work_queue() and work_queue() wake them with SIGCONT, so a restarted sleep would delay hrt_call_*() callouts by up to a second. On NuttX, no PX4 code signals its tasks, and px4_task_delete() does not depend on the sleep returning: without cancellation points, which no board enables, task_delete() terminates the task wherever it is. Failures are now -1 with errno, where the usleep() of NuttX returned the error number. No call site checks the result, so none changes. The sleep test now also sleeps for 1.2 s. Assisted-by: Claude:opus-5.5 * perf(boards): keep the sleep path in ITCM on i.MX RT boards The ITCM lists of these boards hold the sleep path of NuttX: usleep(), clock_nanosleep() and nxsig_nanosleep(). px4_usleep() now enters it through its own function and nanosleep(), which take the place of usleep(), so the path stays in ITCM. usleep() remains linked for the code that still calls it directly, such as the start-up code of the boards and NSH. Assisted-by: Claude:opus-5.5 * fix(drivers): sleep for a second or more with px4_usleep() bmi088_i2c, microstrain, frsky_telemetry and the bit rate detection of the kinetis, stm32 and stm32h7 UAVCAN drivers asked usleep() for a second or more, which POSIX doesn't require it to accept. The usleep() of NuttX accepts it, but px4_usleep() now does on every platform. Assisted-by: Claude:opus-5.5 --- .../scripts/itcm_functions_includes.ld | 3 +- .../scripts/itcm_functions_includes.ld | 3 +- .../scripts/itcm_functions_includes.ld | 3 +- .../scripts/itcm_functions_includes.ld | 3 +- .../scripts/itcm_functions_includes.ld | 3 +- platforms/common/CMakeLists.txt | 1 + .../common/include/px4_platform_common/time.h | 6 ++- platforms/common/sleep.cpp | 51 +++++++++++++++++++ .../bosch/bmi088_i2c/BMI088_Accelerometer.cpp | 6 +-- src/drivers/ins/microstrain/MicroStrain.cpp | 2 +- .../frsky_telemetry/frsky_telemetry.cpp | 2 +- .../driver/include/uavcan_kinetis/can.hpp | 3 +- .../stm32/driver/include/uavcan_stm32/can.hpp | 3 +- .../driver/include/uavcan_stm32h7/can.hpp | 3 +- src/systemcmds/tests/test_sleep.c | 13 +++++ 15 files changed, 90 insertions(+), 15 deletions(-) create mode 100644 platforms/common/sleep.cpp diff --git a/boards/agam/fmu-v6xrt/nuttx-config/scripts/itcm_functions_includes.ld b/boards/agam/fmu-v6xrt/nuttx-config/scripts/itcm_functions_includes.ld index c0a5fd419db..782dfd1c649 100644 --- a/boards/agam/fmu-v6xrt/nuttx-config/scripts/itcm_functions_includes.ld +++ b/boards/agam/fmu-v6xrt/nuttx-config/scripts/itcm_functions_includes.ld @@ -135,7 +135,8 @@ *(.text.sem_post) *(.text._ZN3px417ScheduledWorkItem15ScheduleDelayedEm) *(.text._ZN10FlightTaskC1Ev) -*(.text.usleep) +*(.text.px4_usleep) +*(.text.nanosleep) *(.text._ZN14FlightTaskAutoC1Ev) *(.text.sem_getvalue) *(.text._ZN23MavlinkStreamHighresIMU4sendEv) diff --git a/boards/ark/fmu-v6xrt/nuttx-config/scripts/itcm_functions_includes.ld b/boards/ark/fmu-v6xrt/nuttx-config/scripts/itcm_functions_includes.ld index 0fd83a5558d..42a23cbb780 100644 --- a/boards/ark/fmu-v6xrt/nuttx-config/scripts/itcm_functions_includes.ld +++ b/boards/ark/fmu-v6xrt/nuttx-config/scripts/itcm_functions_includes.ld @@ -130,7 +130,8 @@ *(.text.sem_post) *(.text._ZN3px417ScheduledWorkItem15ScheduleDelayedEm) *(.text._ZN10FlightTaskC1Ev) -*(.text.usleep) +*(.text.px4_usleep) +*(.text.nanosleep) *(.text._ZN14FlightTaskAutoC1Ev) *(.text.sem_getvalue) *(.text._ZN23MavlinkStreamHighresIMU4sendEv) diff --git a/boards/nxp/mr-tropic/nuttx-config/scripts/itcm_functions_includes.ld b/boards/nxp/mr-tropic/nuttx-config/scripts/itcm_functions_includes.ld index 73631ca8be7..b4afcb7b117 100644 --- a/boards/nxp/mr-tropic/nuttx-config/scripts/itcm_functions_includes.ld +++ b/boards/nxp/mr-tropic/nuttx-config/scripts/itcm_functions_includes.ld @@ -134,7 +134,8 @@ *(.text.sem_post) *(.text._ZN3px417ScheduledWorkItem15ScheduleDelayedEm) *(.text._ZN10FlightTaskC1Ev) -*(.text.usleep) +*(.text.px4_usleep) +*(.text.nanosleep) *(.text._ZN14FlightTaskAutoC1Ev) *(.text.sem_getvalue) *(.text._ZN23MavlinkStreamHighresIMU4sendEv) diff --git a/boards/nxp/tropic-community/nuttx-config/scripts/itcm_functions_includes.ld b/boards/nxp/tropic-community/nuttx-config/scripts/itcm_functions_includes.ld index 497830f9aa5..02c04b75ba8 100644 --- a/boards/nxp/tropic-community/nuttx-config/scripts/itcm_functions_includes.ld +++ b/boards/nxp/tropic-community/nuttx-config/scripts/itcm_functions_includes.ld @@ -134,7 +134,8 @@ *(.text.sem_post) *(.text._ZN3px417ScheduledWorkItem15ScheduleDelayedEm) *(.text._ZN10FlightTaskC1Ev) -*(.text.usleep) +*(.text.px4_usleep) +*(.text.nanosleep) *(.text._ZN14FlightTaskAutoC1Ev) *(.text.sem_getvalue) *(.text._ZN23MavlinkStreamHighresIMU4sendEv) diff --git a/boards/px4/fmu-v6xrt/nuttx-config/scripts/itcm_functions_includes.ld b/boards/px4/fmu-v6xrt/nuttx-config/scripts/itcm_functions_includes.ld index c0a5fd419db..782dfd1c649 100644 --- a/boards/px4/fmu-v6xrt/nuttx-config/scripts/itcm_functions_includes.ld +++ b/boards/px4/fmu-v6xrt/nuttx-config/scripts/itcm_functions_includes.ld @@ -135,7 +135,8 @@ *(.text.sem_post) *(.text._ZN3px417ScheduledWorkItem15ScheduleDelayedEm) *(.text._ZN10FlightTaskC1Ev) -*(.text.usleep) +*(.text.px4_usleep) +*(.text.nanosleep) *(.text._ZN14FlightTaskAutoC1Ev) *(.text.sem_getvalue) *(.text._ZN23MavlinkStreamHighresIMU4sendEv) diff --git a/platforms/common/CMakeLists.txt b/platforms/common/CMakeLists.txt index a7fd9a6a178..720d2ae1e90 100644 --- a/platforms/common/CMakeLists.txt +++ b/platforms/common/CMakeLists.txt @@ -52,6 +52,7 @@ add_library(px4_platform STATIC px4_getopt.c px4_cli.cpp shutdown.cpp + sleep.cpp spi.cpp pab_manifest.c Serial.cpp diff --git a/platforms/common/include/px4_platform_common/time.h b/platforms/common/include/px4_platform_common/time.h index d61e23c75cf..107d3f56a4e 100644 --- a/platforms/common/include/px4_platform_common/time.h +++ b/platforms/common/include/px4_platform_common/time.h @@ -25,10 +25,13 @@ __END_DECLS #endif +__BEGIN_DECLS +__EXPORT int px4_usleep(useconds_t usec); +__END_DECLS + #if defined(ENABLE_LOCKSTEP_SCHEDULER) __BEGIN_DECLS -__EXPORT int px4_usleep(useconds_t usec); __EXPORT unsigned int px4_sleep(unsigned int seconds); __EXPORT int px4_pthread_cond_timedwait(pthread_cond_t *cond, pthread_mutex_t *mutex, @@ -37,7 +40,6 @@ __END_DECLS #else -#define px4_usleep system_usleep #define px4_sleep system_sleep #define px4_pthread_cond_timedwait system_pthread_cond_timedwait diff --git a/platforms/common/sleep.cpp b/platforms/common/sleep.cpp new file mode 100644 index 00000000000..4d85cb277ea --- /dev/null +++ b/platforms/common/sleep.cpp @@ -0,0 +1,51 @@ +/**************************************************************************** + * + * Copyright (C) 2026 PX4 Development Team. All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in + * the documentation and/or other materials provided with the + * distribution. + * 3. Neither the name PX4 nor the names of its contributors may be + * used to endorse or promote products derived from this software + * without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS + * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE + * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, + * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, + * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS + * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED + * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN + * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE + * POSSIBILITY OF SUCH DAMAGE. + * + ****************************************************************************/ + +#include + +#if !defined(ENABLE_LOCKSTEP_SCHEDULER) + +int px4_usleep(useconds_t usec) +{ +#if defined(__PX4_QURT) + // no nanosleep() on QURT, whose usleep() accepts a second or more + return usleep(usec); +#else + struct timespec duration; + duration.tv_sec = usec / 1000000; + duration.tv_nsec = (usec % 1000000) * 1000; + return nanosleep(&duration, nullptr); +#endif +} + +#endif diff --git a/src/drivers/imu/bosch/bmi088_i2c/BMI088_Accelerometer.cpp b/src/drivers/imu/bosch/bmi088_i2c/BMI088_Accelerometer.cpp index c091ec9f7b7..232bb1310fc 100644 --- a/src/drivers/imu/bosch/bmi088_i2c/BMI088_Accelerometer.cpp +++ b/src/drivers/imu/bosch/bmi088_i2c/BMI088_Accelerometer.cpp @@ -643,7 +643,7 @@ bool BMI088_Accelerometer::SelfTest() usleep(100000); PX4_WARN("Sensor ErrReg: 0x%02x", CheckSensorErrReg()); Configure(); - usleep(1000000); + px4_usleep(1000000); PX4_WARN("Sensor ErrReg: 0x%02x", CheckSensorErrReg()); const uint8_t ACC_CHIP_ID = RegisterRead(Register::ACC_CHIP_ID); PX4_WARN("ACC_CHIP_ID: 0x%02x", ACC_CHIP_ID); @@ -655,7 +655,7 @@ bool BMI088_Accelerometer::SelfTest() PX4_WARN("ACC_PWR_CONF(0): 0x%02x", RegisterRead(Register::ACC_PWR_CONF)); } - usleep(2000000); + px4_usleep(2000000); PX4_WARN("Sensor ErrReg: 0x%02x", CheckSensorErrReg()); if (RegisterWrite(Register::ACC_RANGE, 0x03) == PX4_OK) { @@ -800,7 +800,7 @@ float *BMI088_Accelerometer::ReadAccelDataFIFO() buffer[0] = 0x10 | 0x40; RegisterWrite(Register::FIFO_CONFIG_1, buffer[0]); PX4_WARN("FIFO_CONFIG_1(0x%02x): 0x%02x", buffer[0], RegisterRead(Register::FIFO_CONFIG_1)); - usleep(1000000); + px4_usleep(1000000); int fifo_fill_level = 0; diff --git a/src/drivers/ins/microstrain/MicroStrain.cpp b/src/drivers/ins/microstrain/MicroStrain.cpp index 0bbb98bd562..5fa99be5569 100755 --- a/src/drivers/ins/microstrain/MicroStrain.cpp +++ b/src/drivers/ins/microstrain/MicroStrain.cpp @@ -202,7 +202,7 @@ mip_cmd_result MicroStrain::forceIdle() break; } else { - usleep(1_s); + px4_usleep(1_s); } } diff --git a/src/drivers/telemetry/frsky_telemetry/frsky_telemetry.cpp b/src/drivers/telemetry/frsky_telemetry/frsky_telemetry.cpp index c68345bedf9..e87bd4c1f73 100644 --- a/src/drivers/telemetry/frsky_telemetry/frsky_telemetry.cpp +++ b/src/drivers/telemetry/frsky_telemetry/frsky_telemetry.cpp @@ -759,7 +759,7 @@ int frsky_telemetry_main(int argc, char *argv[]) thread_should_exit = true; while (thread_running) { - usleep(1000000); + px4_usleep(1000000); PX4_INFO("."); } diff --git a/src/drivers/uavcan/uavcan_drivers/kinetis/driver/include/uavcan_kinetis/can.hpp b/src/drivers/uavcan/uavcan_drivers/kinetis/driver/include/uavcan_kinetis/can.hpp index f03f755a1bf..f61c19d74eb 100644 --- a/src/drivers/uavcan/uavcan_drivers/kinetis/driver/include/uavcan_kinetis/can.hpp +++ b/src/drivers/uavcan/uavcan_drivers/kinetis/driver/include/uavcan_kinetis/can.hpp @@ -9,6 +9,7 @@ #include #include #include +#include namespace uavcan_kinetis { @@ -384,7 +385,7 @@ public: const int res = driver.init(inout_bitrate, CanIface::SilentMode); - usleep(1000000); + px4_usleep(1000000); if (res >= 0) { for (uavcan::uint8_t iface = 0; iface < driver.getNumIfaces(); iface++) { diff --git a/src/drivers/uavcan/uavcan_drivers/stm32/driver/include/uavcan_stm32/can.hpp b/src/drivers/uavcan/uavcan_drivers/stm32/driver/include/uavcan_stm32/can.hpp index 67abcf53c58..4378a1b4a29 100644 --- a/src/drivers/uavcan/uavcan_drivers/stm32/driver/include/uavcan_stm32/can.hpp +++ b/src/drivers/uavcan/uavcan_drivers/stm32/driver/include/uavcan_stm32/can.hpp @@ -8,6 +8,7 @@ #include #include #include +#include namespace uavcan_stm32 { @@ -345,7 +346,7 @@ public: const int res = driver.init(inout_bitrate, CanIface::SilentMode, enabledInterfaces_); - usleep(1000000); + px4_usleep(1000000); if (res >= 0) { for (uavcan::uint8_t iface = 0; iface < driver.getNumIfaces(); iface++) { diff --git a/src/drivers/uavcan/uavcan_drivers/stm32h7/driver/include/uavcan_stm32h7/can.hpp b/src/drivers/uavcan/uavcan_drivers/stm32h7/driver/include/uavcan_stm32h7/can.hpp index f0865a5e7e4..ecfe27e824d 100644 --- a/src/drivers/uavcan/uavcan_drivers/stm32h7/driver/include/uavcan_stm32h7/can.hpp +++ b/src/drivers/uavcan/uavcan_drivers/stm32h7/driver/include/uavcan_stm32h7/can.hpp @@ -8,6 +8,7 @@ #include #include #include +#include namespace uavcan_stm32h7 { @@ -376,7 +377,7 @@ public: const int res = driver.init(inout_bitrate, CanIface::SilentMode, enabledInterfaces_); - usleep(1000000); + px4_usleep(1000000); if (res >= 0) { for (uavcan::uint8_t iface = 0; iface < driver.getNumIfaces(); iface++) { diff --git a/src/systemcmds/tests/test_sleep.c b/src/systemcmds/tests/test_sleep.c index b5dbf64567e..b15867d88ce 100644 --- a/src/systemcmds/tests/test_sleep.c +++ b/src/systemcmds/tests/test_sleep.c @@ -71,6 +71,19 @@ int test_sleep(int argc, char *argv[]) } } + // longer than the second that usleep() has to support + printf("\t 1.2 s sleep\n"); + fflush(stdout); + + const hrt_abstime time_start = hrt_absolute_time(); + const int ret = px4_usleep(1200000); + const hrt_abstime elapsed = hrt_absolute_time() - time_start; + + if (ret != 0 || elapsed < 1200000) { + PX4_ERR("\t Sleep test failed, returned %d after %llu us", ret, (unsigned long long)elapsed); + return PX4_ERROR; + } + printf("\t Sleep test successful.\n"); return OK;