Contiki 3.x
contact-sensor.c
Go to the documentation of this file.
1 /**
2  * \addtogroup mbxxx-platform
3  *
4  * @{
5  */
6 /*
7  * Copyright (c) 2010, STMicroelectronics.
8  * All rights reserved.
9  *
10  * Redistribution and use in source and binary forms, with or without
11  * modification, are permitted provided that the following conditions
12  * are met:
13  * 1. Redistributions of source code must retain the above copyright
14  * notice, this list of conditions and the following disclaimer.
15  * 2. Redistributions in binary form must reproduce the above
16  * copyright notice, this list of conditions and the following
17  * disclaimer in the documentation and/or other materials provided
18  * with the distribution.
19  * 3. The name of the author may not be used to endorse or promote
20  * products derived from this software without specific prior
21  * written permission.
22  *
23  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS
24  * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
25  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26  * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
27  * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
29  * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
30  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
31  * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
32  * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
33  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
34  *
35  * This file is part of the Contiki OS
36  *
37  */
38 /*---------------------------------------------------------------------------*/
39 /**
40 * \file
41 * Contact sensor.
42 * \author
43 * Stefano Pascali <stefano.pascali@st.com>
44 * Marco Grella <marco.grella@st.com>
45 */
46 /*---------------------------------------------------------------------------*/
47 
48 #include PLATFORM_HEADER
49 #include BOARD_HEADER
50 
51 #include "hal/error.h"
52 #include "hal/hal.h"
53 #include "dev/leds.h"
54 
55 #include "dev/contact-sensor.h"
56 void halIrqAIsr(void);
57 
58 static void
59 init(void)
60 {
61 
62  //PC0 configuration: push-pull output
63  halGpioConfig(PORTC_PIN(0), GPIOCFG_OUT);
64  GPIO_PCSET |= PC0;
65 
66  //PB0 (IRQA)configuration: input Pull Down.
67  //halGpioConfig(PORTB_PIN(0), GPIOCFG_IN_PUD);
68  //GPIO_PBOUT &= ~PB0;
69 
70  //PB0 (IRQA)configuration: input Pull Down.
71  halGpioConfig(PORTB_PIN(0), GPIOCFG_IN);
72 
73  //configure IRQA mode: Rising and Falling edge triggered, digital filter enabled
74  // GPIO_INTCFGA =0x00000160;
75 
76  GPIO_INTCFGA = GPIO_INTCFGA_RESET;
77  GPIO_INTCFGA|=GPIO_INTFILT;
78  GPIO_INTCFGA|=(0x03 <<GPIO_INTMOD_BIT);
79 
80  halIrqAIsr();
81  //Enable IRQA interrupt
82  INT_CFGSET = INT_IRQA;
83 
84 
85  // printf("\r\n[Contact Sensor] Initialization Done\r\n");
86 
87 }/* end switch_init */
88 
89 
90 /* :::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::: */
91 /* :::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::: */
92 void halIrqAIsr(void)
93 {
94  //Clear Interrupt Pending Bit
95 
96  INT_GPIOFLAG = INT_IRQAFLAG;
97 
98  if ((GPIO_PBIN & (1)) == 0){
99  leds_on(LEDS_RED);
100  leds_off(LEDS_GREEN);
101  }else{
102  leds_on(LEDS_GREEN);
103  leds_off(LEDS_RED);
104  // switch_closed=TRUE;
105  }
106 }
107 
108 
109 /*---------------------------------------------------------------------------*/
110 static int
111 value(int type)
112 {
113  //printf("[Contact Sensor] Value=%d\r\n", GPIO_PBIN & (1));
114  return (GPIO_PBIN & (1));
115 
116 }
117 /*---------------------------------------------------------------------------*/
118 static int
119 configure(int type, int value)
120 {
121  switch(type){
122  case SENSORS_HW_INIT:
123  init();
124  return 1;
125  case SENSORS_ACTIVE:
126  return 1;
127  }
128 
129  return 0;
130 }
131 /*---------------------------------------------------------------------------*/
132 static int
133 status(int type)
134 {
135  switch(type) {
136 
137  case SENSORS_READY:
138  return 1;
139  }
140 
141  return 0;
142 }
143 /*---------------------------------------------------------------------------*/
144 SENSORS_SENSOR(contact_sensor, CONTACT_SENSOR,
145  value, configure, status);
146 
147 
148 
149 /** @} */
Contact magnetic sensor header file.
#define LEDS_RED
LED1 (Red) -&gt; PC0.
Definition: board.h:89
#define PORTC_PIN(y)
Some registers and variables require indentifying GPIO by a single number instead of the port and pin...
Definition: micro-common.h:48
#define LEDS_GREEN
LED3 (Green) -&gt; PC2.
Definition: board.h:81
Generic set of HAL includes for all platforms.
Return codes for API functions and module definitions.
#define PORTB_PIN(y)
Some registers and variables require indentifying GPIO by a single number instead of the port and pin...
Definition: micro-common.h:42
void halGpioConfig(uint32_t io, uint32_t config)
Configure an IO pin&#39;s operating mode.
Definition: micro-common.c:48