Contiki 3.x
event-sensor.c
1 /*
2  * Copyright (c) 2005, Swedish Institute of Computer Science
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 #include "contiki.h"
33 #include "event-sensor.h"
34 #include "dev/gpio.h"
35 #include "dev/ioc.h"
36 #include "board.h"
37 #include <stdio.h>
38 
39 #define DEBOUNCE_SECS (1 / 4)
40 
41 // Measure events for the rain
42 #define EVENT_PORT RAIN_INT_PORT
43 #define EVENT_PIN RAIN_INT_PIN
44 #define EVENT_VECTOR RAIN_INT_VECTOR
45 
46 const struct sensors_sensor event_sensor;
47 
48 static struct timer debounce_timer;
49 
50 static int event_count;
51 
52 static int status(int type);
53 
54 static void event_isr(uint8_t port, uint8_t pin) {
55  if (timer_expired(&debounce_timer)) {
56  timer_set(&debounce_timer, CLOCK_SECOND); // * DEBOUNCE_SECS);
57  event_count++;
58  sensors_changed(&event_sensor);
59  }
60 }
61 
62 static int value(int reset) {
63  int curr_event_count = event_count;
64 
65  // Reset the event counter if requested
66  if (reset) {
67  event_count = 0;
68  }
69 
70  return curr_event_count;
71 }
72 
73 static int configure(int type, int c) {
74  switch (type) {
75 
76  case SENSORS_ACTIVE:
77  // If enabling has been requested, and we're not already active
78  if (c && !status(SENSORS_ACTIVE)) {
79 
80  // Set up a rising edge interrupt
81  GPIO_SOFTWARE_CONTROL(GPIO_PORT_TO_BASE(EVENT_PORT), GPIO_PIN_MASK(EVENT_PIN));
82  GPIO_SET_INPUT(GPIO_PORT_TO_BASE(EVENT_PORT), GPIO_PIN_MASK(EVENT_PIN));
83  GPIO_DETECT_EDGE(GPIO_PORT_TO_BASE(EVENT_PORT), GPIO_PIN_MASK(EVENT_PIN));
85  GPIO_DETECT_RISING(GPIO_PORT_TO_BASE(EVENT_PORT), GPIO_PIN_MASK(EVENT_PIN));
86 
87  // Disable any overrides enabling pull up / downs, or output
88  ioc_set_over(EVENT_PORT, EVENT_PIN, IOC_OVERRIDE_DIS);
89 
90  gpio_register_callback(&event_isr, EVENT_PORT, EVENT_PIN);
91 
92  // Set the timer to 0 so it is already expired on the first ISR
93  timer_set(&debounce_timer, 0);
94  event_count = 0;
95 
96  // Enable the interupt
97  GPIO_ENABLE_INTERRUPT(GPIO_PORT_TO_BASE(EVENT_PORT), GPIO_PIN_MASK(EVENT_PIN));
98  nvic_interrupt_enable(EVENT_VECTOR);
99 
100  } else {
101  GPIO_DISABLE_INTERRUPT(GPIO_PORT_TO_BASE(EVENT_PORT), GPIO_PIN_MASK(EVENT_PIN));
102  }
103 
104  return 1;
105 
106  default:
107  return 0;
108  }
109 }
110 
111 static int status(int type) {
112  switch (type) {
113  case SENSORS_ACTIVE:
114  case SENSORS_READY:
115  // If the interrupt is enabled, we're active
116  return (REG(GPIO_PORT_TO_BASE(EVENT_PORT) + GPIO_IE) & GPIO_PIN_MASK(EVENT_PIN));
117  default:
118  return 0;
119  }
120 }
121 
122 SENSORS_SENSOR(event_sensor, "Event Count", value, configure, status);
#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
#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 GPIO_PIN_MASK(PIN)
Converts a pin number to a pin mask.
Definition: gpio.h:321
Header file with definitions related to the I/O connections on the Muntjac platform, cc2538-based.
#define IOC_OVERRIDE_DIS
Override Disabled.
Definition: ioc.h:226
#define GPIO_IE
Interrupt mask.
Definition: gpio.h:340
#define GPIO_DETECT_EDGE(PORT_BASE, PIN_MASK)
Set pins with PIN_MASK of port with PORT_BASE to detect edge.
Definition: gpio.h:155
void timer_set(struct timer *t, clock_time_t interval)
Set a timer.
Definition: timer.c:64
A timer.
Definition: timer.h:86
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
#define GPIO_PORT_TO_BASE(PORT)
Converts a port number to the port base address.
Definition: gpio.h:329
#define CLOCK_SECOND
A second, measured in system clock time.
Definition: clock.h:82
#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 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
int timer_expired(struct timer *t)
Check if a timer has expired.
Definition: timer.c:122