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

Eclipse ThreadX SysTick retune-to-live-CPUCLK0 demo (issue #287). 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_threadx.h"
#include "tx_api.h"
Include dependency graph for main.c:

Go to the source code of this file.

Enumerations

enum  retune_stack_t : uint16_t { k_retune_thread_stack_bytes = 1024U }
 Stack size, in bytes, for the worker thread. More...
enum  retune_priority_t : uint8_t { k_retune_thread_priority = 4U }
 Thread priority for the worker thread. More...
enum  retune_period_t : uint16_t { k_retune_loop_ticks = 250U }
 Worker sleep period in ThreadX ticks (tx_user.h pins a tick to 1 ms). More...
enum  retune_syst_addr_t : uintptr_t { k_retune_syst_rvr_addr = 0xE000E014UL }
 SysTick reload register address (Cortex-M System Control Space). More...

Functions

static uint32_t internal_retune_read_syst_rvr (void)
 Read back the SysTick reload register (SYST_RVR).
static void internal_retune_thread_entry (ULONG thread_input)
 Worker entry: toggle LED1 and advance the liveness counter.
void tx_application_define (void *first_unused_memory)
 ThreadX application-define callback: retune SysTick, then spawn.
void main (void)
 Application entry.

Variables

volatile uint32_t g_threadx_retune_tick = 0U
 Scheduler-liveness counter – bumped by the worker on each wake.
volatile uint32_t g_threadx_retune_reload = 0U
 SysTick reload the retune computed for the live CPUCLK0.
volatile uint32_t g_threadx_retune_bad = 0U
 Non-zero iff the retune failed or SYST_RVR did not match the independently-computed reload for the live clock.
static uint8_t s_retune_stack [k_retune_thread_stack_bytes]
 Stack for the worker thread (32-bit aligned per ARMv8-M AAPCS).
static TX_THREAD s_retune_thread
 Worker thread control block.

Detailed Description

Eclipse ThreadX SysTick retune-to-live-CPUCLK0 demo (issue #287).

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

Proves that ra8_threadx_systick_retune reprograms SysTick.LOAD from the live CPUCLK0 rate so the ThreadX kernel tick is accurate no matter which clock the app raised. Unlike threadx_blink – which relies on ra8_cgc_init() bringing CPUCLK0 to exactly the 1 GHz the tx_initialize_low_level.S reload assumes – this app additionally calls the retune hook from tx_application_define and self-checks the result:

  1. main() raises CPUCLK0 to the PLL1 target with ra8_cgc_init().
  2. tx_application_define (which ThreadX runs after _tx_initialize_low_level but before scheduling) calls ra8_threadx_systick_retune.
  3. It then independently recomputes the expected reload with ra8_threadx_systick_reload_for from the live CPUCLK0 and reads SYST_RVR back. Any mismatch (or a retune error) bumps g_threadx_retune_bad; the computed reload is published in g_threadx_retune_reload for observability.
  4. One worker thread toggles LED1 and advances g_threadx_retune_tick every wake – the scheduler-liveness counter the HIL / ra8_emulator probe samples.
HIL / EIL contract
hil.conf uses HIL_MODE=jlink_memprobe:
  • primary g_threadx_retune_tick must advance >= 3 (scheduler alive),
  • failure g_threadx_retune_bad must stay 0 (retune matched the live clock). ra8_emulator runs the identical ARM retune path – the SYST_RVR write lands in the emulated System Control Space and reads back – so EIL == HIL.
Threads
Name Priority Period Action
retune 4 250 ms loop Toggle LED1, bump tick
Since
0.1.0

Definition in file main.c.

Enumeration Type Documentation

◆ retune_period_t

enum retune_period_t : uint16_t

Worker sleep period in ThreadX ticks (tx_user.h pins a tick to 1 ms).

Enumerator
k_retune_loop_ticks 

Retune loop ticks.

Definition at line 89 of file main.c.

◆ retune_priority_t

enum retune_priority_t : uint8_t

Thread priority for the worker thread.

Enumerator
k_retune_thread_priority 

Retune thread priority.

Definition at line 82 of file main.c.

◆ retune_stack_t

enum retune_stack_t : uint16_t

Stack size, in bytes, for the worker thread.

Enumerator
k_retune_thread_stack_bytes 

Retune thread stack bytes.

Definition at line 75 of file main.c.

◆ retune_syst_addr_t

enum retune_syst_addr_t : uintptr_t

SysTick reload register address (Cortex-M System Control Space).

SYST_RVR is architectural (Arm), not a Renesas peripheral, so it needs no HUM citation. Read back here to confirm what the retune actually programmed.

Enumerator
k_retune_syst_rvr_addr 

Retune syst rvr address.

Definition at line 101 of file main.c.

Function Documentation

◆ internal_retune_read_syst_rvr()

uint32_t internal_retune_read_syst_rvr ( void )
static

Read back the SysTick reload register (SYST_RVR).

Performs one volatile read from the architected Cortex-M System Control Space address used to verify the ThreadX clock retune.

Returns
Current SYST_RVR value (the reload the kernel tick uses).
Return values
0..k_ra8_systick_reload_maxWhatever was last programmed.
Precondition
The System Control Space is mapped (always true on Cortex-M).
_tx_initialize_low_level / the retune has run.
Postcondition
No state modified (pure read).
Returned value reflects the live SYST_RVR.
Note
Not called on the host build; SCS is unmapped there.
Since
0.1.0

Definition at line 174 of file main.c.

References k_retune_syst_rvr_addr, and RA8_INTERNAL.

Referenced by tx_application_define().

◆ internal_retune_thread_entry()

void internal_retune_thread_entry ( ULONG thread_input)
static

Worker entry: toggle LED1 and advance the liveness counter.

Repeats the visible LED toggle and externally observed counter increment at the configured ThreadX sleep cadence.

Parameters
[in]thread_inputUnused (ThreadX cookie).
Returns
None.
Precondition
The ThreadX scheduler is running.
LED1 has been configured by main().
Postcondition
LED1 toggles once per loop; g_threadx_retune_tick advances.
The thread yields the CPU via tx_thread_sleep.
Note
Permanent worker used only by this retune validation image.
Since
0.1.0

Definition at line 202 of file main.c.

References g_threadx_retune_tick, k_ra8_board_led1, k_retune_loop_ticks, ra8_board_led_toggle(), RA8_INTERNAL, and tx_thread_sleep.

Referenced by tx_application_define().

◆ main()

void main ( void )

Application entry.

The application entry point Reset_Handler hands control to.

Raises CPUCLK0, configures LED1, launches ThreadX.

Precondition
Reset_Handler has copied .data + zeroed .bss.
SystemInit has set VTOR, FPU, and priority grouping.
Postcondition
CPUCLK0 is at the PLL1 target before the kernel starts.
On any HAL init failure the function halts in __WFI.
Since
0.1.0

Definition at line 284 of file main.c.

References k_ra8_board_led1, k_ra8_ok, ra8_board_led_init(), ra8_cgc_init(), and ra8_isr_globals_enable().

◆ tx_application_define()

void tx_application_define ( void * first_unused_memory)

ThreadX application-define callback: retune SysTick, then spawn.

Parameters
[in]first_unused_memoryUnused – the demo uses a static stack.
Precondition
Called from _tx_initialize_kernel_enter with IRQs masked.
ra8_cgc_init() has published the live CPUCLK0 rate.
Postcondition
SysTick is retuned to the live clock (or g_threadx_retune_bad set).
The worker thread is created and auto-started.

Definition at line 227 of file main.c.

References g_threadx_retune_bad, g_threadx_retune_reload, internal_retune_read_syst_rvr(), internal_retune_thread_entry(), k_ra8_clock_id_cpuclk0, k_ra8_ok, k_ra8_threadx_tick_hz, k_retune_thread_priority, k_retune_thread_stack_bytes, ra8_cgc_get_clock_hz(), ra8_threadx_systick_reload_for(), ra8_threadx_systick_retune(), s_retune_stack, s_retune_thread, s_thread_name, TX_AUTO_START, TX_NO_TIME_SLICE, TX_SUCCESS, and tx_thread_create.

Variable Documentation

◆ g_threadx_retune_bad

volatile uint32_t g_threadx_retune_bad = 0U

Non-zero iff the retune failed or SYST_RVR did not match the independently-computed reload for the live clock.

Note
The memprobe asserts this stays 0. Written once at init.
Warning
A non-zero value means the kernel tick would drift.
Since
0.1.0

Definition at line 135 of file main.c.

Referenced by tx_application_define().

◆ g_threadx_retune_reload

volatile uint32_t g_threadx_retune_reload = 0U

SysTick reload the retune computed for the live CPUCLK0.

Published for observability (999999 at CPUCLK0 = 1 GHz).

Note
Written once from tx_application_define; read externally only.
Since
0.1.0

Definition at line 125 of file main.c.

Referenced by tx_application_define().

◆ g_threadx_retune_tick

volatile uint32_t g_threadx_retune_tick = 0U

Scheduler-liveness counter – bumped by the worker on each wake.

Note
Read externally by the memprobe; firmware never reads it back.
Since
0.1.0

Definition at line 116 of file main.c.

Referenced by internal_retune_thread_entry().

◆ s_retune_stack

uint8_t s_retune_stack[k_retune_thread_stack_bytes]
static

Stack for the worker thread (32-bit aligned per ARMv8-M AAPCS).

Definition at line 146 of file main.c.

Referenced by tx_application_define().

◆ s_retune_thread

TX_THREAD s_retune_thread
static

Worker thread control block.

ThreadX zeroes it on tx_thread_create.

Definition at line 151 of file main.c.

Referenced by tx_application_define().