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
36
37#include <stdint.h>
38
39#include "ra8_attributes.h"
40#include "ra8_board_ek_ra8d2.h"
41#include "ra8_boot_entry.h"
42#include "ra8_cgc.h"
43#include "ra8_err.h"
44#include "ra8_isr.h"
45#include "ra8_psa_crypto.h"
46#include "ra8_time.h"
47
49typedef enum : uint32_t {
50 k_rng_demo_baud = 115200U,
53
62
64static const uint8_t s_rng_demo_prefix[] = "rng: ";
65static const uint8_t s_rng_demo_eol[] = "\r\n";
66
72static const uint8_t s_rng_demo_pass_msg[] = "rng: PRNG stub OK (deterministic, NOT entropy)\r\n";
73
88{
89 while (1) {
90 __asm__ volatile("wfi");
91 }
92}
93
113RA8_INTERNAL static uint8_t internal_rng_demo_nibble_to_hex(uint8_t nibble)
114{
115 uint8_t n = (uint8_t)(nibble & (uint8_t)k_rng_demo_nibble_mask);
116 if (n < (uint8_t)k_rng_demo_alpha_threshold) {
117 return (uint8_t)('0' + n);
118 }
119 return (uint8_t)('a' + (n - (uint8_t)k_rng_demo_alpha_threshold));
120}
121
136{
137 uint32_t cpuclk0_hz = 0U;
138
139 if (ra8_cgc_init() != k_ra8_ok) {
141 }
144 }
145 if (ra8_time_init(cpuclk0_hz) != k_ra8_ok) {
147 }
150 }
153 }
154 if (ra8_psa_crypto_init() != k_ra8_ok) {
156 }
157}
158
186{
187 uint8_t rng[k_rng_demo_bytes_per_line] = {};
188 uint8_t hex[(uint16_t)k_rng_demo_bytes_per_line * k_rng_demo_hex_per_byte] = {};
189 const uint32_t hex_len =
190 (uint32_t)((uint16_t)k_rng_demo_bytes_per_line * (uint16_t)k_rng_demo_hex_per_byte);
191
192 if (ra8_psa_crypto_random(rng, sizeof(rng)) != k_ra8_ok) {
193 return k_ra8_err_hw_error;
194 }
195 /* Stuck-bit detector: if every byte in the 32-byte sample is the
196 * same value, the TRNG entropy path is almost certainly broken
197 * (probability of 32 random bytes all matching is 1 in 2^248).
198 * Without this check the demo would happily print "trng: 00...00"
199 * or "trng: ff...ff" forever and the probe would pass. */
200 bool all_same = true;
201 for (uint8_t i = 1U; i < (uint8_t)k_rng_demo_bytes_per_line; ++i) {
202 if (rng[i] != rng[0]) {
203 all_same = false;
204 break;
205 }
206 }
207 if (all_same) {
208 const uint8_t fail_banner[] = "rng_demo: FAIL stuck\r\n";
209 (void)ra8_board_uart_console_write(fail_banner, (size_t)(sizeof(fail_banner) - 1U));
210 return k_ra8_err_hw_error;
211 }
212 for (uint8_t i = 0U; i < (uint8_t)k_rng_demo_bytes_per_line; ++i) {
213 const size_t hi = (size_t)i * (size_t)k_rng_demo_hex_per_byte;
214 hex[hi] =
215 internal_rng_demo_nibble_to_hex((uint8_t)(rng[i] >> (uint8_t)k_rng_demo_nibble_shift));
216 hex[hi + 1U] = internal_rng_demo_nibble_to_hex(rng[i]);
217 }
218
220 k_ra8_ok) {
221 return k_ra8_err_hw_error;
222 }
223 if (ra8_board_uart_console_write(hex, (size_t)hex_len) != k_ra8_ok) {
224 return k_ra8_err_hw_error;
225 }
226 if (ra8_board_uart_console_write(s_rng_demo_eol, (size_t)(sizeof(s_rng_demo_eol) - 1U)) !=
227 k_ra8_ok) {
228 return k_ra8_err_hw_error;
229 }
230 /* Success-only verdict for the HIL scrape; fire-and-forget so it adds
231 * no decision to the documented MC/DC vector set above. */
233 (size_t)(sizeof(s_rng_demo_pass_msg) - 1U));
234 return k_ra8_ok;
235}
236
void main(void)
Secure fallback main entry point.
Definition main.c:37
rng_demo_byte_t
Per-emit byte counts.
Definition main.c:55
@ k_rng_demo_bytes_per_line
Rng demo bytes per line.
Definition main.c:56
@ k_rng_demo_nibble_mask
Rng demo nibble mask.
Definition main.c:58
@ k_rng_demo_alpha_threshold
Rng demo alpha threshold.
Definition main.c:60
@ k_rng_demo_hex_per_byte
Rng demo hex per byte.
Definition main.c:57
@ k_rng_demo_nibble_shift
Rng demo nibble shift.
Definition main.c:59
static const uint8_t s_rng_demo_pass_msg[]
Verdict line emitted only after a non-stuck sample is dumped.
Definition main.c:72
rng_demo_config_t
Compile-time settings for the demo.
Definition main.c:49
@ k_rng_demo_baud
Rng demo baud.
Definition main.c:50
@ k_rng_demo_period_ms
Rng demo period ms.
Definition main.c:51
static const uint8_t s_rng_demo_prefix[]
Fixed prefix and CR/LF tail used on every emit.
Definition main.c:64
static void internal_rng_demo_panic_halt(void)
Park the processor after a fatal setup or entropy-path failure.
Definition main.c:87
static void internal_rng_demo_setup_or_halt(void)
Bring CGC, SysTick, SCI8, LED1, and PSA crypto up.
Definition main.c:135
static uint8_t internal_rng_demo_nibble_to_hex(uint8_t nibble)
Convert a nibble to its ASCII hex representation.
Definition main.c:113
static ra8_err_t internal_rng_demo_emit_one_line(void)
Emit one hex line of TRNG output.
Definition main.c:185
static const uint8_t s_rng_demo_eol[]
Definition main.c:65
Annotation-attribute framework macros for ra8-firmware.
#define RA8_INTERNAL
Marker that a function is intended to be static (file-local).
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_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
@ k_ra8_err_hw_error
Generic hardware fault detected (error flag set, fault interrupt).
Definition ra8_err.h:310
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
Application-level PSA Crypto facade over tf-psa-crypto.
ra8_err_t ra8_psa_crypto_init(void)
One-shot facade initialisation.
ra8_err_t ra8_psa_crypto_random(uint8_t *out, size_t out_len)
Fill out[0..out_len-1] with cryptographically secure bytes.
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