ELEC-C7222
Libraries for ELEC C7222 Course Work
Loading...
Searching...
No Matches
main_freertos.cpp File Reference

C++ FreeRTOS LED test for Pico 2 W using std::thread. More...

#include <assert.h>
#include <chrono>
#include <new>
#include <stdio.h>
#include <iostream>
#include <thread>
#include <functional>
#include "pico/stdlib.h"
Include dependency graph for main_freertos.cpp:

Classes

class  BaseClass
 Singleton startup coordinator. More...
 

Macros

#define LED_DELAY_MS   100
 

Functions

int pico_led_init ()
 
void pico_set_led (bool led_on)
 Set the onboard LED state.
 
static void led_task ()
 LED blink worker executed by std::thread.
 
static void log_task ()
 Low-priority periodic logger executed by std::thread.
 
int main ()
 Application entry point.
 

Variables

std::thread led_thread
 Global LED worker thread handle.
 
std::thread log_thread
 Global logger worker thread handle.
 

Detailed Description

C++ FreeRTOS LED test for Pico 2 W using std::thread.

This source tests a Pico 2 W board by blinking the onboard LED while running a low-priority logger task. It uses the Pico C SDK for board/LED access and FreeRTOS as the scheduler, but the application task model is C++-oriented.

Difference from main_freertos.c:

  • main_freertos.c creates tasks directly with xTaskCreate() and uses vTaskDelay().
  • main_freertos.cpp creates std::thread tasks and uses std::this_thread::sleep_for(), with priorities assigned from inside the running task via vTaskPrioritySet().

Dependency on FreeRTOS-cpp11:

  • The std::thread/chrono behavior in this file depends on the FreeRTOS-cpp11 integration (FREERTOS_CPP11_SOURCES and FREERTOS_CPP11_INCLUDE_DIRS in CMake).
  • FreeRTOS-cpp11 provides the libstdc++ threading glue for FreeRTOS so C++ threading APIs map to FreeRTOS primitives.

Macro Definition Documentation

◆ LED_DELAY_MS

#define LED_DELAY_MS   100

Function Documentation

◆ led_task()

static void led_task ( )
static

LED blink worker executed by std::thread.

Behavior:

  • Raises its own FreeRTOS priority to tskIDLE_PRIORITY + 1.
  • Toggles LED ON/OFF forever with LED_DELAY_MS period.

Usage:

  • Spawn via std::thread{led_task} after FreeRTOS scheduler is running.
Here is the call graph for this function:
Here is the caller graph for this function:

◆ log_task()

static void log_task ( )
static

Low-priority periodic logger executed by std::thread.

Behavior:

  • Sets its FreeRTOS priority to tskIDLE_PRIORITY.
  • Prints a heartbeat log once per second forever.

Usage:

  • Spawn via std::thread{log_task} after scheduler start.
Here is the caller graph for this function:

◆ main()

int main ( void  )

Application entry point.

Startup sequence:

  1. Initialize stdio and LED backend.
  2. Create a single bootstrap FreeRTOS task.
  3. Bootstrap task (BaseClass::startup()) creates detached std::thread workers.
  4. Start scheduler with vTaskStartScheduler().

How this is used:

  • Build and link with Pico SDK, FreeRTOS kernel, and FreeRTOS-cpp11.
  • Flash to Pico 2 W to validate C++ thread mapping onto FreeRTOS and LED operation.
Returns
Never returns under normal conditions.
Here is the call graph for this function:

◆ pico_led_init()

int pico_led_init ( void  )
Here is the caller graph for this function:

◆ pico_set_led()

void pico_set_led ( bool  led_on)

Set the onboard LED state.

Usage:

  • Call after pico_led_init() returns PICO_OK.
  • Pass true to switch LED on, false to switch LED off.
Parameters
led_onDesired LED state.
Here is the caller graph for this function:

Variable Documentation

◆ led_thread

std::thread led_thread

Global LED worker thread handle.

Created in BaseClass::startup() and detached immediately.

◆ log_thread

std::thread log_thread

Global logger worker thread handle.

Created in BaseClass::startup() and detached immediately.