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
48
49#include <stdint.h>
50
51#include "ra8_attributes.h"
52#include "ra8_board_ek_ra8d2.h"
53#include "ra8_boot_entry.h"
54#include "ra8_cgc.h"
55#include "ra8_err.h"
56#include "ra8_isr.h"
57
58/*
59 * The host unit-test build (RA8_OFF_TARGET) does not link the
60 * ThreadX vendor tree, so `tx_api.h` (and the ThreadX-port header
61 * `ra8_threadx.h`) are pulled in only on the cross-compile target.
62 */
63#ifndef RA8_OFF_TARGET
64#include "ra8_threadx.h"
65#include "tx_api.h"
66#endif
67
68/* ---------------------------------------------------------------------------
69 * Tunables (typed enums per the project's no-magic-number rule).
70 * --------------------------------------------------------------------------- */
71
75typedef enum : uint16_t {
78
82typedef enum : uint8_t {
85
89typedef enum : uint16_t {
92
101typedef enum : uintptr_t {
102 k_retune_syst_rvr_addr = 0xE000E014UL,
104
105/* ---------------------------------------------------------------------------
106 * HIL / EIL observables. Global (non-static) + volatile so the symbols stay
107 * linker-visible for J-Link / ra8_emulator --dump-sym and survive optimization.
108 * --------------------------------------------------------------------------- */
109
116volatile uint32_t g_threadx_retune_tick = 0U;
117
125volatile uint32_t g_threadx_retune_reload = 0U;
126
135volatile uint32_t g_threadx_retune_bad = 0U;
136
137#ifndef RA8_OFF_TARGET
138/* ---------------------------------------------------------------------------
139 * Static thread + stack storage (cross build only -- TX_THREAD is a vendor
140 * type unavailable on the host clang-tidy pass).
141 * --------------------------------------------------------------------------- */
142
146[[gnu::aligned(8)]] static uint8_t s_retune_stack[k_retune_thread_stack_bytes];
147
152
153/* SysTick handler lives in libs/ra8_core/src/ra8_time.c -- the shared weak
154 * SysTick_Handler dispatches to ThreadX via a weak extern to
155 * `_tx_timer_interrupt`, so no per-app override is needed (issue #8). */
156
175{
176 volatile const uint32_t* rvr = (volatile const uint32_t*)k_retune_syst_rvr_addr;
177 return *rvr;
178}
179
180/* ---------------------------------------------------------------------------
181 * Thread body.
182 * --------------------------------------------------------------------------- */
183
203{
204 (void)thread_input;
205 while (1) {
209 }
210}
211
212/* ---------------------------------------------------------------------------
213 * tx_application_define -- ThreadX calls this once, after
214 * _tx_initialize_low_level but before the first scheduling decision.
215 * --------------------------------------------------------------------------- */
216
227void tx_application_define(void* first_unused_memory)
228{
229 static CHAR s_thread_name[] = "retune";
230
231 (void)first_unused_memory;
232
233 /* Reprogram SysTick.LOAD from the live CPUCLK0 (issue #287). */
234 const ra8_err_t retune_err = ra8_threadx_systick_retune();
235
236 /* Independently recompute the expected reload from the live clock and
237 * compare against what retune actually left in SYST_RVR. Any deviation
238 * means the kernel tick would drift, so flag it for the probe. */
239 uint32_t cpuclk_hz = 0U;
240 uint32_t expected = 0U;
241 const ra8_err_t clk_err = ra8_cgc_get_clock_hz(k_ra8_clock_id_cpuclk0, &cpuclk_hz);
242 const ra8_err_t rel_err =
243 ra8_threadx_systick_reload_for(cpuclk_hz, (uint32_t)k_ra8_threadx_tick_hz, &expected);
244 const uint32_t live_rvr = internal_retune_read_syst_rvr();
245
246 g_threadx_retune_reload = expected;
247 if ((retune_err != k_ra8_ok) || (clk_err != k_ra8_ok) || (rel_err != k_ra8_ok) ||
248 (live_rvr != expected)) {
250 }
251
255 0U,
262 if (err != TX_SUCCESS) {
263 while (1) {
264 __asm__ volatile("wfi");
265 }
266 }
267}
268#endif /* !RA8_OFF_TARGET */
269
270/* ---------------------------------------------------------------------------
271 * main() -- raise the clock, bring up the LED, then drop into ThreadX.
272 * --------------------------------------------------------------------------- */
273
284void main(void)
285{
286 /* Raise CPUCLK0 to the PLL1 target so the retune has a real
287 * high-frequency clock to program SysTick against. */
288 if (ra8_cgc_init() != k_ra8_ok) {
289 while (1) {
290 __asm__ volatile("wfi");
291 }
292 }
294 while (1) {
295 __asm__ volatile("wfi");
296 }
297 }
298
299#ifndef RA8_OFF_TARGET
301 tx_kernel_enter();
302#endif
303
304 while (1) {
305 __asm__ volatile("wfi");
306 }
307}
void main(void)
Secure fallback main entry point.
Definition main.c:37
void tx_application_define(void *first_unused_memory)
ThreadX system-define hook: build worker thread + byte pool.
Definition main.c:942
static CHAR s_thread_name[]
Definition main.c:212
volatile uint32_t g_threadx_retune_tick
Scheduler-liveness counter – bumped by the worker on each wake.
Definition main.c:116
retune_stack_t
Stack size, in bytes, for the worker thread.
Definition main.c:75
@ k_retune_thread_stack_bytes
Retune thread stack bytes.
Definition main.c:76
static uint8_t s_retune_stack[k_retune_thread_stack_bytes]
Stack for the worker thread (32-bit aligned per ARMv8-M AAPCS).
Definition main.c:146
static uint32_t internal_retune_read_syst_rvr(void)
Read back the SysTick reload register (SYST_RVR).
Definition main.c:174
retune_period_t
Worker sleep period in ThreadX ticks (tx_user.h pins a tick to 1 ms).
Definition main.c:89
@ k_retune_loop_ticks
Retune loop ticks.
Definition main.c:90
static TX_THREAD s_retune_thread
Worker thread control block.
Definition main.c:151
volatile uint32_t g_threadx_retune_reload
SysTick reload the retune computed for the live CPUCLK0.
Definition main.c:125
volatile uint32_t g_threadx_retune_bad
Non-zero iff the retune failed or SYST_RVR did not match the independently-computed reload for the li...
Definition main.c:135
static void internal_retune_thread_entry(ULONG thread_input)
Worker entry: toggle LED1 and advance the liveness counter.
Definition main.c:202
retune_priority_t
Thread priority for the worker thread.
Definition main.c:82
@ k_retune_thread_priority
Retune thread priority.
Definition main.c:83
retune_syst_addr_t
SysTick reload register address (Cortex-M System Control Space).
Definition main.c:101
@ k_retune_syst_rvr_addr
Retune syst rvr address.
Definition main.c:102
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_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_led1
LED1, BLUE, P600 (jumper E27).
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
RA8 <-> Eclipse ThreadX glue: SysTick kernel-tick retuning.
@ k_ra8_threadx_tick_hz
1 kHz -> 1 ms kernel tick.
Definition ra8_threadx.h:61
ra8_err_t ra8_threadx_systick_reload_for(uint32_t cpuclk_hz, uint32_t tick_hz, uint32_t *out_reload)
Compute the SysTick reload for a given core clock + tick rate.
ra8_err_t ra8_threadx_systick_retune(void)
Reprogram SysTick.LOAD from the live CPUCLK0 rate.
unsigned int UINT
ThreadX-compatible unsigned int (host stub).
#define TX_SUCCESS
#define tx_thread_create
#define tx_thread_sleep
#define TX_NO_TIME_SLICE
char CHAR
ThreadX-compatible CHAR (host stub).
#define TX_AUTO_START
unsigned long ULONG
ThreadX-compatible unsigned long (host stub).
Opaque thread stand-in for the host build.