ra8-firmware 0.1.0
Bare-metal firmware for the Renesas RA8 family (RA8D2 / RA8P1)
Loading...
Searching...
No Matches
main.c
Go to the documentation of this file.
1
42
43#include <stdint.h>
44
45#include "ra8_board_ek_ra8d2.h"
46#include "ra8_boot_entry.h"
47#include "ra8_cgc.h"
48#include "ra8_err.h"
49#include "ra8_isr.h"
50#include "ra8_lpm.h"
51#include "ra8_mstp.h"
52#include "ra8_power_profile.h"
53#include "ra8_time.h"
54
56typedef enum : uint32_t {
57 k_pp_demo_baud = 115200U,
62
70
74static const uint8_t k_pp_demo_pass_msg[] = "pp: profile OK\r\n";
75
82static void pp_demo_panic_halt(void)
83{
84 while (1) {
85 __asm__ volatile("wfi");
86 }
87}
88
99static uint64_t pp_demo_now_us(void* ctx)
100{
101 (void)ctx;
102 return (uint64_t)ra8_time_ms() * (uint64_t)k_pp_demo_us_per_ms;
103}
104
106static void pp_demo_clocks_or_halt(void)
107{
108 uint32_t cpuclk0_hz = 0U;
109 if (ra8_cgc_init() != k_ra8_ok) {
111 }
114 }
115 if (ra8_mstp_init() != k_ra8_ok) {
117 }
118 if (ra8_time_init(cpuclk0_hz) != k_ra8_ok) {
120 }
123 }
124}
125
127static void pp_demo_modules_or_halt(void)
128{
129 const ra8_lpm_config_t lpm_cfg = {
130 .io_port_keep = false,
131 .opa_bus_keep = true,
132 .sscr_fast_return = true,
133 .dcdc_softstart = k_ra8_lpm_dcssmode_128us,
134 .sscr_low_power = k_ra8_lpm_ss2lp_default,
135 };
136 if (ra8_lpm_init(&lpm_cfg) != k_ra8_ok) {
138 }
139 const ra8_power_profile_config_t pp_cfg = {
140 .pulse = nullptr,
141 .now_us = pp_demo_now_us,
142 .user_ctx_gpio = nullptr,
143 .user_ctx_time = nullptr,
144 };
145 if (ra8_power_profile_init(&pp_cfg) != k_ra8_ok) {
147 }
150 }
153 }
154}
155
161
179static bool pp_demo_cycle_modes(void)
180{
182 return false;
183 }
184 for (volatile uint32_t i = 0U; i < (uint32_t)k_pp_demo_busy_iters; i++) {
185 /* Busy spin so the analyser sees a clear ACTIVE shape. */
186 }
188 return false;
189 }
190
192 return false;
193 }
195 return false;
196 }
198 return false;
199 }
200
201 /* Deep Sleep and Software Standby skipped: in both modes the CPU
202 * clock (and therefore SysTick) is gated, so the demo has no wake
203 * source and ra8_lpm_enter_sleep hangs WFI indefinitely. Configuring
204 * AGT/RTC as a wake source would require deeper board-specific
205 * setup. The "pp: a=" stats line still prints with deep_standby and
206 * software_standby region times = 0. */
207 return true;
208}
209
221static uint8_t pp_demo_uint_to_dec(uint8_t* buf, uint64_t v)
222{
223 uint8_t tmp[k_pp_demo_uint_dec_max];
224 uint8_t n = 0U;
225 uint64_t r = v;
226 if (r == 0U) {
227 buf[0] = (uint8_t)k_pp_demo_ascii_zero;
228 return 1U;
229 }
230 while ((r > 0U) && (n < (uint8_t)k_pp_demo_uint_dec_max)) {
231 tmp[n] = (uint8_t)((uint8_t)k_pp_demo_ascii_zero + (uint8_t)(r % (uint64_t)k_pp_demo_dec_base));
232 r = r / (uint64_t)k_pp_demo_dec_base;
233 n++;
234 }
235 for (uint8_t i = 0U; i < n; i++) {
236 buf[i] = tmp[(uint8_t)(n - 1U - i)];
237 }
238 return n;
239}
240
255static uint32_t pp_demo_append(uint8_t* buf, uint32_t off, const char* tag, uint64_t val)
256{
257 uint32_t i = 0U;
258 while (tag[i] != '\0') {
259 buf[off++] = (uint8_t)tag[i];
260 i++;
261 }
262 off += pp_demo_uint_to_dec(&buf[off], val);
263 return off;
264}
265
278static uint32_t pp_demo_format_line(uint8_t* out, const ra8_power_profile_stats_t* stats)
279{
280 uint32_t off = 0U;
281 off = pp_demo_append(out,
282 off,
283 "pp: a=",
285 off =
287 off = pp_demo_append(out,
288 off,
289 " d=",
291 off = pp_demo_append(out,
292 off,
293 " st=",
295 const uint8_t suffix[] = " us\r\n";
296 for (uint32_t i = 0U; i < sizeof(suffix) - 1U; i++) {
297 out[off++] = suffix[i];
298 }
299 return off;
300}
301
313void main(void)
314{
317
318 while (1) {
319 bool profile_ok = true;
320 if (!pp_demo_cycle_modes()) {
321 profile_ok = false;
323 /* Emit a FAIL banner over SCI so the HIL negative regex catches
324 * a silently-broken profile_mark_enter/exit path; without this
325 * line the test would pass on a chip where the LPM tracker was
326 * never actually transitioning regions. */
327 const uint8_t fail_banner[] = "pp: FAIL cycle_modes\r\n";
328 (void)ra8_board_uart_console_write(fail_banner, (size_t)(sizeof(fail_banner) - 1U));
329 }
330 ra8_power_profile_stats_t stats = {};
331 if (ra8_power_profile_get_stats(&stats) != k_ra8_ok) {
332 profile_ok = false;
334 const uint8_t stats_fail[] = "pp: FAIL get_stats\r\n";
335 (void)ra8_board_uart_console_write(stats_fail, (size_t)(sizeof(stats_fail) - 1U));
336 }
337 uint8_t out[k_pp_demo_print_buf] = {};
338 const uint32_t off = pp_demo_format_line(out, &stats);
339 if (ra8_board_uart_console_write(out, (size_t)off) != k_ra8_ok) {
340 break;
341 }
342 /* Single-condition guard (no compound decision): the verdict prints
343 * only when neither failure branch above flipped profile_ok. */
344 if (profile_ok) {
346 (size_t)(sizeof(k_pp_demo_pass_msg) - 1U));
347 }
349 break;
350 }
352 }
353
355}
void main(void)
Secure fallback main entry point.
Definition main.c:37
pp_demo_const_t
App-wide tunables.
Definition main.c:56
@ k_pp_demo_baud
Pp demo baud.
Definition main.c:57
@ k_pp_demo_us_per_ms
Pp demo us per ms.
Definition main.c:60
@ k_pp_demo_busy_iters
Pp demo busy iters.
Definition main.c:59
@ k_pp_demo_period_ms
Pp demo period ms.
Definition main.c:58
static void pp_demo_panic_halt(void)
Park forever after a fatal init failure.
Definition main.c:82
static bool pp_demo_cycle_modes(void)
Cycle through ACTIVE -> SLEEP -> DEEP_SLEEP -> SOFTWARE_STANDBY, marking entry/exit on each.
Definition main.c:179
static void pp_demo_setup_or_halt(void)
Definition main.c:156
static uint64_t pp_demo_now_us(void *ctx)
Time-base hook for the profiler – wraps ra8_time_ms to us.
Definition main.c:99
static uint8_t pp_demo_uint_to_dec(uint8_t *buf, uint64_t v)
Write a decimal uint64 (low 32 bits) into buf.
Definition main.c:221
static const uint8_t k_pp_demo_pass_msg[]
Success verdict line emitted only when a full mode cycle and the stats read both returned k_ra8_ok (n...
Definition main.c:74
static uint32_t pp_demo_append(uint8_t *buf, uint32_t off, const char *tag, uint64_t val)
Append tag then the decimal of val to buf at off.
Definition main.c:255
static void pp_demo_clocks_or_halt(void)
Bring CGC + SysTick + MSTP + UART console up.
Definition main.c:106
pp_demo_byte_t
Single-byte ASCII conversion constants.
Definition main.c:64
@ k_pp_demo_dec_base
Pp demo dec base.
Definition main.c:66
@ k_pp_demo_ascii_zero
Pp demo ascii zero.
Definition main.c:65
@ k_pp_demo_uint_dec_max
Max digits in a uint32 base-10.
Definition main.c:67
@ k_pp_demo_print_buf
Max bytes in one print line.
Definition main.c:68
static void pp_demo_modules_or_halt(void)
Bring LPM + power profiler + LEDs up; panic-halts on fail.
Definition main.c:127
static uint32_t pp_demo_format_line(uint8_t *out, const ra8_power_profile_stats_t *stats)
Format the per-cycle stats line into out.
Definition main.c:278
Board-support layer for the Renesas EK-RA8D2 v1 evaluation kit.
ra8_err_t ra8_board_led_toggle(ra8_board_led_id_t led)
Toggle led's output state.
ra8_err_t ra8_board_led_init(ra8_board_led_id_t led)
Configure led as a digital output, initial level low (off).
ra8_err_t ra8_board_led_on(ra8_board_led_id_t led)
Drive led HIGH (light it).
@ k_ra8_board_led2
LED2, GREEN, P303 (jumper E26).
@ k_ra8_board_led1
LED1, BLUE, P600 (jumper E27).
ra8_err_t ra8_board_uart_console_write(const uint8_t *data, size_t len)
Polled blocking write to the J-Link OB VCOM console.
ra8_err_t ra8_board_uart_console_init(uint32_t baud)
Configure SCI8 + PD02/PD03 as the debug-console UART.
Boot entry points shared between a vector table and its startup code.
High-level Clock Generation Circuit driver.
ra8_err_t ra8_cgc_get_clock_hz(ra8_clock_id_t id, uint32_t *out_hz)
Query the current frequency of a clock-tree domain.
Definition ra8_cgc.c:132
@ k_ra8_clock_id_cpuclk0
Cortex-M85 CPUCLK0.
Definition ra8_cgc.h:70
ra8_err_t ra8_cgc_init(void)
Configure the clock tree to a safe default.
Definition ra8_cgc.c:727
Error Code Definitions for ra8-firmware.
@ k_ra8_ok
Success – operation completed with all postconditions satisfied.
Definition ra8_err.h:119
NVIC + ICU IELSR allocator.
void ra8_isr_globals_enable(void)
Globally enable maskable interrupts (PRIMASK = 0).
Definition ra8_isr.c:439
Low Power Mode (LPM) HAL driver – public API.
ra8_err_t ra8_lpm_init(const ra8_lpm_config_t *cfg)
Initialise the LPM block from a config descriptor.
Definition ra8_lpm.c:358
ra8_err_t ra8_lpm_enter_sleep(ra8_sleep_mode_t mode)
Programme LPSCR for mode and enter the requested sleep.
Definition ra8_lpm.c:735
@ k_ra8_lpm_ss2lp_default
SS2LP_0 – default mode.
@ k_ra8_sleep_mode_sleep
CPU sleep, peripherals run.
@ k_ra8_lpm_dcssmode_128us
128 us soft-start (cold-reset default).
Ref-counted Module Stop Control wrapper for the RA8D2.
ra8_err_t ra8_mstp_init(void)
Re-establish the ra8_mstp ref-count table from current hardware state.
Definition ra8_mstp.c:291
Power-profiling helper – public API.
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_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.
@ k_ra8_power_profile_region_deep_standby
Deep-software-standby mode.
@ k_ra8_power_profile_region_active
CPU active / running mode.
@ k_ra8_power_profile_region_sleep
Sleep mode (CPU clock stopped).
@ k_ra8_power_profile_region_software_standby
Software-standby mode.
ra8_err_t ra8_power_profile_init(const ra8_power_profile_config_t *cfg)
Initialise the power profiler.
SysTick-based tick counter, delay and timestamp helpers.
ra8_err_t ra8_time_init(uint32_t cpu_hz)
Initialise SysTick for a 1 kHz tick interrupt.
Definition ra8_time.c:59
void ra8_delay_ms(uint32_t ms)
Busy-wait for at least ms milliseconds.
Definition ra8_time.c:129
uint32_t ra8_time_ms(void)
Get the current 1 kHz tick count.
Definition ra8_time.c:108
Configuration descriptor for ra8_lpm_init.
Definition ra8_lpm.h:96
Initialisation configuration.
uint64_t total_time_us
Sum of (exit - enter) over all closed pairs, microseconds.
Aggregate snapshot of all per-region accumulators.
ra8_power_profile_region_stats_t regions[k_ra8_power_profile_max_regions]
Per-region accumulators.