# 5.2 An Overview of Asynchronous Tools in FreeRTOS

### <span class="ng-star-inserted">Software Timers: For Application-Scheduled Events</span>

<span class="ng-star-inserted">A </span>**<span class="ng-star-inserted">FreeRTOS Software Timer</span>**<span class="ng-star-inserted"> is a tool used to schedule the execution of a function at a future time. It's like setting an alarm clock within your software. When the timer expires, the RTOS automatically calls a predefined function, known as a </span>**<span class="ng-star-inserted">callback function</span>**<span class="ng-star-inserted">.</span>

**<span class="ng-star-inserted">Key Characteristics:</span>**

- **<span class="ng-star-inserted">Managed by the RTOS:</span>**<span class="ng-star-inserted"> Software timers are managed by a dedicated RTOS task (the "timer daemon"). This means they do not consume CPU time while they are waiting to expire.</span>
- **<span class="ng-star-inserted">Tied to the System Tick:</span>**<span class="ng-star-inserted"> The resolution of a software timer is determined by the FreeRTOS system tick rate (</span>`<span class="inline-code ng-star-inserted">configTICK_RATE_HZ</span>`<span class="ng-star-inserted">). You cannot schedule a timer for a period shorter than one tick.</span>
- **<span class="ng-star-inserted">Use Case:</span>**<span class="ng-star-inserted"> Ideal for repetitive, low-priority, or application-level timing. For example, you might use a software timer to:</span>
    
    
    - <span class="ng-star-inserted">Read a temperature sensor every five seconds.</span>
    - <span class="ng-star-inserted">Update a clock display once per minute.</span>
    - <span class="ng-star-inserted">Turn off an LED 500ms after it was turned on.</span>

**<span class="ng-star-inserted">Types of Software Timers:</span>**

1. **<span class="ng-star-inserted">One-Shot Timer:</span>**<span class="ng-star-inserted"> Executes its callback function only once after it is started.</span>
2. **<span class="ng-star-inserted">Auto-Reload Timer:</span>**<span class="ng-star-inserted"> Executes its callback function repeatedly at a fixed interval until it is explicitly stopped.</span>

### <span class="ng-star-inserted">Hardware Interrupts: For Hardware-Triggered Events</span>

<span class="ng-star-inserted">A </span>**<span class="ng-star-inserted">Hardware Interrupt</span>**<span class="ng-star-inserted"> is a mechanism for a hardware peripheral to signal the CPU that it needs immediate attention. Unlike a software timer, which is scheduled by your application, an interrupt is triggered by an external, physical event.</span>

**<span class="ng-star-inserted">Key Characteristics:</span>**

- **<span class="ng-star-inserted">High Priority:</span>**<span class="ng-star-inserted"> An interrupt will immediately preempt the currently running code, regardless of the task's priority. The CPU will save its current state and jump to execute the ISR.</span>
- **<span class="ng-star-inserted">Hardware-Driven:</span>**<span class="ng-star-inserted"> They are generated by peripherals like GPIO pins (e.g., a button press), hardware timers (for precise timing), or communication interfaces like UART/SPI (e.g., data has arrived).</span>
- **<span class="ng-star-inserted">Use Case:</span>**<span class="ng-star-inserted"> Essential for time-critical operations and reacting to external events with minimal latency. For example, you would use a hardware interrupt to:</span>
    
    
    - <span class="ng-star-inserted">Count pulses from a motor encoder to measure its speed.</span>
    - <span class="ng-star-inserted">Immediately stop a machine when a safety limit switch is triggered.</span>
    - <span class="ng-star-inserted">Capture incoming data from a high-speed sensor before it is overwritten.</span>

<span class="ng-star-inserted">In summary, the choice between them is driven by the source of the event:</span>

- <span class="ng-star-inserted">Use a </span>**<span class="ng-star-inserted">Software Timer</span>**<span class="ng-star-inserted"> when the event is driven by the logic of your application ("</span><span class="ng-star-inserted">I need to do X in 500 milliseconds</span><span class="ng-star-inserted">").</span>
- <span class="ng-star-inserted">Use a </span>**<span class="ng-star-inserted">Hardware Interrupt</span>**<span class="ng-star-inserted"> when the event is driven by an external hardware signal that requires an immediate response ("</span><span class="ng-star-inserted">The hardware needs attention NOW</span><span class="ng-star-inserted">").</span>