// standard includes
#include <stdio.h>
#include <stdint.h>
#include <stdlib.h>
// freeRTOS includes
#include <FreeRTOS.h>
#include <task.h>
#include <queue.h>
#include <timers.h>
#include <semphr.h>
uint8_t counter = 0;
SemaphoreHandle_t xMutex;
// funct defines
void vApplicationStackOverflowHook(
TaskHandle_t xTask,
char *pcTaskName
)
{
(void)xTask;
printf(
"[!] Stack overflow nel task: %s\n",
pcTaskName != NULL ? pcTaskName : "unknown"
);
taskDISABLE_INTERRUPTS();
for (;;)
{
}
}
void incrementCounter(void* parameters) __attribute__((noreturn));
void decrementCounter(void* parameters) __attribute__((noreturn));
void incrementCounter(void* parameters)
{
for (;;)
{
// lock of the mutex
//xSemaphoreTake(xMutex,0);
//printf("[*] Pre-increment counter value:%d\n",counter);
//printf("\n");
//taskENTER_CRITICAL();
int tmp = counter;
//vTaskDelay(pdMS_TO_TICKS(500));
tmp++;
//vTaskDelay(pdMS_TO_TICKS(500));
counter = tmp;
//taskENTER_CRITICAL();
printf("[*] Post-increment counter value:%d\n",counter);
printf("\n");
//taskEXIT_CRITICAL();
vTaskDelay(pdMS_TO_TICKS(100));
//taskEXIT_CRITICAL();
//xSemaphoreGive(xMutex);
}
}
void decrementCounter(void* parameters)
{
for (;;)
{
// lock of the mutex
//xSemaphoreTake(xMutex,0);
//printf("[*] Pre-increment counter value:%d\n",counter);
//printf("\n");
//taskENTER_CRITICAL();
if(counter==0)
{
taskYIELD();
}
int tmp = counter;
//vTaskDelay(pdMS_TO_TICKS(500));
tmp--;
//vTaskDelay(pdMS_TO_TICKS(500));
counter = tmp;
//taskENTER_CRITICAL();
printf("[*] Post-decrement counter value:%d\n",counter);
printf("\n");
//taskEXIT_CRITICAL();
vTaskDelay(pdMS_TO_TICKS(100));
//taskEXIT_CRITICAL();
//xSemaphoreGive(xMutex);
}
}
int main()
{
// Mutex creation
xMutex = xSemaphoreCreateMutex();
// task creation
BaseType_t taskOneFlag = xTaskCreate(incrementCounter,"CounterIncrementTask1",100,NULL,configMAX_PRIORITIES - 1U,NULL);
BaseType_t taskTwoFlag = xTaskCreate(decrementCounter,"CounterDecrementTask2",100,NULL,configMAX_PRIORITIES - 1U,NULL);
//BaseType_t taskThreeFlag = xTaskCreate(incrementCounter,"CounterIncrementTask3",10,NULL,configMAX_PRIORITIES - 1U,NULL);
//BaseType_t taskFourFlag = xTaskCreate(decrementCounter,"CounterDecrementTask4",10,NULL,configMAX_PRIORITIES - 1U,NULL);
//BaseType_t taskFiveFlag = xTaskCreate(incrementCounter,"CounterIncrementTask5",10,NULL,configMAX_PRIORITIES - 1U,NULL);
printf("[*] Task One creation flag: %d\n",taskOneFlag);
printf("[*] Task Two creation flag: %d\n",taskTwoFlag);
//printf("[*] Task Three creation flag: %d\n",taskThreeFlag);
//printf("[*] Task Four creation flag: %d\n",taskFourFlag);
//printf("[*] Task Five creation flag: %d\n",taskFiveFlag);
// scheduler start
vTaskStartScheduler();
//should never reach here
// only reaches here if there are memory allocation problems in FreeRTOS heap
for(;;)
{
printf("[*] There has been some kind of scheduling / memory allocation problem. Check the code... \n");
}
return 0;
}2 views