fatfs sample: file close error (-5)

Hi, I'm running the fatfs sample using an Actinius Icarus board (nrf9160) and a microSD card, connected via SPI. The original sample works as it should (it mounts and opens the SD card and then lists the files on it), but when adding some lines to write a file, I'm having the following issue:

  • If the string I'm trying to write has more than 11 characters, it writes well but then I get the message: "<err> fs: file close error (-5)". When opening the file on the SD card using a USB reader, sometimes I can see the file with the string I wrote, but sometimes It has different characters. I already tried to synchronize the file first using fs_sync() and it returned "file sync error (-5)". I also checked this ticket: https://devzone.nordicsemi.com/f/nordic-q-a/92166/fs-file-write-error--5  but deleting the line does not solve my issue, since it doesn't print the error message but the writing still fails.
  • If the string I'm trying to write has less than 11 characters, the system starts rebooting repeatedly and never writes anything.

This is the .overlay I'm using:

&spi3{
    status = "okay";
    cs-gpios = <&gpio0 03 GPIO_ACTIVE_LOW>;
    sdhc0: sdhc@0 {
        compatible = "zephyr,sdhc-spi-slot";
        reg = <0>;
        status = "okay";
        label = "SDHC0";
       mmc {
            compatible = "zephyr,sdmmc-disk";
            status = "okay";
        };
        spi-max-frequency = <24000000>;
    };
};


This is the additional .config file I added (I'm aware that some of these lines might be unnecessary since they seem to be repeated):

CONFIG_SPI=y
CONFIG_GPIO=y
CONFIG_DISK_ACCESS=y
CONFIG_DISK_DRIVER_SDMMC=y
CONFIG_DISK_DRIVERS=y
CONFIG_FS_FATFS_EXFAT=y
CONFIG_SDHC=y
CONFIG_LOG=y
CONFIG_FILE_SYSTEM=y
CONFIG_FAT_FILESYSTEM_ELM=y
CONFIG_PRINTK=y
CONFIG_MAIN_STACK_SIZE=4096
CONFIG_SPI_NRFX_RAM_BUFFER_SIZE=8


And these are the lines I added to main.c:
struct fs_file_t file;
int err;
char data[] = "aaaaaaaaaaaa";                          //This is the string whose length determines how the system works

err = fs_open(&file, "/SD:/test.txt",FS_O_WRITE | FS_O_CREATE);
if (err) {
    printk("Error al abrir el archivo: %d\n" ,err);
}
err = fs_write(&file, data, sizeof(data));
if (err < 0) {
    printk("Error al escribir en el archivo: %d\n", err);
}
printk("Escrito correctamente %d bytes.\n", err);
err = fs_close(&file);

I'm working with SDK version 2.1.0.
Thanks in advance,
Rocío
Parents
  • Hi Rocio, 

    I don't have a board with a SDCard setup to test the sample for now. I would suggest to take a look at this ticket: 
    SD card writing speed issue

    The customer was able to write to a file the issue was the speed. But it's not the issue for  you now. Could you take a look at his configuration to see if there is anything missing. 
    I saw that he has 
    CONFIG_DISK_ACCESS_SDHC=y
    CONFIG_DISK_ACCESS_SPI_SDHC=y

  • Hi, thank you very much for your response! I checked the ticket and, as you pointed out, I don't have those lines. But when adding them to my configuration, I get an error message because the symbols CONFIG_DISK_ACCESS_SDHC and CONFIG_DISK_ACCESS_SPI_SDHC are not recognized. I thought this may be because I'm using a different SDK version (and changing it isn't an option since I'm working in a project that started some years ago). 

    After some research on what the proper syntax for those symbols might be, I found CONFIG_DISK_DRIVER_SDMMC, which is already on my .config. Could you help me find any other alternative?

Reply
  • Hi, thank you very much for your response! I checked the ticket and, as you pointed out, I don't have those lines. But when adding them to my configuration, I get an error message because the symbols CONFIG_DISK_ACCESS_SDHC and CONFIG_DISK_ACCESS_SPI_SDHC are not recognized. I thought this may be because I'm using a different SDK version (and changing it isn't an option since I'm working in a project that started some years ago). 

    After some research on what the proper syntax for those symbols might be, I found CONFIG_DISK_DRIVER_SDMMC, which is already on my .config. Could you help me find any other alternative?

Children
  • Hi Rocio, 
    Have you tried to step into the code to see which function inside fs_close() return -5 ? 

    Could you try to test with newer SDK ? In newer SDK the fat_fs sample or fs_sample actually access file and call fs_close(), not just listing dir. Could you try with these samples to see if you have the same problem.

  • Hi, I tried only opening and closing the file, on another computer with a newer SDK version and a nrf52840, and it worked well. For the build, I added the same .conf additional file and the same .overlay (changing only the addresses of the SPI pins, as it was a different board).

    When trying the same code (open and close a file) back on my old SDK and nrf9160, it rebooted as I described previously. 

    Another thing I tried, is not using the array data[] in the writing line (for example: err = fs_write(&file, "11111", 5); ), and the result stills depends on data[]'s length (>11: file close error; 11: system reboots ). This surprised me as I'm not using the array after defining it (if I delete its definition and change nothing else..... the system reboots).

    What else can I try?

  • Update: I copied the new filesystem sample directly from Zephyr's directory and tried to run it on Actinius Icarus. It works well until I add the CONFIG_FS_SAMPLE_CREATE_SOME_ENTRIES=y macro, where I get the following output:
    <inf> main: Creating some dir entries in /SD:
    2,343] <err> fs: file close error (-5)
    [00:00:24.000,488] <err> fs: failed to create directory (-5)
    [00:00:24.000,518] <err> main: Failed to create dir /SD:/some

  • Hi Rocio, 
    Sorry for the late reply. I haven't managed to figure out anything further. The challenge here is that we don't have the same board as yours and I was looking for a SD card reader board but haven't got one. 

    My suggestion, if you still have the issue, is to try step in the code and see why file close return error -5 (IO error). 
    When  you test with nRF52840 did you test with a SD Card ? 
    If you did , you can use a logic analyzer and try to compare the signal out from the nRF52 and the signal out from the nRF91 to see why there is a difference. 

Related