1 /*
2  * Copyright (c) 2023 - 2024, Nordic Semiconductor ASA
3  * All rights reserved.
4  *
5  * SPDX-License-Identifier: BSD-3-Clause
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions are met:
9  *
10  * 1. Redistributions of source code must retain the above copyright notice, this
11  *    list of conditions and the following disclaimer.
12  *
13  * 2. Redistributions in binary form must reproduce the above copyright
14  *    notice, this list of conditions and the following disclaimer in the
15  *    documentation and/or other materials provided with the distribution.
16  *
17  * 3. Neither the name of the copyright holder nor the names of its
18  *    contributors may be used to endorse or promote products derived from this
19  *    software without specific prior written permission.
20  *
21  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
22  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
25  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
26  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
27  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
28  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
29  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
30  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
31  * POSSIBILITY OF SUCH DAMAGE.
32  */
33 
34 #ifndef NRF_VPR_CSR_H__
35 #define NRF_VPR_CSR_H__
36 
37 #include <nrfx.h>
38 
39 #ifdef __cplusplus
40 extern "C" {
41 #endif
42 
43 /**
44  * @defgroup nrf_vpr_csr_hal VPR CSR HAL
45  * @{
46  * @ingroup nrf_vpr
47  * @brief   Hardware access layer for managing the VPR RISC-V CPU Control
48  *          and Status Registers (VPR CSR).
49  */
50 
51 /** @brief Nordic key for CSR writes. */
52 #define NRF_VPR_CSR_NORDIC_KEY_MASK \
53     (VPRCSR_NORDIC_VPRNORDICCTRL_NORDICKEY_Enabled << VPRCSR_NORDIC_VPRNORDICCTRL_NORDICKEY_Pos)
54 
55 /** @brief Interrupt threshold levels. */
56 typedef enum
57 {
58     NRF_VPR_CSR_INT_THRESHOLD_DISABLED = VPRCSR_MINTTHRESH_TH_DISABLED,     ///< Threshold disabled.
59     NRF_VPR_CSR_INT_THRESHOLD_LEVEL0   = VPRCSR_MINTTHRESH_TH_THRESHLEVEL0, ///< Threshold level 0.
60     NRF_VPR_CSR_INT_THRESHOLD_LEVEL1   = VPRCSR_MINTTHRESH_TH_THRESHLEVEL1, ///< Threshold level 1.
61     NRF_VPR_CSR_INT_THRESHOLD_LEVEL2   = VPRCSR_MINTTHRESH_TH_THRESHLEVEL2, ///< Threshold level 2.
62     NRF_VPR_CSR_INT_THRESHOLD_LEVEL3   = VPRCSR_MINTTHRESH_TH_THRESHLEVEL3, ///< Threshold level 3.
63 } nrf_vpr_csr_int_threshold_t;
64 
65 /** @brief Trap causes. */
66 typedef enum
67 {
68     NRF_VPR_CSR_TRAP_CAUSE_INSTR_ADDR_MISALIGNED = VPRCSR_MCAUSE_EXCEPTIONCODE_INSTADDRMISALIGN,   ///< Instruction address misaligned.
69     NRF_VPR_CSR_TRAP_CAUSE_INSTR_ACCESS_FAULT    = VPRCSR_MCAUSE_EXCEPTIONCODE_INSTACCESSFAULT,    ///< Instruction access fault.
70     NRF_VPR_CSR_TRAP_CAUSE_INSTR_ILLEGAL         = VPRCSR_MCAUSE_EXCEPTIONCODE_ILLEGALINST,        ///< Illegal instruction.
71     NRF_VPR_CSR_TRAP_CAUSE_BREAKPOINT            = VPRCSR_MCAUSE_EXCEPTIONCODE_BKPT,               ///< Breakpoint.
72     NRF_VPR_CSR_TRAP_CAUSE_LOAD_ADDR_MISALIGNED  = VPRCSR_MCAUSE_EXCEPTIONCODE_LOADADDRMISALIGN,   ///< Load address misaligned.
73     NRF_VPR_CSR_TRAP_CAUSE_LOAD_ACCESS_FAULT     = VPRCSR_MCAUSE_EXCEPTIONCODE_LOADACCESSFAULT,    ///< Load access fault.
74     NRF_VPR_CSR_TRAP_CAUSE_STORE_ADDR_MISALIGNED = VPRCSR_MCAUSE_EXCEPTIONCODE_STOREADDRMISALIGN,  ///< Store/AMO address misaligned.
75     NRF_VPR_CSR_TRAP_CAUSE_STORE_ACCESS_FAULT    = VPRCSR_MCAUSE_EXCEPTIONCODE_STOREACCESSFAULT,   ///< Store/AMO access misaligned.
76     NRF_VPR_CSR_TRAP_CAUSE_ECALL_M               = VPRCSR_MCAUSE_EXCEPTIONCODE_ECALLMMODE,         ///< Environment call M-mode.
77     NRF_VPR_CSR_TRAP_CAUSE_STACKING_BUS_FAULT    = VPRCSR_MCAUSE_EXCEPTIONCODE_BUSFAULTSTACKING,   ///< Bus fault on stacking.
78     NRF_VPR_CSR_TRAP_CAUSE_STACKING_UNALIGNED    = VPRCSR_MCAUSE_EXCEPTIONCODE_MISALIGNSTACKING,   ///< Misaligned Stacking.
79     NRF_VPR_CSR_TRAP_CAUSE_VECTOR_FAULT          = VPRCSR_MCAUSE_EXCEPTIONCODE_INTVECTORFAULT,     ///< Interrupt Vector Fault.
80     NRF_VPR_CSR_TRAP_CAUSE_STACKING_UNALIGNED_EX = VPRCSR_MCAUSE_EXCEPTIONCODE_STACKINGEXCFAULT,   ///< Fault on Exception Stacking.
81     NRF_VPR_CSR_TRAP_CAUSE_UNSTACKING_UNALIGNED  = VPRCSR_MCAUSE_EXCEPTIONCODE_MISALIGNUNSTACKING, ///< Misaligned Unstacking.
82     NRF_VPR_CSR_TRAP_CAUSE_UNSTACKING_BUS_FAULT  = VPRCSR_MCAUSE_EXCEPTIONCODE_BUSFAULTUNSTACKING, ///< Bus fault on unstacking.
83     NRF_VPR_CSR_TRAP_CAUSE_STORE_TIMEOUT_FAULT   = VPRCSR_MCAUSE_EXCEPTIONCODE_STORETIMEOUTFAULT,  ///< Store timeout fault.
84     NRF_VPR_CSR_TRAP_CAUSE_LOAD_TIMEOUT_FAULT    = VPRCSR_MCAUSE_EXCEPTIONCODE_LOADTIMEOUTFAULT,   ///< Load timeout fault.
85 } nrf_vpr_csr_trap_cause_t;
86 
87 /** @brief Sleep states. */
88 typedef enum
89 {
90     NRF_VPR_CSR_SLEEP_STATE_WAIT       = VPRCSR_NORDIC_VPRNORDICSLEEPCTRL_SLEEPSTATE_WAIT,      ///< During sleep, clock is not turned off.
91     NRF_VPR_CSR_SLEEP_STATE_RESET      = VPRCSR_NORDIC_VPRNORDICSLEEPCTRL_SLEEPSTATE_RESET,     ///< Sleep mode out of reset.
92     NRF_VPR_CSR_SLEEP_STATE_SLEEP      = VPRCSR_NORDIC_VPRNORDICSLEEPCTRL_SLEEPSTATE_SLEEP,     ///< During sleep, clock is turned off.
93     NRF_VPR_CSR_SLEEP_STATE_DEEP_SLEEP = VPRCSR_NORDIC_VPRNORDICSLEEPCTRL_SLEEPSTATE_DEEPSLEEP, ///< During sleep, clock and power are turned off.
94     NRF_VPR_CSR_SLEEP_STATE_HIBERNATE  = VPRCSR_NORDIC_VPRNORDICSLEEPCTRL_SLEEPSTATE_HIBERNATE, ///< During sleep, clock is turned off. All the registers are saved automatically. Restart by a reset.
95 } nrf_vpr_csr_sleep_state_t;
96 
97 /** @brief Function for enabling the interrupts in machine mode. */
98 NRF_STATIC_INLINE void nrf_vpr_csr_machine_interrupts_enable(void);
99 
100 /** @brief Function for disabling the interrupts in machine mode. */
101 NRF_STATIC_INLINE void nrf_vpr_csr_machine_interrupts_disable(void);
102 
103 /**
104  * @brief Function for checking whether interrupts are enabled in machine mode.
105  *
106  * @retval true  Interrupts are enabled.
107  * @retval false Interrupts are disabled.
108  */
109 NRF_STATIC_INLINE bool nrf_vpr_csr_machine_interrupts_check(void);
110 
111 /**
112  * @brief Function for setting the base address of trap vector table.
113  *
114  * @param[in] address Machine trap vector table base address to be set.
115  *                    Has to be aligned on 64-byte or greater power-of-two boundary.
116  */
117 NRF_STATIC_INLINE void nrf_vpr_csr_machine_trap_vector_table_addr_set(uint32_t address);
118 
119 /**
120  * @brief Function for getting the machine trap vector table base address.
121  *
122  * @return Machine trap vector table base address.
123  */
124 NRF_STATIC_INLINE uint32_t nrf_vpr_csr_machine_trap_vector_table_addr_get(void);
125 
126 /**
127  * @brief Function for getting the machine exception program counter.
128  *
129  * @return Virtual address of the instruction that was interrupted or that encountered the exception.
130  */
131 NRF_STATIC_INLINE uint32_t nrf_vpr_csr_machine_exception_pc_get(void);
132 
133 /**
134  * @brief Function for getting the machine trap cause exception code.
135  *
136  * @return Exception code.
137  */
138 NRF_STATIC_INLINE nrf_vpr_csr_trap_cause_t nrf_vpr_csr_machine_trap_cause_code_get(void);
139 
140 /**
141  * @brief Function for checking the state of the interrupt bit for machine trap.
142  *
143  * @retval true  Trap was caused by an interrupt.
144  * @retval false Trap was not caused by an interrupt.
145  */
146 NRF_STATIC_INLINE bool nrf_vpr_csr_machine_trap_interrupt_check(void);
147 
148 /**
149  * @brief Function for getting the machine trap value.
150  *
151  * @return Exception-specific information.
152  */
153 NRF_STATIC_INLINE uint32_t nrf_vpr_csr_machine_trap_value_get(void);
154 
155 /**
156  * @brief Function for setting the machine mode interrupt level threshold.
157  *
158  * @param[in] th Machine mode interrupt level threshold to be set.
159  */
160 NRF_STATIC_INLINE void nrf_vpr_csr_machine_interrupt_threshold_set(nrf_vpr_csr_int_threshold_t th);
161 
162 /**
163  * @brief Function for getting the machine mode interrupt level threshold.
164  *
165  * @return Machine mode interrupt level threshold.
166  */
167 NRF_STATIC_INLINE nrf_vpr_csr_int_threshold_t nrf_vpr_csr_machine_interrupt_threshold_get(void);
168 
169 /**
170  * @brief Function for enabling or disabling the Cycle Counter.
171  *
172  * @param[in] enable True if Cycle Counter is to be enabled, false otherwise.
173  */
174 NRF_STATIC_INLINE void nrf_vpr_csr_machine_cycle_counter_enable_set(bool enable);
175 
176 /**
177  * @brief Function for checking whether the Cycle Counter is enabled.
178  *
179  * @retval true  Cycle counter is enabled.
180  * @retval false Cycle counter is disabled.
181  */
182 NRF_STATIC_INLINE bool nrf_vpr_csr_machine_cycle_counter_enable_check(void);
183 
184 /**
185  * @brief Function for getting the machine cycle counter.
186  *
187  * @return Number of clock cycles executed by the processor core.
188  */
189 NRF_STATIC_INLINE uint64_t nrf_vpr_csr_machine_cycle_counter_get(void);
190 
191 /**
192  * @brief Function for enabling or disabling the Instruction Counter.
193  *
194  * @param[in] enable True if Instruction Counter is to be enabled, false otherwise.
195  */
196 NRF_STATIC_INLINE void nrf_vpr_csr_machine_instruction_counter_enable_set(bool enable);
197 
198 /**
199  * @brief Function for checking whether the Instruction Counter is enabled.
200  *
201  * @retval true  Instruction counter is enabled.
202  * @retval false Instruction counter is disabled.
203  */
204 NRF_STATIC_INLINE bool nrf_vpr_csr_machine_instruction_counter_enable_check(void);
205 /**
206  * @brief Function for getting the machine instruction counter.
207  *
208  * @return Number of instructions exectuted by the processor.
209  */
210 NRF_STATIC_INLINE uint64_t nrf_vpr_csr_machine_instruction_counter_get(void);
211 
212 /**
213  * @brief Function for enabling or disabling the Real-Time Peripherals.
214  *
215  * @param[in] enable True if RT Perhiperals are to be enabled, false otherwise.
216  */
217 NRF_STATIC_INLINE void nrf_vpr_csr_rtperiph_enable_set(bool enable);
218 
219 /**
220  * @brief Function for checking whether the Real-Time Peripherals are enabled.
221  *
222  * @retval true  RT Peripherals are enabled.
223  * @retval false RT Peripherals are disabled.
224  */
225 NRF_STATIC_INLINE bool nrf_vpr_csr_rtperiph_enable_check(void);
226 
227 /**
228  * @brief Function for enabling or disabling the generation of IRQ at position CNT_IRQ_POSITION.
229  *
230  * @param[in] enable True if generation of IRQ at position CNT_IRQ_POSITION is to be enabled, false otherwise.
231  */
232 NRF_STATIC_INLINE void nrf_vpr_csr_cnt_irq_enable_set(bool enable);
233 
234 /**
235  * @brief Function for checking whether the generation of IRQ at position CNT_IRQ_POSITION is enabled.
236  *
237  * @retval true  Generation of IRQ is enabled.
238  * @retval false Generation of IRQ is disabled.
239  */
240 NRF_STATIC_INLINE bool nrf_vpr_csr_cnt_irq_enable_check(void);
241 
242 /**
243  * @brief Function for setting the sleep state.
244  *
245  * @param[in] state Sleep state to be set.
246  */
247 NRF_STATIC_INLINE void nrf_vpr_csr_sleep_state_set(nrf_vpr_csr_sleep_state_t state);
248 
249 /**
250  * @brief Function for getting the sleep state.
251  *
252  * @return Current sleep state.
253  */
254 NRF_STATIC_INLINE nrf_vpr_csr_sleep_state_t nrf_vpr_csr_sleep_state_get(void);
255 
256 /**
257  * @brief Function for enabling or disabling the return to sleep functionality.
258  *
259  * @param[in] enable True if CPU should be forced to return to sleep when it returns in a non-handler program,
260  *                   false otherwise.
261  */
262 NRF_STATIC_INLINE void nrf_vpr_csr_return_to_sleep_set(bool enable);
263 
264 /**
265  * @brief Function for checking whether the return to sleep functionality is enabled.
266  *
267  * @retval true  Return to sleep functionality is enabled.
268  * @retval false Return to sleep functionality is disabled.
269  */
270 NRF_STATIC_INLINE bool nrf_vpr_csr_return_to_sleep_check(void);
271 
272 /**
273  * @brief Function for enabling or disabling the stack on sleep functionality.
274  *
275  * @param[in] enable True if CPU should be forced to stack the context before going to sleep (used in order to have a fast wake-up),
276  *                   false otherwise.
277  */
278 NRF_STATIC_INLINE void nrf_vpr_csr_stack_on_sleep_set(bool enable);
279 
280 /**
281  * @brief Function for checking whether the stack on sleep functionality is enabled.
282  *
283  * @retval true  Stack on sleep functionality is enabled.
284  * @retval false Stack on sleep functionality is disabled.
285  */
286 NRF_STATIC_INLINE bool nrf_vpr_csr_stack_on_sleep_check(void);
287 
288 /**
289  * @brief Function for enabling or disabling the CLIC round robin arbitration.
290  *
291  * @param[in] enable True if round robin arbitration should be used for CLIC interrupt requests,
292  *                   false otherwise.
293  */
294 NRF_STATIC_INLINE void nrf_vpr_csr_clic_round_robin_set(bool enable);
295 
296 /**
297  * @brief Function for checking whether the CLIC round robin arbitration is enabled.
298  *
299  * @retval true  CLIC round robin arbitration is enabled.
300  * @retval false CLIC round robin arbitration is disabled.
301  */
302 NRF_STATIC_INLINE bool nrf_vpr_csr_clic_round_robin_check(void);
303 
304 /**
305  * @brief Function for enabling or disabling the unrecoverable return functionality.
306  *
307  * @param[in] enable True if unrecoverable return from exception is to be forced, false otherwise.
308  */
309 NRF_STATIC_INLINE void nrf_vpr_csr_unrecoverable_return_set(bool enable);
310 
311 /**
312  * @brief Function for checking whether the unrecoverable return is enabled.
313  *
314  * @retval true  Unrecoverable return is enabled.
315  * @retval false Unrecoverable return is disabled.
316  */
317 NRF_STATIC_INLINE bool nrf_vpr_csr_unrecoverable_return_check(void);
318 
319 #ifndef NRF_DECLARE_ONLY
nrf_vpr_csr_machine_interrupts_enable(void)320 NRF_STATIC_INLINE void nrf_vpr_csr_machine_interrupts_enable(void)
321 {
322     nrf_csr_set_bits(VPRCSR_MSTATUS, VPRCSR_MSTATUS_MIE_Msk);
323 }
324 
nrf_vpr_csr_machine_interrupts_disable(void)325 NRF_STATIC_INLINE void nrf_vpr_csr_machine_interrupts_disable(void)
326 {
327     nrf_csr_clear_bits(VPRCSR_MSTATUS, VPRCSR_MSTATUS_MIE_Msk);
328 }
329 
nrf_vpr_csr_machine_interrupts_check(void)330 NRF_STATIC_INLINE bool nrf_vpr_csr_machine_interrupts_check(void)
331 {
332     return nrf_csr_read(VPRCSR_MSTATUS) & VPRCSR_MSTATUS_MIE_Msk;
333 }
334 
nrf_vpr_csr_machine_trap_vector_table_addr_set(uint32_t address)335 NRF_STATIC_INLINE void nrf_vpr_csr_machine_trap_vector_table_addr_set(uint32_t address)
336 {
337     NRFX_ASSERT(!(address & 0xF));
338 
339     nrf_csr_write(VPRCSR_MTVT, address);
340 }
341 
nrf_vpr_csr_machine_trap_vector_table_addr_get(void)342 NRF_STATIC_INLINE uint32_t nrf_vpr_csr_machine_trap_vector_table_addr_get(void)
343 {
344     return nrf_csr_read(VPRCSR_MTVT);
345 }
346 
nrf_vpr_csr_machine_exception_pc_get(void)347 NRF_STATIC_INLINE uint32_t nrf_vpr_csr_machine_exception_pc_get(void)
348 {
349     return nrf_csr_read(VPRCSR_MEPC);
350 }
351 
nrf_vpr_csr_machine_trap_cause_code_get(void)352 NRF_STATIC_INLINE nrf_vpr_csr_trap_cause_t nrf_vpr_csr_machine_trap_cause_code_get(void)
353 {
354     return (nrf_vpr_csr_trap_cause_t)((nrf_csr_read(VPRCSR_MCAUSE)
355             & VPRCSR_MCAUSE_EXCEPTIONCODE_Msk)
356            >> VPRCSR_MCAUSE_EXCEPTIONCODE_Pos);
357 }
358 
nrf_vpr_csr_machine_trap_interrupt_check(void)359 NRF_STATIC_INLINE bool nrf_vpr_csr_machine_trap_interrupt_check(void)
360 {
361     return (nrf_csr_read(VPRCSR_MCAUSE) & VPRCSR_MCAUSE_INTERRUPT_Msk) >> VPRCSR_MCAUSE_INTERRUPT_Pos;
362 }
363 
nrf_vpr_csr_machine_trap_value_get(void)364 NRF_STATIC_INLINE uint32_t nrf_vpr_csr_machine_trap_value_get(void)
365 {
366     return nrf_csr_read(VPRCSR_MTVAL);
367 }
368 
nrf_vpr_csr_machine_interrupt_threshold_set(nrf_vpr_csr_int_threshold_t th)369 NRF_STATIC_INLINE void nrf_vpr_csr_machine_interrupt_threshold_set(nrf_vpr_csr_int_threshold_t th)
370 {
371     nrf_csr_write(VPRCSR_MINTTHRESH, (th << VPRCSR_MINTTHRESH_TH_Pos));
372 }
373 
nrf_vpr_csr_machine_interrupt_threshold_get(void)374 NRF_STATIC_INLINE nrf_vpr_csr_int_threshold_t nrf_vpr_csr_machine_interrupt_threshold_get(void)
375 {
376     return (nrf_csr_read(VPRCSR_MINTTHRESH) & VPRCSR_MINTTHRESH_TH_Msk) >> VPRCSR_MINTTHRESH_TH_Pos;
377 }
378 
nrf_vpr_csr_machine_cycle_counter_enable_set(bool enable)379 NRF_STATIC_INLINE void nrf_vpr_csr_machine_cycle_counter_enable_set(bool enable)
380 {
381     uint32_t reg = nrf_csr_read(VPRCSR_MCOUNTINHIBIT);
382 
383     reg = (reg & ~VPRCSR_MCOUNTINHIBIT_CY_Msk) | (enable ?
384             (VPRCSR_MCOUNTINHIBIT_CY_INCREMENT << VPRCSR_MCOUNTINHIBIT_CY_Pos) :
385             (VPRCSR_MCOUNTINHIBIT_CY_INHIBIT   << VPRCSR_MCOUNTINHIBIT_CY_Pos));
386 
387     nrf_csr_write(VPRCSR_MCOUNTINHIBIT, reg);
388 }
389 
nrf_vpr_csr_machine_cycle_counter_enable_check(void)390 NRF_STATIC_INLINE bool nrf_vpr_csr_machine_cycle_counter_enable_check(void)
391 {
392     uint32_t reg = nrf_csr_read(VPRCSR_MCOUNTINHIBIT);
393 
394     return (reg & (VPRCSR_MCOUNTINHIBIT_CY_INHIBIT << VPRCSR_MCOUNTINHIBIT_CY_Pos)) ? false : true;
395 }
396 
nrf_vpr_csr_machine_cycle_counter_get(void)397 NRF_STATIC_INLINE uint64_t nrf_vpr_csr_machine_cycle_counter_get(void)
398 {
399     return nrf_csr_read(VPRCSR_MCYCLE) | ((uint64_t)nrf_csr_read(VPRCSR_MCYCLEH) << 32);
400 }
401 
nrf_vpr_csr_machine_instruction_counter_enable_set(bool enable)402 NRF_STATIC_INLINE void nrf_vpr_csr_machine_instruction_counter_enable_set(bool enable)
403 {
404     uint32_t reg = nrf_csr_read(VPRCSR_MCOUNTINHIBIT);
405 
406     reg = (reg & ~VPRCSR_MCOUNTINHIBIT_IR_Msk) | (enable ?
407             (VPRCSR_MCOUNTINHIBIT_IR_INCREMENT << VPRCSR_MCOUNTINHIBIT_IR_Pos) :
408             (VPRCSR_MCOUNTINHIBIT_IR_INHIBIT   << VPRCSR_MCOUNTINHIBIT_IR_Pos));
409 
410     nrf_csr_write(VPRCSR_MCOUNTINHIBIT, reg);
411 }
412 
nrf_vpr_csr_machine_instruction_counter_enable_check(void)413 NRF_STATIC_INLINE bool nrf_vpr_csr_machine_instruction_counter_enable_check(void)
414 {
415     uint32_t reg = nrf_csr_read(VPRCSR_MCOUNTINHIBIT);
416 
417     return (reg & (VPRCSR_MCOUNTINHIBIT_IR_INHIBIT << VPRCSR_MCOUNTINHIBIT_IR_Pos)) ? false : true;
418 }
419 
nrf_vpr_csr_machine_instruction_counter_get(void)420 NRF_STATIC_INLINE uint64_t nrf_vpr_csr_machine_instruction_counter_get(void)
421 {
422     return nrf_csr_read(VPRCSR_MINSTRET) | ((uint64_t)nrf_csr_read(VPRCSR_MINSTRETH) << 32);
423 }
424 
nrf_vpr_csr_rtperiph_enable_set(bool enable)425 NRF_STATIC_INLINE void nrf_vpr_csr_rtperiph_enable_set(bool enable)
426 {
427     uint32_t reg = nrf_csr_read(VPRCSR_NORDIC_VPRNORDICCTRL);
428     reg = (reg & ~VPRCSR_NORDIC_VPRNORDICCTRL_ENABLERTPERIPH_Msk) | NRF_VPR_CSR_NORDIC_KEY_MASK;
429 
430     reg |= ((enable ? VPRCSR_NORDIC_VPRNORDICCTRL_ENABLERTPERIPH_Enabled :
431                       VPRCSR_NORDIC_VPRNORDICCTRL_ENABLERTPERIPH_Disabled)
432             << VPRCSR_NORDIC_VPRNORDICCTRL_ENABLERTPERIPH_Pos) | NRF_VPR_CSR_NORDIC_KEY_MASK;
433 
434     nrf_csr_write(VPRCSR_NORDIC_VPRNORDICCTRL, reg);
435 }
436 
nrf_vpr_csr_rtperiph_enable_check(void)437 NRF_STATIC_INLINE bool nrf_vpr_csr_rtperiph_enable_check(void)
438 {
439     return (nrf_csr_read(VPRCSR_NORDIC_VPRNORDICCTRL) & VPRCSR_NORDIC_VPRNORDICCTRL_ENABLERTPERIPH_Msk)
440            >> VPRCSR_NORDIC_VPRNORDICCTRL_ENABLERTPERIPH_Pos;
441 }
442 
nrf_vpr_csr_cnt_irq_enable_set(bool enable)443 NRF_STATIC_INLINE void nrf_vpr_csr_cnt_irq_enable_set(bool enable)
444 {
445     uint32_t reg = nrf_csr_read(VPRCSR_NORDIC_VPRNORDICCTRL);
446     reg &= ~(VPRCSR_NORDIC_VPRNORDICCTRL_CNTIRQENABLE_Msk | NRF_VPR_CSR_NORDIC_KEY_MASK);
447 
448     reg |= ((enable ? VPRCSR_NORDIC_VPRNORDICCTRL_CNTIRQENABLE_Enabled :
449                       VPRCSR_NORDIC_VPRNORDICCTRL_CNTIRQENABLE_Disabled)
450             << VPRCSR_NORDIC_VPRNORDICCTRL_CNTIRQENABLE_Pos) | NRF_VPR_CSR_NORDIC_KEY_MASK;
451 
452     nrf_csr_write(VPRCSR_NORDIC_VPRNORDICCTRL, reg);
453 }
454 
nrf_vpr_csr_cnt_irq_enable_check(void)455 NRF_STATIC_INLINE bool nrf_vpr_csr_cnt_irq_enable_check(void)
456 {
457     return (nrf_csr_read(VPRCSR_NORDIC_VPRNORDICCTRL) & VPRCSR_NORDIC_VPRNORDICCTRL_CNTIRQENABLE_Msk)
458            >> VPRCSR_NORDIC_VPRNORDICCTRL_CNTIRQENABLE_Pos;
459 }
460 
nrf_vpr_csr_sleep_state_set(nrf_vpr_csr_sleep_state_t state)461 NRF_STATIC_INLINE void nrf_vpr_csr_sleep_state_set(nrf_vpr_csr_sleep_state_t state)
462 {
463     uint32_t reg = nrf_csr_read(VPRCSR_NORDIC_VPRNORDICSLEEPCTRL);
464     reg &= ~VPRCSR_NORDIC_VPRNORDICSLEEPCTRL_SLEEPSTATE_Msk;
465 
466     reg |= (uint32_t)state << VPRCSR_NORDIC_VPRNORDICSLEEPCTRL_SLEEPSTATE_Pos;
467     nrf_csr_write(VPRCSR_NORDIC_VPRNORDICSLEEPCTRL, reg);
468 }
469 
nrf_vpr_csr_sleep_state_get(void)470 NRF_STATIC_INLINE nrf_vpr_csr_sleep_state_t nrf_vpr_csr_sleep_state_get(void)
471 {
472     return (nrf_vpr_csr_sleep_state_t)((nrf_csr_read(VPRCSR_NORDIC_VPRNORDICSLEEPCTRL)
473             & VPRCSR_NORDIC_VPRNORDICSLEEPCTRL_SLEEPSTATE_Msk)
474            >> VPRCSR_NORDIC_VPRNORDICSLEEPCTRL_SLEEPSTATE_Pos);
475 }
476 
nrf_vpr_csr_return_to_sleep_set(bool enable)477 NRF_STATIC_INLINE void nrf_vpr_csr_return_to_sleep_set(bool enable)
478 {
479     uint32_t reg = nrf_csr_read(VPRCSR_NORDIC_VPRNORDICSLEEPCTRL);
480     reg &= ~VPRCSR_NORDIC_VPRNORDICSLEEPCTRL_RETURNTOSLEEP_Msk;
481 
482     reg |= (enable ? VPRCSR_NORDIC_VPRNORDICSLEEPCTRL_RETURNTOSLEEP_Enabled :
483                      VPRCSR_NORDIC_VPRNORDICSLEEPCTRL_RETURNTOSLEEP_Disabled)
484            << VPRCSR_NORDIC_VPRNORDICSLEEPCTRL_RETURNTOSLEEP_Pos;
485     nrf_csr_write(VPRCSR_NORDIC_VPRNORDICSLEEPCTRL, reg);
486 }
487 
nrf_vpr_csr_return_to_sleep_check(void)488 NRF_STATIC_INLINE bool nrf_vpr_csr_return_to_sleep_check(void)
489 {
490     return (nrf_csr_read(VPRCSR_NORDIC_VPRNORDICSLEEPCTRL)
491             & VPRCSR_NORDIC_VPRNORDICSLEEPCTRL_RETURNTOSLEEP_Msk)
492            >> VPRCSR_NORDIC_VPRNORDICSLEEPCTRL_RETURNTOSLEEP_Pos;
493 }
494 
nrf_vpr_csr_stack_on_sleep_set(bool enable)495 NRF_STATIC_INLINE void nrf_vpr_csr_stack_on_sleep_set(bool enable)
496 {
497     uint32_t reg = nrf_csr_read(VPRCSR_NORDIC_VPRNORDICSLEEPCTRL);
498     reg &= ~VPRCSR_NORDIC_VPRNORDICSLEEPCTRL_STACKONSLEEP_Msk;
499 
500     reg |= (enable ? VPRCSR_NORDIC_VPRNORDICSLEEPCTRL_STACKONSLEEP_Enabled :
501                      VPRCSR_NORDIC_VPRNORDICSLEEPCTRL_STACKONSLEEP_Disabled)
502            << VPRCSR_NORDIC_VPRNORDICSLEEPCTRL_STACKONSLEEP_Pos;
503     nrf_csr_write(VPRCSR_NORDIC_VPRNORDICSLEEPCTRL, reg);
504 }
505 
nrf_vpr_csr_stack_on_sleep_check(void)506 NRF_STATIC_INLINE bool nrf_vpr_csr_stack_on_sleep_check(void)
507 {
508     return (nrf_csr_read(VPRCSR_NORDIC_VPRNORDICSLEEPCTRL)
509             & VPRCSR_NORDIC_VPRNORDICSLEEPCTRL_STACKONSLEEP_Msk)
510            >> VPRCSR_NORDIC_VPRNORDICSLEEPCTRL_STACKONSLEEP_Pos;
511 }
512 
nrf_vpr_csr_clic_round_robin_set(bool enable)513 NRF_STATIC_INLINE void nrf_vpr_csr_clic_round_robin_set(bool enable)
514 {
515     uint32_t reg = nrf_csr_read(VPRCSR_NORDIC_VPRNORDICFEATURESDISABLE);
516     reg = (reg & ~VPRCSR_NORDIC_VPRNORDICFEATURESDISABLE_DISABLECLICROUNDROBIN_Msk) |
517              NRF_VPR_CSR_NORDIC_KEY_MASK;
518 
519     reg |= (enable ? VPRCSR_NORDIC_VPRNORDICFEATURESDISABLE_DISABLECLICROUNDROBIN_Enabled :
520                      VPRCSR_NORDIC_VPRNORDICFEATURESDISABLE_DISABLECLICROUNDROBIN_Disabled)
521            << VPRCSR_NORDIC_VPRNORDICFEATURESDISABLE_DISABLECLICROUNDROBIN_Pos;
522     nrf_csr_write(VPRCSR_NORDIC_VPRNORDICFEATURESDISABLE, reg);
523 }
524 
nrf_vpr_csr_clic_round_robin_check(void)525 NRF_STATIC_INLINE bool nrf_vpr_csr_clic_round_robin_check(void)
526 {
527     return ((nrf_csr_read(VPRCSR_NORDIC_VPRNORDICFEATURESDISABLE)
528              & VPRCSR_NORDIC_VPRNORDICFEATURESDISABLE_DISABLECLICROUNDROBIN_Msk)
529             >> VPRCSR_NORDIC_VPRNORDICFEATURESDISABLE_DISABLECLICROUNDROBIN_Pos
530             == VPRCSR_NORDIC_VPRNORDICFEATURESDISABLE_DISABLECLICROUNDROBIN_Enabled);
531 }
532 
nrf_vpr_csr_unrecoverable_return_set(bool enable)533 NRF_STATIC_INLINE void nrf_vpr_csr_unrecoverable_return_set(bool enable)
534 {
535     uint32_t reg = nrf_csr_read(VPRCSR_NORDIC_VPRNORDICFEATURESDISABLE);
536     reg = (reg & ~VPRCSR_NORDIC_VPRNORDICFEATURESDISABLE_UNRECOVRETURN_Msk) |
537              NRF_VPR_CSR_NORDIC_KEY_MASK;
538 
539     reg |= (enable ? VPRCSR_NORDIC_VPRNORDICFEATURESDISABLE_UNRECOVRETURN_Enabled :
540                      VPRCSR_NORDIC_VPRNORDICFEATURESDISABLE_UNRECOVRETURN_Disabled)
541            << VPRCSR_NORDIC_VPRNORDICFEATURESDISABLE_UNRECOVRETURN_Pos;
542     nrf_csr_write(VPRCSR_NORDIC_VPRNORDICFEATURESDISABLE, reg);
543 }
544 
nrf_vpr_csr_unrecoverable_return_check(void)545 NRF_STATIC_INLINE bool nrf_vpr_csr_unrecoverable_return_check(void)
546 {
547     return ((nrf_csr_read(VPRCSR_NORDIC_VPRNORDICFEATURESDISABLE)
548              & VPRCSR_NORDIC_VPRNORDICFEATURESDISABLE_UNRECOVRETURN_Msk)
549             >> VPRCSR_NORDIC_VPRNORDICFEATURESDISABLE_UNRECOVRETURN_Pos
550             == VPRCSR_NORDIC_VPRNORDICFEATURESDISABLE_UNRECOVRETURN_Enabled);
551 }
552 
553 #endif // NRF_DECLARE_ONLY
554 
555 /** @} */
556 
557 #ifdef __cplusplus
558 }
559 #endif
560 
561 #endif // NRF_VPR_CSR_H__
562