ra8-firmware 0.1.0
Bare-metal firmware for the Renesas RA8 family (RA8D2 / RA8P1)
Loading...
Searching...
No Matches
main.c File Reference

ra8_psa_crypto_random() dump over SCI8 for the bare EK-RA8D2 EVM More...

#include <stdint.h>
#include "ra8_attributes.h"
#include "ra8_board_ek_ra8d2.h"
#include "ra8_boot_entry.h"
#include "ra8_cgc.h"
#include "ra8_err.h"
#include "ra8_isr.h"
#include "ra8_psa_crypto.h"
#include "ra8_time.h"
Include dependency graph for main.c:

Go to the source code of this file.

Enumerations

enum  rng_demo_config_t : uint32_t {
  k_rng_demo_baud = 115200U ,
  k_rng_demo_period_ms = 1000U
}
 Compile-time settings for the demo. More...
enum  rng_demo_byte_t : uint8_t {
  k_rng_demo_bytes_per_line = 32U ,
  k_rng_demo_hex_per_byte = 2U ,
  k_rng_demo_nibble_mask = 0x0FU ,
  k_rng_demo_nibble_shift = 4U ,
  k_rng_demo_alpha_threshold = 10U
}
 Per-emit byte counts. More...

Functions

static void internal_rng_demo_panic_halt (void)
 Park the processor after a fatal setup or entropy-path failure.
static uint8_t internal_rng_demo_nibble_to_hex (uint8_t nibble)
 Convert a nibble to its ASCII hex representation.
static void internal_rng_demo_setup_or_halt (void)
 Bring CGC, SysTick, SCI8, LED1, and PSA crypto up.
static ra8_err_t internal_rng_demo_emit_one_line (void)
 Emit one hex line of TRNG output.
void main (void)
 The application entry point Reset_Handler hands control to.

Variables

static const uint8_t s_rng_demo_prefix [] = "rng: "
 Fixed prefix and CR/LF tail used on every emit.
static const uint8_t s_rng_demo_eol [] = "\r\n"
static const uint8_t s_rng_demo_pass_msg [] = "rng: PRNG stub OK (deterministic, NOT entropy)\r\n"
 Verdict line emitted only after a non-stuck sample is dumped.

Detailed Description

ra8_psa_crypto_random() dump over SCI8 for the bare EK-RA8D2 EVM

Tag
[Ring 6 / APP] {World: S}

Brings up CGC + SysTick + SCI8 + LED1 + ra8_psa_crypto and once a second pulls 32 bytes from ra8_psa_crypto_random() and emits them as an ASCII hex line on the on-board J-Link OB CDC channel (115200 8N1, TXD8 = PD_02 / RXD8 = PD_03). LED1 toggles once per emit so the heartbeat is also visible without a serial terminal.

Warning
This demo does NOT prove hardware entropy. The RSIP-E50D TRNG has no working register interface on this silicon (see ra8_rsip_trng_read, which fails closed), so ra8_psa_crypto_random here returns a DETERMINISTIC software stub (the same stream every boot), NOT secure random bytes. The verdict line says so explicitly. A green here means only that the API path runs and the stub is not stuck – real entropy needs an FSP-derived RSIP TRNG procedure.

Sequence:

  1. ra8_cgc_init -> CPUCLK0 = 1 GHz, PCLKA = 125 MHz.
  2. ra8_board_uart_console_init(115200) -> routes PD_02/PD_03 and brings up SCI8 at 115200 8N1.
  3. ra8_psa_crypto_init().
  4. Loop forever: ra8_psa_crypto_random(buf, 32) -> emit "rng: <64 hex chars>\r\n" -> toggle LED1 -> ra8_delay_ms(1000).

No external transceiver, board, or harness is required.

Since
0.1.0

Definition in file main.c.

Enumeration Type Documentation

◆ rng_demo_byte_t

enum rng_demo_byte_t : uint8_t

Per-emit byte counts.

Enumerator
k_rng_demo_bytes_per_line 

Rng demo bytes per line.

k_rng_demo_hex_per_byte 

Rng demo hex per byte.

k_rng_demo_nibble_mask 

Rng demo nibble mask.

k_rng_demo_nibble_shift 

Rng demo nibble shift.

k_rng_demo_alpha_threshold 

Rng demo alpha threshold.

Definition at line 55 of file main.c.

◆ rng_demo_config_t

enum rng_demo_config_t : uint32_t

Compile-time settings for the demo.

Enumerator
k_rng_demo_baud 

Rng demo baud.

k_rng_demo_period_ms 

Rng demo period ms.

Definition at line 49 of file main.c.

Function Documentation

◆ internal_rng_demo_emit_one_line()

ra8_err_t internal_rng_demo_emit_one_line ( void )
staticnodiscard

Emit one hex line of TRNG output.

