How can I resolve the SD card initialization error?

Hey guys, I'm still having issues trying to log temperature data from the MCP9808 sensor to an SD card using an ATmega2560 and Zephyr OS, how can I resolve the SD card initialization error? Despite setting up the SD card interface and implementing file write operations, I still encountered the error "Failed to mount SD card." Here's the code:
#include <zephyr.h>
#include <fs/fs.h>
#include <ff.h>
#include <logging/log.h>

LOG_MODULE_REGISTER(main);

void main(void)
{
static FATFS fat_fs;
struct fs_mount_t mp = {
.type = FS_FATFS,
.fs_data = &fat_fs,
};

if (fs_mount(&mp) != 0) {
LOG_ERR("Failed to mount SD card");
return;
}

struct fs_file_t file;
fs_file_t_init(&file);

if (fs_open(&file, "/SD:/temp_log.txt", FS_O_WRITE | FS_O_CREATE) != 0) {
LOG_ERR("Failed to open file");
return;
}

char data[] = "Temperature data";
if (fs_write(&file, data, strlen(data)) != strlen(data)) {
LOG_ERR("Failed to write data");
fs_close(&file);
return;
}

fs_close(&file);
LOG_INF("Data logged to SD card");
}
#include <zephyr.h>
#include <fs/fs.h>
#include <ff.h>
#include <logging/log.h>

LOG_MODULE_REGISTER(main);

void main(void)
{
static FATFS fat_fs;
struct fs_mount_t mp = {
.type = FS_FATFS,
.fs_data = &fat_fs,
};

if (fs_mount(&mp) != 0) {
LOG_ERR("Failed to mount SD card");
return;
}

struct fs_file_t file;
fs_file_t_init(&file);

if (fs_open(&file, "/SD:/temp_log.txt", FS_O_WRITE | FS_O_CREATE) != 0) {
LOG_ERR("Failed to open file");
return;
}

char data[] = "Temperature data";
if (fs_write(&file, data, strlen(data)) != strlen(data)) {
LOG_ERR("Failed to write data");
fs_close(&file);
return;
}

fs_close(&file);
LOG_INF("Data logged to SD card");
}
If the implementation is successful, the output should log the temperature data to the SD card and display a confirmation message.
Solution:
Yeah, @RED HAT I had to verify my file system configuration in Zephyr after which I ensured to recheck the file system permissions...and a little run arounds😂 . thanks anyways
Jump to solution
2 Replies
RED HAT
RED HAT•6mo ago
@Dtynin Hey! It looks like your SD card might not be properly initialized. Make sure the card is formatted correctly and that the file system is supported by Zephyr. Also, check the hardware connections to ensure they are secure.
Solution
Dtynin
Dtynin•6mo ago
Yeah, @RED HAT I had to verify my file system configuration in Zephyr after which I ensured to recheck the file system permissions...and a little run arounds😂 . thanks anyways
Want results from more Discord servers?
Add your server