mirror of
https://github.com/PX4/PX4-Autopilot.git
synced 2026-11-29 11:40:28 +08:00
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
This commit is contained in:
1 parent
c06973172f
commit
9a1429ae90
15 files changed
+90
-15
No files matched your search
@@ -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)
|
||||
|
||||
@@ -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)
|
||||
|
||||
@@ -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)
|
||||
|
||||
@@ -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)
|
||||
|
||||
@@ -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)
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -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
|
||||
|
||||
|
||||
@@ -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 <px4_platform_common/time.h>
|
||||
|
||||
#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
|
||||
@@ -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;
|
||||
|
||||
|
||||
@@ -202,7 +202,7 @@ mip_cmd_result MicroStrain::forceIdle()
|
||||
break;
|
||||
|
||||
} else {
|
||||
usleep(1_s);
|
||||
px4_usleep(1_s);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -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(".");
|
||||
}
|
||||
|
||||
|
||||
@@ -9,6 +9,7 @@
|
||||
#include <uavcan_kinetis/thread.hpp>
|
||||
#include <uavcan/driver/can.hpp>
|
||||
#include <uavcan_kinetis/flexcan.hpp>
|
||||
#include <px4_platform_common/time.h>
|
||||
|
||||
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++) {
|
||||
|
||||
@@ -8,6 +8,7 @@
|
||||
#include <uavcan_stm32/thread.hpp>
|
||||
#include <uavcan/driver/can.hpp>
|
||||
#include <uavcan_stm32/bxcan.hpp>
|
||||
#include <px4_platform_common/time.h>
|
||||
|
||||
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++) {
|
||||
|
||||
@@ -8,6 +8,7 @@
|
||||
#include <uavcan_stm32h7/thread.hpp>
|
||||
#include <uavcan/driver/can.hpp>
|
||||
#include <uavcan_stm32h7/fdcan.hpp>
|
||||
#include <px4_platform_common/time.h>
|
||||
|
||||
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++) {
|
||||
|
||||
@@ -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;
|
||||
|
||||
Reference in new issue
Block a user