|
ELEC-C7222
Libraries for ELEC C7222 Course Work
|
This document introduces the ELEC_C7222 devices layer, its design principles, and how to use it across the two supported platforms. It is intended to help students and teaching staff understand what each device abstraction does, how platform-specific behavior is isolated, and how to use the APIs correctly.
For board-level interfaces and pin assignments, see Board Reference.
The devices module provides a small, consistent set of abstractions for GPIO, LEDs, buttons, on-board LED control, and the on-chip temperature sensor. It aims to:
GpioIn and GpioOut are distinct classes. Input-only and output-only operations are deliberately separated to prevent misuse and reduce ambiguous configuration.GpioIn or GpioOut instance at a time. Platform backends enforce this via internal maps and assertions.Platform::Initialize()). Device initialization is also explicit for OnBoardLED and OnChipTemperatureSensor. This avoids hidden side effects and keeps ordering clear.platform/rpi_pico (real hardware) and platform/grader (simulated). Device headers and src/ code remain platform-agnostic.Key headers in libs/elec_c7222/devices/include:
gpio.hpp GpioIn, GpioOut, and configuration enums. This is the core GPIO abstraction.led.hpp Led, an output-only wrapper that provides On(), Off(), and Toggle().button.hpp Button, an input-only wrapper that provides IsPressed() and optional IRQ usage through GpioIn.onboard_led.hpp OnBoardLED, a singleton for the built-in board LED.onchip_temperature_sensor.hpp OnChipTemperatureSensor, a singleton for the RP2040 temperature sensor.c7222_pico_w_board.hpp PicoWBoard, which maps logical LED and button IDs to board GPIOs.platform.hpp Platform, a singleton that coordinates platform initialization and offers convenience accessors for LEDs/buttons.pwm.hpp PwmOut, a minimal PWM output wrapper with period and duty-cycle configuration.PwmOut provides a minimal PWM interface suitable for LED dimming and simple duty‑cycle control. It exposes:
Ownership rules:
Enable(false) (or destroy the object) to release the pin back to GPIO.Led and PwmOut on the same pin at the same time.Located in libs/elec_c7222/devices/platform/rpi_pico/.
gpio.cpp implements GpioIn/GpioOut using the Pico SDK.onboard_led.cpp drives the on-board LED (either default LED pin or CYW43 LED).onchip_temperature_sensor.cpp configures the ADC and reads temperature values.platform.cpp performs architecture initialization (e.g., CYW43) and exposes device accessors.Located in libs/elec_c7222/devices/platform/grader/.
gpio.cpp provides stub behavior so code compiles without hardware.onboard_led.cpp and onchip_temperature_sensor.cpp return simulated results.platform.cpp mirrors the interface and init flow without hardware dependencies.OnBoardLED and OnChipTemperatureSensor are not auto-initialized. Users must call Initialize() explicitly.PicoWBoard construction performs its own initialization, so it should only be created after platform initialization.Platform::Initialize() from main(). main() can run before the RTOS scheduler starts, so BLE stack init must be performed from a FreeRTOS task context.There is also a simple C-only API for board GPIOs in c7222_pico_w_board.h. It is intended for C code; C++ users should prefer the c7222_pico_w_board.hpp API and device classes.