Can't open file with mass storage for reading (nRF5340, ncs v 2.7.0)

Hi,

I have a mass storage that format a fat_fs volume and create a file when I plug the device by USB. Here is how I create a file : 

    int lErr;
    char lPath[MAX_PATH_LEN];
    snprintf(lPath, MAX_PATH_LEN, "/NAND:/%s", aDstFileName);
    struct fs_file_t lFile;

    fs_file_t_init(&lFile);
	lErr = fs_open(&lFile, lPath, FS_O_CREATE | FS_O_RDWR);
	if (lErr < 0) {
		LOG_ERROR("Failed to open %s: %d", lPath, lErr);
		return E_UNEXPECTED_RESULT;
	}
	lErr = fs_seek(&lFile, 0, FS_SEEK_SET);
	if (lErr < 0) {
		LOG_ERROR("Failed to seek %s: %d", lPath, lErr);
        return E_UNEXPECTED_RESULT;
	}
	lErr = fs_write(&lFile, aBuffer, aBufferSize);
	if (lErr < 0) {
		LOG_ERROR("Failed to write %s: %d", lPath, lErr);
        return E_UNEXPECTED_RESULT;
	}
	lErr= fs_close(&lFile);
	if (lErr< 0) {
		LOG_ERROR("Failed to close %s: %d", lPath, lErr);
		return E_UNEXPECTED_RESULT;
	}

    return SUCCESS;
}

It works fine, and I can see my file is present when I plug the USB. Even if I remove it, it is re-created when I unplug/replug the USB. 

I would like to be able to read a file I would drag and drop in the mass storage. In windows, I create a TEST.TXT and I drag and drop in in the mass storage. Then, when I unplug the USB, and before unmounting the volume, here is what I do : 

    int lErr;
    char lPath[MAX_PATH_LEN];
    char lPath2[MAX_PATH_LEN];
    char lPath3[MAX_PATH_LEN];
    snprintf(lPath, MAX_PATH_LEN, "/NAND:/%s", aFileName);
    snprintf(lPath2, MAX_PATH_LEN, "NAND:/%s", aFileName);
    snprintf(lPath3, MAX_PATH_LEN, "/%s", aFileName);
    struct fs_file_t lFile;

    fs_file_t_init(&lFile);
    lErr = fs_open(&lFile, lPath, FS_O_READ);
    if (lErr < 0) {
        LOG_ERROR("Failed to open %s: %d", lPath, lErr);
        // return E_UNEXPECTED_RESULT;
    }
    lErr = fs_open(&lFile, lPath2, FS_O_READ);
    if (lErr < 0) {
        LOG_ERROR("Failed to open %s: %d", lPath2, lErr);
        // return E_UNEXPECTED_RESULT;
    }

    lErr = fs_open(&lFile, lPath3, FS_O_READ);
    if (lErr < 0) {
        LOG_ERROR("Failed to open %s: %d", lPath3, lErr);
        return E_UNEXPECTED_RESULT;
    }
    
    lErr = fs_read(&lFile, aBuffer, aBufferSize);
    if (lErr < 0) {
        LOG_ERROR("Failed to read %s: %d", lPath, lErr);
        return E_UNEXPECTED_RESULT;
    }
    lErr = fs_close(&lFile);
    if (lErr < 0) {
        LOG_ERROR("Failed to close %s: %d", lPath, lErr);
        return E_UNEXPECTED_RESULT;
    }

    return SUCCESS;

But here, the fs_open() fails with error -2 (no such file or directory) : 

EDIT : when I try the same code with the file I create each time I plug the USB, it works.

What am I missing? 

2nd question : is there a way to list files present in the drive?

Thank you very much

Related