Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[hal] SPI: fixes threading safty issue #2865

Merged
merged 2 commits into from
Feb 19, 2025
Merged

[hal] SPI: fixes threading safty issue #2865

merged 2 commits into from
Feb 19, 2025

Conversation

XuGuohui
Copy link
Member

@XuGuohui XuGuohui commented Feb 19, 2025

Problem

hal_spi_acquire() may fail to acquire mutex and when the caller doesn't check the result, it may corrupt stack.

Solution

Use os_mutex_recursive_lock() in hal_spi_acquire() when timeout is not specified or is non-zero.

Steps to Test

  1. Install MSoM on Muon
  2. Connect to the Particle cloud over Etehrnet only while keeping accessing the Lora module (or the Muon's on-board IO expander).

Example

#include "Particle.h"
#include "mcp23s17.h"

SYSTEM_MODE(SEMI_AUTOMATIC);

Serial1LogHandler logHandler(115200, LOG_LEVEL_ALL);

STARTUP (
    System.setPowerConfiguration(SystemPowerConfiguration()
        .powerSourceMinVoltage(3880)
        .powerSourceMaxCurrent(3000)
        .batteryChargeVoltage(4200)
        .batteryChargeCurrent(900)
        .auxiliaryPowerControlPin(D7)
        .interruptPin(A7)
        .feature(SystemPowerFeature::PMIC_DETECTION));

    System.enableFeature(FEATURE_ETHERNET_DETECTION);

    if_wiznet_pin_remap remap = {};
    remap.base.type = IF_WIZNET_DRIVER_SPECIFIC_PIN_REMAP;
    remap.cs_pin = A3;
    remap.reset_pin = PIN_INVALID;
    remap.int_pin = A4;
    if_request(nullptr, IF_REQ_DRIVER_SPECIFIC, &remap, sizeof(remap), nullptr);
);

void setup() {
    Ethernet.on();
    Ethernet.connect();
    waitUntil(Ethernet.ready);
    Log.info("Local IP: %s", Ethernet.localIP().toString().c_str());
    Particle.connect();
    waitUntil(Particle.connected);
    Log.info("Cloud connected");
}

void loop() {
    delay(1000);
    Log.info("Local IP: %s", Ethernet.localIP().toString().c_str());
    uint8_t temp;
    Mcp23s17::getInstance().readRegister(0x00, &temp);
}

References

N/A


Completeness

  • User is totes amazing for contributing!
  • Contributor has signed CLA (Info here)
  • Problem and Solution clearly stated
  • Run unit/integration/application tests on device
  • Added documentation
  • Added to CHANGELOG.md after merging (add links to docs and issues)

@XuGuohui XuGuohui added the bug label Feb 19, 2025
@XuGuohui XuGuohui added this to the 6.3.1 milestone Feb 19, 2025
@XuGuohui XuGuohui requested a review from avtolstoy February 19, 2025 08:11
@@ -672,12 +672,8 @@ int32_t hal_spi_acquire(hal_spi_interface_t spi, const hal_spi_acquire_config_t*
if (!hal_interrupt_is_isr()) {
os_mutex_recursive_t mutex = spiMap[spi].mutex;
if (mutex) {
// FIXME: os_mutex_recursive_lock doesn't take any arguments, using trylock for now
if (!conf || conf->timeout != 0) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This still needs to be supported. If conf && conf->timeout == 0 we need to trylock due to wiring SPIClass::trylock().

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same for Gen4?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yup.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

But again, only if config is provided and timeout is 0.

@XuGuohui XuGuohui requested a review from avtolstoy February 19, 2025 08:36
@XuGuohui XuGuohui merged commit fd2baf7 into develop Feb 19, 2025
14 checks passed
@XuGuohui XuGuohui deleted the fix/spi_concurrent branch February 19, 2025 09:46
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.

2 participants