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 /*---------------------------------------------------------------------------*/
42 static gpio_hal_pin_cfg_t pin_cfg[GPIO_HAL_PIN_COUNT];
43 static uint8_t pin_state[GPIO_HAL_PIN_COUNT];
44 /*---------------------------------------------------------------------------*/
45 void
47 {
48  if(pin >= GPIO_HAL_PIN_COUNT) {
49  LOG_ERR("Pin %u out of bounds\n", pin);
50  return;
51  }
52 
53  LOG_DBG("Pin %u: Enabled interrupt\n", pin);
54 }
55 /*---------------------------------------------------------------------------*/
56 void
58 {
59  if(pin >= GPIO_HAL_PIN_COUNT) {
60  LOG_ERR("Pin %u out of bounds\n", pin);
61  return;
62  }
63 
64  LOG_DBG("Pin %u: Disabled interrupt\n", pin);
65 }
66 /*---------------------------------------------------------------------------*/
67 void
69 {
70  if(pin >= GPIO_HAL_PIN_COUNT) {
71  LOG_ERR("Pin %u out of bounds\n", pin);
72  return;
73  }
74 
75  pin_cfg[pin] = cfg;
76  LOG_DBG("Pin %u: Set config=0x%02x\n", pin, pin_cfg[pin]);
77 }
78 /*---------------------------------------------------------------------------*/
81 {
82  if(pin >= GPIO_HAL_PIN_COUNT) {
83  LOG_ERR("Pin %u out of bounds\n", pin);
84  return 0;
85  }
86 
87  LOG_DBG("Pin %u: Config=0x%02x\n", pin, pin_cfg[pin]);
88  return pin_cfg[pin];
89 }
90 /*---------------------------------------------------------------------------*/
91 void
93 {
94  if(pin >= GPIO_HAL_PIN_COUNT) {
95  LOG_ERR("Pin %u out of bounds\n", pin);
96  return;
97  }
98 
99  LOG_DBG("Pin %u: Set input\n", pin);
100 }
101 /*---------------------------------------------------------------------------*/
102 void
104 {
105  if(pin >= GPIO_HAL_PIN_COUNT) {
106  LOG_ERR("Pin %u out of bounds\n", pin);
107  return;
108  }
109 
110  LOG_DBG("Pin %u: Set output\n", pin);
111 }
112 /*---------------------------------------------------------------------------*/
113 void
115 {
116  if(pin >= GPIO_HAL_PIN_COUNT) {
117  LOG_ERR("Pin %u out of bounds\n", pin);
118  return;
119  }
120 
121  pin_state[pin] = 1;
122  LOG_DBG("Pin %u: Set\n", pin);
123 }
124 /*---------------------------------------------------------------------------*/
125 void
127 {
128  if(pin >= GPIO_HAL_PIN_COUNT) {
129  LOG_ERR("Pin %u out of bounds\n", pin);
130  return;
131  }
132 
133  pin_state[pin] = 0;
134  LOG_DBG("Pin %u: Clear\n", pin);
135 }
136 /*---------------------------------------------------------------------------*/
137 uint8_t
139 {
140  if(pin >= GPIO_HAL_PIN_COUNT) {
141  LOG_ERR("Pin %u out of bounds\n", pin);
142  return 0;
143  }
144 
145  LOG_DBG("Pin %u: Read=%u\n", pin, pin_state[pin]);
146  return pin_state[pin];
147 }
148 /*---------------------------------------------------------------------------*/
149 void
151 {
152  if(pin >= GPIO_HAL_PIN_COUNT) {
153  LOG_ERR("Pin %u out of bounds\n", pin);
154  return;
155  }
156 
157  pin_state[pin] = value;
158  LOG_DBG("Pin %u: Write=%u\n", pin, pin_state[pin]);
159 }
160 /*---------------------------------------------------------------------------*/
161 void
163 {
164  gpio_hal_pin_t pin;
165 
166  for(pin = 0; pin < GPIO_HAL_PIN_COUNT; pin++) {
167  if(pins & (1 << pin)) {
168  pin_state[pin] = 1;
169  }
170  }
171 
172  LOG_DBG("Set pins 0x%08" PRIx32 "\n", pins);
173 }
174 /*---------------------------------------------------------------------------*/
175 void
177 {
178  gpio_hal_pin_t pin;
179 
180  for(pin = 0; pin < GPIO_HAL_PIN_COUNT; pin++) {
181  if(pins & (1 << pin)) {
182  pin_state[pin] = 0;
183  }
184  }
185 
186  LOG_DBG("Clear pins 0x%08" PRIx32 "\n", pins);
187 }
188 /*---------------------------------------------------------------------------*/
191 {
192  gpio_hal_pin_t pin;
193  gpio_hal_pin_mask_t state = 0;
194 
195  for(pin = 0; pin < GPIO_HAL_PIN_COUNT; pin++) {
196  state |= (pin_state[pin] << pin);
197  }
198 
199  LOG_DBG("Read pins 0x%08" PRIx32 "\n", state);
200  return state;
201 }
202 /*---------------------------------------------------------------------------*/
203 void
205 {
206  gpio_hal_pin_t pin;
207 
208  for(pin = 0; pin < GPIO_HAL_PIN_COUNT; pin++) {
209  if(pins & (1 << pin)) {
210  pin_state[pin] = (value & (1 << pin)) == 0 ? 0 : 1;
211  }
212  }
213 
214  LOG_DBG("Write pins 0x%08" PRIx32 "->0x%08" PRIx32 "\n", pins, value);
215 }
216 /*---------------------------------------------------------------------------*/
void gpio_hal_arch_interrupt_disable(gpio_hal_pin_t pin)
Disable interrupts for a gpio pin.
Definition: gpio-hal-arch.c:57
void gpio_hal_arch_clear_pins(gpio_hal_pin_mask_t pins)
Clear multiple pins to logical low.
void gpio_hal_arch_write_pin(gpio_hal_pin_t pin, uint8_t value)
Write a GPIO pin.
void gpio_hal_arch_pin_set_input(gpio_hal_pin_t pin)
Configure a pin as GPIO input.
Definition: gpio-hal-arch.c:92
gpio_hal_pin_cfg_t gpio_hal_arch_pin_cfg_get(gpio_hal_pin_t pin)
Read the configuration of a GPIO pin.
Definition: gpio-hal-arch.c:97
gpio_hal_pin_mask_t gpio_hal_arch_read_pins(gpio_hal_pin_mask_t pins)
Read multiple pins.
void gpio_hal_arch_interrupt_enable(gpio_hal_pin_t pin)
Enable interrupts for a gpio pin.
Definition: gpio-hal-arch.c:46
uint8_t gpio_hal_pin_cfg_t
GPIO pin configuration.
Definition: gpio-hal.h:85
void gpio_hal_arch_set_pin(gpio_hal_pin_t pin)
Set a GPIO pin to logical high.
void gpio_hal_arch_clear_pin(gpio_hal_pin_t pin)
Clear a GPIO pin (logical low)
void gpio_hal_arch_set_pins(gpio_hal_pin_mask_t pins)
Set multiple pins to logical high.
uint8_t gpio_hal_pin_t
GPIO pin number representation.
Definition: gpio-hal.h:77
uint32_t gpio_hal_pin_mask_t
GPIO pin mask representation.
Definition: gpio-hal.h:99
void gpio_hal_arch_write_pins(gpio_hal_pin_mask_t pins, gpio_hal_pin_mask_t value)
Write multiple pins.
void gpio_hal_arch_pin_set_output(gpio_hal_pin_t pin)
Configure a pin as GPIO output.
uint8_t gpio_hal_arch_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_pin_cfg_set(gpio_hal_pin_t pin, gpio_hal_pin_cfg_t cfg)
Configure a gpio pin.
Definition: gpio-hal-arch.c:48