Contiki-NG
gpio-hal.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 gpio-hal
34  * @{
35  *
36  * \file
37  * Implementation of the platform-independent aspects of the GPIO HAL
38  */
39 /*---------------------------------------------------------------------------*/
40 #include "contiki.h"
41 #include "dev/gpio-hal.h"
42 #include "lib/list.h"
43 #include "sys/log.h"
44 
45 #include <stdint.h>
46 #include <string.h>
47 /*---------------------------------------------------------------------------*/
48 /* Log configuration */
49 #define LOG_MODULE "GPIO HAL"
50 #define LOG_LEVEL LOG_LEVEL_NONE
51 /*---------------------------------------------------------------------------*/
52 LIST(handlers);
53 /*---------------------------------------------------------------------------*/
54 void
56 {
57  list_add(handlers, handler);
58 }
59 /*---------------------------------------------------------------------------*/
60 #if GPIO_HAL_PORT_PIN_NUMBERING
61 /*---------------------------------------------------------------------------*/
62 void
64 {
66 
67  for(this = list_head(handlers); this != NULL; this = this->next) {
68  if((port == this->port) && (pins & this->pin_mask)) {
69  if(this->handler != NULL) {
70  this->handler(port, pins & this->pin_mask);
71  }
72  }
73  }
74 }
75 /*---------------------------------------------------------------------------*/
76 #else
77 /*---------------------------------------------------------------------------*/
78 void
80 {
82 
83  for(this = list_head(handlers); this != NULL; this = this->next) {
84  if(pins & this->pin_mask) {
85  if(this->handler != NULL) {
86  this->handler(pins & this->pin_mask);
87  }
88  }
89  }
90 }
91 /*---------------------------------------------------------------------------*/
92 #endif /* GPIO_HAL_PORT_PIN_NUMBERING */
93 /*---------------------------------------------------------------------------*/
94 void
96 {
97  list_init(handlers);
98  gpio_hal_arch_init();
99 }
100 /*---------------------------------------------------------------------------*/
101 #if GPIO_HAL_ARCH_SW_TOGGLE
102 /*---------------------------------------------------------------------------*/
103 void
105 {
106  gpio_hal_arch_write_pin(port, pin, gpio_hal_arch_read_pin(port, pin) ^ 1);
107 }
108 /*---------------------------------------------------------------------------*/
109 void
111 {
112  gpio_hal_arch_write_pins(port, pins, ~gpio_hal_arch_read_pins(port, pins));
113 }
114 /*---------------------------------------------------------------------------*/
115 void
117 {
118  if(pin >= GPIO_HAL_PIN_COUNT) {
119  LOG_ERR("Pin %u out of bounds\n", pin);
120  return;
121  }
122 
123  gpio_hal_arch_write_pin(GPIO_HAL_NULL_PORT, pin,
124  gpio_hal_arch_read_pin(GPIO_HAL_NULL_PORT, pin) ^ 1);
125 }
126 /*---------------------------------------------------------------------------*/
127 void
129 {
130  gpio_hal_arch_write_pins(GPIO_HAL_NULL_PORT, pins,
131  ~gpio_hal_arch_read_pins(GPIO_HAL_NULL_PORT,
132  pins));
133 }
134 /*---------------------------------------------------------------------------*/
135 #endif /* GPIO_HAL_ARCH_SW_TOGGLE */
136 /*---------------------------------------------------------------------------*/
137 /**
138  * @}
139  */
Datatype for GPIO event handlers.
Definition: gpio-hal.h:180
void gpio_hal_arch_port_toggle_pin(gpio_hal_port_t port, gpio_hal_pin_t pin)
Toggle a GPIO pin.
void gpio_hal_arch_no_port_toggle_pins(gpio_hal_pin_mask_t pins)
Toggle multiple pins.
uint8_t gpio_hal_port_t
A data structure that represents ports.
Definition: gpio-hal.h:110
#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_toggle_pin(gpio_hal_pin_t pin)
Toggle a GPIO pin.
void gpio_hal_init()
Initialise the GPIO HAL.
Definition: gpio-hal.c:95
void gpio_hal_register_handler(gpio_hal_event_handler_t *handler)
Register a function to be called whenever a pin triggers an event.
Definition: gpio-hal.c:55
Linked list manipulation routines.
void * list_head(list_t list)
Get a pointer to the first element of a list.
Definition: list.c:82
uint8_t gpio_hal_pin_t
GPIO pin number representation.
Definition: gpio-hal.h:103
void list_add(list_t list, void *item)
Add an item at the end of a list.
Definition: list.c:142
uint32_t gpio_hal_pin_mask_t
GPIO pin mask representation.
Definition: gpio-hal.h:142
void list_init(list_t list)
Initialize a list.
Definition: list.c:65
#define LIST(name)
Declare a linked list.
Definition: list.h:89
void gpio_hal_arch_port_toggle_pins(gpio_hal_port_t port, gpio_hal_pin_mask_t pins)
Toggle multiple pins.
Header file for the GPIO HAL.
void gpio_hal_event_handler(gpio_hal_port_t port, gpio_hal_pin_mask_t pins)
The platform-independent GPIO event handler.
Header file for the logging system
#define GPIO_HAL_NULL_PORT
Convenience macro to use this as the port argument of macros.
Definition: gpio-hal.h:98