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

SysTick-based tick counter, delay and timestamp helpers. More...

#include <stdint.h>
#include "ra8_err.h"
#include "ra8_time_interface.h"
Include dependency graph for ra8_time.h:

Go to the source code of this file.

Functions

ra8_err_t ra8_time_init (uint32_t cpu_hz)
 Initialise SysTick for a 1 kHz tick interrupt.
uint32_t ra8_time_ms (void)
 Get the current 1 kHz tick count.
void ra8_delay_ms (uint32_t ms)
 Busy-wait for at least ms milliseconds.
void ra8_time_on_tick (void)
 SysTick IRQ handler – called from the vector table.
uint32_t ra8_now_ms (void)
 Short-form alias for ra8_time_ms().
void ra8_sleep_ms (uint32_t ms)
 Short-form alias for ra8_delay_ms().

Variables

const ra8_time_interface_t g_ra8_time_interface_systick
 Production ra8_time_interface_t that forwards to the SysTick-backed ra8_time_ms / ra8_delay_ms helpers.

Detailed Description

SysTick-based tick counter, delay and timestamp helpers.

Uses the Cortex-M SysTick peripheral (architectural, always present) as a 1 kHz tick source. Exposes three entry points:

  • ra8_time_init(cpu_hz): programme SysTick to fire every 1 ms given the current CPU clock in Hz.
  • ra8_time_ms(): return the current tick count in milliseconds.
  • ra8_delay_ms(ms): busy-wait for ms milliseconds.

The tick counter is a 32-bit uint32_t and wraps every ~49.7 days. Code that compares timestamps should use subtraction so the comparison stays monotonic across wrap.

Definition in file ra8_time.h.

Function Documentation

◆ ra8_delay_ms()

void ra8_delay_ms ( uint32_t ms)

Busy-wait for at least ms milliseconds.

Loops on ra8_time_ms() and issues wfi between checks.

Parameters
[in]msMilliseconds to wait. Zero returns immediately.
Precondition
ra8_time_init() has been called.
IRQs are NOT globally masked.
Postcondition
At least ms milliseconds have elapsed.
No internal state modified.
Note
Wall-clock accuracy depends on the tick interrupt not being masked for the duration of the call. Thread-safe.
Since
0.1.0

Busy-wait for at least ms milliseconds.

Loops on s_tick_ms and issues wfi between checks.

Parameters
[in]msMilliseconds to wait. Zero returns immediately.
Precondition
ra8_time_init() has been called.
IRQs are NOT globally masked.
Postcondition
At least ms milliseconds have elapsed.
No internal state modified.
Note
Thread-safe.
Since
0.1.0

Busy-wait for at least ms milliseconds.

ra8_usb calls ra8_delay_ms(1) once during device bring-up. The NS image must NOT link ra8_time.c: its ra8_time_init reprograms the SysTick that ThreadX owns, and its delay else-branch waits on a tick counter the ThreadX SysTick handler never advances. ThreadX's 1 ms tick makes one sleep tick == 1 ms here. Called only from thread context.

Parameters
[in]msMilliseconds to block (0 is rounded up to one tick).
Returns
void.
Precondition
Called from ThreadX thread context (the USB worker), not an ISR.
The ThreadX scheduler is running (1 ms tick live).
Postcondition
The caller blocked for at least ms ticks.
No SysTick reconfiguration occurs.
Note
Not callable from interrupt context.
Since
0.1.0

Definition at line 129 of file ra8_time.c.

References ra8_dwt_cyccnt_read(), s_cycles_per_ms, and s_tick_ms.

