Boss lady
Boss lady
DIIDevHeads IoT Integration Server
Created by Boss lady on 6/24/2024 in #middleware-and-os
How can I manage communication between tasks using queues in FreeRTOS?
I tried adding a function to check if the queue is successfully created but now having a syntax error This my code
// Include necessary FreeRTOS header files
#include "FreeRTOS.h"
#include "task.h"
#include "queue.h"

// Declare the queue handle
QueueHandle_t xQueue;

// Task1 function
void Task1(void* pvParameters) {
// Check if queue is created
if (xQueue == NULL) {
// Queue not created
printf("Queue not created!\n");
vTaskDelete(NULL);
}

int value = 42;
while (1) {
// Send value to the queue
if (xQueueSend(xQueue, &value, portMAX_DELAY) != pdPASS) {
// Queue send failed
printf("Queue send failed!\n");
}

// Delay for a certain interval
vTaskDelay(pdMS_TO_TICKS(1000));
}
}

// Task2 function
void Task2(void* pvParameters) {
// Check if queue is created
if (xQueue == NULL) {
// Queue not created
printf("Queue not created!\n");
vTaskDelete(NULL);
}

int receivedValue;
while (1) {
// Receive value from the queue
if (xQueueReceive(xQueue, &receivedValue, portMAX_DELAY) == pdPASS) {
// Successfully received value from the queue
printf("Received value: %d\n", receivedValue);
}

// Delay for a certain interval
vTaskDelay(pdMS_TO_TICKS(1000))
}
}

int main() {
// Create the queue
xQueue = xQueueCreate(10, sizeof(int));

// Check if queue is created
if (xQueue == NULL) {
// Queue creation failed
printf("Queue creation failed!\n");
return 1;
}

// Create Task1
xTaskCreate(Task1, "Task1", configMINIMAL_STACK_SIZE, NULL, tskIDLE_PRIORITY + 1, NULL);

// Create Task2
xTaskCreate(Task2, "Task2", configMINIMAL_STACK_SIZE, NULL, tskIDLE_PRIORITY + 1, NULL)

// Start scheduler
vTaskStartScheduler();

while(1); // Should never reach here
return 0;
}
// Include necessary FreeRTOS header files
#include "FreeRTOS.h"
#include "task.h"
#include "queue.h"

// Declare the queue handle
QueueHandle_t xQueue;

// Task1 function
void Task1(void* pvParameters) {
// Check if queue is created
if (xQueue == NULL) {
// Queue not created
printf("Queue not created!\n");
vTaskDelete(NULL);
}

int value = 42;
while (1) {
// Send value to the queue
if (xQueueSend(xQueue, &value, portMAX_DELAY) != pdPASS) {
// Queue send failed
printf("Queue send failed!\n");
}

// Delay for a certain interval
vTaskDelay(pdMS_TO_TICKS(1000));
}
}

// Task2 function
void Task2(void* pvParameters) {
// Check if queue is created
if (xQueue == NULL) {
// Queue not created
printf("Queue not created!\n");
vTaskDelete(NULL);
}

int receivedValue;
while (1) {
// Receive value from the queue
if (xQueueReceive(xQueue, &receivedValue, portMAX_DELAY) == pdPASS) {
// Successfully received value from the queue
printf("Received value: %d\n", receivedValue);
}

// Delay for a certain interval
vTaskDelay(pdMS_TO_TICKS(1000))
}
}

int main() {
// Create the queue
xQueue = xQueueCreate(10, sizeof(int));

// Check if queue is created
if (xQueue == NULL) {
// Queue creation failed
printf("Queue creation failed!\n");
return 1;
}

// Create Task1
xTaskCreate(Task1, "Task1", configMINIMAL_STACK_SIZE, NULL, tskIDLE_PRIORITY + 1, NULL);

// Create Task2
xTaskCreate(Task2, "Task2", configMINIMAL_STACK_SIZE, NULL, tskIDLE_PRIORITY + 1, NULL)

// Start scheduler
vTaskStartScheduler();

while(1); // Should never reach here
return 0;
}
12 replies