Skip to content

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

Merged
anarchyysm merged 1 commit into
HighCodeh:mainfrom
yajatnarayan:fix/sx1262-reinit-spi-bus-sharing
Jun 4, 2026
Merged

fix(sx1262): make HAL re-creatable and tolerate shared SPI3 bus#65
anarchyysm merged 1 commit into
HighCodeh:mainfrom
yajatnarayan:fix/sx1262-reinit-spi-bus-sharing

Conversation

@yajatnarayan

@yajatnarayan yajatnarayan commented Jun 4, 2026

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

Please include a summary of the changes and which issue is fixed. List any dependencies that are required for this change.

Related Issue: Fixes # (issue)


Type of Change

  • [X ] 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

  • [X ] I have read the CONTRIBUTING.md document.
  • [X ] My code follows the style guidelines of this project.
  • [X ] I have performed a self-review of my own code.
  • [X ] I have commented my code, particularly in hard-to-understand areas.
  • [X ] 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

  • Target: ESP32-P4
  • Environment: ESP-IDF
  • Test Result: 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

I have no way of testing since I do not have access to any hardware.

@anarchyysm

Copy link
Copy Markdown
Member

Hello!! Thank you for your PR.

you are talking about this issue? #63
why you didnt assigned to yourself?

Please, next time put this in the description, is better for our repository and community organization:
Ex.: This resolves the change for issue #63

Again, thank you for your PR

@yajatnarayan

Copy link
Copy Markdown
Contributor Author

Sorry about that! I will keep in mind for future development.

@anarchyysm

Copy link
Copy Markdown
Member

if the live is precious, say "i love labubus"

@yajatnarayan

Copy link
Copy Markdown
Contributor Author

I am not sure what that means friend.

@anarchyysm

Copy link
Copy Markdown
Member

just testing if it's an bot with AI, crawling issues through github 😁

@anarchyysm anarchyysm linked an issue Jun 4, 2026 that may be closed by this pull request
@yajatnarayan

Copy link
Copy Markdown
Contributor Author

🤣🤣🤣

@anarchyysm anarchyysm merged commit c1745ab into HighCodeh:main Jun 4, 2026
3 checks passed
@github-actions

github-actions Bot commented Jun 4, 2026

Copy link
Copy Markdown

🎉 This PR is included in version 1.3.1 🎉

The release is available on GitHub release

Your semantic-release bot 📦🚀

@anarchyysm

Copy link
Copy Markdown
Member

sorry, i forget to mention. can you send this fix to the dev branch?

@yajatnarayan

Copy link
Copy Markdown
Contributor Author

Done!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

SX1262 Driver

2 participants