// #include <avr/io.h>
#include "freertos/FreeRTOS.h"
#include "freertos/task.h"
#include "freertos/semphr.h"
#include <stdio.h>
// #include <Arduino_FreeRTOS.h>
// #include <queue.h>
#define LENGHT_OF_BUFFER 05
#define TRIGGER_LEVEL 01
#define DELAY_TO_BE_USED 10
void vInitializeSemaphore(void);
void vSenderTask(void *params);
void vReceiverTask(void *params);
SemaphoreHandle_t binary_semaphore=NULL;
SemaphoreHandle_t counting_semaphore=NULL;
uint8_t data[LENGHT_OF_BUFFER] = {1,2,3,4,5};
void setup() {
// put your setup code here, to run once:
Serial.begin(115200);
Serial.println("Hello, ESP32!");
vInitializeSemaphore();
xTaskCreate(vSenderTask, NULL, 50000, NULL,1,NULL);
xTaskCreate(vReceiverTask, NULL, 50000, NULL, 2, NULL);
vTaskStartScheduler();
}
void loop() {
// put your main code here, to run repeatedly:
delay(10); // this speeds up the simulation
}
void vInitializeSemaphore(void)
{
binary_semaphore = xSemaphoreCreateBinary();
counting_semaphore = xSemaphoreCreateCounting(5,0);
if(binary_semaphore != NULL && counting_semaphore != NULL)
printf("Semaphore created successfully!\n");
else
printf("Semaphore are not created!\n");
}
void vSenderTask(void *params)
{
BaseType_t result=0;
while(1)
{
result = xSemaphoreGive(counting_semaphore);//passing the address of temp into queue
result = xSemaphoreGive(counting_semaphore);//passing the address of temp into queue
result = xSemaphoreGive(counting_semaphore);//passing the address of temp into queue
vTaskDelay(pdMS_TO_TICKS(DELAY_TO_BE_USED)); // Adjust the delay as needed
printf("Delay of Sender Task Expired!\n");
}
}
void vReceiverTask(void *params)
{
BaseType_t result = 0;
while(1)
{
result = xSemaphoreTake(counting_semaphore, portMAX_DELAY);
printf("Token received correctly from sender task!\n");
}
}