Contiki-NG
gpio-hal-arch.c
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2017, George Oikonomou - http://www.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 cc2538-gpio-hal
34  * @{
35  *
36  * \file
37  * Implementation file for the CC2538 GPIO HAL functions
38  */
39 /*---------------------------------------------------------------------------*/
40 #include "contiki.h"
41 #include "dev/gpio-hal.h"
42 #include "dev/gpio.h"
43 #include "dev/ioc.h"
44 
45 #include <stdint.h>
46 /*---------------------------------------------------------------------------*/
47 void
49 {
50  uint8_t port, pin_num, pin_mask;
51  uint32_t port_base;
52 
53  port = PIN_TO_PORT(pin);
54  port_base = PIN_TO_PORT_BASE(pin);
55  pin_num = pin % 8;
56  pin_mask = GPIO_PIN_MASK(pin_num);
57 
59 
60  tmp = cfg & GPIO_HAL_PIN_CFG_EDGE_BOTH;
61  if(tmp == GPIO_HAL_PIN_CFG_EDGE_NONE) {
62  GPIO_DISABLE_INTERRUPT(port_base, pin_mask);
63  } else if(tmp == GPIO_HAL_PIN_CFG_EDGE_RISING) {
64  GPIO_DETECT_EDGE(port_base, pin_mask);
65  GPIO_TRIGGER_SINGLE_EDGE(port_base, pin_mask);
66  GPIO_DETECT_RISING(port_base, pin_mask);
67  } else if(tmp == GPIO_HAL_PIN_CFG_EDGE_FALLING) {
68  GPIO_DETECT_EDGE(port_base, pin_mask);
69  GPIO_TRIGGER_SINGLE_EDGE(port_base, pin_mask);
70  GPIO_DETECT_FALLING(port_base, pin_mask);
71  } else if(tmp == GPIO_HAL_PIN_CFG_EDGE_BOTH) {
72  GPIO_DETECT_EDGE(port_base, pin_mask);
73  GPIO_TRIGGER_BOTH_EDGES(port_base, pin_mask);
74  }
75 
76  tmp = cfg & GPIO_HAL_PIN_CFG_PULL_MASK;
77  if(tmp == GPIO_HAL_PIN_CFG_PULL_NONE) {
78  ioc_set_over(port, pin_num, IOC_OVERRIDE_DIS);
79  } else if(tmp == GPIO_HAL_PIN_CFG_PULL_DOWN) {
80  ioc_set_over(port, pin_num, IOC_OVERRIDE_PDE);
81  } else if(tmp == GPIO_HAL_PIN_CFG_PULL_UP) {
82  ioc_set_over(port, pin_num, IOC_OVERRIDE_PUE);
83  }
84 
85  tmp = cfg & GPIO_HAL_PIN_CFG_INT_MASK;
86  if(tmp == GPIO_HAL_PIN_CFG_INT_DISABLE) {
87  GPIO_DISABLE_INTERRUPT(port_base, pin_mask);
88  } else if(tmp == GPIO_HAL_PIN_CFG_INT_ENABLE) {
89  GPIO_ENABLE_INTERRUPT(port_base, pin_mask);
90  NVIC_EnableIRQ(port);
91  }
92 
93  GPIO_SOFTWARE_CONTROL(port_base, pin_mask);
94 }
95 /*---------------------------------------------------------------------------*/
98 {
99  uint8_t port, pin_num, pin_mask;
100  uint32_t port_base;
101  gpio_hal_pin_cfg_t cfg;
102  uint32_t tmp;
103 
104  port = PIN_TO_PORT(pin);
105  port_base = PIN_TO_PORT_BASE(pin);
106  pin_num = pin % 8;
107  pin_mask = GPIO_PIN_MASK(pin_num);
108 
109  cfg = 0;
110 
111  /* Pull */
112  tmp = ioc_get_over(port, pin_num);
113  if(tmp == IOC_OVERRIDE_PUE) {
114  cfg |= GPIO_HAL_PIN_CFG_PULL_UP;
115  } else if(tmp == IOC_OVERRIDE_PDE) {
116  cfg |= GPIO_HAL_PIN_CFG_PULL_DOWN;
117  } else {
118  cfg |= GPIO_HAL_PIN_CFG_PULL_NONE;
119  }
120 
121  /* Interrupt enable/disable */
122  tmp = REG((port_base) + GPIO_IE) & pin_mask;
123  if(tmp == 0) {
124  cfg |= GPIO_HAL_PIN_CFG_INT_DISABLE;
125  } else {
126  cfg |= GPIO_HAL_PIN_CFG_INT_ENABLE;
127  }
128 
129  /* Edge detection */
130  if(REG((port_base) + GPIO_IS) & pin_mask) {
131  cfg |= GPIO_HAL_PIN_CFG_EDGE_NONE;
132  } else {
133  if(REG((port_base) + GPIO_IBE) & pin_mask) {
134  cfg |= GPIO_HAL_PIN_CFG_EDGE_BOTH;
135  } else {
136  if(REG((port_base) + GPIO_IEV) & pin_mask) {
137  cfg |= GPIO_HAL_PIN_CFG_EDGE_RISING;
138  } else {
139  cfg |= GPIO_HAL_PIN_CFG_EDGE_FALLING;
140  }
141  }
142  }
143 
144  return cfg;
145 }
146 /*---------------------------------------------------------------------------*/
147 void
149 {
150  if(value == 1) {
151  gpio_hal_arch_set_pin(GPIO_HAL_NULL_PORT, pin);
152  return;
153  }
154  gpio_hal_arch_clear_pin(GPIO_HAL_NULL_PORT, pin);
155 }
156 /*---------------------------------------------------------------------------*/
157 void
159 {
160  GPIO_SET_PIN(GPIO_A_BASE, pins & 0xFF);
161  GPIO_SET_PIN(GPIO_B_BASE, (pins >> 8) & 0xFF);
162  GPIO_SET_PIN(GPIO_C_BASE, (pins >> 16) & 0xFF);
163  GPIO_SET_PIN(GPIO_D_BASE, (pins >> 24) & 0xFF);
164 }
165 /*---------------------------------------------------------------------------*/
166 void
168 {
169  GPIO_CLR_PIN(GPIO_A_BASE, pins & 0xFF);
170  GPIO_CLR_PIN(GPIO_B_BASE, (pins >> 8) & 0xFF);
171  GPIO_CLR_PIN(GPIO_C_BASE, (pins >> 16) & 0xFF);
172  GPIO_CLR_PIN(GPIO_D_BASE, (pins >> 24) & 0xFF);
173 }
174 /*---------------------------------------------------------------------------*/
177 {
178  gpio_hal_pin_mask_t rv = 0;
179 
180  rv |= GPIO_READ_PIN(GPIO_A_BASE, pins & 0xFF);
181  rv |= GPIO_READ_PIN(GPIO_B_BASE, (pins >> 8) & 0xFF) << 8;
182  rv |= GPIO_READ_PIN(GPIO_C_BASE, (pins >> 16) & 0xFF) << 16;
183  rv |= GPIO_READ_PIN(GPIO_D_BASE, (pins >> 24) & 0xFF) << 24;
184 
185  return rv;
186 }
187 /*---------------------------------------------------------------------------*/
188 void
190  gpio_hal_pin_mask_t value)
191 {
192  GPIO_WRITE_PIN(GPIO_A_BASE, pins & 0xFF, value & 0xFF);
193  GPIO_WRITE_PIN(GPIO_B_BASE, (pins >> 8) & 0xFF, (value >> 8) & 0xFF);
194  GPIO_WRITE_PIN(GPIO_C_BASE, (pins >> 16) & 0xFF, (value >> 16) & 0xFF);
195  GPIO_WRITE_PIN(GPIO_D_BASE, (pins >> 24) & 0xFF, (value >> 24) & 0xFF);
196 }
197 /*---------------------------------------------------------------------------*/
198 /** @} */
#define GPIO_ENABLE_INTERRUPT(PORT_BASE, PIN_MASK)
Enable interrupt triggering for pins with PIN_MASK of port with PORT_BASE.
Definition: gpio.h:201
#define GPIO_C_BASE
GPIO_C.
Definition: gpio.h:57
void gpio_hal_arch_no_port_write_pin(gpio_hal_pin_t pin, uint8_t value)
Write a GPIO pin.
uint32_t ioc_get_over(uint8_t port, uint8_t pin)
Get Port:Pin override function.
Definition: ioc.c:60
#define GPIO_TRIGGER_BOTH_EDGES(PORT_BASE, PIN_MASK)
Set pins with PIN_MASK of port with PORT_BASE to trigger an interrupt on both edges.
Definition: gpio.h:169
#define GPIO_SET_PIN(PORT_BASE, PIN_MASK)
Set pins with PIN_MASK of port with PORT_BASE high.
Definition: gpio.h:106
#define GPIO_WRITE_PIN(PORT_BASE, PIN_MASK, value)
Set pins with PIN_MASK of port with PORT_BASE to value.
Definition: gpio.h:134
Header file with register and macro declarations for the cc2538 GPIO module.
#define GPIO_DETECT_EDGE(PORT_BASE, PIN_MASK)
Set pins with PIN_MASK of port with PORT_BASE to detect edge.
Definition: gpio.h:154
#define GPIO_CLR_PIN(PORT_BASE, PIN_MASK)
Set pins with PIN_MASK of port with PORT_BASE low.
Definition: gpio.h:113
Header file with declarations for the I/O Control module.
#define IOC_OVERRIDE_DIS
Override Disabled.
Definition: ioc.h:226
#define GPIO_DETECT_RISING(PORT_BASE, PIN_MASK)
Set pins with PIN_MASK of port with PORT_BASE to trigger an interrupt on rising edge.
Definition: gpio.h:185
void gpio_hal_arch_no_port_set_pins(gpio_hal_pin_mask_t pins)
Set multiple pins to logical high.
#define GPIO_PIN_MASK(PIN)
Converts a pin number to a pin mask.
Definition: gpio.h:320
#define GPIO_DETECT_FALLING(PORT_BASE, PIN_MASK)
Set pins with PIN_MASK of port with PORT_BASE to trigger an interrupt on falling edge.
Definition: gpio.h:193
#define GPIO_READ_PIN(PORT_BASE, PIN_MASK)
Read pins with PIN_MASK of port with PORT_BASE.
Definition: gpio.h:147
__STATIC_INLINE void NVIC_EnableIRQ(IRQn_Type IRQn)
Enable External Interrupt.
Definition: core_cm0.h:642
#define GPIO_DISABLE_INTERRUPT(PORT_BASE, PIN_MASK)
Disable interrupt triggering for pins with PIN_MASK of port with PORT_BASE.
Definition: gpio.h:209
#define GPIO_SOFTWARE_CONTROL(PORT_BASE, PIN_MASK)
Configure the pin to be software controlled with PIN_MASK of port with PORT_BASE. ...
Definition: gpio.h:258
void gpio_hal_arch_no_port_write_pins(gpio_hal_pin_mask_t pins, gpio_hal_pin_mask_t value)
Write multiple pins.
#define GPIO_IS
Interrupt sense.
Definition: gpio.h:344
#define GPIO_IEV
Interrupt event.
Definition: gpio.h:346
#define GPIO_B_BASE
GPIO_B.
Definition: gpio.h:56
void ioc_set_over(uint8_t port, uint8_t pin, uint8_t over)
Set Port:Pin override function.
Definition: ioc.c:54
gpio_hal_pin_mask_t gpio_hal_arch_no_port_read_pins(gpio_hal_pin_mask_t pins)
Read multiple pins.
void gpio_hal_arch_no_port_clear_pins(gpio_hal_pin_mask_t pins)
Clear multiple pins to logical low.
uint8_t gpio_hal_pin_t
GPIO pin number representation.
Definition: gpio-hal.h:103
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
#define GPIO_D_BASE
GPIO_D.
Definition: gpio.h:58
#define GPIO_A_BASE
GPIO_A.
Definition: gpio.h:55
uint32_t gpio_hal_pin_mask_t
GPIO pin mask representation.
Definition: gpio-hal.h:142
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
#define IOC_OVERRIDE_PDE
Pull Down Enable.
Definition: ioc.h:224
#define IOC_OVERRIDE_PUE
Pull Up Enable.
Definition: ioc.h:223
#define GPIO_TRIGGER_SINGLE_EDGE(PORT_BASE, PIN_MASK)
Set pins with PIN_MASK of port with PORT_BASE to trigger an interrupt on single edge (controlled by G...
Definition: gpio.h:177
#define GPIO_IE
Interrupt mask.
Definition: gpio.h:347
#define GPIO_IBE
Interrupt both edges.
Definition: gpio.h:345
Header file for the GPIO HAL.
uint32_t gpio_hal_pin_cfg_t
GPIO pin configuration.
Definition: gpio-hal.h:118
#define GPIO_HAL_NULL_PORT
Convenience macro to use this as the port argument of macros.
Definition: gpio-hal.h:98