ra8-firmware 0.1.0
Bare-metal firmware for the Renesas RA8 family (RA8D2 / RA8P1)
Loading...
Searching...
No Matches
ra8_systick.c
Go to the documentation of this file.
1
22
23#include "ra8_systick.h"
24
25#include <stdint.h>
26
27#include "ra8_attributes.h"
28#include "ra8_check.h"
29#include "ra8_err.h"
30#include "ra8_log.h"
31
33static const char* const s_tag = "ra8_systick";
34
43typedef enum : uintptr_t {
44 k_ra8_systick_csr = 0xE000E010UL,
45 k_ra8_systick_rvr = 0xE000E014UL,
46 k_ra8_systick_cvr = 0xE000E018UL,
47 k_ra8_dwt_demcr = 0xE000EDFCUL,
48 k_ra8_dwt_ctrl = 0xE0001000UL,
49 k_ra8_dwt_cyccnt = 0xE0001004UL,
51
58typedef enum : uint32_t {
59 k_ra8_systick_csr_enable = 0x00000001UL,
62 k_ra8_dwt_demcr_trcena = 0x01000000UL,
63 k_ra8_dwt_ctrl_cyccntena = 0x00000001UL,
65
86RA8_HW_REGISTER_ACCESS RA8_INTERNAL static inline volatile uint32_t*
88{
89 return (volatile uint32_t*)addr;
90}
91
92ra8_err_t ra8_systick_reload_for(uint32_t cpu_hz, uint32_t tick_hz, uint32_t* out_reload)
93{
94 RA8_CHECK_NULL_PTR(out_reload, s_tag, "out_reload is NULL");
95
96 /* Reject a zero clock or a zero tick rate: both make the reload arithmetic
97 * meaningless (a 0 Hz core, or a division by zero). */
98 if ((cpu_hz == 0U) || (tick_hz == 0U)) {
99 ra8_log_error(s_tag, "cpu_hz / tick_hz must be non-zero");
101 }
102
103 const uint32_t ticks = cpu_hz / tick_hz;
104 if (ticks == 0U) {
105 /* Clock slower than one tick period -> reload would underflow. */
106 ra8_log_error(s_tag, "cpu_hz below one tick period");
108 }
109
110 const uint32_t reload = ticks - 1U;
111 if (reload > (uint32_t)k_ra8_systick_rvr_max) {
112 /* Too high for the 24-bit SYST_RVR field: refuse rather than truncate,
113 * which would silently make the tick far too fast. */
114 ra8_log_error(s_tag, "reload exceeds 24-bit SysTick range");
116 }
117
118 *out_reload = reload;
119 return k_ra8_ok;
120}
121
123{
124 if (reload > (uint32_t)k_ra8_systick_rvr_max) {
125 ra8_log_error(s_tag, "reload exceeds 24-bit SysTick range");
127 }
128
129 /* Arm v8-M ARM: SYST_CSR -- clear ENABLE first so the reload/current writes
130 * land on a stopped counter, then re-enable with the requested config. */
134
135 uint32_t csr_val = (uint32_t)k_ra8_systick_csr_enable;
136 if (tick_irq) {
137 csr_val |= (uint32_t)k_ra8_systick_csr_tickint;
138 }
139 if (src == k_ra8_systick_clk_cpu) {
140 csr_val |= (uint32_t)k_ra8_systick_csr_clksource;
141 }
143 return k_ra8_ok;
144}
145
147{
148 if (reload > (uint32_t)k_ra8_systick_rvr_max) {
149 ra8_log_error(s_tag, "reload exceeds 24-bit SysTick range");
151 }
152
153 /* Arm v8-M ARM: re-arm the reload, then any write to SYST_CVR clears it so
154 * the next count starts from the new reload. The SYST_CSR control bits are
155 * deliberately left as-is. */
158 return k_ra8_ok;
159}
160
162{
163 /* Arm v8-M ARM: SYST_CVR -- reading it is side-effect free (only a write
164 * clears the counter). */
166}
167
169{
170 /* Arm v8-M ARM: DEMCR.TRCENA unlocks the DWT unit; DWT_CTRL.CYCCNTENA then
171 * starts DWT_CYCCNT. Read-modify-write preserves any other trace bits. */
174}
175
177{
178 /* Arm v8-M ARM: DWT_CYCCNT is writable; zero it to start a measured window.
179 */
181}
182
184{
185 /* Arm v8-M ARM: DWT_CYCCNT free-running cycle counter, read side-effect free.
186 */
188}
static const char * s_tag
Logging / check tag.
Definition ra8_app.c:17
Annotation-attribute framework macros for ra8-firmware.
#define RA8_HW_REGISTER_ACCESS
Mark an MMIO accessor function (returns volatile register pointer).
#define RA8_INTERNAL
Marker that a function is intended to be static (file-local).
Validation and Error-Checking Macros for ra8-firmware.
#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_error(tag, message)
RA8 log error.
Definition ra8_log.h:335
ra8_err_t ra8_systick_reload_for(uint32_t cpu_hz, uint32_t tick_hz, uint32_t *out_reload)
Compute the SysTick reload for a given core clock and tick rate.
Definition ra8_systick.c:92
uint32_t ra8_systick_current_value(void)
Read the live SysTick current-value register (SYST_CVR).
ra8_systick_bit_t
Control bits for SYST_CSR, DEMCR, and DWT_CTRL.
Definition ra8_systick.c:58
@ k_ra8_systick_csr_enable
SYST_CSR.ENABLE (bit 0).
Definition ra8_systick.c:59
@ k_ra8_dwt_demcr_trcena
DEMCR.TRCENA (bit 24).
Definition ra8_systick.c:62
@ k_ra8_dwt_ctrl_cyccntena
DWT_CTRL.CYCCNTENA (bit 0).
Definition ra8_systick.c:63
@ k_ra8_systick_csr_clksource
SYST_CSR.CLKSOURCE (bit 2).
Definition ra8_systick.c:61
@ k_ra8_systick_csr_tickint
SYST_CSR.TICKINT (bit 1).
Definition ra8_systick.c:60
ra8_systick_reg_addr_t
Arm v8-M SCS timebase register addresses (PPB 0xE000Exxx).
Definition ra8_systick.c:43
@ k_ra8_systick_csr
SYST_CSR control and status.
Definition ra8_systick.c:44
@ k_ra8_dwt_cyccnt
DWT_CYCCNT free-running counter.
Definition ra8_systick.c:49
@ k_ra8_dwt_ctrl
DWT_CTRL: bit 0 CYCCNTENA.
Definition ra8_systick.c:48
@ k_ra8_systick_rvr
SYST_RVR reload value.
Definition ra8_systick.c:45
@ k_ra8_dwt_demcr
DEMCR: bit 24 TRCENA unlocks DWT.
Definition ra8_systick.c:47
@ k_ra8_systick_cvr
SYST_CVR current value.
Definition ra8_systick.c:46
ra8_err_t ra8_systick_set_reload(uint32_t reload)
Re-arm SysTick with a new reload, leaving the control bits untouched.
static volatile uint32_t * internal_systick_reg(ra8_systick_reg_addr_t addr)
Typed pointer to a 32-bit SCS timebase register.
Definition ra8_systick.c:87
void ra8_dwt_cyccnt_reset(void)
Zero the DWT cycle counter (DWT_CYCCNT).
ra8_err_t ra8_systick_configure(uint32_t reload, ra8_systick_clock_source_t src, bool tick_irq)
Arm and start SysTick with a reload value and clock source.
uint32_t ra8_dwt_cyccnt_read(void)
Sample the DWT cycle counter (DWT_CYCCNT).
void ra8_dwt_cyccnt_enable(void)
Enable the DWT free-running cycle counter (DWT_CYCCNT).
Cortex-M85 SysTick + DWT cycle-counter timebase primitive.
@ k_ra8_systick_rvr_max
Max SYST_RVR reload (24-bit).
Definition ra8_systick.h:62
ra8_systick_clock_source_t
Selects the SysTick counter clock (SYST_CSR.CLKSOURCE).
Definition ra8_systick.h:73
@ k_ra8_systick_clk_cpu
CLKSOURCE = 1: processor (CPU) clock.
Definition ra8_systick.h:75