Skip to content

i2c: F7 master on its registers instead of ST's HAL, 3.6 KB less on F722 - #12157

Open
MrScothh wants to merge 1 commit into
iNavFlight:next-10.1from
MrScothh:feature/f7-i2c-registers
Open

MrScothh wants to merge 1 commit into
iNavFlight:next-10.1from
MrScothh:feature/f7-i2c-registers

Conversation

@MrScothh

Copy link
Copy Markdown
Contributor

This is meant as a stopgap for the F722 targets' flash until the non-blocking I2C of #12133 (issue #12100) lands. When it does, this driver either takes #12133's API in register form, or F7 goes back to bus_i2c_hal.c together with it. Until then the F722 targets are within a few KB of their 480 KB (MATEKF722SE 98.6 % on maintenance-10.x), and every open PR that adds code to them competes for that space.

bus_i2c_stm32f7xx.c replaces bus_i2c_hal.c on F7 only. H7 keeps the HAL driver, and its binaries don't change. The API and the behaviour are the same:

  • blocking transfers that poll the flags the HAL polls (TXIS, RXNE, TC, STOPF): the register and the data go in one write, and a read sends the register and then a repeated start;
  • a NACK, a bus error or I2C_TIMEOUT over the whole transfer is counted in the I2C error counter and answered with i2cInit(), which unsticks the bus, as before; a register transfer of zero bytes is refused, as HAL_I2C_Mem_Read/Write refused it;
  • TIMINGR is computed as bus_i2c_hal.c computes it. I compiled the two functions side by side: equal at 100, 200, 400 and 800 kHz for PCLK1 from 27 to 60 MHz in 250 kHz steps (532 cases);
  • no interrupts for the blocking calls, as the HAL's didn't use them either.

Two differences, both on purpose: an error flag already set together with the final STOPF fails the transfer (the HAL returned success and left the NACK for the next transfer), and a register write of 255 data bytes, 256 with the register byte and one more than NBYTES can hold, is refused. No driver writes more than a few bytes.

With #12063 (barometer read without waiting on the bus): it defines USE_I2C_ASYNC and gives bus_i2c_hal.c an interrupt driven i2cReadAsync() / i2cAsyncState(). This file has the same two under USE_I2C_ASYNC, with the same rules (a blocking transfer lets the read end first, a held bus or a stuck read fails and reinitialises the bus), so the two PRs can be merged in either order. On maintenance-10.x alone that part is not built. A test merge of the two builds on every target listed below, and against maintenance-10.x with #12063 it saves more, since #12063 adds the HAL's interrupt state machine: MAMBAF722_2022B -5544 B, MATEKF722 -5808 B, MATEKF722SE -5364 B, FLYWOOF722PRO -5560 B, KAKUTEF7 -6500 B, MATEKF765 -6824 B, and KAKUTEF7HDV -5880 B with its ITCM from 16384 of 16384 (full with #12063) back to 16232.

Size against maintenance-10.x:

Target Flash RAM
MAMBAF722_2022B -3608 B (97.79 %) -256 B
MATEKF722 -3608 B (96.87 %) -260 B
MATEKF722SE -3608 B (97.84 %) -256 B
FLYWOOF722PRO -3608 B (97.45 %) -256 B
KAKUTEF7 / KAKUTEF7HDV -4152 / -4072 B, ITCM unchanged -264 B
MATEKF765 -3276 B, ITCM -24 B -240 B
TBS_LUCID_H7_WING unchanged unchanged

bus_i2c_hal.c drives the F7 I2C through HAL_I2C_Mem_Read/Write and their
helpers, about 3.6 KB on F722 targets that are at their flash limit. This file
gives the same API with the same behaviour on the peripheral's registers:
blocking transfers that poll the same flags the HAL polls, a NACK or bus error
or I2C_TIMEOUT counted and answered with i2cInit() as before, a register
transfer of zero bytes refused as the HAL refused it, the same bus unstick,
and TIMINGR computed as bus_i2c_hal.c does (equal values for every speed at
PCLK1 from 27 to 60 MHz). The blocking calls use no interrupts, as the HAL's
didn't. H7 keeps bus_i2c_hal.c.

