Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
28 commits
Select commit Hold shift + click to select a range
7ea337b
modules: declare the modules/zephyr submodule and where its dependenc…
davidbuzz Sep 8, 2026
522395c
modules: pin modules/zephyr to a Zephyr fork with the patches the HAL…
davidbuzz Sep 8, 2026
16b99a8
AP_HAL: allocate HAL_BOARD_ZEPHYR and add the Zephyr board header
davidbuzz Oct 8, 2026
ae26e6f
waf: add the Zephyr backend, hwdef and devicetree generators, board c…
davidbuzz Oct 8, 2026
7bd3413
AC_AttitudeControl: name the offset temporary in set_posvelaccel_offset
davidbuzz Oct 8, 2026
b3ebb91
AP_Filesystem: make the FATFS, dirent and errno paths build on Zephyr
davidbuzz Oct 8, 2026
a8ca918
AP_InertialSensor: run the NONE IMU backend on Zephyr and slim its mo…
davidbuzz Oct 8, 2026
c10ac7b
AP_Notify: ignore a null backend in add_backend_helper()
davidbuzz Oct 8, 2026
8f528f8
AP_Scheduler: board-settable LOOP_RATE default and Zephyr overtime print
davidbuzz Oct 8, 2026
2d534e2
AP_CANManager: resolve HAL_CANIface when building for Zephyr
davidbuzz Oct 8, 2026
494f46b
GCS_MAVLink: route GCS_SEND_TEXT through one function, MAVFTP fixes a…
davidbuzz Oct 8, 2026
5822667
AP_Logger: Zephyr io thread timeout and io_timer diagnostics
davidbuzz Oct 8, 2026
699387e
AP_InternalError: ignore the imu_reset internal error on Zephyr as on…
davidbuzz Oct 8, 2026
09ee6c8
AP_RCProtocol: guard the protocol-detection path against a null RC_Ch…
davidbuzz Oct 8, 2026
5163631
AP_HAL_SITL: guard I2CBus::_ioctl() against a missing SITL::SIM singl…
davidbuzz Oct 8, 2026
aaf5e5a
AP_BoardConfig: arm the watchdog by default on Zephyr boards too
davidbuzz Oct 8, 2026
ba2e0b9
AP_RCTelemetry: retry CRSF telemetry init instead of giving up for th…
davidbuzz Oct 8, 2026
2d5c302
AP_GPS: allow a board to override the health gates
davidbuzz Oct 8, 2026
4b743c5
AP_NavEKF3: don't downgrade optimisation when built at -O3
davidbuzz Oct 8, 2026
c1f8ae2
AP_IOMCU: compile on Zephyr by guarding <ch.h> and casting a %lx argu…
davidbuzz Oct 8, 2026
576e358
AP_Arming: send the rudder-disarm message through GCS_SEND_TEXT
davidbuzz Oct 8, 2026
efa9918
AP_HAL_Zephyr: new HAL backend on Zephyr, board targets and documenta…
davidbuzz Oct 8, 2026
ee3bd88
AP_Bootloader: port to Zephyr for the i.MX RT1176 and the STM32H743
davidbuzz Oct 8, 2026
a4638a9
bootloaders: add prebuilt bootloaders for CubeOrangeZephyr and mr_vmu…
davidbuzz Oct 8, 2026
383b066
Copter: fix spool-up block without takeoff checks, stop fast-rate gov…
davidbuzz Oct 8, 2026
ae2e967
Tools: Zephyr bench scripts, Renode models, ramtext renamer, CPUInfo
davidbuzz Oct 8, 2026
345b83f
.github: add a Renode CI workflow: both Zephyr boards and a ChibiOS r…
davidbuzz Oct 8, 2026
037b8fd
docs: mr_vmu_rt1176 pre-flight records
davidbuzz Oct 8, 2026
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
613 changes: 613 additions & 0 deletions .github/workflows/test_renode_zephyr.yml

Large diffs are not rendered by default.

