Hello!
I am new to FreeRTOS and I have a problem.
This is part of the code and the problem is that my code gets stuck in mainThread
just above the if(xSemaphoreTake(Event, SEMAPHORE_TIMEOUT_MS) == pdPASS)
. I have created the _error_pull_timer_handler
especially so that I can release the semaphore.
The timer had the role of reinitializing the communication master slave in the I2C protocole in case it gets lost. There was a 500 miliseconds timer that had the role to do this.
Can you please help me so that I can unblock the semaphore?
static TimerHandle_t error_timer_handler = NULL;
#define ERROR_CHECKER_DELAY_MS 500
#define SEMAPHORE_TIMEOUT_MS pdMS_TO_TICKS(1000) /* 1sec */
static int _start_error_timer()
{
if(error_timer_handler == NULL)
{
return -1;
}
if(xTimerReset(error_timer_handler, 0) != pdPASS)
{
return -2;
}
return 0;
}
static int _stop_scale_error_timer()
{
if(error_timer_handler == NULL)
{
return -1;
}
if (xTimerIsTimerActive(error_timer_handler) == pdFALSE)
{
return -3; // Timer is not active
}
if(xTimerStop(error_timer_handler, 0) != pdPASS)
{
return -2;
}
return 0;
}
// callback function for timer
static void _error_pull_timer_handler(TimerHandle_t xTimer)
{
UNUSED_PARAM(xTimer);
// Attempt to release (give) the semaphore
if(xSemaphoreGive(Event) != pdPASS) {
// add a log
}
else {
// add anoher log
}
}
bool init(void)
{
//Create a binary semaphore : Do not take the first one, it will be used to acquire a previous IRQ if necessary
vSemaphoreCreateBinary(Event);
if(xTaskCreate(eventThread, THREAD_NAME, THREAD_STACK_SIZE, NULL, THREAD_PRIORITY, NULL) != pdPASS) {
return FALSE;
}
// create timer for scale error check
error_timer_handler = xTimerCreate("timer", pdMS_TO_TICKS(ERROR_CHECKER_DELAY_MS), pdTRUE, NULL, error_timer_handler);
if(error_timer_handler == NULL)
{
// add a log
}
else
{
// add another log
}
return TRUE;
}
static void mainThread(void* param)
{
UNUSED_PARAM(param);
while(1)
{
if(xSemaphoreTake(Event, SEMAPHORE_TIMEOUT_MS) == pdPASS)
{
TRACE("Event received\n");
..
}
_stop_scale_error_timer();
}