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

nrf52840 DK USBD MSC multiple directories bug

While developing code for our custom board, we ran into a filesystem corruption issue with fatfs+qspi block device and multiple directories. We were able to reproduce the bug in the usbd_msc example in SDK 15.2 AND 15.3 using nRF52840 DK:

1. Erase qspi external flash via `nrfjprog --qspieraseall`

2. Modified the example code to call f_mkfs() if no file system is found. After successful init, I create two directories and then two files within each of the two directories. See attached code.

3. Ran the program and found two files in the two directories as expected. Ejected the nRF DK MSC volume and rebooted the chip to find the first directory corrupted as shown here:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
/**
* Copyright (c) 2016 - 2018, Nordic Semiconductor ASA
*
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form, except as embedded into a Nordic
* Semiconductor ASA integrated circuit in a product or a software update for
* such product, must reproduce the above copyright notice, this list of
* conditions and the following disclaimer in the documentation and/or other
* materials provided with the distribution.
*
* 3. Neither the name of Nordic Semiconductor ASA nor the names of its
* contributors may be used to endorse or promote products derived from this
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX

I've applied the qspi block device patch explained here: https://devzone.nordicsemi.com/f/nordic-q-a/36654/usbd_msd-disk-initialization-fails-in-usb-unplug-with-sdk15-0/141462#141462

To summarize, opening files in one folder seems to corrupt/destroy files in another folder. This is reproducible every single time. Things I've tried in our custom firmware:

1. Turn on FS_REENTRANT and supply semaphore functions via FreeRTOS
2. Add delay between creating a file in dir1 and a file in dir2