Contiki-NG
gpio-hal-arch.c
Go to the documentation of this file.
1/*
2 * Copyright (c) 2018, Texas Instruments Incorporated - http://www.ti.com/
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 * \addtogroup cc13xx-cc26xx-gpio-hal
32 * @{
33 *
34 * \file
35 * Implementation of the GPIO HAL module for CC13xx/CC26xx. The GPIO
36 * HAL module is implemented by using the PINCC26XX module, except
37 * for multi-dio functions which use the GPIO driverlib module.
38 * \author
39 * Edvard Pettersen <e.pettersen@ti.com>
40 */
41/*---------------------------------------------------------------------------*/
42#include "contiki.h"
43#include "dev/gpio-hal.h"
44/*---------------------------------------------------------------------------*/
45#include <ti/devices/DeviceFamily.h>
46#include DeviceFamily_constructPath(driverlib/gpio.h)
47
48#include <ti/drivers/PIN.h>
49#include <ti/drivers/pin/PINCC26XX.h>
50/*---------------------------------------------------------------------------*/
51#include <stdint.h>
52/*---------------------------------------------------------------------------*/
53static PIN_Config pin_config[] = { PIN_TERMINATE };
54static PIN_State pin_state;
55static PIN_Handle pin_handle;
56/*---------------------------------------------------------------------------*/
57static void
58from_hal_cfg(gpio_hal_pin_cfg_t cfg, PIN_Config *pin_cfg, PIN_Config *pin_mask)
59{
60 /* Pulling config */
61 *pin_mask |= PIN_BM_PULLING;
62
63 switch(cfg & GPIO_HAL_PIN_CFG_PULL_MASK) {
64 default: /* Default to no pullup/pulldown */
65 case GPIO_HAL_PIN_CFG_PULL_NONE: *pin_cfg |= PIN_NOPULL; break;
66 case GPIO_HAL_PIN_CFG_PULL_UP: *pin_cfg |= PIN_PULLUP; break;
67 case GPIO_HAL_PIN_CFG_PULL_DOWN: *pin_cfg |= PIN_PULLDOWN; break;
68 }
69
70 /* Hysteresis config */
71 *pin_mask |= PIN_BM_HYSTERESIS;
72
73 if(cfg & GPIO_HAL_PIN_CFG_HYSTERESIS) {
74 *pin_cfg |= PIN_HYSTERESIS;
75 }
76
77 /* Interrupt config */
78 *pin_mask |= PIN_BM_IRQ;
79
80 if((cfg & GPIO_HAL_PIN_CFG_INT_MASK) == GPIO_HAL_PIN_CFG_INT_ENABLE) {
81 /* Interrupt edge config */
82 switch(cfg & GPIO_HAL_PIN_CFG_EDGE_BOTH) {
83 case GPIO_HAL_PIN_CFG_EDGE_NONE: *pin_cfg |= PIN_IRQ_DIS; break;
84 case GPIO_HAL_PIN_CFG_EDGE_FALLING: *pin_cfg |= PIN_IRQ_NEGEDGE; break;
85 case GPIO_HAL_PIN_CFG_EDGE_RISING: *pin_cfg |= PIN_IRQ_POSEDGE; break;
86 case GPIO_HAL_PIN_CFG_EDGE_BOTH: *pin_cfg |= PIN_IRQ_BOTHEDGES; break;
87 }
88 } else {
89 *pin_cfg |= PIN_IRQ_DIS;
90 }
91}
92/*---------------------------------------------------------------------------*/
93static void
94to_hal_cfg(PIN_Config pin_cfg, gpio_hal_pin_cfg_t *cfg)
95{
96 /* Input config */
97 if(pin_cfg & PIN_BM_INPUT_MODE) {
98 /* Hysteresis config */
99 if((pin_cfg & PIN_BM_HYSTERESIS) == PIN_BM_HYSTERESIS) {
100 *cfg |= GPIO_HAL_PIN_CFG_HYSTERESIS;
101 }
102
103 /* Pulling config */
104 switch(pin_cfg & PIN_BM_PULLING) {
105 case PIN_NOPULL: *cfg |= GPIO_HAL_PIN_CFG_PULL_NONE; break;
106 case PIN_PULLUP: *cfg |= GPIO_HAL_PIN_CFG_PULL_UP; break;
107 case PIN_PULLDOWN: *cfg |= GPIO_HAL_PIN_CFG_PULL_DOWN; break;
108 }
109 }
110
111 /* Interrupt config */
112 if(pin_cfg & PIN_BM_IRQ) {
113 /* Interrupt edge config */
114 switch(pin_cfg & PIN_BM_IRQ) {
115 case PIN_IRQ_DIS: *cfg |= GPIO_HAL_PIN_CFG_EDGE_NONE;
116 *cfg |= GPIO_HAL_PIN_CFG_INT_DISABLE;
117 break;
118 case PIN_IRQ_NEGEDGE: *cfg |= GPIO_HAL_PIN_CFG_EDGE_FALLING;
119 *cfg |= GPIO_HAL_PIN_CFG_INT_ENABLE;
120 break;
121 case PIN_IRQ_POSEDGE: *cfg |= GPIO_HAL_PIN_CFG_EDGE_RISING;
122 *cfg |= GPIO_HAL_PIN_CFG_INT_ENABLE;
123 break;
124 case PIN_IRQ_BOTHEDGES: *cfg |= GPIO_HAL_PIN_CFG_EDGE_BOTH;
125 *cfg |= GPIO_HAL_PIN_CFG_INT_ENABLE;
126 break;
127 }
128 }
129}
130/*---------------------------------------------------------------------------*/
131static void
132gpio_int_cb(PIN_Handle handle, PIN_Id pin_id)
133{
134 /* Unused args */
135 (void)handle;
136
137 /* Notify the GPIO HAL driver */
139}
140/*---------------------------------------------------------------------------*/
141void
143{
144 /* No error checking */
145 pin_handle = PIN_open(&pin_state, pin_config);
146 PIN_registerIntCb(pin_handle, gpio_int_cb);
147}
148/*---------------------------------------------------------------------------*/
149void
151{
152 PIN_Config pin_cfg;
153 PIN_Config irq_cfg;
154
155 pin_cfg = PIN_getConfig(pin);
156 PIN_add(pin_handle, pin_cfg);
157
158 irq_cfg = pin_cfg & PIN_BM_IRQ;
159 PIN_setInterrupt(pin_handle, pin | irq_cfg);
160}
161/*---------------------------------------------------------------------------*/
162void
164{
165 PIN_add(pin_handle, PIN_getConfig(pin));
166
167 PIN_setInterrupt(pin_handle, pin | PIN_IRQ_DIS);
168}
169/*---------------------------------------------------------------------------*/
170void
172{
173 PIN_add(pin_handle, PIN_getConfig(pin));
174
175 /* Clear settings that we are about to change, keep everything else. */
176 PIN_Config pin_cfg = 0;
177 PIN_Config pin_mask = 0;
178
179 from_hal_cfg(cfg, &pin_cfg, &pin_mask);
180
181 PIN_setConfig(pin_handle, pin_mask, pin | pin_cfg);
182}
183/*---------------------------------------------------------------------------*/
186{
187 PIN_Config pin_cfg = PIN_getConfig(pin);
188 gpio_hal_pin_cfg_t cfg = 0;
189
190 to_hal_cfg(pin_cfg, &cfg);
191
192 return cfg;
193}
194/*---------------------------------------------------------------------------*/
197{
198 /* For pins configured as output we need to read DOUT31_0 */
199 gpio_hal_pin_mask_t oe_pins = GPIO_getOutputEnableMultiDio(pins);
200
201 pins &= ~oe_pins;
202
203 return (HWREG(GPIO_BASE + GPIO_O_DOUT31_0) & oe_pins) |
204 GPIO_readMultiDio(pins);
205}
206/*---------------------------------------------------------------------------*/
207uint8_t
209{
210 return (GPIO_getOutputEnableDio(pin))
211 ? PINCC26XX_getOutputValue(pin)
212 : PINCC26XX_getInputValue(pin);
213}
214/*---------------------------------------------------------------------------*/
215/** @} */
Header file for the GPIO HAL.
gpio_hal_pin_mask_t gpio_hal_arch_no_port_read_pins(gpio_hal_pin_mask_t pins)
Read multiple pins.
gpio_hal_pin_cfg_t gpio_hal_arch_no_port_pin_cfg_get(gpio_hal_pin_t pin)
Read the configuration of a GPIO pin.
Definition: gpio-hal-arch.c:97
void gpio_hal_arch_no_port_pin_cfg_set(gpio_hal_pin_t pin, gpio_hal_pin_cfg_t cfg)
Configure a gpio pin.
Definition: gpio-hal-arch.c:48
uint8_t gpio_hal_arch_no_port_read_pin(gpio_hal_pin_t pin)
Read 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
void gpio_hal_arch_no_port_interrupt_disable(gpio_hal_pin_t pin)
Disable interrupts for a gpio pin.
Definition: gpio-hal-arch.c:63
uint32_t gpio_hal_pin_cfg_t
GPIO pin configuration.
Definition: gpio-hal.h:118
void gpio_hal_arch_init(void)
Perform architecture specific gpio initaliaztion.
Definition: gpio-hal-arch.c:46
#define gpio_hal_pin_to_mask(pin)
Convert a pin to a pin mask.
Definition: gpio-hal.h:255
void gpio_hal_arch_no_port_interrupt_enable(gpio_hal_pin_t pin)
Enable interrupts for a gpio pin.
Definition: gpio-hal-arch.c:52
uint8_t gpio_hal_pin_t
GPIO pin number representation.
Definition: gpio-hal.h:103