Task isn't running again after running for the first time

HoldMyBeer
Posts: 4
Joined: Tue Dec 14, 2021 12:37 pm

Task isn't running again after running for the first time

Postby HoldMyBeer » Wed May 11, 2022 6:49 am

I'm writing a simple program to use queues between two tasks, where one task sends data to the queue, and the other task received data from the queue. Here's the code:

Code: Untitled.c Select all


#include <stdio.h>
#include "freertos/FreeRTOS.h"
#include "freertos/task.h"
#include "freertos/queue.h"
#include "driver/gpio.h"

// Create a constant that restricts ESP32 to use only one core when creating tasks
#if CONFIGURE_FREERTOS_UNICORE
static const BaseType_t app_cpu = 0;
#else
static const BaseType_t app_cpu = 1;
#endif

static const uint8_t queue_length = 5; // set max length of queue
static QueueHandle_t queue; // handle for the queue

void print_messages(void *parameters) {
int item;

while(1) {
printf("Spaces in queue :: %d\n", uxQueueSpacesAvailable(queue));
if(xQueueReceive(queue, &item, 0) == pdTRUE) {
printf("Item received :: %d\n", item);
}
else {
printf("Item not received\n");
}

vTaskDelay(1000 / portTICK_PERIOD_MS);
}
}

void send_messages(void *parameters) {
static int num = 0;

while(1) {
printf("Sending num :: %d\n", num);
if(xQueueSend(queue, &num, 0) != pdTRUE) {
printf("Queue is full\n");
}
printf("Sent num\n");
num++;
vTaskDelay(1000 / portTICK_PERIOD_MS);
}
}

void app_main(void) {
vTaskDelay(1000 / portTICK_PERIOD_MS);
queue = xQueueCreate(queue_length, sizeof(int));

xTaskCreatePinnedToCore(
send_messages, // function handler for the task
"Send messages", // name of task
2048, // stack size for the task
NULL, // pointer to argument we'd like to pass
1, // priority
NULL, // task handler
app_cpu // which core to run
);

xTaskCreatePinnedToCore(
print_messages, // function handler for the task
"Print messages", // name of task
1024, // stack size for the task
NULL, // pointer to argument we'd like to pass
1, // priority
NULL, // task handler
app_cpu // which core to run
);
}
Ideally, the tasks should get scheduled in a round-robin way since their priorities are the same. So, the "Send messages" task should write data to the queue, and the "Print messages" task should receive data from the queue and print it. It works for the first time when num = 0, but after that, from the logs, it seems that the "Send messages" task doesn't get to ever run. Here is how the console looks:

Code: Untitled.txt Select all


Sending num :: 0
Sent num
Spaces in queue :: 4
Item received :: 0
Spaces in queue :: 5
Item not received
Spaces in queue :: 5
Item not received
Spaces in queue :: 5
Item not received
Spaces in queue :: 5
Item not received
Spaces in queue :: 5
If "Send messages" runs again, then it should print "Sending num :: <value of num>". Am I missing something here? What's wrong with the code?

markkuk
Posts: 39
Joined: Wed Mar 27, 2019 11:50 am

Re: Task isn't running again after running for the first time

Postby markkuk » Wed May 11, 2022 8:36 am

The stack for "Print messages" task is too small and it breaks the other task. In my system, the program crashes after first run of print_messages():

Code: Select all

Sending num :: 0
Sent num
Spaces in queue :: 4
Item received :: 0
Guru Meditation Error: Core  0 panic'ed (LoadProhibited). Exception was unhandled.

Core  0 register dump:
PC      : 0x40086a70  PS      : 0x00060033  A0      : 0x80085452  A1      : 0x3ffb0b50
Increasing the stack size to 2048 makes the code run correctly:

Code: Select all

Sending num :: 0
Sent num
Spaces in queue :: 4
Item received :: 0
Sending num :: 1
Sent num
Spaces in queue :: 4
Item received :: 1
Sending num :: 2
Sent num
Spaces in queue :: 4
Item received :: 2
Sending num :: 3
Sent num
Spaces in queue :: 4
Item received :: 3

HoldMyBeer
Posts: 4
Joined: Tue Dec 14, 2021 12:37 pm

Re: Task isn't running again after running for the first time

Postby HoldMyBeer » Wed May 11, 2022 9:05 am

Oh I wasn't getting the error for stack overflow. Anyways, fixed the stack size and it works! Thank you! :)

User avatar
mbratch
Posts: 317
Joined: Fri Jun 11, 2021 1:51 pm

Re: Task isn't running again after running for the first time

Postby mbratch » Wed May 11, 2022 6:27 pm

A stack overflow problem may not show a message depending upon whether the code is sane enough at that point to do so. Just keep in mind when providing stack space for a task what that task is calling. Functions like `printf` require a lot of stack.

Who is online

Users browsing this forum: ChatGPT-User and 5 guests