|
ELEC-C7222
Libraries for ELEC C7222 Course Work
|
FreeRTOS C++ device example demonstrating buttons, LEDs, PWM, and synchronization. More...
#include <cassert>#include <thread>#include <chrono>#include <cstdio>#include "freertos_task.hpp"#include "freertos_timer.hpp"#include "gpio.hpp"#include "platform.hpp"#include "button_event.hpp"#include "pwm.hpp"#include "safe_led.hpp"
Functions | |
| void | button1_irq_dispatcher (void *arg) |
| Timer callback that publishes Button 1 events to the task. | |
| void | button1_irq_handler (uint32_t events) |
| GPIO IRQ handler for Button 1. | |
| void | button1_monitor (void *param) |
| Button 1 monitoring task. | |
| void | button2_monitor (void *param) |
| Button 2 monitoring task. | |
| void | system_monitor (void *param) |
| Periodic system task that blinks the shared system LED. | |
| int | main () |
| Program entry point. | |
Variables | |
| c7222::FreeRtosTimer * | dispatcher_timer = nullptr |
| One-shot timer used to defer GPIO IRQ handling to the timer task. | |
| c7222::ButtonEvent | button1_event |
| Thread-safe event mailbox for Button 1 IRQ events. | |
| c7222::SafeLed * | system_led = nullptr |
| Shared LED protected by SafeLed for multi-task coordination. | |
| std::unique_ptr< c7222::PwmOut > | pwm_led3_red = nullptr |
| PWM-controlled LED instance (LED3_RED). | |
| float | duty_cycle = 1.0f |
| Current PWM duty cycle used to dim LED3_RED. | |
FreeRTOS C++ device example demonstrating buttons, LEDs, PWM, and synchronization.
Purpose of this example:
Devices and objects used:
Led and SafeLed.Button/GPIO IRQ and polling helpers.PwmOut to demonstrate duty-cycle control.FreeRtosTimer, FreeRtosTask) to defer ISR work to task context and use wrapper delay/scheduler helpers.Concurrency and synchronization:
std::thread is used to create FreeRTOS tasks through FreeRTOS-CPP11 integration.ButtonEvent uses std::mutex + std::condition_variable to provide a wait-with-timeout event mailbox for button IRQ events.SafeLed uses std::mutex + std::condition_variable to serialize access to a shared LED across multiple tasks.Helper classes:
ButtonEvent collects IRQ events and provides a blocking GetEvents() API.SafeLed ensures exclusive LED control between tasks, avoiding concurrent writes.ISR dispatching model:
FreeRtosTimer, passing the event bitmask as a void* argument.ButtonEvent.ButtonEvent dispatch via one-shot timer (detailed flow):
button1_irq_handler runs in IRQ context.FreeRtosTimer and passes the event bitmask as the timer's callback_arg (no heavy logic in IRQ).button1_irq_dispatcher runs in the FreeRTOS timer service task (normal task context).void* argument back to the event bitmask and calls button1_event.SetEvents(events), which notifies the waiting task.button1_monitor task unblocks in GetEvents() and processes the event.System LED behavior:
button2_monitor and system_monitor.button2_monitor tries to acquire the LED; if it succeeds, the LED stays on while the button remains pressed, then turns off on release.system_monitor blinks it by acquiring the LED for 500 ms, turning it on, then off and releasing it. | void button1_irq_dispatcher | ( | void * | arg | ) |
Timer callback that publishes Button 1 events to the task.
This runs in the FreeRTOS timer service task context, not in interrupt context. It pushes the event bitmask into ButtonEvent.


| void button1_irq_handler | ( | uint32_t | events | ) |
GPIO IRQ handler for Button 1.
Runs in IRQ context. It must be minimal, so it schedules the dispatcher timer and passes the event bitmask as the timer callback argument.


| void button1_monitor | ( | void * | param | ) |
Button 1 monitoring task.


| void button2_monitor | ( | void * | param | ) |
Button 2 monitoring task.
Uses polling (every 100 ms) and coordinates access to the shared system LED via SafeLed. This demonstrates cross-task protection of a shared LED.


| int main | ( | void | ) |
Program entry point.

| void system_monitor | ( | void * | param | ) |
Periodic system task that blinks the shared system LED.
It attempts to acquire the SafeLed with a timeout to avoid blocking indefinitely when another task holds the LED.


| c7222::ButtonEvent button1_event |
Thread-safe event mailbox for Button 1 IRQ events.
Producers: the timer callback (not ISR). Consumers: the Button 1 monitor task.
| c7222::FreeRtosTimer* dispatcher_timer = nullptr |
One-shot timer used to defer GPIO IRQ handling to the timer task.
The ISR records the event and starts this timer. When it fires, it runs in the FreeRTOS timer service task and signals the ButtonEvent object.
| float duty_cycle = 1.0f |
Current PWM duty cycle used to dim LED3_RED.
| std::unique_ptr<c7222::PwmOut> pwm_led3_red = nullptr |
PWM-controlled LED instance (LED3_RED).
| c7222::SafeLed* system_led = nullptr |
Shared LED protected by SafeLed for multi-task coordination.