Referenced by app_bringup_panel(), c6_cam_camera_init(), c6_fwver_worker_entry(), c6_hosted_worker_entry(), c6_join_worker_entry(), c6_probe_sweep_mode(), c6_wifi_worker_entry(), cam_bringup(), cdc_enum_hunt(), cdc_enum_set_address(), cm_bringup_panel(), demo_emit_measurement(), dfu_enum_hunt(), dfu_enum_set_address(), dfu_wait_state(), ez_bringup_panel(), gptp_measure(), hid_enum_hunt(), hid_enum_hunt(), hid_enum_set_address(), hid_enum_set_address(), internal_bring_up_sensor(), internal_c6_cam_worker_entry(), internal_ceu_wait_for_frame(), internal_cs(), internal_cs_try(), internal_dac_demo_one_triangle_period(), internal_enum_assign_addr(), internal_enum_hunt(), internal_enum_hunt(), internal_kick_net(), internal_lcd_bringup_panel(), internal_lcd_bringup_panel(), internal_lpm_demo_one_wake(), internal_pdm_prepare_hardware(), internal_poeg_demo_cycle(), internal_pull_read(), internal_ra8_epaper_pulse_reset(), internal_reset_phase_1s(), internal_reset_phase_8d(), internal_run(), internal_run_one(), internal_set_address(), internal_start_count_source(), internal_transfer(), internal_usbhs_try_clksel(), internal_wait_for_autoneg(), internal_wait_ready(), internal_wait_state(), main(), mg_bringup_panel(), ns_host_enum_hunt(), ns_host_set_address(), ra8_board_camera_delay_ms(), ra8_board_camera_reset(), ra8_board_lcd_panel_power_on(), ra8_board_xspi_pins_init(), ra8_rtc_clock_init(), ra8_sleep_ms(), sfr_bringup_panel(), sh_run(), timer_demo_one_capture(), tsn_check_time_base(), wdt_rr_arm_and_wait_for_reset(), and wifi_hal_worker_entry().

◆ ra8_now_ms()

uint32_t ra8_now_ms ( void )

Short-form alias for ra8_time_ms().

Convenience wrapper used by code that prefers a "now" verb.

Returns
Current millisecond tick count modulo 2^32.
Return values
0..UINT32_MAXSee ra8_time_ms().
Precondition
Same as ra8_time_ms().
None additional.
Postcondition
No state modified.
Result equals ra8_time_ms() at the moment of the call.
Note
Thread-safe.
Since
0.1.0

Definition at line 19 of file timer.c.

References ra8_time_ms().

Referenced by ep_run_refresh_cycle(), and modem_now_ms().

◆ ra8_sleep_ms()

void ra8_sleep_ms ( uint32_t ms)

Short-form alias for ra8_delay_ms().

Convenience wrapper that forwards to ra8_delay_ms().

Parameters
[in]msMilliseconds to sleep. Zero returns immediately.
Precondition
Same as ra8_delay_ms().
None additional.
Postcondition
At least ms milliseconds have elapsed.
No internal state modified.
Note
Thread-safe.
Since
0.1.0

Definition at line 24 of file timer.c.

References ra8_delay_ms().

◆ ra8_time_init()

ra8_err_t ra8_time_init ( uint32_t cpu_hz)
nodiscard

Initialise SysTick for a 1 kHz tick interrupt.

Parameters
[in]cpu_hzCurrent CPU clock in Hz. Used to compute the SysTick reload value (cpu_hz / 1000 - 1).
Returns
k_ra8_ok on success, k_ra8_err_invalid_arg if cpu_hz is zero or would yield a zero reload, or the error propagated from ra8_systick_configure() when the reload does not fit the 24-bit SysTick field (target build only; unreachable at any RA8D2 clock rate).
Note
Must be called after ra8_cgc_init() so the CPU clock is stable.
Since
0.1.0

Initialise SysTick for a 1 kHz tick interrupt.

Computes the reload as cpu_hz / 1000 - 1, programmes SYST_RVR/CVR/CSR. On the off-target host the SCS writes are skipped.

Parameters
[in]cpu_hzCurrent CPU clock in Hz.
Returns
Error code.
Return values
k_ra8_okSysTick programmed; tick interrupt active.
k_ra8_err_invalid_argcpu_hz is zero or yields a zero reload.
Precondition
ra8_cgc_init() has run – CPU clock is stable.
Function is called from a single-threaded context.
Postcondition
On success, SysTick fires every 1 ms.
s_tick_ms is reset to zero.
Note
Not thread-safe; intended for one-shot init only.
Since
0.1.0

Definition at line 59 of file ra8_time.c.

References k_ra8_err_invalid_arg, k_ra8_ms_per_sec, k_ra8_ok, k_ra8_systick_clk_cpu, ra8_dwt_cyccnt_enable(), ra8_log_error, ra8_log_info_val, ra8_systick_configure(), s_cycles_per_ms, s_tag, and s_tick_ms.

