1 /*
2 * Copyright (c) 2016 - 2018, 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 for managing the SYSTICK peripheral.
46 *
47 * SYSTICK is ARM peripheral, not Nordic design.
48 * It means that it has no Nordic-typical interface with Tasks and Events.
49 *
50 * Its usage is limited here to implement simple delays.
51 * Also keep in mind that this timer would 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 Get Configuration and Status Register
84 *
85 * @return Values composed by @ref nrf_systick_csr_flags_t.
86 * @note The @ref NRF_SYSTICK_CSR_COUNTFLAG_MASK value is cleared when CSR register is read.
87 */
88 __STATIC_INLINE uint32_t nrf_systick_csr_get(void);
89
90 /**
91 * @brief Set Configuration and Status Register
92 *
93 * @param[in] val The value composed from @ref nrf_systick_csr_flags_t.
94 */
95 __STATIC_INLINE void nrf_systick_csr_set(uint32_t val);
96
97 /**
98 * @brief Get the current reload value.
99 *
100 * @return The reload register value.
101 */
102 __STATIC_INLINE uint32_t nrf_systick_load_get(void);
103
104 /**
105 * @brief Configure the reload value.
106 *
107 * @param[in] val The value to set in the reload register.
108 */
109 __STATIC_INLINE void nrf_systick_load_set(uint32_t val);
110
111 /**
112 * @brief Read the SysTick current value
113 *
114 * @return The current SysTick value
115 * @sa NRF_SYSTICK_VAL_MASK
116 */
117 __STATIC_INLINE uint32_t nrf_systick_val_get(void);
118
119 /**
120 * @brief Clear 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 __STATIC_INLINE void nrf_systick_val_clear(void);
126
127 /**
128 * @brief Read the calibration register
129 *
130 * @return The calibration register value
131 */
132 __STATIC_INLINE uint32_t nrf_systick_calib_get(void);
133
134
135
136 #ifndef SUPPRESS_INLINE_IMPLEMENTATION
137
nrf_systick_csr_get(void)138 __STATIC_INLINE uint32_t nrf_systick_csr_get(void)
139 {
140 return SysTick->CTRL;
141 }
142
nrf_systick_csr_set(uint32_t val)143 __STATIC_INLINE void nrf_systick_csr_set(uint32_t val)
144 {
145 SysTick->CTRL = val;
146 }
147
nrf_systick_load_get(void)148 __STATIC_INLINE uint32_t nrf_systick_load_get(void)
149 {
150 return SysTick->LOAD;
151 }
152
nrf_systick_load_set(uint32_t val)153 __STATIC_INLINE void nrf_systick_load_set(uint32_t val)
154 {
155 SysTick->LOAD = val;
156 }
157
nrf_systick_val_get(void)158 __STATIC_INLINE uint32_t nrf_systick_val_get(void)
159 {
160 return SysTick->VAL;
161 }
162
nrf_systick_val_clear(void)163 __STATIC_INLINE void nrf_systick_val_clear(void)
164 {
165 SysTick->VAL = 0;
166 }
167
nrf_systick_calib_get(void)168 __STATIC_INLINE uint32_t nrf_systick_calib_get(void)
169 {
170 return SysTick->CALIB;
171 }
172
173 #endif /* SUPPRESS_INLINE_IMPLEMENTATION */
174
175 /** @} */
176
177 #ifdef __cplusplus
178 }
179 #endif
180
181 #endif /* NRF_SYSTICK_H__ */
182