Contiki-NG
Loading...
Searching...
No Matches
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/*---------------------------------------------------------------------------*/
57/**< PIN standard input configuration mimicking IOC_STD_INPUT */
58#define GPIO_HAL_PIN_STD_INPUT (PIN_INPUT_EN | PIN_GPIO_OUTPUT_DIS | \
59 PIN_NOPULL | PIN_DRVSTR_MIN | PIN_IRQ_DIS)
60
61/**< PIN standard input configuration bitmask */
62#define GPIO_HAL_PIN_STD_INPUT_BM (PIN_BM_INPUT_EN | PIN_BM_GPIO_OUTPUT_EN | \
63 PIN_BM_PULLING | PIN_BM_INV_INOUT | \
64 PIN_BM_DRVSTR | PIN_BM_SLEWCTRL | \
65 PIN_BM_HYSTERESIS | PIN_BM_IRQ)
66
67/**< PIN standard input configuration mimicking IOC_STD_OUTPUT */
68#define GPIO_HAL_PIN_STD_OUTPUT (PIN_INPUT_DIS | PIN_GPIO_OUTPUT_EN | \
69 PIN_NOPULL | PIN_DRVSTR_MIN | PIN_IRQ_DIS)
70
71/**< PIN standard output configuration bitmask */
72#define GPIO_HAL_PIN_STD_OUTPUT_BM (PIN_BM_INPUT_EN | PIN_BM_GPIO_OUTPUT_EN | \
73 PIN_BM_PULLING | PIN_BM_INV_INOUT | \
74 PIN_BM_DRVSTR | PIN_BM_SLEWCTRL | \
75 PIN_BM_HYSTERESIS | PIN_BM_IRQ)
76/*---------------------------------------------------------------------------*/
77static void
78from_hal_cfg(gpio_hal_pin_cfg_t cfg, PIN_Config *pin_cfg, PIN_Config *pin_mask)
79{
80 /* Pulling config */
81 *pin_mask |= PIN_BM_PULLING;
82
83 switch(cfg & GPIO_HAL_PIN_CFG_PULL_MASK) {
84 default: /* Default to no pullup/pulldown */
85 case GPIO_HAL_PIN_CFG_PULL_NONE: *pin_cfg |= PIN_NOPULL; break;
86 case GPIO_HAL_PIN_CFG_PULL_UP: *pin_cfg |= PIN_PULLUP; break;
87 case GPIO_HAL_PIN_CFG_PULL_DOWN: *pin_cfg |= PIN_PULLDOWN; break;
88 }
89
90 /* Hysteresis config */
91 *pin_mask |= PIN_BM_HYSTERESIS;
92
93 if(cfg & GPIO_HAL_PIN_CFG_HYSTERESIS) {
94 *pin_cfg |= PIN_HYSTERESIS;
95 }
96
97 /* Interrupt config */
98 *pin_mask |= PIN_BM_IRQ;
99
100 if((cfg & GPIO_HAL_PIN_CFG_INT_MASK) == GPIO_HAL_PIN_CFG_INT_ENABLE) {
101 /* Interrupt edge config */
102 switch(cfg & GPIO_HAL_PIN_CFG_EDGE_BOTH) {
103 case GPIO_HAL_PIN_CFG_EDGE_NONE: *pin_cfg |= PIN_IRQ_DIS; break;
104 case GPIO_HAL_PIN_CFG_EDGE_FALLING: *pin_cfg |= PIN_IRQ_NEGEDGE; break;
105 case GPIO_HAL_PIN_CFG_EDGE_RISING: *pin_cfg |= PIN_IRQ_POSEDGE; break;
106 case GPIO_HAL_PIN_CFG_EDGE_BOTH: *pin_cfg |= PIN_IRQ_BOTHEDGES; break;
107 }
108 } else {
109 *pin_cfg |= PIN_IRQ_DIS;
110 }
111}
112/*---------------------------------------------------------------------------*/
113static void
114to_hal_cfg(PIN_Config pin_cfg, gpio_hal_pin_cfg_t *cfg)
115{
116 /* Input config */
117 if(pin_cfg & PIN_BM_INPUT_MODE) {
118 /* Hysteresis config */
119 if((pin_cfg & PIN_BM_HYSTERESIS) == PIN_BM_HYSTERESIS) {
120 *cfg |= GPIO_HAL_PIN_CFG_HYSTERESIS;
121 }
122
123 /* Pulling config */
124 switch(pin_cfg & (PIN_GEN | PIN_BM_PULLING)) {
125 case PIN_NOPULL: *cfg |= GPIO_HAL_PIN_CFG_PULL_NONE; break;
126 case PIN_PULLUP: *cfg |= GPIO_HAL_PIN_CFG_PULL_UP; break;
127 case PIN_PULLDOWN: *cfg |= GPIO_HAL_PIN_CFG_PULL_DOWN; break;
128 }
129 }
130
131 /* Interrupt config */
132 if(pin_cfg & PIN_BM_IRQ) {
133 /* Interrupt edge config */
134 switch(pin_cfg & (PIN_GEN | PIN_BM_IRQ)) {
135 case PIN_IRQ_DIS: *cfg |= GPIO_HAL_PIN_CFG_EDGE_NONE;
136 *cfg |= GPIO_HAL_PIN_CFG_INT_DISABLE;
137 break;
138 case PIN_IRQ_NEGEDGE: *cfg |= GPIO_HAL_PIN_CFG_EDGE_FALLING;
139 *cfg |= GPIO_HAL_PIN_CFG_INT_ENABLE;
140 break;
141 case PIN_IRQ_POSEDGE: *cfg |= GPIO_HAL_PIN_CFG_EDGE_RISING;
142 *cfg |= GPIO_HAL_PIN_CFG_INT_ENABLE;
143 break;
144 case PIN_IRQ_BOTHEDGES: *cfg |= GPIO_HAL_PIN_CFG_EDGE_BOTH;
145 *cfg |= GPIO_HAL_PIN_CFG_INT_ENABLE;
146 break;
147 }
148 }
149}
150/*---------------------------------------------------------------------------*/
151static void
152gpio_int_cb(PIN_Handle handle, PIN_Id pin_id)
153{
154 /* Unused args */
155 (void)handle;
156
157 /* Notify the GPIO HAL driver */
158 gpio_hal_event_handler(gpio_hal_pin_to_mask(pin_id));
159}
160/*---------------------------------------------------------------------------*/
161void
163{
164 /* No error checking */
165 pin_handle = PIN_open(&pin_state, pin_config);
166 PIN_registerIntCb(pin_handle, gpio_int_cb);
167}
168/*---------------------------------------------------------------------------*/
169
170void
172{
173 PIN_add(pin_handle, PIN_getConfig(pin));
174
175 /* Configure pin as standard input */
176 PIN_Config pin_cfg = GPIO_HAL_PIN_STD_INPUT | pin;
177 PIN_Config pin_mask = GPIO_HAL_PIN_STD_INPUT_BM;
178
179 PIN_setConfig(pin_handle, pin_mask, pin_cfg);
180}
181/*---------------------------------------------------------------------------*/
183{
184 PIN_add(pin_handle, PIN_getConfig(pin));
185
186 /* Configure pin as standard output */
187 PIN_Config pin_cfg = GPIO_HAL_PIN_STD_OUTPUT | pin;
188 PIN_Config pin_mask = GPIO_HAL_PIN_STD_OUTPUT_BM;
189 PIN_setConfig(pin_handle, pin_mask, pin_cfg);
190}
191/*---------------------------------------------------------------------------*/
192void
194{
195 /*
196 * This requires pin cfg already set with GPIO_HAL_PIN_CFG_INT_ENABLE,
197 * i.e. enabled interrupts, thus making this function redundant.
198 * Might be fixed if switching to GPIO or GPIO++ lib. instead of PIN.
199 */
200 PIN_Config pin_cfg;
201 PIN_Config irq_cfg;
202
203 pin_cfg = PIN_getConfig(pin);
204 PIN_add(pin_handle, pin_cfg);
205
206 irq_cfg = pin_cfg & PIN_BM_IRQ;
207 PIN_setInterrupt(pin_handle, pin | irq_cfg);
208}
209/*---------------------------------------------------------------------------*/
210void
212{
213 PIN_add(pin_handle, PIN_getConfig(pin));
214
215 /*
216 * This removes all IRQ config., thus pin cfg must be set again to
217 * re-enable interrupts.
218 */
219 PIN_setInterrupt(pin_handle, pin | PIN_IRQ_DIS);
220}
221/*---------------------------------------------------------------------------*/
222void
224{
225 PIN_add(pin_handle, PIN_getConfig(pin));
226
227 /* Clear settings that we are about to change, keep everything else. */
228 PIN_Config pin_cfg = 0;
229 PIN_Config pin_mask = 0;
230
231 from_hal_cfg(cfg, &pin_cfg, &pin_mask);
232
233 PIN_setConfig(pin_handle, pin_mask, pin | pin_cfg);
234}
235/*---------------------------------------------------------------------------*/
238{
239 PIN_Config pin_cfg = PIN_getConfig(pin);
240 gpio_hal_pin_cfg_t cfg = 0;
241
242 to_hal_cfg(pin_cfg, &cfg);
243
244 return cfg;
245}
246/*---------------------------------------------------------------------------*/
249{
250 /* For pins configured as output we need to read DOUT31_0 */
251 gpio_hal_pin_mask_t oe_pins = GPIO_getOutputEnableMultiDio(pins);
252
253 pins &= ~oe_pins;
254
255 return (HWREG(GPIO_BASE + GPIO_O_DOUT31_0) & oe_pins) |
256 GPIO_readMultiDio(pins);
257}
258/*---------------------------------------------------------------------------*/
259uint8_t
261{
262 return (GPIO_getOutputEnableDio(pin))
263 ? PINCC26XX_getOutputValue(pin)
264 : PINCC26XX_getInputValue(pin);
265}
266/*---------------------------------------------------------------------------*/
267/** @} */
Header file for the GPIO HAL.
#define GPIO_HAL_PIN_STD_OUTPUT
PIN standard output configuration bitmask.
#define GPIO_HAL_PIN_STD_INPUT
< PIN standard input configuration mimicking IOC_STD_INPUT
#define GPIO_HAL_PIN_STD_INPUT_BM
PIN standard input configuration mimicking IOC_STD_OUTPUT.
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.
void gpio_hal_arch_no_port_pin_cfg_set(gpio_hal_pin_t pin, gpio_hal_pin_cfg_t cfg)
Configure a gpio pin.
uint8_t gpio_hal_arch_no_port_read_pin(gpio_hal_pin_t pin)
Read a GPIO pin.
void gpio_hal_arch_init(void)
Perform architecture specific gpio initaliaztion.
void gpio_hal_arch_no_port_pin_set_output(gpio_hal_pin_t pin)
Configure a pin as GPIO output.
void gpio_hal_arch_no_port_pin_set_input(gpio_hal_pin_t pin)
Configure a pin as GPIO input.
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.
uint32_t gpio_hal_pin_cfg_t
GPIO pin configuration.
Definition gpio-hal.h:118
#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.
uint8_t gpio_hal_pin_t
GPIO pin number representation.
Definition gpio-hal.h:103