Skip to main content
RLein.1
Associate III
July 11, 2022
Solved

How do I add FreeRTOS threads without CubeMX deleting them?

  • July 11, 2022
  • 7 replies
  • 2492 views

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

This topic has been closed for replies.
Best answer by KnarfB

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

7 replies

Andrew Neil
Super User
July 11, 2022

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

A complex system that works is invariably found to have evolved from a simple system that worked.A complex system designed from scratch never works and cannot be patched up to make it work.
RLein.1
RLein.1Author
Associate III
July 11, 2022

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
Super User
July 11, 2022

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
RLein.1Author
Associate III
July 11, 2022

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
RLein.1Author
Associate III
July 11, 2022

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
KnarfBBest answer
Super User
July 12, 2022

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
RLein.1Author
Associate III
July 12, 2022

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