Hi,
I have some challenges in USB connection /disconnection through firmware.
For stopping USB MSC:
USB_DeviceStop(g_msc.deviceHandle);
USB_DeviceClassDeinit(CONTROLLER_ID);
For Restarting USB MSC:
g_msc.speed = USB_SPEED_FULL;
g_msc.attach = 0;
g_msc.mscHandle = (class_handle_t)NULL;
g_msc.deviceHandle = NULL;
USB_DeviceClassInit(CONTROLLER_ID, &msc_config_list, &g_msc.deviceHandle);
USB_DeviceRun(g_msc.deviceHandle);
I presently implemented the above code and the code work perfectly fine when there is no transfers happening. I created a scenario by copying a huge file to the K64F USB drive. When the file is being copied, I tried to disable the USB functionality. USB functionality disconnected successfully. But after that, program execution and stuck in an infinite loop of
file: usb_device_ehci.c
function: USB_DeviceEhciCancel()
line: while (ehciState->registerBase->EPFLUSH & primeBit).
I tried a little bit more also by disabling/enabling clock, SD card and interrupt in a proper sequence. But still the same result. Is there a way, I can connect and disconnect the USB functionality like a normal USB Drive even if there are any transfers happening?
Surprisingly, I tried disconnecting and connecting the USB cable (used for MSC) again while the transfer is made. There were no issues at all. It worked the way that everyone would expect.
best regards,
Mahendran
Solved! Go to Solution.
Hi Mahendrad,
I've been reviewing and running your code and I am not getting the same error message as you are. I have followed your steps as you described above, but I am not having a problem. The only message I get by the serial terminal is if I physically disconnect the SD card and try to reinitialize it while its not physically connected but that is ok.
Something that also may also help. When I uploaded your code to my MCUXpresso workspace it asked me to update SDK the example was using. So I'd highly recommend to make sure you are using the latest SDK along with the latest version of MCUXpresso. After doing this, verify if you are getting the same error message.
If you are still running into trouble check the USB_DeviceClassDeinit or USB_DeviceDeinit after using the USB_DeviceStop.
Please let me know any udpates you may have.
Best Regards,
Sabina
Hi Mahendran,
If possible could you attach your project so I can check how you are proceeding with disconnecting during the transfer.
In the meanwhile, from what I understand from your description I recommend that you take a look at the file usb_device_msc, you can find this in the examples that are provided in the SDK, that use the MSC. In this file you will see a function called "USB_DeviceCancel", this function cancels the transfer in a specified endpoint. In the same file you can see how this function is being used. Please let me know if this is what you were looking for.
Best Regards,
Sabina
Hi Sabina,
I am sorry about the late reply.
I am using both K64F and K66F. Accidently, I mentioned as K64F, but I have the issue with K66F. Kindly accept my apologies.
Initially, I was trying to disconnect and connect K66F USB to PC completely through firmware. But, as I mentioned in the previous question, it got into an infinite loop.
So I thought of disconnect and connect it physically. The method was working perfectly fine even when the transfer is happening. But I need to use Memory Card when USB is disconnected. So I need to De init card for other use such as file access and then Init memory Card when I need to use memory Card for USB MSC Functionality. (Note: Since other functionalities such as I2S also reinitializing the Card, I need to DeInit the memory card)
So, below are the steps that I am following in the program.
1. Disconnect the USB cable physically and then press SW3 button to disable USB and memory Card.
2. Press SW3 to re-enable memory Card and USB
3. Physically connect USB to enable USB communication.
(When I develop a PCB for the final project, I am planning to have a switch to disconnect D+ and D- to replicate this condition.)
But the problem is that when I connect back USB cable (as mentioned in step 3 above), I get an error message as in the attached image. When I click the message to fix, it gets fixed and USB MSC functionality works fine. But, I feel the user will not be happy to see those kinds of messages in a product.
It would be of great help if I can fix this issue or a method for a graceful way of disabling USB MSC Functionality, use Memory Card for other purpose and reenable USB MSC Functionality even when the transfer is interrupted.
Thanks,
Mahendran
NB: I'll study, understand and use "USB_DeviceCancel" this week, thanks for the information.
Hi Mahendrad,
I've been reviewing and running your code and I am not getting the same error message as you are. I have followed your steps as you described above, but I am not having a problem. The only message I get by the serial terminal is if I physically disconnect the SD card and try to reinitialize it while its not physically connected but that is ok.
Something that also may also help. When I uploaded your code to my MCUXpresso workspace it asked me to update SDK the example was using. So I'd highly recommend to make sure you are using the latest SDK along with the latest version of MCUXpresso. After doing this, verify if you are getting the same error message.
If you are still running into trouble check the USB_DeviceClassDeinit or USB_DeviceDeinit after using the USB_DeviceStop.
Please let me know any udpates you may have.
Best Regards,
Sabina
Hi Sabina,
I updated MCUXpresso to the latest version, used the latest SDK and tried the same code, which I sent you. The program worked perfectly fine without the Windows popping up a USB issue. Thanks very much for your efforts, this has taken me a big step forward.
As per your suggestion, I'll try using USB_DeviceClassDeinit or USB_DeviceDeinit after using the USB_DeviceStop and avoid using a physical USB switch.
Thanks very much Sabina.
best regards,
Mahendran
Excellent! I'm glad it is working now.
Best Regards,
Sabina