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