Contiki-NG
Loading...
Searching...
No Matches
iaq.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 * This file is part of the Contiki operating system.
30 *
31 */
32/*---------------------------------------------------------------------------*/
33/**
34 * \addtogroup zoul-iaq-sensor
35 * @{
36 * Driver for the RE-Mote IAQ iAQ-Core (Indoor Air Quality Sensor)
37 * \file
38 * Driver for the RE-Mote RF IAQ iAQ-Core sensor (IAQ)
39 * \author
40 * Aitor Mejias <amejias@zolertia.com>
41 */
42/*---------------------------------------------------------------------------*/
43#include <stdio.h>
44#include "contiki.h"
45#include "dev/gpio.h"
46#include "dev/i2c.h"
47#include "iaq.h"
48#include "sys/timer.h"
49#include "sys/etimer.h"
50#include "lib/sensors.h"
51/*---------------------------------------------------------------------------*/
52#define DEBUG 0
53#if DEBUG
54#define PRINTF(...) printf(__VA_ARGS__)
55#else
56#define PRINTF(...)
57#endif
58/*---------------------------------------------------------------------------*/
59/* Callback pointers when interrupt occurs */
60void (*iaq_enable_callback)(uint16_t value);
61/*---------------------------------------------------------------------------*/
62static int16_t enabled;
63/*---------------------------------------------------------------------------*/
64static struct etimer et;
65static simple_iaq_data iaq_data;
66static uint8_t iaq_buffer[IAQ_FRAME_SIZE+1];
67/*---------------------------------------------------------------------------*/
68PROCESS(iaq_process, "IAQ process handler");
69/*---------------------------------------------------------------------------*/
70static int
71status(int type)
72{
73/* Return the status of the iAQ-Core or the status of the driver */
74 if (type == IAQ_STATUS) {
75 return (uint16_t)iaq_data.status;
76 } else if (type == IAQ_DRIVER_STATUS) {
77 return enabled;
78 }
79 return 0;
80}
81/*---------------------------------------------------------------------------*/
82PROCESS_THREAD(iaq_process, ev, data)
83{
84 #if DEBUG
85 uint8_t i = 0;
86 #endif
87
90 i2c_init(I2C_SDA_PORT, I2C_SDA_PIN, I2C_SCL_PORT, I2C_SCL_PIN,
91 I2C_SCL_NORMAL_BUS_SPEED);
92
93 while(1) {
94 etimer_set(&et, (IAQ_POLLING_TIME));
96
98 if(i2c_burst_receive(IAQ_ADDR, &iaq_buffer[0], IAQ_FRAME_SIZE) !=
99 I2C_MASTER_ERR_NONE) {
100 PRINTF("IAQ: Failed to retrieve data from IAQ\n");
101 enabled = IAQ_ERROR;
102 iaq_data.status = IAQ_INTERNAL_ERROR;
103 } else {
104 #if DEBUG
105 PRINTF("IAQ: Buffer ");
106 for (i=1;i<10;i++) {
107 PRINTF("[%d] %x, ", i-1, iaq_buffer[i-1]);
108 }
109 PRINTF("\n");
110 #endif
111 /* Update the status of the sensor. This value readed represents the
112 internal status of the external driver. */
113 switch (iaq_buffer[2]) {
114 case IAQ_INTERNAL_SUCCESS:
115 enabled = IAQ_ACTIVE;
116 break;
117 case IAQ_INTERNAL_RUNIN:
118 enabled = IAQ_INIT_STATE;
119 break;
120 case IAQ_INTERNAL_BUSY:
121 case IAQ_INTERNAL_ERROR:
122 enabled = IAQ_ERROR;
123 break;
124 default:
125 enabled = IAQ_ERROR;
126 break;
127 }
128
129 iaq_data.tvoc = ((uint16_t)iaq_buffer[0] << 8) + iaq_buffer[1];
130 iaq_data.co2 = ((uint16_t)iaq_buffer[7] << 8) + iaq_buffer[8];
131 iaq_data.status = iaq_buffer[2];
132 }
133 }
134 PROCESS_END();
135}
136/*---------------------------------------------------------------------------*/
137static int
138value(int type)
139{
140 if (!enabled) {
141 PRINTF("IAQ: Sensor not enabled\n");
142 return IAQ_ERROR;
143 }
144 if (enabled == IAQ_INIT_STATE) {
145 PRINTF("IAQ: Sensor initializing\n");
146 return IAQ_INIT_STATE;
147 }
148 if (type == IAQ_CO2_VALUE) {
149 return iaq_data.co2;
150 }
151 if (type == IAQ_VOC_VALUE) {
152 return iaq_data.tvoc;
153 }
154 if (type == IAQ_STATUS) {
155 #if DEBUG
156 switch (iaq_data.status) {
157 case IAQ_INTERNAL_SUCCESS:
158 PRINTF("IAQ Status: SUCCESS\n");
159 break;
160 case IAQ_INTERNAL_RUNIN:
161 PRINTF("IAQ Status: WARM UP\n");
162 break;
163 case IAQ_INTERNAL_BUSY:
164 case IAQ_INTERNAL_ERROR:
165 PRINTF("IAQ Status: ERROR\n");
166 break;
167 default:
168 PRINTF("IAQ Status: UNKNOWN STATUS %d\n", iaq_data.status);
169 break;
170 }
171 #endif
172 return iaq_data.status;
173 }
174
175 return IAQ_ERROR;
176}
177/*---------------------------------------------------------------------------*/
178static int
179configure(int type, int value)
180{
181 /* Check the current status. If is initialized or is active, return the same
182 state */
183 if ((enabled == IAQ_INIT_STATE) || (enabled == IAQ_ACTIVE)) {
184 return IAQ_ERROR;
185 }
186
187 /* Fix the status in initial wait status */
188 enabled = IAQ_INIT_STATE;
189
190 /* Start Internal process to measure the iAQ Sensor */
191 process_start(&iaq_process, NULL);
192
193 return enabled;
194}
195/*---------------------------------------------------------------------------*/
196/* name, type, value, configure, status */
197SENSORS_SENSOR(iaq, IAQ_SENSOR, value, configure, status);
198/*---------------------------------------------------------------------------*/
199/**
200 * @}
201 */
202
Event timer header file.
Header file with register and macro declarations for the cc2538 GPIO module.
void i2c_init(uint8_t port_sda, uint8_t pin_sda, uint8_t port_scl, uint8_t pin_scl, uint32_t bus_speed)
Initialize the I2C peripheral and pins.
Definition i2c.c:49
void i2c_master_enable(void)
Enable master I2C module.
Definition i2c.c:91
uint8_t i2c_burst_receive(uint8_t slave_addr, uint8_t *data, uint8_t len)
Perform all operations to receive multiple bytes from a slave.
Definition i2c.c:218
static bool etimer_expired(struct etimer *et)
Check if an event timer has expired.
Definition etimer.h:201
void etimer_set(struct etimer *et, clock_time_t interval)
Set an event timer.
Definition etimer.c:177
#define PROCESS(name, strname)
Declare a process.
Definition process.h:308
#define PROCESS_EXITHANDLER(handler)
Specify an action when a process exits.
Definition process.h:255
#define PROCESS_BEGIN()
Define the beginning of a process.
Definition process.h:121
#define PROCESS_WAIT_EVENT_UNTIL(c)
Wait for an event to be posted to the process, with an extra condition.
Definition process.h:158
#define PROCESS_END()
Define the end of a process.
Definition process.h:132
void process_start(struct process *p, process_data_t data)
Start a process.
Definition process.c:121
#define PROCESS_THREAD(name, ev, data)
Define the body of a process.
Definition process.h:274
Header file with declarations for the I2C Control module.
Header file for the RE-Mote Sensor IAQ.
A timer.
Definition etimer.h:79
Timer library header file.