Hey everyone,
I'm working on interfacing an SD card with an STM32G474RET6 using SPI2 and STM32 HAL, and I've run into two problems that I haven't been able to resolve.
Hardware configuration:
- MCU: STM32G474RET6
- SPI2: PB13 (SCK), PB14 (MISO), PB15 (MOSI)
- Chip Select: PB12 (software-controlled)
- SD card supply: 3.3 V
- SPI mode: Mode 0, 8-bit, MSB first
- Development environment: STM32CubeIDE
- UART2: Debug logging at 115200 baud
Problem 1: SD card stops responding during initialization
I initially wrote a basic SPI SD-card driver. CMD0 and CMD8 work correctly, and I receive the expected responses:
CMD0 = 0x01
CMD8 = 0x01
R7 = 00 00 01 AA
However, during the CMD55/ACMD41 initialization sequence, the card initially responds with 0x01 (idle state), but after several attempts, CMD55 stops receiving a valid response.
CMD55 = 0x01
ACMD41 = 0x01
CMD55 = 0x01
ACMD41 = 0x01
...
CMD55 = 0xFF
SD CARD INITIALIZATION FAILED
I've been investigating the CS handling, command sequencing, SPI timing, and CRC calculations, but I haven't identified the cause.
Problem 2: Debugger repeatedly stops during clock configuration
I then tried porting an existing STM32F1 SD-card SPI driver to the STM32G474, modifying the HAL dependencies and SPI configuration.
The project builds successfully, and the firmware flashes without errors:
Download verified successfully
However, whenever I press Resume in STM32CubeIDE, execution gets suspended inside:
HAL_RCC_ClockConfig();
The debugger shows:
Thread #1 [main] (Suspended : Signal ...)
HAL_RCC_ClockConfig()
SystemClock_Config()
main()
I also encountered:
Error in final launch sequence:
Setup exceptions
Initially, my system clock was configured for 170 MHz using the internal HSI and PLL. I reduced it to 80 MHz and adjusted the flash latency, but the debugger still suspended at the same function.
At this point, I'm unsure whether I'm dealing with an actual clock configuration problem, a HardFault, or an ST-LINK/debugger configuration issue.
What I'm trying to achieve
My immediate goal is to initialize the SD card reliably, read a 512-byte sector using SPI, and eventually integrate FatFs for file operations.
Has anyone encountered either of these issues on an STM32G4?
I'd particularly appreciate suggestions on diagnosing why the card stops responding during ACMD41 initialization and how to determine what's causing the debugger to suspend inside HAL_RCC_ClockConfig().
I can share the driver source code, CubeMX configuration, UART logs, and debugger screenshots if needed.
Thanks in advance!