Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
20 commits
Select commit Hold shift + click to select a range
a7d4a0e
I2C CORE - baro + mag
error414 Oct 7, 2026
a4eaeec
I2C CORE - fix for F4 and AT32
error414 Oct 8, 2026
542dde4
I2C CORE - fix "A held bus can delay motor frames"
error414 Oct 8, 2026
8a021d4
I2C CORE - fix HITL Baro driver
error414 Oct 8, 2026
db55436
I2C CORE - fix rp2350 error handling
error414 Oct 8, 2026
4ca2c35
I2C CORE - update doc
error414 Oct 8, 2026
76bd08a
I2C CORE - fix code review
error414 Oct 11, 2026
90310ff
I2C CORE - apply PR fix #12113
error414 Oct 17, 2026
af07b5b
Merge branch 'maintenance-10.x' into error414/feature/ic_async/core
error414 Nov 2, 2026
71f1e29
I2C CORE - fix IRQ handling for AT32, add state machine instead of wa…
error414 Nov 2, 2026
7a3af1a
I2C CORE - for F7 change I2C HAL to direct register access to reduce …
error414 Nov 5, 2026
dfadf6b
Merge branch 'next-10.1' into error414/feature/ic_async/core
error414 Nov 6, 2026
ef05e53
Merge remote-tracking branch 'inav/next-10.1' into error414/feature/i…
error414 Nov 16, 2026
64091b4
I2C CORE - Delayed interrupts truncate I²C writes
error414 Nov 17, 2026
4b5ea37
I2C CORE - Software I²C reports some NACKs as success
error414 Nov 17, 2026
b071da2
I2C CORE - MPU9250 compass drops from 40 Hz to 10 Hz
error414 Nov 17, 2026
c992957
Update navigation.c
breadoven Nov 23, 2026
c032abc
Merge pull request #12251 from breadoven/abo_autotrans_airspeed_fix
breadoven Dec 2, 2026
fdbd29d
Merge maintenance-10.x into next-10.1 (automatic sync)
github-actions[bot] Dec 2, 2026
f3714b2
Merge pull request #12133 from error414/error414/feature/ic_async/core
sensei-hacker Dec 5, 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
1 change: 1 addition & 0 deletions AGENTS.md
Original file line number Diff line number Diff line change
Expand Up @@ -327,6 +327,7 @@ docs — check it before assuming something isn't documented:
versioning rules, when a new setting is warranted)
- `build-system.md` - CMake build system internals
- `pid-to-servo-computation.md` - PID-to-servo signal path (fixed-wing)
- `non-blocking-i2c.md` - Non-blocking I2C transfers (bus API, platform back-ends, timeouts and recovery, converting a driver)
- `msp/` - MSP protocol doc generation tooling
- Target-specific files in `/src/main/target/`