Referenced by adc_diag_setup_or_halt(), al_setup_or_halt(), app_bringup_clocks(), blc_setup_or_halt(), bm_setup_or_halt(), bs_setup_or_halt(), c6_fwver_setup_or_halt(), c6_hosted_setup_or_halt(), c6_join_setup_or_halt(), c6_probe_clocks_or_halt(), c6_wifi_setup_or_halt(), cam_bringup(), cdc_setup_or_halt(), ch_setup_or_halt(), cm_bringup_clocks(), combo_clocks_or_halt(), crc_demo_setup_or_halt(), demo_setup_or_halt(), demo_setup_or_halt(), dfu_setup_or_halt(), drw_blend_setup_or_halt(), drw_demo_setup_or_halt(), dtc_arm_setup_or_halt(), dtc_demo_setup_or_halt(), ec_setup_or_halt(), ef_setup_or_halt(), ehb_setup_or_halt(), eoh_setup_or_halt(), eop_setup_or_halt(), ep_bringup_core(), ep_setup_or_halt(), erb_setup_or_halt(), es_setup_or_halt(), est_setup_or_halt(), etoc_setup_or_halt(), ez_bringup_clocks(), fileops_setup_or_halt(), flash_journal_setup_or_halt(), fs_fmt_setup_or_halt(), gh_setup_or_halt(), gpt_ecc_clocks_or_halt(), gptp_setup_or_halt(), hid_setup_or_halt(), i3c_demo_clocks_or_halt(), iic_setup_or_halt(), imp_setup_or_halt(), imu_demo_clocks_or_halt(), internal_acmphs_demo_setup(), internal_agt_periodic_setup_or_halt(), internal_audio_loopback_init_clocks_and_led(), internal_bkup_demo_setup_or_halt(), internal_c6_cam_setup_or_halt(), internal_can_demo_setup_or_halt(), internal_canfd_demo_setup_or_halt(), internal_canfd_filter_setup_or_halt(), internal_clocks_or_halt(), internal_clocks_or_halt(), internal_dac_b_demo_setup_or_halt(), internal_dac_demo_setup_or_halt(), internal_demo_clocks_or_halt(), internal_demo_setup_or_halt(), internal_dotf_demo_setup_or_halt(), internal_eth_loopback_setup_or_halt(), internal_gpio_demo_setup_or_halt(), internal_gpt_3p_demo_setup_or_halt(), internal_gpt_irq_demo_setup_or_halt(), internal_gpt_pwm_demo_setup_or_halt(), internal_icu_extint_demo_setup_or_halt(), internal_iic_peripheral_setup_or_halt(), internal_kat_setup_or_halt(), internal_kint_demo_setup_or_halt(), internal_lcd_bringup_clocks(), internal_lpm_deep_setup_or_halt(), internal_lpm_demo_setup_or_halt(), internal_lpm_dpsby1_setup_or_halt(), internal_lpm_dpsby2_setup_or_halt(), internal_lpm_dpsby3_setup_or_halt(), internal_lpm_swstd_setup_or_halt(), internal_lpm_wake_setup_or_halt(), internal_lus_setup_or_halt(), internal_lvd_demo_setup_or_halt(), internal_mecc_setup_or_halt(), internal_motor_3phase_init_clocks_and_led(), internal_mpu_simple_setup_or_halt(), internal_pc_setup_or_halt(), internal_pdg_demo_setup_or_halt(), internal_poeg_demo_setup_or_halt(), internal_rc_setup_or_halt(), internal_rcs_setup_or_halt(), internal_rng_demo_setup_or_halt(), internal_rtc_demo_setup_or_halt(), internal_sd_demo_setup_or_halt(), internal_setup_or_halt(), internal_ssie_loop_setup_or_halt(), internal_tc_setup_or_halt(), internal_td_setup_or_halt(), internal_uart_hello_setup_or_halt(), internal_usb_audio_clocks_or_halt(), internal_wdt_demo_setup_or_halt(), ipc_demo_setup_or_halt(), iu_setup_or_halt(), iv_setup_or_halt(), kat_setup_or_halt(), kb_setup_or_halt(), l3_setup_or_halt(), lcd_bringup_clocks(), lin_hil_setup_or_halt(), lk_setup_or_halt(), lpi_setup_or_halt(), main(), mem_setup_or_halt(), mg_bringup_clocks(), microsd_setup_or_halt(), mlun_setup_or_halt(), modem_clocks_or_halt(), ospirw_setup_or_halt(), pdm_demo_clocks_or_halt(), pp_demo_clocks_or_halt(), riic_target_clocks_or_halt(), rot_setup_or_halt(), sb_setup_or_halt(), scb_demo_setup_or_halt(), sd_demo_setup_or_halt(), sd_setup_or_halt(), sdhi_card_setup_or_halt(), sdhi_demo_setup_or_halt(), sdmsc_setup_or_halt(), sdram_demo_setup_or_halt(), selftest_setup_or_halt(), sfr_bringup_clocks(), sh_setup_or_halt(), tb_setup_or_halt(), timer_demo_setup_or_halt(), tsn_setup_or_halt(), uart_irq_setup_or_halt(), wa_setup_or_halt(), wc_setup_or_halt(), wd_setup_or_halt(), wdt_rr_setup_or_halt(), wdt_window_demo_setup_or_halt(), webp_demo_setup_or_halt(), wifi_hal_setup_or_halt(), wk_setup_or_halt(), and wlun_setup_or_halt().

