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

RTC alarm + UART log demo for EK-RA8D2. 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_rtc.h"
#include "ra8_time.h"
Include dependency graph for main.c:

Go to the source code of this file.

Enumerations

enum  rtc_demo_const_t : uint32_t {
  k_rtc_demo_baud = 115200U ,
  k_rtc_demo_poll_ms = 100U ,
  k_rtc_demo_advance_secs = 10U ,
  k_rtc_demo_ms_per_sec = 1000U
}
 Demo tunables. More...
enum  rtc_demo_calendar_t : uint8_t {
  k_rtc_demo_secs_per_min = 60U ,
  k_rtc_demo_mins_per_hour = 60U ,
  k_rtc_demo_hours_per_day = 24U
}
 Calendar-arithmetic moduli for the +5 s alarm calc. More...
enum  rtc_demo_seed_t : uint16_t {
  k_rtc_demo_alarm_offset_s = 5U ,
  k_rtc_demo_seed_year_lo = 26U ,
  k_rtc_demo_year_base = 2000U ,
  k_rtc_demo_seed_month = 1U ,
  k_rtc_demo_seed_day = 1U
}
 Alarm offset from the current RTC reading. More...

Functions

static void internal_rtc_demo_panic_halt (void)
 Park the processor after an unrecoverable periodic-RTC failure.
static void internal_rtc_demo_setup_or_halt (void)
 Initialize clocks, console, and the periodic demo's RTC seed.
static ra8_err_t internal_rtc_demo_arm_alarm (const ra8_rtc_datetime_t *now)
 Program a +5 s alarm relative to now and enable AIE.
void main (void)
 The application entry point Reset_Handler hands control to.

Variables

static const uint8_t s_rtc_demo_log_msg [] = "rtc_per: tick\r\n"
 Fixed UART diagnostics for boot and periodic RTC events.
static const uint8_t s_rtc_demo_boot_msg [] = "rtc_per: boot\r\n"

Detailed Description

RTC alarm + UART log demo for EK-RA8D2.

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

Sets the on-chip RTC to a known seed time (2026-01-01 00:00:00), schedules an alarm 5 seconds in the future via the new ra8_rtc_set_alarm API, enables RCR1.AIE, and polls ra8_rtc_get_status for the alarm-fired flag (no NVIC needed in this minimal demo). When the alarm fires, the demo logs the wall time over SCI8 (115200 8N1, on the J-Link OB CDC port), clears the status bit, advances the seed by ten seconds, and re-arms.

Sequence:

  1. CGC + SysTick + UART (SCI8 on PD_02 / PD_03) bring-up.
  2. ra8_rtc_init() – start the RTC in 24-hour calendar mode.
  3. ra8_rtc_set() to the seed datetime.
  4. Loop: programme the +5 s alarm, poll for the alarm flag, log "alarm fired", advance the seed by ten seconds.
Since
0.1.0

Definition in file main.c.

Enumeration Type Documentation

◆ rtc_demo_calendar_t

enum rtc_demo_calendar_t : uint8_t

Calendar-arithmetic moduli for the +5 s alarm calc.

Enumerator
k_rtc_demo_secs_per_min 

Rtc demo secs per minimum.

k_rtc_demo_mins_per_hour 

Rtc demo mins per hour.

k_rtc_demo_hours_per_day 

Rtc demo hours per day.

Definition at line 49 of file main.c.

◆ rtc_demo_const_t

enum rtc_demo_const_t : uint32_t

Demo tunables.

Enumerator
k_rtc_demo_baud 

Rtc demo baud.

k_rtc_demo_poll_ms 

Rtc demo poll ms.

k_rtc_demo_advance_secs 

Rtc demo advance secs.

k_rtc_demo_ms_per_sec 

Rtc demo ms per sec.

Definition at line 41 of file main.c.

◆ rtc_demo_seed_t

enum rtc_demo_seed_t : uint16_t

Alarm offset from the current RTC reading.

Enumerator
k_rtc_demo_alarm_offset_s 

Rtc demo alarm offset s.

k_rtc_demo_seed_year_lo 

2026 - 2000.

k_rtc_demo_year_base 

Calendar epoch base for the year field.

k_rtc_demo_seed_month 

Rtc demo seed month.

k_rtc_demo_seed_day 

Rtc demo seed day.

Definition at line 56 of file main.c.

Function Documentation

◆ internal_rtc_demo_arm_alarm()

ra8_err_t internal_rtc_demo_arm_alarm ( const ra8_rtc_datetime_t * now)
staticnodiscard