Expand Down
4 changes: 1 addition & 3 deletions cmake/stm32f7.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,6 @@ set(STM32F7_HAL_SRC
stm32f7xx_hal_flash.c
stm32f7xx_hal_flash_ex.c
stm32f7xx_hal_gpio.c
stm32f7xx_hal_i2c.c
stm32f7xx_hal_i2c_ex.c
stm32f7xx_hal_pcd.c
stm32f7xx_hal_pcd_ex.c
stm32f7xx_hal_pwr.c
Expand Down Expand Up @@ -68,7 +66,7 @@ main_sources(STM32F7_SRC
config/config_streamer_extflash.c

drivers/adc_stm32f7xx.c
drivers/bus_i2c_hal.c
drivers/bus_i2c_stm32f7xx.c
drivers/dma_stm32f7xx.c
drivers/bus_spi_hal_ll.c
drivers/timer.c
Expand Down
1 change: 1 addition & 0 deletions docs/Settings.md
Original file line number Diff line number Diff line change
Expand Up @@ -803,6 +803,7 @@ Defines debug values exposed in debug variables (developer / debugging setting)
| ESC | |
| FW_TURN | |
| MAG | |
| I2C | |

---

Expand Down
212 changes: 212 additions & 0 deletions docs/development/non-blocking-i2c.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,212 @@
# Non-blocking I2C transfers

How a driver starts an I2C transfer without waiting for it, how the platform
drivers carry it to the end, and what happens when it fails. This document
covers the bus layer, the barometer and the compass, the first sensors
converted to it. The other sensor layers (pitot, rangefinder) follow in
later changes.

## Why

An I2C register read of 6 bytes at 400 kHz keeps the wire busy for about
200 µs. Every `busReadBuf()` spins in the platform driver until the whole
transfer has finished, so a BARO task that needs a few microseconds of CPU
shows up as 100 to 300 µs in `tasks`, and the compass, pitot and
rangefinder tasks behave the same way.

With the calls below a task starts a transfer, returns, and picks up the
result on a later run. The wire time stays the same, the CPU time moves to a
handful of short interrupts. The model follows Betaflight: no bus queue, no
DMA, one outstanding transfer per bus, the owner polls for its completion.

## Platform drivers (`drivers/bus_i2c_*.c`)

Every platform implements the same `drivers/bus_i2c.h` API:

| Function | Behaviour |
|---|---|
| `i2cRead()`, `i2cWrite()`, `i2cWriteBuffer()` | Blocking, unchanged for callers. They first wait for a pending non-blocking transfer, so drivers that were not converted keep working on a shared bus. |
| `i2cReadStart()`, `i2cWriteStart()`, `i2cWriteBufferStart()` | Start a transfer and return at once. `false` means nothing was started (bus busy or start failed), try again later. |
| `i2cBusy(device, addr, &error)` | `true` while the given slave's non-blocking transfer runs. `error` reports the outcome of that slave's last completed transfer; the result is kept per address, so another device using the bus in between does not overwrite it. |

| Platform | Implementation |
|---|---|
| STM32F4 | Own interrupt-driven engine in `bus_i2c_stm32f40x.c`, ported from the Betaflight `bus_i2c_stm32f4xx.c` driver. Blocking calls use the same engine and wait for it. |
| STM32F7 | Own interrupt-driven engine on the I2C registers in `bus_i2c_stm32f7xx.c`, the ST HAL I2C module is not built. One CR2 write starts a transfer, the handler feeds TXDR / drains RXDR, issues the repeated START of a register read at TC and finishes at STOPF. Blocking calls use the same engine and wait for it. |
| STM32H7 | `HAL_I2C_Mem_Read_IT()` and friends in `bus_i2c_hal.c`, completion through the `HAL_I2C_*CpltCallback()` / `HAL_I2C_ErrorCallback()` hooks. |
| AT32F43x | The `i2c_application` interrupt handler for the data phase. Its `i2c_memory_*_int()` calls busy-wait for the slave and register address, so the driver does not use them: it sends the register byte from its own event interrupt wrapper and, for a read, issues the repeated START there at TDC (the register byte goes out with a software STOP). The start call only programs the peripheral and checks BUSYF once. |
| RP2350, software I2C, SITL | Synchronous: the start call completes the transfer and returns `true`, `i2cBusy()` is always `false` and carries the outcome. A NACK is a failed transfer, not a refused start. |

`i2cWriteStart()` copies the data byte into driver-owned storage, the caller
does not have to keep it alive. Buffers given to `i2cReadStart()` and
`i2cWriteBufferStart()` must stay valid until `i2cBusy()` reports idle, so
drivers use static buffers.

A write with a zero length sends the register byte alone (the HAL and AT32
blocking paths refuse zero-length transfers, the drivers wrap that). The F7
refuses a register write of 255 data bytes: with the register byte that is
256, one more than its NBYTES field holds.

## Bus layer (`drivers/bus.h`)

| Function | Behaviour |
|---|---|
| `busReadBufStart(dev, reg, buf, len)` | Non-blocking register read. On SPI the read completes inside the call. |
| `busWriteStart(dev, reg, data)` | Non-blocking single register write. |
| `busWriteBufStart(dev, reg, buf, len)` | Non-blocking multi-byte write, the buffer must stay valid until the bus is idle. A zero length sends the register byte alone. |
| `busIsBusy(dev, &error)` | Poll for completion of this device's transfer: `true` while it is on the bus, otherwise `error` carries its outcome. Another device's transfer on the same bus does not affect the result. SPI reports idle. |
| `busReadStepResult_e` | `BUS_READ_STEP_BUSY` (nothing started), `BUS_READ_STEP_NEXT` (intermediate transfer started), `BUS_READ_STEP_LAST` (final transfer started). Used by sensors whose sample needs several transfers. |

Raw access (`reg == 0xFF` with `DEVFLAGS_USE_RAW_REGISTERS`) works the same
way as for the blocking calls.

## Timeouts and recovery

A non-blocking transfer is abandoned when it makes no progress for
`I2C_TIMEOUT` (10 ms) while somebody looks at it: the peripheral is
reinitialised with `i2cInit()`, the error counter goes up and the failure is
recorded for the transfer's owner. Progress is the number of bytes still to
go (`XferCount` on the H7 HAL, `pcount` on the AT32, byte index and register
phase on the F4 and F7), not the time since the start:

* A gap between two looks longer than 1 ms counts as 1 ms. A main loop that
was busy elsewhere for a while (a settings save) does not get a transfer
that is merely late declared stuck, because the interrupts carried it on in
the meantime and the next look sees the progress.
* A transfer that really is stuck still times out when looked at seldom:
about ten looks at 1 ms intervals, or 10 ms of a blocking wait.

A NACK or a bus error ends a non-blocking transfer at once: the error is
recorded for its owner and the counter shown by `status` goes up. The
peripheral is not reinitialised for that, it is fine after a NACK, so an
unplugged sensor costs one short failed transfer per attempt and nothing
more. The F4 and F7 drivers
do not reinitialise on a NACK for blocking calls either; the H7 HAL and AT32
blocking calls keep reinitialising on failure, as before.

The F4 handlers never wait on the bus or reinitialise the peripheral
themselves. The I2C interrupts sit above the motor timers and an unstick
clocks the bus for up to a few ms, so a transfer the handler has to give up
on (a bus error with a START still pending) only flags the bus: the next
`i2cBusy()` or start call on that bus finishes the cycle with a STOP and
reinitialises the peripheral from task context. The repeated START after the
register address is programmed without waiting for it to go out: the handler
clears BTF with a DR read (the ChibiOS I2Cv1 sequence) so the event interrupt
does not re-enter until the START is on the bus and EV5 fires. A repeated
START that never goes out on a held bus shows up as a stuck transfer through
the progress timeout, like any other stalled phase.

The F7 handler follows the same rule. A NACK needs nothing from it: the
hardware sends the STOP itself and the transfer ends at STOPF with the error
recorded. A bus error or an arbitration loss leaves the peripheral anywhere
in the transfer, so the handler gives up on it and flags the bus; the next
`i2cBusy()` or start call reinitialises the peripheral from task context,
like on the F4.

A bus that refuses every start as busy with no transfer of ours in flight
(`HAL_BUSY` on the H7, `BUSYF` on the AT32) is a fault the stuck detection
cannot see, since nothing is in progress. The drivers count the refused
starts the same way as the stuck looks and reinitialise the peripheral after
`I2C_TIMEOUT` of them; the error counter goes up and the failure is recorded
for the address that asked, so the owner sees it on its next `busIsBusy()`.
The F4 and F7 engines do not check the busy flag before a start: the F7
peripheral sends the START once the bus is free, so a held bus shows up on
both as a stuck transfer.

A blocking call issued while a non-blocking transfer is on the bus waits for
it with the same stuck detection, then runs as before.

## Barometer (`sensors/barometer.c`, hooks in `drivers/barometer/barometer.h`)

```
start_ut -> (ut_delay) -> read_ut -> get_ut -> start_up -> (up_delay) -> read_up -> get_up -> calculate
```

* `start_*` triggers a measurement, usually a `busWriteStart()`. The
matching `read_*` is only called once that write is over; a write that
failed (NACK, bus error) restarts the phase, since the sensor's data
registers would still hold the previous conversion.
* `read_*` starts the data transfer with `busReadBufStart()` and returns
`false` when the bus is busy.
* `get_*` only parses the static buffer and returns `false` when the sample
is unusable (the phase is restarted).
* `combined_read = true` skips the temperature phase for sensors that deliver
both values in one read.
* Drivers without `read_*` hooks keep their blocking `get_*` and run exactly
as before.

While a transfer is on the bus the task asks to be called again after 1 ms,
otherwise it returns to the sensor's delays. A delay of 0 keeps the current
task period. The position estimator is fed only when a new sample was
produced, the 1 ms steps bring it nothing.

Converted so far:

* **DPS310 / SPL07-003**: the sensor runs in continuous mode. The temperature
phase reads `MEAS_CFG` every `ut_delay` and `get_ut` checks `PRS_RDY`; a
clear flag restarts the phase, a set flag moves on to the pressure phase,
which reads the six result bytes one scheduler step later. Same sample
rate as before, the sensor's 32 per second.
* **SPL06-001**: command mode, two phases. `start_ut` fires the temperature
command, the temperature bytes are read after `ut_delay`, then the same for
the pressure. A sample takes `ut_delay + up_delay` plus two 1 ms steps,
practically the previous cadence.
* **BMP280 / BME280**: forced mode. `start_up` fires the one-byte trigger
write, the pressure phase reads the six-byte frame after `up_delay`, and
the next trigger follows right after the parse. Note the cadence: a sample
now takes `up_delay` plus one 1 ms step instead of `2 * up_delay`, because
the blocking cycle spent its second half idle. For the default
oversampling that is about 24 ms per sample instead of 46 ms.

The remaining barometer drivers are untouched and still block.

## Compass (`sensors/compass.c`, hooks in `drivers/compass/compass.h`)

`readStart(mag, firstStep)` returns a `busReadStepResult_e`. It is called with
`firstStep` set for the first transfer of a sample and again after every
completed transfer until it returns `BUS_READ_STEP_LAST`; `read()` then only
parses. `compassUpdate()` returns the delay until its next call: 1 ms while a
transfer is on the bus, the regular 100 ms period once the sample was
handled. Drivers without `readStart` keep their blocking `read()`.

Converted so far: QMC5883L and QMC5883P, two transfers per sample. The status
byte is read first; the six data bytes only when `DRDY` confirms a fresh
sample, otherwise `read()` reports the miss as before. A sample costs three
task runs about 1 ms apart, then the task sleeps for the rest of the period.

## Converting a driver

1. Replace the bus access in the periodic measurement path with a static
buffer and a `busReadBufStart()` / `busWriteStart()` call. Return "busy"
to the caller when the start call refuses, and try again on the next run.
2. Pick up the result on a later run: `busIsBusy(dev, &error)` is `false`
once the transfer is over, `error` says whether the buffer is valid.
3. Keep the parse step free of bus access.
4. Leave initialisation and detection on the blocking calls; they wait for a
pending transfer first, so they are safe on a shared bus.
5. One outstanding transfer per device. A sample made of several transfers
(status, then data) takes several runs.

Drivers for other buses (SPI gyros, MSP, fake sensors) need no change.

## Debugging

`set debug_mode = I2C`, then `showdebug` in the CLI:

| Field | Meaning |
|---|---|
| `debug[0]` | interrupts during the last transfer (about 10 for a 6 byte read on F4, 8 or 9 on F7) |
| `debug[1]` | error interrupts since boot |
| `debug[2]` | duration of the last transfer in µs (about 200 for 6 bytes at 400 kHz) |
| `debug[3]` | longest single interrupt handler run in µs (F4 and F7) |
| `debug[4]` | longest wait for a START / STOP bit release in µs (F4 only) |
| `debug[5]` | I2C error counter, also shown by `status` |
| `debug[6]` | F4: interrupt enables found at transfer start (1024 is normal). F7: ISR flags at the last error (16 NACK, 256 bus error, 512 arbitration lost) |
| `debug[7]` | time spent in the start call in µs, should be a few µs |

## Known limitations

* No bus arbitration queue: a device that finds the bus busy retries later.
Fine at the 10 to 60 Hz rates the sensors use.
* The RP2350 driver is still blocking.
* SPI stays synchronous.
1 change: 1 addition & 0 deletions src/main/build/debug.h
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,7 @@ typedef enum {
DEBUG_ESC,
DEBUG_FW_TURN,
DEBUG_MAG,
DEBUG_I2C,
DEBUG_COUNT // also update debugModeNames in cli.c
} debugType_e;

Expand Down
12 changes: 12 additions & 0 deletions src/main/drivers/barometer/barometer.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,13 +23,25 @@ struct baroDev_s;
typedef bool (*baroOpFuncPtr)(struct baroDev_s * baro);
typedef bool (*baroCalculateFuncPtr)(struct baroDev_s * baro, int32_t *pressure, int32_t *temperature);

/*
* Measurement cycle driven by baroUpdate(): start_ut -> (ut_delay) -> read_ut -> get_ut -> start_up -> (up_delay) -> read_up -> get_up -> calculate
*
* A driver may leave any hook NULL. The optional read_* hooks start a non-blocking bus transfer and must return false
* when the bus is busy; the matching get_* then only parses the received buffer once the bus reports idle and returns
* false if the sample is not usable (the phase is restarted). A start_* hook paired with a read_* hook may start a
* non-blocking write: read_* is only called once that write is over and the phase is restarted if it failed. Drivers
* without read_* hooks do their blocking bus access inside get_*. A delay of 0 keeps the current task period.
*/
typedef struct baroDev_s {
busDevice_t * busDev;
bool combined_read; // get_up delivers temperature as well, the temperature phase is skipped
uint16_t ut_delay;
uint16_t up_delay;
baroOpFuncPtr start_ut;
baroOpFuncPtr read_ut;
baroOpFuncPtr get_ut;
baroOpFuncPtr start_up;
baroOpFuncPtr read_up;
baroOpFuncPtr get_up;
baroCalculateFuncPtr calculate;
} baroDev_t;
56 changes: 20 additions & 36 deletions src/main/drivers/barometer/barometer_bmp280.c
Original file line number Diff line number Diff line change
Expand Up @@ -54,51 +54,30 @@ STATIC_UNIT_TESTED bmp280_calib_param_t bmp280_cal;
int32_t bmp280_up = 0;
int32_t bmp280_ut = 0;

static bool bmp280_start_ut(baroDev_t * baro)
{
UNUSED(baro);
return true;
}

static bool bmp280_get_ut(baroDev_t * baro)
{
UNUSED(baro);
return true;
}
// Target of the non-blocking data read, filled by the bus driver in the background
static uint8_t bmp280_data[BMP280_DATA_FRAME_SIZE];

static bool bmp280_start_up(baroDev_t * baro)
{
// start measurement
// set oversampling + power mode (forced), and start sampling
busWrite(baro->busDev, BMP280_CTRL_MEAS_REG, BMP280_MODE);
return true;
return busWriteStart(baro->busDev, BMP280_CTRL_MEAS_REG, BMP280_MODE);
}

static bool bmp280_get_up(baroDev_t * baro)
static bool bmp280_read_up(baroDev_t * baro)
{
uint8_t data[BMP280_DATA_FRAME_SIZE];

//error free measurements
static int32_t bmp280_up_valid;
static int32_t bmp280_ut_valid;
// kick off the read of pressure and temperature, bmp280_get_up() parses it once the bus is idle
return busReadBufStart(baro->busDev, BMP280_PRESSURE_MSB_REG, bmp280_data, BMP280_DATA_FRAME_SIZE);
}

//read data from sensor
bool ack = busReadBuf(baro->busDev, BMP280_PRESSURE_MSB_REG, data, BMP280_DATA_FRAME_SIZE);
static bool bmp280_get_up(baroDev_t * baro)
{
UNUSED(baro);

//check if pressure and temperature readings are valid, otherwise use previous measurements from the moment
if (ack) {
bmp280_up = (int32_t)((((uint32_t)(data[0])) << 12) | (((uint32_t)(data[1])) << 4) | ((uint32_t)data[2] >> 4));
bmp280_ut = (int32_t)((((uint32_t)(data[3])) << 12) | (((uint32_t)(data[4])) << 4) | ((uint32_t)data[5] >> 4));
bmp280_up_valid = bmp280_up;
bmp280_ut_valid = bmp280_ut;
}
else {
//assign previous valid measurements
bmp280_up = bmp280_up_valid;
bmp280_ut = bmp280_ut_valid;
}
bmp280_up = (int32_t)((((uint32_t)(bmp280_data[0])) << 12) | (((uint32_t)(bmp280_data[1])) << 4) | ((uint32_t)bmp280_data[2] >> 4));
bmp280_ut = (int32_t)((((uint32_t)(bmp280_data[3])) << 12) | (((uint32_t)(bmp280_data[4])) << 4) | ((uint32_t)bmp280_data[5] >> 4));

return ack;
return true;
}

// Returns temperature in DegC, resolution is 0.01 DegC. Output value of "5123" equals 51.23 DegC
Expand Down Expand Up @@ -197,12 +176,17 @@ bool bmp280Detect(baroDev_t *baro)
// set oversampling + power mode (forced), and start sampling
busWrite(baro->busDev, BMP280_CTRL_MEAS_REG, BMP280_MODE);

// Forced mode: one measurement per cycle, pressure and temperature come out of one read
baro->combined_read = true;

baro->ut_delay = 0;
baro->get_ut = bmp280_get_ut;
baro->start_ut = bmp280_start_ut;
baro->start_ut = NULL;
baro->read_ut = NULL;
baro->get_ut = NULL;

baro->up_delay = ((T_INIT_MAX + T_MEASURE_PER_OSRS_MAX * (((1 << BMP280_TEMPERATURE_OSR) >> 1) + ((1 << BMP280_PRESSURE_OSR) >> 1)) + (BMP280_PRESSURE_OSR ? T_SETUP_PRESSURE_MAX : 0) + 15) / 16) * 1000;
baro->start_up = bmp280_start_up;
baro->read_up = bmp280_read_up;
baro->get_up = bmp280_get_up;

baro->calculate = bmp280_calculate;
Expand Down
Loading
Loading