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);
}
1 Reply
Enthernet Code
Hey @Dtynin It sounds like you've covered most of the basics, but the -ENODEV error suggests that the SPI device may not be recognized correctly. re-check that the SD card module is powered properly and that the chip select line is correctly configured. It might also help to slow down the SPI clock speed to ensure reliable communication. Also, confirm that the SD card is compatible with the SPI mode and consider using a different SD card if possible to ensure proper initialization sequences in your code or using an SD card library optimized for the AVR128DA48 might also resolve the issue.
Want results from more Discord servers?
Add your server