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
53
54#include <stdint.h>
55
56#include "ra8_board_ek_ra8d2.h"
57#include "ra8_boot_entry.h"
58#include "ra8_cgc.h"
59#include "ra8_check.h"
60#include "ra8_dtc.h"
61#include "ra8_dtc_regs.h"
62#include "ra8_elc.h"
63#include "ra8_err.h"
64#include "ra8_icu_regs.h"
65#include "ra8_isr.h"
66#include "ra8_mstp.h"
67#include "ra8_time.h"
68
70static const char* s_tag = "dtc_demo";
71
80
82typedef enum : uint8_t {
86
97typedef enum : uint16_t {
100
102typedef enum : uint8_t {
105
114typedef enum : uint8_t {
120
136
145typedef enum : uint16_t {
149
160typedef enum : uint32_t {
165
167typedef enum : uint32_t {
169} dtc_cra_t;
170
171static_assert((uint32_t)k_dtc_demo_buf_words == (uint32_t)k_dtc_cra_block_units,
172 "CRA=0x0000 encodes a 256-unit block; buffer must be 256 words");
173
175static const uint8_t k_dtc_demo_ok_msg[] = "dtc: copied 1024B match=Y\r\n";
176static const uint8_t k_dtc_demo_bad_msg[] = "dtc: copied 1024B match=N\r\n";
177
181
187[[gnu::aligned(k_dtc_vt_align)]] static uint32_t s_dtc_vt[k_dtc_vt_entries];
188
195
197static uint16_t s_dtc_slot;
198
205volatile uint32_t g_dtc_match = 0U;
206
213volatile uint32_t g_dtc_bytes = 0U;
214
221volatile uint32_t g_dtc_activations = 0U;
222
229volatile uint32_t g_dtc_isr_count = 0U;
230
237volatile uint32_t g_dtc_heartbeat = 0U;
238
240static void dtc_demo_panic_halt(void)
241{
242 while (1) {
243 __asm__ volatile("wfi");
244 }
245}
246
257static void dtc_demo_complete_cb(void* ctx, uint16_t status)
258{
259 (void)ctx;
260 (void)status;
262}
263
278static void dtc_demo_swevt_isr(void* ctx)
279{
280 (void)ctx;
282}
283
285static void dtc_demo_setup_or_halt(void)
286{
287 uint32_t cpuclk0_hz = 0U;
288
289 if (ra8_cgc_init() != k_ra8_ok) {
291 }
294 }
295 if (ra8_mstp_init() != k_ra8_ok) {
297 }
298 if (ra8_time_init(cpuclk0_hz) != k_ra8_ok) {
300 }
301 if (ra8_isr_init() != k_ra8_ok) {
303 }
304 if (ra8_elc_init() != k_ra8_ok) {
306 }
309 }
312 }
315 }
316}
317
329static void dtc_demo_fill_buffers(void)
330{
331 for (uint32_t i = 0U; i < (uint32_t)k_dtc_demo_buf_words; ++i) {
332 s_src[i] = i ^ (i >> (uint32_t)k_dtc_demo_byte_sh);
333 s_dst[i] = 0U;
334 }
335}
336
353static void dtc_demo_program_ti(void)
354{
355 const uint8_t mra = (uint8_t)(((uint8_t)k_dtc_md_block << k_dtc_mra_md_pos) |
356 ((uint8_t)k_dtc_sz_word << k_dtc_mra_sz_pos) |
357 ((uint8_t)k_dtc_sm_inc << k_dtc_mra_sm_pos));
358 const uint8_t mrb = (uint8_t)((uint8_t)k_dtc_dm_inc << k_dtc_mrb_dm_pos);
359 /* MR[31:24]=MRA, MR[23:16]=MRB, MR[15:8]=MRC(0); HUM Ch 18.2.2 p 786 /
360 * 18.2.3 p 787 / 18.2.4 p 789, Figure 18.4 p 799. SRAM (not MMIO). */
361 s_dtc_ti.MR = ((uint32_t)mra << k_dtc_mra_byte_pos) | ((uint32_t)mrb << k_dtc_mrb_byte_pos);
362 s_dtc_ti.SAR = (uint32_t)(uintptr_t)s_src;
363 s_dtc_ti.DAR = (uint32_t)(uintptr_t)s_dst;
364 s_dtc_ti.CRB = (uint16_t)k_dtc_crb_one_block;
365 s_dtc_ti.CRA = (uint16_t)k_dtc_cra_block_256;
366}
367
383[[nodiscard]] static ra8_err_t dtc_demo_arm_slot(void)
384{
385 volatile uint32_t* slot_reg = ra8_icu_ielsr(s_dtc_slot);
386 if (slot_reg == nullptr) {
387 return k_ra8_err_hw_error;
388 }
389 /* HUM Ch 14.2.17 "IELSRn : ICU Event Link Setting Register n" p 547:
390 * DTCE[24] enables DTC activation; IR[16] is the RW1C status flag. */
391 const uint32_t cur = *slot_reg;
392 *slot_reg = cur | (uint32_t)k_ra8_ielsr_dtce_mask;
393 return k_ra8_ok;
394}
395
410{
413 }
416 }
419 nullptr,
420 (uint8_t)k_dtc_demo_isr_prio,
421 &s_dtc_slot) != k_ra8_ok) {
423 }
424 if (s_dtc_slot >= (uint16_t)k_dtc_vt_entries) {
426 }
427 /* DTCVBR + slot*4 holds the 16-byte-aligned TI start address; bit0 is
428 * the privilege attribution (0 = privileged). HUM Ch 18.3.1 p 796 +
429 * Figure 18.3 p 798. SRAM vector-table write (not MMIO). */
430 s_dtc_vt[s_dtc_slot] = (uint32_t)(uintptr_t)&s_dtc_ti;
431 if (ra8_dtc_enable() != k_ra8_ok) {
433 }
434}
435
449static uint8_t dtc_demo_verify(void)
450{
451 for (uint32_t i = 0U; i < (uint32_t)k_dtc_demo_buf_words; ++i) {
452 if (s_dst[i] != s_src[i]) {
453 return 0U;
454 }
455 }
456 return 1U;
457}
458
479[[nodiscard]] static ra8_err_t dtc_demo_run_once(uint8_t* out_ok)
480{
481 RA8_CHECK_NULL_PTR(out_ok, s_tag, "out_ok must not be nullptr");
482
484 const ra8_err_t arm = dtc_demo_arm_slot();
485 if (arm != k_ra8_ok) {
486 return arm;
487 }
489 if (trig != k_ra8_ok) {
490 return trig;
491 }
493
494 for (uint32_t i = 0U; i < (uint32_t)k_dtc_demo_poll_limit; ++i) {
496 break;
497 }
498 }
499
500 *out_ok = dtc_demo_verify();
501 return k_ra8_ok;
502}
503
504void main(void)
505{
508 /* Completion is detected by polling ``s_dst`` (step 4 below), so the global
509 * interrupt is intentionally left masked. On RA8 silicon, enabling the
510 * DTC-complete CPU interrupt lets its ISR (which writes ``DTCSTS``) race the
511 * in-flight DTC transfer and corrupt the copy; the headless emulator, which
512 * runs the transfer synchronously on the ELSEGR write, does not expose this.
513 * The IELSR slot + ``DTCE`` armed in bring-up still activate the DTC -- we
514 * simply do not take the completion IRQ. */
515
516 while (1) {
518 uint8_t ok = 0U;
519 const ra8_err_t err = dtc_demo_run_once(&ok);
520 const uint8_t good = (err == k_ra8_ok && ok != 0U) ? 1U : 0U;
521 g_dtc_match = (uint32_t)good;
522 g_dtc_bytes = (good != 0U) ? (uint32_t)k_dtc_demo_buf_bytes : 0U;
523 if (good != 0U) {
525 (size_t)(sizeof(k_dtc_demo_ok_msg) - 1U));
527 } else {
529 (size_t)(sizeof(k_dtc_demo_bad_msg) - 1U));
531 }
534 }
536}
void main(void)
Secure fallback main entry point.
Definition main.c:37
volatile uint32_t g_dtc_isr_count
Count of DTC-complete callbacks fanned through the HAL dispatch.
Definition main.c:231
static uint32_t s_dtc_vt[k_dtc_arm_vt_entries]
DTC vector table – one 4-byte TI start address per IELSR slot.
Definition main.c:189
static r_dtc_xfer_info_t s_dtc_ti
The 16-byte Transfer Information block the DTC reads each pass.
Definition main.c:196
volatile uint32_t g_dtc_heartbeat
Bumps once per main-loop pass – liveness for headless probes.
Definition main.c:239
static uint32_t s_src[k_dtc_arm_buf_words]
Source / destination buffers (32-bit aligned by element type).
Definition main.c:181
static uint16_t s_dtc_slot
IELSR slot allocated for the DTC activation = DTC vector number.
Definition main.c:199
volatile uint32_t g_dtc_activations
Count of ELC software-event triggers issued (armed + disarmed).
Definition main.c:223
static uint32_t s_dst[k_dtc_arm_buf_words]
Definition main.c:182
dtc_demo_byte_t
Single-byte constants.
Definition main.c:82
@ k_dtc_demo_byte_sh
Source-pattern shift (i ^ (i >> 8)).
Definition main.c:83
@ k_dtc_demo_isr_prio
NVIC priority for the DTC-complete slot.
Definition main.c:84
static const uint8_t k_dtc_demo_ok_msg[]
Output line tags.
Definition main.c:175
dtc_mr_field_t
DTC Transfer-Information mode-bit field values.
Definition main.c:114
@ k_dtc_sm_inc
MRA.SM[3:2] = 10b: increment SAR.
Definition main.c:117
@ k_dtc_md_block
MRA.MD[7:6] = 10b: block transfer mode.
Definition main.c:115
@ k_dtc_sz_word
MRA.SZ[5:4] = 10b: 32-bit word units.
Definition main.c:116
@ k_dtc_dm_inc
MRB.DM[3:2] = 10b: increment DAR.
Definition main.c:118
dtc_demo_config_t
Compile-time settings.
Definition main.c:73
@ k_dtc_demo_poll_limit
Bounded wait for the block to land.
Definition main.c:78
@ k_dtc_demo_buf_words
32-bit words per buffer / block.
Definition main.c:77
@ k_dtc_demo_period_ms
Delay between copy passes.
Definition main.c:75
@ k_dtc_demo_buf_bytes
Bytes copied per pass (256 x 4).
Definition main.c:76
@ k_dtc_demo_baud
SCI8 baud rate.
Definition main.c:74
dtc_vt_geom_t
DTC vector-table geometry.
Definition main.c:160
@ k_dtc_ti_align
TI start address multiple of 16.
Definition main.c:163
@ k_dtc_vt_entries
One pointer per IELSR slot 0..95.
Definition main.c:161
@ k_dtc_vt_align
DTCVBR 1 KB alignment (HUM 18.2.11).
Definition main.c:162
static uint8_t dtc_demo_verify(void)
Verify that s_dst matches s_src element-by-element.
Definition main.c:449
static void dtc_demo_setup_or_halt(void)
Bring CGC + SysTick + ISR + ELC + SCI8 + LEDs + MSTP up.
Definition main.c:285
static void dtc_demo_swevt_isr(void *ctx)
IELSR-slot ISR for the DTC-complete interrupt.
Definition main.c:278
static void dtc_demo_program_ti(void)
Write the 16-byte TI block describing the block copy.
Definition main.c:353
static const uint8_t k_dtc_demo_bad_msg[]
Definition main.c:176
dtc_demo_event_t
ELC software event 0 -> ICU event number.
Definition main.c:97
@ k_dtc_demo_event_swevt0
ELC software event 0 (HUM Table 19.3).
Definition main.c:98
dtc_count_t
DTC count-register values for one 256-word block.
Definition main.c:145
@ k_dtc_cra_block_256
CRAH = CRAL = 0 => 256-unit block.
Definition main.c:146
@ k_dtc_crb_one_block
CRB = 1 => one block per pass.
Definition main.c:147
volatile uint32_t g_dtc_match
1 when the destination matched the source after the last pass.
Definition main.c:205
static void dtc_demo_complete_cb(void *ctx, uint16_t status)
DTC completion callback (fanned out by ra8_dtc_dispatch).
Definition main.c:257
static ra8_err_t dtc_demo_arm_slot(void)
Re-arm DTC activation on the slot and clear any latched IR.
Definition main.c:383
volatile uint32_t g_dtc_bytes
Bytes the DTC moved on the last successful pass (0 on failure).
Definition main.c:213
static void dtc_demo_bringup_or_halt(void)
Initialise the DTC, allocate its activation slot, and enable it.
Definition main.c:409
static ra8_err_t dtc_demo_run_once(uint8_t *out_ok)
Program + activate one DTC block copy and wait for completion.
Definition main.c:479
dtc_mr_pos_t
Bit positions inside the DTC TI MR word.
Definition main.c:128
@ k_dtc_mra_sz_pos
MRA.SZ field position.
Definition main.c:130
@ k_dtc_mrb_byte_pos
MRB byte offset within MR.
Definition main.c:134
@ k_dtc_mra_sm_pos
MRA.SM field position.
Definition main.c:131
@ k_dtc_mra_md_pos
MRA.MD field position.
Definition main.c:129
@ k_dtc_mrb_dm_pos
MRB.DM field position.
Definition main.c:132
@ k_dtc_mra_byte_pos
MRA byte offset within MR.
Definition main.c:133
static void dtc_demo_fill_buffers(void)
Fill s_src with a deterministic pattern and clear s_dst.
Definition main.c:329
dtc_cra_t
CRA=0x0000 encodes a full 256-unit block.
Definition main.c:167
@ k_dtc_cra_block_units
Units per block when CRAH/CRAL = 0.
Definition main.c:168
static void dtc_demo_panic_halt(void)
Park forever after a fatal init failure.
Definition main.c:240
dtc_demo_swevt_t
ELSEGRn index fired by ra8_elc_software_trigger.
Definition main.c:102
@ k_dtc_demo_swevt_index
ELSEGR0 -> ELC_SWEVT0 (0x0CC).
Definition main.c:103
static const char * s_tag
Logging / check tag.
Definition ra8_app.c:17
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
Data Transfer Controller (DTC) driver.
void ra8_dtc_dispatch(void)
Fire the attached activation callback with current DTCSTS.
Definition ra8_dtc.c:180
ra8_err_t ra8_dtc_attach_handler(ra8_dtc_event_fn_t fn, void *ctx)
Attach a shared activation-complete callback.
Definition ra8_dtc.c:172
ra8_err_t ra8_dtc_enable(void)
Start the DTC module.
Definition ra8_dtc.c:104
ra8_err_t ra8_dtc_init(void *vector_base)
Initialise the DTC and install its vector table base.
Definition ra8_dtc.c:60
DTC (Data Transfer Controller) register layout for the RA8D2.
Event Link Controller driver.
ra8_err_t ra8_elc_init(void)
Reset the ELC register block and enable the controller.
Definition ra8_elc.c:91
ra8_err_t ra8_elc_software_trigger(uint8_t event_index)
Fire a software event via one of the ELSEGRn registers.
Definition ra8_elc.c:147
ra8_elc_event_t
Partial list of ELC events (populate as drivers need them).
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
Interrupt Control Unit (ICU) register layout for the Renesas RA8D2.
static volatile uint32_t * ra8_icu_ielsr(uint16_t index)
Get pointer to IELSR register N.
@ k_ra8_ielsr_dtce_mask
DTCE bit mask.
NVIC + ICU IELSR allocator.
ra8_err_t ra8_isr_init(void)
Initialise the ra8_isr table.
Definition ra8_isr.c:229
ra8_err_t ra8_isr_register(ra8_elc_event_t event, ra8_isr_handler_t handler, void *ctx, uint8_t priority, uint16_t *out_slot)
Allocate an IELSR slot for an ELC event + handler.
Definition ra8_isr.c:296
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
One 16-byte Transfer Information (TI) block referenced by the DTC vector table.