0

I have used STM32CubeMX to create a FreeRTOS task on the STM32F030 development board using the CMSIS-RTOS v2 interface, but the task I created using xTaskCreate() is not scheduling correctly, and the one I created using osTreadNew is working fine,Why is this happening?

I created two tasks using xTaskCreat()

xTaskCreate(
 LibmodbusServerTask,
 "LibmodbusServerTask",
 512,
 NULL,
 osPriorityNormal,
 NULL
 );
xTaskCreate(
 AHT20Task,
 "AHT20Task",
 512,
 NULL,
 osPriorityNormal,
 NULL
 );

There's also a default task

osThreadId_t defaultTaskHandle;
const osThreadAttr_t defaultTask_attributes = {
 .name = "defaultTask",
 .stack_size = 128 * 4,
 .priority = (osPriority_t) osPriorityNormal,
};
defaultTaskHandle = osThreadNew(StartDefaultTask, NULL, &defaultTask_attributes);

Here's the simplified code for the three tasks

void AHT20Task(void *pvParameters)
{
 while (1)
 {
 //ledTask
 vTaskDelay(100);
 } 
}
void LibmodbusServerTask( void *pvParameters )
{
 //Variables, initialization, connection to modbus
 for (;;) {
 do {
 rc = modbus_receive(ctx, query);
 } while (rc == 0);
 /* The connection is not closed on errors which require on reply such as
 bad CRC in RTU. */
 if (rc == -1 && errno != EMBBADCRC) {
 /* Quit */
 continue;
 }
 rc = modbus_reply(ctx, query, rc, mb_mapping);
 if (rc == -1) {
 //break;
 }
 }
 //close modbus
 vTaskDelete(NULL);
}
void StartDefaultTask(void *argument)
{
 /* USER CODE BEGIN StartDefaultTask */
 /* Infinite loop */
 for(;;)
 { 
 vTaskDelay(500);
 }
 /* USER CODE END StartDefaultTask */
}

I'm debugging code on an STM32F030 development board. The code only enters the StartDefaultTask once at startup and then continuously runs the LibmodbusServerTask. The AHT20Task has never been entered. Next, I simply switched the task creation method and used osThreadNew() to create the task.The program now switches correctly between the LibmodbusServerTask and the AHT20Task.Here is the code to create the task

 osThreadId_t libmodbusTaskHandle;
const osThreadAttr_t libmodbusTask_attributes = {
 .name = "libmodbusTask",
 .stack_size = 128 * 4,
 .priority = (osPriority_t) osPriorityNormal,
};
 osThreadId_t aht20TaskHandle;
const osThreadAttr_t aht20Task_attributes = {
 .name = "aht20HandleTask",
 .stack_size = 128 * 4,
 .priority = (osPriority_t) osPriorityNormal,
};
 libmodbusTaskHandle = osThreadNew(LibmodbusServerTask, NULL, &libmodbusTask_attributes);
 aht20TaskHandle = osThreadNew(AHT20Task, NULL, &aht20Task_attributes);

This is part of my STM32CubeMX configuration. STM32CubeMX configuration sys timer

asked Sep 16, 2024 at 8:24
2
  • Did you also try without the CMSIS_V2 interface? (Select "Disabled" in the drop-down box at the top of your screenshot). If you don't specifically need it, it is just another layer of abstraction that might cause confusion. IMHO the code generation in CubeMX is not great when enabling FreeRTOS and CMSIS. For example xTaskCreate(..., osPriorityNormal, NULL); is wrong: it's mixing the priority levels from CMSIS (of type osPriority) with that of FreeRTOS - they are not the same. I don't think this explains your problem, but it's just easier to stick with one interface (raw FreeRTOS). Commented Sep 17, 2024 at 8:21
  • To improve this question (and possibly get a more helpful answer), please update your question with a minimal reproducible example, at least a simple main that can be copy-pasted and tested by others. For example, we don't know how you start the scheduler (e.g. using osKernelStart() or vTaskStartScheduler()) and how you concluded that there's no task switch in the first example (the AHT20Task has no observable behavior, so you could only check it using a debugger, and it's easy to make a mistake and draw the wrong conclusions). See How to Ask. Commented Sep 17, 2024 at 8:27

0

Know someone who can answer? Share a link to this question via email, Twitter, or Facebook.

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.