Contiki 3.x
gpio.c
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2012, Texas Instruments Incorporated - http://www.ti.com/
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  * 1. Redistributions of source code must retain the above copyright
9  * notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  * notice, this list of conditions and the following disclaimer in the
12  * documentation and/or other materials provided with the distribution.
13  *
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  * \addtogroup cc2538-gpio
33  * @{
34  *
35  * \file
36  * Implementation of the cc2538 GPIO controller
37  */
38 #include "contiki.h"
39 #include "sys/energest.h"
40 #include "dev/leds.h"
41 #include "dev/gpio.h"
42 #include "dev/nvic.h"
43 #include "reg.h"
44 #include "lpm.h"
45 
46 #include <string.h>
47 
48 /**
49  * \brief Pointer to a function to be called when a GPIO interrupt is detected.
50  * Callbacks for Port A, Pins[0:7] are stored in positions [0:7] of this
51  * buffer, Port B callbacks in [8:15] and so on
52  */
54 /*---------------------------------------------------------------------------*/
55 void
56 gpio_register_callback(gpio_callback_t f, uint8_t port, uint8_t pin)
57 {
58  gpio_callbacks[(port << 3) + pin] = f;
59 }
60 /*---------------------------------------------------------------------------*/
61 /** \brief Run through all registered GPIO callbacks and invoke those
62  * associated with the \a port and the pins specified by \a mask
63  * \param mask Search callbacks associated with pins specified by this mask
64  * \param port Search callbacks associated with this port. Here, port is
65  * specified as a number between 0 and 3. Port A: 0, Port B: 1 etc */
66 void
67 notify(uint8_t mask, uint8_t port)
68 {
69  uint8_t i;
70  gpio_callback_t *f = &gpio_callbacks[port << 3];
71 
72  for(i = 0; i < 8; i++) {
73  if(mask & (1 << i)) {
74  if((*f) != NULL) {
75  (*f)(port, i);
76  }
77  }
78  f++;
79  }
80 }
81 /*---------------------------------------------------------------------------*/
82 /** \brief Interrupt service routine for Port \a port
83  * \param port Number between 0 and 3. Port A: 0, Port B: 1, etc.
84  */
85 static void
86 gpio_port_isr(uint8_t port)
87 {
88  uint32_t base;
89  uint8_t int_status, power_up_int_status;
90 
91  lpm_exit();
92 
93  ENERGEST_ON(ENERGEST_TYPE_IRQ);
94 
95  base = GPIO_PORT_TO_BASE(port);
96  int_status = GPIO_GET_MASKED_INT_STATUS(base);
97  power_up_int_status = GPIO_GET_POWER_UP_INT_STATUS(port);
98 
99  notify(int_status | power_up_int_status, port);
100 
101  GPIO_CLEAR_INTERRUPT(base, int_status);
102  GPIO_CLEAR_POWER_UP_INTERRUPT(port, power_up_int_status);
103 
104  ENERGEST_OFF(ENERGEST_TYPE_IRQ);
105 }
106 /*---------------------------------------------------------------------------*/
107 #define GPIO_PORT_ISR(lowercase_port, uppercase_port) \
108 void \
109 gpio_port_##lowercase_port##_isr(void) \
110 { \
111  gpio_port_isr(GPIO_##uppercase_port##_NUM); \
112 }
113 GPIO_PORT_ISR(a, A)
114 GPIO_PORT_ISR(b, B)
115 GPIO_PORT_ISR(c, C)
116 GPIO_PORT_ISR(d, D)
117 /*---------------------------------------------------------------------------*/
118 void
120 {
121  memset(gpio_callbacks, 0, sizeof(gpio_callbacks));
122 }
123 /** @} */
void gpio_init()
Initialise the GPIO module.
Definition: gpio.c:13
Header file with register manipulation macro definitions.
#define GPIO_CLEAR_POWER_UP_INTERRUPT(PORT, PIN_MASK)
Clear power-up interrupt triggering for pins with PIN_MASK of port PORT.
Definition: gpio.h:311
#define GPIO_CLEAR_INTERRUPT(PORT_BASE, PIN_MASK)
Clear interrupt triggering for pins with PIN_MASK of port with PORT_BASE.
Definition: gpio.h:243
static gpio_callback_t gpio_callbacks[32]
Pointer to a function to be called when a GPIO interrupt is detected.
Definition: gpio.c:53
Header file for the ARM Nested Vectored Interrupt Controller.
static uint8_t int_status(void)
Check whether a data or wake on motion interrupt has occurred.
Header file for the energy estimation mechanism
void(* gpio_callback_t)(uint8_t port, uint8_t pin)
Type definition for callbacks invoked by the GPIO ISRs.
Definition: gpio.h:65
static void gpio_port_isr(uint8_t port)
Interrupt service routine for Port port.
Definition: gpio.c:86
#define NULL
The null pointer.
#define GPIO_GET_POWER_UP_INT_STATUS(PORT)
Get power-up interrupt status of port PORT.
Definition: gpio.h:303
#define GPIO_PORT_TO_BASE(PORT)
Converts a port number to the port base address.
Definition: gpio.h:329
void notify(uint8_t mask, uint8_t port)
Run through all registered GPIO callbacks and invoke those associated with the port and the pins spec...
Definition: gpio.c:67
#define GPIO_GET_MASKED_INT_STATUS(PORT_BASE)
Get masked interrupt status of port with PORT_BASE.
Definition: gpio.h:235
#define lpm_exit()
Perform an 'Exit Deep Sleep' sequence.
Definition: lpm.h:213
void gpio_register_callback(gpio_callback_t f, uint8_t port, uint8_t pin)
Register GPIO callback.
Definition: gpio.c:56