This post is older than 2 years and might not be relevant anymore
More Info: Consider searching for newer posts

nRF9160 and SD card example

Dear All,

I want to create a simple example, where the nRF9160 will create a file in an SD card, read it, write to it etc.

I found an example in ncs/zephyr/samples/subsys/fs/fat_fs. In this example the device will mount the SD card and read out the files in it, so it seems to be the perfect starting point for me.

In the Zephyr documentation (here) it is suggested that I need to implement an overlay. But when I make that overlay file the project won't be loaded on the SES, giving me this error:

My overlay file looks like this:

CONFIG_SPI=y
CONFIG_SPI_1=y
CONFIG_DISK_ACCESS=y
CONFIG_DISK_ACCESS_SDHC=y
CONFIG_DISK_ACCESS_SPI_SDHC=y
CONFIG_DISK_SDHC_VOLUME_NAME="SD"
CONFIG_LOG=y
CONFIG_FILE_SYSTEM=y
CONFIG_FAT_FILESYSTEM_ELM=y
CONFIG_PRINTK=y

From what I understand the overlay file is somehow messing the project up, but I don't see how that happens.

If I do not add the overlay file, the project loads normally, but I am getting this error when I try to compile:

Building ‘zephyr/drivers/spi/libdrivers__spi.a’ from solution ‘build’ in configuration ‘Common’
1> Archiving ‘libdrivers__gpio.a’
2> Compiling ‘spi_nrfx_spim.c’
2> In file included from C:/ncs/zephyr/include/arch/arm/arch.h:26:0,
2>                  from C:/ncs/zephyr/include/arch/cpu.h:17,
2>                  from C:/ncs/zephyr/include/kernel_includes.h:34,
2>                  from C:/ncs/zephyr/include/kernel.h:17,
2>                  from C:/ncs/zephyr/include/device.h:11,
2>                  from C:/ncs/zephyr/include/drivers/spi.h:24,
2>                  from C:/ncs/zephyr/drivers/spi/spi_nrfx_spim.c:7:
2> C:/ncs/zephyr/drivers/spi/spi_nrfx_spim.c:374:8: error: 'DT_NORDIC_NRF_SPIM_SPI_1_IRQ_0_PRIORITY' undeclared (first use in this function); did you mean 'DT_NORDIC_NRF_SPIM_SPI_3_IRQ_0_PRIORITY'?
2> C:/ncs/zephyr/include/arch/arm/irq.h:82:32: note: in definition of macro 'Z_ARCH_IRQ_CONNECT'
2> C:/ncs/zephyr/drivers/spi/spi_nrfx_spim.c:373:3: note: in expansion of macro 'IRQ_CONNECT'
2> C:/ncs/zephyr/drivers/spi/spi_nrfx_spim.c:412:1: note: in expansion of macro 'SPI_NRFX_SPIM_DEVICE'
2> C:/ncs/zephyr/drivers/spi/spi_nrfx_spim.c:374:8: note: each undeclared identifier is reported only once for each function it appears in
2> C:/ncs/zephyr/include/arch/arm/irq.h:82:32: note: in definition of macro 'Z_ARCH_IRQ_CONNECT'
2> C:/ncs/zephyr/drivers/spi/spi_nrfx_spim.c:373:3: note: in expansion of macro 'IRQ_CONNECT'
2> C:/ncs/zephyr/drivers/spi/spi_nrfx_spim.c:412:1: note: in expansion of macro 'SPI_NRFX_SPIM_DEVICE'
2> C:/ncs/zephyr/drivers/spi/spi_nrfx_spim.c:387:17: error: 'DT_NORDIC_NRF_SPIM_SPI_1_SCK_PIN' undeclared here (not in a function); did you mean 'DT_NORDIC_NRF_SPIM_SPI_3_SCK_PIN'?
2> C:/ncs/zephyr/drivers/spi/spi_nrfx_spim.c:412:1: note: in expansion of macro 'SPI_NRFX_SPIM_DEVICE'
2> C:/ncs/zephyr/drivers/spi/spi_nrfx_spim.c:388:17: error: 'DT_NORDIC_NRF_SPIM_SPI_1_MOSI_PIN' undeclared here (not in a function); did you mean 'DT_NORDIC_NRF_SPIM_SPI_3_MOSI_PIN'?
2> C:/ncs/zephyr/drivers/spi/spi_nrfx_spim.c:412:1: note: in expansion of macro 'SPI_NRFX_SPIM_DEVICE'
2> C:/ncs/zephyr/drivers/spi/spi_nrfx_spim.c:389:17: error: 'DT_NORDIC_NRF_SPIM_SPI_1_MISO_PIN' undeclared here (not in a function); did you mean 'DT_NORDIC_NRF_SPIM_SPI_3_MISO_PIN'?
2> C:/ncs/zephyr/drivers/spi/spi_nrfx_spim.c:412:1: note: in expansion of macro 'SPI_NRFX_SPIM_DEVICE'
2> In file included from C:/ncs/zephyr/include/drivers/spi.h:24:0,
2>                  from C:/ncs/zephyr/drivers/spi/spi_nrfx_spim.c:7:
2> C:/ncs/zephyr/drivers/spi/spi_nrfx_spim.c:399:9: error: 'DT_NORDIC_NRF_SPIM_SPI_1_LABEL' undeclared here (not in a function); did you mean 'DT_NORDIC_NRF_SPIM_SPI_3_LABEL'?
2> C:/ncs/zephyr/include/device.h:107:11: note: in definition of macro 'DEVICE_AND_API_INIT'
2> C:/ncs/zephyr/drivers/spi/spi_nrfx_spim.c:398:2: note: in expansion of macro 'DEVICE_DEFINE'
2> C:/ncs/zephyr/drivers/spi/spi_nrfx_spim.c:412:1: note: in expansion of macro 'SPI_NRFX_SPIM_DEVICE'
Build failed


