Contiki-NG
Loading...
Searching...
No Matches
gpio-hal-arch.c
Go to the documentation of this file.
1/*
2 * Copyright (C) 2022 Yago Fontoura do Rosario <yago.rosario@hotmail.com.br>
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 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 * 3. Neither the name of the copyright holder nor the names of its
14 * contributors may be used to endorse or promote products derived
15 * from this software without specific prior written permission.
16 *
17 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
18 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
19 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
20 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
21 * COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
22 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
23 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
24 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
25 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
26 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
27 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
28 * OF THE POSSIBILITY OF SUCH DAMAGE.
29 */
30
31/**
32 * \addtogroup gecko
33 * @{
34 *
35 * \addtogroup gecko-dev Device drivers
36 * @{
37 *
38 * \addtogroup gecko-gpio GPIO HAL driver
39 * @{
40 *
41 * \file
42 * GPIO HAL implementation for the Gecko
43 * \author
44 * Yago Fontoura do Rosario <yago.rosario@hotmail.com.br>
45 *
46 */
47/*---------------------------------------------------------------------------*/
48#include "contiki.h"
49
50#include "dev/gpio-hal.h"
51
52#include "em_gpio.h"
53#include "em_core.h"
54#include "em_cmu.h"
55#include "gpiointerrupt.h"
56/*---------------------------------------------------------------------------*/
57typedef struct {
58 gpio_hal_port_t port;
61} gpio_hal_cache_t;
62/*---------------------------------------------------------------------------*/
63#define GPIO_HAL_CACHE_SIZE 32
64/*---------------------------------------------------------------------------*/
65static gpio_hal_cache_t gpio_hal_cache[GPIO_HAL_CACHE_SIZE] = { 0 };
66/*---------------------------------------------------------------------------*/
67static void
68gpio_hal_arch_callback(uint8_t interrupt_no, void *ctx)
69{
70 (void)ctx;
71 gpio_hal_event_handler(gpio_hal_cache[interrupt_no].port,
72 1 << gpio_hal_cache[interrupt_no].pin);
73}
74/*---------------------------------------------------------------------------*/
75static uint8_t
76gpio_hal_arch_cache_lookup(gpio_hal_port_t port, gpio_hal_pin_t pin)
77{
78 /* Logic extracted from GPIOINT_CallbackRegisterExt */
79#if defined(_GPIO_EXTIPINSELL_MASK)
80 uint32_t intToCheck;
81 uint32_t intGroupStart = (pin & 0xFFC);
82
83 for(uint8_t i = 0; i < 4; i++) {
84 intToCheck = intGroupStart + ((pin + i) & 0x3);
85 if(gpio_hal_cache[i].port == port &&
86 gpio_hal_cache[i].pin == pin) {
87 return (uint8_t)intToCheck;
88 }
89 }
90#else
91 if(gpioCallbacks[pin].callback == 0) {
92 return (uint8_t)pin;
93 }
94#endif
95 return INTERRUPT_UNAVAILABLE;
96}
97/*---------------------------------------------------------------------------*/
98void
100{
101 CMU_ClockEnable(cmuClock_GPIO, true);
102
103 GPIOINT_Init();
104}
105/*---------------------------------------------------------------------------*/
106void
109{
110 unsigned int interrupt;
111 GPIO_Mode_TypeDef def;
112 def = GPIO_PinModeGet(port, pin);
113
114 GPIO_PinModeSet(port, pin, def, (cfg & GPIO_HAL_PIN_CFG_PULL_UP) ? 1 : 0);
115
116 interrupt = GPIOINT_CallbackRegisterExt(pin,
117 (GPIOINT_IrqCallbackPtrExt_t)gpio_hal_arch_callback,
118 NULL);
119 gpio_hal_cache[interrupt].port = port;
120 gpio_hal_cache[interrupt].pin = pin;
121 gpio_hal_cache[interrupt].cfg = cfg;
122 GPIO_ExtIntConfig(port, pin, interrupt,
123 (cfg & GPIO_HAL_PIN_CFG_EDGE_RISING) != 0,
124 (cfg & GPIO_HAL_PIN_CFG_EDGE_FALLING) != 0,
125 (cfg & GPIO_HAL_PIN_CFG_INT_ENABLE) != 0);
126}
127/*---------------------------------------------------------------------------*/
130{
131 uint8_t index;
132
133 index = gpio_hal_arch_cache_lookup(port, pin);
134 if(index != INTERRUPT_UNAVAILABLE) {
135 return gpio_hal_cache[index].cfg;
136 }
137
138 return 0;
139}
140/*---------------------------------------------------------------------------*/
141void
143{
144 uint8_t index;
145
146 index = gpio_hal_arch_cache_lookup(port, pin);
147 if(index != INTERRUPT_UNAVAILABLE) {
148 GPIO_IntEnable(index);
149 }
150}
151/*---------------------------------------------------------------------------*/
152void
154{
155 uint8_t index;
156
157 index = gpio_hal_arch_cache_lookup(port, pin);
158 if(index != INTERRUPT_UNAVAILABLE) {
159 GPIO_IntDisable(index);
160 }
161}
162/*---------------------------------------------------------------------------*/
163void
165{
166 uint32_t val;
167
168 val = GPIO_PortOutGet(port);
169 val |= pins;
170 GPIO_PortOutSet(port, val);
171}
172/*---------------------------------------------------------------------------*/
173/**
174 * @}
175 * @}
176 * @}
177 */
Header file for the GPIO HAL.
void gpio_hal_arch_port_interrupt_enable(gpio_hal_port_t port, gpio_hal_pin_t pin)
Enable interrupts for a gpio pin.
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_port_interrupt_disable(gpio_hal_port_t port, gpio_hal_pin_t pin)
Disable interrupts for a gpio pin.
void gpio_hal_arch_init(void)
Perform architecture specific gpio initaliaztion.
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.
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
uint8_t gpio_hal_pin_t
GPIO pin number representation.
Definition gpio-hal.h:103