Program a +5 s alarm relative to now and enable AIE.

Carries the five-second offset through seconds, minutes, and hours, programs that calendar alarm, then enables both alarm and periodic RTC status sources used by the polling loop.

Parameters
[in]nowCurrent RTC calendar value used as the alarm origin.
Returns
ra8_err_t Status from alarm programming or IRQ-source enablement.
Return values
k_ra8_okThe alarm and both requested status sources were enabled.
(other)The first RTC driver error.
Precondition
now points to a valid initialized calendar value.
The RTC was initialized and its count source is running.
Postcondition
On success an alarm five seconds after now is active.
If alarm programming fails, IRQ-source enablement is not attempted.
Note
Hour carry wraps at 24; this demonstration does not require date carry.
MC/DC:
Compound decision: ra8_rtc_set_alarm != ok || ra8_rtc_set_irq_enable != ok. Two atomic conditions x N+1 = 3 vectors – both ok (steady state) plus each branch fails (covered in test_app_rtc_alarm.c).
Since
0.1.0

Definition at line 170 of file main.c.

References ra8_rtc_datetime_t::hour, k_ra8_ok, k_ra8_rtc_irq_alarm, k_ra8_rtc_irq_periodic, k_rtc_demo_alarm_offset_s, k_rtc_demo_hours_per_day, k_rtc_demo_mins_per_hour, k_rtc_demo_secs_per_min, ra8_rtc_datetime_t::minute, ra8_rtc_set_alarm(), ra8_rtc_set_irq_enable(), and ra8_rtc_datetime_t::second.

◆ internal_rtc_demo_panic_halt()

void internal_rtc_demo_panic_halt ( void )
static

Park the processor after an unrecoverable periodic-RTC failure.

Repeats wait-for-interrupt forever, preserving the RTC and console state at the failure point for an attached debugger.

Returns
None.
Precondition
The caller has no remaining recovery path to attempt.
Any diagnostic UART write that must be retained is complete.
Postcondition
The function never returns to its caller.
No new RTC alarms or periodic-event polls are initiated.
Note
Interrupt wakeups return immediately to the enclosing permanent loop.
Since
0.1.0

Definition at line 84 of file main.c.

References RA8_INTERNAL.

◆ internal_rtc_demo_setup_or_halt()

void internal_rtc_demo_setup_or_halt ( void )
static

Initialize clocks, console, and the periodic demo's RTC seed.

Starts the clock tree and millisecond time base, opens the board UART console, initializes the RTC, and installs the fixed 2026-01-01 calendar value. Any dependency error enters the fatal halt rather than returning partially initialized state.

Returns
None.
Precondition
Reset-time platform initialization configured the core and vector table.
The board console and RTC clock source are available to this image.
Postcondition
On success the console and RTC calendar are ready for the poll loop.
On failure the function never returns to its caller.
Note
Single-shot boot helper; it is not reentrant.
Since
0.1.0

Definition at line 109 of file main.c.

References internal_rtc_demo_panic_halt(), k_ra8_clock_id_cpuclk0, k_ra8_ok, k_rtc_demo_baud, k_rtc_demo_seed_day, k_rtc_demo_seed_month, k_rtc_demo_seed_year_lo, k_rtc_demo_year_base, ra8_board_uart_console_init(), ra8_cgc_get_clock_hz(), ra8_cgc_init(), RA8_INTERNAL, ra8_rtc_init(), ra8_rtc_set(), and ra8_time_init().

◆ 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 193 of file main.c.

References internal_rtc_demo_arm_alarm(), internal_rtc_demo_panic_halt(), internal_rtc_demo_setup_or_halt(), k_ra8_ok, k_ra8_rtc_irq_alarm, k_ra8_rtc_irq_periodic, k_rtc_demo_advance_secs, k_rtc_demo_ms_per_sec, k_rtc_demo_poll_ms, ra8_board_uart_console_write(), ra8_delay_ms(), ra8_isr_globals_enable(), ra8_rtc_clear_status(), ra8_rtc_get(), ra8_rtc_get_status(), s_rtc_demo_boot_msg, and s_rtc_demo_log_msg.

Variable Documentation

◆ s_rtc_demo_boot_msg

const uint8_t s_rtc_demo_boot_msg[] = "rtc_per: boot\r\n"
static

Definition at line 66 of file main.c.

◆ s_rtc_demo_log_msg

const uint8_t s_rtc_demo_log_msg[] = "rtc_per: tick\r\n"
static

Fixed UART diagnostics for boot and periodic RTC events.

Definition at line 65 of file main.c.