1 /*
2 * Copyright (c) 2016 - 2025, 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 #include <nrfx.h>
34
35 #if NRFX_CHECK(NRFX_SYSTICK_ENABLED)
36 #include <nrfx_systick.h>
37
38 /**
39 * @brief Maximum number of ticks to delay
40 *
41 * The maximum number of ticks should be much lower than
42 * Physical maximum count of the SysTick timer.
43 * It is dictated by the fact that it would be impossible to detect delay
44 * properly when the timer value warps around the starting point.
45 */
46 #define NRFX_SYSTICK_TICKS_MAX (NRF_SYSTICK_VAL_MASK / 2UL)
47
48 /**
49 * @brief Number of milliseconds in a second
50 */
51 #define NRFX_SYSTICK_MS (1000UL)
52
53 /**
54 * @brief Number of microseconds in a second
55 */
56 #define NRFX_SYSTICK_US (1000UL * NRFX_SYSTICK_MS)
57
58 /**
59 * @brief Number of milliseconds to wait in single loop
60 *
61 * Constant used by @ref nrd_drv_systick_delay_ms function
62 * to split waiting into loops and rest.
63 *
64 * It describes the number of milliseconds to wait in single loop.
65 *
66 * See @ref nrfx_systick_delay_ms source code for details.
67 */
68 #define NRFX_SYSTICK_MS_STEP (NRFX_SYSTICK_TICKS_MAX / (SystemCoreClock / NRFX_SYSTICK_MS))
69
70 /**
71 * @brief Checks if the given time is in correct range
72 *
73 * Function tests given time is not to big for this library.
74 * Assertion is used for testing.
75 *
76 * @param us Time in microseconds to check
77 */
78 #define NRFX_SYSTICK_ASSERT_TIMEOUT(us) \
79 NRFX_ASSERT(us <= (NRFX_SYSTICK_TICKS_MAX / ((SystemCoreClock) / NRFX_SYSTICK_US)));
80
81 /**
82 * @brief Function that converts microseconds to ticks
83 *
84 * Function converts from microseconds to CPU ticks.
85 *
86 * @param us Number of microseconds
87 *
88 * @return Number of ticks
89 *
90 * @sa nrfx_systick_ms_tick
91 */
nrfx_systick_us_tick(uint32_t us)92 static inline uint32_t nrfx_systick_us_tick(uint32_t us)
93 {
94 return us * ((SystemCoreClock) / NRFX_SYSTICK_US);
95 }
96
97 /**
98 * @brief Function that converts milliseconds to ticks
99 *
100 * Function converts from milliseconds to CPU ticks.
101 *
102 * @param us Number of milliseconds
103 *
104 * @return Number of ticks
105 *
106 * @sa nrfx_systick_us_tick
107 */
nrfx_systick_ms_tick(uint32_t ms)108 static inline uint32_t nrfx_systick_ms_tick(uint32_t ms)
109 {
110 return ms * ((SystemCoreClock) / NRFX_SYSTICK_MS);
111 }
112
nrfx_systick_init(void)113 void nrfx_systick_init(void)
114 {
115 nrf_systick_load_set(NRF_SYSTICK_VAL_MASK);
116 nrf_systick_csr_set(
117 NRF_SYSTICK_CSR_CLKSOURCE_CPU |
118 NRF_SYSTICK_CSR_TICKINT_DISABLE |
119 NRF_SYSTICK_CSR_ENABLE);
120 }
121
nrfx_systick_get(nrfx_systick_state_t * p_state)122 void nrfx_systick_get(nrfx_systick_state_t * p_state)
123 {
124 p_state->time = nrf_systick_val_get();
125 }
126
nrfx_systick_test(nrfx_systick_state_t const * p_state,uint32_t us)127 bool nrfx_systick_test(nrfx_systick_state_t const * p_state, uint32_t us)
128 {
129 NRFX_SYSTICK_ASSERT_TIMEOUT(us);
130
131 const uint32_t diff = NRF_SYSTICK_VAL_MASK & ((p_state->time) - nrf_systick_val_get());
132 return (diff >= nrfx_systick_us_tick(us));
133 }
134
nrfx_systick_delay_ticks(uint32_t ticks)135 void nrfx_systick_delay_ticks(uint32_t ticks)
136 {
137 NRFX_ASSERT(ticks <= NRFX_SYSTICK_TICKS_MAX);
138
139 const uint32_t start = nrf_systick_val_get();
140 while ((NRF_SYSTICK_VAL_MASK & (start - nrf_systick_val_get())) < ticks)
141 {
142 /* Nothing to do */
143 }
144 }
145
nrfx_systick_delay_us(uint32_t us)146 void nrfx_systick_delay_us(uint32_t us)
147 {
148 NRFX_SYSTICK_ASSERT_TIMEOUT(us);
149 nrfx_systick_delay_ticks(nrfx_systick_us_tick(us));
150 }
151
nrfx_systick_delay_ms(uint32_t ms)152 void nrfx_systick_delay_ms(uint32_t ms)
153 {
154 uint32_t n = ms / NRFX_SYSTICK_MS_STEP;
155 uint32_t r = ms % NRFX_SYSTICK_MS_STEP;
156 while (0 != (n--))
157 {
158 nrfx_systick_delay_ticks(nrfx_systick_ms_tick(NRFX_SYSTICK_MS_STEP));
159 }
160 nrfx_systick_delay_ticks(nrfx_systick_ms_tick(r));
161 }
162
163 #endif // NRFX_CHECK(NRFX_SYSTICK_ENABLED)
164