USB Mass Storage Device

This forum is dedicated to feedback, discussions about ongoing or future developments, ideas and suggestions regarding the ChibiOS projects are welcome. This forum is NOT for support.
Post Reply
bvernoux
Posts: 6
Joined: Sun Dec 01, 2013 3:56 pm
Been thanked: 1 time

Re: USB Mass Storage Device

Post by bvernoux »

I confirm USB CDC + Mass Storage with native ChibiOS support will be a must have.
Al B
Posts: 24
Joined: Thu Jun 09, 2016 4:01 am
Been thanked: 2 times

Re: USB Mass Storage Device

Post by Al B »

Hi again,

Thought I would provide an update.

I got ST's USB implementations for both MSC and CDC working on STM32F407 and STM32L476.

MSC was reasonably straight forward, since I am quite familiar with STs driver issues on uSD and eMMC (quite a number of bugs unfortunately).
They have 2 (well actually many more than that, but I am focusing on most recent ST HAL) MSC implementations depending on which processor and what speed of USB port (HS vs FS):
- one for FS that uses DMA on uSD accesses and uses CPU-FIFO accesses on USB side.
- one for HS, but seems like possibly works for F4 FS too (I have not tried either of these), that uses DMA on both uSD and USB stacks.
Since I am focused on L476, which only has FS port, I only debugged/fixed the FS version with the uSD-DMA and the USB-FIFO datapath.

In a previous post I INcorrectly stated that USB FS should have a target of >2MB/s based on FS throughput of 6MB/s, which was wishful thinking, as FS is limited (unfortunately) to 12Mbps or just 1.5MB/s. And with ST's datapath implementation using sequential multi-block reads, and max uSD throughput limited to 12MB/s (with 24MHz SDMMC_CK and 4 bit IO), the actual theoretical limit of the ST FS throughput is 1.33MB/s. With a minor tweak of MSC_MEDIA_PACKET size (to 32k, although 8 and 16k are all about the same performance) I was able to consistently get .98MB/s read transfer rate (157 seconds for 154.5MB file read from F4/L4 to PC and Mac) on FS USB. At this time I don't think it is worth the extra effort to see if using DMA on the USB access side can get a potential extra .35MB/s (also since ST seems to have partially carved out portions of the DMA on the L4 implementation, I suspect there may be subtle issues in the USB-DMA implementation).

On the CDC, that was a totally different matter. ST's implementation is not very good in my opinion and I could not get it to work properly. So I totally rewrote the CDC application including serialization aspects, and was able to get that working very well, to both PC and Mac and seems to run well at all speeds from 110 to 921600 bps on a wide variety of Terminal Emulators.

But now onto the major issue. Getting both CDC and MSC to work together as a Composite MSC-CDC is a Royal Pain!!! I commend anyone that had/has the patience/skills to get this combo working. Even with 2 very solid standalone classes, I spent considerable time fiddling around with different USB configuration parameters, different VID/PIDs, etc. etc. in an attempt to get drivers on PC/Mac to work for both of these simultaneously. But NO LUCK. There are people that say that they were able to get config's identical to ones I tried working on Mac or Linux, but I was not able to do so. ST's ST-link requires drivers to be installed to get their implementation working. So I also started into writing INF drivers to get the PC working, but have given up on that since they are quite tricky/tedious on their own and would also be deployment challenges as Users would need to load these drivers on their PC as well.

So ultimately, I have parked the Composite Class and have changed my application (using ST HAL not Chibios) to switch between one or the other of CDC/MSC Classes depending on what is required by the application. And will work with any PC, Mac. Linux box the User might have with no new driver installations...

Now I realize that my work on ST implementations isn't going to help Chibios implementations, but thought that these results may be useful.
So hopefully this info will be of some use to whoever tries to move Chibios MSC / Combo_MSC_CDC implementations forward.

Best wishes,
Al
User avatar
Giovanni
Site Admin
Posts: 14891
Joined: Wed May 27, 2009 8:48 am
Has thanked: 1202 times
Been thanked: 996 times

Re: USB Mass Storage Device

Post by Giovanni »

Hi,

You should look at the IAD demos in ChibiOS, IAD is what allows to create composite devices. For example: \testhal\STM32\STM32F4xx\USB_CDC_IAD.

Giovanni
Al B
Posts: 24
Joined: Thu Jun 09, 2016 4:01 am
Been thanked: 2 times

Re: USB Mass Storage Device

