Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
97 changes: 97 additions & 0 deletions libraries/Storage/examples/SDListFiles/SDListFiles.ino
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
/*
SD List Files

This sketch demonstrates how to list files on an SD card
on Arduino boards with Zephyr RTOS.

The SD card is auto-mounted at boot as a FAT filesystem
at "/SD:". If no card is inserted, the mount is skipped.

Compatible with boards that have an SD card slot
(e.g. Portenta H7 with a Portenta Breakout Board).

This example code is in the public domain.
*/

#include <Arduino.h>
#include <zephyr/fs/fs.h>

#define SD_MOUNT_PT "/SD:"

void listDir(const char *path) {
struct fs_dir_t dir = {0};
struct fs_dirent entry;

fs_dir_t_init(&dir);
int res = fs_opendir(&dir, path);

if (res) {
Serial.print("Error opening directory ");
Serial.print(path);
Serial.print(" [error code: ");
Serial.print(res);
Serial.println("]");
return;
}

Serial.print("\nContents of ");
Serial.print(path);
Serial.println(":");

bool empty = true;
while (true) {
res = fs_readdir(&dir, &entry);
if (res || entry.name[0] == '\0') {
break;
}

empty = false;
if (entry.type == FS_DIR_ENTRY_FILE) {
Serial.print(" [FILE] ");
Serial.print(entry.name);
Serial.print(" (");
Serial.print((size_t) entry.size);
Serial.println(" bytes)");
} else if (entry.type == FS_DIR_ENTRY_DIR) {
Serial.print(" [DIR ] ");
Serial.println(entry.name);
}
}

if (empty) {
Serial.println(" <empty>");
}

fs_closedir(&dir);
}

void setup() {
Serial.begin(115200);
while (!Serial);

Serial.println("\n=== SD Card File Listing ===\n");

// Check if the SD card is mounted
struct fs_statvfs stat;
int res = fs_statvfs(SD_MOUNT_PT, &stat);
if (res) {
Serial.println("SD card not mounted. Is one inserted?");
return;
}

unsigned long total_kb = (unsigned long)stat.f_frsize * stat.f_blocks / 1024;
unsigned long free_kb = (unsigned long)stat.f_frsize * stat.f_bfree / 1024;
Serial.print("SD card: ");
Serial.print(free_kb / 1024);
Serial.print(" MB free / ");
Serial.print(total_kb / 1024);
Serial.println(" MB total");

listDir(SD_MOUNT_PT);

Serial.println("\nDone!");
}

void loop() {
delay(1000);
}
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ CONFIG_I2C_TARGET=y
CONFIG_SDHC=y
CONFIG_SDHC_INIT_PRIORITY=75
CONFIG_SDIO_STACK=y
CONFIG_SDMMC_STM32_HWFC=y
CONFIG_CYW4343W=y
CONFIG_WIFI=y

Expand Down Expand Up @@ -94,17 +95,17 @@ CONFIG_FS_LITTLEFS_PROG_SIZE=4096
CONFIG_FS_LITTLEFS_CACHE_SIZE=4096

CONFIG_FAT_FILESYSTEM_ELM=y
CONFIG_FS_FATFS_EXFAT=n
CONFIG_FS_FATFS_EXFAT=y
CONFIG_FS_FATFS_MKFS=y
CONFIG_FS_FATFS_LFN=y
CONFIG_FS_FATFS_LFN_MODE_HEAP=y
CONFIG_FS_FATFS_CODEPAGE=437
CONFIG_FS_FATFS_MIN_SS=4096
CONFIG_FS_FATFS_MIN_SS=512
CONFIG_FS_FATFS_MAX_SS=4096
CONFIG_FS_FATFS_MAX_LFN=255
CONFIG_FS_FATFS_FSTAB_AUTOMOUNT=y
CONFIG_FS_FATFS_CUSTOM_MOUNT_POINT_COUNT=2
CONFIG_FS_FATFS_CUSTOM_MOUNT_POINTS="wlan,ota"
CONFIG_FS_FATFS_CUSTOM_MOUNT_POINT_COUNT=3
CONFIG_FS_FATFS_CUSTOM_MOUNT_POINTS="wlan,ota,SD"

CONFIG_ENTROPY_GENERATOR=y
CONFIG_TEST_RANDOM_GENERATOR=y
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -329,6 +329,39 @@
qspi_flash: &mx25l12833f {};
#include "../common/arduino_flash_fs.dtsi"

/* PLL2_R at 48MHz for SDMMC2: HSE(25MHz) / 5 * 48 / 5 = 48MHz */
&pll2 {
div-m = <5>;
mul-n = <48>;
div-p = <2>;
div-q = <2>;
div-r = <5>;
clocks = <&clk_hse>;
status = "okay";
};

&sdmmc2 {
status = "okay";
pinctrl-0 = <&sdmmc2_d0_pb14 &sdmmc2_d1_pb15
&sdmmc2_d2_pb3 &sdmmc2_d3_pb4
&sdmmc2_ck_pd6 &sdmmc2_cmd_pd7>;
pinctrl-names = "default";
clocks = <&rcc STM32_CLOCK(AHB2, 9U)>,
<&rcc STM32_SRC_PLL2_R SDMMC_SEL(1)>;
disk-name = "SD";
bus-width = <4>;
clk-div = <1>;
};

&fstab {
sd_fs: sd_fs {
compatible = "zephyr,fstab,fatfs";
automount;
disk-access;
mount-point = "/SD:";
};
};

/ {
zephyr,user {
digital-pin-gpios = <&gpioh 15 0>, /* D0 */
Expand Down
2 changes: 1 addition & 1 deletion variants/common/arduino_flash_fs.dtsi
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@
cache-size = <4096>;
};

fstab {
fstab: fstab {
compatible = "zephyr,fstab";

wlan_fs: wlan_fs {
Expand Down
Loading