Under USE_I2C_ASYNC, which iNavFlight#12063 defines, it also has the interrupt driven
i2cReadAsync() and i2cAsyncState() that iNavFlight#12063 gives bus_i2c_hal.c, with the
same rules, so the two can be merged in either order.

Unlike the HAL, an error flag set together with the final STOPF fails the
transfer instead of leaving it for the next one. A register write of 255 data
bytes, one more than NBYTES can carry with the register byte, is refused; no
driver writes more than a few.
@qodo-code-review

Copy link
Copy Markdown
Contributor

ⓘ Qodo reviews are paused because the subscription is no longer active. Ask your workspace admin to reactivate the subscription to resume reviews. Manage billing

@qodo-free-for-open-source-projects

Copy link
Copy Markdown

PR Summary by Qodo

Replace F7 HAL I2C master with a register-based driver

✨ Enhancement 🕐 40+ Minutes

Grey Divider

AI Description

• Replace the F7 HAL I2C master to reclaim roughly 3.6 KB of F722 flash.
• Preserve blocking transfers, timing, and bus recovery; add conditional support for asynchronous
 reads.
• Leave H7 on its existing HAL driver.
Diagram

graph TD
  Build["F7 build"] --> Driver["Register driver"] --> Blocking["Blocking transfers"] --> Peripheral["I2C peripheral"]
  Clients["I2C callers"] --> Driver --> Async["Optional async reads"] --> Peripheral
  Blocking --> Recovery["Bus recovery"] --> Peripheral
  Async --> Recovery
Loading
High-Level Assessment

The following are alternative approaches to this PR:

1. Wait for the shared non-blocking I2C work
  • ➕ Avoids maintaining a temporary F7-specific driver alongside the HAL implementation.
  • ➖ Does not address the immediate flash pressure on F722 targets.
  • ➖ Depends on a separate change and its integration schedule.

Recommendation: Use the F7-specific driver as a flash-saving stopgap while retaining the existing API and H7 implementation. Reassess whether to adapt or retire it when the shared non-blocking I2C work lands; review its optional interrupt path particularly carefully because it is not built without USE_I2C_ASYNC.

Files changed (2) +462 / -1

Enhancement (1) +461 / -0
bus_i2c_stm32f7xx.cImplement F7 I2C master operations on peripheral registers +461/-0

Implement F7 I2C master operations on peripheral registers

• Adds blocking register and raw transfers, peripheral timing configuration, error counting, and bus reinitialization without the HAL I2C master. Conditionally adds interrupt-driven asynchronous reads for USE_I2C_ASYNC, including completion tracking and stuck-transfer recovery.

src/main/drivers/bus_i2c_stm32f7xx.c

Other (1) +1 / -1
stm32f7.cmakeSelect the register-based I2C driver for F7 builds +1/-1

Select the register-based I2C driver for F7 builds

• Replaces bus_i2c_hal.c with bus_i2c_stm32f7xx.c in the F7 source list. The H7 source selection remains unchanged.

cmake/stm32f7.cmake

@qodo-free-for-open-source-projects

Copy link
Copy Markdown

Code Review by Qodo

🐞 Bugs (1) 📘 Rule violations (0) 📜 Skill insights (0)

Grey Divider


Remediation recommended

1. Busy buses trigger premature recovery 🐞 Bug ☼ Reliability
Description
i2cReady() returns the peripheral without waiting for ISR.BUSY to clear, so blocking transfers
issue START immediately and apply the 10 ms transfer timeout while another transaction may still
own the bus. If the bus remains busy for more than 10 ms, the driver reports an error and runs
i2cInit()'s unstick pulses; the previous HAL path waited up to 25 ms for the bus to become idle
before starting a transfer.
Code

src/main/drivers/bus_i2c_stm32f7xx.c[299]

+    return i2cHardwareMap[device].dev;
Evidence
The new readiness check returns the peripheral without inspecting BUSY, and the blocking write path
immediately programs CR2 with START. Its wait fails after I2C_TIMEOUT (10 ms) and calls recovery,
whose unstick routine pulses the pins. The replaced HAL implementation waits for BUSY to clear
first, using a separate 25 ms idle-wait limit.

