Contiki-NG
gpio-hal-arch.c
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2020, George Oikonomou - https://spd.gr
3  * Copyright (C) 2020 Yago Fontoura do Rosario <yago.rosario@hotmail.com.br>
4  * All rights reserved.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions
8  * are met:
9  * 1. Redistributions of source code must retain the above copyright
10  * notice, this list of conditions and the following disclaimer.
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  * 3. Neither the name of the copyright holder nor the names of its
15  * contributors may be used to endorse or promote products derived
16  * from this software without specific prior written permission.
17  *
18  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
20  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
21  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
22  * COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
23  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
24  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
25  * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
27  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
29  * OF THE POSSIBILITY OF SUCH DAMAGE.
30  */
31 
32 /**
33  * \addtogroup nrf
34  * @{
35  *
36  * \addtogroup nrf-dev Device drivers
37  * @{
38  *
39  * \addtogroup nrf-gpio GPIO HAL driver
40  * @{
41  *
42  * \file
43  * GPIO HAL implementation for the nRF
44  * \author
45  * Yago Fontoura do Rosario <yago.rosario@hotmail.com.br>
46  *
47  */
48 /*---------------------------------------------------------------------------*/
49 #include "contiki.h"
50 
51 #include "dev/gpio-hal.h"
52 
53 #include "nrfx_gpiote.h"
54 
55 #include "hal/nrf_gpio.h"
56 /*---------------------------------------------------------------------------*/
57 #define PIN_TO_PORT(pin) (pin >> 5)
58 #define PIN_TO_NUM(pin) (pin & 0x1F)
59 /*---------------------------------------------------------------------------*/
60 /**
61  * @brief GPIO event handler
62  *
63  * @param pin GPIO pin
64  * @param action Action
65  */
66 static void
67 pin_event_handler(nrfx_gpiote_pin_t pin, nrf_gpiote_polarity_t action)
68 {
69  gpio_hal_port_t port;
70  gpio_hal_pin_mask_t pin_mask;
71 
72  port = PIN_TO_PORT(pin);
73  pin_mask = gpio_hal_pin_to_mask(PIN_TO_NUM(pin));
74 
75  gpio_hal_event_handler(port, pin_mask);
76 }
77 /*---------------------------------------------------------------------------*/
78 void
80 {
81  if(!nrfx_gpiote_is_init()) {
82  nrfx_gpiote_init(NRFX_GPIOTE_DEFAULT_CONFIG_IRQ_PRIORITY);
83  }
84 }
85 /*---------------------------------------------------------------------------*/
86 void
88 {
90  nrfx_gpiote_in_config_t gpiote_config = {
91  .is_watcher = false,
92  .hi_accuracy = true,
93  };
94 
95  uint32_t pin_number = NRF_GPIO_PIN_MAP(port, pin);
96 
97  tmp = cfg & GPIO_HAL_PIN_CFG_EDGE_BOTH;
98  if(tmp == GPIO_HAL_PIN_CFG_EDGE_NONE) {
99  gpiote_config.sense = GPIOTE_CONFIG_POLARITY_None;
100  } else if(tmp == GPIO_HAL_PIN_CFG_EDGE_RISING) {
101  gpiote_config.sense = NRF_GPIOTE_POLARITY_LOTOHI;
102  } else if(tmp == GPIO_HAL_PIN_CFG_EDGE_FALLING) {
103  gpiote_config.sense = NRF_GPIOTE_POLARITY_HITOLO;
104  } else if(tmp == GPIO_HAL_PIN_CFG_EDGE_BOTH) {
105  gpiote_config.sense = NRF_GPIOTE_POLARITY_TOGGLE;
106  }
107 
108  tmp = cfg & GPIO_HAL_PIN_CFG_PULL_MASK;
109  if(tmp == GPIO_HAL_PIN_CFG_PULL_NONE) {
110  gpiote_config.pull = NRF_GPIO_PIN_NOPULL;
111  } else if(tmp == GPIO_HAL_PIN_CFG_PULL_DOWN) {
112  gpiote_config.pull = NRF_GPIO_PIN_PULLDOWN;
113  } else if(tmp == GPIO_HAL_PIN_CFG_PULL_UP) {
114  gpiote_config.pull = NRF_GPIO_PIN_PULLUP;
115  }
116 
117  nrfx_gpiote_in_init(pin_number, &gpiote_config, pin_event_handler);
118 
119  tmp = cfg & GPIO_HAL_PIN_CFG_INT_MASK;
120  if(tmp == GPIO_HAL_PIN_CFG_INT_DISABLE) {
121  nrfx_gpiote_in_event_disable(pin_number);
122  } else if(tmp == GPIO_HAL_PIN_CFG_INT_ENABLE) {
123  nrfx_gpiote_in_event_enable(pin_number, true);
124  }
125 }
126 /*---------------------------------------------------------------------------*/
129 {
130  uint8_t i;
131  uint32_t pin_number;
132  gpio_hal_pin_cfg_t cfg = GPIO_HAL_PIN_CFG_PULL_NONE |
133  GPIO_HAL_PIN_CFG_EDGE_NONE |
134  GPIO_HAL_PIN_CFG_INT_DISABLE;
135  nrf_gpio_pin_pull_t pull;
136  nrf_gpiote_polarity_t polarity;
137 
138  pin_number = NRF_GPIO_PIN_MAP(port, pin);
139 
140  /* First, check if the pin is configured as output */
141  if(nrf_gpio_pin_dir_get(pin_number) == NRF_GPIO_PIN_DIR_OUTPUT) {
142  return 0;
143  }
144 
145  /*
146  * Input pin. Check all GPIOTE channel configurations and figure out which
147  * channel corresponds to our pin of interest. For that channel, read out
148  * the GPIOTE configuration
149  */
150  for(i = 0; i < GPIOTE_CH_NUM; i++) {
151  if(nrf_gpiote_event_pin_get(NRF_GPIOTE, i) == pin_number) {
152  polarity = nrf_gpiote_event_polarity_get(NRF_GPIOTE, i);
153 
154  if(polarity == NRF_GPIOTE_POLARITY_LOTOHI) {
155  cfg |= GPIO_HAL_PIN_CFG_EDGE_BOTH;
156  } else if(polarity == NRF_GPIOTE_POLARITY_HITOLO) {
157  cfg |= GPIO_HAL_PIN_CFG_EDGE_BOTH;
158  } else if(polarity == NRF_GPIOTE_POLARITY_TOGGLE) {
159  cfg |= GPIO_HAL_PIN_CFG_EDGE_BOTH;
160  }
161 
162  pull = nrf_gpio_pin_pull_get(pin_number);
163 
164  if(pull == NRF_GPIO_PIN_PULLDOWN) {
165  cfg |= GPIO_HAL_PIN_CFG_PULL_DOWN;
166  } else if(pull == NRF_GPIO_PIN_PULLUP) {
167  cfg |= GPIO_HAL_PIN_CFG_PULL_UP;
168  }
169 
170  if(nrf_gpiote_int_enable_check(NRF_GPIOTE, 1 << i)) {
171  cfg |= GPIO_HAL_PIN_CFG_INT_ENABLE;
172  }
173  return cfg;
174  }
175  }
176 
177  /* Did not find a GPIOTE channel configured for this pin */
178  return 0;
179 }
180 /*---------------------------------------------------------------------------*/
181 uint8_t
183 {
184  return (uint8_t)nrf_gpio_pin_read(NRF_GPIO_PIN_MAP(port, pin));
185 }
186 /*---------------------------------------------------------------------------*/
187 void
189 {
190  NRF_GPIO_Type *gpio_regs[GPIO_COUNT] = GPIO_REG_LIST;
191 
192  if(port >= GPIO_COUNT) {
193  return;
194  }
195 
196  nrf_gpio_port_out_set(gpio_regs[port], pins);
197 }
198 /*---------------------------------------------------------------------------*/
199 void
201 {
202  NRF_GPIO_Type *gpio_regs[GPIO_COUNT] = GPIO_REG_LIST;
203 
204  if(port >= GPIO_COUNT) {
205  return;
206  }
207 
208  nrf_gpio_port_out_clear(gpio_regs[port], pins);
209 }
210 /*---------------------------------------------------------------------------*/
211 void
213 {
214  if(port >= GPIO_COUNT) {
215  return;
216  }
217  gpio_hal_arch_write_pins(port, pins, ~gpio_hal_arch_read_pins(port, pins));
218 }
219 /*---------------------------------------------------------------------------*/
222 {
223  NRF_GPIO_Type *gpio_regs[GPIO_COUNT] = GPIO_REG_LIST;
224 
225  if(port >= GPIO_COUNT) {
226  return 0;
227  }
228 
229  return nrf_gpio_port_in_read(gpio_regs[port]);
230 }
231 /*---------------------------------------------------------------------------*/
232 void
234  gpio_hal_pin_mask_t value)
235 {
236  NRF_GPIO_Type *gpio_regs[GPIO_COUNT] = GPIO_REG_LIST;
237 
238  if(port >= GPIO_COUNT) {
239  return;
240  }
241 
242  nrf_gpio_port_out_write(gpio_regs[port], value);
243 }
244 /*---------------------------------------------------------------------------*/
245 /**
246  * @}
247  * @}
248  * @}
249  */
static void pin_event_handler(nrfx_gpiote_pin_t pin, nrf_gpiote_polarity_t action)
GPIO event handler.
Definition: gpio-hal-arch.c:67
void gpio_hal_arch_init(void)
Perform architecture specific gpio initaliaztion.
Definition: gpio-hal-arch.c:46
void gpio_hal_arch_port_set_pins(gpio_hal_port_t port, gpio_hal_pin_mask_t pins)
Set multiple pins to logical high.
void gpio_hal_arch_port_clear_pins(gpio_hal_port_t port, gpio_hal_pin_mask_t pins)
Clear multiple pins to logical low.
uint8_t gpio_hal_arch_port_read_pin(gpio_hal_port_t port, gpio_hal_pin_t pin)
Read a GPIO pin.
uint8_t gpio_hal_port_t
A data structure that represents ports.
Definition: gpio-hal.h:110
void gpio_hal_arch_port_toggle_pins(gpio_hal_port_t port, gpio_hal_pin_mask_t pins)
Toggle multiple pins.
gpio_hal_pin_cfg_t gpio_hal_arch_port_pin_cfg_get(gpio_hal_port_t port, gpio_hal_pin_t pin)
Read the configuration of a GPIO pin.
uint8_t gpio_hal_pin_t
GPIO pin number representation.
Definition: gpio-hal.h:103
void gpio_hal_arch_port_write_pins(gpio_hal_port_t port, gpio_hal_pin_mask_t pins, gpio_hal_pin_mask_t value)
Write multiple pins.
uint32_t gpio_hal_pin_mask_t
GPIO pin mask representation.
Definition: gpio-hal.h:142
void gpio_hal_arch_port_pin_cfg_set(gpio_hal_port_t port, gpio_hal_pin_t pin, gpio_hal_pin_cfg_t cfg)
Configure a gpio pin.
Definition: gpio-hal-arch.c:87
#define gpio_hal_pin_to_mask(pin)
Convert a pin to a pin mask.
Definition: gpio-hal.h:255
Header file for the GPIO HAL.
void gpio_hal_event_handler(gpio_hal_port_t port, gpio_hal_pin_mask_t pins)
The platform-independent GPIO event handler.
uint32_t gpio_hal_pin_cfg_t
GPIO pin configuration.
Definition: gpio-hal.h:118
gpio_hal_pin_mask_t gpio_hal_arch_port_read_pins(gpio_hal_port_t port, gpio_hal_pin_mask_t pins)
Read multiple pins.