cancel
Showing results for 
Search instead for 
Did you mean: 

MX_USB_HOST_Init(); Issue on FreeRTOS CMSIS V1

AMIR-F-O
Associate II

Hi Everyone

I am starting to use FreeRTOS but faced an issue if there is someone to help. 

I configured USB_FS MSC. when I use MX_USB_HOST_Init(); the FreeRTOS stop and not working. but when I disable it, everything is fine.

I have used the USB_FS MSC in Super loop on mode without any issue.

1 ACCEPTED SOLUTION

Accepted Solutions
nouirakh
ST Employee

@AMIR-F-O 

The USBHProcess function is empty, it can cause problems. The USBHProcess function is responsible for handling the USB Host stack's state machine and processing USB events. If this function is not implemented correctly, the USB Host stack will not function properly, leading to issues such as the inability to detect or communicate with USB devices.
I think to ensure proper USB Host functionality, the USBHProcess function must call USBH_Process periodically. This allows the USB Host stack to handle events and manage device communication effectively.
The function should look something like this:

static void USBHProcess(void const * argument)
{
    /* USER CODE BEGIN USBHProcess */
    while (1)
    {
        // Process USB Host state machine
        USBH_Process(&hUsbHostFS);
        
        // Delay to allow other tasks to run
        osDelay(1);
    }
    /* USER CODE END USBHProcess */
}

 

View solution in original post

9 REPLIES 9
nouirakh
ST Employee

Hello @AMIR-F-O 

Most likely, it is an issue of stack size because USB operations can be stack-heavy due to the large amount of data processing.Can you change that stack size for the task that is calling MX_USB_HOST_Init(); and ensure that is sufficiently large.
Another common cause is a conflict between the USB host task and other system tasks. For example If a lower priority task holds a resource needed by a higher priority task, the system can halt, and you can check also the configurations interrupts in MX because Incorrect interrupt priority configuration can cause issues in a FreeRTOS environment so, be sure that the USB interrupt priority is compatible with the FreeRTOS settings.
Could you please verify these potential causes? If the issue persists, we can consider other problems that might be responsible for it.

Hello @nouirakh 

I increased the stack size and heap size as you can see. and also change the default task that MX_USB_HOST_Init(); is in there. but nothing happened.

AMIRFO_0-1720243149584.png

Is this the place to increase stack size that you mentioned?

ap size in 

nouirakh
ST Employee

Hello @AMIR-F-O 

You can change the stack sizes using STM32CubeMX by following these steps:

  • Go to the "Middleware" section in the left-hand pane.
  • Click on "FreeRTOS" to open the FreeRTOS configuration settings.
  • In the FreeRTOS configuration window, you will see a list of tasks.
  • For each task, you can set the stack size. Click on the task you want to modify. In the properties pane, find the "Stack Size" field. Enter the desired stack size for the task.

thankyou @nouirakh for reply

I Increased the stack size as you mentioned but randomly have trouble again.

AMIRFO_0-1720430116691.png

AMIRFO_1-1720430135982.png

 

beside that I change task priority and likewise I wrote random stuck in initializing. but somehow after 5 or more reset it pass to run program. after that I can't read or write USB like when I use USB_MSC in superloop mode. (I mean "usb_host.c" file)

I have some question for be sure that I have no bug on Cube IDE.

1. is there any visible task when generating code for USB_USE_OS?

2. Do I need function to run USB in tasks? 

3.this is code generated Initializing USB is it ok?

AMIRFO_2-1720430254693.png

and more to ask :)

 

nouirakh
ST Employee

Hello @AMIR-F-O 

Visible task when generating code for USB_USE_OS?
When you enable USB Host with FreeRTOS in STM32CubeMX, it should generate tasks that handle USB operations. These tasks are typically created in the usb_host.c
 file. You should see a task dedicated to USB processing, often named something like USBHProcess.
Running USB in Tasks?
Yes, you need to ensure that the USB Host processing is running within a FreeRTOS task. This is usually handled by the generated code, but you need to make sure that the task is properly created and scheduled.
Code generated Initializing USB is it ok?
Open the usb_host.c file and look for the task creation code. It should look something like this:

