2019-04-28 02:07 PM
2019-04-30 09:15 AM
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.
2019-04-30 03:05 PM
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
2019-05-18 01:59 PM
thank you so much