Contiki 3.x
pulse-sensor.c
1 /*
2  * Copyright (c) 2015, Copyright Robert Olsson / Radio Sensors AB
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  * Author : Robert Olsson robert@radio-sensors.com
33  * Created : 2015-11-22
34  */
35 
36 #include "contiki.h"
37 #include "lib/sensors.h"
38 #include "dev/pulse-sensor.h"
39 #include "rss2.h"
40 
41 const struct sensors_sensor pulse_sensor;
42 
43 #define NP 2
44 
45 uint32_t volatile pc[NP];
46 
47 /*
48  * Note interrupt sources can be woken up from sleep mode PWR_SAVE
49  * Two interrupt ports, #0 green terminal block. #1 pin header via
50  * a comparator.
51  */
52 
53 static void
54 port_irq_ctrl(uint8_t on)
55 {
56  if(on) {
57 
58  DDRD &= ~(1 << PD2);
59  PORTD &= ~(1 << PD2);
60  EIMSK = 0;
61  EICRA |= 0x20; /* Falling edge INT2 */
62  EIMSK |= (1 << PD2); /* Enable interrupt for pin */
63 
64  /* p1 port */
65  DDRD &= ~(1 << PD3);
66  PORTD &= ~(1 << PD3);
67  EIMSK |= (1 << PD3); /* Enable interrupt for pin */
68  EICRA |= 0x80; /* Falling edge */
69  PCICR |= (1 << PCIE0); /* And enable irq PCINT 7:0 */
70  } else {
71  EICRA = 0;
72  PORTD |= (1 << PD2);
73  EIMSK &= ~(1 << PD2); /* Disable interrupt for pin */
74 
75  PORTD |= (1 << PD3);
76  EIMSK &= ~(1 << PD3); /* Disable interrupt for pin */
77  }
78 }
79 ISR(INT2_vect)
80 {
81  if(!(PCICR & (1 << PCIE0))) {
82  return;
83  }
84  pc[0]++;
85 }
86 
87 ISR(INT3_vect)
88 {
89  if(!(PCICR & (1 << PCIE0))) {
90  return;
91  }
92  pc[1]++;
93 }
94 /*---------------------------------------------------------------------------*/
95 static int
96 value(int type)
97 {
98  if(type == 0) {
99  return (int)pc[0];
100  }
101  if(type == 1) {
102  return (int)pc[1];
103  }
104  return -1;
105 }
106 /*---------------------------------------------------------------------------*/
107 static int
108 status(int type)
109 {
110  return 0;
111 }
112 /*---------------------------------------------------------------------------*/
113 static int
114 configure(int type, int c)
115 {
116  port_irq_ctrl(1); /* Enable pulse counts */
117  return 0;
118 }
119 /*---------------------------------------------------------------------------*/
120 SENSORS_SENSOR(pulse_sensor, "Pulse", value, configure, status);
#define PORTD
Peripheral PORTD base pointer.
Definition: MKL25Z4.h:4450