Requests one bounded sample, rejects an all-equal stuck pattern, converts every byte to lowercase hex, and emits the sample plus verdict.

MC/DC:
Compound decision: random != ok || sci_write_prefix != ok || sci_write_payload != ok || sci_write_eol != ok. Four atomic conditions x N+1 = 5 vectors; the all-ok vector is the steady-state runtime path and each error branch is covered by the host integration tests.
Returns
Error code from the first failing primitive, or k_ra8_ok.
Return values
k_ra8_okLine transmitted.
k_ra8_err_hw_errorUnderlying primitive failed.
Precondition
internal_rng_demo_setup_or_halt returned cleanly.
The console sink can accept the fixed prefix, payload, and line ending.
Postcondition
On success 70 bytes (6 prefix + 64 hex + 2 EOL) plus the fixed "trng: entropy OK\r\n" verdict line have been sent.
On failure, the function returns before reporting a successful verdict.
Note
The PSA backend is deterministic on this silicon and is not claimed as entropy.
Since
0.1.0

Definition at line 185 of file main.c.

References internal_rng_demo_nibble_to_hex(), k_ra8_err_hw_error, k_ra8_ok, k_rng_demo_bytes_per_line, k_rng_demo_hex_per_byte, k_rng_demo_nibble_shift, ra8_board_uart_console_write(), RA8_INTERNAL, ra8_psa_crypto_random(), s_rng_demo_eol, s_rng_demo_pass_msg, and s_rng_demo_prefix.

Referenced by main().

◆ internal_rng_demo_nibble_to_hex()

uint8_t internal_rng_demo_nibble_to_hex ( uint8_t nibble)
static

Convert a nibble to its ASCII hex representation.

Masks the input to four bits and maps values below ten to decimal digits and the remaining values to lowercase hexadecimal letters.

Parameters
[in]nibbleLower 4 bits used; upper 4 bits ignored.
Returns
ASCII byte in '0'..'9' or 'a'..'f'.
Return values
0x30..0x39Decimal ASCII digit for nibble values zero through nine.
0x61..0x66Lowercase ASCII letter for nibble values ten through fifteen.
Precondition
The caller accepts that bits above the low nibble are discarded.
The execution character set uses contiguous ASCII digit and letter codes.
Postcondition
Return value is a printable ASCII character.
The input value and all shared state remain unchanged.
Note
The conversion deliberately emits lowercase hexadecimal.
Since
0.1.0

Definition at line 113 of file main.c.

References k_rng_demo_alpha_threshold, k_rng_demo_nibble_mask, and RA8_INTERNAL.

Referenced by internal_rng_demo_emit_one_line().

◆ internal_rng_demo_panic_halt()

void internal_rng_demo_panic_halt ( void )
static

Park the processor after a fatal setup or entropy-path failure.

Executes wait-for-interrupt indefinitely so a failed diagnostic cannot continue producing output that could be mistaken for valid samples.

Precondition
A required setup step or runtime entropy check has failed.
No remaining foreground recovery operation can make progress safely.
Postcondition
This function does not return.
The processor remains in a low-activity wait loop.
Note
This terminal path preserves the diagnostic failure state for probing.
Since
0.1.0

Definition at line 87 of file main.c.

References RA8_INTERNAL.

Referenced by internal_rng_demo_setup_or_halt(), and main().

◆ internal_rng_demo_setup_or_halt()

void internal_rng_demo_setup_or_halt ( void )
static

Bring CGC, SysTick, SCI8, LED1, and PSA crypto up.

Initializes each prerequisite in dependency order and transfers to internal_rng_demo_panic_halt on the first error.

Precondition
The function runs during single-threaded application startup.
EK-RA8D2 board registers are accessible through the platform mapping.
Postcondition
On return, timing, console, LED1, and the PSA crypto facade are ready.
Any prerequisite failure prevents a return to the caller.
Note
The helper centralizes the fail-closed startup policy for this demo.
Since
0.1.0

Definition at line 135 of file main.c.

References internal_rng_demo_panic_halt(), k_ra8_board_led1, k_ra8_clock_id_cpuclk0, k_ra8_ok, k_rng_demo_baud, ra8_board_led_init(), ra8_board_uart_console_init(), ra8_cgc_get_clock_hz(), ra8_cgc_init(), RA8_INTERNAL, ra8_psa_crypto_init(), and ra8_time_init().

Referenced by main().

◆ main()

void main ( void )

The application entry point Reset_Handler hands control to.

Returns void, not int. This is a freestanding image: there is no hosted C environment, no process and nothing to report an exit status to. ISO C fixes main at int only for a hosted implementation; for a freestanding one (C23 5.1.2.1) the startup function's name and type are implementation-defined, and this is that definition. Reset_Handler discards no value because there is none to discard, and if main ever does return, startup halts the CPU rather than resuming anything.