1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -178,3 +178,4 @@ env.bak/
venv.bak/
autotest_result_*_junit.xml
.python-version
binaries/*
12 changes: 12 additions & 0 deletions .gitmodules
Original file line number Diff line number Diff line change
@@ -1,3 +1,15 @@
[submodule "modules/zephyr"]
path = modules/zephyr
url = https://github.com/davidbuzz/zephyr.git
branch = main_buzz
shallow = false

# NOTE: Zephyr dependency repos (modules/zephyr/modules/..., bootloader/...)
# are NOT git submodules here: git cannot track paths inside the
# modules/zephyr gitlink. Their single source of truth (name/url/SHA/path)
# is Tools/zephyr/zephyr_manifest_v4_4_0_map.tsv; run
# Tools/zephyr/zephyr_get_prerequisites.sh to (re)generate the checkouts.

[submodule "modules/waf"]
path = modules/waf
url = https://github.com/ArduPilot/waf.git
Expand Down
94 changes: 89 additions & 5 deletions ArduCopter/rate_thread.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -164,6 +164,18 @@ static inline bool run_decimated_callback(uint8_t decimation_rate, uint8_t& deci
/*
thread for rate control
*/
/*
Minimum time between successive INCREASES of the attitude rate. The decrease
path deliberately keeps the 100 ms check, so CPU protection is unchanged; only
the climb is slowed. Without this both directions moved one rung per 100 ms,
so a board that cannot sustain its configured rate climbed the whole ladder in
~400 ms, overran, walked back down in ~400 ms and repeated - a limit cycle of
about 1 s, observed on an RT1176 cycling 202/253/337/506/1012 Hz continuously.
*/
#ifndef RATE_INCREASE_SETTLE_MS
#define RATE_INCREASE_SETTLE_MS 1000
#endif

