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
55
56#include <stdint.h>
57
58#include "ra8_attributes.h"
59#include "ra8_bkup.h"
60#include "ra8_board_ek_ra8d2.h"
61#include "ra8_boot_entry.h"
62#include "ra8_cgc.h"
63#include "ra8_check.h"
64#include "ra8_err.h"
65#include "ra8_isr.h"
66#include "ra8_mstp.h"
67#include "ra8_time.h"
68
70static const char* s_tag = "bkup_demo";
71
73typedef enum : uint32_t {
74 k_bkup_demo_baud = 115200U,
76 k_bkup_demo_sentinel = 0x600DCAFEU,
77 k_bkup_demo_pat_mult = 0x01010101U,
78 k_bkup_demo_pat_xor = 0xA5A5A5A5U,
80
87
95static const uint8_t s_bkup_demo_ok_msg[] = "bkup: rw=ok ";
103static const uint8_t s_bkup_demo_bad_msg[] = "bkup: rw=BAD ";
111static const uint8_t s_bkup_demo_surv_y[] = "survived=Y ";
119static const uint8_t s_bkup_demo_surv_n[] = "survived=N ";
127static const uint8_t s_bkup_demo_crlf[] = "\r\n";
128
135volatile uint32_t g_bkup_rw_ok = 0U;
136
143volatile uint32_t g_bkup_survived = 0U;
144
151volatile uint32_t g_bkup_boot_count = 0U;
152
159volatile uint32_t g_bkup_heartbeat = 0U;
160
172[[noreturn]] RA8_INTERNAL static void internal_bkup_demo_panic_halt(void)
173{
174 while (1) {
175 __asm__ volatile("wfi");
176 }
177}
178
193RA8_INTERNAL static uint32_t internal_bkup_demo_pattern(uint8_t i)
194{
195 return ((uint32_t)i * (uint32_t)k_bkup_demo_pat_mult) ^ (uint32_t)k_bkup_demo_pat_xor;
196}
197
210{
211 uint32_t cpuclk0_hz = 0U;
212
213 if (ra8_cgc_init() != k_ra8_ok) {
215 }
218 }
219 if (ra8_mstp_init() != k_ra8_ok) {
221 }
222 if (ra8_time_init(cpuclk0_hz) != k_ra8_ok) {
224 }
227 }
230 }
233 }
234
235 /* Bring the VBATT backup-register block up before any VBTBKRn touch. Two
236 * silicon preconditions must both hold or every VBTBKRn write is dropped:
237 *
238 * 1. Voltage monitor 0 (LVD0) reset must be enabled -- OFS1.PVDAS = 0
239 * (HUM Ch 12.1.3 p 499: "It is necessary to enable voltage monitor 0
240 * resets to use the battery backup function"; Ch 12.3.2 p 514). This
241 * is an option byte set in CMakeLists.txt (OFS1 = 0xFFFFFFF0); without
242 * it the VBATT area stays held in VBATT_POR reset (VBPORF = 1) and the
243 * whole block -- VBTBPCR1, VBTBKRn -- rejects writes. This is the
244 * dominant #131 root cause, verified on the bench by debugger reads.
245 * 2. VBTBER.VBAE must be 1 before access -- HUM Ch 12.2.6 p 504 ("You
246 * must write 1 to VBAE before accessing VBTBKR", then "wait for at
247 * least 500 ns"). VBAE resets to 1, but ra8_bkup_init writes it and
248 * performs the mandatory settle so the sequence is explicit/correct.
249 *
250 * On the EK-RA8D2 VBATT is tied to VCC, so the battery power-supply switch
251 * stays stopped (enable_switch = false). The window stays open for the life
252 * of the demo (a J-Link/SYSRESETREQ reset keeps VCC/VBATT powered, so
253 * leaving VBAE = 1 does not lose data). */
254 const ra8_bkup_config_t bkup_cfg = {
255 .vdet_level = k_ra8_bkup_vdet_2p80v,
256 .enable_switch = false,
257 .enable_backup = true,
258 };
259 if (ra8_bkup_init(&bkup_cfg) != k_ra8_ok) {
261 }
262}
263
283[[nodiscard]] RA8_INTERNAL static ra8_err_t internal_bkup_demo_rw_check(uint8_t* out_ok)
284{
285 RA8_CHECK_NULL_PTR(out_ok, s_tag, "out_ok must not be nullptr");
286
287 for (uint8_t i = 0U; i < (uint8_t)k_bkup_demo_data_words; ++i) {
289 if (werr != k_ra8_ok) {
290 return werr;
291 }
292 }
293 uint8_t ok = 1U;
294 for (uint8_t i = 0U; i < (uint8_t)k_bkup_demo_data_words; ++i) {
295 uint32_t got = 0U;
296 const ra8_err_t rerr = ra8_bkup_read_word(i, &got);
297 if (rerr != k_ra8_ok) {
298 return rerr;
299 }
300 if (got != internal_bkup_demo_pattern(i)) {
301 ok = 0U;
302 }
303 }
304 *out_ok = ok;
305 return k_ra8_ok;
306}
307
333{
334 uint32_t sentinel = 0U;
335 const ra8_err_t serr = ra8_bkup_read_word((uint8_t)k_bkup_demo_idx_sentinel, &sentinel);
336 if (serr != k_ra8_ok) {
337 return serr;
338 }
339 uint32_t boot = 0U;
340 if (sentinel == (uint32_t)k_bkup_demo_sentinel) {
341 g_bkup_survived = 1U;
342 const ra8_err_t rerr = ra8_bkup_read_word((uint8_t)k_bkup_demo_idx_boot, &boot);
343 if (rerr != k_ra8_ok) {
344 return rerr;
345 }
346 boot += 1U;
347 } else {
348 g_bkup_survived = 0U;
349 boot = 1U;
350 const ra8_err_t merr =
352 if (merr != k_ra8_ok) {
353 return merr;
354 }
355 }
356 const ra8_err_t cerr = ra8_bkup_write_word((uint8_t)k_bkup_demo_idx_boot, boot);
357 if (cerr != k_ra8_ok) {
358 return cerr;
359 }
360 g_bkup_boot_count = boot;
361 return k_ra8_ok;
362}
363
376{
377 const uint8_t* tag = (g_bkup_survived != 0U) ? s_bkup_demo_surv_y : s_bkup_demo_surv_n;
378 /* Both suffixes are the same length ("survived=Y " / "survived=N "). */
379 static_assert(sizeof(s_bkup_demo_surv_y) == sizeof(s_bkup_demo_surv_n),
380 "survived=Y / survived=N suffixes must be equal length");
381 const uint32_t len = (uint32_t)(sizeof(s_bkup_demo_surv_y) - 1U);
382 (void)ra8_board_uart_console_write(tag, (size_t)len);
383}
384
385void main(void)
386{
388 /* Clear PRIMASK so SysTick can dispatch and ra8_delay_ms() uses the
389 * SysTick path (ra8_emulator does not advance DWT_CYCCNT). No NVIC sources
390 * are armed by this demo. */
392
395 }
396
397 while (1) {
398 uint8_t rw = 0U;
399 const ra8_err_t err = internal_bkup_demo_rw_check(&rw);
400 const uint8_t good = (err == k_ra8_ok && rw != 0U) ? 1U : 0U;
401 g_bkup_rw_ok = (uint32_t)good;
402
403 if (good != 0U) {
405 (size_t)(sizeof(s_bkup_demo_ok_msg) - 1U));
407 } else {
409 (size_t)(sizeof(s_bkup_demo_bad_msg) - 1U));
411 }
416 }
418}
void main(void)
Secure fallback main entry point.
Definition main.c:37
static ra8_err_t internal_bkup_demo_survival_check(void)
Detect reset survival and maintain the boot counter.
Definition main.c:332
static void internal_bkup_demo_setup_or_halt(void)
Initialize clocks, time, console, LEDs, and the backup domain.
Definition main.c:209
static const uint8_t s_bkup_demo_crlf[]
Console line terminator for each periodic report.
Definition main.c:127
volatile uint32_t g_bkup_rw_ok
1 when words 0..29 read back exactly what was written.
Definition main.c:135
volatile uint32_t g_bkup_heartbeat
Bumps once per main-loop pass – liveness for headless probes.
Definition main.c:159
static const uint8_t s_bkup_demo_surv_n[]
Cold-start survival suffix.
Definition main.c:119
static void internal_bkup_demo_report_survival(void)
Emit the fixed-width reset-survival suffix over the console.
Definition main.c:375
static const uint8_t s_bkup_demo_surv_y[]
Warm-reset survival suffix.
Definition main.c:111
bkup_demo_layout_t
Backup-word layout (32 words total; 0..29 data, 30/31 reserved).
Definition main.c:82
@ k_bkup_demo_idx_boot
Boot-counter word index.
Definition main.c:85
@ k_bkup_demo_data_words
Count of rw-pattern words (0..29).
Definition main.c:83
@ k_bkup_demo_idx_sentinel
Reset-survival sentinel word index.
Definition main.c:84
static const uint8_t s_bkup_demo_ok_msg[]
Prefix for a successful backup-word read/write report.
Definition main.c:95
static void internal_bkup_demo_panic_halt(void)
Park forever after a fatal initialization failure.
Definition main.c:172
static ra8_err_t internal_bkup_demo_rw_check(uint8_t *out_ok)
Write words 0..29 with the pattern, read them back, and compare.
Definition main.c:283
volatile uint32_t g_bkup_boot_count
Boot counter held in backup word 31 (increments per reset on HW).
Definition main.c:151
static uint32_t internal_bkup_demo_pattern(uint8_t i)
Derive the deterministic pattern for one backup word.
Definition main.c:193
bkup_demo_config_t
Compile-time settings.
Definition main.c:73
@ k_bkup_demo_period_ms
Delay between reports.
Definition main.c:75
@ k_bkup_demo_pat_mult
Per-word pattern multiplier.
Definition main.c:77
@ k_bkup_demo_pat_xor
Per-word pattern XOR mask.
Definition main.c:78
@ k_bkup_demo_sentinel
"good cafe" reset-survival marker.
Definition main.c:76
@ k_bkup_demo_baud
SCI8 console baud rate.
Definition main.c:74
static const uint8_t s_bkup_demo_bad_msg[]
Prefix for a failed backup-word read/write report.
Definition main.c:103
volatile uint32_t g_bkup_survived
1 when the sentinel was found at boot (state retained across reset).
Definition main.c:143
static const char * s_tag
Logging / check tag.
Definition ra8_app.c:17
Annotation-attribute framework macros for ra8-firmware.
#define RA8_INTERNAL
Marker that a function is intended to be static (file-local).
Battery Backup Function (VBATT) HAL driver – public API.
ra8_err_t ra8_bkup_write_word(uint8_t word_index, uint32_t value)
Write one 32-bit word into the VBTBKRn array.
Definition ra8_bkup.c:487
ra8_err_t ra8_bkup_init(const ra8_bkup_config_t *cfg)
Configure the battery-backup block from cfg.
Definition ra8_bkup.c:222
@ k_ra8_bkup_vdet_2p80v
000b: 2.80 V (default after reset).
Definition ra8_bkup.h:66
ra8_err_t ra8_bkup_read_word(uint8_t word_index, uint32_t *out)
Read one 32-bit word from the VBTBKRn array.
Definition ra8_bkup.c:474
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).
@ 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
Validation and Error-Checking Macros for ra8-firmware.
#define RA8_CHECK_NULL_PTR(ptr, tag, message)
Reject nullptr pointer, returning k_ra8_err_null_ptr.
Definition ra8_check.h:243
Error Code Definitions for ra8-firmware.
@ 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
NVIC + ICU IELSR allocator.
void ra8_isr_globals_enable(void)
Globally enable maskable interrupts (PRIMASK = 0).
Definition ra8_isr.c:439
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
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
Configuration descriptor passed to ra8_bkup_init.
Definition ra8_bkup.h:147