I started with ncs/nrf/samples/nrf9160/lte_ble_gateway and have taken parts of ncs/zephyr/samples/subsys/fs/fat_fs to get SD card support
prj.conf
CONFIG_NEWLIB_LIBC=y
CONFIG_NEWLIB_LIBC_FLOAT_PRINTF=y
CONFIG_NEWLIB_LIBC_FLOAT_SCANF=y
CONFIG_ASSERT=y
CONFIG_REBOOT=y
# Network
CONFIG_NETWORKING=y
CONFIG_NET_NATIVE=n
CONFIG_NET_SOCKETS=y
CONFIG_NET_SOCKETS_OFFLOAD=y
# LTE link control
CONFIG_LTE_LINK_CONTROL=y
CONFIG_LTE_AUTO_INIT_AND_CONNECT=n
# AT host
CONFIG_AT_HOST_LIBRARY=y
# BSD library
CONFIG_BSD_LIBRARY=y
# Disable Modem traces, since we need UART1 for HCI
CONFIG_BSD_LIBRARY_TRACE_ENABLED=n
# Library for buttons and LEDs
CONFIG_DK_LIBRARY=y
CONFIG_DK_LIBRARY_INVERT_LEDS=n
# Console for user association
CONFIG_CONSOLE_SUBSYS=y
CONFIG_CONSOLE_GETCHAR=y
# Enable Bluetooth stack and libraries
CONFIG_BT=y
CONFIG_BT_H4=y
CONFIG_BT_WAIT_NOP=y
CONFIG_BT_CENTRAL=y
CONFIG_BT_GATT_CLIENT=y
CONFIG_BT_GATT_DM=y
CONFIG_BT_SCAN=y
CONFIG_BT_SCAN_FILTER_ENABLE=y
CONFIG_BT_SCAN_UUID_CNT=1
CONFIG_BT_GATT_NUS_C=y
CONFIG_BT_MAX_CONN=16
CONFIG_BT_CONN_CTX=y
CONFIG_BT_L2CAP_RX_MTU=80
CONFIG_BT_L2CAP_TX_MTU=80
# UART bridge connection
CONFIG_UART_2_NRF_FLOW_CONTROL=y
CONFIG_UART_INTERRUPT_DRIVEN=y
# SD card
CONFIG_SPI=y
CONFIG_SPI_3=y
CONFIG_DISK_ACCESS=y
CONFIG_DISK_ACCESS_SDHC=y
CONFIG_DISK_ACCESS_SPI_SDHC=y
CONFIG_FILE_SYSTEM=y
CONFIG_FAT_FILESYSTEM_ELM=y
CONFIG_NRFX_SPIM=y
CONFIG_SPI_NRFX_RAM_BUFFER_SIZE=64
# Heap and stacks
CONFIG_HEAP_MEM_POOL_SIZE=16384
CONFIG_MAIN_STACK_SIZE=8192
# Enable the MQTT library.
CONFIG_MQTT_LIB=y
boards/nrf9160dk_nrf9160ns.overlay
/ {
chosen {
zephyr,bt-uart=&uart2;
};
};
&uart2 {
compatible = "nordic,nrf-uarte";
current-speed = <1000000>;
status = "okay";
tx-pin = <18>;
rx-pin = <17>;
rts-pin = <21>;
cts-pin = <19>;
};
&spi3 {
status = "okay";
cs-gpios = <&gpio0 0 0>;
sck-pin = <3>;
mosi-pin = <2>;
miso-pin = <1>;
sdhc0: sdhc@0 {
compatible = "zephyr,mmc-spi-slot";
reg = <0>;
status = "okay";
label = "SDHC0";
spi-max-frequency = <24000000>;
};
};
The main file is currently just from the fat_fs projects
main.c
#include <zephyr.h>
#include <device.h>
#include <disk/disk_access.h>
#include <logging/log.h>
#include <fs/fs.h>
#include <ff.h>
LOG_MODULE_REGISTER(main);
static int lsdir(const char *path);
static FATFS fat_fs;
/* mounting info */
static struct fs_mount_t mp = {
.type = FS_FATFS,
.fs_data = &fat_fs,
};
/*
* Note the fatfs library is able to mount only strings inside _VOLUME_STRS
* in ffconf.h
*/
static const char *disk_mount_pt = "/SD:";
void main(void)
{
/* raw disk i/o */
do {
static const char *disk_pdrv = "SD";
u64_t memory_size_mb;
u32_t block_count;
u32_t block_size;
if (disk_access_init(disk_pdrv) != 0) {
LOG_ERR("Storage init ERROR!");
break;
}
if (disk_access_ioctl(disk_pdrv,
DISK_IOCTL_GET_SECTOR_COUNT, &block_count)) {
LOG_ERR("Unable to get sector count");
break;
}
LOG_INF("Block count %u", block_count);
if (disk_access_ioctl(disk_pdrv,
DISK_IOCTL_GET_SECTOR_SIZE, &block_size)) {
LOG_ERR("Unable to get sector size");
break;
}
printk("Sector size %u\n", block_size);
memory_size_mb = (u64_t)block_count * block_size;
printk("Memory Size(MB) %u\n", (u32_t)memory_size_mb>>20);
} while (0);
mp.mnt_point = disk_mount_pt;
int res = fs_mount(&mp);
if (res == FR_OK) {
printk("Disk mounted.\n");
lsdir(disk_mount_pt);
} else {
printk("Error mounting disk.\n");
}
while (1) {
k_sleep(K_MSEC(1000));
}
}
static int lsdir(const char *path)
{
int res;
struct fs_dir_t dirp;
static struct fs_dirent entry;
/* Verify fs_opendir() */
res = fs_opendir(&dirp, path);
if (res) {
printk("Error opening dir %s [%d]\n", path, res);
return res;
}
printk("\nListing dir %s ...\n", path);
for (;;) {
/* Verify fs_readdir() */
res = fs_readdir(&dirp, &entry);
/* entry.name[0] == 0 means end-of-dir */
if (res || entry.name[0] == 0) {
break;
}
if (entry.type == FS_DIR_ENTRY_DIR) {
printk("[DIR ] %s\n", entry.name);
} else {
printk("[FILE] %s (size = %zu)\n",
entry.name, entry.size);
}
}
/* Verify fs_closedir() */
fs_closedir(&dirp);
return res;
}
When I run the code I get this output:
*** Booting Zephyr OS build v2.3.0-rc1-ncs1 ***
*** Booting Zephyr OS build v2.3.0-rc1-ncs1 ***
Flash regions Domain Permissions
00 00 0x00000 0x08000 Secure rwxl
01 31 0x08000 0x100000 Non-Secure rwxl
Non-secure callable region 0 placed in flash region 0 with size 32.
SRAM region Domain Permissions
00 07 0x00000 0x10000 Secure rwxl
08 31 0x10000 0x40000 Non-Secure rwxl
Peripheral Domain Status
00 NRF_P0 Non-Secure OK
01 NRF_CLOCK Non-Secure OK
02 NRF_RTC0 Non-Secure OK
03 NRF_RTC1 Non-Secure OK
04 NRF_NVMC Non-Secure OK
05 NRF_UARTE1 Non-Secure OK
06 NRF_UARTE2 Non-Secure OK
07 NRF_TWIM2 Non-Secure OK
08 NRF_SPIM3 Non-Secure OK
09 NRF_TIMER0 Non-Secure OK
10 NRF_TIMER1 Non-Secure OK
11 NRF_TIMER2 Non-Secure OK
12 NRF_SAADC Non-Secure OK
13 NRF_PWM0 Non-Secure OK
14 NRF_PWM1 Non-Secure OK
15 NRF_PWM2 Non-Secure OK
16 NRF_PWM3 Non-Secure OK
17 NRF_WDT Non-Secure OK
18 NRF_IPC Non-Secure OK
19 NRF_VMC Non-Secure OK
20 NRF_FPU Non-Secure OK
21 NRF_EGU1 Non-Secure OK
22 NRF_EGU2 Non-Secure OK
23 NRF_DPPIC Non-Secure OK
24 NRF_GPIOTE1 Non-Secure OK
25 NRF_REGULATORS Non-Secure OK
SPM: NS image at 0xc000
SPM: NS MSP at 0x2002d530
SPM: NS reset vector at 0x10311
SPM: prepare to jump to Non-Secure image.
*** Booting Zephyr OS build v2.3.0-rc1-ncs1 ***
*** Booting Zephyr OS build v2.3.0-rc1-ncs1 ***
Flash regions Domain Permissions
00 00 0x00000 0x08000 Secure rwxl
01 31 0x08000 0x100000 Non-Secure rwxl
Development setup:
OS: Ubuntu 20.04
SW: nrf SDK v1.3.0
HW: nrf9160-dk v0.9.0, AdaFruit MICROSD CARD BREAKOUT BOARD+ (https://www.adafruit.com/product/254)