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 checking if a given value is in a given range.
411  *
412  * @note @p val is evaluated twice.
413  *
414  * @param[in] val A value to be checked.
415  * @param[in] min The lower bound (inclusive).
416  * @param[in] max The upper bound (inclusive).
417  *
418  * @retval true  The value is in the given range.
419  * @retval false The value is out of the given range.
420  */
421 #define NRFX_IN_RANGE(val, min, max) ((val) >= (min) && (val) <= (max))
422 
423 /**
424  * @brief Macro for performing rounded integer division (as opposed to
425  *        truncating the result).
426  *
427  * @param[in] a Numerator.
428  * @param[in] b Denominator.
429  *
430  * @return Rounded (integer) result of dividing @c a by @c b.
431  */
432 #define NRFX_ROUNDED_DIV(a, b) \
433     ((((a) < 0) ^ ((b) < 0)) ? (((a) - (b) / 2) / (b)) : (((a) + (b) / 2) / (b)))
434 
435 /**
436  * @brief Macro for performing integer division, making sure the result is rounded up.
437  *
438  * @details A typical use case for this macro is to compute the number of objects
439  *          with size @c b required to hold @c a number of bytes.
440  *
441  * @param[in] a Numerator.
442  * @param[in] b Denominator.
443  *
444  * @return Integer result of dividing @c a by @c b, rounded up.
445  */
446 #define NRFX_CEIL_DIV(a, b) ((((a) - 1) / (b)) + 1)
447 
448 /**
449  * @brief Macro for getting the number of elements in an array.
450  *
451  * @param[in] array Name of the array.
452  *
453  * @return Array element count.
454  */
455 #define NRFX_ARRAY_SIZE(array) (sizeof(array) / sizeof((array)[0]))
456 
457 /**
458  * @brief Macro for getting the offset (in bytes) from the beginning of a structure
459  *        of the specified type to its specified member.
460  *
461  * @param[in] type   Structure type.
462  * @param[in] member Structure member whose offset is searched for.
463  *
464  * @return Member offset in bytes.
465  */
466 #define NRFX_OFFSETOF(type, member) ((size_t) & (((type *)0)->member))
467 
468 /**
469  * @brief Macro for checking whether given number is power of 2.
470  *
471  * @param[in] val Tested value.
472  *
473  * @retval true  The value is power of 2.
474  * @retval false The value is not power of 2.
475  */
476 #define NRFX_IS_POWER_OF_TWO(val) (((val) != 0) && ((val) & ((val) - 1)) == 0)
477 
478 /**
479  * @brief Macro for checking whether a given number is even.
480  *
481  * @param[in] val Tested value.
482  *
483  * @retval true  The value is even.
484  * @retval false The value is odd.
485  */
486 #define NRFX_IS_EVEN(val) (((val) % 2)  == 0)
487 
488 /**
489  * @brief Macro for checking if given lengths of EasyDMA transfers do not exceed
490  *        the limit of the specified peripheral.
491  *
492  * @param[in] peripheral Peripheral to check the lengths against.
493  * @param[in] length1    First length to be checked.
494  * @param[in] length2    Second length to be checked (pass 0 if not needed).
495  *
496  * @retval true  The length of buffers does not exceed the limit of the specified peripheral.
497  * @retval false The length of buffers exceeds the limit of the specified peripheral.
498  */
499 #define NRFX_EASYDMA_LENGTH_VALIDATE(peripheral, length1, length2)            \
500     (((length1) < (1U << NRFX_CONCAT_2(peripheral, _EASYDMA_MAXCNT_SIZE))) && \
501      ((length2) < (1U << NRFX_CONCAT_2(peripheral, _EASYDMA_MAXCNT_SIZE))))
502 
503 /**
504  * @brief Macro for waiting until condition is met.
505  *
506  * @param[in]  condition Condition to meet.
507  * @param[in]  attempts  Maximum number of condition checks. Must not be 0.
508  * @param[in]  delay_us  Delay between consecutive checks, in microseconds.
509  * @param[out] result    Boolean variable to store the result of the wait process.
510  *                       Set to true if the condition is met or false otherwise.
511  */
512 #define NRFX_WAIT_FOR(condition, attempts, delay_us, result) \
513 do {                                                         \
514     result =  false;                                         \
515     uint32_t remaining_attempts = (attempts);                \
516     do {                                                     \
517            if (condition)                                    \
518            {                                                 \
519                result =  true;                               \
520                break;                                        \
521            }                                                 \
522            NRFX_DELAY_US(delay_us);                          \
523     } while (--remaining_attempts);                          \
524 } while(0)
525 
526 /**
527  * @brief Macro for getting the ID number of the specified peripheral.
528  *
529  * For peripherals in Nordic SoCs, there is a direct relationship between their
530  * ID numbers and their base addresses. See the chapter "Peripheral interface"
531  * (section "Peripheral ID") in the Product Specification.
532  *
533  * @param[in] base_addr Peripheral base address or pointer.
534  *
535  * @return ID number associated with the specified peripheral.
536  */
537 #define NRFX_PERIPHERAL_ID_GET(base_addr) (uint16_t)(((uint32_t)(base_addr) >> 12) & 0x000001FF)
538 
539 /**
540  * @brief Macro for getting the interrupt number assigned to a specific
541  *        peripheral.
542  *
543  * For peripherals in Nordic SoCs, the IRQ number assigned to a peripheral is
544  * equal to its ID number. See the chapter "Peripheral interface" (sections
545  * "Peripheral ID" and "Interrupts") in the Product Specification.
546  *
547  * @param[in] base_addr Peripheral base address or pointer.
548  *
549  * @return Interrupt number associated with the specified peripheral.
550  */
551 #define NRFX_IRQ_NUMBER_GET(base_addr) NRFX_PERIPHERAL_ID_GET(base_addr)
552 
553 /**
554  * @brief Macro for converting frequency in kHz to Hz.
555  *
556  * @param[in] freq Frequency value in kHz.
557  *
558  * @return Number of Hz in @p freq kHz.
559  */
560 #define NRFX_KHZ_TO_HZ(freq) ((freq) * 1000)
561 
562 /**
563  * @brief Macro for converting frequency in MHz to Hz.
564  *
565  * @param[in] freq Frequency value in MHz.
566  *
567  * @return Number of Hz in @p freq MHz.
568  */
569 #define NRFX_MHZ_TO_HZ(freq) ((freq) * 1000 * 1000)
570 
571 /** @brief IRQ handler type. */
572 typedef void (* nrfx_irq_handler_t)(void);
573 
574 /** @brief Driver state. */
575 typedef enum
576 {
577     NRFX_DRV_STATE_UNINITIALIZED, ///< Uninitialized.
578     NRFX_DRV_STATE_INITIALIZED,   ///< Initialized but powered off.
579     NRFX_DRV_STATE_POWERED_ON,    ///< Initialized and powered on.
580 } nrfx_drv_state_t;
581 
582 /**
583  * @brief Function for checking if an object is placed in the Data RAM region.
584  *
585  * Several peripherals (the ones using EasyDMA) require the transfer buffers
586  * to be placed in the Data RAM region. 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 located in the Data RAM region.
592  * @retval false The pointed object is not located in the Data RAM region.
593  */
594 NRF_STATIC_INLINE bool nrfx_is_in_ram(void const * p_object);
595 
596 /**
597  * @brief Function for checking if an object is aligned to a 32-bit word
598  *
599  * Several peripherals (the ones using EasyDMA) require the transfer buffers
600  * to be aligned to a 32-bit word. This function can be used to check if
601  * this condition is met.
602  *
603  * @param[in] p_object  Pointer to an object whose location is to be checked.
604  *
605  * @retval true  The pointed object is aligned to a 32-bit word.
606  * @retval false The pointed object is not aligned to a 32-bit word.
607  */
608 NRF_STATIC_INLINE bool nrfx_is_word_aligned(void const * p_object);
609 
610 /**
611  * @brief Function for getting the interrupt number for the specified peripheral.
612  *
613  * @param[in] p_reg Peripheral base pointer.
614  *
615  * @return Interrupt number associated with the pointed peripheral.
616  */
617 NRF_STATIC_INLINE IRQn_Type nrfx_get_irq_number(void const * p_reg);
618 
619 /**
620  * @brief Function for converting an INTEN register bit position to the
621  *        corresponding event identifier.
622  *
623  * The event identifier is the offset between the event register address and
624  * the peripheral base address, and is equal (thus, can be directly cast) to
625  * the corresponding value of the enumerated type from HAL (nrf_*_event_t).
626  *
627  * @param[in] bit INTEN register bit position.
628  *
629  * @return Event identifier.
630  *
631  * @sa nrfx_event_to_bitpos
632  */
633 NRF_STATIC_INLINE uint32_t nrfx_bitpos_to_event(uint32_t bit);
634 
635 /**
636  * @brief Function for converting an event identifier to the corresponding
637  *        INTEN register bit position.
638  *
639  * The event identifier is the offset between the event register address and
640  * the peripheral base address, and is equal (thus, can be directly cast) to
641  * the corresponding value of the enumerated type from HAL (nrf_*_event_t).
642  *
643  * @param[in] event Event identifier.
644  *
645  * @return INTEN register bit position.
646  *
647  * @sa nrfx_bitpos_to_event
648  */
649 NRF_STATIC_INLINE uint32_t nrfx_event_to_bitpos(uint32_t event);
650 
651 #ifndef NRF_DECLARE_ONLY
652 
nrfx_is_in_ram(void const * p_object)653 NRF_STATIC_INLINE bool nrfx_is_in_ram(void const * p_object)
654 {
655     return ((((uint32_t)p_object) & 0xE0000000u) == 0x20000000u);
656 }
657 
nrfx_is_word_aligned(void const * p_object)658 NRF_STATIC_INLINE bool nrfx_is_word_aligned(void const * p_object)
659 {
660     return ((((uint32_t)p_object) & 0x3u) == 0u);
661 }
662 
nrfx_get_irq_number(void const * p_reg)663 NRF_STATIC_INLINE IRQn_Type nrfx_get_irq_number(void const * p_reg)
664 {
665     return (IRQn_Type)NRFX_IRQ_NUMBER_GET(p_reg);
666 }
667 
nrfx_bitpos_to_event(uint32_t bit)668 NRF_STATIC_INLINE uint32_t nrfx_bitpos_to_event(uint32_t bit)
669 {
670     static const uint32_t event_reg_offset = 0x100u;
671     return event_reg_offset + (bit * sizeof(uint32_t));
672 }
673 
nrfx_event_to_bitpos(uint32_t event)674 NRF_STATIC_INLINE uint32_t nrfx_event_to_bitpos(uint32_t event)
675 {
676     static const uint32_t event_reg_offset = 0x100u;
677     return (event - event_reg_offset) / sizeof(uint32_t);
678 }
679 
680 #endif // NRF_DECLARE_ONLY
681 
682 /** @} */
683 
684 #ifdef __cplusplus
685 }
686 #endif
687 
688 #endif // NRFX_COMMON_H__
689