Contiki 3.x
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/ioc.h"
51 /*---------------------------------------------------------------------------*/
52 #define DEBUG 0
53 #if DEBUG
54 #define PRINTF(...) printf(__VA_ARGS__)
55 #else
56 #define PRINTF(...)
57 #endif
58 /*---------------------------------------------------------------------------*/
59 #define MOTION_SENSOR_PORT_BASE GPIO_PORT_TO_BASE(MOTION_SENSOR_PORT)
60 #define MOTION_SENSOR_PIN_MASK GPIO_PIN_MASK(MOTION_SENSOR_PIN)
61 /*---------------------------------------------------------------------------*/
62 void (*presence_int_callback)(uint8_t value);
63 /*---------------------------------------------------------------------------*/
64 PROCESS(motion_int_process, "Motion interrupt process handler");
65 /*---------------------------------------------------------------------------*/
66 PROCESS_THREAD(motion_int_process, ev, data)
67 {
69  PROCESS_BEGIN();
70 
71  while(1) {
72  PROCESS_YIELD_UNTIL(ev == PROCESS_EVENT_POLL);
73  presence_int_callback(0);
74  }
75  PROCESS_END();
76 }
77 /*---------------------------------------------------------------------------*/
78 static void
79 motion_interrupt_handler(uint8_t port, uint8_t pin)
80 {
81  process_poll(&motion_int_process);
82 }
83 /*---------------------------------------------------------------------------*/
84 static int
85 status(int type)
86 {
87  return MOTION_SUCCESS;
88 }
89 /*---------------------------------------------------------------------------*/
90 static int
91 value(int type)
92 {
93  return GPIO_READ_PIN(MOTION_SENSOR_PORT_BASE, MOTION_SENSOR_PIN_MASK);
94 }
95 /*---------------------------------------------------------------------------*/
96 static int
97 configure(int type, int value)
98 {
99  if(type != MOTION_ACTIVE) {
100  PRINTF("Motion: invalid configuration option\n");
101  return MOTION_ERROR;
102  }
103 
104  if(!value) {
105  presence_int_callback = NULL;
106  GPIO_DISABLE_INTERRUPT(MOTION_SENSOR_PORT_BASE, MOTION_SENSOR_PIN_MASK);
107  return MOTION_SUCCESS;
108  }
109 
110  /* Configure interruption */
111  GPIO_SOFTWARE_CONTROL(MOTION_SENSOR_PORT_BASE, MOTION_SENSOR_PIN_MASK);
112  GPIO_SET_INPUT(MOTION_SENSOR_PORT_BASE, MOTION_SENSOR_PIN_MASK);
113  GPIO_DETECT_RISING(MOTION_SENSOR_PORT_BASE, MOTION_SENSOR_PIN_MASK);
114  GPIO_TRIGGER_SINGLE_EDGE(MOTION_SENSOR_PORT_BASE, MOTION_SENSOR_PIN_MASK);
115  ioc_set_over(MOTION_SENSOR_PORT, MOTION_SENSOR_PIN, IOC_OVERRIDE_DIS);
116  gpio_register_callback(motion_interrupt_handler, MOTION_SENSOR_PORT,
117  MOTION_SENSOR_PIN);
118 
119  process_start(&motion_int_process, NULL);
120 
121  GPIO_ENABLE_INTERRUPT(MOTION_SENSOR_PORT_BASE, MOTION_SENSOR_PIN_MASK);
122  nvic_interrupt_enable(MOTION_SENSOR_VECTOR);
123  return MOTION_SUCCESS;
124 }
125 /*---------------------------------------------------------------------------*/
126 SENSORS_SENSOR(motion_sensor, MOTION_SENSOR, value, configure, status);
127 /*---------------------------------------------------------------------------*/
128 /** @} */
#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:178
void process_poll(struct process *p)
Request a process to be polled.
Definition: process.c:371
#define GPIO_ENABLE_INTERRUPT(PORT_BASE, PIN_MASK)
Enable interrupt triggering for pins with PIN_MASK of port with PORT_BASE.
Definition: gpio.h:202
#define GPIO_SET_INPUT(PORT_BASE, PIN_MASK)
Set pins with PIN_MASK of port with PORT_BASE to input.
Definition: gpio.h:93
#define IOC_OVERRIDE_DIS
Override Disabled.
Definition: ioc.h:226
#define PROCESS_END()
Define the end of a process.
Definition: process.h:131
#define PROCESS(name, strname)
Declare a process.
Definition: process.h:307
#define PROCESS_THREAD(name, ev, data)
Define the body of a process.
Definition: process.h:273
#define GPIO_READ_PIN(PORT_BASE, PIN_MASK)
Read pins with PIN_MASK of port with PORT_BASE.
Definition: gpio.h:148
Digital motion sensor header file.
#define PROCESS_EXITHANDLER(handler)
Specify an action when a process exits.
Definition: process.h:254
#define NULL
The null pointer.
Header file with declarations for the I/O Control module.
#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:259
Header file for the cc2538 System Control driver.
#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:186
void process_start(struct process *p, process_data_t data)
Start a process.
Definition: process.c:99
#define PROCESS_YIELD_UNTIL(c)
Yield the currently running process until a condition occurs.
Definition: process.h:178
void nvic_interrupt_enable(uint32_t intr)
Enables interrupt intr.
Definition: nvic.c:64
#define GPIO_DISABLE_INTERRUPT(PORT_BASE, PIN_MASK)
Disable interrupt triggering for pins with PIN_MASK of port with PORT_BASE.
Definition: gpio.h:210
void ioc_set_over(uint8_t port, uint8_t pin, uint8_t over)
Set Port:Pin override function.
Definition: ioc.c:54
void gpio_register_callback(gpio_callback_t f, uint8_t port, uint8_t pin)
Register GPIO callback.
Definition: gpio.c:56
#define PROCESS_BEGIN()
Define the beginning of a process.
Definition: process.h:120