Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix simultaneous use of SDcard and USB MSD #1523

Merged
merged 1 commit into from
Dec 4, 2019
Merged

Fix simultaneous use of SDcard and USB MSD #1523

merged 1 commit into from
Dec 4, 2019

Conversation

martin-kuhn
Copy link
Contributor

@martin-kuhn martin-kuhn commented Dec 2, 2019

Description

The Windows.Storage library allows file operations on SD cards and USB MSD.

Motivation and Context

With the present code, it is only possible to use either the SD card or the USB MSD but not both at the same time. To get this to work, small changes were necessary.

How Has This Been Tested?

This has been tested on a custom STM32F427 based board with an SD card slot and an USB Host port. With the FileAccess sample from the GitHub repo, which has been adjusted to use two removable devices, the functionality could be verified.

Types of changes

  • Improvement (non-breaking change that improves a feature, code or algorithm)
  • Bug fix (non-breaking change which fixes an issue)
  • New feature (non-breaking change which adds functionality)
  • Breaking change (fix or feature that would cause existing functionality to change)

Checklist:

  • My code follows the code style of this project.
  • My change requires a change to the documentation.
  • I have updated the documentation accordingly.
  • I have read the CONTRIBUTING document.
  • I have added tests to cover my changes.
  • All new and existing tests passed.

@nfbot
Copy link
Member

nfbot commented Dec 2, 2019

Hi @martin-kuhn,

I'm nanoFramework bot.
Thank you for your contribution!

A human will be reviewing it shortly. 😉

Copy link
Member

@josesimoes josesimoes left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM!
Thanks for the fix. 👍

@josesimoes josesimoes added Platform: STM32 Everything related specifically with ChibiOS platform Type: enhancement labels Dec 2, 2019
@josesimoes
Copy link
Member

@martin-kuhn have you tested this code with the following variations? 🤔

  • 1. just the SD Card
  • 2. just the USB MSD
  • 3. none

@martin-kuhn
Copy link
Contributor Author

Yes, I checked all of the four possibilities:

  • Both SD card and USB MSD
  • Just the SD card
  • Just the USB MSD
  • none of them
    ...which work as expected.

@josesimoes
Copy link
Member

Are you sure that you're not getting a Null on 3. Instead of am empty collection?
Because you moved the StorageFolder instantiation to inside the for loop, which will never get there if there are no drives.

@martin-kuhn
Copy link
Contributor Author

With no device inserted I get an Windows.Storage.StorageFolder array of length 0 (as you can see in the picture). If there's only one device, then the array has length 1 and for two devices it has length 2.
I had to move the instantiation into the for loop because otherwise it gets done only for the first pointer. At the end of the for loop, storageFolder (which is a pointer to the StorageFolder array) gets incremented and then we have to create an instance also for this one. Otherwise, the second drive would always be Null (as it was before).
RemovableDevices

@@ -73,6 +73,7 @@ HRESULT Library_win_storage_native_Windows_Storage_StorageFolder::GetRemovableSt
uint32_t driveCount = 0;
char workingDrive[sizeof(DRIVE_PATH_LENGTH)];
uint16_t driveIterator = 0;
bool busy = false;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Don't think this busy flag is needed. The sdCardEnumerated and usbMsdEnumerated should take care of this.
I've just tested your fix without it and it seems to be working as expected.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I tested it quickly without the busy flag. It is needed anyway. Without the busy flag, both if conditions of the SDcard and the USB MSD are true because they are both ready and not enumerated at the beginning. So they get executed both in the first iteration of the loop and then the workingDrive of the SD card gets overwritten by the workingDrive of the USB MSD. This means that in both iterations the same device (the second one, which is the USB MSD) is mounted. So you'll end up with an array which has two StorageFolders but they are exactly the same.
I would have preferred to use the enumerated flags instead of an additional busy flag, but this is not possible because if you use only one drive, the other one is not defined.

Copy link
Member

@josesimoes josesimoes left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Waiting for your comment on the busy flag. 😃

@josesimoes
Copy link
Member

Just tested the changes with all possible variations in the PR submitted to your repo and all it's working.

image

image

image

image

@martin-kuhn
Copy link
Contributor Author

martin-kuhn commented Dec 4, 2019

Thanks for your solution. I came to the same one yesterday, but I dropped it because of this:
This seems to work as long as HAL_USE_SDC is defined. You increase maxDriveCount twice for the SD-card but never for the USB MSD (by accident I guess?). In my opinion, this does not work if someone wants to use only the USB MSD. With maxDriveCount set to 1, the for loop could never reach a driveIterator with 1, so the if(driveIterator == USB_MSD_DRIVE_INDEX_NUMERIC) would never be executed. Right?

@josesimoes
Copy link
Member

josesimoes commented Dec 4, 2019

That shouldn't be a problem. The SD_CARD_DRIVE_INDEX_NUMERIC is set to 1 or 0 according to the defines on what's being used.
Agreed that's not obvious that's why there is that comment trying to explain how these work. It's on the target storage header file. It was previously on the C file but I moved it there so it could be used in the rest of the code (as it is now 😜)

@martin-kuhn
Copy link
Contributor Author

Ok, I see. So USB_MSD_DRIVE_INDEX_NUMERIC is set to 0 when HAL_USE_SDC is not defined. Great!
Maybe we should avoid that the code starting from line 167 does not get executed if none of the if-conditions are true. What do you think? That's the case when the filesystems are not ready yet.

@martin-kuhn
Copy link
Contributor Author

Ah, that's already the case. I didn't see that, sorry :)

@josesimoes
Copy link
Member

I don't think that check is required. If the file systems aren't ready the drive count will be 0 and that code block is never entered.

@josesimoes
Copy link
Member

Can you please squash all these so the changes being made here are clearer?

@martin-kuhn
Copy link
Contributor Author

I cleaned up the code with some small changes (instantiation of StorageFolder was there twice, maxDriveCount fixed). It's tested and it works in all of the 4 possibilities.
What did you mean by "squash"?

@josesimoes
Copy link
Member

josesimoes commented Dec 4, 2019

What did you mean by "squash"?

https://gist.github.com/patik/b8a9dc5cd356f9f6f980

@claassistantio
Copy link

claassistantio commented Dec 4, 2019

CLA assistant check
All committers have signed the CLA.

@martin-kuhn
Copy link
Contributor Author

finished 🙂

Copy link
Member

@josesimoes josesimoes left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM!

@josesimoes josesimoes changed the title Add support for parallel use of SDcard and USB MSD Fix simultaneous use of SDcard and USB MSD Dec 4, 2019
@josesimoes josesimoes merged commit 8cda296 into nanoframework:develop Dec 4, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Platform: STM32 Everything related specifically with ChibiOS platform Type: bug
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants