Hi,
I'm working with an ESP32 S2 based dev board, more precisely an Ai-Thinker ESP-12K and micro SD breakout board connected through SPI.
I'm using PlatformIO and the .ini file (which I'm attaching at the bottom) I use is a modified one to get the Arduino framework to compile and work with the ESP32-S2 which I think still doesn't have official support from PlatformIO. The toolchain works and I have the board working perfectly fine with different devices.
The problem I have is that when I try to run one of the examples for the SD card, some sort of simplified data logger, the access to the SD card seems to work fine anywhere but in the loop() routine where I got it hanging everytime I try to.
I think it might have something to do with, afaik, the fact that loop() routine runs as a task in the scheduler of the FreeRTOS that the esp32 has running "behind the scenes" when using Arduino as a framework and the availability of resources such as SPI. Actually at some point I saw some xSemaphore error, which for some reason I don't see any more.
I'm attaching the code and the errors, note that I do some 3 iterations of the same functions in the setup() routine which work and write to the SD card and how just whenever I get into the loop() it crashes.
The code:
[Codebox]
#include <Arduino.h>
#include "FS.h"
#include "SD.h"
#include "SPI.h"
#define SD_CS_PIN 34
#define SD_MOSI_PIN 35
#define SD_SCK_PIN 36
#define SD_MISO_PIN 37
float temp;
float hum;
String formattedDate;
String dayStamp;
String timeStamp;
String dataMessage;
void readTempHum(){
temp = 23 + millis()/1000;
hum = millis()/2000;
Serial.print("temp = ");
Serial.println(temp);
Serial.print("hum = ");
Serial.println(hum);
}
// Function to get date and time from NTPClient
void getTimeStamp() {
timeStamp = "15:31:23";
dayStamp = "04/10/2021";
Serial.printf("%s-%s\n", timeStamp, dayStamp);
}
void appendFile(fs::FS &fs, const char * path, const char * message) {
Serial.printf("Appending to file: %s\n", path);
File file = fs.open(path, FILE_APPEND);
if(!file) {
Serial.println("Failed to open file for appending");
return;
}
if(file.print(message)) {
Serial.println("Message appended");
} else {
Serial.println("Append failed");
}
file.close();
}
void writeFile(fs::FS &fs, const char * path, const char * message) {
Serial.printf("Writing file: %s\n", path);
File file = fs.open(path, FILE_WRITE);
if(!file) {
Serial.println("Failed to open file for writing");
return;
}
if(file.print(message)) {
Serial.println("File written");
} else {
Serial.println("Write failed");
}
file.close();
}
// Write the sensor readings on the SD card
void logSDCard() {
dataMessage = String(dayStamp) + "," + String(timeStamp) + "," +
String(temp) + "," + String(hum)+ "\r\n";
Serial.print("Save data: ");
Serial.println(dataMessage);
appendFile(SD, "/data.txt", dataMessage.c_str());
}
void setup() {
Serial.begin(115200);
// Initialize SD card
SPIClass spi = SPIClass(HSPI);
spi.begin(SD_SCK_PIN, SD_MISO_PIN, SD_MOSI_PIN, SD_CS_PIN);
if (!SD.begin(SD_CS_PIN,spi,80000000)) {
Serial.println("Card Mount Failed");
return;
}
uint8_t cardType = SD.cardType();
if(cardType == CARD_NONE) {
Serial.println("No SD card attached");
return;
}
File file = SD.open("/data.txt");
if(!file) {
Serial.println("File doens't exist");
Serial.println("Creating file...");
writeFile(SD, "/data.txt", "Date, Time, temp, hum \r\n");
}
else {
Serial.println("File already exists");
}
file.close();
delay(1000);
for(int i=0; i<3; i++){
Serial.printf("Iteration in setup #%d\n", i+1);
readTempHum();
getTimeStamp();
logSDCard();
delay(1000);
}
}
int i=0;
void loop() {
Serial.printf("Iteration in loop #%d\n", i+1);
readTempHum();
getTimeStamp();
logSDCard();
delay(1000);
i++;
}
[/Codebox]
The console output:
[Codebox]
Rebooting...
ESP-ROM:esp32s2-rc4-20191025
Build:Oct 25 2019
rst:0xc (RTC_SW_CPU_RST),boot:0xb (SPI_FAST_FLASH_BOOT)
Saved PC:0x40026528
SPIWP:0xee
mode:DIO, clock div:2
load:0x3ffe6100,len:0x424
load:0x4004c000,len:0x844
load:0x40050000,len:0x2460
entry 0x4004c180
File already exists
Iteration in setup #1
temp = 28.00
hum = 2.00
15:31:23-04/10/2021
Save data: 04/10/2021,15:31:23,28.00,2.00
Appending to file: /data.txt
Message appended
Iteration in setup #2
temp = 29.00
hum = 3.00
15:31:23-04/10/2021
Save data: 04/10/2021,15:31:23,29.00,3.00
Appending to file: /data.txt
Message appended
Iteration in setup #3
temp = 30.00
hum = 3.00
15:31:23-04/10/2021
Save data: 04/10/2021,15:31:23,30.00,3.00
Appending to file: /data.txt
Message appended
Iteration in loop #1
temp = 32.00
hum = 4.00
15:31:23-04/10/2021
Save data: 04/10/2021,15:31:23,32.00,4.00
Appending to file: /data.txt
Guru Meditation Error: Core 0 panic'ed (LoadProhibited). Exception was unhandled.
Core 0 register dump:
PC : 0x400a71e2 PS : 0x00060d30 A0 : 0x80081d4a A1 : 0x3ffc60c0
A2 : 0x0064732f A3 : 0x3ffcd684 A4 : 0x00000000 A5 : 0x00000002
A6 : 0x00000001 A7 : 0x00000000 A8 : 0x80091915 A9 : 0x3ffc61d0
A10 : 0x3ffcd678 A11 : 0x000127d7 A12 : 0x0000001c A13 : 0x00000000
A14 : 0x00000002 A15 : 0x00008000 SAR : 0x0000001f EXCCAUSE: 0x0000001c
EXCVADDR: 0x00647343 LBEG : 0x0000001c LEND : 0x00000000 LCOUNT : 0x40027079
Backtrace:0x400a71df:0x3ffc60c00x40081d47:0x3ffc60e0 0x40082716:0x3ffc6110 0x4008d555:0x3ffc6140 0x4008d794:0x3ffc6160 0x4008def9:0x3ffc6180 0x4008e002:0x3ffc61a0 0x4008fa42:0x3ffc61d0 0x40091912:0x3ffc6200 0x4008a1fe:0x3ffc6220 0x4001a1af:0x3ffc6240 0x4001a6d9:0x3ffc6260 0x40095ee5:0x3ffc6280 0x40095bf6:0x3ffc62f0 0x40095a72:0x3ffc6310 0x40095aa4:0x3ffc6340 0x40094d21:0x3ffc6360 0x40095701:0x3ffc63a0 0x40081bf0:0x3ffc6420 0x4008153d:0x3ffc6450 0x4008170d:0x3ffc6490 0x40081786:0x3ffc6510 0x40085ed1:0x3ffc6530
ELF file SHA256: 0000000000000000
Rebooting...
[/Codebox]
The .ini file:
[Codebox]
[env:MagTag]
platform = espressif32
platform_packages =
toolchain-xtensa32s2
framework-arduinoespressif32@https://github.com/espressif/arduino-es ... 0.0-alpha1
framework = arduino
board = esp32dev
board_build.mcu = esp32s2
board_build.partitions = huge_app.csv
build_unflags =
-DARDUINO_ESP32_DEV
-DARDUINO_VARIANT="esp32"
build_flags =
-DARDUINO_MAGTAG29_ESP32S2
; -DARDUINO_SERIAL_PORT=1
-DARDUINO_VARIANT="adafruit_magtag29_esp32s2"
; -DBOARD_HAS_PSRAM
-DCORE_DEBUG_LEVEL=5
monitor_speed = 115200
[/Codebox]
Accessing SD Card through SPI hangs up inside loop() with an ESP32-S2
Re: Accessing SD Card through SPI hangs up inside loop() with an ESP32-S2
Would SPIClass spi being declared in setup() cause problems? If you move the spi declaration to the global scope and only initialize spi in setup(), does that work?
Re: Accessing SD Card through SPI hangs up inside loop() with an ESP32-S2
I had the same problem as Nasker and the idea/solution from savage solved it! Thank you very much
Who is online
Users browsing this forum: Google [Bot] and 94 guests