src/main/drivers/bus_i2c_stm32f7xx.c[286-300]
src/main/drivers/bus_i2c_stm32f7xx.c[309-336]
src/main/drivers/bus_i2c_stm32f7xx.c[433-459]
lib/main/STM32F7/Drivers/STM32F7xx_HAL_Driver/Src/stm32f7xx_hal_i2c.c[1125-1143]
lib/main/STM32F7/Drivers/STM32F7xx_HAL_Driver/Src/stm32f7xx_hal_i2c.c[340-345]

Agent prompt
The issue below was found during a code review. Follow the provided context and guidance below and implement a solution

## Issue description
Blocking F7 transfers no longer wait for an already-busy bus before issuing START. A transaction that holds the bus for more than the new transfer timeout can trigger bus recovery while it is still active.

## Fix Focus Areas
- src/main/drivers/bus_i2c_stm32f7xx.c[286-300]
- src/main/drivers/bus_i2c_stm32f7xx.c[302-357]

## Recommended Fix
Before either blocking transfer issues START, wait for `ISR.BUSY` to clear with a bounded idle-wait timeout separate from the transfer timeout. On timeout, return through the existing failure and recovery path.

ⓘ Copy this prompt and use it to remediate the issue with your preferred AI generation tools


Grey Divider

Context sources
Review mode: Auto: ⚖️ Balanced: Downgraded extended -> standard: change is below the extended eligibility bar (hunks 2/18, lines 463/200; both must reach the floor). Router rationale: New register-level blocking and asynchronous I2C paths contain many subtle hardware defects.

Grey Divider

Tip of the day
💡 Did you know, you can move or hide resolved findings to focus on what still needs action

More tips ↗ | Customize Qodo ↗ | Qodo docs ↗

Grey Divider

Qodo Logo

@github-actions

Copy link
Copy Markdown

RAM / Flash usage vs. base commit 5927460 — commit a0dfba4

Target Flash Δ RAM Δ
MATEKF405 ±0 B (±0.00%) CCM: ±0 B (±0.00%)
RAM: ±0 B (±0.00%)
MATEKF722 -3608 B (-0.75%) ITCM_RAM: ±0 B (±0.00%)
RAM: -260 B (-0.30%)
TCM: ±0 B (±0.00%)
MATEKF765 -3276 B (-0.43%) DTCM_RAM: -4 B (-0.01%)
SRAM1: -236 B (-0.19%)
MATEKH743 ±0 B (±0.00%) D2_RAM: ±0 B (±0.00%)
DTCM_RAM: ±0 B (±0.00%)
ITCM_RAM: ±0 B (±0.00%)
RAM: ±0 B (±0.00%)

See RAM/flash optimization guide for techniques to reduce usage.

@github-actions

Copy link
Copy Markdown

Test firmware build ready — commit a0dfba4

Download firmware for PR #12157

251 targets built. Find your board's .hex file by name on that page (e.g. MATEKF405SE.hex). Files are individually downloadable — no GitHub login required.

Development build for testing only. Use Full Chip Erase when flashing.

@error414

Copy link
Copy Markdown
Contributor

I would say that there is no reasson use HAL for H7 and use direct register for manipulating for F7. The code should be same for H7 as well.

What I see what can be a litlle bit problematic is that this PR changes code for all I2c devices, and all I2c devices would have to be tested. It's a reasson I why close PR for I2C refactor and create I2C async core + and then each driver in extra PR.

So far here are two PRs for I2C refactor :( #12063 and #12133 which do same, and plenty of PRs whith dependency.

@MrScothh

MrScothh commented Nov 1, 2026

Copy link
Copy Markdown
Contributor Author

Agreed on the H7: its I2C is the same V2 peripheral, so the same file would cover it with its own clock inputs. I kept it to F7 because that is where the flash is short and because the PR is a stopgap, as the body says: the H7 binaries stay as they are and nothing has to be reverted there. If you would rather have one driver for both I will extend it. On #12063 and #12133: they overlap on the asynchronous read and #12133 is the general one. I am fine closing #12063 once #12133 is in, and this driver then takes #12133's API in register form, or F7 goes back to the HAL with it.

error414 added a commit to error414/inav that referenced this pull request Nov 5, 2026
@error414 error414 mentioned this pull request Nov 5, 2026
9 tasks done
@sensei-hacker sensei-hacker added this to the 10.1 milestone Nov 15, 2026
@sensei-hacker
sensei-hacker changed the base branch from maintenance-10.x to next-10.1 November 15, 2026 22:03
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants