- Problem: The SDK cannot detect whether an SD card is inserted or removed. Error returns are not exposed.
- Root Cause: The SD card driver lacks proper initialization feedback or runtime status checks.
In the FreeRTOS SDK, there is a function that is available to detect whether an SD card has been inserted or removed.
You can refer to the function SD_disk_status in sdcard.c. However, do note that the SD card has to be properly initialized before calling this function to check its status.
If you’re using the Arduino SDK, please note that this feature is still under development.
I am using the Arduino SDK, Is there any hardware pin to detect the SD card in slot?
Could you try the following code for the run time checking?
while (fs.status() != 1) {
fs.begin();
}
fs.status() is the function for checking runtime status.
If we use the fs.status() to check, any GPIO can be the indicate pin.
The fs.status is always 0 in both cases if SD card inserted or removed
The fsStatus checks if the sdcard is init or not.
int AmebaFatFS::status(void)
{
return fatfs_sd_is_inited();
}
I am using arduino sdk 4.0.9
ArduinoIDE 2.3.6
That is odd, after fs.begin(); if you check fs.status()
- if SD card inserted, get “1“
- if SD card removed, get “0“
Could you provide the log of trying basic demo by updating following code
while (fs.status() != 1) {
fs.begin();
}
And could you try the latest early release version?
thanks.
Thanks for replying M-ichae-l
Is it a stable release?
For the early release version, it may not stable as official release version.
But we can use it for current debugging and will QC and release it ASAP.
The issue persists in latest rellease
Hi @M-ichae-l
I have checked the sd card status after fs.begin(); and its working in this scenario. but we would like to check it before fs.begin();
is there a way to check it before?
also we have updated the sdk to the pre release version 4.1.0.
you can use the “status()” before the “begin“
SD card must “begin“ in order to be used, and “status()” is to check if proceed to “begin“.
But if there is no “begin“, no matter does/does not insert the SD card, it is always “0“.
In this case, please allow me more time to investigate both SW and HW solution.
You may want to refer to HDK, if want to get it from HW point of view.
The EVB AMB82-mini has the SD card socket circuit refer to the following pic.
There is a pin “SD_CD“ it is low when there is SD card connected and high when is not. It can be used for the indicator before the sdio init.
But SD_CD pin is critical, so that there is no HW pin out AMB82-mini EVB or and software Arduino API to direct control it.
Therefore, I will suggest to use lower level API to achieve the function we wanted. Please refer to my following test code.
#include "gpio_api.h"
#include "gpio_ex_api.h"
#include "AmebaFatFS.h"
AmebaFatFS fs;
gpio_t gpio_cd;
int gpio_test = 1;
void setup()
{
}
void test()
{
gpio_init(&gpio_cd, PS_4);
gpio_dir(&gpio_cd, PIN_INPUT);
gpio_mode(&gpio_cd, PullNone);
gpio_test = gpio_read(&gpio_cd);
if (gpio_test == 0) {
gpio_deinit(&gpio_cd);
printf("-------Have sd card!!!! \r\n");
fs.begin();
uint64_t used_bytes = fs.get_used_space();
uint64_t free_bytes = fs.get_free_space();
printReadableSize(used_bytes, "Used space on SD card");
printReadableSize(free_bytes, "Free space on SD card");
fs.end();
} else {
gpio_deinit(&gpio_cd);
printf("-------No sd card!!!! \r\n");
}
}
void loop()
{
test();
delay(1000);
}
void printReadableSize(uint64_t bytes, const char* label)
{
double value = (double)bytes;
const char* unit = "B";
if (value >= 1024.0) {
value /= 1024.0;
unit = "KB";
}
if (value >= 1024.0) {
value /= 1024.0;
unit = "MB";
}
if (value >= 1024.0) {
value /= 1024.0;
unit = "GB";
}
printf("%s: %llu bytes (%.2f %s)\r\n", label, bytes, value, unit);
}
The test code provided is only a temporary solution, so please make sure to test it thoroughly before using it - especially when switching between GPIO and SDIO multiple times within a short period.
If you encounter any further issues, please let me know and we can discuss them here.