void Copter::rate_controller_thread()
{
uint8_t target_rate_decimation = constrain_int16(g2.att_decimation.get(), 1,
Expand All @@ -183,12 +195,24 @@ void Copter::rate_controller_thread()
uint32_t now_ms = AP_HAL::millis();
uint32_t last_rate_check_ms = 0;
uint32_t last_rate_increase_ms = 0;
/* MEASUREMENT, not for merge: the raw gyro rate and the rate the attitude
loop actually achieves are two of the numbers this board is judged on,
and nothing reports them. The governor's statustext only fires when the
rate CHANGES, so a settled governor is silent and indistinguishable from
one that never ran. */
uint32_t last_rate_report_ms = 0;
uint32_t rate_iter_total = 0;
uint32_t last_iter_total = 0;
uint32_t report_dt_base_ms = AP_HAL::millis();
#if HAL_LOGGING_ENABLED
uint32_t last_rtdt_log_ms = now_ms;
#endif
uint32_t last_notch_sample_ms = now_ms;
bool was_using_rate_thread = false;
bool notify_fixed_rate_active = true;
/* Highest decimation (lowest rate) rung that has been found unsustainable.
0 means none known yet, which permits the initial climb. */
uint8_t failed_decimation = 0;
bool was_armed = false;
uint32_t running_slow = 0;
#ifdef RATE_LOOP_TIMING_DEBUG
Expand Down Expand Up @@ -258,6 +282,13 @@ void Copter::rate_controller_thread()
if (AP::scheduler().get_extra_loop_us() == 0) {
rate_loop_count++;
}
/* MEASUREMENT, not for merge: rate_loop_count above is NOT a rate - it
is gated on get_extra_loop_us() and the governor zeroes it - and
att_hz in the report below is the NOMINAL rate (gyro rate divided by
decimation), not a measurement. This counts every iteration
unconditionally, so the delta between two reports over a known
interval is the rate the thread actually achieves. */
rate_iter_total++;

// run the rate controller on all available samples
// it is important not to drop samples otherwise the filtering will be fubar
Expand Down Expand Up @@ -365,16 +396,42 @@ void Copter::rate_controller_thread()
|| target_rate_decimation > rate_decimation) {
const uint8_t new_rate_decimation = MAX(rate_decimation + 1, target_rate_decimation);
const uint32_t new_attitude_rate = ins.get_raw_gyro_rate_hz() / new_rate_decimation;
if (new_attitude_rate > AP::scheduler().get_filtered_loop_rate_hz()) {
/* Configured rate, not the measured one. get_filtered_loop_rate_hz()
is 1/filtered_loop_time, a first-order low-pass with a ~100
sample time constant - about 0.5 s at 200 Hz - so the floor
moved with the lagged MEASURED loop rate. That is a feedback
path: the more the main loop recovers, the higher the minimum
attitude rate this accepts, which loads the main loop again.
With a lag comparable to the hunt period it is the textbook
condition for a limit cycle. Measured on a board running
193-308 Hz, the floor wandered across three of the five rungs.
get_loop_rate_hz() is the stable parameter value. */
if (new_attitude_rate > AP::scheduler().get_loop_rate_hz()) {
rate_decimation = new_rate_decimation;
rate_controller_set_rates(rate_decimation, rates, true);
prev_loop_count = rate_loop_count;
rate_loop_count = 0;
running_slow = 0;
/* Remember the rung that just proved unsustainable, so the
climb below does not walk straight back onto it. */
failed_decimation = rate_decimation - 1;
}
} else if (rate_decimation > target_rate_decimation && rate_loop_count > att_rate/10 // ensure 100ms worth of good readings
/* SETTLE TIME on the way UP only; the way down stays at the
100 ms check above so CPU protection is unchanged. Both
directions previously used the same 100 ms gate and moved one
rung, which makes a board that cannot reach its target rate
oscillate by construction - fast-down, fast-up. A governor
needs fast-down, slow-up. */
&& now_ms - last_rate_increase_ms >= RATE_INCREASE_SETTLE_MS
&& (prev_loop_count > att_rate/10 // ensure there was 100ms worth of good readings at the higher rate
|| prev_loop_count == 0 // last rate was actually a lower rate so keep going quickly
/* Was "|| prev_loop_count == 0", which made the 10 s retry
below unreachable: the up-step itself sets prev_loop_count
to 0 a few lines down, so after the first climb this
clause was always true and the retry never consulted. The
added test keeps the fast path below a known-bad rung and
defers to the 10 s retry at or past it. */
|| (prev_loop_count == 0 && rate_decimation - 1 > failed_decimation)
|| now_ms - last_rate_increase_ms >= 10000)) { // every 10s retry
rate_decimation = rate_decimation - 1;

Expand All @@ -400,6 +457,21 @@ void Copter::rate_controller_thread()
}
#endif

if (now_ms - last_rate_report_ms >= 10000) {
last_rate_report_ms = now_ms;
const uint32_t iters = rate_iter_total - last_iter_total;
last_iter_total = rate_iter_total;
hal.console->printf("RATELOOP gyro_hz=%u dec=%u nominal_hz=%u measured_hz=%u "
"tgt_dec=%u iters=%lu\n",
unsigned(ins.get_raw_gyro_rate_hz()),
unsigned(rate_decimation),
unsigned(ins.get_raw_gyro_rate_hz() / MAX(rate_decimation, 1)),
unsigned((uint64_t)iters * 1000U / MAX(now_ms - report_dt_base_ms, 1U)),
unsigned(target_rate_decimation),
(unsigned long)iters);
report_dt_base_ms = now_ms;
}

was_using_rate_thread = true;
}
}
Expand Down Expand Up @@ -427,9 +499,21 @@ void Copter::rate_controller_set_rates(uint8_t rate_decimation, RateControllerRa
attitude_control->set_notch_sample_rate(attitude_rate);
hal.rcout->set_dshot_rate(SRV_Channels::get_dshot_rate(), attitude_rate);
motors->set_dt_s(1.0f / attitude_rate);
gcs().send_text(warn_cpu_high ? MAV_SEVERITY_WARNING : MAV_SEVERITY_INFO,
"Rate CPU %s, rate set to %uHz",
warn_cpu_high ? "high" : "normal", (unsigned) attitude_rate);
/* Rate-limited to one message per 10 s. The governor steps the rate up and
down whenever the CPU measurement crosses its threshold, and on a board
that cannot sustain the top rate it hunts continuously - observed cycling
202/253/337/506/1012 Hz and back, several messages per second. That floods
the GCS link and buries everything else. The rate still changes as often
as it needs to; only the reporting is throttled, and a change that is
suppressed is still visible in the next message's value. */
static uint32_t last_rate_msg_ms; // rate thread only, no other caller
const uint32_t now_ms = AP_HAL::millis();
if (last_rate_msg_ms == 0 || now_ms - last_rate_msg_ms >= 10000) {
last_rate_msg_ms = now_ms;
gcs().send_text(warn_cpu_high ? MAV_SEVERITY_WARNING : MAV_SEVERITY_INFO,
"Rate CPU %s, rate set to %uHz",
warn_cpu_high ? "high" : "normal", (unsigned) attitude_rate);
}
#if HAL_LOGGING_ENABLED
if (attitude_rate > 1000) {
rates.fast_logging_rate = calc_gyro_decimation(rate_decimation, 1000); // 1Khz
Expand Down
8 changes: 8 additions & 0 deletions ArduCopter/takeoff_check.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -51,5 +51,13 @@ void Copter::takeoff_check()
gcs().send_text(MAV_SEVERITY_CRITICAL, "%s CPU overload (%4.1f%%)", prefix_str, avg_load);
}
}
#else
// AP_MotorsMulticopter blocks spool-up once spin-up completes and waits for
// this function to clear it. The checks above are the only code that ever
// does, so on a board built without ESC telemetry - no serial rcout and no
// CAN - the block is set and never lifted: the motors stay in GROUND_IDLE
// and the vehicle cannot take off at all. There is nothing to check here,
// so clear it.
motors->set_spoolup_block(false);
#endif
}
54 changes: 43 additions & 11 deletions Tools/AP_Bootloader/AP_Bootloader.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,32 +12,38 @@
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
/*
ArduPilot bootloader. This implements the same protocol originally
developed for PX4, but builds on top of the ChibiOS HAL

It does not use the full AP_HAL API in order to keep the firmware
size below the maximum of 16kByte required for F4 based
boards. Instead it uses the ChibiOS APIs directly
*/
/* ArduPilot bootloader, implementing the same protocol as the original. */

