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

Enabling SD Card in asset_tracker V1

Hi

I am trying to get the SDHC/Disk access with FAT working on the nRF9160 dev platform and Asset_Tracker V1 app

It is shown to be connected on the TRACE connector

so P0.21 to P0.25 and the DBG_CMD going to U1B pin 125 if soldered

So I have bought the correct socket and soldered it down

I am only using the 9140 for this

In the config I turn on SDHC

and I have tried the following code added to the asset_tracker v1 app

#include <fs/fs.h>
#include <ff.h>
#include <disk/disk_access.h>

static FATFS fat_fs;
/* mounting info */
static struct fs_mount_t mp = {
	.type = FS_FATFS,
	.fs_data = &fat_fs,
};

static const char *disk_mount_pt = "/SD:";

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;
}

static void init_disk(void)
{
    /* raw disk i/o */
    do {
            static const char *disk_pdrv = "SD";
            uint64_t memory_size_mb;
            uint32_t block_count;
            uint32_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 = (uint64_t)block_count * block_size;
            printk("Memory Size(MB) %u\n", (uint32_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");
    }
}

void main(void)
{
	LOG_INF("Asset tracker started");
	k_work_q_start(&application_work_q, application_stack_area,
		       K_THREAD_STACK_SIZEOF(application_stack_area),
		       CONFIG_APPLICATION_WORKQUEUE_PRIORITY);
	if (IS_ENABLED(CONFIG_WATCHDOG)) {
		watchdog_init_and_start(&application_work_q);
	}

#if defined(CONFIG_USE_UI_MODULE)
	ui_init(ui_evt_handler);
#endif

#if defined(CONFIG_LWM2M_CARRIER)
	k_sem_take(&nrf_modem_lib_initialized, K_FOREVER);
#else
	handle_nrf_modem_lib_init_ret();
#endif


//TRY THE DISK
  init_disk();

	cloud_api_init();

	work_init();
	
	............

But I have no luck.

Do I also need to configure a SPI port in hal_nordic? and specify the pins in use somewhere?

Is there a working example of the SDCard socket being used anywhere that I can refer to?

  • Hello, Billy!

    Please have a look at this case and the solution provided there. Feel free to reach out if you're facing any issues with it!

    Best regards,
    Carl Richard

  • Hi Carl,

    In the asset tracker folder, I have copied across the settings from the example in the link you sent to prj.conf

    But where is the overlay file? or does the asset_tracker not have one?

    The asset_tracker files:

    I was imagining I could copy the data from the example overlay file into the asset_tracker overlay file and change the pin numbers appropriately

  • Hi again!

    The asset tracker does not have an overlay file and this must be added. For the nRF9160DK you can create a new file called "nrf9160dk_nrf9160ns.overlay" and add your desired configurations there. This will be included automatically next time you build the application (make sure to reopen the project in SES). Please notify me if you face any issues with this!

    Best regards,
    Carl Richard

  • Hi Carl,

    I'm still struggling with this....

    I have the following overlay:

    &spi3 {
            status = "okay";
            cs-gpios = <&gpio0 25 GPIO_ACTIVE_LOW>;
            sck-pin = <21>;
    		mosi-pin = <27>;
    		miso-pin = <22>;
    
            sdhc0: sdhc@0 {
                    compatible = "zephyr,mmc-spi-slot";
                    reg = <0>;
                    status = "okay";
                    label = "SDHC0";
                    spi-max-frequency = <24000000>;
            };
    };
    

    Prj.conf

    #SDHC
    CONFIG_DISK_ACCESS=y
    CONFIG_LOG=y
    CONFIG_FILE_SYSTEM=y
    CONFIG_FAT_FILESYSTEM_ELM=y
    CONFIG_PRINTK=y
    
    # SPI
    CONFIG_DISK_ACCESS_SDHC=y
    CONFIG_DISK_ACCESS_SPI_SDHC=y
    CONFIG_SPI=y
    CONFIG_SPI_3=y
    CONFIG_SPI_NRFX_RAM_BUFFER_SIZE=8
    CONFIG_MAIN_STACK_SIZE=4096
    
    #GPIO
    CONFIG_GPIO=y
    
    # Segger RTT
    CONFIG_USE_SEGGER_RTT=y
    CONFIG_RTT_CONSOLE=y
    CONFIG_UART_CONSOLE=n
    CONFIG_LOG_BACKEND_RTT=y
    CONFIG_LOG_BACKEND_UART=n

    when i run the code I get

    *** Booting Zephyr OS build v2.4.99-ncs1  ***
    [00:00:00.202,301] <inf> at_host: UART check failed: 12. Dropping buffer and retrying.
    [00:00:00.212,921] <inf> asset_tracker: Asset tracker started
    [00:00:00.213,348] <inf> watchdog: Watchdog timeout installed. Timeout: 60000
    [00:00:00.213,775] <inf> watchdog: Watchdog started
    [00:00:00.214,141] <inf> watchdog: Watchdog feed enabled. Timeout: 30000
    [00:00:00.218,505] <err> asset_tracker: Storage init ERROR!
    [00:00:00.222,534] <err> fs: fs mount error (-5)

    On the scope, I see the CS and CLK lines moving, but nothing on the MISO or MOSI (I am running the board in 3V mode)

    Could there be something else in the asset_tracker app that is causing a problem for me?

  • Hi Carl,

    It seems to be something to do with the SPI config as if I move the MISO and MOSI to other pins, the same problem is apparent, I never see any activity on them, I have just moved them to 0.23 and 0.24, removed the SDCARD and just watched the lines.

    CLK works,

    CS works

    nothing on MOSI

    I'm pretty sure the init routine should be sending a string of 0xff's via MOSI on startup.

    What could I have missed to stop the signals?

Related