1 /*
2 * Copyright (c) 2016 - 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_SYSTICK_H__
35 #define NRF_SYSTICK_H__
36
37 #include <nrfx.h>
38
39 #ifdef __cplusplus
40 extern "C" {
41 #endif
42
43 /**
44 * @defgroup nrf_systick_hal SYSTICK HAL
45 * @{
46 * @ingroup nrf_systick
47 * @brief Hardware access layer (HAL) for managing the SYSTICK peripheral.
48 *
49 * SYSTICK is a peripheral designed by ARM.
50 * This means that it does not feature the typical Nordic interface with Tasks and Events.
51 *
52 * Its usage is limited here to the implementation of simple delays.
53 * Moreover, keep in mind that this timer will be stopped when CPU is sleeping
54 * (WFE/WFI instruction is successfully executed).
55 */
56
57 /**
58 * @brief Mask of usable bits in the SysTick value.
59 */
60 #define NRF_SYSTICK_VAL_MASK SysTick_VAL_CURRENT_Msk
61
62 /**
63 * @brief Flags used by SysTick configuration.
64 *
65 * @sa nrf_systick_csr_set
66 * @sa nrf_systick_csr_get
67 */
68 typedef enum {
69 NRF_SYSTICK_CSR_COUNTFLAG_MASK = SysTick_CTRL_COUNTFLAG_Msk, /**< Status flag: Returns 1 if timer counted to 0 since the last read of this register. */
70
71 NRF_SYSTICK_CSR_CLKSOURCE_MASK = SysTick_CTRL_CLKSOURCE_Msk, /**< Configuration bit: Select the SysTick clock source. */
72 NRF_SYSTICK_CSR_CLKSOURCE_REF = 0U << SysTick_CTRL_CLKSOURCE_Pos, /**< Configuration value: Select reference clock. */
73 NRF_SYSTICK_CSR_CLKSOURCE_CPU = 1U << SysTick_CTRL_CLKSOURCE_Pos, /**< Configuration value: Select CPU clock. */
74
75 NRF_SYSTICK_CSR_TICKINT_MASK = SysTick_CTRL_TICKINT_Msk, /**< Configuration bit: Enables SysTick exception request. */
76 NRF_SYSTICK_CSR_TICKINT_ENABLE = 1U << SysTick_CTRL_TICKINT_Pos, /**< Configuration value: Counting down to zero does not assert the SysTick exception request. */
77 NRF_SYSTICK_CSR_TICKINT_DISABLE = 0U << SysTick_CTRL_TICKINT_Pos, /**< Configuration value: Counting down to zero to asserts the SysTick exception request. */
78
79 NRF_SYSTICK_CSR_ENABLE_MASK = SysTick_CTRL_ENABLE_Msk, /**< Configuration bit: Enable the SysTick timer. */
80 NRF_SYSTICK_CSR_ENABLE = 1U << SysTick_CTRL_ENABLE_Pos, /**< Configuration value: Counter enabled. */
81 NRF_SYSTICK_CSR_DISABLE = 0U << SysTick_CTRL_ENABLE_Pos /**< Configuration value: Counter disabled. */
82 } nrf_systick_csr_flags_t;
83
84 /**
85 * @brief Function for getting Configuration and Status Register.
86 *
87 * @note The @ref NRF_SYSTICK_CSR_COUNTFLAG_MASK value is cleared when CSR register is read.
88 * @return Values composed by @ref nrf_systick_csr_flags_t.
89 */
90 NRF_STATIC_INLINE uint32_t nrf_systick_csr_get(void);
91
92 /**
93 * @brief Function for setting Configuration and Status Register.
94 *
95 * @param[in] val The value composed from @ref nrf_systick_csr_flags_t.
96 */
97 NRF_STATIC_INLINE void nrf_systick_csr_set(uint32_t val);
98
99 /**
100 * @brief Function for getting the current reload value.
101 *
102 * @return The reload register value.
103 */
104 NRF_STATIC_INLINE uint32_t nrf_systick_load_get(void);
105
106 /**
107 * @brief Function for configuring the reload value.
108 *
109 * @param[in] val The value to be set in the reload register.
110 */
111 NRF_STATIC_INLINE void nrf_systick_load_set(uint32_t val);
112
113 /**
114 * @brief Function for reading the SysTick current value.
115 *
116 * @return The current SysTick value
117 * @sa NRF_SYSTICK_VAL_MASK
118 */
119 NRF_STATIC_INLINE uint32_t nrf_systick_val_get(void);
120
121 /**
122 * @brief Function for clearing the SysTick current value.
123 *
124 * @note The SysTick does not allow setting current value.
125 * Any write to VAL register would clear the timer.
126 */
127 NRF_STATIC_INLINE void nrf_systick_val_clear(void);
128
129 /**
130 * @brief Function for reading the calibration register.
131 *
132 * @return The calibration register value.
133 */
134 NRF_STATIC_INLINE uint32_t nrf_systick_calib_get(void);
135
136
137 #ifndef NRF_DECLARE_ONLY
138
nrf_systick_csr_get(void)139 NRF_STATIC_INLINE uint32_t nrf_systick_csr_get(void)
140 {
141 return SysTick->CTRL;
142 }
143
nrf_systick_csr_set(uint32_t val)144 NRF_STATIC_INLINE void nrf_systick_csr_set(uint32_t val)
145 {
146 SysTick->CTRL = val;
147 }
148
nrf_systick_load_get(void)149 NRF_STATIC_INLINE uint32_t nrf_systick_load_get(void)
150 {
151 return SysTick->LOAD;
152 }
153
nrf_systick_load_set(uint32_t val)154 NRF_STATIC_INLINE void nrf_systick_load_set(uint32_t val)
155 {
156 SysTick->LOAD = val;
157 }
158
nrf_systick_val_get(void)159 NRF_STATIC_INLINE uint32_t nrf_systick_val_get(void)
160 {
161 return SysTick->VAL;
162 }
163
nrf_systick_val_clear(void)164 NRF_STATIC_INLINE void nrf_systick_val_clear(void)
165 {
166 SysTick->VAL = 0;
167 }
168
nrf_systick_calib_get(void)169 NRF_STATIC_INLINE uint32_t nrf_systick_calib_get(void)
170 {
171 return SysTick->CALIB;
172 }
173
174 #endif /* NRF_DECLARE_ONLY */
175
176 /** @} */
177
178 #ifdef __cplusplus
179 }
180 #endif
181
182 #endif /* NRF_SYSTICK_H__ */
183