#include <AP_HAL/AP_HAL.h>
#ifdef __ZEPHYR__
// TODO(zephyr-bootloader, UNTESTED): no ChibiOS runtime/usbcfg/watchdog/CAN/
// flash-from-sd/network support below - see hwdef_zephyr.h and
// support_Zephyr.cpp for what's actually implemented.
#include <zephyr/kernel.h>
#include "hwdef_zephyr.h"
#define BOOTLOADER_DEV_LIST 1
#else
#include "ch.h"
#include "hal.h"
#include "hwdef.h"
#include <AP_HAL_ChibiOS/hwdef/common/usbcfg.h>
#include <AP_HAL_ChibiOS/hwdef/common/stm32_util.h>
#include <AP_HAL_ChibiOS/hwdef/common/watchdog.h>
#include "support.h"
#include "bl_protocol.h"
#include "flash_from_sd.h"
#include "can.h"
#include "network.h"
#endif

// self-guarding: defines AP_BOOTLOADER_MCUBOOT_AB 0 unless a board sets it,
// so the #if below is legal on boards that have never heard of MCUBoot A/B
#include "mcuboot_ab.h"
#include "support.h"
#include "bl_protocol.h"
#include <stdio.h>
#if EXT_FLASH_SIZE_MB
#include <AP_FlashIface/AP_FlashIface_JEDEC.h>
#endif
#include <AP_CheckFirmware/AP_CheckFirmware.h>
#include "network.h"

extern "C" {
int main(void);
Expand All @@ -47,7 +53,13 @@ struct boardinfo board_info = {
.board_type = APJ_BOARD_ID,
.board_rev = 0,
.fw_size = (BOARD_FLASH_SIZE - (FLASH_BOOTLOADER_LOAD_KB + FLASH_RESERVE_END_KB + APP_START_OFFSET_KB))*1024,
#ifdef __ZEPHYR__
// No separate external-flash region on this board - see hwdef_zephyr.h:
Comment thread
davidbuzz marked this conversation as resolved.
Resolved
// the whole FlexSPI-mapped flash is accounted for via fw_size above.
.extf_size = 0
#else
.extf_size = (EXT_FLASH_SIZE_MB * 1024 * 1024) - (EXT_FLASH_RESERVE_START_KB + EXT_FLASH_RESERVE_END_KB) * 1024
#endif
};

#ifndef HAL_BOOTLOADER_TIMEOUT
Expand All @@ -66,14 +78,30 @@ AP_FlashIface_JEDEC ext_flash;
static BL_Network network;
#endif

#ifdef __ZEPHYR__
/* On Zephyr, main() belongs to the Zephyr application shim. */
extern "C" int ardupilot_entry(int argc, char *const argv[]);
extern "C" int ardupilot_entry(int argc, char *const argv[])
{
(void)argc;
(void)argv;
#else
int main(void)
{
#endif
#ifdef AP_BOOTLOADER_CUSTOM_HERE4
custom_startup();
#endif

flash_init();

#if AP_BOOTLOADER_MCUBOOT_AB
// Overwrite-only A/B: if a signature-checked image is pending in slot 1,
// copy it over slot 0 before the normal boot flow. See mcuboot_ab.cpp and
// libraries/AP_HAL_Zephyr/BOOTLOADER_SECURITY.md.
(void)mcuboot_ab_update();
#endif

#if AP_FLASH_ECC_CHECK_ENABLED
check_ecc_errors();
#endif
Expand Down Expand Up @@ -200,7 +228,11 @@ int main(void)
while (!ext_flash.init()) {
// keep trying until we get it working
// there's no future without it
#ifdef __ZEPHYR__
k_msleep(20);
#else
chThdSleep(chTimeMS2I(20));
#endif
}
#endif

Expand Down
12 changes: 11 additions & 1 deletion Tools/AP_Bootloader/README.md
Original file line number Diff line number Diff line change
@@ -1,11 +1,17 @@
# ArduPilot Bootloader

This is the bootloader used for STM32 boards for ArduPilot. To build
This is the bootloader used for STM32 boards, and Zephyr boards, for ArduPilot. To build
the bootloader do this:

```bash
./waf configure --board BOARDNAME --bootloader
./waf bootloader
# and in some cases you can get help to flash it to a target.
./waf bootloader --upload

# after the above BL is on the target , you can do this to put the main app on the device with the BL.
./waf copter --upload
# or use uploader.py directoy for advanced needs.
```

the bootloader will be in build/BOARDNAME/bin. If you have the
Expand All @@ -27,3 +33,7 @@ hwdef-bl.dat files

The bootloader can load from USB or UARTs. The list of devices to load
from is given in the SERIAL_ORDER option in hwdef-bl.dat

for boards running on the Zephyr HAL, one of the binaries emitted by
the BL build is MCUBoot compatible, if you dont know what this is
dont worry, its a Zephyr thing.
Loading
Loading