1 /*
2  * Copyright (c) 2021 - 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 NRFY_GPIO_H__
35 #define NRFY_GPIO_H__
36 
37 #include <nrfx.h>
38 #include <hal/nrf_gpio.h>
39 
40 #ifdef __cplusplus
41 extern "C" {
42 #endif
43 
44 /**
45  * @defgroup nrfy_gpio GPIO HALY
46  * @{
47  * @ingroup nrf_gpio
48  * @brief   Hardware access layer with cache and barrier support for managing the GPIO peripheral.
49  */
50 
51 /** @refhal{nrf_gpio_range_cfg_output} */
nrfy_gpio_range_cfg_output(uint32_t pin_range_start,uint32_t pin_range_end)52 NRFY_STATIC_INLINE void nrfy_gpio_range_cfg_output(uint32_t pin_range_start,
53                                                    uint32_t pin_range_end)
54 {
55     nrf_gpio_range_cfg_output(pin_range_start, pin_range_end);
56     nrf_barrier_w();
57 }
58 
59 /** @refhal{nrf_gpio_range_cfg_input} */
nrfy_gpio_range_cfg_input(uint32_t pin_range_start,uint32_t pin_range_end,nrf_gpio_pin_pull_t pull_config)60 NRFY_STATIC_INLINE void nrfy_gpio_range_cfg_input(uint32_t            pin_range_start,
61                                                   uint32_t            pin_range_end,
62                                                   nrf_gpio_pin_pull_t pull_config)
63 {
64     nrf_gpio_range_cfg_input(pin_range_start, pin_range_end, pull_config);
65     nrf_barrier_w();
66 }
67 
68 /** @refhal{nrf_gpio_cfg} */
nrfy_gpio_cfg(uint32_t pin_number,nrf_gpio_pin_dir_t dir,nrf_gpio_pin_input_t input,nrf_gpio_pin_pull_t pull,nrf_gpio_pin_drive_t drive,nrf_gpio_pin_sense_t sense)69 NRFY_STATIC_INLINE void nrfy_gpio_cfg(uint32_t             pin_number,
70                                       nrf_gpio_pin_dir_t   dir,
71                                       nrf_gpio_pin_input_t input,
72                                       nrf_gpio_pin_pull_t  pull,
73                                       nrf_gpio_pin_drive_t drive,
74                                       nrf_gpio_pin_sense_t sense)
75 {
76     nrf_gpio_cfg(pin_number, dir, input, pull, drive, sense);
77     nrf_barrier_w();
78 }
79 
80 /** @refhal{nrf_gpio_reconfigure} */
nrfy_gpio_reconfigure(uint32_t pin_number,const nrf_gpio_pin_dir_t * p_dir,const nrf_gpio_pin_input_t * p_input,const nrf_gpio_pin_pull_t * p_pull,const nrf_gpio_pin_drive_t * p_drive,const nrf_gpio_pin_sense_t * p_sense)81 NRFY_STATIC_INLINE void nrfy_gpio_reconfigure(uint32_t                     pin_number,
82                                               const nrf_gpio_pin_dir_t *   p_dir,
83                                               const nrf_gpio_pin_input_t * p_input,
84                                               const nrf_gpio_pin_pull_t *  p_pull,
85                                               const nrf_gpio_pin_drive_t * p_drive,
86                                               const nrf_gpio_pin_sense_t * p_sense)
87 {
88     nrf_gpio_reconfigure(pin_number, p_dir, p_input, p_pull, p_drive, p_sense);
89     nrf_barrier_w();
90 }
91 
92 /** @refhal{nrf_gpio_cfg_output} */
nrfy_gpio_cfg_output(uint32_t pin_number)93 NRFY_STATIC_INLINE void nrfy_gpio_cfg_output(uint32_t pin_number)
94 {
95     nrf_gpio_cfg_output(pin_number);
96     nrf_barrier_w();
97 }
98 
99 /** @refhal{nrf_gpio_cfg_input} */
nrfy_gpio_cfg_input(uint32_t pin_number,nrf_gpio_pin_pull_t pull_config)100 NRFY_STATIC_INLINE void nrfy_gpio_cfg_input(uint32_t pin_number, nrf_gpio_pin_pull_t pull_config)
101 {
102     nrf_gpio_cfg_input(pin_number, pull_config);
103     nrf_barrier_w();
104 }
105 
106 /** @refhal{nrf_gpio_cfg_default} */
nrfy_gpio_cfg_default(uint32_t pin_number)107 NRFY_STATIC_INLINE void nrfy_gpio_cfg_default(uint32_t pin_number)
108 {
109     nrf_gpio_cfg_default(pin_number);
110     nrf_barrier_w();
111 }
112 
113 /** @refhal{nrf_gpio_cfg_watcher} */
nrfy_gpio_cfg_watcher(uint32_t pin_number)114 NRFY_STATIC_INLINE void nrfy_gpio_cfg_watcher(uint32_t pin_number)
115 {
116     nrf_barrier_r();
117     nrf_gpio_cfg_watcher(pin_number);
118     nrf_barrier_rw();
119 }
120 
121 /** @refhal{nrf_gpio_input_disconnect} */
nrfy_gpio_input_disconnect(uint32_t pin_number)122 NRFY_STATIC_INLINE void nrfy_gpio_input_disconnect(uint32_t pin_number)
123 {
124     nrf_barrier_r();
125     nrf_gpio_input_disconnect(pin_number);
126     nrf_barrier_rw();
127 }
128 
129 /** @refhal{nrf_gpio_cfg_sense_input} */
nrfy_gpio_cfg_sense_input(uint32_t pin_number,nrf_gpio_pin_pull_t pull_config,nrf_gpio_pin_sense_t sense_config)130 NRFY_STATIC_INLINE void nrfy_gpio_cfg_sense_input(uint32_t             pin_number,
131                                                   nrf_gpio_pin_pull_t  pull_config,
132                                                   nrf_gpio_pin_sense_t sense_config)
133 {
134     nrf_gpio_cfg_sense_input(pin_number, pull_config, sense_config);
135     nrf_barrier_w();
136 }
137 
138 /** @refhal{nrf_gpio_cfg_sense_set} */
nrfy_gpio_cfg_sense_set(uint32_t pin_number,nrf_gpio_pin_sense_t sense_config)139 NRFY_STATIC_INLINE void nrfy_gpio_cfg_sense_set(uint32_t             pin_number,
140                                                 nrf_gpio_pin_sense_t sense_config)
141 {
142     nrf_barrier_r();
143     nrf_gpio_cfg_sense_set(pin_number, sense_config);
144     nrf_barrier_rw();
145 }
146 
147 /** @refhal{nrf_gpio_pin_dir_set} */
nrfy_gpio_pin_dir_set(uint32_t pin_number,nrf_gpio_pin_dir_t direction)148 NRFY_STATIC_INLINE void nrfy_gpio_pin_dir_set(uint32_t pin_number, nrf_gpio_pin_dir_t direction)
149 {
150     nrf_gpio_pin_dir_set(pin_number, direction);
151     nrf_barrier_w();
152 }
153 
154 /** @refhal{nrf_gpio_pin_set} */
nrfy_gpio_pin_set(uint32_t pin_number)155 NRFY_STATIC_INLINE void nrfy_gpio_pin_set(uint32_t pin_number)
156 {
157     nrf_gpio_pin_set(pin_number);
158     nrf_barrier_w();
159 }
160 
161 /** @refhal{nrf_gpio_pin_clear} */
nrfy_gpio_pin_clear(uint32_t pin_number)162 NRFY_STATIC_INLINE void nrfy_gpio_pin_clear(uint32_t pin_number)
163 {
164     nrf_gpio_pin_clear(pin_number);
165     nrf_barrier_w();
166 }
167 
168 /** @refhal{nrf_gpio_pin_toggle} */
nrfy_gpio_pin_toggle(uint32_t pin_number)169 NRFY_STATIC_INLINE void nrfy_gpio_pin_toggle(uint32_t pin_number)
170 {
171     nrf_gpio_pin_toggle(pin_number);
172     nrf_barrier_w();
173 }
174 
175 /** @refhal{nrf_gpio_pin_write} */
nrfy_gpio_pin_write(uint32_t pin_number,uint32_t value)176 NRFY_STATIC_INLINE void nrfy_gpio_pin_write(uint32_t pin_number, uint32_t value)
177 {
178     nrf_gpio_pin_write(pin_number, value);
179     nrf_barrier_w();
180 }
181 
182 /** @refhal{nrf_gpio_pin_read} */
nrfy_gpio_pin_read(uint32_t pin_number)183 NRFY_STATIC_INLINE uint32_t nrfy_gpio_pin_read(uint32_t pin_number)
184 {
185     nrf_barrier_r();
186     uint32_t pin = nrf_gpio_pin_read(pin_number);
187     nrf_barrier_r();
188     return pin;
189 }
190 
191 /** @refhal{nrf_gpio_pin_out_read} */
nrfy_gpio_pin_out_read(uint32_t pin_number)192 NRFY_STATIC_INLINE uint32_t nrfy_gpio_pin_out_read(uint32_t pin_number)
193 {
194     nrf_barrier_rw();
195     uint32_t pin = nrf_gpio_pin_out_read(pin_number);
196     nrf_barrier_r();
197     return pin;
198 }
199 
200 /** @refhal{nrf_gpio_pin_sense_get} */
nrfy_gpio_pin_sense_get(uint32_t pin_number)201 NRFY_STATIC_INLINE nrf_gpio_pin_sense_t nrfy_gpio_pin_sense_get(uint32_t pin_number)
202 {
203     nrf_barrier_rw();
204     nrf_gpio_pin_sense_t pin_sense = nrf_gpio_pin_sense_get(pin_number);
205     nrf_barrier_r();
206     return pin_sense;
207 }
208 
209 /** @refhal{nrf_gpio_pin_dir_get} */
nrfy_gpio_pin_dir_get(uint32_t pin_number)210 NRFY_STATIC_INLINE nrf_gpio_pin_dir_t nrfy_gpio_pin_dir_get(uint32_t pin_number)
211 {
212     nrf_barrier_rw();
213     nrf_gpio_pin_dir_t pin_dir = nrf_gpio_pin_dir_get(pin_number);
214     nrf_barrier_r();
215     return pin_dir;
216 }
217 
218 /** @refhal{nrf_gpio_pin_input_get} */
nrfy_gpio_pin_input_get(uint32_t pin_number)219 NRFY_STATIC_INLINE nrf_gpio_pin_input_t nrfy_gpio_pin_input_get(uint32_t pin_number)
220 {
221     nrf_barrier_rw();
222     nrf_gpio_pin_input_t pin_input = nrf_gpio_pin_input_get(pin_number);
223     nrf_barrier_r();
224     return pin_input;
225 }
226 
227 /** @refhal{nrf_gpio_pin_pull_get} */
nrfy_gpio_pin_pull_get(uint32_t pin_number)228 NRFY_STATIC_INLINE nrf_gpio_pin_pull_t nrfy_gpio_pin_pull_get(uint32_t pin_number)
229 {
230     nrf_barrier_rw();
231     nrf_gpio_pin_pull_t pin_pull = nrf_gpio_pin_pull_get(pin_number);
232     nrf_barrier_r();
233     return pin_pull;
234 }
235 
236 /** @refhal{nrf_gpio_port_dir_output_set} */
nrfy_gpio_port_dir_output_set(NRF_GPIO_Type * p_reg,uint32_t out_mask)237 NRFY_STATIC_INLINE void nrfy_gpio_port_dir_output_set(NRF_GPIO_Type * p_reg, uint32_t out_mask)
238 {
239     nrf_gpio_port_dir_output_set(p_reg, out_mask);
240     nrf_barrier_w();
241 }
242 
243 /** @refhal{nrf_gpio_port_dir_input_set} */
nrfy_gpio_port_dir_input_set(NRF_GPIO_Type * p_reg,uint32_t in_mask)244 NRFY_STATIC_INLINE void nrfy_gpio_port_dir_input_set(NRF_GPIO_Type * p_reg, uint32_t in_mask)
245 {
246     nrf_gpio_port_dir_input_set(p_reg, in_mask);
247     nrf_barrier_w();
248 }
249 
250 /** @refhal{nrf_gpio_port_dir_write} */
nrfy_gpio_port_dir_write(NRF_GPIO_Type * p_reg,uint32_t dir_mask)251 NRFY_STATIC_INLINE void nrfy_gpio_port_dir_write(NRF_GPIO_Type * p_reg, uint32_t dir_mask)
252 {
253     nrf_gpio_port_dir_write(p_reg, dir_mask);
254     nrf_barrier_w();
255 }
256 
257 /** @refhal{nrf_gpio_port_dir_read} */
nrfy_gpio_port_dir_read(NRF_GPIO_Type const * p_reg)258 NRFY_STATIC_INLINE uint32_t nrfy_gpio_port_dir_read(NRF_GPIO_Type const * p_reg)
259 {
260     nrf_barrier_rw();
261     uint32_t port_dir = nrf_gpio_port_dir_read(p_reg);
262     nrf_barrier_r();
263     return port_dir;
264 }
265 
266 /** @refhal{nrf_gpio_port_in_read} */
nrfy_gpio_port_in_read(NRF_GPIO_Type const * p_reg)267 NRFY_STATIC_INLINE uint32_t nrfy_gpio_port_in_read(NRF_GPIO_Type const * p_reg)
268 {
269     nrf_barrier_rw();
270     uint32_t port_in = nrf_gpio_port_in_read(p_reg);
271     nrf_barrier_r();
272     return port_in;
273 }
274 
275 /** @refhal{nrf_gpio_port_out_read} */
nrfy_gpio_port_out_read(NRF_GPIO_Type const * p_reg)276 NRFY_STATIC_INLINE uint32_t nrfy_gpio_port_out_read(NRF_GPIO_Type const * p_reg)
277 {
278     nrf_barrier_rw();
279     uint32_t port_out = nrf_gpio_port_out_read(p_reg);
280     nrf_barrier_r();
281     return port_out;
282 }
283 
284 /** @refhal{nrf_gpio_port_out_write} */
nrfy_gpio_port_out_write(NRF_GPIO_Type * p_reg,uint32_t value)285 NRFY_STATIC_INLINE void nrfy_gpio_port_out_write(NRF_GPIO_Type * p_reg, uint32_t value)
286 {
287     nrf_gpio_port_out_write(p_reg, value);
288     nrf_barrier_w();
289 }
290 
291 /** @refhal{nrf_gpio_port_out_set} */
nrfy_gpio_port_out_set(NRF_GPIO_Type * p_reg,uint32_t set_mask)292 NRFY_STATIC_INLINE void nrfy_gpio_port_out_set(NRF_GPIO_Type * p_reg, uint32_t set_mask)
293 {
294     nrf_gpio_port_out_set(p_reg, set_mask);
295     nrf_barrier_w();
296 }
297 
298 /** @refhal{nrf_gpio_port_out_clear} */
nrfy_gpio_port_out_clear(NRF_GPIO_Type * p_reg,uint32_t clr_mask)299 NRFY_STATIC_INLINE void nrfy_gpio_port_out_clear(NRF_GPIO_Type * p_reg, uint32_t clr_mask)
300 {
301     nrf_gpio_port_out_clear(p_reg, clr_mask);
302     nrf_barrier_w();
303 }
304 
305 /** @refhal{nrf_gpio_ports_read} */
nrfy_gpio_ports_read(uint32_t start_port,uint32_t length,uint32_t * p_masks)306 NRFY_STATIC_INLINE void nrfy_gpio_ports_read(uint32_t   start_port,
307                                              uint32_t   length,
308                                              uint32_t * p_masks)
309 {
310     nrf_barrier_r();
311     nrf_gpio_ports_read(start_port, length, p_masks);
312     nrf_barrier_r();
313 }
314 
315 #if defined(NRF_GPIO_LATCH_PRESENT)
316 /** @refhal{nrf_gpio_latches_read} */
nrfy_gpio_latches_read(uint32_t start_port,uint32_t length,uint32_t * p_masks)317 NRFY_STATIC_INLINE void nrfy_gpio_latches_read(uint32_t   start_port,
318                                                uint32_t   length,
319                                                uint32_t * p_masks)
320 {
321     nrf_barrier_r();
322     nrf_gpio_latches_read(start_port, length, p_masks);
323     nrf_barrier_r();
324 }
325 
326 /** @refhal{nrf_gpio_latches_read_and_clear} */
nrfy_gpio_latches_read_and_clear(uint32_t start_port,uint32_t length,uint32_t * p_masks)327 NRFY_STATIC_INLINE void nrfy_gpio_latches_read_and_clear(uint32_t   start_port,
328                                                          uint32_t   length,
329                                                          uint32_t * p_masks)
330 {
331     nrf_barrier_r();
332     nrf_gpio_latches_read_and_clear(start_port, length, p_masks);
333     nrf_barrier_rw();
334 }
335 
336 /** @refhal{nrf_gpio_pin_latch_get} */
nrfy_gpio_pin_latch_get(uint32_t pin_number)337 NRFY_STATIC_INLINE uint32_t nrfy_gpio_pin_latch_get(uint32_t pin_number)
338 {
339     nrf_barrier_r();
340     uint32_t pin_latch = nrf_gpio_pin_latch_get(pin_number);
341     nrf_barrier_r();
342     return pin_latch;
343 }
344 
345 /** @refhal{nrf_gpio_pin_latch_clear} */
nrfy_gpio_pin_latch_clear(uint32_t pin_number)346 NRFY_STATIC_INLINE void nrfy_gpio_pin_latch_clear(uint32_t pin_number)
347 {
348     nrf_gpio_pin_latch_clear(pin_number);
349     nrf_barrier_w();
350 }
351 #endif // defined(NRF_GPIO_LATCH_PRESENT)
352 
353 #if NRF_GPIO_HAS_SEL
354 /** @refhal{nrf_gpio_pin_control_select} */
nrfy_gpio_pin_control_select(uint32_t pin_number,nrf_gpio_pin_sel_t ctrl)355 NRFY_STATIC_INLINE void nrfy_gpio_pin_control_select(uint32_t pin_number, nrf_gpio_pin_sel_t ctrl)
356 {
357     nrf_barrier_r();
358     nrf_gpio_pin_control_select(pin_number, ctrl);
359     nrf_barrier_rw();
360 }
361 #endif
362 
363 #if NRF_GPIO_HAS_CLOCKPIN
364 /** @refhal{nrf_gpio_pin_clock_set} */
nrfy_gpio_pin_clock_set(uint32_t pin_number,bool enable)365 NRFY_STATIC_INLINE void nrfy_gpio_pin_clock_set(uint32_t pin_number, bool enable)
366 {
367     nrf_gpio_pin_clock_set(pin_number, enable);
368     nrf_barrier_w();
369 }
370 
371 /** @refhal{nrf_gpio_pin_clock_check} */
nrfy_gpio_pin_clock_check(uint32_t pin_number)372 NRFY_STATIC_INLINE bool nrfy_gpio_pin_clock_check(uint32_t pin_number)
373 {
374     nrf_barrier_rw();
375     bool pin_clock = nrf_gpio_pin_clock_check(pin_number);
376     nrf_barrier_r();
377     return pin_clock;
378 }
379 #endif
380 
381 /** @refhal{nrf_gpio_pin_present_check} */
nrfy_gpio_pin_present_check(uint32_t pin_number)382 NRFY_STATIC_INLINE bool nrfy_gpio_pin_present_check(uint32_t pin_number)
383 {
384     return nrf_gpio_pin_present_check(pin_number);
385 }
386 
387 /** @refhal{nrf_gpio_pin_port_number_extract} */
nrfy_gpio_pin_port_number_extract(uint32_t * p_pin)388 NRFY_STATIC_INLINE uint32_t nrfy_gpio_pin_port_number_extract(uint32_t * p_pin)
389 {
390     return nrf_gpio_pin_port_number_extract(p_pin);
391 }
392 
393 /** @} */
394 
395 #ifdef __cplusplus
396 }
397 #endif
398 
399 #endif // NRFY_GPIO_H__
400