ELEC-C7222
Libraries for ELEC C7222 Course Work
Loading...
Searching...
No Matches
main_freertos_board_example.c File Reference

FreeRTOS + Pico W board validation example. More...

#include <stdio.h>
#include "pico/stdlib.h"
#include "FreeRTOS.h"
#include "event_groups.h"
#include "semphr.h"
#include "task.h"
#include "c7222_pico_w_board.h"
Include dependency graph for main_freertos_board_example.c:

Functions

static void button_irq_handler (uint32_t gpio, uint32_t events)
 Shared GPIO IRQ handler for B1 and B2.
 
static void task_button_b1 (void *ctx)
 Task for button B1 (interrupt-driven).
 
static void task_button_b2 (void *ctx)
 Task for button B2 (interrupt-driven).
 
static void task_button_b3 (void *ctx)
 Task for button B3 (polled).
 
static void task_button_b4 (void *ctx)
 Task for button B4 (polled).
 
static void task_manager (void *ctx)
 Management/heartbeat task.
 
int main (void)
 Entry point for the FreeRTOS board validation example.
 

Variables

static SemaphoreHandle_t b1_semaphore
 Binary semaphore used to signal B1 press events from ISR to task.
 
static EventGroupHandle_t b2_event_group
 Event group used to signal B2 press events from ISR to task.
 
static const EventBits_t kB2PressedBit = (1u << 0)
 

Detailed Description

FreeRTOS + Pico W board validation example.

This example is intended for students who are testing the Pico W board for the first time. It validates the c7222_pico_w_board API and demonstrates foundational FreeRTOS patterns commonly used on microcontrollers.

Learning goals and intent

  • Verify that GPIO mapping for LEDs and buttons matches the board layout.
  • Show how to combine interrupts with tasks for responsive input handling.
  • Compare interrupt-driven inputs (B1/B2) against polled inputs (B3/B4).
  • Practice using FreeRTOS synchronization primitives and task scheduling.

What this example demonstrates

  • Board GPIO initialization using c7222_pico_w_board_init_gpio().
  • Active-low LEDs: LEDs are driven low to turn ON and high to turn OFF.
  • Button input with pull-ups: buttons are configured as inputs with internal pull-ups, so a pressed button reads low.
  • FreeRTOS tasks: five tasks run concurrently:
    • One task for each button (B1–B4).
    • One manager task that prints a heartbeat.
  • Binary semaphore: B1 uses a binary semaphore signaled from an IRQ.
  • Event group: B2 uses an event flag bit signaled from an IRQ.
  • IRQ handling: B1 and B2 use falling-edge interrupts to wake tasks.
  • Polling: B3 and B4 are polled to compare interrupt vs polling behavior.

FreeRTOS features shown

  • xTaskCreate: create multiple concurrent tasks.
  • Binary semaphore: a lightweight signal from ISR to task (B1).
  • Event group: flag-based wakeup for a task (B2).
  • ISR safe APIs: xSemaphoreGiveFromISR and xEventGroupSetBitsFromISR.
  • vTaskDelay: basic cooperative timing and debouncing for polling tasks.

Expected behavior

  • Press B1: the B1 task prints a message and toggles LED1_GREEN.
  • Press B2: the B2 task prints a message and toggles LED2_GREEN.
  • Press B3: the polling task prints a message and toggles LED3_GREEN.
  • Press B4: the polling task prints a message and toggles LED3_RED.
  • The manager task prints a heartbeat message once per second.
  • The IRQ handler lights LED1_RED for B1 and LED2_RED for B2 while pressed.

Notes for first-time testing

  • Make sure the board is powered and connected.
  • LEDs are active-low on this board; ON means GPIO is 0.
  • Button presses generate falling-edge interrupts because the inputs are pulled up and go low when pressed.
  • If nothing prints, confirm USB serial is enabled and the baud rate matches.

Function Documentation

◆ button_irq_handler()

static void button_irq_handler ( uint32_t  gpio,
uint32_t  events 
)
static

Shared GPIO IRQ handler for B1 and B2.

Uses ISR-safe FreeRTOS APIs to notify the corresponding task. Also updates indicator LEDs so students can see edge detection immediately.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ main()

int main ( void  )

Entry point for the FreeRTOS board validation example.

Board GPIO initialization (LEDs + buttons).

IPC primitives (created before enabling IRQs).

Attach IRQs for falling edges on B1 and B2.

Create tasks: one per button + manager.

Here is the call graph for this function:

◆ task_button_b1()

static void task_button_b1 ( void *  ctx)
static

Task for button B1 (interrupt-driven).

Waits on a binary semaphore signaled by the GPIO ISR and toggles LED1_GREEN.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ task_button_b2()

static void task_button_b2 ( void *  ctx)
static

Task for button B2 (interrupt-driven).

Waits on an event-group bit signaled by the GPIO ISR and toggles LED2_GREEN.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ task_button_b3()

static void task_button_b3 ( void *  ctx)
static

Task for button B3 (polled).

Periodically reads the input and toggles LED3_GREEN on press.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ task_button_b4()

static void task_button_b4 ( void *  ctx)
static

Task for button B4 (polled).

Periodically reads the input and toggles LED3_RED on press.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ task_manager()

static void task_manager ( void *  ctx)
static

Management/heartbeat task.

Prints a periodic heartbeat so students can verify the scheduler is running.

Here is the caller graph for this function:

Variable Documentation

◆ b1_semaphore

SemaphoreHandle_t b1_semaphore
static

Binary semaphore used to signal B1 press events from ISR to task.

◆ b2_event_group

EventGroupHandle_t b2_event_group
static

Event group used to signal B2 press events from ISR to task.

◆ kB2PressedBit

const EventBits_t kB2PressedBit = (1u << 0)
static