1 /*
2  * Copyright (c) 2017 - 2018, Nordic Semiconductor ASA
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions are met:
7  *
8  * 1. Redistributions of source code must retain the above copyright notice, this
9  *    list of conditions and the following disclaimer.
10  *
11  * 2. Redistributions in binary form must reproduce the above copyright
12  *    notice, this list of conditions and the following disclaimer in the
13  *    documentation and/or other materials provided with the distribution.
14  *
15  * 3. Neither the name of the copyright holder nor the names of its
16  *    contributors may be used to endorse or promote products derived from this
17  *    software without specific prior written permission.
18  *
19  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
20  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
23  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29  * POSSIBILITY OF SUCH DAMAGE.
30  */
31 
32 #ifndef NRFX_COMMON_H__
33 #define NRFX_COMMON_H__
34 
35 #include <stdint.h>
36 #include <stddef.h>
37 #include <stdbool.h>
38 
39 #include <nrf.h>
40 #include <nrf_peripherals.h>
41 
42 #ifdef __cplusplus
43 extern "C" {
44 #endif
45 
46 /**
47  * @defgroup nrfx_common Common module
48  * @{
49  * @ingroup nrfx
50  * @brief Common module.
51  */
52 
53 /**
54  * @brief Macro for checking if the specified identifier is defined and it has
55  *        a non-zero value.
56  *
57  * Normally, preprocessors treat all undefined identifiers as having the value
58  * zero. However, some tools, like static code analyzers, may issue a warning
59  * when such identifier is evaluated. This macro gives the possibility to suppress
60  * such warnings only in places where this macro is used for evaluation, not in
61  * the whole analyzed code.
62  */
63 #define NRFX_CHECK(module_enabled)  (module_enabled)
64 
65 /**
66  * @brief Macro for concatenating two tokens in macro expansion.
67  *
68  * @note This macro is expanded in two steps so that tokens given as macros
69  *       themselves are fully expanded before they are merged.
70  *
71  * @param p1  First token.
72  * @param p2  Second token.
73  *
74  * @return The two tokens merged into one, unless they cannot together form
75  *         a valid token (in such case, the preprocessor issues a warning and
76  *         does not perform the concatenation).
77  *
78  * @sa NRFX_CONCAT_3
79  */
80 #define NRFX_CONCAT_2(p1, p2)       NRFX_CONCAT_2_(p1, p2)
81 /**
82  * @brief Internal macro used by @ref NRFX_CONCAT_2 to perform the expansion
83  *        in two steps.
84  */
85 #define NRFX_CONCAT_2_(p1, p2)      p1 ## p2
86 
87 /**
88  * @brief Macro for concatenating three tokens in macro expansion.
89  *
90  * @note This macro is expanded in two steps so that tokens given as macros
91  *       themselves are fully expanded before they are merged.
92  *
93  * @param p1  First token.
94  * @param p2  Second token.
95  * @param p3  Third token.
96  *
97  * @return The three tokens merged into one, unless they cannot together form
98  *         a valid token (in such case, the preprocessor issues a warning and
99  *         does not perform the concatenation).
100  *
101  * @sa NRFX_CONCAT_2
102  */
103 #define NRFX_CONCAT_3(p1, p2, p3)   NRFX_CONCAT_3_(p1, p2, p3)
104 /**
105  * @brief Internal macro used by @ref NRFX_CONCAT_3 to perform the expansion
106  *        in two steps.
107  */
108 #define NRFX_CONCAT_3_(p1, p2, p3)  p1 ## p2 ## p3
109 
110 /**@brief Macro for performing rounded integer division (as opposed to
111  *        truncating the result).
112  *
113  * @param a  Numerator.
114  * @param b  Denominator.
115  *
116  * @return Rounded (integer) result of dividing @c a by @c b.
117  */
118 #define NRFX_ROUNDED_DIV(a, b)  (((a) + ((b) / 2)) / (b))
119 
120 /**@brief Macro for checking if given lengths of EasyDMA transfers do not exceed
121  *        the limit of the specified peripheral.
122  *
123  * @param peripheral Peripheral to check the lengths against.
124  * @param length1    First length to be checked.
125  * @param length2    Second length to be checked (pass 0 if not needed).
126  *
127  * @return
128  */
129 #define NRFX_EASYDMA_LENGTH_VALIDATE(peripheral, length1, length2)            \
130     (((length1) < (1U << NRFX_CONCAT_2(peripheral, _EASYDMA_MAXCNT_SIZE))) && \
131      ((length2) < (1U << NRFX_CONCAT_2(peripheral, _EASYDMA_MAXCNT_SIZE))))
132 
133 /**@brief Macro for waiting until condition is met.
134  *
135  * @param[in]  condition Condition to meet.
136  * @param[in]  attempts  Maximum number of condition checks. Must not be 0.
137  * @param[in]  delay_us  Delay between consecutive checks, in microseconds.
138  * @param[out] result    Boolean variable to store the result of the wait process.
139  *                       Set to true if the condition is met or false otherwise.
140  */
141 #define NRFX_WAIT_FOR(condition, attempts, delay_us, result) \
142 do {                                                         \
143     result =  false;                                         \
144     uint32_t remaining_attempts = (attempts);                \
145     do {                                                     \
146            if (condition)                                    \
147            {                                                 \
148                result =  true;                               \
149                break;                                        \
150            }                                                 \
151            NRFX_DELAY_US(delay_us);                          \
152     } while (--remaining_attempts);                          \
153 } while(0)
154 
155 /**
156  * @brief Macro for getting the interrupt number assigned to a specific
157  *        peripheral.
158  *
159  * In Nordic SoCs the IRQ number assigned to a peripheral is equal to the ID
160  * of this peripheral, and there is a direct relationship between this ID and
161  * the peripheral base address, i.e. the address of a fixed block of 0x1000
162  * bytes of address space assigned to this peripheral.
163  * See the chapter "Peripheral interface" (sections "Peripheral ID" and
164  * "Interrupts") in the product specification of a given SoC.
165  *
166  * @param[in] base_addr  Peripheral base address or pointer.
167  *
168  * @return Interrupt number associated with the specified peripheral.
169  */
170 #define NRFX_IRQ_NUMBER_GET(base_addr)  (uint8_t)((uint32_t)(base_addr) >> 12)
171 
172 /**
173  * @brief IRQ handler type.
174  */
175 typedef void (* nrfx_irq_handler_t)(void);
176 
177 /**
178  * @brief Driver state.
179  */
180 typedef enum
181 {
182     NRFX_DRV_STATE_UNINITIALIZED, ///< Uninitialized.
183     NRFX_DRV_STATE_INITIALIZED,   ///< Initialized but powered off.
184     NRFX_DRV_STATE_POWERED_ON,    ///< Initialized and powered on.
185 } nrfx_drv_state_t;
186 
187 
188 /**
189  * @brief Function for checking if an object is placed in the Data RAM region.
190  *
191  * Several peripherals (the ones using EasyDMA) require the transfer buffers
192  * to be placed in the Data RAM region. This function can be used to check if
193  * this condition is met.
194  *
195  * @param[in] p_object  Pointer to an object whose location is to be checked.
196  *
197  * @retval true  If the pointed object is located in the Data RAM region.
198  * @retval false Otherwise.
199  */
200 __STATIC_INLINE bool nrfx_is_in_ram(void const * p_object);
201 
202 /**
203  * @brief Function for getting the interrupt number for a specific peripheral.
204  *
205  * @param[in] p_reg  Peripheral base pointer.
206  *
207  * @return Interrupt number associated with the pointed peripheral.
208  */
209 __STATIC_INLINE IRQn_Type nrfx_get_irq_number(void const * p_reg);
210 
211 /**
212  * @brief Function for converting an INTEN register bit position to the
213  *        corresponding event identifier.
214  *
215  * The event identifier is the offset between the event register address and
216  * the peripheral base address, and is equal (thus, can be directly cast) to
217  * the corresponding value of the enumerated type from HAL (nrf_*_event_t).
218 
219  * @param bit  INTEN register bit position.
220  *
221  * @return Event identifier.
222  *
223  * @sa nrfx_event_to_bitpos
224  */
225 __STATIC_INLINE uint32_t nrfx_bitpos_to_event(uint32_t bit);
226 
227 /**
228  * @brief Function for converting an event identifier to the corresponding
229  *        INTEN register bit position.
230  *
231  * The event identifier is the offset between the event register address and
232  * the peripheral base address, and is equal (thus, can be directly cast) to
233  * the corresponding value of the enumerated type from HAL (nrf_*_event_t).
234  *
235  * @param event  Event identifier.
236  *
237  * @return INTEN register bit position.
238  *
239  * @sa nrfx_bitpos_to_event
240  */
241 __STATIC_INLINE uint32_t nrfx_event_to_bitpos(uint32_t event);
242 
243 
244 #ifndef SUPPRESS_INLINE_IMPLEMENTATION
245 
nrfx_is_in_ram(void const * p_object)246 __STATIC_INLINE bool nrfx_is_in_ram(void const * p_object)
247 {
248     return ((((uint32_t)p_object) & 0xE0000000u) == 0x20000000u);
249 }
250 
nrfx_get_irq_number(void const * p_reg)251 __STATIC_INLINE IRQn_Type nrfx_get_irq_number(void const * p_reg)
252 {
253     return (IRQn_Type)NRFX_IRQ_NUMBER_GET(p_reg);
254 }
255 
nrfx_bitpos_to_event(uint32_t bit)256 __STATIC_INLINE uint32_t nrfx_bitpos_to_event(uint32_t bit)
257 {
258     static const uint32_t event_reg_offset = 0x100u;
259     return event_reg_offset + (bit * sizeof(uint32_t));
260 }
261 
nrfx_event_to_bitpos(uint32_t event)262 __STATIC_INLINE uint32_t nrfx_event_to_bitpos(uint32_t event)
263 {
264     static const uint32_t event_reg_offset = 0x100u;
265     return (event - event_reg_offset) / sizeof(uint32_t);
266 }
267 
268 #endif
269 
270 /** @} */
271 
272 #ifdef __cplusplus
273 }
274 #endif
275 
276 #endif // NRFX_COMMON_H__
277