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