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

HAL SysTick + DWT timebase demo for EK-RA8D2 (SCI8 @ 115200). More...

#include <stddef.h>
#include <stdint.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_systick.h"
#include "ra8_time.h"
Include dependency graph for main.c:

Go to the source code of this file.

Enumerations

enum  hal_timebase_config_t : uint32_t {
  k_demo_baud = 115200U ,
  k_demo_period_ms = 1000U ,
  k_demo_tick_hz = 1000U ,
  k_demo_u32_digits = 10U ,
  k_demo_hz_per_mhz = 1000000U ,
  k_demo_dec_base = 10U
}
 Compile-time settings for the demo. More...

Functions

static void demo_panic_halt (void)
 Halt forever in WFI – used as a panic stop on init failure.
static void demo_emit (const uint8_t *data, size_t len)
 Write a byte span to the SCI8 console, ignoring flow-control errors.
static void demo_emit_u32 (uint32_t value)
 Emit a uint32_t as decimal ASCII over the console.
static ra8_err_t demo_setup_or_halt (uint32_t *out_cpuclk_hz)
 Bring up CGC + raw ra8_time + console + LED + the HAL timebase.
static void demo_emit_measurement (uint32_t cpuclk_hz)
 Emit one measurement line: DWT-timed length of a 1 s delay.
void main (void)
 Application entry.

Variables

static const uint8_t k_demo_lbl_reload [] = "reload="
 Label emitted once with the HAL-computed SysTick reload.
static const uint8_t k_demo_lbl_ms [] = "hal timebase ms="
 Label opening each per-second measurement line.
static const uint8_t k_demo_lbl_cycles [] = " cycles="
 Field separator before the DWT cycle count.
static const uint8_t k_demo_lbl_us [] = " us="
 Field separator before the derived microseconds.
static const uint8_t k_demo_crlf [] = "\r\n"
 Line terminator.

Detailed Description

HAL SysTick + DWT timebase demo for EK-RA8D2 (SCI8 @ 115200).

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

