Dtynin
Dtynin
DIIDevHeads IoT Integration Server
Created by Dtynin on 7/23/2024 in #middleware-and-os
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.
4 replies