|
ra8-firmware 0.1.0
Bare-metal firmware for the Renesas RA8 family (RA8D2 / RA8P1)
|
Power-profiling helper – implementation. More...
#include "ra8_power_profile.h"#include <string.h>#include "ra8_attributes.h"#include "ra8_check.h"#include "ra8_err.h"#include "ra8_log.h"Go to the source code of this file.
Macros | |
| #define | RA8_POWER_PROFILE_TAG "PWRPROF" |
| Logging tag passed to ra8_log_* from this TU. | |
Functions | |
| static ra8_err_t | internal_validate_region (ra8_power_profile_region_id_t region_id) |
| Validate a region ID against the static array bound. | |
| static uint64_t | internal_now_us (void) |
| Read the current timestamp via the configured clock hook. | |
| static void | internal_fire_pulse (ra8_power_profile_region_id_t region_id, bool entering) |
| Fire the GPIO pulse hook if one was configured. | |
| 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). | |
Variables | |
| static bool | s_initialized = false |
| Module initialisation flag. | |
| static ra8_power_profile_config_t | s_cfg = {} |
| Cached copy of the caller-supplied configuration. | |
| static ra8_power_profile_stats_t | s_stats = {} |
| Per-region accumulator array (NASA Rule 3 – static). | |
Power-profiling helper – implementation.
See ra8_power_profile.h for the public contract. The implementation is a flat array of accumulators plus two function-pointer hooks (GPIO pulse, microsecond clock). Nothing in this file touches the RA8D2 register map directly; that decoupling keeps the helper trivially testable on the host.
Definition in file ra8_power_profile.c.
| #define RA8_POWER_PROFILE_TAG "PWRPROF" |
Logging tag passed to ra8_log_* from this TU.
Centralised so all log lines emitted by the profiler use a single, grep-friendly prefix. Allowed by CLAUDE.md "Constants and Macros" because it deduplicates the same string literal across multiple callsites.
Definition at line 40 of file ra8_power_profile.c.
Referenced by internal_validate_region(), ra8_power_profile_get_stats(), ra8_power_profile_init(), ra8_power_profile_mark_enter(), ra8_power_profile_mark_exit(), and ra8_power_profile_reset_stats().
|
static |
Fire the GPIO pulse hook if one was configured.
Wrapper around the function-pointer dispatch so the public APIs can call a single helper instead of repeating the null guard.
| [in] | region_id | Region whose edge is being marked. |
| [in] | entering | true for an enter edge, false for exit. |
Definition at line 175 of file ra8_power_profile.c.
References RA8_INTERNAL, and s_cfg.
Referenced by ra8_power_profile_mark_enter(), and ra8_power_profile_mark_exit().
|
static |
Read the current timestamp via the configured clock hook.
Returns 0 if no clock hook was supplied. Centralising the null check here keeps the enter/exit fast paths free of branches that never change after init.
| k_ra8_ok | Operation succeeded. |
Definition at line 148 of file ra8_power_profile.c.
References RA8_INTERNAL, and s_cfg.
Referenced by ra8_power_profile_mark_enter(), and ra8_power_profile_mark_exit().
|
static |
Validate a region ID against the static array bound.
Pulled out of the public API path so each entry point has the same single-source-of-truth bounds check, and so coverage of the out-of-range branch only needs to be exercised once.
| [in] | region_id | Region identifier to validate. |
| k_ra8_ok | region_id is in range. |
| k_ra8_err_range_check_failed | region_id is out of range. |
Definition at line 118 of file ra8_power_profile.c.
References k_ra8_err_range_check_failed, k_ra8_ok, k_ra8_power_profile_max_regions, RA8_INTERNAL, ra8_log_error, and RA8_POWER_PROFILE_TAG.
Referenced by ra8_power_profile_mark_enter(), and ra8_power_profile_mark_exit().
| 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.
|
static |
Cached copy of the caller-supplied configuration.
Holds the GPIO pulse hook, microsecond-clock hook, and the two opaque user contexts. Copied verbatim from ra8_power_profile_init's argument so the caller is free to release the source buffer afterwards.
Definition at line 75 of file ra8_power_profile.c.
Referenced by internal_fire_pulse(), internal_now_us(), main(), and ra8_power_profile_init().
|
static |
Module initialisation flag.
Set to true by ra8_power_profile_init and never cleared. Every public API except init rejects calls when this is false via RA8_VALIDATE_INIT.
Definition at line 58 of file ra8_power_profile.c.
|
static |
Per-region accumulator array (NASA Rule 3 – static).
Indexed by ra8_power_profile_region_id_t value. Capacity is k_ra8_power_profile_max_regions (16) so the entire structure fits well within Cortex-M85 SRAM with room to spare.
Definition at line 93 of file ra8_power_profile.c.
Referenced by ra8_power_profile_get_stats(), ra8_power_profile_init(), ra8_power_profile_mark_enter(), ra8_power_profile_mark_exit(), and ra8_power_profile_reset_stats().