espressif / arduino-esp32

Arduino core for the ESP32
GNU Lesser General Public License v2.1
13.67k stars 7.42k forks source link

libraries/SD: there is conflict when sharing the same SPI bus in multi tasks (Solved) #8534

Closed LeoYan closed 1 year ago

LeoYan commented 1 year ago

Board

ESP32 Dev Module

Device Description

on the SPI bus connected the follows:

  1. SD card module
  2. OLED 128x32

Hardware Configuration

use the standard SPI pins connect to OLED and SD

Version

v2.0.9

IDE Name

Arduino IDE

Operating System

macOS big sur

Flash frequency

40MHz

PSRAM enabled

no

Upload speed

115200

Description

Issue:

Solution:

libraries/SD: in ff_sd_status() of diskio.c, add ‘AcquireSPI card_locked()’ before to prevent …there is conflict when sharing the same SPI bus in multi tasks


DSTATUS ff_sd_status(uint8_t pdrv)
{

>     ardu_sdcard_t * card = s_cards[pdrv];
>     AcquireSPI card_locked(card);

    if(sdTransaction(pdrv, SEND_STATUS, 0, NULL))
    {
        log_e("Check status failed");
        return STA_NOINIT;
    }
    return s_cards[pdrv]->status;
}

Sketch

task1(){
  while(1){
    OLED.show();
    delay(20);
  }
}

taks2(){
  ...
  f.open("");
  f.read();
  ...
}

Debug Message

- the Backtrace:

0x4010cbb4: sdTransaction(unsigned char, char, unsigned int, unsigned int*) at */build/../components/arduino/libraries/SD/src/sd_diskio.cpp:245
0x4010cbe6: ff_sd_status(unsigned char) at */build/../components/arduino/libraries/SD/src/sd_diskio.cpp:619
0x40112ff6: ff_disk_status at ~/SDK/esp-idf/components/fatfs/diskio/diskio.c:66
0x4011348e: find_volume at ~/SDK/esp-idf/components/fatfs/src/ff.c:3244
0x40114249: f_open at ~/SDK/esp-idf/components/fatfs/src/ff.c:3567
...
0x40203ead: fs::FS::exists(char const*) at */build/../components/arduino/libraries/FS/src/FS.cpp:242


### Other Steps to Reproduce

_No response_

### I have checked existing issues, online documentation and the Troubleshooting Guide

- [X] I confirm I have checked existing issues, online documentation and Troubleshooting guide.
P-R-O-C-H-Y commented 1 year ago

@LeoYan Thank you from finding this out and providing a fix. I have opened a PR linked to this issue. This issue will be closed after merging the fix automatically.

LeoYan commented 1 year ago

thanks