Skip to content

Instantly share code, notes, and snippets.

@sankarcheppali
Created August 25, 2017 19:20
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save sankarcheppali/33911be61e85828d7b75bf7b83cba741 to your computer and use it in GitHub Desktop.
Save sankarcheppali/33911be61e85828d7b75bf7b83cba741 to your computer and use it in GitHub Desktop.
inter task communication using queues
#include <stdio.h>
#include "freertos/FreeRTOS.h"
#include "freertos/task.h"
#include "freertos/queue.h"
QueueHandle_t q=NULL;
void consumer_task(void *pvParameter)
{
unsigned long counter;
if(q == NULL){
printf("Queue is not ready");
return;
}
while(1){
xQueueReceive(q,&counter,(TickType_t )(1000/portTICK_PERIOD_MS));
printf("value received on queue: %lu \n",counter);
vTaskDelay(500/portTICK_PERIOD_MS); //wait for 500 ms
}
}
void producer_task(void *pvParameter){
unsigned long counter=1;
if(q == NULL){
printf("Queue is not ready \n");
return;
}
while(1){
printf("value sent on queue: %lu \n",counter);
xQueueSend(q,(void *)&counter,(TickType_t )0); // add the counter value to the queue
counter++;
vTaskDelay(1000/portTICK_PERIOD_MS); //wait for a second
}
}
void app_main()
{
q=xQueueCreate(20,sizeof(unsigned long));
if(q != NULL){
printf("Queue is created\n");
vTaskDelay(1000/portTICK_PERIOD_MS); //wait for a second
xTaskCreate(&producer_task,"producer_task",2048,NULL,5,NULL);
printf("producer task started\n");
xTaskCreate(&consumer_task,"consumer_task",2048,NULL,5,NULL);
printf("consumer task started\n");
}else{
printf("Queue creation failed");
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment