cancel
Showing results for 
Search instead for 
Did you mean: 

How do I add FreeRTOS threads without CubeMX deleting them?

RLein.1
Associate II

I created a project for an STM32H753 MCU and have added FreeRTOS via CubeMX called from CubeIDE. CubeMX generates the following code in main.c:

 /* Init scheduler */

 osKernelInitialize(); /* Call init function for freertos objects (in freertos.c) */

 MX_FREERTOS_Init();

 /* Start scheduler */

 osKernelStart();

Based on some research, I assume that my new thread declarations need to occur between those two sections like this:

 /* Init scheduler */

 osKernelInitialize(); /* Call init function for freertos objects (in freertos.c) */

 MX_FREERTOS_Init();

 osThreadNew( &thread_mainLoop, NULL, NULL );

 osThreadNew( &thread_debugPort, NULL, &thread1_attr );

 /* Start scheduler */

 osKernelStart();

Unfortunately, every time I make a change via CubeMX, my declarations are deleted. I've tried wrapping my code with USER CODE BEGIN/END 2.5 and USER CODE BEGIN/END RTOS_THREADS (copied from a project created using TouchGFX Designer) comments, but that doesn't help.

How do I get around this annoyance?

Thanks

1 ACCEPTED SOLUTION

Accepted Solutions
KnarfB
Principal III

I never got MX_FREERTOS_Init(); but the above init Sequence instead. How does the implementation of MX_FREERTOS_Init() look like? Maybe you find the user code sections there

hth

KnarfB

View solution in original post

7 REPLIES 7
Andrew Neil
Evangelist III

CubeMX should provide those "wrappers" in the code it generates - you need to use the ones that CubeMX generates.

RLein.1
Associate II

Thanks for the reply. As shown above, CubeMX did not created wrappers for user code between those sections in my project. My understanding is that my calls to create new threads must be after the os initialization and the os start.

KnarfB
Principal III

How exacttly did you create the project? STM32CubeMX 6.6.1. for STM32CubeIDE generates all USER blocks and respects all edits as usual:

osKernelInitialize();
 
  /* USER CODE BEGIN RTOS_MUTEX */
  /* add mutexes, ... */
  /* USER CODE END RTOS_MUTEX */
 
  /* USER CODE BEGIN RTOS_SEMAPHORES */
  /* add semaphores, ... */
  /* USER CODE END RTOS_SEMAPHORES */
 
  /* USER CODE BEGIN RTOS_TIMERS */
  /* start timers, add new ones, ... */
  /* USER CODE END RTOS_TIMERS */
 
  /* USER CODE BEGIN RTOS_QUEUES */
  /* add queues, ... */
  /* USER CODE END RTOS_QUEUES */
 
  /* Create the thread(s) */
  /* creation of defaultTask */
  defaultTaskHandle = osThreadNew(StartDefaultTask, NULL, &defaultTask_attributes);
 
  /* creation of myTask02 */
  myTask02Handle = osThreadNew(StartTask02, NULL, &myTask02_attributes);
 
  /* USER CODE BEGIN RTOS_THREADS */
  /* add threads, ... */
  osThreadNew(StartDefaultTask, NULL, &defaultTask_attributes);
  /* USER CODE END RTOS_THREADS */
 
  /* USER CODE BEGIN RTOS_EVENTS */
  /* add events, ... */
  /* USER CODE END RTOS_EVENTS */
 
  /* Start scheduler */
  osKernelStart();

myTask02 was generated by CubeMX config.

hth

KnarfB

RLein.1
Associate II

If I remember correctly, I started the project via CubeIDE and accessed CubeMX from the project it created. I just looked and see that my version of CubeMX is 6.3.0. I could try updating.

RLein.1
Associate II

I updated all my ST tools and still get the same generated code:

 /* Init scheduler */

 osKernelInitialize(); /* Call init function for freertos objects (in freertos.c) */

 MX_FREERTOS_Init();

 /* Start scheduler */

 osKernelStart();

No wrapper for user code.

KnarfB
Principal III

I never got MX_FREERTOS_Init(); but the above init Sequence instead. How does the implementation of MX_FREERTOS_Init() look like? Maybe you find the user code sections there

hth

KnarfB

RLein.1
Associate II

That's it! I looked at osKernelInitialize, but not MX_FREERTOS_Init (probably got distracted). I appreciate your help!