Dtynin
Dtynin
How to fix "Failed to initialize SD card -ENODEV" error on AVR128DA48 using SPI?
I'm working on logging pressure data to an SD card via the SPI interface on the AVR128DA48 microcontroller. However, I'm encountering an issue where the SD card fails to initialize. I have ensured the SD card is properly formatted and compatible with the system, and also verified the SPI wiring and connection to the SD card module, but I'm still facing the Failed to initialize SD card -ENODEV error. how can I overcome this and log the pressure data? this is my instruction snippet:
#include <zephyr.h>
#include <device.h>
#include <drivers/spi.h>
#include <fs/fs.h>

#define SD_CARD_SPI_DEV "SPI_0"
#define PRESSURE_LOG_FILE "/sdcard/pressure_log.txt"

void main(void)
{
const struct device *spi_dev = device_get_binding(SD_CARD_SPI_DEV);
struct sensor_value pressure;

if (spi_dev == NULL) {
printk("Failed to bind to SPI device\n");
return;
}

struct fs_file_t file;
fs_open(&file, PRESSURE_LOG_FILE, FS_O_CREATE | FS_O_WRITE);

const struct device *bmp280 = device_get_binding(DT_LABEL(DT_INST(0, bosch_bmp280)));

while (1) {
sensor_sample_fetch(bmp280);
sensor_channel_get(bmp280, SENSOR_CHAN_PRESS, &pressure);

fs_write(&file, &pressure, sizeof(pressure));
fs_sync(&file);
k_sleep(K_SECONDS(10));
}

fs_close(&file);
}
#include <zephyr.h>
#include <device.h>
#include <drivers/spi.h>
#include <fs/fs.h>

#define SD_CARD_SPI_DEV "SPI_0"
#define PRESSURE_LOG_FILE "/sdcard/pressure_log.txt"

void main(void)
{
const struct device *spi_dev = device_get_binding(SD_CARD_SPI_DEV);
struct sensor_value pressure;

if (spi_dev == NULL) {
printk("Failed to bind to SPI device\n");
return;
}

struct fs_file_t file;
fs_open(&file, PRESSURE_LOG_FILE, FS_O_CREATE | FS_O_WRITE);

const struct device *bmp280 = device_get_binding(DT_LABEL(DT_INST(0, bosch_bmp280)));

while (1) {
sensor_sample_fetch(bmp280);
sensor_channel_get(bmp280, SENSOR_CHAN_PRESS, &pressure);

fs_write(&file, &pressure, sizeof(pressure));
fs_sync(&file);
k_sleep(K_SECONDS(10));
}

fs_close(&file);
}
2 replies