Contiki-NG
gpio-hal-arch.c
Go to the documentation of this file.
1/*
2 * Copyright (c) 2020, George Oikonomou - https://spd.gr
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
7 * are met:
8 *
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 nrf52840-gpio-hal
34 * @{
35 *
36 * \file
37 * Implementation file for the nRF52840 GPIO HAL functions
38 */
39/*---------------------------------------------------------------------------*/
40#include "contiki.h"
41#include "dev/gpio-hal.h"
42#include "nrfx_gpiote.h"
43#include "nrf_gpio.h"
44
45#include <stdint.h>
46#include <string.h>
47/*---------------------------------------------------------------------------*/
48#define PIN_TO_PORT(pin) (pin >> 5)
49#define PIN_TO_NUM(pin) (pin & 0x1F)
50/*---------------------------------------------------------------------------*/
51static void
52pin_event_handler(nrfx_gpiote_pin_t pin, nrf_gpiote_polarity_t action)
53{
54 gpio_hal_port_t port;
55 gpio_hal_pin_mask_t pin_mask;
56
57 port = PIN_TO_PORT(pin);
58 pin_mask = gpio_hal_pin_to_mask(PIN_TO_NUM(pin));
59
60 gpio_hal_event_handler(port, pin_mask);
61}
62/*---------------------------------------------------------------------------*/
63void
65{
66 if(!nrfx_gpiote_is_init()) {
67 nrfx_gpiote_init();
68 }
69}
70/*---------------------------------------------------------------------------*/
71void
73{
75 nrfx_gpiote_in_config_t gpiote_config = {
76 .is_watcher = false,
77 .hi_accuracy = true,
78 };
79
80 uint32_t pin_number = NRF_GPIO_PIN_MAP(port, pin);;
81
82 tmp = cfg & GPIO_HAL_PIN_CFG_EDGE_BOTH;
83 if(tmp == GPIO_HAL_PIN_CFG_EDGE_NONE) {
84 gpiote_config.sense = GPIOTE_CONFIG_POLARITY_None;
85 } else if(tmp == GPIO_HAL_PIN_CFG_EDGE_RISING) {
86 gpiote_config.sense = NRF_GPIOTE_POLARITY_LOTOHI;
87 } else if(tmp == GPIO_HAL_PIN_CFG_EDGE_FALLING) {
88 gpiote_config.sense = NRF_GPIOTE_POLARITY_HITOLO;
89 } else if(tmp == GPIO_HAL_PIN_CFG_EDGE_BOTH) {
90 gpiote_config.sense = NRF_GPIOTE_POLARITY_TOGGLE;
91 }
92
93 tmp = cfg & GPIO_HAL_PIN_CFG_PULL_MASK;
94 if(tmp == GPIO_HAL_PIN_CFG_PULL_NONE) {
95 gpiote_config.pull = NRF_GPIO_PIN_NOPULL;
96 } else if(tmp == GPIO_HAL_PIN_CFG_PULL_DOWN) {
97 gpiote_config.pull = NRF_GPIO_PIN_PULLDOWN;
98 } else if(tmp == GPIO_HAL_PIN_CFG_PULL_UP) {
99 gpiote_config.pull = NRF_GPIO_PIN_PULLUP;
100 }
101
102 nrfx_gpiote_in_init(pin_number, &gpiote_config, pin_event_handler);
103
104 tmp = cfg & GPIO_HAL_PIN_CFG_INT_MASK;
105 if(tmp == GPIO_HAL_PIN_CFG_INT_DISABLE) {
106 nrfx_gpiote_in_event_disable(pin_number);
107 } else if(tmp == GPIO_HAL_PIN_CFG_INT_ENABLE) {
108 nrfx_gpiote_in_event_enable(pin_number, true);
109 }
110}
111/*---------------------------------------------------------------------------*/
114{
115 uint8_t i;
116 uint32_t pin_number;
117 gpio_hal_pin_cfg_t cfg = GPIO_HAL_PIN_CFG_PULL_NONE |
118 GPIO_HAL_PIN_CFG_EDGE_NONE |
119 GPIO_HAL_PIN_CFG_INT_DISABLE;
120 nrf_gpio_pin_pull_t pull;
121 nrf_gpiote_polarity_t polarity;
122
123 pin_number = NRF_GPIO_PIN_MAP(port, pin);
124
125 /* First, check if the pin is configured as output */
126 if(nrf_gpio_pin_dir_get(pin_number) == NRF_GPIO_PIN_DIR_OUTPUT) {
127 return 0;
128 }
129
130 /*
131 * Input pin. Check all GPIOTE channel configurations and figure out which
132 * channel corresponds to our pin of interest. For that channel, read out
133 * the GPIOTE configuration
134 */
135 for(i = 0; i < GPIOTE_CH_NUM; i++) {
136 if(nrf_gpiote_event_pin_get(i) == pin_number) {
137 polarity = nrf_gpiote_event_polarity_get(i);
138
139 if(polarity == NRF_GPIOTE_POLARITY_LOTOHI) {
140 cfg |= GPIO_HAL_PIN_CFG_EDGE_BOTH;
141 } else if(polarity == NRF_GPIOTE_POLARITY_HITOLO) {
142 cfg |= GPIO_HAL_PIN_CFG_EDGE_BOTH;
143 } else if(polarity == NRF_GPIOTE_POLARITY_TOGGLE) {
144 cfg |= GPIO_HAL_PIN_CFG_EDGE_BOTH;
145 }
146
147 pull = nrf_gpio_pin_pull_get(pin_number);
148
149 if(pull == NRF_GPIO_PIN_PULLDOWN) {
150 cfg |= GPIO_HAL_PIN_CFG_PULL_DOWN;
151 } else if(pull == NRF_GPIO_PIN_PULLUP) {
152 cfg |= GPIO_HAL_PIN_CFG_PULL_UP;
153 }
154
155 if(nrf_gpiote_int_is_enabled(1 << i)) {
156 cfg |= GPIO_HAL_PIN_CFG_INT_ENABLE;
157 }
158 return cfg;
159 }
160 }
161
162 /* Did not find a GPIOTE channel configured for this pin */
163 return 0;
164}
165/*---------------------------------------------------------------------------*/
166uint8_t
168 return (uint8_t)nrf_gpio_pin_read(NRF_GPIO_PIN_MAP(port, pin));
169}
170/*---------------------------------------------------------------------------*/
171void
173{
174 NRF_GPIO_Type *gpio_regs[GPIO_COUNT] = GPIO_REG_LIST;
175
176 if(port >= GPIO_COUNT) {
177 return;
178 }
179
180 nrf_gpio_port_out_set(gpio_regs[port], pins);
181}
182/*---------------------------------------------------------------------------*/
183void
185{
186 NRF_GPIO_Type *gpio_regs[GPIO_COUNT] = GPIO_REG_LIST;
187
188 if(port >= GPIO_COUNT) {
189 return;
190 }
191
192 nrf_gpio_port_out_clear(gpio_regs[port], pins);
193}
194/*---------------------------------------------------------------------------*/
195void
197{
198 if(port >= GPIO_COUNT) {
199 return;
200 }
201 gpio_hal_arch_write_pins(port, pins, ~gpio_hal_arch_read_pins(port, pins));
202}
203/*---------------------------------------------------------------------------*/
206{
207 NRF_GPIO_Type *gpio_regs[GPIO_COUNT] = GPIO_REG_LIST;
208
209 if(port >= GPIO_COUNT) {
210 return 0;
211 }
212
213 return nrf_gpio_port_in_read(gpio_regs[port]);
214}
215/*---------------------------------------------------------------------------*/
216void
219{
220 NRF_GPIO_Type *gpio_regs[GPIO_COUNT] = GPIO_REG_LIST;
221
222 if(port >= GPIO_COUNT) {
223 return;
224 }
225
226 nrf_gpio_port_out_write(gpio_regs[port], value);
227}
228/*---------------------------------------------------------------------------*/
229/** @} */
Header file for the GPIO HAL.
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.
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_init(void)
Perform architecture specific gpio initaliaztion.
Definition: gpio-hal-arch.c:99
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.
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_mask_t
GPIO pin mask representation.
Definition: gpio-hal.h:142
uint32_t gpio_hal_pin_cfg_t
GPIO pin configuration.
Definition: gpio-hal.h:118
uint8_t gpio_hal_port_t
A data structure that represents ports.
Definition: gpio-hal.h:110
#define gpio_hal_pin_to_mask(pin)
Convert a pin to a pin mask.
Definition: gpio-hal.h:255
uint8_t gpio_hal_pin_t
GPIO pin number representation.
Definition: gpio-hal.h:103
void gpio_hal_arch_port_clear_pins(gpio_hal_port_t port, gpio_hal_pin_mask_t pins)
Clear multiple pins to logical low.
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.
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_port_toggle_pins(gpio_hal_port_t port, gpio_hal_pin_mask_t pins)
Toggle multiple pins.
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.
uint8_t gpio_hal_arch_port_read_pin(gpio_hal_port_t port, gpio_hal_pin_t pin)
Read a GPIO pin.