cancel
Showing results for 
Search instead for 
Did you mean: 

USBX MSC SD card + FILEX SD card problem

ABasi.2
Senior

Hi, i'm trying to make a firmware that can read an SD card with filex but that can also connect that SD card to USBX MSC device (no simultaney).

 

i'm able to do the two functionality in two separate firmware, but the problem is to merge.

 

for now is sufficient that the card is read at start up and next i can see the SD card with USBX MSC device

when i can do that the firmware shall evolve to connect the SD card to USBX MSC and after a modification by PC disconnect the SD card the time necessary to see if something is changed and do stuff

 

unfortunately the first step is totally ok, at startUp i can read the SDcard with  FILEX but.. after that USBX wont work

 

i'm using a threadX flag to indicate at the USB that is possible to use the SD card, there is some example about myu goal?

i will attach to this message the relevant files..

 

thank you in advance for any hint.

 

best reguards

 

9 REPLIES 9
T_Hamdi
ST Employee

Hello @ABasi.2

Yes, it is possible to combine both functionalities  SD card access using FileX and exposing the SD card as a USB MSC device with USBX .

The key is to switch between the two modes, ensuring that each mode cleanly closes access to the SD card before switching. For example, you can use a button or a signal to trigger this mode change.

You can refer to the following STM32CubeH5 examples on GitHub:

These two examples can be integrated into the same firmware. Then by implementing a switch (button or signal) according to your use case, you can toggle between the two modes. At each switch, make sure to properly manage closing and reopening the SD card access to avoid conflicts.

 

To give better visibility on the answered topics, please click on "Accept as Solution" on the reply which solved your issue or answered your question.
Hamdi Teyeb

Hello T_Hamdi and thank you for your link

BUT , as i said in the message i am already able to make the two separate firmware

the problem is in the switch metod, if you see my file you see that in this case i'm opening the media, do stff, closing the media in the app_filex.c file , after that i set a flag

 

only after the flag setted in the USBX part the firmware is using the SD card (actually i start USB after the flag), so i'm expecting there is no conflict at all.. BUT when i plug in the USB Cable someting goes wrong

 

it seems is not possible to opening the sd media in the USBX file.. and i don't understand why..

 

thank you for any advice

 

best reguards

T_Hamdi
ST Employee

Hello @ABasi.2 

Thank you for your explanation. To better understand the issue and provide you with accurate advice, could you please share the main.c file where the application handles the switching between the USBx MSC and Filex ?

 

To give better visibility on the answered topics, please click on "Accept as Solution" on the reply which solved your issue or answered your question.
Hamdi Teyeb

Hello T_Hamdi

 

i don't have a switch in the main.c file

 

the "switch" is inside app_filex.c

 

the last line is 

tx_event_flags_set(&basXflags,0x02, TX_OR);

here i set a flag

 

