xTaskCreatePinnedToCore doesn't return after starting task

lordryck
Posts: 3
Joined: Sat Aug 26, 2023 2:37 am

xTaskCreatePinnedToCore doesn't return after starting task

Postby lordryck » Sat Aug 26, 2023 3:19 am

I have a really odd story I need help with. I'm using an ESP32 with CAN Bus to emulate a CAN Bus device in my truck so that I can do development on a different board without having to buy two of the expensive vehicle boxes. It has been working great for the past 18 months. For the CAN stuff I've been using Collin Kidder's esp32_can and can_common libraries. Until last week I had been doing all my development work on an older Arduino IDE (1.8.13). Last week I decided to upgrade to 2.1.1. Along with that came, I believe, upgraded compiler and other tools. And my application broke. It crashed on a completely (to me) innocuous line in can_common. I upgraded both libraries to the lastest. They are fairly significantly changed because he's using the official TWAI drivers now. No more crash but now it hangs. I struggled for a bit then decided maybe I'd have better luck with a different IDE and compiler and I switched to VSCode with ESP-IDF (and have also tried platformio). I know where the problem is but I can't for the life of me figure out why.

esp_can_builtin.cpp has this code:

Code: Select all

 // func        desc    stack, params, priority, handle to task
 xTaskCreate(&task_CAN, "CAN_RX", 8192, this, 15, NULL);
 // this next task implements our better filtering on top of the TWAI library. Accept all frames then filter in here VVVVV
 xTaskCreatePinnedToCore(&task_LowLevelRX, "CAN_LORX", 8192, this, 19, NULL, 1);
 xTaskCreatePinnedToCore(&CAN_WatchDog_Builtin, "CAN_WD_BI", 4096, this, 10, NULL, 1);
 initializedResources = true;
The xTaskCreate goes fine. The first xTaskCreatePinnedToCore hangs. Sort of. I stuck a print statement in task_LowLevelRX and it is running just fine. But for some reason xTaskCreatePinnedToCore isn't returning so the next xTaskCreatePinnedToCore never runs.

Here's that task:

Code: Select all

void task_LowLevelRX(void *pvParameters)
{
    ESP32CAN *espCan = (ESP32CAN *)pvParameters;
    while (1)
    {
        twai_message_t message;
        if (twai_receive(&message, pdMS_TO_TICKS(100)) == ESP_OK)
        {
            espCan->processFrame(message);
        }
    }
}
To make things even more mysterious, I get the same behavior on two different brands of boards (Fusion and SKPang). And I've talked to Collin and he can't reproduce it. I am also using WiFi in case that matters. The Fusion board uses pins 4 and 5 for CAN and pin 16 has to be pulled low. The other board is 24 and 25 and no other pins are involved and I'm not using any other pins for anything.

Anyone have any idea why xTaskCreatePinnedToCore wouldn't return after it starts the task?

ESP_Sprite
Posts: 9739
Joined: Thu Nov 26, 2015 4:08 am

Re: xTaskCreatePinnedToCore doesn't return after starting task

Postby ESP_Sprite » Sun Aug 27, 2023 3:18 am

That is odd... perhaps you can enable core dumps and force a guru meditation (e.g. by dereferencing a null pointer)? That should give you a crashdump which includes the location each task is at; you might be able to see why the main task is blocking that way. Alternatively, if you have a JTAG debugger you should also be able to inspect that that way.

lordryck
Posts: 3
Joined: Sat Aug 26, 2023 2:37 am

Re: xTaskCreatePinnedToCore doesn't return after starting task

Postby lordryck » Tue Aug 29, 2023 2:13 pm

It still perplexes me why a compiler change apparently trigged this error, but I have fixed it. I added a

Code: Select all

vTaskDelay(1)
into the loop in task_LowLevelRX. Apparently with that task set at a relatively high priority, the scheduler never interrupted it. Putting even a single tick delay in allows the scheduler to grab control.

ESP_Sprite
Posts: 9739
Joined: Thu Nov 26, 2015 4:08 am

Re: xTaskCreatePinnedToCore doesn't return after starting task

Postby ESP_Sprite » Wed Aug 30, 2023 2:59 am

That still doesn't make sense... given that twai_receive is passed a timeout, I assume it's blocking on some sort of FreeRTOS thing inside, which should do the exact same thing as the vTaskDelay if no data is received. The only exception I can think of is that twai_receive somehow returns immediately and as such the task keeps spinning in the while(1) loop, but that would have triggered the task watchdog, and I assume you haven't seen a task watchdog warning?

lordryck
Posts: 3
Joined: Sat Aug 26, 2023 2:37 am

Re: xTaskCreatePinnedToCore doesn't return after starting task

Postby lordryck » Wed Aug 30, 2023 12:31 pm

No, nothing. Everything just hangs. The only reason I knew it wasn't really "hung" was I put a print inside the loop and it merrily printed. It's as though it got stuck so hard in the loop that xTaskCreatePinnedToCore wasn't able to detach itself. I added another print immediately after that call and it was never reached.

Who is online

Users browsing this forum: Majestic-12 [Bot] and 63 guests