We are running a survey to help us improve the experience for all of our members. If you see the survey appear, please take the time to tell us about your experience if you can.
Hello,
I've ported an example for an Infineon Evalboard to the MBC4300 Evalboard. The example shows how to access the SD-Card file system either via USB host or from the uController.
http://www.keil.com/appnotes/docs/apnt_273.asp
This example seems to work as expected on my MBC4300, I can access the SD-Card from PC Host and the uController can write files onto the SD-Card. I want to use a similar case in my application, but something does not work as expexted, so I went back to the example code to look at some details using the debugger.
// original code from the example: case DEV_START_RECORD: // hide logical unit /*B1*/ USBD_MSC0_SetMediaOwnerFS(); finit("M0:"); fmount("M0:"); f_but = fopen("ButtonsLog.txt","a"); f_adc = fopen("AdcLog.txt","a"); ADC_StartConversion(); gState = DEV_RECORDING; break;
This code should change the ownership to the uController and create two files.
Condition 1: NO USB-Cable is attached When I set a breakpoint at B1 and step over the function "USBD_MSC0_SetMediaOwnerFS()" it takes ~3 seconds (timeout defined inside the function) After the call the Variable "usbd_msc0_media_own" is 2 which means a change is requested (and still pending). Function returns USBD_MSC0_ERROR. The following functions (finit, fmount, fopen) do NOT fail although the ownership is not granted.
Condition 2: USB-Cable IS attached Function returns "USBD_MSC0_OK" after <1sec. After the call the Variable "usbd_msc0_media_own" is 0
I would expect the opposite behavior.
I don't have the Infineon Evalboard so I cannot test the original unmodified example. Is it possible that I made a mistake when I ported the example?
Is there a more detailed documentation available about the functions USBD_MSC0_SetMediaOwnerFS() and USBD_MSC0_SetMediaOwnerUSB()
e.g.: Does it automatically close open files when ownership is changed from FS to USB? Would it reject a change from USB to FS when the USB-host accesses the SD-Card? Would it allow a change from USB to FS when the USB-cable is plugged in but USB-host does NOT access athe SD-card? Thank you.
Main thing to keep in mind is that these functions depend on USB Host requests (mainly TestUnitReady that is executed periodically by USB Host) that means that USB connection is necessary for role switching in this case, of course if USB is not connected you can access SD Card with File System but role switching can not happen if there is no USB connection.
Closing of the open files before switching ownership should be done by user before media ownership change is requested.
Ownership changes depend only on USB Host requests that want to access the media but such cases as USB host accessing media when ownership change is requested are not handled by USB stack they are under user control completely and they are user's responsibility.
Thank you, now this makes more sense to me. Where can I find more info about the internal mechanism of this function?
Maybe you have a suggestion for following case:
User connects a USB cable. Application calls
USBD_MSC0_SetMediaOwnerUSB()
User unplugs the USB cable. Application wants to access the File system and calls
USBD_MSC0_SetMediaOwnerFS()
The function will run into a timeout and fail. But it does not mean that access to the file system is not allowed. What can I do with the return value?
I could check the VBus voltage before calling the function and could find out that there is no USB cable connected. Is it save to omit the function call
and only call
finit ("M0:"); fmount ("M0:");
in this case, or is there still a lock/unlock or similar that should take place although the function fails? Thank you.
Well, you are correct, if cable is disconnected, function to switch ownership will timeout, and it is best then that you manually mount SD Card for usage with File System.
You will probably be able to figure out the details on how to do that by analyzing source file responsible for executing the switch (the mechanics) which is in USBD_User_MSC_0.c file most important part is in function uint32_t USBD_MSC0_CheckMedia (void).
BTW there is an official MassStorage example for Keil's MCB4300 eval board in LPC4300_DFP pack in a path similar to this: C:\Keil\ARM\Pack\Keil\LPC4300_DFP\2.4.0\Boards\Keil\MCB4300\Middleware\USB\Device\MassStorage