and after that i have the sdio_disk media already closed (in this example i don't need to reopen a file)

 

in app_usbx_device.c 

 

there is :

static VOID app_ux_device_thread_entry(ULONG thread_input)
{
  /* USER CODE BEGIN app_ux_device_thread_entry */
  TX_PARAMETER_NOT_USED(thread_input);

  ULONG flags;

  // Initialization of USB device
  MX_USB_PCD_Init();

  // PMA from 0x00 to 0x3F -> BTABLE
  HAL_PCDEx_PMAConfig(&hpcd_USB_DRD_FS, 0x00, PCD_SNG_BUF, 0x40); // endpoint 0 OUT -> CMD OUT
  HAL_PCDEx_PMAConfig(&hpcd_USB_DRD_FS, 0x80, PCD_SNG_BUF, 0x80); // endpoint 0 IN -> CMD IN
  HAL_PCDEx_PMAConfig(&hpcd_USB_DRD_FS, 0x81, PCD_SNG_BUF, 0xC0); // endpoint 1 IN -> MSC IN
  HAL_PCDEx_PMAConfig(&hpcd_USB_DRD_FS, 0x01, PCD_SNG_BUF, 0x100); // endpoint 1 OUT -> MSC OUT

  _ux_dcd_stm32_initialize((ULONG)USB_DRD_FS, (ULONG)&hpcd_USB_DRD_FS);


  tx_event_flags_get(&basXflags,0x02, TX_OR,&flags, TX_WAIT_FOREVER);



  HAL_PCD_Start(&hpcd_USB_DRD_FS); // Start device USB

 where i wayt forever thiat flag to start the usb device

 

after that in ux_device_msc. this function are called:

 

ULONG USBD_STORAGE_GetMediaLastLba(VOID)
{
  ULONG LastLba = 0U;

  /* USER CODE BEGIN USBD_STORAGE_GetMediaLastLba */

  if(sdio_disk.fx_media_id != FX_MEDIA_ID)
  {
    fx_media_open(&sdio_disk, FX_SD_VOLUME_NAME, fx_stm32_sd_driver, (VOID *)FX_NULL, (VOID *) fx_sd_media_memory, sizeof(fx_sd_media_memory));
  }
  //if(sdio_disk.fx_media_id == FX_MEDIA_ID)
  {
    LastLba = sdio_disk.fx_media_total_sectors - 1;
  }
  /* USER CODE END USBD_STORAGE_GetMediaLastLba */

  return LastLba;
}

/**
  * @brief  USBD_STORAGE_GetMediaBlocklength
  *         Get Media block length.
  * @param  none.
  * @retval block length.
  */
ULONG USBD_STORAGE_GetMediaBlocklength(VOID)
{
  ULONG MediaBlockLen = 0U;

  /* USER CODE BEGIN USBD_STORAGE_GetMediaBlocklength */
  if(sdio_disk.fx_media_id != FX_MEDIA_ID)
  {
    fx_media_open(&sdio_disk, FX_SD_VOLUME_NAME, fx_stm32_sd_driver, (VOID *)FX_NULL, (VOID *) fx_sd_media_memory, sizeof(fx_sd_media_memory));
  }

  //if(sdio_disk.fx_media_id == FX_MEDIA_ID)
  {
    MediaBlockLen = sdio_disk.fx_media_bytes_per_sector;
  }
  /* USER CODE END USBD_STORAGE_GetMediaBlocklength */

  return MediaBlockLen;
}

 but

fx_media_open(&sdio_disk, FX_SD_VOLUME_NAME, fx_stm32_sd_driver, (VOID *)FX_NULL, (VOID *) fx_sd_media_memory, sizeof(fx_sd_media_memory));

fails!

 

and i don't understand why.. the media is closed.. why i can open it in this file?

 

thank you

 

best reguards

Hello

 

i think i have found the real problem causing the issue

 

i'm trying to use the sdio_disk FX_MEDIA opened in app_fileX.c in ux_device_msc.c declaring it 

extern FX_MEDIA sdio_disk;

 

but 

 

in app_fileX.c  aftern the fx_media_open the struct sdio_disk if full of data

 

in ux_device_msc.c is empty!

 

i cannot understand why, using the debugger the flow is correct with the flag i'm using but the FX_MEDIA sdio_disk seems to became empty..

 

i have created a small new project with only the fundamendals, i attach here  the relevant files

 

thank you if you have an advice

 

best reguards

Hello @T_Hamdi 

i found the real problem but not the solution

 

the proble is in the two function 

ULONG USBD_STORAGE_GetMediaLastLba(VOID) 

ULONG USBD_STORAGE_GetMediaBlocklength(VOID)

 

these two function are called way before  i was thinking, they are called at initialization of the USBX

 

i'm trying to use a flag to wait until the fileX have finalize to use the sd card BUT

the flag have a strange behaviour, i have created the flag in the 

UINT MX_FileX_Init(VOID *memory_ptr)
{
  UINT ret = FX_SUCCESS;
  TX_BYTE_POOL *byte_pool = (TX_BYTE_POOL*)memory_ptr;
  VOID *pointer;

/* USER CODE BEGIN MX_FileX_MEM_POOL */

/* USER CODE END MX_FileX_MEM_POOL */

/* USER CODE BEGIN 0 */
  tx_event_flags_create(&RAMflags,"RAM flags");
/* USER CODE END 0 */

 

and a have a tx_event_flags_set(&RAMflags,0x01, TX_OR);

 

at the end of void fx_app_thread_entry(ULONG thread_input)

 

but 

the debug execution arrive correctly first at the flag creation but when arrive at 

 

ULONG USBD_STORAGE_GetMediaLastLba(VOID)
{
  ULONG LastLba = 0U;


  /* USER CODE BEGIN USBD_STORAGE_GetMediaLastLba */
  
  ULONG flags;
  tx_event_flags_get(&RAMflags,0x01, TX_OR,&flags, TX_WAIT_FOREVER);
  {
    LastLba = sram_disk.fx_media_total_sectors - 1;
  }
  /* USER CODE END USBD_STORAGE_GetMediaLastLba */

  return LastLba;
}

 

doesn't wait!

 

why that? where is my error? the tx_event_flags_get don't have to wayt for the tx_event_flags_set(&RAMflags,0x01, TX_OR);???

 

any advice is appreciated

 

best reguards

T_Hamdi
ST Employee

hello @ABasi.2 

To avoid the unexpected behavior caused by USBX MSC functions (USBD_STORAGE_GetMediaLastLba and USBD_STORAGE_GetMediaBlocklength) being called before FileX initialization is complete, I propose modifying the initialization sequence to start USBX MSC only after FileX has fully completed its initialization.

Specifically, this means:

  • Perform all FileX initialization steps (media open, close, ...) in a dedicated thread or task.
  • Once FileX initialization is fully done and the media is ready, signal this event using a ThreadX event flag, 
  • Only after receiving this signal, start the USBX MSC thread or trigger USBX MSC initialization.

This approach ensures that USBX functions querying the media will only be called after FileX has successfully opened and prepared the SD card, preventing premature calls that cause errors or unexpected non-blocking behavior.

To give better visibility on the answered topics, please click on "Accept as Solution" on the reply which solved your issue or answered your question.
Hamdi Teyeb

thank you for your response @T_Hamdi 

can you provide a little example of that? 

as i said in my last post the "flag approach" seems have some problem

 

a little example code is really appreciated

 

best reguards

@T_Hamdi 

 

another test .. another understanding

 

the problem to use tx_event_flags_get in the function USBD_STORAGE_GetMediaLastLba is that that function isn't is a thread! so is not possible to wait

 

so i think the only thing i can do is to use a "normal flag"

 

but in order to do that i need to understand whitch data i can use in the variable of  *media_status  in the USBD_STORAGE_Status to tell the firmware that the media has changed and the USBD_STORAGE_GetMediaLastLba have to be recalled

 

there is a status like "media changed"?

 

also what is the meaning of the return ULONG of the  USBD_STORAGE_GetMediaLastLba(VOID)?