ra8-firmware 0.1.0
Bare-metal firmware for the Renesas RA8 family (RA8D2 / RA8P1)
Loading...
Searching...
No Matches
ra8_power_profile.c
Go to the documentation of this file.
1
18
19#include "ra8_power_profile.h"
20
21#include <string.h>
22
23#include "ra8_attributes.h"
24#include "ra8_check.h"
25#include "ra8_err.h"
26#include "ra8_log.h"
27
40#define RA8_POWER_PROFILE_TAG "PWRPROF"
41
58static bool s_initialized = false;
59
76
94
119{
120 if ((uint8_t)region_id >= (uint8_t)k_ra8_power_profile_max_regions) {
121 ra8_log_error(RA8_POWER_PROFILE_TAG, "region id out of range");
123 }
124 return k_ra8_ok;
125}
126
148RA8_INTERNAL static uint64_t internal_now_us(void)
149{
150 if (s_cfg.now_us == nullptr) {
151 return 0U;
152 }
153 return s_cfg.now_us(s_cfg.user_ctx_time);
154}
155
176{
177 if (s_cfg.pulse == nullptr) {
178 return;
179 }
180 s_cfg.pulse(s_cfg.user_ctx_gpio, region_id, entering);
181}
182
184{
185 RA8_CHECK_NULL_PTR(cfg, RA8_POWER_PROFILE_TAG, "cfg must not be nullptr");
186
187 s_cfg = *cfg;
189 s_initialized = true;
190 return k_ra8_ok;
191}
192
194{
196
197 const ra8_err_t range_err = internal_validate_region(region_id);
198 if (range_err != k_ra8_ok) {
199 return range_err;
200 }
201
202 ra8_power_profile_region_stats_t* slot = &s_stats.regions[(uint8_t)region_id];
203 slot->entries += 1U;
205 slot->is_open = true;
206
207 internal_fire_pulse(region_id, true);
208 return k_ra8_ok;
209}
210
212{
214
215 const ra8_err_t range_err = internal_validate_region(region_id);
216 if (range_err != k_ra8_ok) {
217 return range_err;
218 }
219
220 ra8_power_profile_region_stats_t* slot = &s_stats.regions[(uint8_t)region_id];
221 slot->exits += 1U;
222
223 ra8_err_t ret = k_ra8_ok;
224 if (slot->is_open) {
225 const uint64_t now = internal_now_us();
226 if (now >= slot->last_enter_us) {
227 slot->total_time_us += (now - slot->last_enter_us);
228 }
229 slot->is_open = false;
230 } else {
231 ra8_log_error(RA8_POWER_PROFILE_TAG, "exit without matching enter");
233 }
234
235 internal_fire_pulse(region_id, false);
236 return ret;
237}
238
240{
242 RA8_CHECK_NULL_PTR(out_stats, RA8_POWER_PROFILE_TAG, "out_stats must not be nullptr");
243
244 (void)memcpy(out_stats, &s_stats, sizeof(*out_stats));
245 return k_ra8_ok;
246}
247
Annotation-attribute framework macros for ra8-firmware.
#define RA8_INTERNAL
Marker that a function is intended to be static (file-local).
Validation and Error-Checking Macros for ra8-firmware.
#define RA8_VALIDATE_INIT(initialized, tag, message)
Precondition: module must be initialized.
Definition ra8_check.h:334
#define RA8_CHECK_NULL_PTR(ptr, tag, message)
Reject nullptr pointer, returning k_ra8_err_null_ptr.
Definition ra8_check.h:243
static bool s_initialized
True once display_init has succeeded.
Error Code Definitions for ra8-firmware.
@ k_ra8_err_invalid_state
Module in wrong state for requested operation.
Definition ra8_err.h:161
@ k_ra8_err_range_check_failed
Value outside range enforced by RA8_CHECK_RANGE / RA8_CHECK_RANGE_TAG.
Definition ra8_err.h:471
@ k_ra8_ok
Success – operation completed with all postconditions satisfied.
Definition ra8_err.h:119
ra8_err_codes_t ra8_err_t
Canonical error-return type used by every ra8-firmware API.
Definition ra8_err.h:546
void * memcpy(void *dst, const void *src, size_t n)
Copy memory area between non-overlapping regions.
Lightweight Logging Interface for ra8-firmware.
#define ra8_log_error(tag, message)
RA8 log error.
Definition ra8_log.h:335
ra8_err_t ra8_power_profile_get_stats(ra8_power_profile_stats_t *out_stats)
Copy the current statistics snapshot to the caller.
static ra8_power_profile_config_t s_cfg
Cached copy of the caller-supplied configuration.
ra8_err_t ra8_power_profile_mark_enter(ra8_power_profile_region_id_t region_id)
Mark entry into a tracked region.
static void internal_fire_pulse(ra8_power_profile_region_id_t region_id, bool entering)
Fire the GPIO pulse hook if one was configured.
static uint64_t internal_now_us(void)
Read the current timestamp via the configured clock hook.
static ra8_power_profile_stats_t s_stats
Per-region accumulator array (NASA Rule 3 – static).
static ra8_err_t internal_validate_region(ra8_power_profile_region_id_t region_id)
Validate a region ID against the static array bound.
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_reset_stats(void)
Zero every accumulator (preserves the configured hooks).
ra8_err_t ra8_power_profile_init(const ra8_power_profile_config_t *cfg)
Initialise the power profiler.
#define RA8_POWER_PROFILE_TAG
Logging tag passed to ra8_log_* from this TU.
Power-profiling helper – public API.
ra8_power_profile_region_id_t
Default named region IDs.
@ k_ra8_power_profile_max_regions
Maximum number of distinct regions tracked.
Initialisation configuration.
Per-region accumulator returned by ra8_power_profile_get_stats.
uint64_t last_enter_us
Timestamp of the most recent unmatched mark_enter.
uint64_t total_time_us
Sum of (exit - enter) over all closed pairs, microseconds.
bool is_open
true if a mark_enter is awaiting a mark_exit.
uint64_t entries
Total number of mark_enter calls for this region.
uint64_t exits
Total number of mark_exit calls for this region.
Aggregate snapshot of all per-region accumulators.