|
ra8-firmware 0.1.0
Bare-metal firmware for the Renesas RA8 family (RA8D2 / RA8P1)
|
Power-profiling helper – public API. More...
Go to the source code of this file.
Data Structures | |
| struct | ra8_power_profile_region_stats_t |
| Per-region accumulator returned by ra8_power_profile_get_stats. More... | |
| struct | ra8_power_profile_stats_t |
| Aggregate snapshot of all per-region accumulators. More... | |
| struct | ra8_power_profile_config_t |
| Initialisation configuration. More... | |
Typedefs | |
| typedef void(* | ra8_power_profile_gpio_pulse_fn_t) (void *ctx, ra8_power_profile_region_id_t region_id, bool entering) |
| GPIO-toggle hook used to emit profiler edges. | |
| typedef uint64_t(* | ra8_power_profile_now_us_fn_t) (void *ctx) |
| Wall-clock timestamp hook (microseconds since some epoch). | |
Enumerations | |
| enum | ra8_power_profile_limits_t : uint8_t { k_ra8_power_profile_max_regions = 16 } |
| Static sizing constants for the profiler. More... | |
| enum | ra8_power_profile_region_id_t : uint8_t { k_ra8_power_profile_region_active = 0 , k_ra8_power_profile_region_sleep = 1 , k_ra8_power_profile_region_software_standby = 2 , k_ra8_power_profile_region_deep_standby = 3 , k_ra8_power_profile_region_snooze = 4 , k_ra8_power_profile_region_user_0 = 5 } |
| Default named region IDs. More... | |
Functions | |
| ra8_err_t | ra8_power_profile_init (const ra8_power_profile_config_t *cfg) |
| Initialise the power profiler. | |
| ra8_err_t | ra8_power_profile_mark_enter (ra8_power_profile_region_id_t region_id) |
| Mark entry into a tracked region. | |
| ra8_err_t | ra8_power_profile_mark_exit (ra8_power_profile_region_id_t region_id) |
| Mark exit from a tracked region. | |
| ra8_err_t | ra8_power_profile_get_stats (ra8_power_profile_stats_t *out_stats) |
| Copy the current statistics snapshot to the caller. | |
| ra8_err_t | ra8_power_profile_reset_stats (void) |
| Zero every accumulator (preserves the configured hooks). | |
Power-profiling helper – public API.
Lightweight Dynamic-Trace-Style (DTS) power-profiling helper. The library lets application code wrap regions of execution with ra8_power_profile_mark_enter / ra8_power_profile_mark_exit pairs so that an external power analyser (e.g. a Joulescope or a scope+shunt) can correlate measured current draw with named code regions. Each enter/exit edge pulses a configured GPIO so the analyser sees a clean trigger; on the firmware side the helper also accumulates per-region elapsed time and entry counts, which doubles as a coarse wall-clock budget tracker.
The library is decoupled from any concrete GPIO or RTC HAL via function-pointer hooks (Dependency Inversion – see CLAUDE.md "SOLID Principles for C"). Production code wires the hooks to ra8_gpio_* and ra8_rtc_*; unit tests inject mocks. There are no calls into ra8_hal/ from this translation unit.
Layout in this firmware:
Region IDs are a fixed-size C23 typed enum capped at k_ra8_power_profile_max_regions (16) to satisfy NASA Power-of-10 Rule 3 (no dynamic allocation): all per-region counters live in a static array sized at compile time.
Definition in file ra8_power_profile.h.
| typedef void(* ra8_power_profile_gpio_pulse_fn_t) (void *ctx, ra8_power_profile_region_id_t region_id, bool entering) |
GPIO-toggle hook used to emit profiler edges.
Called twice per region transition (rising + falling edges) so the external analyser can detect a precise boundary. The implementation is expected to drive a single GPIO pin from the configured pulse level back to the idle level.
| [in] | ctx | Opaque hook context (passed through from cfg). |
| [in] | region_id | Region being entered or exited. |
| [in] | entering | true if marking entry, false if marking exit. |
Definition at line 106 of file ra8_power_profile.h.
| typedef uint64_t(* ra8_power_profile_now_us_fn_t) (void *ctx) |
Wall-clock timestamp hook (microseconds since some epoch).
Returns a monotonically non-decreasing 64-bit microsecond timestamp. The profiler subtracts the enter timestamp from the exit timestamp to compute time-in-region. The hook is typically backed by RTC sub-second counters or by AGT/GPT.
| [in] | ctx | Opaque hook context (passed through from cfg). |
Definition at line 127 of file ra8_power_profile.h.
| enum ra8_power_profile_limits_t : uint8_t |
Static sizing constants for the profiler.
Used to bound the per-region accumulator array at compile time (NASA Rule 3). Region IDs must be in [0, k_ra8_power_profile_max_regions).
| Enumerator | |
|---|---|
| k_ra8_power_profile_max_regions | Maximum number of distinct regions tracked. |
Definition at line 61 of file ra8_power_profile.h.
| enum ra8_power_profile_region_id_t : uint8_t |
Default named region IDs.
Applications are free to use raw integer values in [0, k_ra8_power_profile_max_regions) instead of these names. The enumeration provides a few suggested labels that match common low-power-mode regions on the RA8D2: active, sleep, software- standby, deep-software-standby, and snooze.
Definition at line 78 of file ra8_power_profile.h.
| ra8_err_t ra8_power_profile_get_stats | ( | ra8_power_profile_stats_t * | out_stats | ) |
Copy the current statistics snapshot to the caller.
Performs a structure copy of the internal accumulator array. The snapshot reflects all enter/exit calls observed so far, including still-open regions (whose total_time_us has not yet been updated for the current open span).
| [out] | out_stats | Caller-supplied buffer to receive the snapshot. |
| k_ra8_ok | Success. |
| k_ra8_err_null_ptr | out_stats is nullptr. |
| k_ra8_err_not_initialized | Module not initialized. |
Definition at line 239 of file ra8_power_profile.c.
References k_ra8_ok, memcpy(), RA8_CHECK_NULL_PTR, RA8_POWER_PROFILE_TAG, RA8_VALIDATE_INIT, s_initialized, and s_stats.
Referenced by main().
| ra8_err_t ra8_power_profile_init | ( | const ra8_power_profile_config_t * | cfg | ) |
Initialise the power profiler.
Stores the supplied hooks, zeroes every per-region accumulator, and marks the module as initialized. Calling ra8_power_profile_init a second time re-initialises (the previous accumulators are discarded). The function never touches hardware directly; all side effects flow through the hooks in cfg.
Algorithm:
| [in] | cfg | Pointer to a fully-populated config descriptor. |
| k_ra8_ok | Success. |
| k_ra8_err_null_ptr | cfg is nullptr. |
Definition at line 183 of file ra8_power_profile.c.
References k_ra8_ok, RA8_CHECK_NULL_PTR, RA8_POWER_PROFILE_TAG, s_cfg, s_initialized, and s_stats.
Referenced by pp_demo_modules_or_halt().
| ra8_err_t ra8_power_profile_mark_enter | ( | ra8_power_profile_region_id_t | region_id | ) |
Mark entry into a tracked region.
Pulses the configured GPIO with entering=true and records the current timestamp. If a previous enter for the same region had no matching exit, the previous open timestamp is silently overwritten (the entries counter still increments so the imbalance is detectable in the snapshot).
Algorithm:
| [in] | region_id | Region identifier in [0, k_ra8_power_profile_max_regions). |
| k_ra8_ok | Success. |
| k_ra8_err_not_initialized | ra8_power_profile_init was not called. |
| k_ra8_err_range_check_failed | region_id is out of range. |
Definition at line 193 of file ra8_power_profile.c.
References ra8_power_profile_region_stats_t::entries, internal_fire_pulse(), internal_now_us(), internal_validate_region(), ra8_power_profile_region_stats_t::is_open, k_ra8_ok, ra8_power_profile_region_stats_t::last_enter_us, RA8_POWER_PROFILE_TAG, RA8_VALIDATE_INIT, s_initialized, and s_stats.
Referenced by pp_demo_cycle_modes().
| ra8_err_t ra8_power_profile_mark_exit | ( | ra8_power_profile_region_id_t | region_id | ) |
Mark exit from a tracked region.
Pulses the configured GPIO with entering=false, computes the elapsed time since the matching mark_enter (using the time hook), and folds the delta into total_time_us. If no enter is outstanding for region_id the function still increments exits and returns k_ra8_err_invalid_state so the caller can surface the imbalance.
Algorithm:
| [in] | region_id | Region identifier in [0, k_ra8_power_profile_max_regions). |
| k_ra8_ok | Success: matching enter was open. |
| k_ra8_err_invalid_state | No matching open enter for this region. |
| k_ra8_err_not_initialized | ra8_power_profile_init was not called. |
| k_ra8_err_range_check_failed | region_id is out of range. |
Definition at line 211 of file ra8_power_profile.c.
References ra8_power_profile_region_stats_t::exits, internal_fire_pulse(), internal_now_us(), internal_validate_region(), ra8_power_profile_region_stats_t::is_open, k_ra8_err_invalid_state, k_ra8_ok, ra8_power_profile_region_stats_t::last_enter_us, ra8_log_error, RA8_POWER_PROFILE_TAG, RA8_VALIDATE_INIT, s_initialized, s_stats, and ra8_power_profile_region_stats_t::total_time_us.
Referenced by pp_demo_cycle_modes().
| ra8_err_t ra8_power_profile_reset_stats | ( | void | ) |
Zero every accumulator (preserves the configured hooks).
Useful between profiling phases when the application wants a fresh window of measurements without paying the cost of a full re-init.
| k_ra8_ok | Success. |
| k_ra8_err_not_initialized | Module not initialized. |
Definition at line 248 of file ra8_power_profile.c.
References k_ra8_ok, RA8_POWER_PROFILE_TAG, RA8_VALIDATE_INIT, s_initialized, and s_stats.