Skip to content

phy: make the PHY I2C critical section nesting safe - #74

Merged
deadprogram merged 1 commit into
mainfrom
phy-i2c-critical-nesting
Sep 30, 2026
Merged

deadprogram merged 1 commit into
mainfrom
phy-i2c-critical-nesting

Conversation

@deadprogram

@deadprogram deadprogram commented Sep 29, 2026 •

Copy link
Copy Markdown
Member

This PR fixes PS being left at INTLEVEL 3 after BLE init on the ESP32-S3 and the classic ESP32.

The blob nests phy_i2c_enter_critical. The old PS was kept in one static value, so the inner call saved INTLEVEL 3 over the outer value. When both calls exited, PS stayed at INTLEVEL 3, and that blocked all level 1 interrupts, for example GPIO and I2S. WiFi hid the bug, because espradio_wifi_unmask lowers INTLEVEL on each pass. A program that uses BLE without WiFi never got those interrupts again.

Now only the outer enter and exit pair saves and restores PS.

Testing on a xiao-esp32s3

  1. During BLE init there were 816 calls, nested 2 deep. Before the fix PS was 0x60023 after init, and after the fix it is 0x60020.
  2. The I2S driver from machine/esp32xx: add I2S support tinygo#5787 plays audio while BLE advertises, and the device is visible from a host scan.
  3. WiFi scans still work.

This has been tested on actual hardware, which is correction from the original description.

The blob nests phy_i2c_enter_critical, but the old PS was kept in one
static value. The inner call saved INTLEVEL 3 over the outer value, so PS
stayed at INTLEVEL 3 after BLE init. This blocked all level 1 interrupts,
for example GPIO and I2S, on programs that use BLE without WiFi. Now only
the outer pair saves and restores PS.

Tested on a xiao-esp32s3. PS is at INTLEVEL 0 after BLE init, the I2S
interrupt runs while BLE advertises, and WiFi scans still work. The same
change for the classic ESP32 builds but is not tested on hardware.

Signed-off-by: deadprogram <ron@hybridgroup.com>
@deadprogram

Copy link
Copy Markdown
Member Author

Any feedback before merge here?

@deadprogram

Copy link
Copy Markdown
Member Author

Merging since hardware testing shows it is quite needed.

@deadprogram
deadprogram merged commit 9a37b24 into main Sep 30, 2026
1 check passed
@deadprogram
deadprogram deleted the phy-i2c-critical-nesting branch September 30, 2026 17:57
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant