Need help with USB device MSC implementation

Post here first, or if you can't find a relevant section!
Post Reply
matixan
Posts: 4
Joined: Sun May 23, 2021 5:57 pm
Location: Poland
Contact:

Need help with USB device MSC implementation

Post by matixan »

Hello! I am new to this forum. There is my problem:

I am a beginner in STM and need some help.
I want to use SPI flash ic to store various data(fonts, graphics for LCD and config files) that the microcontroller can read and the user will have access to after connecting the board via usb. Like usb card reader. I've already seen examples from STMCubeIDE but I guess I can't use these files in an arduino environment anyway. Unfortunately, the source code of STM32duino also doesn't give me any examples.
I will be grateful for any examples or links from which I can use something. So far I have used arduino and I did not have to use the advanced features that STM32f4 can offer.
At this moment I figured out how to work with usb serial and its working fine for debugging.
My setup:
Custom board using STM32f401RBT6 (my github project link: https://github.com/matixan/STMBOX-ecig-battery )
Platformio with latest STM32duino core
Transfer speed ​​doesn't really matter to me, but ofc faster=better

Secondly, If i managed how to do this, which library for SPI flash(W25Q64) should I use? Does it matter?
ag123
Posts: 1657
Joined: Thu Dec 19, 2019 5:30 am
Answers: 24

Re: Need help with USB device MSC implementation

Post by ag123 »

dealing with USB MSC isn't a 'for beginners' thing.
there are a bunch of specs you can find on usb.org, those are basic reading materials

https://www.usb.org/document-library/ma ... verview-14
https://www.usb.org/document-library/ma ... lk-only-10
https://www.usb.org/document-library/ma ... ication-10
https://www.usb.org/document-library/ma ... ication-10
https://www.usb.org/document-library/ma ... ication-11

accordingly in addition to the above usb mass storage is really scsi over usb mass storage (e.g. bulk only), the ufi specs has some of that.

then u'd need to get familiar with stm32 usb drivers as well, the starting point normally is to start with an 'easier' protocol such as cdc (usb-serial)

and first u'd need to blink a led with the stm32duino core. if you have not reached here, there is a long distance to go before reaching usb msc.

oh and last but not least these FAQs and read this 1st are the normal place to start
viewtopic.php?f=2&t=301
viewtopic.php?f=2&t=3
matixan
Posts: 4
Joined: Sun May 23, 2021 5:57 pm
Location: Poland
Contact:

Re: Need help with USB device MSC implementation

Post by matixan »

Well, I have this board working fine(for more than month) such as reading external ADC and using LCD. So I'm not such a newbie.
Don't get me wrong but i don't want to write low level usb drivers myself. It would be too difficult for me and time consuming.
After some more research, I found that Marlin firmware(for 3d printers) is using something similar i want.
I can use Marlin code but its multi-platform and a lot links to file and weird definitions. It's more than a bit complicated.
It is also strange that there are no other projects on the web that use MSC. It is so little popular or simply nobody shares it? I ignore the fact that in CubeIDE it would be easier. It's ugly and has display problems on my computer
fredbox
Posts: 125
Joined: Thu Dec 19, 2019 3:05 am
Answers: 2

Re: Need help with USB device MSC implementation

Post by fredbox »

Secondly, If i managed how to do this, which library for SPI flash(W25Q64) should I use? Does it matter?
The W25Q64 has a thorough data sheet showing the commands.

I found a library for a W25Q16 at https://github.com/derekevans/W25Q16. The commands in this library match the W25Q64 data sheet, so I don't think you would have much trouble adapting it for your project.
ag123
Posts: 1657
Joined: Thu Dec 19, 2019 5:30 am
Answers: 24

Re: Need help with USB device MSC implementation

Post by ag123 »

ok here is a hint, and mainly only the official core. you can try the cube MX generated codes for MSC. That codes can be ported into stm32duino core and integrated with the rest of the core. then when you are doing that do not select USB serial so that there would not be a conflict of 2 set of usb codes.
to get an idea of how to integrate that you can look at how the core integrates USB Serial which is basically the cube MX USB CDC Serial usb device driver codes. just that you are going to replace the whole of that with USB MSC from Cube MX.

u'd also need to dig into the USB MSC codes to figure out where to patch the interfaces that read / write the SPI flash.
for SPI flash like mention prior by others there are specs and libraries for it. the 'hard' part is to link USB MSC and the reading /writing codes for SPI flash.
another rather tricky thing is that when USB MSC is accessing flash, other mcu codes should not 'touch' it to prevent corrupting data.
if you want to try to read those data off SPI flash from within the mcu, you would need in additional codes from SD-FAT that read the FAT file system, otherwise the spi flash would simply be like a 'block' device to usb MSC, the details of how the files / folders etc are organized are normally not handled by USB MSC but rather by the host OS handling FAT filesystem.

USB MSC itself the specs are confusing at best. I kind of figured out that USB MSC is really SCSI over USB MSC. i.e. you need to process SCSI commands done over USB MSC bulk-only-transport. it may help to do more web searches e.g. from microsoft's site etc.

and i'd guess you may need some sort of usb sniffer to troubleshoot things. there are various (commercial) implementations out there.
for 'free' ones i think wireshark can do some of it but mainly in linux using the kernel's usbmon interface. i'm not sure if the same would work in windows.
matixan
Posts: 4
Joined: Sun May 23, 2021 5:57 pm
Location: Poland
Contact:

Re: Need help with USB device MSC implementation

Post by matixan »

After playing for a while I was able to create MSC RAMdisk in CubeIDE. Windows found the disk without any problems, asked for formatting and after that I was able to copy files from the PC to the MCU ram and vice versa. From this point, using flash memory will be easy. But there is another problem. When I copied files responsible for USB support etc. to vs code The program won't compile. I think I properly done including files. Also I'm using MX_USB_DEVICE_Init(); in my setup
Erros:

Code: Select all

Building in release mode
Linking .pio\build\genericSTM32F401RB\firmware.elf
c:/users/matix/.platformio/packages/toolchain-gccarmnoneeabi/bin/../lib/gcc/arm-none-eabi/9.2.1/../../../../arm-none-eabi/bin/ld.exe: .pio/build/genericSTM32F401RB/src/main.cpp.o: in function `MX_USB_DEVICE_Init':
main.cpp:(.text.MX_USB_DEVICE_Init+0x2c): undefined reference to `USBD_MSC_RegisterStorage'
c:/users/matix/.platformio/packages/toolchain-gccarmnoneeabi/bin/../lib/gcc/arm-none-eabi/9.2.1/../../../../arm-none-eabi/bin/ld.exe: main.cpp:(.text.MX_USB_DEVICE_Init+0x50): undefined reference to `FS_Desc'
c:/users/matix/.platformio/packages/toolchain-gccarmnoneeabi/bin/../lib/gcc/arm-none-eabi/9.2.1/../../../../arm-none-eabi/bin/ld.exe: main.cpp:(.text.MX_USB_DEVICE_Init+0x5c): undefined reference to `USBD_MSC'
c:/users/matix/.platformio/packages/toolchain-gccarmnoneeabi/bin/../lib/gcc/arm-none-eabi/9.2.1/../../../../arm-none-eabi/bin/ld.exe: main.cpp:(.text.MX_USB_DEVICE_Init+0x60): undefined reference to `USBD_Storage_Interface_fops_FS'
collect2.exe: error: ld returned 1 exit status
*** [.pio\build\genericSTM32F401RB\firmware.elf] Error 1
ag123
Posts: 1657
Joined: Thu Dec 19, 2019 5:30 am
Answers: 24

Re: Need help with USB device MSC implementation

Post by ag123 »

i'd guess it'd take some effort to figure out the 'usb driver stack' including the MSC handling part of that. you would need to transplant the whole of it.
for grafting something this 'large' errors are inevitable due to dependencies.
the tactic is normally to fix the dependencies error one after another

as for myself i used makefiles and eclipse
viewtopic.php?f=41&t=183
so i'd figure out the dependencies one set at a time and rebuild and check errors.

my guess is using competent platforms like platform io, vscode etc would help similarly. possibly simpler than the makefile approach
but you may like to take a look at my makefiles to see how i graft the includes and defines.

i think platform io and vs code use arduino-cli instead. i'm not too familiar with how that'd work.
but i'd guess one way is to copy those codes into the sketch project directory and try to add the includes, source files, replicating the structure
but if that fails you may need to try to dig into maybe boards.txt and platform.txt and possibly graft that directly in the core source files
Last edited by ag123 on Mon May 24, 2021 4:07 pm, edited 1 time in total.
User avatar
fpiSTM
Posts: 1745
Joined: Wed Dec 11, 2019 7:11 pm
Answers: 91
Location: Le Mans
Contact:

Re: Need help with USB device MSC implementation

Post by fpiSTM »

FYI
A PR already exists for this:
https://github.com/stm32duino/Arduino_C ... /pull/1088

An other one manages also Host + MSC:
https://github.com/stm32duino/Arduino_C ... pull/11961

they are complementary
matixan
Posts: 4
Joined: Sun May 23, 2021 5:57 pm
Location: Poland
Contact:

Re: Need help with USB device MSC implementation

Post by matixan »

I just saw it. So I will wait until the PR is ready and stable. It's a bit dead for now, but I'm in no hurry to do so.
At this moment i got various errors due to conflict in CDC and MSC
Post Reply

Return to “General discussion”