1 /*
2  * Copyright (c) 2015 - 2025, 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_GPIOTE_H__
35 #define NRFX_GPIOTE_H__
36 
37 #include <nrfx.h>
38 #include <haly/nrfy_gpiote.h>
39 #include <haly/nrfy_gpio.h>
40 
41 #ifdef __cplusplus
42 extern "C" {
43 #endif
44 
45 /* On devices with single instance (with no ID) use instance 0. */
46 #if defined(NRF_GPIOTE) && defined(NRFX_GPIOTE_ENABLED) && !defined(NRFX_GPIOTE0_ENABLED)
47 #define NRFX_GPIOTE0_ENABLED 1
48 #endif
49 
50 /**
51  * @defgroup nrfx_gpiote GPIOTE driver
52  * @{
53  * @ingroup nrf_gpiote
54  * @brief   GPIO Task Event (GPIOTE) peripheral driver.
55  */
56 
57 /** @brief Data structure of the GPIO tasks and events (GPIOTE) driver instance. */
58 typedef struct
59 {
60     NRF_GPIOTE_Type * p_reg;        ///< Pointer to a structure containing GPIOTE registers.
61     uint8_t           drv_inst_idx; ///< Index of the driver instance. For internal use only.
62 } nrfx_gpiote_t;
63 
64 #ifndef __NRFX_DOXYGEN__
65 enum {
66     /* List all enabled driver instances (in the format NRFX_\<instance_name\>_INST_IDX). */
67     NRFX_INSTANCE_ENUM_LIST(GPIOTE)
68     NRFX_GPIOTE_ENABLED_COUNT
69 };
70 #endif
71 
72 /** @brief Macro for creating an instance of the GPIOTE driver. */
73 #define NRFX_GPIOTE_INSTANCE(id)                             \
74 {                                                            \
75     .p_reg        = NRFX_CONCAT(NRF_, GPIOTE, id),           \
76     .drv_inst_idx = NRFX_CONCAT(NRFX_GPIOTE, id, _INST_IDX), \
77 }
78 
79 /** @brief Pin. */
80 typedef uint32_t nrfx_gpiote_pin_t;
81 
82 /** @brief Triggering options. */
83 typedef enum
84 {
85     NRFX_GPIOTE_TRIGGER_NONE,                                   ///< No trigger on a pin.
86     NRFX_GPIOTE_TRIGGER_LOTOHI = GPIOTE_CONFIG_POLARITY_LoToHi, ///< Low to high edge trigger.
87     NRFX_GPIOTE_TRIGGER_HITOLO,                                 ///< High to low edge trigger.
88     NRFX_GPIOTE_TRIGGER_TOGGLE,                                 ///< Edge toggle trigger.
89     NRFX_GPIOTE_TRIGGER_LOW,                                    ///< Level low trigger.
90     NRFX_GPIOTE_TRIGGER_HIGH,                                   ///< Level high trigger.
91     NRFX_GPIOTE_TRIGGER_MAX,                                    ///< Triggering options count.
92 } nrfx_gpiote_trigger_t;
93 
94 /**
95  * @brief Pin interrupt handler prototype.
96  *
97  * @param[in] pin       Pin that triggered this event.
98  * @param[in] trigger   Trigger that led to this event.
99  * @param[in] p_context User context.
100  */
101 typedef void (*nrfx_gpiote_interrupt_handler_t)(nrfx_gpiote_pin_t     pin,
102                                                 nrfx_gpiote_trigger_t trigger,
103                                                 void *                p_context);
104 
105 /** @brief Structure for configuring a GPIOTE task. */
106 typedef struct
107 {
108     uint8_t               task_ch;  ///< GPIOTE channel to be used.
109                                     /**< Set to value allocated using
110                                      *   @ref nrfx_gpiote_channel_alloc. It is a user
111                                      *   responsibility to free the channel. */
112     nrf_gpiote_polarity_t polarity; ///< Task polarity configuration.
113                                     /**< @ref NRF_GPIOTE_POLARITY_NONE is used
114                                      *   to disable previously configured task. */
115     nrf_gpiote_outinit_t  init_val; ///< Initial pin state.
116 } nrfx_gpiote_task_config_t;
117 
118 /** @brief Structure for configuring an output pin. */
119 typedef struct
120 {
121     nrf_gpio_pin_drive_t drive;         ///< Drive configuration.
122     nrf_gpio_pin_input_t input_connect; ///< Input buffer connection.
123     nrf_gpio_pin_pull_t  pull;          ///< Pull configuration.
124                                         /**< Pull setting is used together with
125                                          *   drive configurations D0 and D1. */
126 } nrfx_gpiote_output_config_t;
127 
128 /** @brief Structure for configuring pin interrupt/event. */
129 typedef struct
130 {
131     nrfx_gpiote_trigger_t trigger;      ///< Specify trigger.
132     uint8_t const *       p_in_channel; ///< Pointer to GPIOTE channel for IN event.
133                                         /**< If NULL, the sensing mechanism is used
134                                          *   instead. Note that when channel is provided
135                                          *   only edge triggering can be used. */
136 } nrfx_gpiote_trigger_config_t;
137 
138 /** @brief Structure for configuring a pin interrupt handler. */
139 typedef struct
140 {
141     nrfx_gpiote_interrupt_handler_t handler;   ///< User handler.
142     void *                          p_context; ///< Context passed to the event handler.
143 } nrfx_gpiote_handler_config_t;
144 
145 /** @brief @deprecated Structure for configuring an input pin. */
146 typedef struct
147 {
148     nrf_gpio_pin_pull_t pull; ///< Pull configuration.
149 } nrfx_gpiote_input_config_t;
150 
151 /** @brief Structure for configuring an input pin. */
152 typedef struct
153 {
154     nrf_gpio_pin_pull_t          const * p_pull_config;    ///< Pull configuration. If NULL, the current configuration is untouched.
155     nrfx_gpiote_trigger_config_t const * p_trigger_config; ///< Interrupt/event configuration. If NULL, the current configuration is untouched.
156     nrfx_gpiote_handler_config_t const * p_handler_config; ///< Handler configuration. If NULL it is untouched.
157 } nrfx_gpiote_input_pin_config_t;
158 
159 /** @brief Output pin default configuration. */
160 #define NRFX_GPIOTE_DEFAULT_OUTPUT_CONFIG           \
161 {                                                   \
162     .drive = NRF_GPIO_PIN_S0S1,                     \
163     .input_connect = NRF_GPIO_PIN_INPUT_DISCONNECT, \
164     .pull = NRF_GPIO_PIN_NOPULL                     \
165 }
166 
167 /** @brief Bitfield representing all GPIOTE channels available to the application
168  *         for the specified GPIOTE instance.
169  *
170  * @param[in] idx GPIOTE instance index.
171  */
172 #define NRFX_GPIOTE_APP_CHANNELS_MASK(idx) \
173             (NRFX_BIT_MASK(NRFX_CONCAT_3(GPIOTE, idx, _CH_NUM)) & \
174              ~(NRFX_CONCAT_3(NRFX_GPIOTE, idx, _CHANNELS_USED)))
175 
176 /**
177  * @brief Macro returning GPIOTE interrupt handler.
178  *
179  * @param[in] idx GPIOTE index.
180  *
181  * @return Interrupt handler.
182  */
183 #define NRFX_GPIOTE_INST_HANDLER_GET(idx) NRFX_CONCAT_3(nrfx_gpiote_, idx, _irq_handler)
184 
185 /**
186  * @brief Function for checking if a GPIOTE input pin is set.
187  *
188  * @param[in] pin Pin.
189  *
190  * @retval true  The input pin is set.
191  * @retval false The input pin is not set.
192  */
193 bool nrfx_gpiote_in_is_set(nrfx_gpiote_pin_t pin);
194 
195 #if NRFX_API_VER_AT_LEAST(3, 2, 0) || defined(__NRFX_DOXYGEN__)
196 
197 /** @brief Input pin pull default configuration. */
198 #define NRFX_GPIOTE_DEFAULT_PULL_CONFIG NRF_GPIO_PIN_NOPULL
199 
200 /**
201  * @brief Function for initializing the GPIOTE driver instance.
202  *
203  * @param[in] p_instance         Pointer to the driver instance structure.
204  * @param[in] interrupt_priority Interrupt priority.
205  *
206  * @retval NRFX_SUCCESS             Initialization was successful.
207  * @retval NRFX_ERROR_ALREADY       The driver is already initialized.
208  * @retval NRFX_ERROR_INVALID_STATE The driver is already initialized.
209  *                                  Deprecated - use @ref NRFX_ERROR_ALREADY instead.
210  */
211 nrfx_err_t nrfx_gpiote_init(nrfx_gpiote_t const * p_instance, uint8_t interrupt_priority);
212 
213 /**
214  * @brief Function for checking if the GPIOTE driver instance is initialized.
215  *
216  * @details The GPIOTE driver instance is a shared module. Therefore, check if
217  * the module is already initialized and skip initialization if it is.
218  *
219  * @param[in] p_instance Pointer to the driver instance structure.
220  *
221  * @retval true  The module is already initialized.
222  * @retval false The module is not initialized.
223  */
224 bool nrfx_gpiote_init_check(nrfx_gpiote_t const * p_instance);
225 
226 /**
227  * @brief Function for uninitializing the GPIOTE driver instance.
228  *
229  * @param[in] p_instance Pointer to the driver instance structure.
230  */
231 void nrfx_gpiote_uninit(nrfx_gpiote_t const * p_instance);
232 
233 /**
234  * @brief Function for allocating a GPIOTE channel.
235  * @details This function allocates the first unused GPIOTE channel from
236  *          pool defined in @ref NRFX_GPIOTE_APP_CHANNELS_MASK.
237  *
238  * @note Function is thread safe as it uses @ref nrfx_flag32_alloc.
239  * @note Routines that allocate and free the GPIOTE channels are independent
240  *       from the rest of the driver. In particular, the driver does not need
241  *       to be initialized when this function is called.
242  *
243  * @param[in]  p_instance Pointer to the driver instance structure.
244  * @param[out] p_channel  Pointer to the GPIOTE channel that has been allocated.
245  *
246  * @retval NRFX_SUCCESS      The channel was successfully allocated.
247  * @retval NRFX_ERROR_NO_MEM There is no available channel to be used.
248  */
249 nrfx_err_t nrfx_gpiote_channel_alloc(nrfx_gpiote_t const * p_instance, uint8_t * p_channel);
250 
251 /**
252  * @brief Function for freeing a GPIOTE channel.
253  * @details This function frees a GPIOTE channel that was allocated using
254  *          @ref nrfx_gpiote_channel_alloc.
255  *
256  * @note Function is thread safe as it uses @ref nrfx_flag32_free.
257  * @note Routines that allocate and free the GPIOTE channels are independent
258  *       from the rest of the driver. In particular, the driver does not need
259  *       to be initialized when this function is called.
260  *
261  * @param[in] p_instance Pointer to the driver instance structure.
262  * @param[in] channel    GPIOTE channel to be freed.
263  *
264  * @retval NRFX_SUCCESS             The channel was successfully freed.
265  * @retval NRFX_ERROR_INVALID_PARAM The channel is not user-configurable.
266  */
267 nrfx_err_t nrfx_gpiote_channel_free(nrfx_gpiote_t const * p_instance, uint8_t channel);
268 
269 /**
270  * @brief Function for configuring the specified input pin and input event/interrupt.
271  *
272  * Prior to calling this function pin can be uninitialized or configured as input or
273  * output. However, following transitions and configurations are invalid and result
274  * in error returned by the function:
275  *
276  * - Setting level trigger (e.g. @ref NRFX_GPIOTE_TRIGGER_HIGH) and using GPIOTE
277  *   channel for the same pin.
278  * - Reconfiguring pin to input (@p p_config->p_pull_config not NULL) when pin was configured
279  *   to use GPIOTE task. Prior to that, task must be disabled by configuring it with
280  *   polarity set to @ref NRF_GPIOTE_POLARITY_NONE.
281  * - Configuring trigger using GPIOTE channel for pin previously configured as output
282  *   pin. Only sensing can be used for an output pin.
283  *
284  * Function can be used to configure trigger and handler for sensing input changes
285  * on an output pin. In that case, prior to that output pin must be configured with
286  * input buffer connected. In that case @p p_config->p_pull_config is NULL to avoid
287  * reconfiguration of the pin.
288  *
289  * @param[in] p_instance Pointer to the driver instance structure.
290  * @param[in] pin        Absolute pin number.
291  * @param[in] p_config   Pointer to the structure with input pin configuration.
292  *
293  * @retval NRFX_SUCCESS             Configuration was successful.
294  * @retval NRFX_ERROR_INVALID_PARAM Invalid configuration.
295  */
296 nrfx_err_t nrfx_gpiote_input_configure(nrfx_gpiote_t const *                  p_instance,
297                                        nrfx_gpiote_pin_t                      pin,
298                                        nrfx_gpiote_input_pin_config_t const * p_config);
299 
300 /**
301  * @brief Function for configuring the specified output pin to be used by the driver.
302  *
303  * Prior to calling this function pin can be uninitialized or configured as input or
304  * output. However, following transitions and configurations are invalid and result
305  * in error returned by the function:
306  *
307  * - Reconfiguring pin to output when pin was configured as input with trigger using
308  *   GPIOTE channel. Prior to that, trigger must be disabled by configuring it as
309  *   @ref NRFX_GPIOTE_TRIGGER_NONE.
310  * - Configuring pin as output without input buffer connected when prior to that
311  *   trigger was configured. In that case input buffer must be connected.
312  * - Configuring GPIOTE task for pin which was previously configured as input. Before
313  *   using GPIOTE task pin must be configured as output by providing @p p_config.
314  *
315  * @param[in] p_instance    Pointer to the driver instance structure.
316  * @param[in] pin           Absolute pin number.
317  * @param[in] p_config      Pin configuration. If NULL pin configuration is not applied.
318  * @param[in] p_task_config GPIOTE task configuration. If NULL task is not used.
319  *
320  * @retval NRFX_SUCCESS             Configuration was successful.
321  * @retval NRFX_ERROR_INVALID_PARAM Invalid configuration.
322  */
323 nrfx_err_t nrfx_gpiote_output_configure(nrfx_gpiote_t const *               p_instance,
324                                         nrfx_gpiote_pin_t                   pin,
325                                         nrfx_gpiote_output_config_t const * p_config,
326                                         nrfx_gpiote_task_config_t const *   p_task_config);
327 
328 /**
329  * @brief Function for deinitializing the specified pin.
330  *
331  * Specified pin and associated GPIOTE channel are restored to the default configuration.
332  *
333  * @warning GPIOTE channel used by the pin is not freed.
334  *
335  * @param[in] p_instance Pointer to the driver instance structure.
336  * @param[in] pin        Absolute pin number.
337  *
338  * @retval NRFX_SUCCESS             Uninitialization was successful.
339  * @retval NRFX_ERROR_INVALID_PARAM Pin not used by the driver.
340  */
341 nrfx_err_t nrfx_gpiote_pin_uninit(nrfx_gpiote_t const * p_instance, nrfx_gpiote_pin_t pin);
342 
343 /**
344  * @brief Function for enabling trigger for the given pin.
345  *
346  * When GPIOTE event is used trigger can be enabled without enabling interrupt,
347  * e.g. for PPI.
348  *
349  * @param[in] p_instance Pointer to the driver instance structure.
350  * @param[in] pin        Absolute pin number.
351  * @param[in] int_enable True to enable the interrupt. Must be true when sensing is used.
352  */
353 void nrfx_gpiote_trigger_enable(nrfx_gpiote_t const * p_instance,
354                                 nrfx_gpiote_pin_t     pin,
355                                 bool                  int_enable);
356 
357 /**
358  * @brief Function for disabling trigger for the given pin.
359  *
360  * @param[in] p_instance Pointer to the driver instance structure.
361  * @param[in] pin        Absolute pin number.
362  */
363 void nrfx_gpiote_trigger_disable(nrfx_gpiote_t const * p_instance, nrfx_gpiote_pin_t pin);
364 
365 /**
366  * @brief Set global callback called for each event.
367  *
368  * @param[in] p_instance Pointer to the driver instance structure.
369  * @param[in] handler    Global handler. Can be NULL.
370  * @param[in] p_context  Context passed to the handler.
371  */
372 void nrfx_gpiote_global_callback_set(nrfx_gpiote_t const *           p_instance,
373                                      nrfx_gpiote_interrupt_handler_t handler,
374                                      void *                          p_context);
375 
376 /**
377  * @brief Function for retrieving Task/Event channel index associated with the given pin.
378  *
379  * @param[in]  p_instance Pointer to the driver instance structure.
380  * @param[in]  pin        Absolute pin number.
381  * @param[out] p_channel  Location to write the channel index.
382  *
383  * @retval NRFX_SUCCESS             Channel successfully written.
384  * @retval NRFX_ERROR_INVALID_PARAM Pin is not configured or not using Task or Event.
385  */
386 nrfx_err_t nrfx_gpiote_channel_get(nrfx_gpiote_t const * p_instance,
387                                    nrfx_gpiote_pin_t     pin,
388                                    uint8_t *             p_channel);
389 
390 /**
391  * @brief Function for retrieving number of channels for specified GPIOTE instance.
392  *
393  * @param[in] p_instance Pointer to the driver instance structure.
394  *
395  * @return Number of channels for specified GPIOTE instance.
396  */
397 uint32_t nrfx_gpiote_channels_number_get(nrfx_gpiote_t const * p_instance);
398 
399 /**
400  * @brief Function for setting a GPIOTE output pin.
401  *
402  * @param[in] p_instance Pointer to the driver instance structure.
403  * @param[in] pin        Pin number.
404  */
405 void nrfx_gpiote_out_set(nrfx_gpiote_t const * p_instance, nrfx_gpiote_pin_t pin);
406 
407 /**
408  * @brief Function for clearing a GPIOTE output pin.
409  *
410  * @param[in] p_instance Pointer to the driver instance structure.
411  * @param[in] pin        Pin number.
412  */
413 void nrfx_gpiote_out_clear(nrfx_gpiote_t const * p_instance, nrfx_gpiote_pin_t pin);
414 
415 /**
416  * @brief Function for toggling a GPIOTE output pin.
417  *
418  * @param[in] p_instance Pointer to the driver instance structure.
419  * @param[in] pin        Pin number.
420  */
421 void nrfx_gpiote_out_toggle(nrfx_gpiote_t const * p_instance, nrfx_gpiote_pin_t pin);
422 
423 /**
424  * @brief Function for enabling a GPIOTE output pin task.
425  *
426  * @param[in] p_instance Pointer to the driver instance structure.
427  * @param[in] pin        Pin number.
428  */
429 void nrfx_gpiote_out_task_enable(nrfx_gpiote_t const * p_instance, nrfx_gpiote_pin_t pin);
430 
431 /**
432  * @brief Function for disabling a GPIOTE output pin task.
433  *
434  * @param[in] p_instance Pointer to the driver instance structure.
435  * @param[in] pin        Pin number.
436  */
437 void nrfx_gpiote_out_task_disable(nrfx_gpiote_t const * p_instance, nrfx_gpiote_pin_t pin);
438 
439 /**
440  * @brief Function for getting the OUT task for the specified output pin.
441  *
442  * @details The returned task identifier can be used within @ref nrf_gpiote_hal,
443  *          for example, to configure a DPPI channel.
444  *
445  * @param[in] p_instance Pointer to the driver instance structure.
446  * @param[in] pin        Pin number.
447  *
448  * @return OUT task associated with the specified output pin.
449  */
450 nrf_gpiote_task_t nrfx_gpiote_out_task_get(nrfx_gpiote_t const * p_instance, nrfx_gpiote_pin_t pin);
451 
452 /**
453  * @brief Function for getting the address of the OUT task for the specified output pin.
454  *
455  * @param[in] p_instance Pointer to the driver instance structure.
456  * @param[in] pin        Pin number.
457  *
458  * @return Address of OUT task.
459  */
460 uint32_t nrfx_gpiote_out_task_address_get(nrfx_gpiote_t const * p_instance, nrfx_gpiote_pin_t pin);
461 
462 #if defined(GPIOTE_FEATURE_SET_PRESENT) || defined(__NRFX_DOXYGEN__)
463 /**
464  * @brief Function for getting the SET task for the specified output pin.
465  *
466  * @details The returned task identifier can be used within @ref nrf_gpiote_hal,
467  *          for example, to configure a DPPI channel.
468  *
469  * @param[in] p_instance Pointer to the driver instance structure.
470  * @param[in] pin        Pin number.
471  *
472  * @return SET task associated with the specified output pin.
473  */
474 nrf_gpiote_task_t nrfx_gpiote_set_task_get(nrfx_gpiote_t const * p_instance, nrfx_gpiote_pin_t pin);
475 
476 /**
477  * @brief Function for getting the address of the SET task for the specified output pin.
478  *
479  * @param[in] p_instance Pointer to the driver instance structure.
480  * @param[in] pin        Pin number.
481  *
482  * @return Address of SET task.
483  */
484 uint32_t nrfx_gpiote_set_task_address_get(nrfx_gpiote_t const * p_instance, nrfx_gpiote_pin_t pin);
485 #endif // defined(GPIOTE_FEATURE_SET_PRESENT) || defined(__NRFX_DOXYGEN__)
486 
487 #if defined(GPIOTE_FEATURE_CLR_PRESENT) || defined(__NRFX_DOXYGEN__)
488 /**
489  * @brief Function for getting the CLR task for the specified output pin.
490  *
491  * @details The returned task identifier can be used within @ref nrf_gpiote_hal,
492  *          for example, to configure a DPPI channel.
493  *
494  * @param[in] p_instance Pointer to the driver instance structure.
495  * @param[in] pin        Pin number.
496  *
497  * @return CLR task associated with the specified output pin.
498  */
499 nrf_gpiote_task_t nrfx_gpiote_clr_task_get(nrfx_gpiote_t const * p_instance, nrfx_gpiote_pin_t pin);
500 
501 /**
502  * @brief Function for getting the address of the SET task for the specified output pin.
503  *
504  * @param[in] p_instance Pointer to the driver instance structure.
505  * @param[in] pin        Pin number.
506  *
507  * @return Address of CLR task.
508  */
509 uint32_t nrfx_gpiote_clr_task_address_get(nrfx_gpiote_t const * p_instance, nrfx_gpiote_pin_t pin);
510 #endif // defined(GPIOTE_FEATURE_CLR_PRESENT) || defined(__NRFX_DOXYGEN__)
511 
512 /**
513  * @brief Function for getting the GPIOTE event for the specified input pin.
514  *
515  * @details The returned event identifier can be used within @ref nrf_gpiote_hal,
516  *          for example, to configure a DPPI channel.
517  *          If the pin is configured to use low-accuracy mode, the PORT event
518  *          is returned.
519  *
520  * @param[in] p_instance Pointer to the driver instance structure.
521  * @param[in] pin        Pin number.
522  *
523  * @return Event associated with the specified input pin.
524  */
525 nrf_gpiote_event_t nrfx_gpiote_in_event_get(nrfx_gpiote_t const * p_instance,
526                                             nrfx_gpiote_pin_t     pin);
527 
528 /**
529  * @brief Function for getting the address of a GPIOTE input pin event.
530  * @details If the pin is configured to use low-accuracy mode, the address of the PORT event is returned.
531  *
532  * @param[in] p_instance Pointer to the driver instance structure.
533  * @param[in] pin        Pin number.
534  *
535  * @return Address of the specified input pin event.
536  */
537 uint32_t nrfx_gpiote_in_event_address_get(nrfx_gpiote_t const * p_instance, nrfx_gpiote_pin_t pin);
538 
539 /**
540  * @brief Function for forcing a specific state on the pin configured as task.
541  *
542  * @param[in] p_instance Pointer to the driver instance structure.
543  * @param[in] pin        Pin number.
544  * @param[in] state      Pin state.
545  */
546 void nrfx_gpiote_out_task_force(nrfx_gpiote_t const * p_instance,
547                                 nrfx_gpiote_pin_t     pin,
548                                 uint8_t               state);
549 
550 /**
551  * @brief Function for triggering the task OUT manually.
552  *
553  * @param[in] p_instance Pointer to the driver instance structure.
554  * @param[in] pin        Pin number.
555  */
556 void nrfx_gpiote_out_task_trigger(nrfx_gpiote_t const * p_instance, nrfx_gpiote_pin_t pin);
557 
558 #if defined(GPIOTE_FEATURE_SET_PRESENT) || defined(__NRFX_DOXYGEN__)
559 /**
560  * @brief Function for triggering the task SET manually.
561  *
562  * @param[in] p_instance Pointer to the driver instance structure.
563  * @param[in] pin        Pin number.
564  */
565 void nrfx_gpiote_set_task_trigger(nrfx_gpiote_t const * p_instance, nrfx_gpiote_pin_t pin);
566 #endif // defined(GPIOTE_FEATURE_SET_PRESENT) || defined(__NRFX_DOXYGEN__)
567 
568 #if defined(GPIOTE_FEATURE_CLR_PRESENT) || defined(__NRFX_DOXYGEN__)
569 /**
570  * @brief Function for triggering the task CLR manually.
571  *
572  * @param[in] p_instance Pointer to the driver instance structure.
573  * @param[in] pin        Pin number.
574  */
575 void nrfx_gpiote_clr_task_trigger(nrfx_gpiote_t const * p_instance, nrfx_gpiote_pin_t pin);
576 #endif // defined(GPIOTE_FEATURE_CLR_PRESENT) || defined(__NRFX_DOXYGEN__)
577 
578 #if NRF_GPIOTE_HAS_LATENCY || defined(__NRFX_DOXYGEN__)
579 /**
580  * @brief Function for setting the latency mode for a given GPIOTE instance.
581  *
582  * @note Available for event mode with rising or falling edge detection on the pin.
583  *       Toggle task mode can only be used with low latency mode setting.
584  *
585  * @param[in] p_instance Pointer to the driver instance structure.
586  * @param[in] latency    Latency mode to be set.
587  */
588 NRFX_STATIC_INLINE void nrfx_gpiote_latency_set(nrfx_gpiote_t const * p_instance,
589                                                 nrf_gpiote_latency_t  latency);
590 
591 /**
592  * @brief Function for getting the latency mode for a given GPIOTE instance.
593  *
594  * @param[in] p_instance Pointer to the driver instance structure.
595  *
596  * @return Latency mode.
597  */
598 NRFX_STATIC_INLINE nrf_gpiote_latency_t nrfx_gpiote_latency_get(nrfx_gpiote_t const * p_instance);
599 #endif
600 
601 #ifndef NRFX_DECLARE_ONLY
602 
603 #if NRF_GPIOTE_HAS_LATENCY
nrfx_gpiote_latency_set(nrfx_gpiote_t const * p_instance,nrf_gpiote_latency_t latency)604 NRFX_STATIC_INLINE void nrfx_gpiote_latency_set(nrfx_gpiote_t const * p_instance,
605                                                 nrf_gpiote_latency_t  latency)
606 {
607     nrfy_gpiote_latency_set(p_instance->p_reg, latency);
608 }
609 
nrfx_gpiote_latency_get(nrfx_gpiote_t const * p_instance)610 NRFX_STATIC_INLINE nrf_gpiote_latency_t nrfx_gpiote_latency_get(nrfx_gpiote_t const * p_instance)
611 {
612     return nrfy_gpiote_latency_get(p_instance->p_reg);
613 }
614 #endif // NRF_GPIOTE_HAS_LATENCY
615 
616 #endif // NRFX_DECLARE_ONLY
617 
618 #else
619 
620 #if !defined(NRF_GPIOTE_INDEX)
621 /* Choose the instance to use in case of using deprecated single-instance driver variant. */
622 #if defined(HALTIUM_XXAA)
623 #define NRF_GPIOTE_INDEX 130
624 #elif defined(LUMOS_XXAA)
625 #define NRF_GPIOTE_INDEX 20
626 #elif (defined(NRF5340_XXAA_APPLICATION) || defined(NRF91_SERIES)) && \
627       defined(NRF_TRUSTZONE_NONSECURE)
628 #define NRF_GPIOTE_INDEX 1
629 #else
630 #define NRF_GPIOTE_INDEX 0
631 #endif
632 #endif
633 
634 #if !defined(nrfx_gpiote_irq_handler)
635 #define nrfx_gpiote_irq_handler NRFX_CONCAT(nrfx_gpiote_, NRF_GPIOTE_INDEX, _irq_handler)
636 #endif
637 
638 #define NRFX_GPIOTE_DEFAULT_INPUT_CONFIG \
639 {                                        \
640     .pull = NRF_GPIO_PIN_NOPULL          \
641 }
642 
643 nrfx_err_t nrfx_gpiote_init(uint8_t interrupt_priority);
644 
645 bool nrfx_gpiote_is_init(void);
646 
647 void nrfx_gpiote_uninit(void);
648 
649 nrfx_err_t nrfx_gpiote_channel_alloc(uint8_t * p_channel);
650 
651 nrfx_err_t nrfx_gpiote_channel_free(uint8_t channel);
652 
653 nrfx_err_t nrfx_gpiote_input_configure(nrfx_gpiote_pin_t                    pin,
654                                        nrfx_gpiote_input_config_t const *   p_input_config,
655                                        nrfx_gpiote_trigger_config_t const * p_trigger_config,
656                                        nrfx_gpiote_handler_config_t const * p_handler_config);
657 
658 nrfx_err_t nrfx_gpiote_output_configure(nrfx_gpiote_pin_t                   pin,
659                                         nrfx_gpiote_output_config_t const * p_config,
660                                         nrfx_gpiote_task_config_t const *   p_task_config);
661 
662 nrfx_err_t nrfx_gpiote_pin_uninit(nrfx_gpiote_pin_t pin);
663 
664 void nrfx_gpiote_trigger_enable(nrfx_gpiote_pin_t pin, bool int_enable);
665 
666 void nrfx_gpiote_trigger_disable(nrfx_gpiote_pin_t pin);
667 
668 void nrfx_gpiote_global_callback_set(nrfx_gpiote_interrupt_handler_t handler,
669                                      void *                          p_context);
670 
671 nrfx_err_t nrfx_gpiote_channel_get(nrfx_gpiote_pin_t pin, uint8_t *p_channel);
672 
673 void nrfx_gpiote_out_set(nrfx_gpiote_pin_t pin);
674 
675 void nrfx_gpiote_out_clear(nrfx_gpiote_pin_t pin);
676 
677 void nrfx_gpiote_out_toggle(nrfx_gpiote_pin_t pin);
678 
679 void nrfx_gpiote_out_task_enable(nrfx_gpiote_pin_t pin);
680 
681 void nrfx_gpiote_out_task_disable(nrfx_gpiote_pin_t pin);
682 
683 nrf_gpiote_task_t nrfx_gpiote_out_task_get(nrfx_gpiote_pin_t pin);
684 
685 uint32_t nrfx_gpiote_out_task_address_get(nrfx_gpiote_pin_t pin);
686 
687 #if defined(GPIOTE_FEATURE_SET_PRESENT)
688 nrf_gpiote_task_t nrfx_gpiote_set_task_get(nrfx_gpiote_pin_t pin);
689 
690 uint32_t nrfx_gpiote_set_task_address_get(nrfx_gpiote_pin_t pin);
691 #endif
692 
693 #if defined(GPIOTE_FEATURE_CLR_PRESENT)
694 nrf_gpiote_task_t nrfx_gpiote_clr_task_get(nrfx_gpiote_pin_t pin);
695 
696 uint32_t nrfx_gpiote_clr_task_address_get(nrfx_gpiote_pin_t pin);
697 #endif
698 
699 nrf_gpiote_event_t nrfx_gpiote_in_event_get(nrfx_gpiote_pin_t pin);
700 
701 uint32_t nrfx_gpiote_in_event_address_get(nrfx_gpiote_pin_t pin);
702 
703 void nrfx_gpiote_out_task_force(nrfx_gpiote_pin_t pin, uint8_t state);
704 
705 void nrfx_gpiote_out_task_trigger(nrfx_gpiote_pin_t pin);
706 
707 #if defined(GPIOTE_FEATURE_SET_PRESENT)
708 void nrfx_gpiote_set_task_trigger(nrfx_gpiote_pin_t pin);
709 #endif
710 
711 #if defined(GPIOTE_FEATURE_CLR_PRESENT)
712 void nrfx_gpiote_clr_task_trigger(nrfx_gpiote_pin_t pin);
713 #endif
714 
715 #if NRF_GPIOTE_HAS_LATENCY
716 NRFX_STATIC_INLINE void nrfx_gpiote_latency_set(nrf_gpiote_latency_t latency);
717 
718 NRFX_STATIC_INLINE nrf_gpiote_latency_t nrfx_gpiote_latency_get(void);
719 #endif
720 
721 #ifndef NRFX_DECLARE_ONLY
722 #if NRF_GPIOTE_HAS_LATENCY
nrfx_gpiote_latency_set(nrf_gpiote_latency_t latency)723 NRFX_STATIC_INLINE void nrfx_gpiote_latency_set(nrf_gpiote_latency_t latency)
724 {
725     nrfy_gpiote_latency_set(NRFX_CONCAT(NRF_, GPIOTE, NRF_GPIOTE_INDEX), latency);
726 }
727 
nrfx_gpiote_latency_get(void)728 NRFX_STATIC_INLINE nrf_gpiote_latency_t nrfx_gpiote_latency_get(void)
729 {
730     return nrfy_gpiote_latency_get(NRFX_CONCAT(NRF_, GPIOTE, NRF_GPIOTE_INDEX));
731 }
732 #endif // NRF_GPIOTE_HAS_LATENCY
733 #endif // NRFX_DECLARE_ONLY
734 #endif // NRFX_API_VER_AT_LEAST(3, 2, 0) || defined(__NRFX_DOXYGEN__)
735 
736 /** @} */
737 
738 /*
739  * Declare interrupt handlers for all enabled driver instances in the following format:
740  * nrfx_\<periph_name\>_\<idx\>_irq_handler (for example, nrfx_gpiote_0_irq_handler).
741  *
742  * A specific interrupt handler for the driver instance can be retrieved by using
743  * the NRFX_GPIOTE_INST_HANDLER_GET macro.
744  *
745  * Here is a sample of using the NRFX_GPIOTE_INST_HANDLER_GET macro to map an interrupt handler
746  * in a Zephyr application:
747  *
748  * IRQ_CONNECT(NRFX_IRQ_NUMBER_GET(NRF_GPIOTE_INST_GET(\<instance_index\>)), \<priority\>,
749  *             NRFX_GPIOTE_INST_HANDLER_GET(\<instance_index\>), 0, 0);
750  */
751 NRFX_INSTANCE_IRQ_HANDLERS_DECLARE(GPIOTE, gpiote)
752 
753 #ifdef __cplusplus
754 }
755 #endif
756 
757 #endif // NRFX_GPIOTE_H__
758