2024-07-04 06:57 AM
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.
Solved! Go to Solution.
2024-07-08 03:53 AM
@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 */
}
2024-07-05 02:18 AM
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.
2024-07-05 10:22 PM
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.
Is this the place to increase stack size that you mentioned?
ap size in
2024-07-08 01:46 AM
Hello @AMIR-F-O
You can change the stack sizes using STM32CubeMX by following these steps:
2024-07-08 02:18 AM
thankyou @nouirakh for reply
I Increased the stack size as you mentioned but randomly have trouble again.
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?
and more to ask :)
2024-07-08 03:03 AM
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.
2024-07-08 03:32 AM
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 */
}
/**
* @}
*/
/**
* @}
*/
2024-07-08 03:53 AM
@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 */
}
2024-07-08 04:00 AM
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?
2024-07-08 05:04 AM
Dear @nouirakh How about osThreadDef of USBH? should I make it my self or no need for that?
I just little bit confused!