0
\$\begingroup\$

I am a newbie to freeRTOS and I was trying to learn about semaphores,I am using ATmega32a microcontroller, the plan was to create 3 tasks each is blinking the same led for some time with different frequencies the result was the led is just on forever.

so I canceled 2 tasks and kept only one task to catch the problem and the led is just on forever again.

I used the debugger and what happens is that it never continue the loop of for loop it toggles the bit and then delay and then toggle again and then go out to check the semaphore and repeat, this is very strange behavior.

but that's in the debugger, in reality, it never toggles it's on forever. can you help me?

#include <avr/io.h>
/* FreeRTOS files. */
#include "FreeRTOS.h"
#include "task.h"
#include "croutine.h"
#include "FreeRTOSConfig.h"
#include "semphr.h"
#include <avr/interrupt.h>


/* Define all the tasks */
static void ledBlinkingtask1(void* pvParameters);


xSemaphoreHandle  MySemaphore = 0;

int main(void) {
    DDRB=0xff;

    /* Call FreeRTOS APIs to create tasks, all tasks has the same priority "1" with the
    same stack size*/
    xTaskCreate(ledBlinkingtask1,"LED1",
    configMINIMAL_STACK_SIZE, NULL, 1, NULL );
    
    
    // Start the RTOS kernel
    vTaskStartScheduler();

    return 0;
}

static void ledBlinkingtask1(void* pvParameters){
    
    const uint16_t blinkDelay = 500;
    while (1)
    {
        if (xSemaphoreTake(MySemaphore,2000))
        {
            for (int i =0;i<9;i++)
            {
                PORTB ^= (1<<0); //toggle PB0 //PB0
                vTaskDelay(blinkDelay); //wait some time
            }
            xSemaphoreGive(MySemaphore);

        }
    }
}
\$\endgroup\$
2
  • 1
    \$\begingroup\$ A Semaphore in FreeRTOS is not just an alias for a simple variable like an int. You have to ask FreeRTOS to 'Create' the Semaphore before you can use it. \$\endgroup\$
    – brhans
    Jun 14, 2020 at 21:45
  • \$\begingroup\$ yes problem solved I needed this declaration vSemaphoreCreateBinary(MySemaphore); thank you \$\endgroup\$
    – mr.Arrow
    Jun 14, 2020 at 21:50

1 Answer 1

2
\$\begingroup\$

Somewhere in your main or before the while(1) you should have created the semaphore with one of the following

xSemaphoreCreateBinary
xSemaphoreCreateBinaryStatic
xSemaphoreCreateCounting
xSemaphoreCreateCountingStatic
xSemaphoreCreateMutex
xSemaphoreCreateMutexStatic
xSemaphoreCreateRecursiveMutex
xSemaphoreCreateRecursiveMutexStatic 
\$\endgroup\$
3
  • \$\begingroup\$ thank you i discovered that this line is missing from main \$\endgroup\$
    – mr.Arrow
    Jun 14, 2020 at 21:49
  • \$\begingroup\$ vSemaphoreCreateBinary(MySemaphore); \$\endgroup\$
    – mr.Arrow
    Jun 14, 2020 at 21:49
  • \$\begingroup\$ @mr.Arrow In the documentation they say that "vSemaphoreCreateBinary [use xSemaphoreCreateBinary() for new designs]" so that function is probably deprecated and will be removed eventually, if I were you I would just use the xSemaphoreCreateBinary one. \$\endgroup\$
    – jDAQ
    Jun 14, 2020 at 22:01

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.