void MX_USB_HOST_Init(void)
{
    /* USER CODE BEGIN USB_HOST_Init_PreTreatment */

    /* USER CODE END USB_HOST_Init_PreTreatment */

    /* Init Host Library, Add Supported Class and Start the library. */
    USBH_Init(&hUsbHostFS, USBH_UserProcess, HOST_FS);
    USBH_RegisterClass(&hUsbHostFS, USBH_MSC_CLASS);
    USBH_Start(&hUsbHostFS);

    /* USER CODE BEGIN USB_HOST_Init_PostTreatment */

    /* USER CODE END USB_HOST_Init_PostTreatment */
}

static void USBHProcess(void const * argument)
{
    /* USER CODE BEGIN USBHProcess */
    while (1)
    {
        USBH_Process(&hUsbHostFS);
        osDelay(1);
    }
    /* USER CODE END USBHProcess */
}

 
I hope my answer has helped you. When your question is answered please close this topic by marking as Best the reply that answered you, it will help others find that answer faster. Thanks for your contribution.

Thankyou @nouirakh 

the Code Below show all script of my "usb_host.c" that code generator made it.

So this is the problem. code generator has bug for me. I am using CUBE IDE 1.15.1. what should I do for this issue? or should I add some code manually into it!

/* USER CODE BEGIN Header */
/* USER CODE END Header */

/* Includes ------------------------------------------------------------------*/

#include "usb_host.h"
#include "usbh_core.h"
#include "usbh_msc.h"

/* USER CODE BEGIN Includes */
/* USER CODE END Includes */

/* USER CODE BEGIN PV */
/* USER CODE END PV */

/* USER CODE BEGIN PFP */
/* USER CODE END PFP */

/* USB Host core handle declaration */
USBH_HandleTypeDef hUsbHostFS;
ApplicationTypeDef Appli_state = APPLICATION_IDLE;

/*
 * -- Insert your variables declaration here --
 */
/* USER CODE BEGIN 0 */
/* USER CODE END 0 */

/*
 * user callback declaration
 */
static void USBH_UserProcess(USBH_HandleTypeDef *phost, uint8_t id);

/*
 * -- Insert your external function declaration here --
 */
/* USER CODE BEGIN 1 */
/* USER CODE END 1 */

/**
  * Init USB host library, add supported class and start the library
  * @retval None
  */
void MX_USB_HOST_Init(void)
{
  /* USER CODE BEGIN USB_HOST_Init_PreTreatment */
  /* USER CODE END USB_HOST_Init_PreTreatment */

  /* Init host Library, add supported class and start the library. */
  if (USBH_Init(&hUsbHostFS, USBH_UserProcess, HOST_FS) != USBH_OK)
  {
    Error_Handler();
  }
  if (USBH_RegisterClass(&hUsbHostFS, USBH_MSC_CLASS) != USBH_OK)
  {
    Error_Handler();
  }
  if (USBH_Start(&hUsbHostFS) != USBH_OK)
  {
    Error_Handler();
  }
  /* USER CODE BEGIN USB_HOST_Init_PostTreatment */
  /* USER CODE END USB_HOST_Init_PostTreatment */
}

/*
 * user callback definition
 */
static void USBH_UserProcess  (USBH_HandleTypeDef *phost, uint8_t id)
{
  /* USER CODE BEGIN CALL_BACK_1 */
  /* USER CODE END CALL_BACK_1 */
}

/**
  * @}
  */

/**
  * @}
  */

 

nouirakh
ST Employee

@AMIR-F-O 

The USBHProcess function is empty, it can cause problems. The USBHProcess function is responsible for handling the USB Host stack's state machine and processing USB events. If this function is not implemented correctly, the USB Host stack will not function properly, leading to issues such as the inability to detect or communicate with USB devices.
I think to ensure proper USB Host functionality, the USBHProcess function must call USBH_Process periodically. This allows the USB Host stack to handle events and manage device communication effectively.
The function should look something like this:

static void USBHProcess(void const * argument)
{
    /* USER CODE BEGIN USBHProcess */
    while (1)
    {
        // Process USB Host state machine
        USBH_Process(&hUsbHostFS);
        
        // Delay to allow other tasks to run
        osDelay(1);
    }
    /* USER CODE END USBHProcess */
}

 

AMIR-F-O
Associate II

thankyou @nouirakh 

So is it cause by some wrong setting in CUBE IDE or is there any other cause? I am looking for deep solution for this. other thing for MX_USB_HOST_Init(); function can I put it in int main (void){} before initializing tasks?

Dear @nouirakh How about osThreadDef of USBH? should I make it my self or no need for that?

I just little bit confused!