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

STM32L5 Crash while reading SD card or calling .end() #83

Closed
haydenth opened this issue Mar 27, 2025 · 4 comments · Fixed by #84
Closed

STM32L5 Crash while reading SD card or calling .end() #83

haydenth opened this issue Mar 27, 2025 · 4 comments · Fixed by #84
Labels
bug 🐛 Something isn't working
Milestone

Comments

@haydenth
Copy link

haydenth commented Mar 27, 2025

I'm experiencing an issue with the SD card reader and the STM32L5. This is using the STM32 DK board referenced here:
stm32duino/Arduino_Core_STM32#2694

The issue is that reading from the SD card causes the microcontroller to either freeze, or crash and restart if the watchdog is enabled (i have watchdog enabled).

To Reproduce a Crash

#include <Arduino.h>
#include <STM32SD.h>
#include "stm32l5xx_hal.h"
#include "stm32l5xx_hal_flash.h"
#include "stm32l5xx_hal_rcc.h"

#define FIRMWARE_FILENAME "stm32l5_series_test.ino.bin"

File root;
uint32_t loopCount = 0;

void setup() {

  Serial.begin(115200);
  while(!Serial);
  pinMode(LED_GREEN, OUTPUT);
  pinMode(LED_RED, OUTPUT);

  delay(1000);
  Serial.print("Initializing SD card...");

  if (SD.begin(SD_DETECT_PIN)) {
    Serial.println("SD card initialized");
  } else {
    Serial.println("SD card failed to initialize");
  }

}

void loop() {

  digitalWrite(LED_GREEN, HIGH);
  digitalWrite(LED_RED, LOW);
  Serial.println("Hello World");
  delay(1000);
  digitalWrite(LED_GREEN, LOW);
  digitalWrite(LED_RED, HIGH);

  File test = SD.open(FIRMWARE_FILENAME, FILE_READ);
  if (test) {
    Serial.println("Firmware file exists");
    test.close();
  } else {
    Serial.println("No firmware file found");
  }

  loopCount++;
  Serial.print("Loop count: ");
  Serial.println(loopCount);

  Serial.flush();
  Serial.println("Looping in 5 seconds...");
  delay(5000);
}

And here's what the serial log looks like as this runs.

Initializing SD card...SD card initialized

Hello World

No firmware file found

Loop count: 1

Hello World

No firmware file found

Loop count: 2

Hello World

No firmware file found

Loop count: 3

Hello World

No firmware file found

Loop count: 4

Hello World

Device /dev/ttyACM1 disconnected. Waiting for reconnect...
Device /dev/ttyACM1 not found. Retrying in 1 second...
Device /dev/ttyACM1 detected. Opening connection...
Initializing SD card...SD card initialized

Hello World

No firmware file found

Loop count: 1

Hello World

No firmware file found

Loop count: 2

Hello World

No firmware file found

Loop count: 3

So it appears to be crashing every few SD card reads - it's really unpredictable. Sometimes it goes for a while and sometimes it will only do 2-3 loops before there's a crash.

Here's how I build an upload

#!/bin/bash

# delete the build folder
rm -rf ../test_build

# compile the code
arduino-cli compile --fqbn STMicroelectronics:stm32:Disco:pnum=STM32L562E_DK,usb=CDCgen,opt=ogstd,dbg=enable_all,rtlib=nanofp \
  /home/tom/projects/corrosion-sensor-firmware/stm32l5_series_test/ \
  --output-dir /home/tom/projects/corrosion-sensor-firmware/test_build \
  --verbose \
  --export-binaries

# if previous command fails, exit
if [ $? -ne 0 ]; then
  exit 1
fi

# erase the memory under Reset
STM32_Programmer_CLI -c port=SWD mode=UR -e all -ob RDP=0xAA

# now upload with hard reset
STM32_Programmer_CLI -c port=SWD mode=UR \
  -d ../test_build/stm32l5_series_test.ino.bin 0x08000000 \
  -v -hardRst

And to reproduce a crash even simpler:


void setup() {

  Serial.begin(115200);
  while(!Serial);
  pinMode(LED_GREEN, OUTPUT);
  pinMode(LED_RED, OUTPUT);

  delay(1000);
  Serial.print("Initializing SD card...");

  if (SD.begin(SD_DETECT_PIN)) {
    Serial.println("SD card initialized");
  } else {
    Serial.println("SD card failed to initialize");
  }

  SD.end();
}

I've tried a few things, including changing the heap size and tweaking some timing but I can't seem to figure out why it's doing this. If I have it dump crash information, it looks like this. not sure if helpful.

==== HARDFAULT DETECTED ====

HFSR:  0x40000000

CFSR:  0x8200

MMFAR: 0x0

Device /dev/ttyACM1 disconnected. Waiting for reconnect...
Device /dev/ttyACM1 not found. Retrying in 1 second...
Device /dev/ttyACM1 detected. Opening connection...
Initializing SD card...Starting SD card initialization...

SD initialized on attempt 1

==== HARDFAULT DETECTED ====

HFSR:  0x40000000

CFSR:  0x8200

MMFAR: 0x0

BFAR:  0x0

R0:    0x8041

R1:    0xFFFFFFB0

R2:    0x4000D400

R3:    0x0

R12:   0x4000D8E0

LR:    0x8041

I've tried different SD cards, of different sizes and can't seem to find a pattern. It does this with all of them. It is especially reproducible by calling the .end() method.

