Author Topic: STM32U5 USB Mass Storage with some external QSPI or SPI flash  (Read 2780 times)

0 Members and 1 Guest are viewing this topic.

Offline uer166Topic starter

  • Super Contributor
  • ***
  • Posts: 1030
  • Country: us
STM32U5 USB Mass Storage with some external QSPI or SPI flash
« on: September 07, 2024, 01:35:38 am »
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.
 

Offline peter-h

  • Super Contributor
  • ***
  • Posts: 4443
  • Country: gb
  • Doing electronics since the 1960s...
Re: STM32U5 USB Mass Storage with some external QSPI or SPI flash
« Reply #1 on: September 07, 2024, 09:55:43 pm »
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.

« Last Edit: September 07, 2024, 09:58:13 pm by peter-h »
Z80 Z180 Z280 Z8 S8 8031 8051 H8/300 H8/500 80x86 90S1200 32F417
 
The following users thanked this post: uer166

Offline uer166Topic starter

  • Super Contributor
  • ***
  • Posts: 1030
  • Country: us
Re: STM32U5 USB Mass Storage with some external QSPI or SPI flash
« Reply #2 on: September 07, 2024, 10:59:23 pm »
Any reason for FAT12? I'm reading about the support for it in latest Windows being iffy after Win10.

 

Offline peter-h

  • Super Contributor
  • ***
  • Posts: 4443
  • Country: gb
  • Doing electronics since the 1960s...
Re: STM32U5 USB Mass Storage with some external QSPI or SPI flash
« Reply #3 on: September 08, 2024, 05:42:15 am »
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 FAT12 on removable drives. On system partitions, sure. I did a dig around and all I can find is this
https://answers.microsoft.com/en-us/windows/forum/all/a-change-in-fat12-file-system-from-xp-to-7/ad9e6c73-6561-e011-8dfc-68b599b31bf5
which should not affect you.
« Last Edit: September 08, 2024, 06:43:25 am by peter-h »
Z80 Z180 Z280 Z8 S8 8031 8051 H8/300 H8/500 80x86 90S1200 32F417
 

Offline Njk

  • Frequent Contributor
  • **
  • Posts: 366
  • Country: ru
Re: STM32U5 USB Mass Storage with some external QSPI or SPI flash
« Reply #4 on: September 13, 2024, 01:28:03 pm »
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.
« Last Edit: September 13, 2024, 01:30:08 pm by Njk »
 

Offline uer166Topic starter

  • Super Contributor
  • ***
  • Posts: 1030
  • Country: us
Re: STM32U5 USB Mass Storage with some external QSPI or SPI flash
« Reply #5 on: October 15, 2024, 12:06:02 am »
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.
 

Offline dmendesf

  • Frequent Contributor
  • **
  • Posts: 342
  • Country: br
Re: STM32U5 USB Mass Storage with some external QSPI or SPI flash
« Reply #6 on: October 15, 2024, 02:41:24 am »
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:



 
The following users thanked this post: uer166

Offline uer166Topic starter

  • Super Contributor
  • ***
  • Posts: 1030
  • Country: us
Re: STM32U5 USB Mass Storage with some external QSPI or SPI flash
« Reply #7 on: October 16, 2024, 02:44:17 am »
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/599274


The major snag I hit is this code block per the example guide:
Code: [Select]
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  |O
 

Offline dmendesf

  • Frequent Contributor
  • **
  • Posts: 342
  • Country: br
Re: STM32U5 USB Mass Storage with some external QSPI or SPI flash
« Reply #8 on: October 16, 2024, 03:56:17 am »
Notice LL libraries are usually independent of full HAL libraries. Will need to update CubeMX to see how much they fucked up this time...

 

Offline SiliconWizard

  • Super Contributor
  • ***
  • Posts: 15956
  • Country: fr
Re: STM32U5 USB Mass Storage with some external QSPI or SPI flash
« Reply #9 on: October 16, 2024, 05:39:01 am »
You could consider TinyUSB as an alternative: https://github.com/hathach/tinyusb
it 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.
 
The following users thanked this post: uer166

Offline Doctorandus_P

  • Super Contributor
  • ***
  • Posts: 4010
  • Country: nl
Re: STM32U5 USB Mass Storage with some external QSPI or SPI flash
« Reply #10 on: October 16, 2024, 11:32:29 am »
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.
 

Offline peter-h

  • Super Contributor
  • ***
  • Posts: 4443
  • Country: gb
  • Doing electronics since the 1960s...
Re: STM32U5 USB Mass Storage with some external QSPI or SPI flash
« Reply #11 on: October 16, 2024, 02:03:49 pm »
Quote
ST has obsoleted the baked-in FATFS and their MSC drivers in favor of Azure crap (FileX and USBX are the drivers specifically)

No 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.
Z80 Z180 Z280 Z8 S8 8031 8051 H8/300 H8/500 80x86 90S1200 32F417
 
The following users thanked this post: uer166

Offline uer166Topic starter

  • Super Contributor
  • ***
  • Posts: 1030
  • Country: us
Re: STM32U5 USB Mass Storage with some external QSPI or SPI flash
« Reply #12 on: October 18, 2024, 03:31:08 am »
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/599274


The major snag I hit is this code block per the example guide:
Code: [Select]
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  |O

Answering 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.
 

Offline uer166Topic starter

  • Super Contributor
  • ***
  • Posts: 1030
  • Country: us
Re: STM32U5 USB Mass Storage with some external QSPI or SPI flash
« Reply #13 on: October 18, 2024, 03:33:29 am »
No 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).

Yeah 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.
 


Share me

Digg  Facebook  SlashDot  Delicious  Technorati  Twitter  Google  Yahoo
Smf