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
24
25#include <stdint.h>
26#include <string.h>
27
28#include "ra8_board_ek_ra8d2.h"
29#include "ra8_boot_entry.h"
30#include "ra8_cgc.h"
31#include "ra8_err.h"
32#include "ra8_isr.h"
33#include "ra8_time.h"
34#include "ra8_xspi.h"
35
46
62volatile uint32_t g_fj_match = 0U;
63
80volatile uint32_t g_fj_mismatch = 0U;
81
98
114volatile uint32_t g_fj_last_step = k_fj_step_init;
115
127volatile uint32_t g_fj_last_counter = 0U;
128
139volatile uint32_t g_fj_last_echoed = 0U;
140
160volatile uint32_t g_fj_jedec_id = 0U;
161
180typedef enum : uint32_t {
181 k_fj_err_none = 0xFFFFFFFFU,
183
185
188{
189 while (1) {
190 __asm__ volatile("wfi");
191 }
192}
193
200static void flash_journal_pack(uint32_t counter, uint8_t* rec)
201{
202 for (uint8_t i = 0U; i < (uint8_t)k_journal_counter_bytes; i++) {
203 rec[i] =
204 (uint8_t)((counter >> ((uint32_t)k_journal_byte_shift * i)) & (uint32_t)k_journal_byte_mask);
205 }
206 for (uint8_t i = (uint8_t)k_journal_counter_bytes; i < (uint8_t)k_journal_record_bytes; i++) {
207 rec[i] = i;
208 }
209}
210
217static uint32_t flash_journal_unpack(const uint8_t* rec)
218{
219 uint32_t v = 0U;
220 for (uint8_t i = 0U; i < (uint8_t)k_journal_counter_bytes; i++) {
221 v |= ((uint32_t)rec[i]) << ((uint32_t)k_journal_byte_shift * i);
222 }
223 return v;
224}
225
242[[nodiscard]] static ra8_err_t flash_journal_round_trip(uint32_t counter, uint32_t* echoed)
243{
244 uint8_t rec[k_journal_record_bytes];
245 uint8_t back[k_journal_record_bytes];
246 g_fj_last_counter = counter;
247 flash_journal_pack(counter, rec);
249 (uint32_t)k_journal_record_addr) != k_ra8_ok) {
251 return k_ra8_err_hw_error;
252 }
255 (uint32_t)k_journal_record_addr,
256 rec,
257 (uint32_t)k_journal_record_bytes) != k_ra8_ok) {
259 return k_ra8_err_hw_error;
260 }
263 (uint32_t)k_journal_record_addr,
264 back,
265 (uint32_t)k_journal_record_bytes) != k_ra8_ok) {
267 return k_ra8_err_hw_error;
268 }
270 *echoed = flash_journal_unpack(back);
271 g_fj_last_echoed = *echoed;
272 return k_ra8_ok;
273}
274
290{
291 uint32_t cpuclk0_hz = 0U;
292 if (ra8_cgc_init() != k_ra8_ok) {
294 }
297 }
298 if (ra8_time_init(cpuclk0_hz) != k_ra8_ok) {
300 }
303 }
306 }
307 /* Best-effort U15 I/O-expander courtesy write. The on-board Octo-SPI
308 * flash (U3, ISSI IS25LX512M; UM section 6.3 p 35) is connected through
309 * the SW4-3 analog mux, which is hardware-only. A firmware sweep of the
310 * full U15 output space (issue #44; see docs/HARDWARE_BRINGUP.md) proved
311 * the expander's GPIOs do NOT gate the OSPI bus, so this call cannot
312 * affect flash reachability -- it is kept only as a no-op courtesy and
313 * its return code is stamped to g_fj_expander_err for the memprobe. If
314 * the JEDEC read below returns 0x00FFFFFF the bus is sitting at the board
315 * pull-ups (flash not passed through SW4-3), which is a physical check. */
317 g_fj_expander_err = (uint32_t)exp_err;
318 /* OCTA pins come out of reset as GPIO; ra8_board_xspi_pins_init
319 * routes them to PSEL=0x1C and pulses RESET_L on P106 so the
320 * IS25LX512M lands in standard-SPI mode with deterministic state.
321 * HUM Ch 20.6 "Multiplexed Pin Function Selector" p 871. */
324 }
327 }
328 uint32_t jedec_id = 0U;
329 (void)ra8_xspi_flash_read_id((uint8_t)k_journal_xspi_instance, &jedec_id);
330 g_fj_jedec_id = jedec_id;
331}
332
333void main(void)
334{
337
338 uint32_t counter = 0U;
339 while (1) {
340 uint32_t echoed = 0U;
341 const ra8_err_t err = flash_journal_round_trip(counter, &echoed);
342 if (err == k_ra8_ok && echoed == counter) {
344 g_fj_match += 1U;
346 } else {
348 g_fj_mismatch += 1U;
349 if (err == k_ra8_ok) {
351 }
352 }
353 counter++;
355 }
357}
void main(void)
Secure fallback main entry point.
Definition main.c:37
volatile uint32_t g_fj_expander_err
Return code from the best-effort U15 Octo-SPI-active override.
Definition main.c:184
static void flash_journal_setup_or_halt(void)
Run all init-time bring-up that must complete before the loop.
Definition main.c:289
volatile uint32_t g_fj_last_echoed
Last counter value read back from flash.
Definition main.c:139
fj_err_sentinel_t
Sentinel for g_fj_expander_err meaning "no error captured yet".
Definition main.c:180
@ k_fj_err_none
Fj error none.
Definition main.c:181
volatile uint32_t g_fj_last_step
Last round-trip step value (J-Link memprobe diagnostic).
Definition main.c:114
static void flash_journal_panic_halt(void)
Park the CPU after a fatal init failure.
Definition main.c:187
static uint32_t flash_journal_unpack(const uint8_t *rec)
Decode the counter from a 16-byte record.
Definition main.c:217
flash_journal_const_t
Demo tunables.
Definition main.c:37
@ k_journal_xspi_instance
Journal XSPI instance.
Definition main.c:41
@ k_journal_record_bytes
Journal record bytes.
Definition main.c:39
@ k_journal_record_addr
Journal record address.
Definition main.c:40
@ k_journal_byte_mask
Journal byte mask.
Definition main.c:43
@ k_journal_counter_bytes
Journal counter bytes.
Definition main.c:42
@ k_journal_byte_shift
Journal byte shift.
Definition main.c:44
@ k_journal_period_ms
Journal period ms.
Definition main.c:38
volatile uint32_t g_fj_last_counter
Last counter the firmware tried to write.
Definition main.c:127
volatile uint32_t g_fj_match
HIL liveness counter – incremented on every successful erase -> program -> read-back -> compare round...
Definition main.c:62
static ra8_err_t flash_journal_round_trip(uint32_t counter, uint32_t *echoed)
Erase + program + read-back round-trip of one record.
Definition main.c:242
static void flash_journal_pack(uint32_t counter, uint8_t *rec)
Pack a counter into a 16-byte record (little-endian + padding).
Definition main.c:200
volatile uint32_t g_fj_mismatch
HIL failure counter – incremented on any erase, program, or read failure, or when the read-back bytes...
Definition main.c:80
fj_step_t
Step codes for g_fj_last_step (erase/program/read/compare chain).
Definition main.c:87
@ k_fj_step_compare_ok
Read-back matched.
Definition main.c:92
@ k_fj_step_read_failed
Record read-back failed.
Definition main.c:95
@ k_fj_step_compare_mismatch
Read-back differed (data error).
Definition main.c:96
@ k_fj_step_init
No round-trip run yet.
Definition main.c:88
@ k_fj_step_program_ok
Record program succeeded.
Definition main.c:90
@ k_fj_step_read_ok
Record read-back succeeded.
Definition main.c:91
@ k_fj_step_program_failed
Record program failed.
Definition main.c:94
@ k_fj_step_erase_ok
Sector erase succeeded.
Definition main.c:89
@ k_fj_step_erase_failed
Sector erase failed.
Definition main.c:93
volatile uint32_t g_fj_jedec_id
One-shot JEDEC ID read at boot (memprobe diagnostic).
Definition main.c:160
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_io_expander_set_octospi_active(void)
Program the U15 I/O expander to request the Octo-SPI SW4 layout.
ra8_err_t ra8_board_xspi_pins_init(void)
Route the 12 OCTA bus pins to the xSPI peripheral and pulse RESET_L.
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
@ k_ra8_xspi_lio_1s1s1s
1S-1S-1S extended SPI.
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
xSPI / Octo-SPI driver (flash read/program/erase + ID/status)
ra8_err_t ra8_xspi_flash_program(uint8_t instance, uint32_t flash_addr, const uint8_t *data, uint32_t len)
Program (write) len bytes at flash_addr.
ra8_err_t ra8_xspi_init(uint8_t instance, ra8_xspi_lio_mode_t mode)
Initialise an xSPI instance in the chosen link-layer IO mode.
Definition ra8_xspi.c:372
ra8_err_t ra8_xspi_flash_read(uint8_t instance, uint32_t flash_addr, uint8_t *buf, uint32_t len)
Read len bytes from external flash into buf.
ra8_err_t ra8_xspi_flash_erase_sector(uint8_t instance, uint32_t flash_addr)
Erase the 4 KiB sector containing flash_addr.
ra8_err_t ra8_xspi_flash_read_id(uint8_t instance, uint32_t *out_id)
Read the JEDEC ID (opcode 0x9F), returning 24 bits in a uint32_t.