refactor(uavcan): build without NuttX headers (#29280)

* refactor(uavcan): drop unused NuttX headers

uavcan_main.cpp and uavcan_servers.cpp include arch/chip/chip.h, which neither uses, and uavcan_servers.cpp also includes nuttx/config.h, which px4_config.h already includes.

Assisted-by: Claude:opus-5.5
Signed-off-by: Benjamin Cabé <benjamin@zephyrproject.org>

* refactor(uavcan): build without NuttX headers

The DroneCAN driver included NuttX headers outside its NuttX-specific code and masked the interrupts with the NuttX functions directly. The interrupts are now masked with UavcanCriticalSection, which uses the NuttX functions on NuttX and a process-wide lock on POSIX, which has no interrupts, and the NuttX headers are only included on NuttX. On NuttX, the interrupts are masked as before.

Assisted-by: Claude:opus-5.5
Signed-off-by: Benjamin Cabé <benjamin@zephyrproject.org>

---------

Signed-off-by: Benjamin Cabé <benjamin@zephyrproject.org>
This commit is contained in:
Benjamin Cabé authored and GitHub committed 2026-10-17 12:09:24 -06:00
1 parent 63032c5ac9
commit e2f45b2fa2
5 files changed
+85 -22

No files matched your search

+2 -2
View File
@@ -39,6 +39,7 @@
#include <systemlib/err.h>
#include <uavcan/uavcan.hpp>
#include <drivers/uavcan/uavcan_drivers/critical_section.hpp>
#include <uavcan/helpers/heap_based_pool_allocator.hpp>
// TODO: Entire UAVCAN application should be moved into a namespace later; this is the first step.
@@ -46,8 +47,7 @@ namespace uavcan_node
{
struct AllocatorSynchronizer {
const ::irqstate_t state = ::enter_critical_section();
~AllocatorSynchronizer() { ::leave_critical_section(state); }
const UavcanCriticalSection lock;
};
struct Allocator : public uavcan::HeapBasedPoolAllocator<uavcan::MemPoolBlockSize, AllocatorSynchronizer> {
@@ -0,0 +1,67 @@
/****************************************************************************
*
* 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.
*
****************************************************************************/
#pragma once
#if defined(__PX4_NUTTX)
#include <nuttx/irq.h>
#elif defined(__PX4_POSIX)
#include <mutex>
#endif
/**
* Masks the interrupts in its scope, or, on POSIX, which has no interrupts, holds a process-wide lock.
*/
class UavcanCriticalSection
{
public:
#if defined(__PX4_NUTTX)
UavcanCriticalSection() : _flags(enter_critical_section()) {}
~UavcanCriticalSection() { leave_critical_section(_flags); }
private:
const irqstate_t _flags;
#elif defined(__PX4_POSIX)
UavcanCriticalSection() { mutex().lock(); }
~UavcanCriticalSection() { mutex().unlock(); }
private:
static std::recursive_mutex &mutex()
{
static std::recursive_mutex m;
return m;
}
#else
#error "UavcanCriticalSection has no implementation for this system"
#endif
};
+16 -14
View File
@@ -35,7 +35,7 @@
#include <stdint.h>
#include <drivers/drv_hrt.h>
#include <nuttx/irq.h>
#include "critical_section.hpp"
namespace uavcan_hrt_clock
{
@@ -70,21 +70,27 @@ public:
uint64_t utcUsec() const
{
irqstate_t flags = enter_critical_section();
const uint64_t utc = utcUsecFromInterrupt();
leave_critical_section(flags);
return utc;
const UavcanCriticalSection lock;
return utcUsecFromInterrupt();
}
/**
* UTC at an earlier HRT time, for example when a frame was received.
*/
uint64_t utcUsecAt(uint64_t hrt) const
{
const UavcanCriticalSection lock;
return _set ? utcAt(hrt) : 0;
}
void setUtc(uint64_t utc_usec)
{
irqstate_t flags = enter_critical_section();
const UavcanCriticalSection lock;
const uint64_t now = hrt_absolute_time();
_offset_usec = int64_t(utc_usec) - int64_t(now);
_anchor = now;
_last_adjustment_at = now;
_set = true;
leave_critical_section(flags);
}
/**
@@ -93,7 +99,7 @@ public:
*/
void adjustUtc(int64_t adjustment_usec)
{
irqstate_t flags = enter_critical_section();
const UavcanCriticalSection lock;
const uint64_t now = hrt_absolute_time();
if (!_set) {
@@ -125,16 +131,12 @@ public:
_adjustments++;
_last_adjustment_usec = int32_t(adjustment_usec);
}
leave_critical_section(flags);
}
SyncStatus status() const
{
irqstate_t flags = enter_critical_section();
const SyncStatus s{_adjustments, _last_adjustment_usec, int32_t((_rate_q32 * 1000000000LL) >> 32)};
leave_critical_section(flags);
return s;
const UavcanCriticalSection lock;
return SyncStatus{_adjustments, _last_adjustment_usec, int32_t((_rate_q32 * 1000000000LL) >> 32)};
}
private:
-2
View File
@@ -54,8 +54,6 @@
#include <parameters/param.h>
#include <version/version.h>
#include <arch/chip/chip.h>
#include <uORB/topics/esc_status.h>
#include <drivers/drv_hrt.h>
-4
View File
@@ -34,8 +34,6 @@
#include <px4_platform_common/tasks.h>
#include <drivers/drv_hrt.h>
#include <nuttx/config.h>
#include <cstdlib>
#include <cstring>
#include <cctype>
@@ -47,8 +45,6 @@
#include <parameters/param.h>
#include <version/version.h>
#include <arch/chip/chip.h>
#include "uavcan_main.hpp"
#include "uavcan_servers.hpp"