Contiki-NG
relay.c
Go to the documentation of this file.
1/*
2 * Copyright (c) 2016, 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 */
30/*---------------------------------------------------------------------------*/
31/**
32 * \addtogroup zoul-relay
33 * @{
34 *
35 * \file
36 * Driver for a relay actuator
37 */
38/*---------------------------------------------------------------------------*/
39#include "contiki.h"
40#include "relay.h"
41#include "dev/gpio.h"
42#include "lib/sensors.h"
43#include "dev/ioc.h"
44/*---------------------------------------------------------------------------*/
45#define RELAY_PORT_BASE GPIO_PORT_TO_BASE(RELAY_PORT)
46#define RELAY_PIN_MASK GPIO_PIN_MASK(RELAY_PIN)
47/*---------------------------------------------------------------------------*/
48static uint8_t enabled;
49/*---------------------------------------------------------------------------*/
50static int
51relay_on(void)
52{
53 if(enabled) {
54 GPIO_SET_PIN(RELAY_PORT_BASE, RELAY_PIN_MASK);
55 return RELAY_SUCCESS;
56 }
57 return RELAY_ERROR;
58}
59/*---------------------------------------------------------------------------*/
60static int
61relay_off(void)
62{
63 if(enabled) {
64 GPIO_CLR_PIN(RELAY_PORT_BASE, RELAY_PIN_MASK);
65 return RELAY_SUCCESS;
66 }
67 return RELAY_ERROR;
68}
69/*---------------------------------------------------------------------------*/
70static int
71status(int type)
72{
73 switch(type) {
74 case SENSORS_ACTIVE:
75 return GPIO_READ_PIN(RELAY_PORT_BASE, RELAY_PIN_MASK);
76 case SENSORS_READY:
77 return enabled;
78 }
79 return RELAY_ERROR;
80}
81/*---------------------------------------------------------------------------*/
82static int
83value(int type)
84{
85 switch(type) {
86 case RELAY_OFF:
87 return relay_on();
88 case RELAY_ON:
89 return relay_off();
90 case RELAY_TOGGLE:
91 if(status(SENSORS_ACTIVE)) {
92 return relay_off();
93 } else {
94 return relay_on();
95 }
96 default:
97 return RELAY_ERROR;
98 }
99}
100/*---------------------------------------------------------------------------*/
101static int
102configure(int type, int value)
103{
104 if(type != SENSORS_ACTIVE) {
105 return RELAY_ERROR;
106 }
107
108 if(value) {
109 GPIO_SOFTWARE_CONTROL(RELAY_PORT_BASE, RELAY_PIN_MASK);
110 GPIO_SET_OUTPUT(RELAY_PORT_BASE, RELAY_PIN_MASK);
111 ioc_set_over(RELAY_PORT, RELAY_PIN, IOC_OVERRIDE_OE);
112 GPIO_CLR_PIN(RELAY_PORT_BASE, RELAY_PIN_MASK);
113 enabled = 1;
114 return RELAY_SUCCESS;
115 }
116
117 GPIO_SET_INPUT(RELAY_PORT_BASE, RELAY_PIN_MASK);
118 enabled = 0;
119 return RELAY_SUCCESS;
120}
121/*---------------------------------------------------------------------------*/
122SENSORS_SENSOR(relay, RELAY_ACTUATOR, value, configure, status);
123/*---------------------------------------------------------------------------*/
124/** @} */
Header file with register and macro declarations for the cc2538 GPIO 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:258
#define GPIO_SET_INPUT(PORT_BASE, PIN_MASK)
Set pins with PIN_MASK of port with PORT_BASE to input.
Definition: gpio.h:78
#define GPIO_SET_PIN(PORT_BASE, PIN_MASK)
Set pins with PIN_MASK of port with PORT_BASE high.
Definition: gpio.h:106
#define GPIO_CLR_PIN(PORT_BASE, PIN_MASK)
Set pins with PIN_MASK of port with PORT_BASE low.
Definition: gpio.h:113
#define GPIO_SET_OUTPUT(PORT_BASE, PIN_MASK)
Set pins with PIN_MASK of port with PORT_BASE to output.
Definition: gpio.h:85
#define GPIO_READ_PIN(PORT_BASE, PIN_MASK)
Read pins with PIN_MASK of port with PORT_BASE.
Definition: gpio.h:147
#define IOC_OVERRIDE_OE
Output Enable.
Definition: ioc.h:222
void ioc_set_over(uint8_t port, uint8_t pin, uint8_t over)
Set Port:Pin override function.
Definition: ioc.c:54
Header file with declarations for the I/O Control module.
Header file for the generic relay driver.