1 /*
2 * Copyright (c) 2020 - 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
34 #ifndef NRF_COMMON_H__
35 #define NRF_COMMON_H__
36
37 #ifdef __cplusplus
38 extern "C" {
39 #endif
40
41 #ifndef NRFX_EVENT_READBACK_ENABLED
42 #define NRFX_EVENT_READBACK_ENABLED 1
43 #endif
44
45 #ifndef NRFX_CONFIG_API_VER_MAJOR
46 #define NRFX_CONFIG_API_VER_MAJOR 3
47 #endif
48
49 #ifndef NRFX_CONFIG_API_VER_MINOR
50 #define NRFX_CONFIG_API_VER_MINOR 8
51 #endif
52
53 #ifndef NRFX_CONFIG_API_VER_MICRO
54 #define NRFX_CONFIG_API_VER_MICRO 0
55 #endif
56
57 #if defined(ISA_RISCV)
58 #define RISCV_FENCE(p, s) __asm__ __volatile__ ("fence " #p "," #s : : : "memory")
59 #endif
60
61 #if defined(DPPI_PRESENT)
62 #ifndef NRF_SUBSCRIBE_PUBLISH_ENABLE
63 #define NRF_SUBSCRIBE_PUBLISH_ENABLE (0x01UL << 31UL)
64 #endif
65 #if defined(NRF_RADIO)
66 #define NRF_SUBSCRIBE_PUBLISH_OFFSET_RADIO \
67 (NRFX_OFFSETOF(NRF_RADIO_Type, SUBSCRIBE_TXEN) - NRFX_OFFSETOF(NRF_RADIO_Type, TASKS_TXEN))
68 #define NRF_SUBSCRIBE_PUBLISH_OFFSET(task_or_event) \
69 ((NRFX_IN_RANGE(task_or_event, (uint32_t)NRF_RADIO, \
70 (uint32_t)NRF_RADIO + sizeof(NRF_RADIO_Type))) ? \
71 (NRF_SUBSCRIBE_PUBLISH_OFFSET_RADIO) : \
72 (0x80uL))
73 #else
74 #define NRF_SUBSCRIBE_PUBLISH_OFFSET(task_or_event) 0x80uL
75 #endif // defined(NRF_RADIO)
76 #endif // defined(DPPI_PRESENT)
77
78 #if !defined(NRFY_CACHE_WB)
79 #define NRFY_CACHE_WB(p_buffer, size)
80 #endif
81
82 #if !defined(NRFY_CACHE_INV)
83 #define NRFY_CACHE_INV(p_buffer, size)
84 #endif
85
86 #if !defined(NRFY_CACHE_WBINV)
87 #define NRFY_CACHE_WBINV(p_buffer, size)
88 #endif
89
90 #if defined(NRFX_CLZ)
91 #define NRF_CLZ(value) NRFX_CLZ(value)
92 #elif defined(ISA_ARM)
93 #define NRF_CLZ(value) __CLZ(value)
94 #else
95 #define NRF_CLZ(value) __builtin_clz(value)
96 #endif
97
98 #if defined(NRFX_CTZ)
99 #define NRF_CTZ(value) NRFX_CTZ(value)
100 #elif defined(ISA_ARM)
101 #define NRF_CTZ(value) __CLZ(__RBIT(value))
102 #else
103 #define NRF_CTZ(value) __builtin_ctz(value)
104 #endif
105
106 #if defined(HALTIUM_XXAA) || defined(LUMOS_XXAA)
107 #define DMA_BUFFER_UNIFIED_BYTE_ACCESS 1
108 #endif
109
110 #if defined(LUMOS_XXAA)
111 #if defined(NRF_TRUSTZONE_NONSECURE)
112 /* Non-secure images must have CPU frequency specified and cannot rely on default values,
113 * as NRF_OSCILLATORS might be assigned and configured by Secure image. */
114 #if defined(NRF_CONFIG_CPU_FREQ_MHZ) && (NRF_CONFIG_CPU_FREQ_MHZ == 64)
115 #define NRF_CPU_FREQ_IS_64MHZ 1
116 #elif defined(NRF_CONFIG_CPU_FREQ_MHZ) && (NRF_CONFIG_CPU_FREQ_MHZ == 128)
117 #define NRF_CPU_FREQ_IS_128MHZ 1
118 #elif defined(NRF_CONFIG_CPU_FREQ_MHZ) && (NRF_CONFIG_CPU_FREQ_MHZ == 256)
119 #define NRF_CPU_FREQ_IS_256MHZ 1
120 #elif !defined(NRF_CONFIG_CPU_FREQ_MHZ)
121 #error "MCU frequency not specified"
122 #else
123 #error "Invalid MCU frequency"
124 #endif
125
126 #else
127
128 #if defined(NRF_SKIP_CLOCK_CONFIGURATION) || \
129 (defined(NRF_CONFIG_CPU_FREQ_MHZ) && (NRF_CONFIG_CPU_FREQ_MHZ == 64))
130 #define NRF_CPU_FREQ_IS_64MHZ 1
131 #elif defined(NRF_CONFIG_CPU_FREQ_MHZ) && (NRF_CONFIG_CPU_FREQ_MHZ == 128)
132 #define NRF_CPU_FREQ_IS_128MHZ 1
133 #elif !defined(NRF_CONFIG_CPU_FREQ_MHZ)
134 /* If clock configuration is not skipped and frequency not specified,
135 * SystemInit() applies 128 MHz setting. */
136 #define NRF_CPU_FREQ_IS_128MHZ 1
137 #elif defined(NRF_CONFIG_CPU_FREQ_MHZ) && (NRF_CONFIG_CPU_FREQ_MHZ == 256)
138 #define NRF_CPU_FREQ_IS_256MHZ 1
139 #else
140 #error "Invalid MCU frequency"
141 #endif
142 #endif
143 #endif
144
145 /** @brief Macro for extracting relative pin number from the absolute pin number. */
146 #define NRF_PIN_NUMBER_TO_PIN(pin) ((pin) & 0x1F)
147
148 /** @brief Macro for extracting port number from the absolute pin number. */
149 #define NRF_PIN_NUMBER_TO_PORT(pin) ((pin) >> 5)
150
151 /** @brief Macro for extracting absolute pin number from the relative pin and port numbers. */
152 #define NRF_PIN_PORT_TO_PIN_NUMBER(pin, port) (((pin) & 0x1F) | ((port) << 5))
153
154 #if defined(LUMOS_XXAA)
155 typedef NRF_DOMAINID_Type nrf_domain_t;
156 typedef NRF_OWNERID_Type nrf_owner_t;
157 #endif
158
159 #if defined(HALTIUM_XXAA)
160 typedef NRF_DOMAINID_Type nrf_domain_t;
161 typedef NRF_PROCESSORID_Type nrf_processor_t;
162 typedef NRF_OWNERID_Type nrf_owner_t;
163 #endif
164
165 /**
166 * @brief Function for checking if an object is accesible by EasyDMA of given peripheral instance.
167 *
168 * Peripherals that use EasyDMA require buffers to be placed in certain memory regions.
169 *
170 * @param[in] p_reg Peripheral base pointer.
171 * @param[in] p_object Pointer to an object whose location is to be checked.
172 *
173 * @retval true The pointed object is located in the memory region accessible by EasyDMA.
174 * @retval false The pointed object is not located in the memory region accessible by EasyDMA.
175 */
176 NRF_STATIC_INLINE bool nrf_dma_accessible_check(void const * p_reg, void const * p_object);
177
178 NRF_STATIC_INLINE void nrf_barrier_w(void);
179
180 NRF_STATIC_INLINE void nrf_barrier_r(void);
181
182 NRF_STATIC_INLINE void nrf_barrier_rw(void);
183
184 NRF_STATIC_INLINE bool nrf_event_check(void const * p_reg, uint32_t event);
185
186 NRF_STATIC_INLINE uint32_t nrf_task_event_address_get(void const * p_reg, uint32_t task_event);
187
188 #if defined(ADDRESS_DOMAIN_Msk)
189 NRF_STATIC_INLINE uint8_t nrf_address_domain_get(uint32_t addr);
190 #endif
191
192 #if defined(ADDRESS_REGION_Msk)
193 NRF_STATIC_INLINE nrf_region_t nrf_address_region_get(uint32_t addr);
194 #endif
195
196 #if defined(ADDRESS_SECURITY_Msk)
197 NRF_STATIC_INLINE bool nrf_address_security_get(uint32_t addr);
198 #endif
199
200 #if defined(ADDRESS_BUS_Msk)
201 NRF_STATIC_INLINE uint8_t nrf_address_bus_get(uint32_t addr, size_t size);
202 #endif
203
204 #if defined(ADDRESS_SLAVE_Msk)
205 NRF_STATIC_INLINE uint8_t nrf_address_slave_get(uint32_t addr);
206 #endif
207
208 #if defined(ADDRESS_PERIPHID_Msk)
209 NRF_STATIC_INLINE uint16_t nrf_address_periphid_get(uint32_t addr);
210 #endif
211
212 #ifndef NRF_DECLARE_ONLY
213
nrf_event_readback(void * p_event_reg)214 NRF_STATIC_INLINE void nrf_event_readback(void * p_event_reg)
215 {
216 #if NRFX_CHECK(NRFX_EVENT_READBACK_ENABLED) && !defined(NRF51)
217 (void)*((volatile uint32_t *)(p_event_reg));
218 #else
219 (void)p_event_reg;
220 #endif
221 }
222
nrf_barrier_w(void)223 NRF_STATIC_INLINE void nrf_barrier_w(void)
224 {
225 #if defined(ISA_RISCV)
226 RISCV_FENCE(ow, ow);
227 #endif
228 }
229
nrf_barrier_r(void)230 NRF_STATIC_INLINE void nrf_barrier_r(void)
231 {
232 #if defined(ISA_RISCV)
233 RISCV_FENCE(ir, ir);
234 #endif
235 }
236
nrf_barrier_rw(void)237 NRF_STATIC_INLINE void nrf_barrier_rw(void)
238 {
239 #if defined(ISA_RISCV)
240 RISCV_FENCE(iorw, iorw);
241 #endif
242 }
243
244 #if defined(ADDRESS_DOMAIN_Msk)
nrf_address_domain_get(uint32_t addr)245 NRF_STATIC_INLINE uint8_t nrf_address_domain_get(uint32_t addr)
246 {
247 return (uint8_t)((addr & ADDRESS_DOMAIN_Msk) >> ADDRESS_DOMAIN_Pos);
248 }
249 #endif
250
251 #if defined(ADDRESS_REGION_Msk)
nrf_address_region_get(uint32_t addr)252 NRF_STATIC_INLINE nrf_region_t nrf_address_region_get(uint32_t addr)
253 {
254 return (nrf_region_t)((addr & ADDRESS_REGION_Msk) >> ADDRESS_REGION_Pos);
255 }
256 #endif
257
258 #if defined(ADDRESS_SECURITY_Msk)
nrf_address_security_get(uint32_t addr)259 NRF_STATIC_INLINE bool nrf_address_security_get(uint32_t addr)
260 {
261 return ((addr & ADDRESS_SECURITY_Msk) >> ADDRESS_SECURITY_Pos);
262 }
263 #endif
264
265 #if defined(ADDRESS_BUS_Msk)
nrf_address_bus_get(uint32_t addr,size_t size)266 NRF_STATIC_INLINE uint8_t nrf_address_bus_get(uint32_t addr, size_t size)
267 {
268 return (uint8_t)((addr & ADDRESS_BUS_Msk & ~(size - 1)) >> ADDRESS_BUS_Pos);
269 }
270 #endif
271
272 #if defined(ADDRESS_SLAVE_Msk)
nrf_address_slave_get(uint32_t addr)273 NRF_STATIC_INLINE uint8_t nrf_address_slave_get(uint32_t addr)
274 {
275 return (uint8_t)((addr & ADDRESS_SLAVE_Msk) >> ADDRESS_SLAVE_Pos);
276 }
277 #endif
278
279 #if defined(ADDRESS_PERIPHID_Msk)
nrf_address_periphid_get(uint32_t addr)280 NRF_STATIC_INLINE uint16_t nrf_address_periphid_get(uint32_t addr)
281 {
282 return (uint16_t)((addr & ADDRESS_PERIPHID_Msk) >> ADDRESS_PERIPHID_Pos);
283 }
284 #endif
285
nrf_dma_accessible_check(void const * p_reg,void const * p_object)286 NRF_STATIC_INLINE bool nrf_dma_accessible_check(void const * p_reg, void const * p_object)
287 {
288 #if defined(HALTIUM_XXAA)
289 if (nrf_address_bus_get((uint32_t)p_reg, 0x10000) == 0x8E)
290 {
291 /* When peripheral instance is high-speed check whether */
292 /* p_object is placed in GRAM2x or GRAM0x */
293 bool gram0x = ((uint32_t)p_object & 0xEFF00000) == 0x2F000000;
294 bool gram2x = ((uint32_t)p_object & 0xEFF80000) == 0x2F880000;
295 return gram0x || gram2x;
296 }
297 else
298 {
299 /* When peripheral instance is low-speed check whether */
300 /* p_object is placed in GRAM3x */
301 return ((((uint32_t)p_object) & 0xEFFE0000u) == 0x2FC00000u);
302 }
303 #else
304 (void)p_reg;
305 return ((((uint32_t)p_object) & 0xE0000000u) == 0x20000000u);
306 #endif
307 }
308
nrf_event_check(void const * p_reg,uint32_t event)309 NRF_STATIC_INLINE bool nrf_event_check(void const * p_reg, uint32_t event)
310 {
311 return (bool)*(volatile const uint32_t *)((const uint8_t *)p_reg + (uint32_t)event);
312 }
313
nrf_task_event_address_get(void const * p_reg,uint32_t task_event)314 NRF_STATIC_INLINE uint32_t nrf_task_event_address_get(void const * p_reg, uint32_t task_event)
315 {
316 return (uint32_t)((const uint8_t *)p_reg + task_event);
317 }
318 #endif // NRF_DECLARE_ONLY
319
320 #ifdef __cplusplus
321 }
322 #endif
323
324 #endif // NRF_COMMON_H__
325