1 /*
2  * Copyright (c) 2019 Nordic Semiconductor ASA
3  *
4  * SPDX-License-Identifier: Apache-2.0
5  */
6 
7 #include <zephyr/kernel.h>
8 
9 #include <nrfx_gpiote.h>
10 #include <helpers/nrfx_gppi.h>
11 
12 #include <zephyr/logging/log.h>
13 #include <zephyr/irq.h>
14 LOG_MODULE_REGISTER(nrfx_sample, LOG_LEVEL_INF);
15 
16 #define INPUT_PIN	NRF_DT_GPIOS_TO_PSEL(DT_ALIAS(sw0), gpios)
17 #define OUTPUT_PIN	NRF_DT_GPIOS_TO_PSEL(DT_ALIAS(led0), gpios)
18 
19 #define GPIOTE_INST	NRF_DT_GPIOTE_INST(DT_ALIAS(sw0), gpios)
20 #define GPIOTE_NODE	DT_NODELABEL(_CONCAT(gpiote, GPIOTE_INST))
21 
22 BUILD_ASSERT(NRF_DT_GPIOTE_INST(DT_ALIAS(led0), gpios) == GPIOTE_INST,
23 	"Both sw0 and led0 GPIOs must use the same GPIOTE instance");
24 BUILD_ASSERT(IS_ENABLED(_CONCAT(CONFIG_, _CONCAT(NRFX_GPIOTE, GPIOTE_INST))),
25 	"NRFX_GPIOTE" STRINGIFY(GPIOTE_INST) " must be enabled in Kconfig");
26 
27 
button_handler(nrfx_gpiote_pin_t pin,nrfx_gpiote_trigger_t trigger,void * context)28 static void button_handler(nrfx_gpiote_pin_t pin,
29 			   nrfx_gpiote_trigger_t trigger,
30 			   void *context)
31 {
32 	LOG_INF("GPIO input event callback");
33 }
34 
main(void)35 int main(void)
36 {
37 	LOG_INF("nrfx_gpiote sample on %s", CONFIG_BOARD);
38 
39 	nrfx_err_t err;
40 	uint8_t in_channel, out_channel;
41 	uint8_t ppi_channel;
42 	const nrfx_gpiote_t gpiote = NRFX_GPIOTE_INSTANCE(GPIOTE_INST);
43 
44 	/* Connect GPIOTE instance IRQ to irq handler */
45 	IRQ_CONNECT(DT_IRQN(GPIOTE_NODE), DT_IRQ(GPIOTE_NODE, priority), nrfx_isr,
46 		    NRFX_CONCAT(nrfx_gpiote_, GPIOTE_INST, _irq_handler), 0);
47 
48 	/* Initialize GPIOTE (the interrupt priority passed as the parameter
49 	 * here is ignored, see nrfx_glue.h).
50 	 */
51 	err = nrfx_gpiote_init(&gpiote, 0);
52 	if (err != NRFX_SUCCESS) {
53 		LOG_ERR("nrfx_gpiote_init error: 0x%08X", err);
54 		return 0;
55 	}
56 
57 	err = nrfx_gpiote_channel_alloc(&gpiote, &in_channel);
58 	if (err != NRFX_SUCCESS) {
59 		LOG_ERR("Failed to allocate in_channel, error: 0x%08X", err);
60 		return 0;
61 	}
62 
63 	err = nrfx_gpiote_channel_alloc(&gpiote, &out_channel);
64 	if (err != NRFX_SUCCESS) {
65 		LOG_ERR("Failed to allocate out_channel, error: 0x%08X", err);
66 		return 0;
67 	}
68 
69 	/* Initialize input pin to generate event on high to low transition
70 	 * (falling edge) and call button_handler()
71 	 */
72 	static const nrf_gpio_pin_pull_t pull_config = NRF_GPIO_PIN_PULLUP;
73 	nrfx_gpiote_trigger_config_t trigger_config = {
74 		.trigger = NRFX_GPIOTE_TRIGGER_HITOLO,
75 		.p_in_channel = &in_channel,
76 	};
77 	static const nrfx_gpiote_handler_config_t handler_config = {
78 		.handler = button_handler,
79 	};
80 	nrfx_gpiote_input_pin_config_t input_config = {
81 		.p_pull_config = &pull_config,
82 		.p_trigger_config = &trigger_config,
83 		.p_handler_config = &handler_config
84 	};
85 
86 	err = nrfx_gpiote_input_configure(&gpiote, INPUT_PIN, &input_config);
87 
88 	if (err != NRFX_SUCCESS) {
89 		LOG_ERR("nrfx_gpiote_input_configure error: 0x%08X", err);
90 		return 0;
91 	}
92 
93 	/* Initialize output pin. SET task will turn the LED on,
94 	 * CLR will turn it off and OUT will toggle it.
95 	 */
96 	static const nrfx_gpiote_output_config_t output_config = {
97 		.drive = NRF_GPIO_PIN_S0S1,
98 		.input_connect = NRF_GPIO_PIN_INPUT_DISCONNECT,
99 		.pull = NRF_GPIO_PIN_NOPULL,
100 	};
101 	const nrfx_gpiote_task_config_t task_config = {
102 		.task_ch = out_channel,
103 		.polarity = NRF_GPIOTE_POLARITY_TOGGLE,
104 		.init_val = 1,
105 	};
106 	err = nrfx_gpiote_output_configure(&gpiote, OUTPUT_PIN,
107 					   &output_config,
108 					   &task_config);
109 	if (err != NRFX_SUCCESS) {
110 		LOG_ERR("nrfx_gpiote_output_configure error: 0x%08X", err);
111 		return 0;
112 	}
113 
114 	nrfx_gpiote_trigger_enable(&gpiote, INPUT_PIN, true);
115 	nrfx_gpiote_out_task_enable(&gpiote, OUTPUT_PIN);
116 
117 	LOG_INF("nrfx_gpiote initialized");
118 
119 	/* Allocate a (D)PPI channel. */
120 	err = nrfx_gppi_channel_alloc(&ppi_channel);
121 	if (err != NRFX_SUCCESS) {
122 		LOG_ERR("nrfx_gppi_channel_alloc error: 0x%08X", err);
123 		return 0;
124 	}
125 
126 	/* Configure endpoints of the channel so that the input pin event is
127 	 * connected with the output pin OUT task. This means that each time
128 	 * the button is pressed, the LED pin will be toggled.
129 	 */
130 	nrfx_gppi_channel_endpoints_setup(ppi_channel,
131 		nrfx_gpiote_in_event_address_get(&gpiote, INPUT_PIN),
132 		nrfx_gpiote_out_task_address_get(&gpiote, OUTPUT_PIN));
133 
134 	/* Enable the channel. */
135 	nrfx_gppi_channels_enable(BIT(ppi_channel));
136 
137 	LOG_INF("(D)PPI configured, leaving main()");
138 	return 0;
139 }
140