Contiki-NG
motion-sensor.c
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2015, Zolertia <http://www.zolertia.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  * 3. Neither the name of the Institute nor the names of its contributors
14  * may be used to endorse or promote products derived from this software
15  * without specific prior written permission.
16  *
17  * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
18  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20  * ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
21  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27  * SUCH DAMAGE.
28  *
29  * This file is part of the Contiki operating system.
30  *
31  */
32 /*---------------------------------------------------------------------------*/
33 /**
34  * \addtogroup zoul-motion-sensor
35  * @{
36  *
37  * \file
38  * Digital motion sensor driver
39  * \author
40  * Antonio Lignan <alinan@zolertia.com>
41  */
42 /*---------------------------------------------------------------------------*/
43 #include <stdio.h>
44 #include "contiki.h"
45 #include "dev/i2c.h"
46 #include "dev/motion-sensor.h"
47 #include "lib/sensors.h"
48 #include "dev/sys-ctrl.h"
49 #include "dev/gpio.h"
50 #include "dev/gpio-hal.h"
51 #include "dev/ioc.h"
52 /*---------------------------------------------------------------------------*/
53 #define DEBUG 0
54 #if DEBUG
55 #define PRINTF(...) printf(__VA_ARGS__)
56 #else
57 #define PRINTF(...)
58 #endif
59 /*---------------------------------------------------------------------------*/
60 #define MOTION_SENSOR_PORT_BASE GPIO_PORT_TO_BASE(MOTION_SENSOR_PORT)
61 #define MOTION_SENSOR_PIN_MASK GPIO_PIN_MASK(MOTION_SENSOR_PIN)
62 /*---------------------------------------------------------------------------*/
63 void (*presence_int_callback)(uint8_t value);
64 /*---------------------------------------------------------------------------*/
65 PROCESS(motion_int_process, "Motion interrupt process handler");
66 /*---------------------------------------------------------------------------*/
67 PROCESS_THREAD(motion_int_process, ev, data)
68 {
70  PROCESS_BEGIN();
71 
72  while(1) {
73  PROCESS_YIELD_UNTIL(ev == PROCESS_EVENT_POLL);
74  presence_int_callback(0);
75  }
76  PROCESS_END();
77 }
78 /*---------------------------------------------------------------------------*/
79 static void
80 motion_interrupt_handler(gpio_hal_pin_mask_t pin_mask)
81 {
82  process_poll(&motion_int_process);
83 }
84 /*---------------------------------------------------------------------------*/
85 static gpio_hal_event_handler_t motion_handler = {
86  .next = NULL,
87  .handler = motion_interrupt_handler,
88  .pin_mask = gpio_hal_pin_to_mask(MOTION_SENSOR_PIN) << (MOTION_SENSOR_PORT << 3),
89 };
90 /*---------------------------------------------------------------------------*/
91 static int
92 status(int type)
93 {
94  return MOTION_SUCCESS;
95 }
96 /*---------------------------------------------------------------------------*/
97 static int
98 value(int type)
99 {
100  return GPIO_READ_PIN(MOTION_SENSOR_PORT_BASE, MOTION_SENSOR_PIN_MASK);
101 }
102 /*---------------------------------------------------------------------------*/
103 static int
104 configure(int type, int value)
105 {
106  if(type != MOTION_ACTIVE) {
107  PRINTF("Motion: invalid configuration option\n");
108  return MOTION_ERROR;
109  }
110 
111  if(!value) {
112  presence_int_callback = NULL;
113  GPIO_DISABLE_INTERRUPT(MOTION_SENSOR_PORT_BASE, MOTION_SENSOR_PIN_MASK);
114  return MOTION_SUCCESS;
115  }
116 
117  /* Configure interruption */
118  GPIO_SOFTWARE_CONTROL(MOTION_SENSOR_PORT_BASE, MOTION_SENSOR_PIN_MASK);
119  GPIO_SET_INPUT(MOTION_SENSOR_PORT_BASE, MOTION_SENSOR_PIN_MASK);
120  GPIO_DETECT_RISING(MOTION_SENSOR_PORT_BASE, MOTION_SENSOR_PIN_MASK);
121  GPIO_TRIGGER_SINGLE_EDGE(MOTION_SENSOR_PORT_BASE, MOTION_SENSOR_PIN_MASK);
122  ioc_set_over(MOTION_SENSOR_PORT, MOTION_SENSOR_PIN, IOC_OVERRIDE_DIS);
123  gpio_hal_register_handler(&motion_handler);
124 
125  process_start(&motion_int_process, NULL);
126 
127  GPIO_ENABLE_INTERRUPT(MOTION_SENSOR_PORT_BASE, MOTION_SENSOR_PIN_MASK);
128  NVIC_EnableIRQ(MOTION_SENSOR_VECTOR);
129  return MOTION_SUCCESS;
130 }
131 /*---------------------------------------------------------------------------*/
132 SENSORS_SENSOR(motion_sensor, MOTION_SENSOR, value, configure, status);
133 /*---------------------------------------------------------------------------*/
134 /** @} */
Datatype for GPIO event handlers.
Definition: gpio-hal.h:180
#define GPIO_ENABLE_INTERRUPT(PORT_BASE, PIN_MASK)
Enable interrupt triggering for pins with PIN_MASK of port with PORT_BASE.
Definition: gpio.h:201
#define PROCESS(name, strname)
Declare a process.
Definition: process.h:307
Header file for the cc2538 System Control driver.
#define PROCESS_YIELD_UNTIL(c)
Yield the currently running process until a condition occurs.
Definition: process.h:178
#define PROCESS_BEGIN()
Define the beginning of a process.
Definition: process.h:120
Header file with register and macro declarations for the cc2538 GPIO module.
#define PROCESS_END()
Define the end of a process.
Definition: process.h:131
Header file with declarations for the I/O Control module.
#define IOC_OVERRIDE_DIS
Override Disabled.
Definition: ioc.h:226
#define GPIO_DETECT_RISING(PORT_BASE, PIN_MASK)
Set pins with PIN_MASK of port with PORT_BASE to trigger an interrupt on rising edge.
Definition: gpio.h:185
#define GPIO_READ_PIN(PORT_BASE, PIN_MASK)
Read pins with PIN_MASK of port with PORT_BASE.
Definition: gpio.h:147
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
__STATIC_INLINE void NVIC_EnableIRQ(IRQn_Type IRQn)
Enable External Interrupt.
Definition: core_cm0.h:642
#define GPIO_DISABLE_INTERRUPT(PORT_BASE, PIN_MASK)
Disable interrupt triggering for pins with PIN_MASK of port with PORT_BASE.
Definition: gpio.h:209
#define GPIO_SOFTWARE_CONTROL(PORT_BASE, PIN_MASK)
Configure the pin to be software controlled with PIN_MASK of port with PORT_BASE. ...
Definition: gpio.h:258
#define PROCESS_EXITHANDLER(handler)
Specify an action when a process exits.
Definition: process.h:254
void process_poll(struct process *p)
Request a process to be polled.
Definition: process.c:371
void ioc_set_over(uint8_t port, uint8_t pin, uint8_t over)
Set Port:Pin override function.
Definition: ioc.c:54
#define GPIO_SET_INPUT(PORT_BASE, PIN_MASK)
Set pins with PIN_MASK of port with PORT_BASE to input.
Definition: gpio.h:78
uint32_t gpio_hal_pin_mask_t
GPIO pin mask representation.
Definition: gpio-hal.h:142
#define GPIO_TRIGGER_SINGLE_EDGE(PORT_BASE, PIN_MASK)
Set pins with PIN_MASK of port with PORT_BASE to trigger an interrupt on single edge (controlled by G...
Definition: gpio.h:177
PROCESS_THREAD(cc2538_rf_process, ev, data)
Implementation of the cc2538 RF driver process.
Definition: cc2538-rf.c:1107
#define gpio_hal_pin_to_mask(pin)
Convert a pin to a pin mask.
Definition: gpio-hal.h:255
Header file for the GPIO HAL.
Digital motion sensor header file.
void process_start(struct process *p, process_data_t data)
Start a process.
Definition: process.c:99