Contiki-NG
gpio-hal-arch.c
1 /*
2  * Copyright (c) 2018, 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 #include "sys/log.h"
35 
36 #include <stdint.h>
37 /*---------------------------------------------------------------------------*/
38 /* Log configuration */
39 #define LOG_MODULE "GPIO arch"
40 #define LOG_LEVEL LOG_LEVEL_NONE
41 /*---------------------------------------------------------------------------*/
43 static uint8_t pin_state[GPIO_HAL_PIN_COUNT];
44 /*---------------------------------------------------------------------------*/
45 void
47 {
48  /* Do nothing */
49 }
50 /*---------------------------------------------------------------------------*/
51 void
53 {
54  if(pin >= GPIO_HAL_PIN_COUNT) {
55  LOG_ERR("Pin %u out of bounds\n", pin);
56  return;
57  }
58 
59  LOG_DBG("Pin %u: Enabled interrupt\n", pin);
60 }
61 /*---------------------------------------------------------------------------*/
62 void
64 {
65  if(pin >= GPIO_HAL_PIN_COUNT) {
66  LOG_ERR("Pin %u out of bounds\n", pin);
67  return;
68  }
69 
70  LOG_DBG("Pin %u: Disabled interrupt\n", pin);
71 }
72 /*---------------------------------------------------------------------------*/
73 void
75 {
76  if(pin >= GPIO_HAL_PIN_COUNT) {
77  LOG_ERR("Pin %u out of bounds\n", pin);
78  return;
79  }
80 
81  pin_cfg[pin] = cfg;
82  LOG_DBG("Pin %u: Set config=0x%02x\n", pin, pin_cfg[pin]);
83 }
84 /*---------------------------------------------------------------------------*/
87 {
88  if(pin >= GPIO_HAL_PIN_COUNT) {
89  LOG_ERR("Pin %u out of bounds\n", pin);
90  return 0;
91  }
92 
93  LOG_DBG("Pin %u: Config=0x%02x\n", pin, pin_cfg[pin]);
94  return pin_cfg[pin];
95 }
96 /*---------------------------------------------------------------------------*/
97 void
99 {
100  if(pin >= GPIO_HAL_PIN_COUNT) {
101  LOG_ERR("Pin %u out of bounds\n", pin);
102  return;
103  }
104 
105  LOG_DBG("Pin %u: Set input\n", pin);
106 }
107 /*---------------------------------------------------------------------------*/
108 void
110 {
111  if(pin >= GPIO_HAL_PIN_COUNT) {
112  LOG_ERR("Pin %u out of bounds\n", pin);
113  return;
114  }
115 
116  LOG_DBG("Pin %u: Set output\n", pin);
117 }
118 /*---------------------------------------------------------------------------*/
119 void
121 {
122  if(pin >= GPIO_HAL_PIN_COUNT) {
123  LOG_ERR("Pin %u out of bounds\n", pin);
124  return;
125  }
126 
127  pin_state[pin] = 1;
128  LOG_DBG("Pin %u: Set\n", pin);
129 }
130 /*---------------------------------------------------------------------------*/
131 void
133 {
134  if(pin >= GPIO_HAL_PIN_COUNT) {
135  LOG_ERR("Pin %u out of bounds\n", pin);
136  return;
137  }
138 
139  pin_state[pin] = 0;
140  LOG_DBG("Pin %u: Clear\n", pin);
141 }
142 /*---------------------------------------------------------------------------*/
143 uint8_t
145 {
146  if(pin >= GPIO_HAL_PIN_COUNT) {
147  LOG_ERR("Pin %u out of bounds\n", pin);
148  return 0;
149  }
150 
151  LOG_DBG("Pin %u: Read=%u\n", pin, pin_state[pin]);
152  return pin_state[pin];
153 }
154 /*---------------------------------------------------------------------------*/
155 void
157 {
158  if(pin >= GPIO_HAL_PIN_COUNT) {
159  LOG_ERR("Pin %u out of bounds\n", pin);
160  return;
161  }
162 
163  pin_state[pin] = value;
164  LOG_DBG("Pin %u: Write=%u\n", pin, pin_state[pin]);
165 }
166 /*---------------------------------------------------------------------------*/
167 void
169 {
170  gpio_hal_pin_t pin;
171 
172  for(pin = 0; pin < GPIO_HAL_PIN_COUNT; pin++) {
173  if(pins & (1 << pin)) {
174  pin_state[pin] = 1;
175  }
176  }
177 
178  LOG_DBG("Set pins 0x%08" PRIx32 "\n", pins);
179 }
180 /*---------------------------------------------------------------------------*/
181 void
183 {
184  gpio_hal_pin_t pin;
185 
186  for(pin = 0; pin < GPIO_HAL_PIN_COUNT; pin++) {
187  if(pins & (1 << pin)) {
188  pin_state[pin] = 0;
189  }
190  }
191 
192  LOG_DBG("Clear pins 0x%08" PRIx32 "\n", pins);
193 }
194 /*---------------------------------------------------------------------------*/
197 {
198  gpio_hal_pin_t pin;
199  gpio_hal_pin_mask_t state = 0;
200 
201  for(pin = 0; pin < GPIO_HAL_PIN_COUNT; pin++) {
202  state |= (pin_state[pin] << pin);
203  }
204 
205  LOG_DBG("Read pins 0x%08" PRIx32 "\n", state);
206  return state;
207 }
208 /*---------------------------------------------------------------------------*/
209 void
211  gpio_hal_pin_mask_t value)
212 {
213  gpio_hal_pin_t pin;
214 
215  for(pin = 0; pin < GPIO_HAL_PIN_COUNT; pin++) {
216  if(pins & (1 << pin)) {
217  pin_state[pin] = (value & (1 << pin)) == 0 ? 0 : 1;
218  }
219  }
220 
221  LOG_DBG("Write pins 0x%08" PRIx32 "->0x%08" PRIx32 "\n", pins, value);
222 }
223 /*---------------------------------------------------------------------------*/
void gpio_hal_arch_init(void)
Perform architecture specific gpio initaliaztion.
Definition: gpio-hal-arch.c:46
void gpio_hal_arch_no_port_set_pin(gpio_hal_pin_t pin)
Set a GPIO pin to logical high.
void gpio_hal_arch_no_port_write_pin(gpio_hal_pin_t pin, uint8_t value)
Write a GPIO pin.
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
void gpio_hal_arch_no_port_set_pins(gpio_hal_pin_mask_t pins)
Set multiple pins to logical high.
#define GPIO_HAL_PIN_COUNT
Specifies the total number of pins on a device.
Definition: gpio-hal.h:128
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_pin_set_output(gpio_hal_pin_t pin)
Configure a pin as GPIO output.
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
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
void gpio_hal_arch_no_port_clear_pin(gpio_hal_pin_t pin)
Clear a GPIO pin (logical low)
uint8_t gpio_hal_arch_no_port_read_pin(gpio_hal_pin_t pin)
Read a GPIO pin.
Header file for the GPIO HAL.
Header file for the logging system
void gpio_hal_arch_no_port_pin_set_input(gpio_hal_pin_t pin)
Configure a pin as GPIO input.
Definition: gpio-hal-arch.c:98
uint32_t gpio_hal_pin_cfg_t
GPIO pin configuration.
Definition: gpio-hal.h:118
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