### Scheduling
- Priority of a task is the only element that matters to decide which task has to be switched in.
- Priority is simply a number given to a task when it is created or changed manually using `vTaskPriorityGet() and vTaskPrioritySet()`
- Event-based or continuous tasks are preempted by periodic tasks
- Scheduler shares running time among all priority-equally tasks
- Round Robin implementation (no idea what this is)
- "The programmer has to make sure there is no higher priority task taking all running time for itself"
### Queue management
- Do not belong to any task in particular. Queue is FIFO, elements are read in the order that they have been written.
- If several tasks are trying to read a queue, the highest priority task reads it first
- If several tasks with the same priority are trying to read, the first task that asked for a read operation is chosen. Task may also specify a max waiting time for the queue to allow it to be read. After this time, task switches to "Ready" state
- When a single task reads in a queue, it is moved to "Blocked" state and moved back to "Ready" as soon as data has been written in the queue by another task or interrupt
### Resource management
#### Binary Semaphores
- Effective way to synchronize tasks. Can be seen as a queue which contains only one element (why?)
- An illustration is given in figure 7 of the PDF. That makes it clear. Interrupt gives a semaphore and the task takes it.
#### Mutexes
- Designed to provide mutual exclusion and prevent deadlocking (what are these things?)
- **Mutual exclusion**
- Ensuring only one task (or thread) can access a shared resource at a given time
- When a task wants to access a shared resource, it takes/locks the mutex. If another task wants to access the same resource while the mutex is locked, it will be blocked until the first task gives/unlocks the mutex.
- By ensuring that only one task can access the shared resource at a time, mutex helps prevent race conditions/data inconsistencies
- **Deadlocking**
- Situation where 2+ tasks are waiting indefinitely for a set of resources, and each of those resources is held by one of the waiting tasks. This leads to a state where the resource is stuck indefinitely
#### Difference between binary semaphores and mutexes
- Priority inheritance is the only difference between a binary semaphore and a mutex. When several tasks ask for a mutex, the mutex holder's priority is set to the highest waiting task priority. Using a mutex raises complexity and should be avoided whenever possible.
#### Counting semaphores
- Can be "taken" limited number of times before it becomes unavailable
- Maintains value which is increased when it is given and decreased when taken
### Interrupt handling
- Fully implemented in hardware. Software can only give methods to handle a given interrupt
- Interrupt priorities are not related to task priorities and will always preempt them
- Function defined as interrupt handlers cannot freely use FreeRTOS API: access to queues/semaphores is forbidden through normal functions
-
### Sources
![[freertos overview.pdf]]![[Developing Basic Applications With FreeRTOS on TM4C MCUs.pdf]]
- Timers in FreeRTOS
- https://www.freertos.org/RTOS-software-timer.html
- Relevant to [[BMS Code Rewrite]]
- Small delay between starting and reading