STM32U5 USB Mass Storage with some external QSPI or SPI flash - Page 1
Forum section: electronics / general

author: uer166
author_rank: Super Contributor
timestamp: September 07, 2024, 01:35:38 am
content: I have a need for a device where: FW updates, map updates, and log extraction could be done by connecting it to a PC, and appearing as a USB stick.The memory requirements are modest: maybe 8-64MB or so. Does anyone have any experience with such a system? Currently my FW architecture is a ISR superloop, and would love to keep it that way if possible (no FreeRTOS). Code simplicity/line count counts for a lot in this project.On the hardware side I could use a single SPI or maybe QSPI flash memory, but other suggestions are welcome.

author: peter-h
author_rank: Super Contributor
timestamp: September 07, 2024, 09:55:43 pm
content: I have a 32F417 project which has a 4MB Adesto SPI FLASH, 512 byte sectors, AT45DB321E, and you can have various device profiles on the USB client. In my case I have CDC (VCP port) and MSC (2MB FAT12 removable drive).USB MSC is what you want.ST Cube MX can generate most of the code for you for MSC profile, and you just need to write the interface to your FLASH. The above device is ideal because Windows wants to see 512 byte sectors.Still, a fair bit of work, but MSC is a lot easier than CDC.I posted various threads here about this, a few years ago. Sector write is 15ms, read is about 200us. If you dig them out, that will help you. Bits of code there, also how to implement a Busy state to the host.The entire ST USB code is interrupt driven so you don't need an RTOS for that.

author: uer166
author_rank: Super Contributor
timestamp: September 07, 2024, 10:59:23 pm
content: Any reason for FAT12? I'm reading about the support for it in latest Windows being iffy after Win10.

author: peter-h
author_rank: Super Contributor
timestamp: September 08, 2024, 05:42:15 am
content: If you do 2MB then it has to be FAT12. If you can do 4MB then you can do FAT16, IIRC.Not heard of Windows stopping support of FAT12on removable drives. On system partitions, sure. I did a dig around and all I can find is thishttps://answers.microsoft.com/en-us/windows/forum/all/a-change-in-fat12-file-system-from-xp-to-7/ad9e6c73-6561-e011-8dfc-68b599b31bf5which should not affect you.

author: Njk
author_rank: Frequent Contributor
timestamp: September 13, 2024, 01:28:03 pm
content: Done that many years ago with an 8-bit MCU and SPI flash. With no OS, of course. It must be easier now because of existing pre-built libraries.IIRC, USB MSC is a very short document, actually a stub. Basically, it states that the mass storage device shall be accessible with SCSI commands over USB. So you'll have to implement an SCSI server according to the T10 specification.

author: uer166
author_rank: Super Contributor
timestamp: October 15, 2024, 12:06:02 am
content: After some deliberation I realized that eMMC has internal management and seems like it's easy-ish to interface with. STM32U5 has a SDMMC interface so it should be doable, perhaps in 1 or 4-bit mode to reduce package size requirements.. The cost of a 8+ MB SPI flash seems to be similar to a 4GB eMMC, so the only downsides are power consumption and perhaps firmware complexity. The up-side is I never have to worry about running out of space, even for the lifetime of the device incl. logs and map files.If anyone has implemented what essentially amounts to a USB stick (that does a whole lot more, but that's beside the point), I'd love to hear thoughts/examples. My thought is:STM32<->eMMC in 4-bit mode, running FATFS and a USB MSC driver.

author: dmendesf
author_rank: Frequent Contributor
timestamp: October 15, 2024, 02:41:24 am
content: I did exactly that, twice: once with a F4 (everything worked first time), other with a F7 (lots of problems, DMA never worked fine both ways. Had to settle for software copying).CubeMX does almost everything:When you enable usb+msc you need 3 empty functions to fill. One returns drive size, one writes N 512 bytes sectors, and other reads N 512 sectors.When you enable SD-MMC you get 3 functions: init, read 512 byte sectors and Write 512 byte sectors.I think you can imagine how easy is to connect these two blocks. There are lots of details (how to determine disk size, write protection, etc, but they can be ignored / hardcoded to make things work fast).Watch this:

