Contiki 3.x
reed-relay.c
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2014, Texas Instruments Incorporated - http://www.ti.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 copyright holder nor the names of its
14  * contributors may be used to endorse or promote products derived
15  * from this software without specific prior written permission.
16  *
17  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
18  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
19  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
20  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
21  * COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
22  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
23  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
24  * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
25  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
26  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
27  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
28  * OF THE POSSIBILITY OF SUCH DAMAGE.
29  */
30 /*---------------------------------------------------------------------------*/
31 /**
32  * \addtogroup sensortag-cc26xx-reed-relay
33  * @{
34  *
35  * \file
36  * Driver for the Sensortag-CC26XX Reed Relay
37  */
38 /*---------------------------------------------------------------------------*/
39 #include "contiki.h"
40 #include "sys/clock.h"
41 #include "sys/timer.h"
42 #include "lib/sensors.h"
43 #include "sensortag/reed-relay.h"
44 #include "gpio-interrupt.h"
45 #include "sys/timer.h"
46 
47 #include "ti-lib.h"
48 
49 #include <stdint.h>
50 /*---------------------------------------------------------------------------*/
51 static struct timer debouncetimer;
52 /*---------------------------------------------------------------------------*/
53 #define REED_IO_CFG (IOC_CURRENT_2MA | IOC_STRENGTH_AUTO | \
54  IOC_IOPULL_DOWN | IOC_SLEW_DISABLE | \
55  IOC_HYST_DISABLE | IOC_BOTH_EDGES | \
56  IOC_INT_DISABLE | IOC_IOMODE_NORMAL | \
57  IOC_NO_WAKE_UP | IOC_INPUT_ENABLE)
58 /*---------------------------------------------------------------------------*/
59 /**
60  * \brief Handler for Sensortag-CC26XX reed interrupts
61  */
62 static void
64 {
65  if(!timer_expired(&debouncetimer)) {
66  ENERGEST_OFF(ENERGEST_TYPE_IRQ);
67  return;
68  }
69 
70  sensors_changed(&reed_relay_sensor);
71  timer_set(&debouncetimer, CLOCK_SECOND / 2);
72 }
73 /*---------------------------------------------------------------------------*/
74 static int
75 value(int type)
76 {
77  return (int)ti_lib_gpio_pin_read(1 << BOARD_IOID_REED_RELAY);
78 }
79 /*---------------------------------------------------------------------------*/
80 /**
81  * \brief Configuration function for the button sensor for all buttons.
82  *
83  * \param type SENSORS_HW_INIT: Initialise. SENSORS_ACTIVE: Enables/Disables
84  * depending on 'value'
85  * \param value 0: disable, non-zero: enable
86  * \return Always returns 1
87  */
88 static int
89 configure(int type, int value)
90 {
91  switch(type) {
92  case SENSORS_HW_INIT:
93  ti_lib_ioc_int_disable(BOARD_IOID_REED_RELAY);
94  ti_lib_gpio_event_clear(1 << BOARD_IOID_REED_RELAY);
95 
96  /* Enable the GPIO clock when the CM3 is running */
97  ti_lib_prcm_peripheral_run_enable(PRCM_PERIPH_GPIO);
98 
99  /* S/W control, input, pull-down */
100  ti_lib_ioc_port_configure_set(BOARD_IOID_REED_RELAY, IOC_PORT_GPIO,
101  REED_IO_CFG);
102 
103  gpio_interrupt_register_handler(BOARD_IOID_REED_RELAY,
105  break;
106  case SENSORS_ACTIVE:
107  if(value) {
108  ti_lib_ioc_int_enable(BOARD_IOID_REED_RELAY);
109  } else {
110  ti_lib_ioc_int_disable(BOARD_IOID_REED_RELAY);
111  }
112  break;
113  default:
114  break;
115  }
116  return 1;
117 }
118 /*---------------------------------------------------------------------------*/
119 /**
120  * \brief Status function for the reed
121  * \param type SENSORS_ACTIVE or SENSORS_READY
122  * \return 1 Interrupt enabled, 0: Disabled
123  */
124 static int
125 status(int type)
126 {
127  switch(type) {
128  case SENSORS_ACTIVE:
129  case SENSORS_READY:
130  return (ti_lib_ioc_port_configure_get(BOARD_IOID_REED_RELAY)
131  & IOC_INT_ENABLE) == IOC_INT_ENABLE;
132  break;
133  default:
134  break;
135  }
136  return 0;
137 }
138 /*---------------------------------------------------------------------------*/
139 SENSORS_SENSOR(reed_relay_sensor, "REED", value, configure, status);
140 /*---------------------------------------------------------------------------*/
141 /** @} */
Header file with macros which rename TI CC26xxware functions.
Timer library header file.
void timer_set(struct timer *t, clock_time_t interval)
Set a timer.
Definition: timer.c:64
Header file for the Sensortag-CC26XX Reed Relay.
A timer.
Definition: timer.h:86
Header file for the CC13xx/CC26xx GPIO interrupt management.
static int configure(int type, int value)
Configuration function for the button sensor for all buttons.
Definition: reed-relay.c:89
#define CLOCK_SECOND
A second, measured in system clock time.
Definition: clock.h:82
void gpio_interrupt_register_handler(uint8_t ioid, gpio_interrupt_handler_t f)
Register a GPIO interrupt handler.
static int status(int type)
Status function for the reed.
Definition: reed-relay.c:125
int timer_expired(struct timer *t)
Check if a timer has expired.
Definition: timer.c:122
static void reed_interrupt_handler(uint8_t ioid)
Handler for Sensortag-CC26XX reed interrupts.
Definition: reed-relay.c:63