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;