1 /*
2 * Copyright (c) 2016 - 2020, Nordic Semiconductor ASA
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions are met:
7 *
8 * 1. Redistributions of source code must retain the above copyright notice, this
9 * list of conditions and the following disclaimer.
10 *
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
14 *
15 * 3. Neither the name of the copyright holder nor the names of its
16 * contributors may be used to endorse or promote products derived from this
17 * software without specific prior written permission.
18 *
19 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
20 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
23 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29 * POSSIBILITY OF SUCH DAMAGE.
30 */
31
32 #ifndef NRF_SYSTICK_H__
33 #define NRF_SYSTICK_H__
34
35 #include <nrfx.h>
36
37 #ifdef __cplusplus
38 extern "C" {
39 #endif
40
41 /**
42 * @defgroup nrf_systick_hal SYSTICK HAL
43 * @{
44 * @ingroup nrf_systick
45 * @brief Hardware access layer (HAL) for managing the SYSTICK peripheral.
46 *
47 * SYSTICK is a peripheral designed by ARM.
48 * This means that it does not feature the typical Nordic interface with Tasks and Events.
49 *
50 * Its usage is limited here to the implementation of simple delays.
51 * Moreover, keep in mind that this timer will be stopped when CPU is sleeping
52 * (WFE/WFI instruction is successfully executed).
53 */
54
55 /**
56 * @brief Mask of usable bits in the SysTick value.
57 */
58 #define NRF_SYSTICK_VAL_MASK SysTick_VAL_CURRENT_Msk
59
60 /**
61 * @brief Flags used by SysTick configuration.
62 *
63 * @sa nrf_systick_csr_set
64 * @sa nrf_systick_csr_get
65 */
66 typedef enum {
67 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. */
68
69 NRF_SYSTICK_CSR_CLKSOURCE_MASK = SysTick_CTRL_CLKSOURCE_Msk, /**< Configuration bit: Select the SysTick clock source. */
70 NRF_SYSTICK_CSR_CLKSOURCE_REF = 0U << SysTick_CTRL_CLKSOURCE_Pos, /**< Configuration value: Select reference clock. */
71 NRF_SYSTICK_CSR_CLKSOURCE_CPU = 1U << SysTick_CTRL_CLKSOURCE_Pos, /**< Configuration value: Select CPU clock. */
72
73 NRF_SYSTICK_CSR_TICKINT_MASK = SysTick_CTRL_TICKINT_Msk, /**< Configuration bit: Enables SysTick exception request. */
74 NRF_SYSTICK_CSR_TICKINT_ENABLE = 1U << SysTick_CTRL_TICKINT_Pos, /**< Configuration value: Counting down to zero does not assert the SysTick exception request. */
75 NRF_SYSTICK_CSR_TICKINT_DISABLE = 0U << SysTick_CTRL_TICKINT_Pos, /**< Configuration value: Counting down to zero to asserts the SysTick exception request. */
76
77 NRF_SYSTICK_CSR_ENABLE_MASK = SysTick_CTRL_ENABLE_Msk, /**< Configuration bit: Enable the SysTick timer. */
78 NRF_SYSTICK_CSR_ENABLE = 1U << SysTick_CTRL_ENABLE_Pos, /**< Configuration value: Counter enabled. */
79 NRF_SYSTICK_CSR_DISABLE = 0U << SysTick_CTRL_ENABLE_Pos /**< Configuration value: Counter disabled. */
80 } nrf_systick_csr_flags_t;
81
82 /**
83 * @brief Function for getting Configuration and Status Register.
84 *
85 * @note The @ref NRF_SYSTICK_CSR_COUNTFLAG_MASK value is cleared when CSR register is read.
86 * @return Values composed by @ref nrf_systick_csr_flags_t.
87 */
88 NRF_STATIC_INLINE uint32_t nrf_systick_csr_get(void);
89
90 /**
91 * @brief Function for setting Configuration and Status Register.
92 *
93 * @param[in] val The value composed from @ref nrf_systick_csr_flags_t.
94 */
95 NRF_STATIC_INLINE void nrf_systick_csr_set(uint32_t val);
96
97 /**
98 * @brief Function for getting the current reload value.
99 *
100 * @return The reload register value.
101 */
102 NRF_STATIC_INLINE uint32_t nrf_systick_load_get(void);
103
104 /**
105 * @brief Function for configuring the reload value.
106 *
107 * @param[in] val The value to be set in the reload register.
108 */
109 NRF_STATIC_INLINE void nrf_systick_load_set(uint32_t val);
110
111 /**
112 * @brief Function for reading the SysTick current value.
113 *
114 * @return The current SysTick value
115 * @sa NRF_SYSTICK_VAL_MASK
116 */
117 NRF_STATIC_INLINE uint32_t nrf_systick_val_get(void);
118
119 /**
120 * @brief Function for clearing the SysTick current value.
121 *
122 * @note The SysTick does not allow setting current value.
123 * Any write to VAL register would clear the timer.
124 */
125 NRF_STATIC_INLINE void nrf_systick_val_clear(void);
126
127 /**
128 * @brief Function for reading the calibration register.
129 *
130 * @return The calibration register value.
131 */
132 NRF_STATIC_INLINE uint32_t nrf_systick_calib_get(void);
133
134
135 #ifndef NRF_DECLARE_ONLY
136
nrf_systick_csr_get(void)137 NRF_STATIC_INLINE uint32_t nrf_systick_csr_get(void)
138 {
139 return SysTick->CTRL;
140 }
141
nrf_systick_csr_set(uint32_t val)142 NRF_STATIC_INLINE void nrf_systick_csr_set(uint32_t val)
143 {
144 SysTick->CTRL = val;
145 }
146
nrf_systick_load_get(void)147 NRF_STATIC_INLINE uint32_t nrf_systick_load_get(void)
148 {
149 return SysTick->LOAD;
150 }
151
nrf_systick_load_set(uint32_t val)152 NRF_STATIC_INLINE void nrf_systick_load_set(uint32_t val)
153 {
154 SysTick->LOAD = val;
155 }
156
nrf_systick_val_get(void)157 NRF_STATIC_INLINE uint32_t nrf_systick_val_get(void)
158 {
159 return SysTick->VAL;
160 }
161
nrf_systick_val_clear(void)162 NRF_STATIC_INLINE void nrf_systick_val_clear(void)
163 {
164 SysTick->VAL = 0;
165 }
166
nrf_systick_calib_get(void)167 NRF_STATIC_INLINE uint32_t nrf_systick_calib_get(void)
168 {
169 return SysTick->CALIB;
170 }
171
172 #endif /* NRF_DECLARE_ONLY */
173
174 /** @} */
175
176 #ifdef __cplusplus
177 }
178 #endif
179
180 #endif /* NRF_SYSTICK_H__ */
181