You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

STM32L073RZT6+Waveshare SD卡SPI连接与FATFS FR_NOT_READY问题求助

Hey there! Let's break down your SD card questions first, then dive into fixing that FR_NOT_READY error you're hitting with your SPI setup.


Hardware Questions

1. Is my SPI physical connection correct?

Your wiring is mostly spot-on:

  • 3.3V and GND connections are correct (SD cards strictly require 3.3V power—never use 5V!)
  • SPI1's SCK (PB3), MISO (PB4), and MOSI (PB5) map correctly to the module's SCLK, DO, and DI pins, which matches the STM32L073's SPI1 peripheral pinout.
  • Using PA9 as a GPIO-controlled CS pin makes sense. Since SD card CS is active-low, starting with it pulled high (as you do in your code) is the right default state—only pull it low when you're actively communicating with the card.

Just double-check that your module's Pin6 is indeed the CS/SS pin (some modules label it differently), and note that if your module has a CD pin, you don't need to connect it unless you want card-detection functionality.

2. What are CMD and CD?

  • CMD: Short for Command, this pin is only used in SDIO mode to send commands to the SD card and receive responses. In SPI mode, commands and responses are transmitted over MOSI/MISO, so you don't need to connect this pin at all.
  • CD: Short for Card Detect, this is a GPIO input pin that lets you detect if an SD card is inserted into the module. The logic varies by module (some pull high when a card is inserted, others pull low), but you can leave it unconnected if you don't need this feature.

3. What do the D0-D3 pins do?

These are data pins for SDIO mode:

  • D0: In SDIO mode, it's a bidirectional data pin. In SPI mode, this is the SD card's DO (MISO) pin—your only data input channel for SPI communications.
  • D1-D3: These are only used in SDIO's 4-bit wide mode to enable parallel data transfer (faster read/write speeds). You don't need to connect these pins at all when using SPI mode.

4. SDIO Mode: Configuration & Physical Connection

Physical Wiring (for STM32L073RZT6)

Match your module's pins to these STM32 pins:

  • Module CLK ↔ PC12 (SDIO_CLK)
  • Module CMD ↔ PD2 (SDIO_CMD)
  • Module D0 ↔ PC8 (SDIO_D0)
  • Module D1 ↔ PC9 (SDIO_D1)
  • Module D2 ↔ PC10 (SDIO_D2)
  • Module D3 ↔ PC11 (SDIO_D3)
  • 3.3V ↔ Module Pin1, GND ↔ Module Pin2
Setup Steps in STM32CubeMX
  1. Enable the SDIO peripheral, and select 4-bit bus width (or 1-bit if you don't need maximum speed).
  2. Configure the SDIO clock: Keep it below 400kHz for initialization, then you can bump it up to a maximum of 25MHz (the STM32L0's SDIO limit) for normal operation.
  3. Enable the FATFS middleware, and select SDIO as the storage medium.
  4. Generate code—HAL will handle the SDIO initialization and FATFS low-level drivers automatically.

Fixing the FR_NOT_READY Error in SPI Mode

This error almost always means the SD card isn't being initialized correctly, or there's a SPI communication mismatch. Try these fixes:

1. Correct SPI Timing Configuration

SD cards in SPI mode require SPI Mode 0 (CPOL=0, CPHA=0): clock is low when idle, data is sampled on the clock's rising edge. But your current setup uses:

hspi1.Init.CLKPolarity = SPI_POLARITY_HIGH;
hspi1.Init.CLKPhase = SPI_PHASE_1EDGE;

This is SPI Mode 2, which won't work with SD cards. Update your SPI init to:

hspi1.Init.CLKPolarity = SPI_POLARITY_LOW;
hspi1.Init.CLKPhase = SPI_PHASE_1EDGE;

2. Fix CS Pin Control in FATFS Drivers

CubeMX's auto-generated FATFS code might not handle the CS pin correctly. Open diskio.c and ensure the disk_initialize and disk_ioctl functions properly toggle the CS pin:

  • Pull CS low before sending any SD card commands
  • Pull CS high after completing the command sequence

For example, add this to disk_initialize:

HAL_GPIO_WritePin(GPIOA, GPIO_PIN_9, GPIO_PIN_RESET); // Pull CS low for init
// SD card initialization command sequence happens here
HAL_GPIO_WritePin(GPIOA, GPIO_PIN_9, GPIO_PIN_SET); // Pull CS high when done

3. Improve Error Handling in Your Code

Your current code only checks for FR_NOT_READY on f_open, but you need to validate the f_mount result first—if mounting fails, all subsequent operations will fail too. Update your code to:

/* Mount SD Card */
fres = f_mount(&fs, "", 0);
if(fres != FR_OK) {
    // Handle mount failure based on error code:
    // FR_NO_FILESYSTEM: Card is unformatted—you'll need to format it first
    // FR_NOT_READY: Card failed initialization
    // FR_DISK_ERR: Hardware communication error
    while(1) {
        HAL_GPIO_TogglePin(GPIOA, GPIO_PIN_5);
        HAL_Delay(500);
    }
}

/* Open file to write */
fres = f_open(&fil, "first.txt", FA_OPEN_ALWAYS | FA_READ | FA_WRITE);
if(fres != FR_OK) {
    while(1) {
        HAL_GPIO_TogglePin(GPIOA, GPIO_PIN_5);
        HAL_Delay(200);
    }
}

4. Check SD Card Compatibility

Some high-capacity SDHC/SDXC cards require the ACMD41 initialization command. Make sure your FATFS driver supports these card types. If you're unsure, test with an older, smaller SD card (<2GB, standard SD) to rule out compatibility issues.


内容的提问来源于stack exchange,提问作者Till

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.12 04:46:07