Contiki-NG
Loading...
Searching...
No Matches
gpio-hal-arch.c
1/*
2 * Copyright (c) 2019, 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#include "contiki.h"
33#include "dev/gpio-hal.h"
34
35#include <string.h>
36#include <stdint.h>
37/*---------------------------------------------------------------------------*/
38/* Log configuration */
39#include "sys/log.h"
40#define LOG_MODULE "GPIO arch"
41#define LOG_LEVEL LOG_LEVEL_NONE
42/*---------------------------------------------------------------------------*/
44static uint8_t pin_state[GPIO_HAL_PIN_COUNT];
45/*---------------------------------------------------------------------------*/
46/*
47 * Callbacks used to notify other modules (e.g. leds_arch) that the state of
48 * a pin has changed. Mainly used for output pins, since input pins can always
49 * simply generate a virtual interrupt
50 */
51static void (*pin_callbacks[GPIO_HAL_PIN_COUNT])(void);
52/*---------------------------------------------------------------------------*/
53void
54gpio_hal_arch_register_pin_callback(gpio_hal_pin_t pin,
55 gpio_hal_arch_pin_callback cb)
56{
57 if(pin >= GPIO_HAL_PIN_COUNT) {
58 return;
59 }
60 pin_callbacks[pin] = cb;
61}
62/*---------------------------------------------------------------------------*/
63static void
64set_pin_state(gpio_hal_pin_t pin, uint8_t state)
65{
66 if(pin >= GPIO_HAL_PIN_COUNT) {
67 return;
68 }
69 pin_state[pin] = state;
70
71 if(pin_callbacks[pin] != NULL) {
72 pin_callbacks[pin]();
73 }
74}
75/*---------------------------------------------------------------------------*/
76void
78{
79 memset(pin_cfg, 0, sizeof(pin_cfg));
80 memset(pin_state, 0, sizeof(pin_state));
81 memset(pin_callbacks, 0, sizeof(pin_callbacks));
82}
83/*---------------------------------------------------------------------------*/
84void
86{
87 if(pin >= GPIO_HAL_PIN_COUNT) {
88 LOG_ERR("Pin %u out of bounds\n", pin);
89 return;
90 }
91
92 pin_cfg[pin] |= GPIO_HAL_PIN_CFG_INT_ENABLE;
93
94 LOG_DBG("Pin %u: Enabled interrupt\n", pin);
95}
96/*---------------------------------------------------------------------------*/
97void
99{
100 if(pin >= GPIO_HAL_PIN_COUNT) {
101 LOG_ERR("Pin %u out of bounds\n", pin);
102 return;
103 }
104
105 pin_cfg[pin] &= ~GPIO_HAL_PIN_CFG_INT_ENABLE;
106
107 LOG_DBG("Pin %u: Disabled interrupt\n", pin);
108}
109/*---------------------------------------------------------------------------*/
110void
112{
113 if(pin >= GPIO_HAL_PIN_COUNT) {
114 LOG_ERR("Pin %u out of bounds\n", pin);
115 return;
116 }
117
118 pin_cfg[pin] = cfg;
119 LOG_DBG("Pin %u: Set config=0x%02x\n", pin, pin_cfg[pin]);
120}
121/*---------------------------------------------------------------------------*/
124{
125 if(pin >= GPIO_HAL_PIN_COUNT) {
126 LOG_ERR("Pin %u out of bounds\n", pin);
127 return 0;
128 }
129
130 LOG_DBG("Pin %u: Config=0x%02x\n", pin, pin_cfg[pin]);
131 return pin_cfg[pin];
132}
133/*---------------------------------------------------------------------------*/
134void
136{
137 if(pin >= GPIO_HAL_PIN_COUNT) {
138 LOG_ERR("Pin %u out of bounds\n", pin);
139 return;
140 }
141
142 LOG_DBG("Pin %u: Set input\n", pin);
143}
144/*---------------------------------------------------------------------------*/
145void
147{
148 if(pin >= GPIO_HAL_PIN_COUNT) {
149 LOG_ERR("Pin %u out of bounds\n", pin);
150 return;
151 }
152
153 LOG_DBG("Pin %u: Set output\n", pin);
154}
155/*---------------------------------------------------------------------------*/
156void
158{
159 set_pin_state(pin, 1);
160 LOG_DBG("Pin %u: Set\n", pin);
161}
162/*---------------------------------------------------------------------------*/
163void
165{
166 set_pin_state(pin, 0);
167 LOG_DBG("Pin %u: Clear\n", pin);
168}
169/*---------------------------------------------------------------------------*/
170uint8_t
172{
173 if(pin >= GPIO_HAL_PIN_COUNT) {
174 LOG_ERR("Pin %u out of bounds\n", pin);
175 return 0;
176 }
177
178 LOG_DBG("Pin %u: Read=%u\n", pin, pin_state[pin]);
179 return pin_state[pin];
180}
181/*---------------------------------------------------------------------------*/
182void
184{
185 set_pin_state(pin, value);
186 LOG_DBG("Pin %u: Write=%u\n", pin, pin_state[pin]);
187}
188/*---------------------------------------------------------------------------*/
189void
191{
192 gpio_hal_pin_t pin;
193
194 for(pin = 0; pin < GPIO_HAL_PIN_COUNT; pin++) {
195 if(pins & (1 << pin)) {
196 set_pin_state(pin, 1);
197 }
198 }
199
200 LOG_DBG("Set pins 0x%08" PRIx32 "\n", pins);
201}
202/*---------------------------------------------------------------------------*/
203void
205{
206 gpio_hal_pin_t pin;
207
208 for(pin = 0; pin < GPIO_HAL_PIN_COUNT; pin++) {
209 if(pins & (1 << pin)) {
210 set_pin_state(pin, 0);
211 }
212 }
213
214 LOG_DBG("Clear pins 0x%08" PRIx32 "\n", pins);
215}
216/*---------------------------------------------------------------------------*/
219{
220 gpio_hal_pin_t pin;
221 gpio_hal_pin_mask_t state = 0;
222
223 for(pin = 0; pin < GPIO_HAL_PIN_COUNT; pin++) {
224 state |= (pin_state[pin] << pin);
225 }
226
227 LOG_DBG("Read pins 0x%08" PRIx32 "\n", state);
228 return state;
229}
230/*---------------------------------------------------------------------------*/
231void
234{
235 gpio_hal_pin_t pin;
236
237 for(pin = 0; pin < GPIO_HAL_PIN_COUNT; pin++) {
238 if(pins & (1 << pin)) {
239 set_pin_state(pin, (value & (1 << pin)) == 0 ? 0 : 1);
240 }
241 }
242
243 LOG_DBG("Write pins 0x%08" PRIx32 "->0x%08" PRIx32 "\n", pins, value);
244}
245/*---------------------------------------------------------------------------*/
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.
void gpio_hal_arch_no_port_write_pins(gpio_hal_pin_mask_t pins, gpio_hal_pin_mask_t value)
Write multiple pins.
void gpio_hal_arch_no_port_set_pins(gpio_hal_pin_mask_t pins)
Set multiple pins to logical high.
void gpio_hal_arch_no_port_pin_cfg_set(gpio_hal_pin_t pin, gpio_hal_pin_cfg_t cfg)
Configure a gpio pin.
void gpio_hal_arch_no_port_clear_pins(gpio_hal_pin_mask_t pins)
Clear multiple pins to logical low.
void gpio_hal_arch_no_port_write_pin(gpio_hal_pin_t pin, uint8_t value)
Write 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_clear_pin(gpio_hal_pin_t pin)
Clear a GPIO pin (logical low)
void gpio_hal_arch_no_port_interrupt_disable(gpio_hal_pin_t pin)
Disable interrupts for a gpio pin.
void gpio_hal_arch_no_port_set_pin(gpio_hal_pin_t pin)
Set a GPIO pin to logical high.
uint32_t gpio_hal_pin_cfg_t
GPIO pin configuration.
Definition gpio-hal.h:118
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
#define GPIO_HAL_PIN_COUNT
Specifies the total number of pins on a device.
Definition gpio-hal.h:128
Header file for the logging system.