@fpistm fpistm added the bug 🐛 Something isn't working label Mar 27, 2025
@fpistm fpistm added this to the 1.4.1 milestone Mar 27, 2025
fpistm added a commit to fpistm/STM32SD that referenced this issue Mar 27, 2025
Fixes stm32duino#83

Signed-off-by: Frederic Pillon <frederic.pillon@st.com>
@fpistm
Copy link
Member

fpistm commented Mar 27, 2025

Hi @haydenth
Thanks for the issue.
I've found the issue. Don"t understand how some examples works before...
PR available see #84

@haydenth
Copy link
Author

OK this did fix the issue with .end() causing a crash, but it didn't fix the issue with crashes happening loop(). Here's my sketch:

#include <Arduino.h>
#include <STM32SD.h>
#include "stm32l5xx_hal.h"
#include "stm32l5xx_hal_flash.h"
#include "stm32l5xx_hal_rcc.h"

#define FIRMWARE_FILENAME "stm32l5_series_test.ino.bin"

File root;
uint32_t loopCount = 0;

void setup() {

  Serial.begin(115200);
  while(!Serial);
  pinMode(LED_GREEN, OUTPUT);
  pinMode(LED_RED, OUTPUT);

  delay(1000);
  Serial.print("Initializing SD card...");

  if (SD.begin(SD_DETECT_PIN)) {
    Serial.println("SD card initialized");
  } else {
    Serial.println("SD card failed to initialize");
  }

}

void loop() {

  digitalWrite(LED_GREEN, HIGH);
  digitalWrite(LED_RED, LOW);
  Serial.println("Hello World");
  delay(1000);
  digitalWrite(LED_GREEN, LOW);
  digitalWrite(LED_RED, HIGH);

  File test = SD.open(FIRMWARE_FILENAME, FILE_READ);
  if (test) {
    Serial.println("Firmware file exists");
    test.close();
  } else {
    Serial.println("No firmware file found");
  }

  loopCount++;
  Serial.print("Loop count: ");
  Serial.println(loopCount);

  Serial.flush();
  Serial.println("Looping in 2 seconds...");
  delay(2000);
}

And here's the logs I get. Note that it resets itself (the watchdog does this, I think) but without the watchdog, it will just hang.

Hello World

Firmware file exists

Loop count: 2

Looping in 2 seconds...

Hello World

Firmware file exists

Loop count: 3

Looping in 2 seconds...

Hello World

Firmware file exists

Loop count: 4

Looping in 2 seconds...

Hello World

Device /dev/ttyACM1 disconnected. Waiting for reconnect...
Device /dev/ttyACM1 not found. Retrying in 1 second...
Device /dev/ttyACM1 detected. Opening connection...
Initializing SD card...SD card initialized

Hello World

Firmware file exists

Loop count: 1

Looping in 2 seconds...

I'm running the latest pull request and board defs for the STM32L5 DK. I can share if you want but it's right from here:
stm32duino/Arduino_Core_STM32#2696

@fpistm
Copy link
Member

fpistm commented Mar 27, 2025

Using your sketch, I have no crash.
You said you used the watchdog but can't see any code for this nor the reload. So if really enable and no reload then it will automatically restart each time it expires.

Running ~10min.

last part of the log

19:09:42.404 -> Loop count: 137
19:09:42.404 -> Looping in 2 seconds...
19:09:44.414 -> Hello World
19:09:45.418 -> Firmware file exists
19:09:45.418 -> Loop count: 138
19:09:45.418 -> Looping in 2 seconds...
19:09:47.420 -> Hello World
19:09:48.389 -> Firmware file exists
19:09:48.422 -> Loop count: 139
19:09:48.422 -> Looping in 2 seconds...
19:09:50.417 -> Hello World
19:09:51.419 -> Firmware file exists
19:09:51.419 -> Loop count: 140
19:09:51.419 -> Looping in 2 seconds...
19:09:53.421 -> Hello World
19:09:54.420 -> Firmware file exists
19:09:54.420 -> Loop count: 141
19:09:54.420 -> Looping in 2 seconds...
19:09:56.416 -> Hello World
19:09:57.418 -> Firmware file exists
19:09:57.418 -> Loop count: 142
19:09:57.418 -> Looping in 2 seconds...
19:09:59.411 -> Hello World
19:10:00.407 -> Firmware file exists
19:10:00.407 -> Loop count: 143
19:10:00.407 -> Looping in 2 seconds...
19:10:02.408 -> Hello World
19:10:03.408 -> Firmware file exists
19:10:03.408 -> Loop count: 144
19:10:03.408 -> Looping in 2 seconds...
19:10:05.416 -> Hello World
19:10:06.415 -> Firmware file exists
19:10:06.415 -> Loop count: 145
19:10:06.415 -> Looping in 2 seconds...
19:10:08.421 -> Hello World
19:10:09.421 -> Firmware file exists
19:10:09.421 -> Loop count: 146
19:10:09.421 -> Looping in 2 seconds...
19:10:11.420 -> Hello World
19:10:12.416 -> Firmware file exists
19:10:12.416 -> Loop count: 147
19:10:12.416 -> Looping in 2 seconds...

@haydenth
Copy link
Author

Welp, the issue must be on my end then, I'll push some buttons until it works. Thanks for your help

@fpistm fpistm closed this as completed in 25ee4ac Mar 27, 2025
@github-project-automation github-project-automation bot moved this from To do to Done in STM32duino libraries Mar 27, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug 🐛 Something isn't working
Projects
Development

Successfully merging a pull request may close this issue.

2 participants