ra8-firmware 0.1.0
Bare-metal firmware for the Renesas RA8 family (RA8D2 / RA8P1)
Loading...
Searching...
No Matches
tx_systick_retune.c
Go to the documentation of this file.
1
22
23#include <stdint.h>
24
25#include "ra8_cgc.h"
26#include "ra8_check.h"
27#include "ra8_err.h"
28#include "ra8_log.h"
29#include "ra8_systick.h"
30#include "ra8_threadx.h"
31
32static const char* s_tag = "TX_SYST";
33
34ra8_err_t ra8_threadx_systick_reload_for(uint32_t cpuclk_hz, uint32_t tick_hz, uint32_t* out_reload)
35{
36 RA8_CHECK_NULL_PTR(out_reload, s_tag, "out_reload is NULL");
37
38 /* Reject a zero clock or a zero tick rate: both would make the reload
39 * arithmetic meaningless (division by zero / a 0 Hz core). */
40 if ((cpuclk_hz == 0U) || (tick_hz == 0U)) {
41 ra8_log_error(s_tag, "cpuclk_hz / tick_hz must be non-zero");
43 }
44
45 const uint32_t ticks = cpuclk_hz / tick_hz;
46 if (ticks == 0U) {
47 /* Clock slower than one tick period -> reload would underflow. */
48 ra8_log_error(s_tag, "cpuclk_hz below one tick period");
50 }
51
52 const uint32_t reload = ticks - 1U;
53 if (reload > (uint32_t)k_ra8_systick_reload_max) {
54 /* Clock too high for the 24-bit SYST_RVR field: refuse rather than
55 * truncate (which would silently make the tick far too fast). */
56 ra8_log_error(s_tag, "reload exceeds 24-bit SysTick range");
58 }
59
60 *out_reload = reload;
61 return k_ra8_ok;
62}
63
65{
66 uint32_t cpuclk_hz = 0U;
68 s_tag,
69 "CPUCLK0 query failed");
70
71 uint32_t reload = 0U;
73 ra8_threadx_systick_reload_for(cpuclk_hz, (uint32_t)k_ra8_threadx_tick_hz, &reload),
74 s_tag,
75 "SysTick reload computation failed");
76
77 /* Re-arm SysTick for the live clock through the shared timebase primitive.
78 * Only the reload + current-value words change; the enable / clock-source /
79 * tick-interrupt bits set by _tx_initialize_low_level.S are left as-is
80 * (ra8_systick_set_reload writes SYST_RVR and clears SYST_CVR, never
81 * SYST_CSR). The reload already fit the 24-bit field in reload_for above, so
82 * this write cannot be rejected -- but the return is still checked. */
83 RA8_RETURN_ON_ERROR(ra8_systick_set_reload(reload), s_tag, "SysTick reload program failed");
84
85 ra8_log_info_val(s_tag, "systick retuned reload", reload);
86 return k_ra8_ok;
87}
static const char * s_tag
Logging / check tag.
Definition ra8_app.c:17
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
Validation and Error-Checking Macros for ra8-firmware.
#define RA8_RETURN_ON_ERROR(err, tag, message)
Early return on error, propagating the code upward.
Definition ra8_check.h:184
#define RA8_CHECK_NULL_PTR(ptr, tag, message)
Reject nullptr pointer, returning k_ra8_err_null_ptr.
Definition ra8_check.h:243
Error Code Definitions for ra8-firmware.
@ k_ra8_err_out_of_range
Sensor or peripheral output out of valid range.
Definition ra8_err.h:337
@ k_ra8_err_invalid_arg
Invalid function argument.
Definition ra8_err.h:152
@ 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
Lightweight Logging Interface for ra8-firmware.
#define ra8_log_info_val(tag, message, value)
RA8 log info val.
Definition ra8_log.h:366
#define ra8_log_error(tag, message)
RA8 log error.
Definition ra8_log.h:335
Cortex-M85 SysTick + DWT cycle-counter timebase primitive.
ra8_err_t ra8_systick_set_reload(uint32_t reload)
Re-arm SysTick with a new reload, leaving the control bits untouched.
RA8 <-> Eclipse ThreadX glue: SysTick kernel-tick retuning.
@ k_ra8_threadx_tick_hz
1 kHz -> 1 ms kernel tick.
Definition ra8_threadx.h:61
@ k_ra8_systick_reload_max
Max SYST_RVR reload (24-bit).
Definition ra8_threadx.h:76
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.