1 /*
2  * Copyright (c) 2017 - 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 NRFX_COMMON_H__
35 #define NRFX_COMMON_H__
36 
37 #include <stdint.h>
38 #include <stddef.h>
39 #include <stdbool.h>
40 #include <string.h>
41 #include <limits.h>
42 
43 #include <nrf.h>
44 #include "nrfx_utils.h"
45 #include <nrf_peripherals.h>
46 #include <nrf_mem.h>
47 #include "nrfx_ext.h"
48 
49 #ifdef __cplusplus
50 extern "C" {
51 #endif
52 
53 #if defined(__CORTEX_M) || defined(__NRFX_DOXYGEN__)
54 #define ISA_ARM     1
55 #elif defined(__VPR_REV)
56 #define ISA_RISCV   1
57 #else
58 #define ISA_UNKNOWN 1
59 #endif
60 
61 #if defined(ISA_RISCV)
62 #define __STATIC_INLINE __attribute__((always_inline)) static inline
63 #endif
64 
65 #ifndef NRFX_STATIC_INLINE
66 #ifdef NRFX_DECLARE_ONLY
67 #define NRFX_STATIC_INLINE
68 #else
69 #define NRFX_STATIC_INLINE __STATIC_INLINE
70 #endif
71 #endif // NRFX_STATIC_INLINE
72 
73 #define NRFY_STATIC_INLINE __STATIC_INLINE
74 
75 #ifndef NRF_STATIC_INLINE
76 #ifdef NRF_DECLARE_ONLY
77 #define NRF_STATIC_INLINE
78 #else
79 #define NRF_STATIC_INLINE __STATIC_INLINE
80 #endif
81 #endif // NRF_STATIC_INLINE
82 
83 /**
84  * @defgroup nrfx_common Common module
85  * @{
86  * @ingroup nrfx
87  * @brief Common module.
88  */
89 
90 /**
91  * @brief Macro for checking if the specified identifier is defined and it has
92  *        a non-zero value.
93  *
94  * Normally, preprocessors treat all undefined identifiers as having the value
95  * zero. However, some tools, like static code analyzers, can issue a warning
96  * when such identifier is evaluated. This macro gives the possibility to suppress
97  * such warnings only in places where this macro is used for evaluation, not in
98  * the whole analyzed code.
99  */
100 #define NRFX_CHECK(module_enabled) NRFX_IS_ENABLED(module_enabled)
101 
102 /**
103  * @brief Macro for checking if the configured API version is greater than or equal
104  *        to the specified API version.
105  *
106  * @note API version to be used is configured using following symbols:
107  *       - @ref NRFX_CONFIG_API_VER_MAJOR
108  *       - @ref NRFX_CONFIG_API_VER_MINOR
109  *       - @ref NRFX_CONFIG_API_VER_MICRO
110  *
111  * @param[in] major Major API version.
112  * @param[in] minor Minor API version.
113  * @param[in] micro Micro API version.
114  *
115  * @retval true  Configured API version is greater than or equal to the specified API version.
116  * @retval false Configured API version is smaller than the specified API version.
117  */
118 #define NRFX_API_VER_AT_LEAST(major, minor, micro) \
119     ((NRFX_CONFIG_API_VER_MAJOR >= (major)) &&     \
120      (NRFX_CONFIG_API_VER_MINOR >= (minor)) &&     \
121      (NRFX_CONFIG_API_VER_MICRO >= (micro)))
122 
123 /**
124  * @brief Macro for creating unsigned integer with bit position @p x set.
125  *
126  * @param[in] x Bit position to be set.
127  *
128  * @return Unsigned integer with requested bit position set.
129  */
130 #define NRFX_BIT(x) (1UL << (x))
131 
132 /**
133  * @brief Macro for returning bit mask or 0 if @p x is 0.
134  *
135  * @param[in] x Bit mask size. Bit mask has bits 0 through x-1 (inclusive) set.
136  *
137  * @return Bit mask.
138  */
139 #define NRFX_BIT_MASK(x) (((x) == 32) ? UINT32_MAX : ((1UL << (x)) - 1))
140 
141 /**
142  * @brief Macro for returning size in bits for given size in bytes.
143  *
144  * @param[in] x Size in bytes.
145  *
146  * @return Size in bits.
147  */
148 #define NRFX_BIT_SIZE(x) ((x) << 3)
149 
150 /**
151  * @brief Macro for concatenating two tokens in macro expansion.
152  *
153  * @note This macro is expanded in two steps so that tokens given as macros
154  *       themselves are fully expanded before they are merged.
155  *
156  * @param[in] p1 First token.
157  * @param[in] p2 Second token.
158  *
159  * @return The two tokens merged into one, unless they cannot together form
160  *         a valid token (in such case, the preprocessor issues a warning and
161  *         does not perform the concatenation).
162  *
163  * @sa NRFX_CONCAT_3
164  */
165 #define NRFX_CONCAT_2(p1, p2) NRFX_CONCAT_2_(p1, p2)
166 
167 /** @brief Internal macro used by @ref NRFX_CONCAT_2 to perform the expansion in two steps. */
168 #define NRFX_CONCAT_2_(p1, p2) p1 ## p2
169 
170 /**
171  * @brief Macro for concatenating three tokens in macro expansion.
172  *
173  * @note This macro is expanded in two steps so that tokens given as macros
174  *       themselves are fully expanded before they are merged.
175  *
176  * @param[in] p1 First token.
177  * @param[in] p2 Second token.
178  * @param[in] p3 Third token.
179  *
180  * @return The three tokens merged into one, unless they cannot together form
181  *         a valid token (in such case, the preprocessor issues a warning and
182  *         does not perform the concatenation).
183  *
184  * @sa NRFX_CONCAT_2
185  */
186 #define NRFX_CONCAT_3(p1, p2, p3) NRFX_CONCAT_3_(p1, p2, p3)
187 
188 /** @brief Internal macro used by @ref NRFX_CONCAT_3 to perform the expansion in two steps. */
189 #define NRFX_CONCAT_3_(p1, p2, p3) p1 ## p2 ## p3
190 
191 /**
192  * @brief Macro for computing the absolute value of an integer number.
193  *
194  * @param[in] a Input value.
195  *
196  * @return Absolute value.
197  */
198 #define NRFX_ABS(a) ((a) < (0) ? -(a) : (a))
199 
200 /**
201  * @brief Macro for checking whether any of the instance of the specified peripheral supports a given feature.
202  *
203  * Macro checks flags set in \<device\>_peripherals.h file.
204  *
205  * Macro supports check on instances with following names:
206  * - \<periph_name\>0 - \<periph_name\>255 - e.g. SPIM0, SPIM255
207  * - \<periph_name\>00 - \<periph_name\>099 - e.g. SPIM00, SPIM099
208  * - \<periph_name\>000 - \<periph_name\>009 - e.g. SPIM000, SPIM009
209  *
210  * @param[in] periph_name  Peripheral name, e.g. SPIM.
211  * @param[in] feature_name Feature flag name suffix following an instance name, e.g.
212  *                         _FEATURE_HARDWARE_CSN_PRESENT.
213  *
214  * @retval 1 At least one instance on current device supports a given feature.
215  * @retval 0 None of peripheral instances supports a given feature.
216  */
217 #define NRFX_FEATURE_PRESENT(periph_name, feature_name)                                            \
218         NRFX_COND_CODE_0(NRFX_CONCAT(0,                                                            \
219                             _NRFX_FEATURE_PRESENT(periph_name, feature_name, 256),                 \
220                             _NRFX_FEATURE_PRESENT(NRFX_CONCAT(periph_name, 0), feature_name, 100), \
221                             _NRFX_FEATURE_PRESENT(NRFX_CONCAT(periph_name, 00), feature_name, 10)  \
222                          ),                                                                        \
223                         (0), (1))
224 
225 /**
226  * @brief Macro for resolving provided user macro for enabled instances of a driver.
227  *
228  * Macro checks if driver instances are enabled for all potential instaces of a
229  * peripheral. It takes peripheral name and checks whether NRFX_\<peripheral\>\<id\>_ENABLED
230  * is set to 1 and if yes then provided macro is evaluated for given instance.
231  *
232  * Macro supports check on instances with following names:
233  * - \<periph_name\>0 - \<periph_name\>255 - e.g. SPIM0, SPIM255
234  * - \<periph_name\>00 - \<periph_name\>099 - e.g. SPIM00, SPIM099
235  * - \<periph_name\>000 - \<periph_name\>009 - e.g. SPIM000, SPIM009
236  *
237  * @param[in] periph_name Peripheral name, e.g. SPIM.
238  * @param[in] macro       Macro which is resolved if driver instance is enabled. Macro has following
239  *                        arguments: macro(periph_name, prefix, i, ...).
240  * @param[in] sep         Separator added between all evaluations, in parentheses.
241  * @param[in] off_code    Code injected for disabled instances, in parentheses.
242  */
243 #define NRFX_FOREACH_ENABLED(periph_name, macro, sep, off_code, ...)                  \
244         NRFX_LISTIFY(256, _NRFX_EVAL_IF_ENABLED, sep,                                 \
245                      off_code, periph_name, , macro, __VA_ARGS__) NRFX_DEBRACKET sep  \
246         NRFX_LISTIFY(100, _NRFX_EVAL_IF_ENABLED, sep,                                 \
247                      off_code, periph_name, 0, macro, __VA_ARGS__) NRFX_DEBRACKET sep \
248         NRFX_LISTIFY(10, _NRFX_EVAL_IF_ENABLED, sep,                                  \
249                      off_code, periph_name, 00, macro, __VA_ARGS__)
250 
251 /**
252  * @brief Macro for resolving provided user macro for present instances of a peripheral.
253  *
254  * Macro checks if peripheral instances are present by checking if there is a token
255  * NRF_\<periph_name\>\<id\> defined with wrapped in parenthesis value.
256  *
257  * Macro supports check on instances with following names:
258  * - \<periph_name\>0 - \<periph_name\>255 - e.g. SPIM0, SPIM255
259  * - \<periph_name\>00 - \<periph_name\>099 - e.g. SPIM00, SPIM099
260  * - \<periph_name\>000 - \<periph_name\>009 - e.g. SPIM000, SPIM009
261  * - \<periph_name\> - e.g. SPIM
262  *
263  * @param[in] periph_name Peripheral name, e.g. SPIM.
264  * @param[in] macro       Macro which is resolved if peripheral instance is present.
265  *                        Macro has following arguments: macro(periph_name, prefix, i, ...).
266  * @param[in] sep         Separator added between all evaluations, in parentheses.
267  * @param[in] off_code    Code injected for disabled instances, in parentheses.
268  */
269 #define NRFX_FOREACH_PRESENT(periph_name, macro, sep, off_code, ...)                   \
270         NRFX_LISTIFY(256, _NRFX_EVAL_IF_PRESENT, sep,                                  \
271                      off_code, periph_name, , macro, __VA_ARGS__) NRFX_DEBRACKET sep   \
272         NRFX_LISTIFY(100, _NRFX_EVAL_IF_PRESENT, sep,                                  \
273                      off_code, periph_name, 0, macro, __VA_ARGS__) NRFX_DEBRACKET sep  \
274         NRFX_LISTIFY(10, _NRFX_EVAL_IF_PRESENT, sep,                                   \
275                      off_code, periph_name, 00, macro, __VA_ARGS__) NRFX_DEBRACKET sep \
276         _NRFX_EVAL_IF_PRESENT(, off_code, periph_name, , macro, __VA_ARGS__)
277 
278 /**
279  * @brief Macro for resolving provided user macro on concatenated peripheral name
280  *        and instance index.
281  *
282  * Execute provided macro with single argument <instance\>
283  * that is the concatenation of @p periph_name, @p prefix and @p i.
284  *
285  * @param[in] i           Instance index.
286  * @param[in] periph_name Peripheral name, e.g. SPIM.
287  * @param[in] prefix      Prefix added before instance index, e.g. some device has
288  *                        instances named like SPIM00. First 0 is passed here as prefix.
289  * @param[in] macro       Macro which is executed.
290  * @param[in] ...         Variable length arguments passed to the @p macro. Macro has following
291  *                        arguments: macro(instance, ...).
292  */
293 #define NRFX_INSTANCE_CONCAT(periph_name, prefix, i, macro, ...) \
294       macro(NRFX_CONCAT(periph_name, prefix, i), __VA_ARGS__)
295 
296 /**
297  * @brief Macro for creating a content for enum which is listing enabled driver instances.
298  *
299  * It creates comma separated list of entries like NRFX_\<instance_name\>_INST_IDX,
300  * e.g. (NRFX_SPIM0_INST_IDX) for all enabled instances (NRFX_\<instance_name\>_ENABLED
301  * is set to 1). It should be called within enum declaration. Created enum is used
302  * by the driver to index all enabled instances of the driver.
303  *
304  * @param[in] periph_name Peripheral name (e.g. SPIM).
305  */
306 #define NRFX_INSTANCE_ENUM_LIST(periph_name) \
307         NRFX_FOREACH_ENABLED(periph_name, _NRFX_INST_ENUM, (), ())
308 
309 /**
310  * @brief Macro for creating an interrupt handler for all enabled driver instances.
311  *
312  * Macro creates a set of functions which calls generic @p irq_handler function with two parameters:
313  * - peripheral instance register pointer
314  * - pointer to a control block structure associated with the given instance
315  *
316  * Generic interrupt handler function with above mentioned parameters named @p irq_handler
317  * must be implemented in the driver.
318  *
319  * @note Handlers are using enum which should be generated using @ref NRFX_INSTANCE_ENUM_LIST.
320  *
321  * @param[in] periph_name       Peripheral name, e.g. SPIM.
322  * @param[in] periph_name_small Peripheral name written with small letters, e.g. spim.
323  */
324 #define NRFX_INSTANCE_IRQ_HANDLERS(periph_name, periph_name_small) \
325     NRFX_FOREACH_ENABLED(periph_name, _NRFX_IRQ_HANDLER, (), (), periph_name_small)
326 
327 /**
328  * @brief Macro for creating an interrupt handler for all enabled driver instances
329  *        with the specified extra parameter.
330  *
331  * Macro creates set of function which calls generic @p irq_handler function with three parameters:
332  * - peripheral instance register pointer
333  * - pointer to a control block structure associated with the given instance
334  * - provided @p ext_macro called with peripheral name suffix (e.g. 01 for TIMER01)
335  *
336  * Generic interrupt handler function with above mentioned parameters named @p irq_handler
337  * must be implemented in the driver.
338  *
339  * @note Handlers are using enum which should be generated using @ref NRFX_INSTANCE_ENUM_LIST.
340  *
341  * @param[in] periph_name       Peripheral name, e.g. SPIM.
342  * @param[in] periph_name_small Peripheral name written with small letters, e.g. rtc.
343  * @param[in] ext_macro         External macro to be executed for each instance.
344  */
345 #define NRFX_INSTANCE_IRQ_HANDLERS_EXT(periph_name, periph_name_small, ext_macro) \
346     NRFX_FOREACH_ENABLED(periph_name, _NRFX_IRQ_HANDLER_EXT, (), (), periph_name_small, ext_macro)
347 
348 /**
349  * @brief Macro for declaring an interrupt handler for all enabled driver instances.
350  *
351  * Macro creates set of function declarations. It is intended to be used in the driver header.
352  *
353  * @param[in] periph_name       Peripheral name, e.g. SPIM.
354  * @param[in] periph_name_small Peripheral name written with small letters, e.g. spim.
355  */
356 #define NRFX_INSTANCE_IRQ_HANDLERS_DECLARE(periph_name, periph_name_small) \
357     NRFX_FOREACH_ENABLED(periph_name, _NRFX_IRQ_HANDLER_DECLARE, (), (), periph_name_small)
358 
359 /**
360  * @brief Macro for generating comma-separated list of interrupt handlers for all
361  *        enabled driver instances.
362  *
363  * Interrupt handlers are generated using @ref NRFX_INSTANCE_IRQ_HANDLERS.
364  * It is intended to be used to create a list which is used for passing an interrupt
365  * handler function to the PRS driver.
366  *
367  * @param[in] periph_name       Peripheral name, e.g. SPIM.
368  * @param[in] periph_name_small Peripheral name written with small letters, e.g. spim.
369  */
370 #define NRFX_INSTANCE_IRQ_HANDLERS_LIST(periph_name, periph_name_small) \
371     NRFX_FOREACH_ENABLED(periph_name, _NRFX_IRQ_HANDLER_LIST, (), (), periph_name_small)
372 
373 /**
374  * @brief Macro for checking if given peripheral instance is present on the target.
375  *
376  * Macro utilizes the fact that for each existing instance a define is created which points to
377  * the memory mapped register set casted to a register set structure. It is wrapped in parenthesis
378  * and existance of parethesis wrapping is used to determine if instance exists. It if does not
379  * exist then token (e.g. NRF_SPIM10) is undefined so it does not have parenthesis wrapping.
380  *
381  * Since macro returns literal 1 it can be used by other macros.
382  *
383  * @param[in] _inst Instance, .e.g SPIM10.
384  *
385  * @retval 1 If instance is present.
386  * @retval 0 If instance is not present.
387  */
388 #define NRFX_INSTANCE_PRESENT(_inst) NRFX_ARG_HAS_PARENTHESIS(NRFX_CONCAT(NRF_, _inst))
389 
390 /**
391  * @brief Macro for getting the smaller value between two arguments.
392  *
393  * @param[in] a First argument.
394  * @param[in] b Second argument.
395  *
396  * @return Smaller value between two arguments.
397  */
398 #define NRFX_MIN(a, b) ((a) < (b) ? (a) : (b))
399 
400 /**
401  * @brief Macro for getting the larger value between two arguments.
402  *
403  * @param[in] a First argument.
404  * @param[in] b Second argument.
405  *
406  * @return Larger value between two arguments.
407  */
408 #define NRFX_MAX(a, b) ((a) > (b) ? (a) : (b))
409 
410 /**
411  * @brief Macro for checking if a given value is in a given range.
412  *
413  * @note @p val is evaluated twice.
414  *
415  * @param[in] val A value to be checked.
416  * @param[in] min The lower bound (inclusive).
417  * @param[in] max The upper bound (inclusive).
418  *
419  * @retval true  The value is in the given range.
420  * @retval false The value is out of the given range.
421  */
422 #define NRFX_IN_RANGE(val, min, max) ((val) >= (min) && (val) <= (max))
423 
424 /**
425  * @brief Macro for performing rounded integer division (as opposed to
426  *        truncating the result).
427  *
428  * @param[in] a Numerator.
429  * @param[in] b Denominator.
430  *
431  * @return Rounded (integer) result of dividing @c a by @c b.
432  */
433 #define NRFX_ROUNDED_DIV(a, b) \
434     ((((a) < 0) ^ ((b) < 0)) ? (((a) - (b) / 2) / (b)) : (((a) + (b) / 2) / (b)))
435 
436 /**
437  * @brief Macro for performing integer division, making sure the result is rounded up.
438  *
439  * @details A typical use case for this macro is to compute the number of objects
440  *          with size @c b required to hold @c a number of bytes.
441  *
442  * @param[in] a Numerator.
443  * @param[in] b Denominator.
444  *
445  * @return Integer result of dividing @c a by @c b, rounded up.
446  */
447 #define NRFX_CEIL_DIV(a, b) ((((a) - 1) / (b)) + 1)
448 
449 /**
450  * @brief Macro for getting the number of elements in an array.
451  *
452  * @param[in] array Name of the array.
453  *
454  * @return Array element count.
455  */
456 #define NRFX_ARRAY_SIZE(array) (sizeof(array) / sizeof((array)[0]))
457 
458 /**
459  * @brief Macro for getting the offset (in bytes) from the beginning of a structure
460  *        of the specified type to its specified member.
461  *
462  * @param[in] type   Structure type.
463  * @param[in] member Structure member whose offset is searched for.
464  *
465  * @return Member offset in bytes.
466  */
467 #define NRFX_OFFSETOF(type, member) ((size_t) & (((type *)0)->member))
468 
469 /**
470  * @brief Macro for checking whether given number is power of 2.
471  *
472  * @param[in] val Tested value.
473  *
474  * @retval true  The value is power of 2.
475  * @retval false The value is not power of 2.
476  */
477 #define NRFX_IS_POWER_OF_TWO(val) (((val) != 0) && ((val) & ((val) - 1)) == 0)
478 
479 /**
480  * @brief Macro for checking whether a given number is even.
481  *
482  * @param[in] val Tested value.
483  *
484  * @retval true  The value is even.
485  * @retval false The value is odd.
486  */
487 #define NRFX_IS_EVEN(val) (((val) % 2)  == 0)
488 
489 /**
490  * @brief Macro for checking if given lengths of EasyDMA transfers do not exceed
491  *        the limit of the specified peripheral.
492  *
493  * @param[in] peripheral Peripheral to check the lengths against.
494  * @param[in] length1    First length to be checked.
495  * @param[in] length2    Second length to be checked (pass 0 if not needed).
496  *
497  * @retval true  The length of buffers does not exceed the limit of the specified peripheral.
498  * @retval false The length of buffers exceeds the limit of the specified peripheral.
499  */
500 #define NRFX_EASYDMA_LENGTH_VALIDATE(peripheral, length1, length2)            \
501     (((length1) < (1U << NRFX_CONCAT_2(peripheral, _EASYDMA_MAXCNT_SIZE))) && \
502      ((length2) < (1U << NRFX_CONCAT_2(peripheral, _EASYDMA_MAXCNT_SIZE))))
503 
504 /**
505  * @brief Macro for waiting until condition is met.
506  *
507  * @param[in]  condition Condition to meet.
508  * @param[in]  attempts  Maximum number of condition checks. Must not be 0.
509  * @param[in]  delay_us  Delay between consecutive checks, in microseconds.
510  * @param[out] result    Boolean variable to store the result of the wait process.
511  *                       Set to true if the condition is met or false otherwise.
512  */
513 #define NRFX_WAIT_FOR(condition, attempts, delay_us, result) \
514 do {                                                         \
515     result =  false;                                         \
516     uint32_t remaining_attempts = (attempts);                \
517     do {                                                     \
518            if (condition)                                    \
519            {                                                 \
520                result =  true;                               \
521                break;                                        \
522            }                                                 \
523            NRFX_DELAY_US(delay_us);                          \
524     } while (--remaining_attempts);                          \
525 } while(0)
526 
527 /**
528  * @brief Macro for getting the ID number of the specified peripheral.
529  *
530  * For peripherals in Nordic SoCs, there is a direct relationship between their
531  * ID numbers and their base addresses. See the chapter "Peripheral interface"
532  * (section "Peripheral ID") in the Product Specification.
533  *
534  * @param[in] base_addr Peripheral base address or pointer.
535  *
536  * @return ID number associated with the specified peripheral.
537  */
538 #define NRFX_PERIPHERAL_ID_GET(base_addr) (uint16_t)(((uint32_t)(base_addr) >> 12) & 0x000001FF)
539 
540 /**
541  * @brief Macro for getting the interrupt number assigned to a specific
542  *        peripheral.
543  *
544  * For peripherals in Nordic SoCs, the IRQ number assigned to a peripheral is
545  * equal to its ID number. See the chapter "Peripheral interface" (sections
546  * "Peripheral ID" and "Interrupts") in the Product Specification.
547  *
548  * @param[in] base_addr Peripheral base address or pointer.
549  *
550  * @return Interrupt number associated with the specified peripheral.
551  */
552 #define NRFX_IRQ_NUMBER_GET(base_addr) NRFX_PERIPHERAL_ID_GET(base_addr)
553 
554 /**
555  * @brief Macro for converting frequency in kHz to Hz.
556  *
557  * @param[in] freq Frequency value in kHz.
558  *
559  * @return Number of Hz in @p freq kHz.
560  */
561 #define NRFX_KHZ_TO_HZ(freq) ((freq) * 1000)
562 
563 /**
564  * @brief Macro for converting frequency in MHz to Hz.
565  *
566  * @param[in] freq Frequency value in MHz.
567  *
568  * @return Number of Hz in @p freq MHz.
569  */
570 #define NRFX_MHZ_TO_HZ(freq) ((freq) * 1000 * 1000)
571 
572 /** @brief IRQ handler type. */
573 typedef void (* nrfx_irq_handler_t)(void);
574 
575 /** @brief Driver state. */
576 typedef enum
577 {
578     NRFX_DRV_STATE_UNINITIALIZED, ///< Uninitialized.
579     NRFX_DRV_STATE_INITIALIZED,   ///< Initialized but powered off.
580     NRFX_DRV_STATE_POWERED_ON,    ///< Initialized and powered on.
581 } nrfx_drv_state_t;
582 
583 /**
584  * @brief Function for checking if an object is placed in the Data RAM region.
585  *
586  * Several peripherals (the ones using EasyDMA) require the transfer buffers
587  * to be placed in the Data RAM region. This function can be used to check if
588  * this condition is met.
589  *
590  * @param[in] p_object Pointer to an object whose location is to be checked.
591  *
592  * @retval true  The pointed object is located in the Data RAM region.
593  * @retval false The pointed object is not located in the Data RAM region.
594  */
595 NRF_STATIC_INLINE bool nrfx_is_in_ram(void const * p_object);
596 
597 /**
598  * @brief Function for checking if an object is aligned to a 32-bit word
599  *
600  * Several peripherals (the ones using EasyDMA) require the transfer buffers
601  * to be aligned to a 32-bit word. This function can be used to check if
602  * this condition is met.
603  *
604  * @param[in] p_object  Pointer to an object whose location is to be checked.
605  *
606  * @retval true  The pointed object is aligned to a 32-bit word.
607  * @retval false The pointed object is not aligned to a 32-bit word.
608  */
609 NRF_STATIC_INLINE bool nrfx_is_word_aligned(void const * p_object);
610 
611 /**
612  * @brief Function for getting the interrupt number for the specified peripheral.
613  *
614  * @param[in] p_reg Peripheral base pointer.
615  *
616  * @return Interrupt number associated with the pointed peripheral.
617  */
618 NRF_STATIC_INLINE IRQn_Type nrfx_get_irq_number(void const * p_reg);
619 
620 /**
621  * @brief Function for converting an INTEN register bit position to the
622  *        corresponding event identifier.
623  *
624  * The event identifier is the offset between the event register address and
625  * the peripheral base address, and is equal (thus, can be directly cast) to
626  * the corresponding value of the enumerated type from HAL (nrf_*_event_t).
627  *
628  * @param[in] bit INTEN register bit position.
629  *
630  * @return Event identifier.
631  *
632  * @sa nrfx_event_to_bitpos
633  */
634 NRF_STATIC_INLINE uint32_t nrfx_bitpos_to_event(uint32_t bit);
635 
636 /**
637  * @brief Function for converting an event identifier to the corresponding
638  *        INTEN register bit position.
639  *
640  * The event identifier is the offset between the event register address and
641  * the peripheral base address, and is equal (thus, can be directly cast) to
642  * the corresponding value of the enumerated type from HAL (nrf_*_event_t).
643  *
644  * @param[in] event Event identifier.
645  *
646  * @return INTEN register bit position.
647  *
648  * @sa nrfx_bitpos_to_event
649  */
650 NRF_STATIC_INLINE uint32_t nrfx_event_to_bitpos(uint32_t event);
651 
652 #ifndef NRF_DECLARE_ONLY
653 
nrfx_is_in_ram(void const * p_object)654 NRF_STATIC_INLINE bool nrfx_is_in_ram(void const * p_object)
655 {
656     return ((((uint32_t)p_object) & 0xE0000000u) == 0x20000000u);
657 }
658 
nrfx_is_word_aligned(void const * p_object)659 NRF_STATIC_INLINE bool nrfx_is_word_aligned(void const * p_object)
660 {
661     return ((((uint32_t)p_object) & 0x3u) == 0u);
662 }
663 
nrfx_get_irq_number(void const * p_reg)664 NRF_STATIC_INLINE IRQn_Type nrfx_get_irq_number(void const * p_reg)
665 {
666     return (IRQn_Type)NRFX_IRQ_NUMBER_GET(p_reg);
667 }
668 
nrfx_bitpos_to_event(uint32_t bit)669 NRF_STATIC_INLINE uint32_t nrfx_bitpos_to_event(uint32_t bit)
670 {
671     static const uint32_t event_reg_offset = 0x100u;
672     return event_reg_offset + (bit * sizeof(uint32_t));
673 }
674 
nrfx_event_to_bitpos(uint32_t event)675 NRF_STATIC_INLINE uint32_t nrfx_event_to_bitpos(uint32_t event)
676 {
677     static const uint32_t event_reg_offset = 0x100u;
678     return (event - event_reg_offset) / sizeof(uint32_t);
679 }
680 
681 #endif // NRF_DECLARE_ONLY
682 
683 /** @} */
684 
685 #ifdef __cplusplus
686 }
687 #endif
688 
689 #endif // NRFX_COMMON_H__
690