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
28
29#include <stdint.h>
30
31#include "ra8_attributes.h"
32#include "ra8_board_ek_ra8d2.h"
33#include "ra8_boot_entry.h"
34#include "ra8_cgc.h"
35#include "ra8_err.h"
36#include "ra8_isr.h"
37#include "ra8_rtc.h"
38#include "ra8_time.h"
39
47
54
63
64static const uint8_t s_rtc_demo_log_msg[] = "rtc: alarm fired\r\n";
65static const uint8_t s_rtc_demo_boot_msg[] = "rtc: boot\r\n";
66
85{
86 while (1) {
87 __asm__ volatile("wfi");
88 }
89}
90
110{
111 uint32_t cpuclk0_hz = 0U;
112 if (ra8_cgc_init() != k_ra8_ok) {
114 }
117 }
118 if (ra8_time_init(cpuclk0_hz) != k_ra8_ok) {
120 }
123 }
124 /* Enable global interrupts before the RTC count-source bring-up: that
125 * step blocks on ra8_delay_ms for the sub-clock stabilization time, and
126 * with IRQs unmasked ra8_delay_ms advances off the SysTick tick counter
127 * (the demo registers no RTC NVIC handler -- it polls the status flag). */
129 /* Bring up and select the RTC count source BEFORE ra8_rtc_init(), or the
130 * counter never advances and the +5 s alarm never fires. Primary source is
131 * the EK-RA8D2 32.768 kHz sub-clock crystal (accurate). On a board without
132 * the crystal, swap the argument to k_ra8_rtc_clk_loco (internal LOCO,
133 * crystal-free) and re-flash. */
136 }
137 if (ra8_rtc_init() != k_ra8_ok) {
139 }
140 const ra8_rtc_datetime_t seed = {
141 .year = (uint16_t)(k_rtc_demo_year_base + k_rtc_demo_seed_year_lo),
142 .month = (uint8_t)k_rtc_demo_seed_month,
143 .day = (uint8_t)k_rtc_demo_seed_day,
144 .weekday = 0U,
145 .hour = 0U,
146 .minute = 0U,
147 .second = 0U,
148 };
149 if (ra8_rtc_set(&seed) != k_ra8_ok) {
151 }
152}
153
182[[nodiscard]] RA8_INTERNAL static ra8_err_t
184{
185 ra8_rtc_datetime_t a = *now;
186 uint16_t s = (uint16_t)a.second + (uint16_t)k_rtc_demo_alarm_offset_s;
187 a.second = (uint8_t)(s % (uint16_t)k_rtc_demo_secs_per_min);
188 uint16_t add_min = (uint16_t)(s / (uint16_t)k_rtc_demo_secs_per_min);
189 uint16_t m = (uint16_t)a.minute + add_min;
190 a.minute = (uint8_t)(m % (uint16_t)k_rtc_demo_mins_per_hour);
191 uint16_t add_hr = (uint16_t)(m / (uint16_t)k_rtc_demo_mins_per_hour);
192 uint16_t h = (uint16_t)a.hour + add_hr;
193 a.hour = (uint8_t)(h % (uint16_t)k_rtc_demo_hours_per_day);
195 if (err != k_ra8_ok) {
196 return err;
197 }
199}
200
201void main(void)
202{
204
205 /* Boot banner -- emit before the RTC poll loop so the HIL host can
206 * confirm the firmware booted even when the sub-clock crystal is not
207 * running and the alarm-fired path never reaches its own write.
208 * (Global IRQs were already enabled in internal_rtc_demo_setup_or_halt.) */
210 (size_t)(sizeof(s_rtc_demo_boot_msg) - 1U));
211
212 while (1) {
213 ra8_rtc_datetime_t now = {};
214 if (ra8_rtc_get(&now) != k_ra8_ok) {
215 break;
216 }
218 break;
219 }
220
221 /* Poll the alarm flag -- on real silicon RCR1.AIE drives an NVIC
222 * line, but for this demo we just spin against the status bit. */
223 uint8_t status = 0U;
224 do {
226 if (ra8_rtc_get_status(&status) != k_ra8_ok) {
228 }
229 } while ((status & (uint8_t)k_ra8_rtc_irq_alarm) == 0U);
230
232 (size_t)(sizeof(s_rtc_demo_log_msg) - 1U)) != k_ra8_ok) {
233 break;
234 }
236 break;
237 }
239 }
241}
void main(void)
Secure fallback main entry point.
Definition main.c:37
static void internal_rtc_demo_setup_or_halt(void)
Initialize clocks, timekeeping, console, and RTC calendar state.
Definition main.c:109
rtc_demo_seed_t
Alarm offset from the current RTC reading.
Definition main.c:56
@ k_rtc_demo_seed_day
Rtc demo seed day.
Definition main.c:61
@ k_rtc_demo_seed_month
Rtc demo seed month.
Definition main.c:60
@ k_rtc_demo_alarm_offset_s
Rtc demo alarm offset s.
Definition main.c:57
@ k_rtc_demo_seed_year_lo
2026 - 2000.
Definition main.c:58
@ k_rtc_demo_year_base
Calendar epoch base for the year field.
Definition main.c:59
static const uint8_t s_rtc_demo_log_msg[]
Definition main.c:64
static const uint8_t s_rtc_demo_boot_msg[]
Definition main.c:65
static void internal_rtc_demo_panic_halt(void)
Park the processor after an unrecoverable RTC demo failure.
Definition main.c:84
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.
Definition main.c:183
rtc_demo_calendar_t
Calendar-arithmetic moduli for the +5 s alarm calc.
Definition main.c:49
@ k_rtc_demo_mins_per_hour
Rtc demo mins per hour.
Definition main.c:51
@ k_rtc_demo_hours_per_day
Rtc demo hours per day.
Definition main.c:52
@ k_rtc_demo_secs_per_min
Rtc demo secs per minimum.
Definition main.c:50
rtc_demo_const_t
Demo tunables.
Definition main.c:41
@ k_rtc_demo_ms_per_sec
Rtc demo ms per sec.
Definition main.c:45
@ k_rtc_demo_baud
Rtc demo baud.
Definition main.c:42
@ k_rtc_demo_poll_ms
Rtc demo poll ms.
Definition main.c:43
@ k_rtc_demo_advance_secs
Rtc demo advance secs.
Definition main.c:44
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_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
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
Real-Time Clock driver (BCD calendar mode).
ra8_err_t ra8_rtc_clock_init(ra8_rtc_clk_src_t src)
Bring up and select the RTC count source (HUM Fig 26.3).
Definition ra8_rtc.c:260
ra8_err_t ra8_rtc_set_alarm(const ra8_rtc_datetime_t *alarm)
Programme the alarm-match registers (hour/minute/second).
Definition ra8_rtc.c:437
ra8_err_t ra8_rtc_init(void)
Start the RTC in 24-hour calendar mode.
Definition ra8_rtc.c:308
ra8_err_t ra8_rtc_clear_status(uint8_t mask)
Clear RCR1 IRQ enable bits.
Definition ra8_rtc.c:513
@ k_ra8_rtc_clk_subclock
RCKSEL=0: 32.768 kHz sub-clock crystal (SOSC).
Definition ra8_rtc.h:58
ra8_err_t ra8_rtc_set(const ra8_rtc_datetime_t *dt)
Write the calendar registers.
Definition ra8_rtc.c:343
@ k_ra8_rtc_irq_alarm
RCR1.AIE.
Definition ra8_rtc.h:139
ra8_err_t ra8_rtc_get(ra8_rtc_datetime_t *out)
Read the calendar registers.
Definition ra8_rtc.c:385
ra8_err_t ra8_rtc_get_status(uint8_t *out_mask)
Read RCR1 IRQ enable bits.
Definition ra8_rtc.c:506
ra8_err_t ra8_rtc_set_irq_enable(uint8_t mask)
Enable one or more RTC IRQ sources via RCR1.
Definition ra8_rtc.c:498
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
Calendar date + time decoded from BCD.
Definition ra8_rtc.h:31
uint8_t hour
0..23.
Definition ra8_rtc.h:36
uint8_t minute
0..59.
Definition ra8_rtc.h:37
uint8_t second
0..59.
Definition ra8_rtc.h:38