cancel
Showing results for 
Search instead for 
Did you mean: 

Good evening everyone, I work with freeRTOS running on STM32F4. I receive data via UART and I want it to be with a binary semaphore, how to write the interrupt routine ?

AGHAZ
Associate II
 
3 REPLIES 3
turboscrew
Senior III

It's a bit heavy, because in FreeRTOS almost all synchronization mechanisms are based on queues. Also semaphores.

The FreeRTOS semaphores are quite easy to use - they are just 'given' (signal) and taken (waited).

There are two functions for them - the ones used within processes and the ones used from interrupts.

Personally I'd prefer lockless ring buffers or small assembly pieces using semafore instructions (LDREX, STREX).

The idea of the lockless ring buffer is that one party only writes tail and the other only writes head.

If head/tail indices fit into a byte, it's inherently safe, but usually bigger values (uint16_t, uint32_t) are written atomically as well.

turboscrew
Senior III

If you want to use a binary semaphore, you first create it then 'give' it. Created semaphore has 'invalid' status, and it needs to be given to 'initialize' it.

SemaphoreHandle_t semaphore;

semaphore = xSemaphoreCreateBinary( );

xSemaphoreGive(semaphore); // now free

Then you give or take as needed:

xSemaphoreTake( SemaphoreHandle_t xSemaphore, TickType_t xTicksToWait );

xSemaphoreGive( SemaphoreHandle_t xSemaphore );

xSemaphoreTakeFromISR (SemaphoreHandle_t xSemaphore, signed BaseType_t *pxHigherPriorityTaskWoken );

xSemaphoreGiveFromISR (SemaphoreHandle_t xSemaphore, signed BaseType_t *pxHigherPriorityTaskWoken);

There are separate functions for tasks and ISRs, because ISRs don't have process context, and can't be put to sleep.

Read more here: https://www.freertos.org/a00113.html

AGHAZ
Associate II

thank you so much