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  */
53 static gpio_callback_t gpio_callbacks[32];
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 */
83 void
85 {
86  lpm_exit();
87 
88  ENERGEST_ON(ENERGEST_TYPE_IRQ);
89 
91 
94 
95  ENERGEST_OFF(ENERGEST_TYPE_IRQ);
96 }
97 /*---------------------------------------------------------------------------*/
98 /** \brief Interrupt service routine for Port B */
99 void
101 {
102  lpm_exit();
103 
104  ENERGEST_ON(ENERGEST_TYPE_IRQ);
105 
107 
110 
111  ENERGEST_OFF(ENERGEST_TYPE_IRQ);
112 }
113 /*---------------------------------------------------------------------------*/
114 /** \brief Interrupt service routine for Port C */
115 void
117 {
118  lpm_exit();
119 
120  ENERGEST_ON(ENERGEST_TYPE_IRQ);
121 
123 
126 
127  ENERGEST_OFF(ENERGEST_TYPE_IRQ);
128 }
129 /*---------------------------------------------------------------------------*/
130 /** \brief Interrupt service routine for Port D */
131 void
133 {
134  lpm_exit();
135 
136  ENERGEST_ON(ENERGEST_TYPE_IRQ);
137 
139 
142 
143  ENERGEST_OFF(ENERGEST_TYPE_IRQ);
144 }
145 /*---------------------------------------------------------------------------*/
146 void
148 {
149  memset(gpio_callbacks, 0, sizeof(gpio_callbacks));
150 }
151 /** @} */
#define GPIO_A_NUM
GPIO_A: 0.
Definition: gpio.h:79
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_C_BASE
GPIO_C.
Definition: gpio.h:72
void gpio_port_d_isr()
Interrupt service routine for Port D.
Definition: gpio.c:132
#define GPIO_B_NUM
GPIO_B: 1.
Definition: gpio.h:80
Header file for the ARM Nested Vectored Interrupt Controller.
#define GPIO_A_BASE
GPIO_A.
Definition: gpio.h:70
#define GPIO_D_NUM
GPIO_D: 3.
Definition: gpio.h:82
Header file with register and macro declarations for the cc2538 GPIO module.
#define NULL
The null pointer.
void gpio_init()
Initialise the GPIO module.
Definition: gpio.c:147
#define lpm_exit()
Perform an &#39;Exit Deep Sleep&#39; sequence.
Definition: lpm.h:213
void gpio_port_b_isr()
Interrupt service routine for Port B.
Definition: gpio.c:100
Header file with register manipulation macro definitions.
Header file for the energy estimation mechanism
#define GPIO_MIS
Interrupt status - masked.
Definition: gpio.h:289
#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:258
void gpio_register_callback(gpio_callback_t f, uint8_t port, uint8_t pin)
Register GPIO callback.
Definition: gpio.c:56
void gpio_port_c_isr()
Interrupt service routine for Port C.
Definition: gpio.c:116
#define GPIO_B_BASE
GPIO_B.
Definition: gpio.h:71
#define GPIO_C_NUM
GPIO_C: 2.
Definition: gpio.h:81
void(* gpio_callback_t)(uint8_t port, uint8_t pin)
Type definition for callbacks invoked by the GPIO ISRs.
Definition: gpio.h:65
#define GPIO_CLEAR_INTERRUPT(PORT_BASE, PIN_MASK)
Clear interrupt triggering for pins with PIN_MASK of port with PORT_BASE.
Definition: gpio.h:198
void gpio_port_a_isr()
Interrupt service routine for Port A.
Definition: gpio.c:84
#define GPIO_D_BASE
GPIO_D.
Definition: gpio.h:73