◆ ra8_time_ms()

uint32_t ra8_time_ms ( void )

Get the current 1 kHz tick count.

Returns the SysTick-incremented s_tick_ms counter. Wraps every ~49.7 days; compare via subtraction.

Returns
Milliseconds since ra8_time_init(), wrapping at 2^32.
Return values
0..UINT32_MAXMonotonically increasing modulo 2^32.
Precondition
ra8_time_init() has been called and SysTick IRQ is firing.
Reader is OK with single-word atomicity.
Postcondition
No internal state modified.
Successive calls return non-decreasing values modulo 2^32.
Note
Thread-safe (single-word read on Cortex-M is atomic).
Since
0.1.0

Get the current 1 kHz tick count.

Returns the SysTick-incremented s_tick_ms counter.

Returns
Milliseconds since ra8_time_init(), modulo 2^32.
Return values
0..UINT32_MAXCurrent tick count.
Precondition
ra8_time_init() has been called.
Reader is OK with single-word atomicity.
Postcondition
No state modified.
Successive calls are non-decreasing modulo 2^32.
Note
Thread-safe (atomic single-word read on Cortex-M).
Since
0.1.0

Get the current 1 kHz tick count.

The polled host ladder (cdc_enum_hunt) uses ra8_time_ms for its attach timeout. ThreadX's tick is 1 ms here, so tx_time_get (ticks since boot) is already a millisecond count. Replaces ra8_time.c (dropped from the NS link – see ra8_delay_ms).

Returns
Milliseconds since the ThreadX scheduler started.
Return values
0Immediately after the kernel starts.
Precondition
The ThreadX scheduler is running.
Called from thread context.
Postcondition
No state changes (pure read of the kernel tick).
The return value is monotonic between wraps (~49 days).
Note
Thread-safe (single-word kernel read).
Since
0.1.0

Definition at line 108 of file ra8_time.c.

References s_tick_ms.

Referenced by c6_cam_camera_capture_jpeg(), cdc_enum_hunt(), dfu_enum_hunt(), ez_poll_touch(), gptp_measure(), hid_enum_hunt(), hid_enum_hunt(), internal_enum_hunt(), internal_enum_hunt(), internal_pdm_capture(), internal_pdm_stream_data(), main(), ns_host_enum_hunt(), pp_demo_now_us(), ra8_now_ms(), sdram_demo_read_check(), sdram_demo_write_pass(), selftest_verify_mram_raw(), selftest_verify_mram_raw(), selftest_verify_mram_raw(), selftest_verify_mram_raw(), selftest_verify_ospi_raw(), sh_cover_gesture(), sh_loupe_held(), sh_run(), and tsn_check_time_base().

◆ ra8_time_on_tick()

void ra8_time_on_tick ( void )

SysTick IRQ handler – called from the vector table.

Override the weak SysTick_Handler alias with ra8_time_on_tick by declaring void SysTick_Handler(void) { ra8_time_on_tick(); } somewhere in the application, OR link ra8_time.c with the already-provided SysTick_Handler implementation in this file.

Precondition
Invoked from SysTick IRQ context (or test equivalent).
ra8_time_init() has set up the SysTick reload value.
Postcondition
s_tick_ms is incremented by exactly one.
No other state modified.
Note
IRQ-safe; SysTick cannot pre-empt itself.
Since
0.1.0

SysTick IRQ handler – called from the vector table.

Increments s_tick_ms. Invoked from SysTick IRQ.

Precondition
Invoked from SysTick IRQ context (or test equivalent).
ra8_time_init() has set up the SysTick reload.
Postcondition
s_tick_ms is incremented by exactly one.
No other state is modified.
Note
IRQ-safe; SysTick cannot pre-empt itself.
Since
0.1.0

Definition at line 170 of file ra8_time.c.

References s_tick_ms.

Referenced by SysTick_Handler().

Variable Documentation

◆ g_ra8_time_interface_systick

const ra8_time_interface_t g_ra8_time_interface_systick
extern

Production ra8_time_interface_t that forwards to the SysTick-backed ra8_time_ms / ra8_delay_ms helpers.