Skip to content

fix(sx1262): make HAL re-creatable and tolerate shared SPI3 bus#67

Merged
anarchyysm merged 2 commits into
HighCodeh:devfrom
yajatnarayan:fix/sx1262-reinit-spi-bus-sharing-dev
Jun 4, 2026
Merged

fix(sx1262): make HAL re-creatable and tolerate shared SPI3 bus#67
anarchyysm merged 2 commits into
HighCodeh:devfrom
yajatnarayan:fix/sx1262-reinit-spi-bus-sharing-dev

Conversation

@yajatnarayan

Copy link
Copy Markdown
Contributor

Adds sx1262_hal_destroy() and sx1262_deinit() so the driver can be torn down and rebuilt within a single boot. Previously, a second call to sx1262_hal_create() early-returned with the stale SPI device handle, mutex, and GPIO state from the first run, causing subsequent status reads to return corrupted values (e.g. 0x15 / chip_mode 1, an impossible value) even though the chip itself was healthy.

Also fixes a pre-existing bus-ownership bug: kernel_init() initializes SPI3 first via spi_init() because ST7789 shares the bus, so the sx1262_hal_create() call to spi_bus_initialize() was returning ESP_ERR_INVALID_STATE and treating it as fatal. Both error-cleanup paths also called spi_bus_free(SPI3_HOST), which would have torn down the bus ST7789 was actively using.

Changes:

  • sx1262_hal_create(): treat ESP_ERR_INVALID_STATE from spi_bus_initialize as success; drop spi_bus_free() from error paths.
  • sx1262_hal_destroy(): new function that removes the SX1262 SPI device, deletes the mutex, and clears is_initialized. SPI3 bus left intact.
  • sx1262_deinit(): new function that stops the IRQ task (if running), calls sx1262_hal_destroy(), and clears s_is_initialized. Safe no-op when nothing is initialized.
  • meshtastic_app_start(): calls sx1262_deinit() before sx1262_hal_create() so re-entry rebuilds the SPI device from scratch.

Description

Related Issue: Fixes # (issue)


Type of Change

  • Bug fix (non-breaking change which fixes an issue)
  • New feature (non-breaking change which adds functionality)
  • Breaking change (fix or feature that would cause existing functionality to not work as expected)
  • Documentation update

Checklist

  • I have read the CONTRIBUTING.md document.
  • My code follows the style guidelines of this project.
  • I have performed a self-review of my own code.
  • I have commented my code, particularly in hard-to-understand areas.
  • I have made corresponding changes to the documentation.
  • My changes generate no new warnings during build.
  • I have tested this change on physical hardware. (Please specify which target below)

Hardware Testing Details

  • NA

Adds sx1262_hal_destroy() and sx1262_deinit() so the driver can be
torn down and rebuilt within a single boot. Previously, a second call
to sx1262_hal_create() early-returned with the stale SPI device handle,
mutex, and GPIO state from the first run, causing subsequent status
reads to return corrupted values (e.g. 0x15 / chip_mode 1, an impossible
value) even though the chip itself was healthy.

Also fixes a pre-existing bus-ownership bug: kernel_init() initializes
SPI3 first via spi_init() because ST7789 shares the bus, so the
sx1262_hal_create() call to spi_bus_initialize() was returning
ESP_ERR_INVALID_STATE and treating it as fatal. Both error-cleanup paths
also called spi_bus_free(SPI3_HOST), which would have torn down the bus
ST7789 was actively using.

Changes:
- sx1262_hal_create(): treat ESP_ERR_INVALID_STATE from spi_bus_initialize
  as success; drop spi_bus_free() from error paths.
- sx1262_hal_destroy(): new function that removes the SX1262 SPI device,
  deletes the mutex, and clears is_initialized. SPI3 bus left intact.
- sx1262_deinit(): new function that stops the IRQ task (if running),
  calls sx1262_hal_destroy(), and clears s_is_initialized. Safe no-op
  when nothing is initialized.
- meshtastic_app_start(): calls sx1262_deinit() before sx1262_hal_create()
  so re-entry rebuilds the SPI device from scratch.

Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
@yajatnarayan

Copy link
Copy Markdown
Contributor Author

Formatting error on code from previous commits.

Pre-existing clang-format violations on dev that fail the CI format
check (tools/format.sh --check runs over the whole tree). Reformatted
with the CI-pinned clang-format 22.1.3; cosmetic only, no logic changes.

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
@anarchyysm anarchyysm merged commit 2af5b15 into HighCodeh:dev Jun 4, 2026
3 checks passed
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.

2 participants