2024-10-01 12:12 AM
I am on an H573 and trying to rd/wr a file on a fat32-formatted USB memory stick using the mass storage controller.
To jump right into the problem (details further below), I am trying to understand what to do about this block when I am running in standalone mode. This snip comes from the example code which is in RTOS mode. I want standalone mode.
--Background--
As a starting point, I grabbed the Ux_MSC_Host example, and got that working with no difficulties on the H573 dev kit MB1677. That demo uses FileX, UsbX, ThreadX, and some power delivery peripherals.
Then I started some changes:
But...
When I tried to implement something meaningful in UX_DEVICE_INSERTION status, it turns out that the expression
media = &storage_media->ux_host_class_storage_media;
fails to compile because the type "FX_MEDIA" is undefined. I need FX_MEDIA so I can use functions like fx_file_open or fx_file_read.
Apart from the undefined type, the field itself is if-def'ed away within ux_host_class_storage.h:
typedef struct UX_HOST_CLASS_STORAGE_MEDIA_STRUCT
{
#if !defined(UX_HOST_CLASS_STORAGE_NO_FILEX)
UX_MEDIA ux_host_class_storage_media;
ULONG ux_host_class_storage_media_partition_start;
VOID *ux_host_class_storage_media_memory;
ULONG ux_host_class_storage_media_status;
ULONG ux_host_class_storage_media_lun;
ULONG ux_host_class_storage_media_sector_size;
#else
struct UX_HOST_CLASS_STORAGE_STRUCT
*ux_host_class_storage_media_storage;
ULONG ux_host_class_storage_media_number_sectors;
USHORT ux_host_class_storage_media_sector_size;
UCHAR ux_host_class_storage_media_lun;
UCHAR ux_host_class_storage_media_status;
#endif
} UX_HOST_CLASS_STORAGE_MEDIA;
and the #defines are set earlier:
#if defined(UX_HOST_STANDALONE) && !defined(UX_HOST_CLASS_STORAGE_NO_FILEX)
#define UX_HOST_CLASS_STORAGE_NO_FILEX
#endif
I thought maybe I could tweak this vendor file (slightly queasy about that). But that's when I found statements like this littered throughout the library:
I want FileX (and picked it in Cube), so this #define is confusing. I guess the question is: what is the proper configuration of #defines to bring all these pieces together properly? Or is standalone host msc not a supported feature? Are there lib tweaks I can make that will fake it?
I have found these informative - but they vary:
Lastly, I attached the ioc and a pdf with recap/screenshots of what I changed.
2024-10-04 9:44 AM
Hi NotMark
This post has been escalated to the ST Online Support Team for additional assistance. We'll contact you directly.
Kind Regards
Joe
STMicro Support
2024-11-20 1:52 AM
HI Joe,NotMark,
Can you tell me how this problem was finally solved? I also encountered this problem. Thank you.
2024-11-20 7:33 AM
Hi Alon99,
ST automatically opened a support ticket and gave some additional help (appreciate their proactiveness!).
It sounded like there is a way to do this that involves using Cube for initial generation and then removing its RTOS manually. Exactly what that involves, or how much maintenance it would cause on subsequent Cube regenerations is unknown.
I will copy details from the ticket -- maybe those can help you. However, for my use case, I ended up using the ST-provided USB host library instead of USBX. This alternate approach had a lot of examples and was very straightforward. I have it on my low priority list to revisit USBX someday, but it is not my priority right now.
Anyway, here is the ST thread:
-----------------------------------------------------------------------
(Employee)
October 10, 2024 at 9:29 PM
Are you strapped to using USBX for standalone?
If you are wondering about the implementation of USB in standalone, then I can refer you to this example:
and this one:
STM32Cube_FW_H7_V1.6.0\Projects\STM32H747IDISCO\Applications\USB_Device\HID_Standalone
(Customer)
October 11, 2024 at 7:27 AM
USBX standalone works, that isn't the problem directly. The issue is using USBX standalone when FILEX is also enabled. I believe the current implementation is such that USBX can be standalone (without files), and FILEX can be standalone (without USBX) but when used together then THREADX is required. At least that is what I picked up from the #defines in the header files -- it appears the defines mutually exclude this combination.
USBX standalone: yes because of examples such as the CDC you provided below
FILEX standalone: yes because of examples such as SDcard that can open files
USBX standalone + FILEX standalone: ???
I liked the USBX library API better than other alternatives I evaluated, but the specific usecase is integrating a usb stack into a preexisting code base, and that code already has a preexisting rtos, and for various reasons is not able to swap. So threadx cannot be included as part of the usb stack.
To workaround this issue, I changed directions, and integrated ST's native usb library (https://github.com/STMicroelectronics/stm32-mw-usb-host) and fatfs instead. I got those working in standalone.
If you are able to propose a way to disable THREADX, I will reevaluate the decision and look at USBX + FILEX again. Otherwise my current understanding is that the ecosystem is not technically viable given the constaints we are selecting. I'd be very happy to be proven wrong though! :grinning_face:
(Employee)
October 14, 2024 at 3:39 PM
I asked my colleague who is more experienced with the usbx and filex. It seems like he ran into runtime issues when trying to enable standalone USBX + FileX at the same time on the U0. I think the issue is mainly related to the CubeMX generator. Can you try generating the cubemx project with threadx enabled and then simply disable the threadx in the source code? Also, the legacy USB + FatFs should have no issues though.
We are in the process of fixing this, however, it will be a 1 year until that happens. I will double check on what's the status internally.
He also said, this workaround does not work in composite class, only if you use in single usb class.
2024-11-24 8:40 PM
Hi NotMark,
I tried for a few days and finally failed, it seems that Standalone USBX and FileX are not feasible at the moment, thanks for your reply.
2025-06-16 1:03 AM
Hi NotMark
After trying to get the USBX + FileX to work without THREADX, I will give up...
It looks as the standalone version of USBX is not completely implemented and the last update (v6.4.0) was in 2022... So, not the right configuration yet. Wondering if it will ever be implemented properly...
Did you get the legacy solution with USB + FatFS to work? Could you please share the details on how you did?
Many thanks in advance!
Best Regards
Gerardo
2025-06-16 12:52 PM
Yes, I did get that working.
host library (unmodified)
https://github.com/STMicroelectronics/stm32_mw_usb_host/blob/master/
fatfs library (unmodified)
https://github.com/STMicroelectronics/stm32_mw_fatfs/blob/master/
stm32mxcube init
for an STM32U5(95)...
stm32mxcube - Connectivity -> USB_OTG_HS:
GPIO: D+/- to PA12/PA11
MODE: Internal HS PHY, Host only
Host Channels: 16
Speed: High 480
IP DMA: Disabled
Physical: Internal HS PHY
VBUS: Disabled
Signal SoF: Disabled
Ref Clock: 32Mhz
stm32mxcube - NVIC:
USB OTG HS Global Interrupt enabled, prio 5, sub prio 0
stm32mxcube - CLOCK:
I have a 16Mhz external HSE
PLL1Source Mux: HSE = 16
PLL1M: div/1 = 16
PLL1m*n: mult*10 = 160
PLL1M/p: div/5 = 32
This results in PLL1P being 32Mhz
Selected OTG-HS-Clock-Mux as PLL1P
Thus USB kernel clocked at 32Mhz, to match the ref clock selected in the periph above
Then I went looking for some example code on how to stitch these together.
Specifically, in the example filters, I picked...
MCU/MPU: stm32f
Middleware: USB_Host (NOT the USBXHOST)
I extracted a number of these examples and did a diff to see how they varied.
I ended up swiping (and modifying):
usbh_conf.c
usb_host.c/h
- stm32u5xx_hal_msp
(note the comment & c.f. that url if you want more details)
void HAL_MspInit(void)
{
/* USER CODE BEGIN MspInit 0 */
//from https://community.st.com/t5/stm32-mcus-products/stm32u5ax-usb-corereset-timeout/m-p/578564#M219799
//cube isn't enabling one of the clocks that usb needs
//without this enabled, then USB_CoreReset() times out.
__HAL_RCC_SYSCFG_CLK_ENABLE();
/* USER CODE END MspInit 0 */
__HAL_RCC_PWR_CLK_ENABLE();
/* System interrupt init*/
/* PendSV_IRQn interrupt configuration */
HAL_NVIC_SetPriority(PendSV_IRQn, 15, 0);
/* USER CODE BEGIN MspInit 1 */
/* USER CODE END MspInit 1 */
}
updated defines in usbh_conf.h
#define HOST_HS 1
#define HOST_FS 0
in usbh_diskio_config.h, usbh_diskio.c/h
file_operations.c/h and explorer.c
the ISR was already set up by Cube
To summarize, the resulting structure, including the libs is this:
the first and last folders are where I touched anything; the two middle folders (fatfs & usbhost), are unmodified. The cube generated folder sits parallel to these four folders but I didn't include that here because it contains some proprietary folder names.
AppLogic is somewhat chip agnostic, I've tried it on three flavors of chips; whereas the Port folder (which contains some headers) is what has to be tweaked for each micro. -- i.e., if you're highspeed, fullspeed, the name of the typedef Cube made for you, and so forth.