The firmware lane is compiled -ffreestanding (see cmake/ra8_add_app.cmake) and the flag and this signature travel together: without it both GCC and clang reject a non-int main (-Wmain / -Wmain-return-type). Do not remove one without the other.

That coupling is why the declaration sits behind __STDC_HOSTED__ == 0, which -ffreestanding sets and a hosted build does not. The guard is not defensive dressing: this header is reachable from host builds (the unit tests compile ra8_core natively), and an unguarded void main(void); makes every hosted translation unit that includes it fail with conflicting types for 'main' against its own ISO int main. The declaration therefore exists exactly where its contract does.

Hosted first-party code – everything under tests/ and tools/ – uses the ISO int main(...) contract instead, because it genuinely does run under an OS that reads the exit status. scripts/checks/check_entry_points.py holds each domain to its own contract (#707).

Declared here, once, for the same reason SystemInit is: every vector_table.c used to restate it as a local extern int32_t main(void);, sixteen copies that no compiler ever compared against the definition – and roughly thirty of them had silently drifted out of agreement with the main they called.

Precondition
Reset_Handler has copied .data and zeroed .bss.
SystemInit has configured the clock tree and VTOR.
Postcondition
Control does not return; the image runs until reset or halt.
Any value the application wanted to report has been logged, not returned.
Note
Not thread-safe; single-threaded startup context only.
Warning
Only valid while the translation unit is compiled -ffreestanding. A hosted build rejects this signature.
See also
SystemInit()
Since
0.1.0

The application entry point Reset_Handler hands control to.

Brings up CGC + BSP audio then plays blocks.

Precondition
Reset_Handler has copied .data and zeroed .bss.
SystemInit has set VTOR, FPU, and priority grouping.
Postcondition
On clean entry the CPU stays in the playback loop forever.
On any HAL init failure the function halts in WFI.
Since
0.1.0

The application entry point Reset_Handler hands control to.

Brings up CGC + GPT triple, runs sweep.

Precondition
Reset_Handler has copied .data and zeroed .bss.
SystemInit has set VTOR, FPU, and priority grouping.
Postcondition
On clean entry the CPU stays in the sweep loop forever.
On any HAL init failure the function halts in WFI.
Since
0.1.0

The application entry point Reset_Handler hands control to.

Brings up clocks + UART + RMII pins, then ThreadX.

Precondition
Reset_Handler has copied .data and zeroed .bss.
Postcondition
On clean entry the kernel runs the worker thread once.
Since
0.1.0

The application entry point Reset_Handler hands control to.

Brings up clocks + UART, then enters ThreadX.

Precondition
Reset_Handler has copied .data and zeroed .bss.
SystemInit has set VTOR, FPU, and priority grouping.
Postcondition
On clean entry the kernel runs the worker thread forever.
On any HAL init failure the function halts in WFI.
Since
0.1.0

The application entry point Reset_Handler hands control to.

Brings up LED, console, SDHI pins, then ThreadX.

Precondition
Reset_Handler has copied .data + zeroed .bss.
SystemInit has set VTOR, FPU, and priority grouping.
Postcondition
CPUCLK0 is raised to the PLL1 target before the kernel starts.
On clean entry the SD card thread runs forever.
On any HAL init failure the function halts in __WFI.
Since
0.1.0

The application entry point Reset_Handler hands control to.

Brings up CGC + USB-FS + UAC1, then enters the iso-IN feed loop forever.

Precondition
Reset_Handler has copied .data and zeroed .bss.
SystemInit has set VTOR, FPU, and priority grouping.
Postcondition
On clean entry the CPU stays in the iso-IN feed loop forever.
On any HAL init failure the function halts in WFI.
Since
0.1.0

The application entry point Reset_Handler hands control to.

Precondition
SystemInit set VTOR / FPU / priority grouping.

The application entry point Reset_Handler hands control to.

See file header.

Precondition
Boot init has completed.
The secure-boot library's BLXNS into NS image either failed or was skipped (the call site in ra8_trustzone_init is a no-op on host builds).
Postcondition
Diagnostic counter latched, CPU parked in a halt loop.
Function never returns.
Note
Single-threaded entry.
Since
0.1.0

The application entry point Reset_Handler hands control to.

Precondition
Reset_Handler copied .data and zeroed .bss.
SystemInit set VTOR / FPU / priority grouping.
Postcondition
On success g_eoh_chapters / g_eoh_crc hold the parsed results, the banner is emitted, and g_eoh_heartbeat advances once per frame.
On any failure g_eoh_err is non-zero and the CPU parks (no heartbeat).
Since
0.1.0

The application entry point Reset_Handler hands control to.

Precondition
Reset_Handler copied .data and zeroed .bss.
SystemInit set VTOR / FPU / priority grouping.
Postcondition
The chapters/ch0-CRC banner is emitted; the CPU then loops in WFI.
Since
0.1.0

The application entry point Reset_Handler hands control to.

Precondition
Reset_Handler copied .data and zeroed .bss.
SystemInit set VTOR / FPU / priority grouping.
Postcondition
On success the g_etoc_* result globals hold the parsed TOC values, the banner is emitted, and g_etoc_heartbeat advances once per frame.
On any failure g_etoc_err is non-zero and the CPU parks (no heartbeat).
Since
0.1.0

The application entry point Reset_Handler hands control to.

Precondition
Reset_Handler copied .data and zeroed .bss; SystemInit set VTOR/FPU.
Postcondition
The shelf scans on the panel; taps open books, browse, and read.
Since
0.1.0

The application entry point Reset_Handler hands control to.

Precondition
Reset_Handler copied .data and zeroed .bss.
SystemInit set VTOR / FPU / priority grouping.
Postcondition
The slab/arena/tile/vmem banner is emitted; the CPU then loops in WFI.
Since
0.1.0

The application entry point Reset_Handler hands control to.

Precondition
Reset_Handler copied .data and zeroed .bss.
SystemInit set VTOR / FPU / priority grouping.
Postcondition
On success the cache globals are latched and g_pc_heartbeat advances.
On any failure g_pc_err is non-zero and the CPU parks (no heartbeat).
Since
0.1.0

The application entry point Reset_Handler hands control to.

Profiles power modes once a second.

Precondition
Reset_Handler has copied .data and zeroed .bss.
SystemInit has set VTOR, FPU, and priority grouping.
Postcondition
On clean entry the CPU stays in the profile + blink loop.
On any HAL hard error LED2 latches ON.
Since
0.1.0

The application entry point Reset_Handler hands control to.

Precondition
Reset_Handler has copied .data and zeroed .bss.
The shared board boot files installed the vector table.
Postcondition
The demo has run once and its verdict banner is streaming steadily.
The CPU idles re-emitting the banner (or halts after a fatal init error).
Since
0.1.0

The application entry point Reset_Handler hands control to.

Brings up the clocks, console, SPI, and SD card, then runs the shared ra8_io VFS round-trip over the SD-over-SPI block device. On success it prints the exact PASS banner the HIL runner and ra8_emulator smoke gate scrape for; on any failure it prints FAIL and parks the core.

Precondition
Reset_Handler has copied .data and zeroed .bss.
SystemInit has set VTOR, FPU, and priority grouping.
Postcondition
On a clean run the CPU loops forever after the PASS banner.
On any failure the function prints FAIL and halts in WFI.
Note
Not thread-safe; this is the single-threaded app entry.
Since
0.1.0

The application entry point Reset_Handler hands control to.

Initialises logging and the console, brings up the OSPI NOR volume, runs the erase-before-write round-trip, and prints a single PASS/FAIL verdict line over SCI8 before parking in an infinite loop.

Precondition
SystemInit configured VTOR / FPU / priority grouping.
The OSPI NOR array is present (modelled in ra8_emulator, real on silicon).
Postcondition
Exactly one PASS or FAIL verdict line has been queued on SCI8.
Control parks in an infinite loop; the function never returns.
Note
Single-threaded; runs to the park loop on the main stack.
Since
0.1.0

The application entry point Reset_Handler hands control to.

Precondition
Reset_Handler copied .data and zeroed .bss.
SystemInit set VTOR / FPU / priority grouping.
Postcondition
The page-count + render-hash banner is emitted; the CPU loops in WFI.
Since
0.1.0

Definition at line 237 of file main.c.

References internal_rng_demo_emit_one_line(), internal_rng_demo_panic_halt(), internal_rng_demo_setup_or_halt(), k_ra8_board_led1, k_ra8_ok, k_rng_demo_period_ms, ra8_board_led_toggle(), ra8_delay_ms(), and ra8_isr_globals_enable().

Variable Documentation

◆ s_rng_demo_eol

const uint8_t s_rng_demo_eol[] = "\r\n"
static

Definition at line 65 of file main.c.

Referenced by internal_rng_demo_emit_one_line().

◆ s_rng_demo_pass_msg

const uint8_t s_rng_demo_pass_msg[] = "rng: PRNG stub OK (deterministic, NOT entropy)\r\n"
static

Verdict line emitted only after a non-stuck sample is dumped.

Deliberately does NOT claim entropy – ra8_psa_crypto_random returns a deterministic software stub on this silicon (no working RSIP TRNG).

Definition at line 72 of file main.c.

Referenced by internal_rng_demo_emit_one_line().

◆ s_rng_demo_prefix

const uint8_t s_rng_demo_prefix[] = "rng: "
static

Fixed prefix and CR/LF tail used on every emit.

Definition at line 64 of file main.c.

Referenced by internal_rng_demo_emit_one_line().