Exercises the ra8_core SysTick / DWT timebase primitive (ra8_systick_reload_for, ra8_systick_configure, ra8_systick_current_value, ra8_dwt_cyccnt_enable / ra8_dwt_cyccnt_reset / ra8_dwt_cyccnt_read) on real silicon, ALONGSIDE the existing raw-poke ra8_time path, so the two can be diffed on the bench (issue #582). Both program the same architectural SysTick registers; this app shows the HAL primitive producing the identical reload the raw path uses, and uses the DWT cycle counter to measure the wall-clock length of an ra8_delay_ms() produced by the raw path.

Sequence:

  1. ra8_cgc_init() – XTAL + PLL1 up, CPUCLK0 = 1 GHz, PCLKA = 125 MHz.
  2. ra8_cgc_get_clock_hz(k_ra8_clock_id_cpuclk0, ...) – the live core clock.
  3. ra8_time_init(cpuclk0_hz) – the raw-poke path arms SysTick + DWT and backs ra8_delay_ms().
  4. ra8_board_uart_console_init(115200) – SCI8 J-Link OB console.
  5. ra8_dwt_cyccnt_enable() – the HAL primitive owns the DWT enable.
  6. ra8_systick_reload_for(cpuclk0_hz, 1000, &reload) – HAL arithmetic; print the reload so it can be compared against SYST_RVR on the bench.
  7. ra8_systick_configure(reload, k_ra8_systick_clk_cpu, true) – the HAL path arms the same SysTick the raw path did (same reload, same control bits), so ra8_delay_ms() keeps working.
  8. Loop: reset DWT, ra8_delay_ms(1000), read DWT -> print the measured cycles and derived microseconds, and toggle LED1 as a heartbeat.

Verification: open the J-Link OB CDC port at 115200 8N1. You should see a reload=999999 line once, then a hal timebase ms=1000 cycles=<n> us=<n> line per second with LED1 toggling in lock-step. On a 1 GHz core the measured microseconds land near 1000000 for a 1000 ms delay.

Author
Brighton Sikarskie
Date
2026-08-02
Since
0.1.0

Definition in file main.c.

Enumeration Type Documentation

◆ hal_timebase_config_t

enum hal_timebase_config_t : uint32_t

Compile-time settings for the demo.

Enumerator
k_demo_baud 

Console baud.

k_demo_period_ms 

Heartbeat + measured-delay period.

k_demo_tick_hz 

1 kHz -> 1 ms SysTick tick.

k_demo_u32_digits 

Max decimal digits in a uint32_t.

k_demo_hz_per_mhz 

Hz per MHz -> cycles-per-us divisor.

k_demo_dec_base 

Decimal radix for the number emitter.

Definition at line 58 of file main.c.

Function Documentation

◆ demo_emit()

void demo_emit ( const uint8_t * data,
size_t len )
static

Write a byte span to the SCI8 console, ignoring flow-control errors.

Thin wrapper over the board console so the callers below read as a sequence of field emissions. A transient console error is non-fatal for a demo, so the return is intentionally discarded.

Parameters
[in]dataPointer to the bytes to emit. Must be non-NULL.
[in]lenNumber of bytes to emit. Zero is a no-op.
Precondition
data points to at least len readable bytes.
The console has been initialised.
Postcondition
len bytes have been queued to the console (best effort).
No application state is modified.
Note
Not thread-safe; called only from the single-threaded main loop.
Since
0.1.0

Definition at line 118 of file main.c.

References ra8_board_uart_console_write().

Referenced by demo_emit_measurement(), demo_emit_u32(), and demo_setup_or_halt().

◆ demo_emit_measurement()

void demo_emit_measurement ( uint32_t cpuclk_hz)
static

Emit one measurement line: DWT-timed length of a 1 s delay.

Zeroes the DWT counter, runs ra8_delay_ms(k_demo_period_ms) (the raw path), samples the elapsed cycles, and derives microseconds as cycles / (cpuclk_hz / 1e6). The divisor is floored at one so a sub-MHz clock cannot divide by zero.

Parameters
[in]cpuclk_hzLive CPUCLK0 in Hz, used to convert cycles to us.
Precondition
The DWT cycle counter is enabled and the console is up.
cpuclk_hz is the clock ra8_delay_ms() was calibrated against.
Postcondition
One hal timebase ... line has been emitted.
The DWT counter has been zeroed and has resumed counting.
Note
Not thread-safe; called only from the single-threaded main loop.
Since
0.1.0

Definition at line 249 of file main.c.

References demo_emit(), demo_emit_u32(), k_demo_crlf, k_demo_hz_per_mhz, k_demo_lbl_cycles, k_demo_lbl_ms, k_demo_lbl_us, k_demo_period_ms, ra8_delay_ms(), ra8_dwt_cyccnt_read(), and ra8_dwt_cyccnt_reset().

Referenced by main().

◆ demo_emit_u32()

void demo_emit_u32 ( uint32_t value)
static

Emit a uint32_t as decimal ASCII over the console.

Formats value into a fixed 10-byte scratch buffer from the least significant digit up, then emits the significant span. Uses only single-condition, statically bounded loops (at most 10 iterations) so it carries no compound decision and no unbounded loop.

Parameters
[in]valueValue to print in base ten.
Precondition
The console has been initialised.
None beyond the above.
Postcondition
The decimal text of value has been queued to the console.
No application state is modified.
Note
Not thread-safe; called only from the single-threaded main loop.
Since
0.1.0

Definition at line 141 of file main.c.

References demo_emit(), k_demo_dec_base, and k_demo_u32_digits.

Referenced by demo_emit_measurement(), and demo_setup_or_halt().

◆ demo_panic_halt()

void demo_panic_halt ( void )
static

Halt forever in WFI – used as a panic stop on init failure.

Parks the core so a boot failure is a visible dead LED / silent console rather than undefined execution. Only a debugger or an external reset resumes it.

Precondition
Called only after a fatal error during boot.
The console may or may not be up; no output is attempted here.
Postcondition
The CPU is parked in a WFI spin.
No further application code runs.
Note
Never returns.
Since
0.1.0

Definition at line 93 of file main.c.

Referenced by demo_fs_format_or_panic(), demo_lx_format_or_panic(), demo_lx_open_or_panic(), demo_setup_or_halt(), demo_setup_or_halt(), and main().

◆ demo_setup_or_halt()

ra8_err_t demo_setup_or_halt ( uint32_t * out_cpuclk_hz)
static

Bring up CGC + raw ra8_time + console + LED + the HAL timebase.

Runs the raw ra8_time_init() path first (it arms SysTick + DWT and backs ra8_delay_ms()), then re-arms the SAME SysTick through the HAL primitive with the reload the HAL arithmetic derives – demonstrating the primitive substitutes for the raw poke with no timing change. Panic-halts on any failure. On success the HAL-computed reload has been emitted once.

Parameters
[out]out_cpuclk_hzReceives the live CPUCLK0 frequency in Hz.
Returns
Error code.
Return values
k_ra8_okEverything up; *out_cpuclk_hz is valid.
k_ra8_err_invalid_argout_cpuclk_hz is NULL.
Precondition
out_cpuclk_hz is a valid, writable uint32_t.
Reset_Handler and SystemInit have completed.
Postcondition
On success SysTick + DWT are running and the console is up.
On any HAL failure the function does not return (panic-halt).
Note
Not thread-safe; one-shot boot helper.
Since
0.1.0

Definition at line 187 of file main.c.

References demo_emit(), demo_emit_u32(), demo_panic_halt(), k_demo_baud, k_demo_crlf, k_demo_lbl_reload, k_demo_tick_hz, k_ra8_board_led1, k_ra8_clock_id_cpuclk0, k_ra8_err_invalid_arg, k_ra8_ok, k_ra8_systick_clk_cpu, ra8_board_led_init(), ra8_board_uart_console_init(), ra8_cgc_get_clock_hz(), ra8_cgc_init(), ra8_dwt_cyccnt_enable(), ra8_systick_configure(), ra8_systick_reload_for(), and ra8_time_init().

◆ main()

void main ( void )

Application entry.

The application entry point Reset_Handler hands control to.

Brings up the timebase then measures 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 measure + blink loop forever.
On any HAL init failure the function halts in WFI.
Note
Never returns.
Since
0.1.0

Definition at line 281 of file main.c.

References demo_emit_measurement(), demo_panic_halt(), demo_setup_or_halt(), k_ra8_board_led1, k_ra8_ok, ra8_board_led_toggle(), and ra8_isr_globals_enable().

Variable Documentation

◆ k_demo_crlf

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

Line terminator.

Definition at line 76 of file main.c.

Referenced by demo_emit_measurement(), and demo_setup_or_halt().

◆ k_demo_lbl_cycles

const uint8_t k_demo_lbl_cycles[] = " cycles="
static

Field separator before the DWT cycle count.

Definition at line 72 of file main.c.

Referenced by demo_emit_measurement().

◆ k_demo_lbl_ms

const uint8_t k_demo_lbl_ms[] = "hal timebase ms="
static

Label opening each per-second measurement line.

Definition at line 70 of file main.c.

Referenced by demo_emit_measurement().

◆ k_demo_lbl_reload

const uint8_t k_demo_lbl_reload[] = "reload="
static

Label emitted once with the HAL-computed SysTick reload.

Definition at line 68 of file main.c.

Referenced by demo_setup_or_halt().

◆ k_demo_lbl_us

const uint8_t k_demo_lbl_us[] = " us="
static

Field separator before the derived microseconds.

Definition at line 74 of file main.c.

Referenced by demo_emit_measurement().