Post by Al B »

Hi Giovanni,

Thanks for the reference, I'll look at it more closely when I have time.
The usbcfg.c file looks to be much better done than most others I have seen elsewhere.

However using IAD's was one of the approaches I did try. The issue with Composite devices seems to be that the Host needs multiple drivers loaded simultaneously to handle the multiple Classes. When the classes are trivial or similar it seems that the Host (PC/Mac/Linux) has an easier time with this, possibly as is the case with the IAD CDC example you referred.

But with CDC and MSC, where the drivers are completely different or possibly conflicting, and at least one of them is not trivial, it seems this is where the wheels come off the cart... I understand the IAD concept, but haven't been able to get it working. It is quite possible (ie. quite likely ;) ) my Composite implementation still has bugs in it, but debugging these is somewhat difficult with the very limited tools that I have at my disposal. Further, I don't think that Windows implements a generic IAD handler so seems like custom drivers/INF might still be required (but not 100% sure of this).

Thanks,
Al
User avatar
Giovanni
Site Admin
Posts: 14891
Joined: Wed May 27, 2009 8:48 am
Has thanked: 1202 times
Been thanked: 996 times

Re: USB Mass Storage Device

Post by Giovanni »

A custom inf file is required, correct, the demo includes one.

Giovanni
User avatar
DeusExMachina
Posts: 223
Joined: Tue Apr 03, 2012 5:08 am
Has thanked: 3 times
Been thanked: 3 times

Re: USB Mass Storage Device

Post by DeusExMachina »

jayalfredprufrock
I'd definitely be grateful to see your latest code for MSC functionality. I'd need it to work reliably on both windows and OSX so if it already works well on windows and older macs, I'd be willing to put some time into getting it to work on El Capitan.

I'm really busy now, but next week I promise to share it. BTW it does not work in Linux too, but at least it is much easy to figure out what is wrong now...
User avatar
DeusExMachina
Posts: 223
Joined: Tue Apr 03, 2012 5:08 am
Has thanked: 3 times
Been thanked: 3 times

Re: USB Mass Storage Device

Post by DeusExMachina »

Well, I have no USB3300 PHY at home, so I had to port my code to USB FS. There are some bad news: For Windows this code works EXTREMELY SLOW (just opening the drive F: takes several minutes). Linux and Mac OS does not work at all. Enumeration works well, but something is wrong after that. I think that a low speed phenomenon for HS has the same cause.
Anyway, here is the code
https://github.com/Nomados/Chibios-16.x ... age-device
User avatar
DeusExMachina
Posts: 223
Joined: Tue Apr 03, 2012 5:08 am
Has thanked: 3 times
Been thanked: 3 times

Re: USB Mass Storage Device

Post by DeusExMachina »

Haha, changing the SD card solved the problem. It works pretty fast now!
User avatar
DeusExMachina
Posts: 223
Joined: Tue Apr 03, 2012 5:08 am
Has thanked: 3 times
Been thanked: 3 times

Re: USB Mass Storage Device

Post by DeusExMachina »

