1 /*
2 * Copyright (c) 2020 - 2023, 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 0
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 + NRF_SUBSCRIBE_PUBLISH_OFFSET_RADIO)) ? \
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
79
80 #if defined(NRFX_CLZ)
81 #define NRF_CLZ(value) NRFX_CLZ(value)
82 #elif defined(ISA_ARM)
83 #define NRF_CLZ(value) __CLZ(value)
84 #else
85 #define NRF_CLZ(value) __builtin_clz(value)
86 #endif
87
88 #if defined(NRFX_CTZ)
89 #define NRF_CTZ(value) NRFX_CTZ(value)
90 #elif defined(ISA_ARM)
91 #define NRF_CTZ(value) __CLZ(__RBIT(value))
92 #else
93 #define NRF_CTZ(value) __builtin_ctz(value)
94 #endif
95
96 /** @brief Macro for extracting relative pin number from the absolute pin number. */
97 #define NRF_PIN_NUMBER_TO_PIN(pin) ((pin) & 0x1F)
98
99 /** @brief Macro for extracting port number from the absolute pin number. */
100 #define NRF_PIN_NUMBER_TO_PORT(pin) ((pin) >> 5)
101
102 /** @brief Macro for extracting absolute pin number from the relative pin and port numbers. */
103 #define NRF_PIN_PORT_TO_PIN_NUMBER(pin, port) (((pin) & 0x1F) | ((port) << 5))
104
105 /**
106 * @brief Function for checking if an object is accesible by EasyDMA of given peripheral instance.
107 *
108 * Peripherals that use EasyDMA require buffers to be placed in certain memory regions.
109 *
110 * @param[in] p_reg Peripheral base pointer.
111 * @param[in] p_object Pointer to an object whose location is to be checked.
112 *
113 * @retval true The pointed object is located in the memory region accessible by EasyDMA.
114 * @retval false The pointed object is not located in the memory region accessible by EasyDMA.
115 */
116 NRF_STATIC_INLINE bool nrf_dma_accessible_check(void const * p_reg, void const * p_object);
117
118 NRF_STATIC_INLINE void nrf_barrier_w(void);
119
120 NRF_STATIC_INLINE void nrf_barrier_r(void);
121
122 NRF_STATIC_INLINE void nrf_barrier_rw(void);
123
124 #ifndef NRF_DECLARE_ONLY
125
nrf_event_readback(void * p_event_reg)126 NRF_STATIC_INLINE void nrf_event_readback(void * p_event_reg)
127 {
128 #if NRFX_CHECK(NRFX_EVENT_READBACK_ENABLED) && !defined(NRF51)
129 (void)*((volatile uint32_t *)(p_event_reg));
130 #else
131 (void)p_event_reg;
132 #endif
133 }
134
nrf_barrier_w(void)135 NRF_STATIC_INLINE void nrf_barrier_w(void)
136 {
137 #if defined(ISA_RISCV)
138 RISCV_FENCE(ow, ow);
139 #endif
140 }
141
nrf_barrier_r(void)142 NRF_STATIC_INLINE void nrf_barrier_r(void)
143 {
144 #if defined(ISA_RISCV)
145 RISCV_FENCE(ir, ir);
146 #endif
147 }
148
nrf_barrier_rw(void)149 NRF_STATIC_INLINE void nrf_barrier_rw(void)
150 {
151 #if defined(ISA_RISCV)
152 RISCV_FENCE(iorw, iorw);
153 #endif
154 }
155
156 #if defined(ADDRESS_DOMAIN_Msk)
nrf_address_domain_get(uint32_t addr)157 NRF_STATIC_INLINE uint8_t nrf_address_domain_get(uint32_t addr)
158 {
159 return (uint8_t)((addr & ADDRESS_DOMAIN_Msk) >> ADDRESS_DOMAIN_Pos);
160 }
161 #endif
162
163 #if defined(ADDRESS_REGION_Msk)
nrf_address_region_get(uint32_t addr)164 NRF_STATIC_INLINE nrf_region_t nrf_address_region_get(uint32_t addr)
165 {
166 return (nrf_region_t)((addr & ADDRESS_REGION_Msk) >> ADDRESS_REGION_Pos);
167 }
168 #endif
169
170 #if defined(ADDRESS_SECURITY_Msk)
nrf_address_security_get(uint32_t addr)171 NRF_STATIC_INLINE bool nrf_address_security_get(uint32_t addr)
172 {
173 return ((addr & ADDRESS_SECURITY_Msk) >> ADDRESS_SECURITY_Pos);
174 }
175 #endif
176
177 #if defined(ADDRESS_BUS_Msk)
nrf_address_bus_get(uint32_t addr,size_t size)178 NRF_STATIC_INLINE uint8_t nrf_address_bus_get(uint32_t addr, size_t size)
179 {
180 return (uint8_t)((addr & ADDRESS_BUS_Msk & ~(size - 1)) >> ADDRESS_BUS_Pos);
181 }
182 #endif
183
184 #if defined(ADDRESS_BRIDGE_GROUP_Msk)
nrf_address_bridge_group_get(uint32_t addr)185 NRF_STATIC_INLINE uint8_t nrf_address_bridge_group_get(uint32_t addr)
186 {
187 return (uint8_t)((addr & ADDRESS_BRIDGE_GROUP_Msk) >> ADDRESS_BRIDGE_GROUP_Pos);
188 }
189 #endif
190
191 #if defined(ADDRESS_DOMAIN_SPEED_Msk)
nrf_address_domain_speed_get(uint32_t addr)192 NRF_STATIC_INLINE nrf_domain_speed_t nrf_address_domain_speed_get(uint32_t addr)
193 {
194 return (nrf_domain_speed_t)((addr & ADDRESS_DOMAIN_SPEED_Msk) >> ADDRESS_DOMAIN_SPEED_Pos);
195 }
196 #endif
197
198 #if defined(ADDRESS_SLAVE_Msk)
nrf_address_slave_get(uint32_t addr)199 NRF_STATIC_INLINE uint8_t nrf_address_slave_get(uint32_t addr)
200 {
201 return (uint8_t)((addr & ADDRESS_SLAVE_Msk) >> ADDRESS_SLAVE_Pos);
202 }
203 #endif
204
205 #if defined(ADDRESS_PERIPHID_Msk)
nrf_address_periphid_get(uint32_t addr)206 NRF_STATIC_INLINE uint16_t nrf_address_periphid_get(uint32_t addr)
207 {
208 return (uint16_t)((addr & ADDRESS_PERIPHID_Msk) >> ADDRESS_PERIPHID_Pos);
209 }
210 #endif
211
nrf_dma_accessible_check(void const * p_reg,void const * p_object)212 NRF_STATIC_INLINE bool nrf_dma_accessible_check(void const * p_reg, void const * p_object)
213 {
214 #if defined(NRF_DMA_ACCESS_EXT)
215 NRF_DMA_ACCESS_EXT
216 #else
217 (void)p_reg;
218 return ((((uint32_t)p_object) & 0xE0000000u) == 0x20000000u);
219 #endif
220 }
221
222 #endif // NRF_DECLARE_ONLY
223
224 #ifdef __cplusplus
225 }
226 #endif
227
228 #endif // NRF_COMMON_H__
229