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 void
62 {
64 
65  for(this = list_head(handlers); this != NULL; this = this->next) {
66  if(pins & this->pin_mask) {
67  if(this->handler != NULL) {
68  this->handler(pins & this->pin_mask);
69  }
70  }
71  }
72 }
73 /*---------------------------------------------------------------------------*/
74 void
76 {
77  list_init(handlers);
78  gpio_hal_arch_init();
79 }
80 /*---------------------------------------------------------------------------*/
81 #if GPIO_HAL_ARCH_SW_TOGGLE
82 /*---------------------------------------------------------------------------*/
83 void
85 {
86  if(pin >= GPIO_HAL_PIN_COUNT) {
87  LOG_ERR("Pin %u out of bounds\n", pin);
88  return;
89  }
90 
91  gpio_hal_arch_write_pin(pin, gpio_hal_arch_read_pin(pin) ^ 1);
92 }
93 /*---------------------------------------------------------------------------*/
94 void
96 {
97  gpio_hal_arch_write_pins(pins, ~gpio_hal_arch_read_pins(pins));
98 }
99 /*---------------------------------------------------------------------------*/
100 #endif /* GPIO_HAL_ARCH_SW_TOGGLE */
101 /*---------------------------------------------------------------------------*/
102 /**
103  * @}
104  */
Datatype for GPIO event handlers.
Definition: gpio-hal.h:129
gpio_hal_pin_mask_t gpio_hal_arch_read_pins(gpio_hal_pin_mask_t pins)
Read multiple pins.
void gpio_hal_init()
Initialise the GPIO HAL.
Definition: gpio-hal.c:75
void gpio_hal_event_handler(gpio_hal_pin_mask_t pins)
The platform-independent GPIO event handler.
Definition: gpio-hal.c:61
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
void gpio_hal_arch_toggle_pin(gpio_hal_pin_t pin)
Toggle a GPIO pin.
void gpio_hal_arch_toggle_pins(gpio_hal_pin_mask_t pins)
Toggle multiple pins.
uint8_t gpio_hal_pin_t
GPIO pin number representation.
Definition: gpio-hal.h:77
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:99
void list_init(list_t list)
Initialize a list.
Definition: list.c:65
#define LIST(name)
Declare a linked list.
Definition: list.h:88
Header file for the GPIO HAL.
Header file for the logging system