author: uer166
author_rank: Super Contributor
timestamp: October 16, 2024, 02:44:17 am
content: That video makes it look a little easier than it seems.ST has obsoleted the baked-in FATFS and their MSC drivers in favor of Azure crap (FileX and USBX are the drivers specifically). No problem, the legacy drivers are still supported per:https://community.st.com/t5/stm32-mcus/how-to-use-stmicroelectronics-classic-usb-device-middleware-with/ta-p/599274The major snag I hit is this code block per the example guide:Code:USBD_StatusTypeDef USBD_LL_Init(USBD_HandleTypeDef *pdev){pdev->pData = &hpcd_USB_DRD_FS;HAL_PCDEx_PMAConfig((PCD_HandleTypeDef*)pdev->pData , 0x00 , PCD_SNG_BUF, 0x40);HAL_PCDEx_PMAConfig((PCD_HandleTypeDef*)pdev->pData , 0x80 , PCD_SNG_BUF, 0x80);HAL_PCDEx_PMAConfig((PCD_HandleTypeDef*)pdev->pData , CDC_IN_EP , PCD_SNG_BUF, 0xC0);HAL_PCDEx_PMAConfig((PCD_HandleTypeDef*)pdev->pData , CDC_OUT_EP , PCD_SNG_BUF, 0x100);HAL_PCDEx_PMAConfig((PCD_HandleTypeDef*)pdev->pData , CDC_CMD_EP , PCD_SNG_BUF, 0x140);return USBD_OK;}Ignoring the fact that I have no clue WTF this is, the U5 HAL doesn't have this API at all, into the rabbit hole it goes

author: dmendesf
author_rank: Frequent Contributor
timestamp: October 16, 2024, 03:56:17 am
content: Notice LL libraries are usually independent of full HAL libraries. Will need to update CubeMX to see how much they fucked up this time...

author: SiliconWizard
author_rank: Super Contributor
timestamp: October 16, 2024, 05:39:01 am
content: You could consider TinyUSB as an alternative:https://github.com/hathach/tinyusbit does support the U5, MSC (and a lot of other targets, so makes porting easy) and while it's not perfect, it's probably a better option if you want to avoid STM libraries and tools.

author: Doctorandus_P
author_rank: Super Contributor
timestamp: October 16, 2024, 11:32:29 am
content: I once bought a beaglebone black and connected it to my (Linux) PC with an USB cable, That was enough to both power it and for connecting to it with a web browser, because Linux supports a network interface over USB by default.I also bought some "Black Pills" (STM32F411) from We-Act, and soldered on some SPI flash chips myself (It has an empty socket for it on the bottom) and I also put microPython on it. I got it to work and wrote some flashing led application, but I don't know why I would want to write python on a microcontroller and quickly lost interest. But from what I can remember, it enumerated as a dual device with both a mass storage device (you can drop files on it) and some kind of terminal emulator get gets you the REPL.

author: peter-h
author_rank: Super Contributor
timestamp: October 16, 2024, 02:03:49 pm
content: QuoteNo idea why since FatFS is excellent, just works, and wasn't hard to implement. In my project we disabled LFN and are running it with a 2MB FAT12 filesystem, via both internal access (FatFS) and external USB MSC (which is 100% interrupt driven).Azure is just more stuff to learn.A few things to watch for in ST's USB code e.g. they do a malloc when USB connects and a free when it disconnects, and since windoze puts its USB to sleep fairly often, you eventually blow up the heap via fragmentation. This was changed to a static buffer.

author: uer166
author_rank: Super Contributor
timestamp: October 18, 2024, 03:31:08 am
content: Quote from: uer166 on October 16, 2024, 02:44:17 amAnswering for others who may stumble with similar issue: I don't have a solution yet, however the reason for this is that ST has 2 USB IPs with minor differences (more than 2 if you include the HS-enabled one in H7):H5 has the USB DRD (Dual Role Device), while the U5 has the USB OTG (On-The-Go). The equivalent API to the above is HAL_PCDEx_SetRxFiFo() and HAL_PCDEx_SetTxFiFo, and example code that should work is actually for the STM32H7 which shares the OTG FS bits with the STM32U5.

author: uer166
author_rank: Super Contributor
timestamp: October 18, 2024, 03:33:29 am
content: Quote from: peter-h on October 16, 2024, 02:03:49 pmYeah my plan is to use FatFS still, but I expect that part to be easier since it's agnostic to HW. The USB MSC driver is likely more annoying due to this library change in the new versions of ST libs. While this seems to be a pain, I find overall using STM32Cube saves time in the long run for me once I get some of the quirks.