Well, I have collected some data. It works in Linux, but it is very unstable (problems with big data transfers). Still has no changes with Mac OS El Capitan.
Here are a dmesg logs for both systems
[spoil]<<Ubuntu Linux>>
[ 3124.967584] usb 2-2.2: new full-speed USB device number 15 using uhci_hcd
[ 3125.081257] usb 2-2.2: New USB device found, idVendor=0483, idProduct=babe
[ 3125.081260] usb 2-2.2: New USB device strings: Mfr=1, Product=2, SerialNumber=3
[ 3125.081262] usb 2-2.2: Product: MSD Example
[ 3125.081263] usb 2-2.2: Manufacturer: STMicroMSD
[ 3125.081264] usb 2-2.2: SerialNumber: 000000000001
[ 3125.087939] scsi10 : usb-storage 2-2.2:1.0
[ 3126.090380] scsi 10:0:0:0: Direct-Access ChibiOS Exmp MSD 0.1 PQ: 0 ANSI: 4
[ 3126.094794] sd 10:0:0:0: [sdc] 31291392 512-byte logical blocks: (16.0 GB/14.9 GiB)
[ 3126.094910] sd 10:0:0:0: Attached scsi generic sg2 type 0
[ 3126.095412] sd 10:0:0:0: [sdc] Write Protect is off
[ 3126.095414] sd 10:0:0:0: [sdc] Mode Sense: 03 00 00 00
[ 3126.096135] sd 10:0:0:0: [sdc] No Caching mode page present
[ 3126.096138] sd 10:0:0:0: [sdc] Assuming drive cache: write through
[ 3126.100777] sd 10:0:0:0: [sdc] No Caching mode page present
[ 3126.100780] sd 10:0:0:0: [sdc] Assuming drive cache: write through
[ 3126.107459] sdc: sdc1
[ 3126.112463] sd 10:0:0:0: [sdc] No Caching mode page present
[ 3126.112466] sd 10:0:0:0: [sdc] Assuming drive cache: write through
[ 3126.112468] sd 10:0:0:0: [sdc] Attached SCSI removable disk
[ 3265.569916] usb 2-2.2: reset full-speed USB device number 15 using uhci_hcd
[ 3266.238493] usb 2-2.2: reset full-speed USB device number 15 using uhci_hcd
[ 3312.187890] usb 2-2.2: reset full-speed USB device number 15 using uhci_hcd
[ 3343.372024] hub 2-2:1.0: cannot reset port 2 (err = -110)
[ 3344.372108] hub 2-2:1.0: cannot reset port 2 (err = -110)
[ 3345.372048] hub 2-2:1.0: cannot reset port 2 (err = -110)
[ 3346.371228] hub 2-2:1.0: cannot reset port 2 (err = -110)
[ 3347.371306] hub 2-2:1.0: cannot reset port 2 (err = -110)
[ 3347.371310] hub 2-2:1.0: Cannot enable port 2. Maybe the USB cable is bad?
[ 3348.371378] hub 2-2:1.0: cannot disable port 2 (err = -110)
[ 3350.370503] hub 2-2:1.0: cannot reset port 2 (err = -110)
[ 3351.370602] hub 2-2:1.0: cannot reset port 2 (err = -110)
[ 3352.370828] hub 2-2:1.0: cannot reset port 2 (err = -110)
[ 3373.364273] sd 10:0:0:0: Device offlined - not ready after error recovery
[ 3373.364287] sd 10:0:0:0: [sdc] Unhandled error code
[ 3373.364288] sd 10:0:0:0: [sdc]
[ 3373.364289] Result: hostbyte=DID_ABORT driverbyte=DRIVER_OK
[ 3373.364290] sd 10:0:0:0: [sdc] CDB:
[ 3373.364291] Write(10): 2a 00 00 01 cb 00 00 00 f0 00
[ 3373.364295] end_request: I/O error, dev sdc, sector 117504
[ 3373.364310] sd 10:0:0:0: rejecting I/O to offline device
[ 3373.364312] sd 10:0:0:0: killing request
[ 3373.364321] sd 10:0:0:0: rejecting I/O to offline device
[ 3373.364322] sd 10:0:0:0: [sdc] killing request
[ 3373.364331] sd 10:0:0:0: rejecting I/O to offline device
[ 3373.364339] sd 10:0:0:0: rejecting I/O to offline device
[ 3373.364345] sd 10:0:0:0: rejecting I/O to offline device
[ 3373.364351] sd 10:0:0:0: rejecting I/O to offline device
[ 3378.361288] hub 2-2:1.0: hub_port_status failed (err = -110)


<<MAC El Capitan>>

USBMSC Identifier (non-unique): 000000000001 0x483 0xbabe 0x100, 1
006082.142663 MSD Example@14200000: AppleUSBDevice::ResetDevice: <software attempt to RESET>
006082.217280 MSD Example@14200000: AppleUSBDevice::ResetDevice: <software attempt to RESET>
006082.808513 MSD Example@14200000: AppleUSBDevice::ResetDevice: <software attempt to RESET>
006082.886435 MSD Example@14200000: AppleUSBDevice::ResetDevice: <software attempt to RESET>
006128.809462 MSD Example@14200000: AppleUSBDevice::ResetDevice: <software attempt to RESET>
006128.886098 MSD Example@14200000: AppleUSBDevice::ResetDevice: <software attempt to RESET>
[/spoil]
User avatar
DeusExMachina
Posts: 223
Joined: Tue Apr 03, 2012 5:08 am
Has thanked: 3 times
Been thanked: 3 times

Re: USB Mass Storage Device

Post by DeusExMachina »

I made transfer rate measurements in Windows with FS/HS and results are the same:
WR: 230 KByte/s
RD: 960KByte/s
So, there is no merit to use High Speed PHY with this driver
Post Reply