which obviously comes from the fact that spi1 is not correctly initialized.

So what is the proper form of the overlay file?

Parents
  • At the moment I can get my code loaded and compiled in SES, but I still cannot read data from the SD card.

    So what I have now looks like this:
    &spi3 {
            status = "okay";
            cs-gpios = <&gpio0 16 0>;        
            sdhc0: sdhc@0 {
                    compatible = "mmc-spi-slot";
                    reg = <0>;
                    status = "okay";
                    label = "SDHC0";
                    spi-max-frequency = <24000000>;
            };
    };
    My device has the following pinout:
    ss: 16
    miso:17
    mosi:18
    sck:19
    The prj.conf file I am using looks like this:
    CONFIG_SPI=y
    CONFIG_SPI_3=y
    CONFIG_SPI_NRFX=y
    CONFIG_SPI_3_NRF_SPIM=y
    CONFIG_DISK_ACCESS=y
    CONFIG_DISK_ACCESS_SDHC=y
    CONFIG_DISK_ACCESS_SPI_SDHC=y
    CONFIG_DISK_SDHC_VOLUME_NAME="SD"
    CONFIG_LOG=y
    CONFIG_FILE_SYSTEM=y
    CONFIG_FAT_FILESYSTEM_ELM=y
    CONFIG_PRINTK=y# CONFIG_NEWLIB_LIBC=y
    # CONFIG_BSD_LIBRARY=y
    # CONFIG_BSD_LIBRARY_TRACE_ENABLED=n
    # CONFIG_TRUSTED_EXECUTION_NONSECURE=y
    # CONFIG_NET_BUF_USER_DATA_SIZE=1024CONFIG_GPIO=y
    I am pretty sure that I am missing something in the overlay file, but I don't know what.When I run the code I get the following output:
    12:19:44.143 -> ***** Booting Zephyr OS build v2.0.99-ncs1-4-g52160ca9a102 *****
    12:19:44.177 -> Error mounting disk.
    12:19:44.177 -> [00:00:00.007,751] [1;31m<err> main: Storage init ERROR![0m
    12:19:44.177 -> [00:00:00.007,781] [1;31m<err> fs: fs mount error (-5)[0m
    Thank you in advance for your assistance.
  • Hello ,

    Sorry for the delayed response. I am currently working with tag v1.1.0 of the nrf sdk.

    At the moment I have the following prj.conf:

    CONFIG_SPI=y
    CONFIG_SPI_3=y
    CONFIG_DISK_ACCESS=y
    CONFIG_DISK_ACCESS_SDHC=y
    CONFIG_DISK_ACCESS_SPI_SDHC=y
    CONFIG_DISK_SDHC_VOLUME_NAME="SD"
    CONFIG_LOG=y
    CONFIG_FILE_SYSTEM=y
    CONFIG_FAT_FILESYSTEM_ELM=y
    CONFIG_PRINTK=y
    

    overlay file:

    &spi3 {
            status = "okay";
            sck-pin = <19>;
            mosi-pin = <18>;
            miso-pin = <17>;
    };
    
    &spi3 {
            cs-gpios = <&gpio0 16 1>;
    
            sdhc0: sdhc@0 {
                    compatible = "zephyr,mmc-spi-slot";
                    reg = <0>;
                    status = "okay";
                    label = "SDHC0";
                    spi-max-frequency = <24000000>;
            };
    };

    and the main is this:

    /*
     * Copyright (c) 2019 Tavish Naruka <[email protected]>
     *
     * SPDX-License-Identifier: Apache-2.0
     */
    
    /* Sample which uses the filesystem API and SDHC driver */
    
    #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 compile the code, I see the following message:

    [00:00:00.005,950] <inf> spi_nrfx_spim: CS control inhibited (no GPIO device)

    If I remove the sd card from the reader, I see the following messages:

    14:45:40.709 -> [00:02:14.745,361] <err> main: Storage init ERROR!
    14:45:44.716 -> Error mounting disk.
    14:45:45.698 -> [00:02:19.747,680] <err> fs: fs mount error (-5)


    The sd card I am using is the this one.

    I have formatted the sd card like this:

  • Giannis Anastasopoulos said:
    The sd card I am using is the this one.

     What sd card reader are you using?

  • I am just using the pins of the card, with SPI, I am not using an SDcard reader.

    So the slave device is the SD card itself

Reply Children
No Data
Related