Contiki-NG
Loading...
Searching...
No Matches
pm10-sensor.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-pm10-sensor
35 * @{
36 *
37 * \file
38 * GP2Y1010AU0F PM10 sensor example using the ADC sensors wrapper
39 * \author
40 * Toni Lozano <tlozano@zolertia.com>
41 */
42/*---------------------------------------------------------------------------*/
43#include <stdio.h>
44#include "contiki.h"
45#include "adc-sensors.h"
46#include "adc-zoul.h"
47#include "zoul-sensors.h"
48#include "dev/pm10-sensor.h"
49#include "dev/sys-ctrl.h"
50#include "lib/sensors.h"
51#include "dev/gpio.h"
52#include "dev/ioc.h"
53/*---------------------------------------------------------------------------*/
54#define PM10_SENSOR_PORT_BASE GPIO_PORT_TO_BASE(PM10_SENSOR_CTRL_PORT)
55#define PM10_SENSOR_PIN_MASK GPIO_PIN_MASK(PM10_SENSOR_CTRL_PIN)
56/*---------------------------------------------------------------------------*/
57static int pm10_channel;
58/*---------------------------------------------------------------------------*/
59static int
60configure(int type, int value)
61{
62 if(type != SENSORS_ACTIVE) {
63 return PM10_ERROR;
64 }
65
66 if(value) {
67 /* Set as output, used as pulse-driven wave */
68 GPIO_SOFTWARE_CONTROL(PM10_SENSOR_PORT_BASE, PM10_SENSOR_PIN_MASK);
69 ioc_set_over(PM10_SENSOR_CTRL_PORT, PM10_SENSOR_CTRL_PIN, IOC_OVERRIDE_DIS);
70 GPIO_SET_OUTPUT(PM10_SENSOR_PORT_BASE, PM10_SENSOR_PIN_MASK);
71 GPIO_CLR_PIN(PM10_SENSOR_PORT_BASE, PM10_SENSOR_PIN_MASK);
72
73 pm10_channel = (1 << value);
74 return adc_zoul.configure(SENSORS_HW_INIT, pm10_channel);
75 }
76
77 pm10_channel = 0;
78 return PM10_SUCCESS;
79}
80/*---------------------------------------------------------------------------*/
81static int
82value(int type)
83{
84 uint32_t val;
85
86 if(!pm10_channel) {
87 return PM10_ERROR;
88 }
89
90 /* Set Pulse Wave pin before measure */
91 GPIO_SET_PIN(PM10_SENSOR_PORT_BASE, PM10_SENSOR_PIN_MASK);
92 /* Pulse wave delay */
93 clock_delay_usec(PM10_SENSOR_PULSE_DELAY);
94 /* Data acquisition */
95 val = (uint32_t)adc_zoul.value(pm10_channel);
96
97 if(val == ZOUL_SENSORS_ERROR) {
98 printf("PM10 sensor: failed retrieving data\n");
99 return PM10_ERROR;
100 }
101
102 /* Default voltage divisor relation is 5/3 aprox, change at adc_wrapper.h,
103 * calculations below assume a decimation rate of 512 (12 bits ENOB) and
104 * AVVD5 voltage reference of 3.3V
105 */
106 val *= PM10_EXTERNAL_VREF;
107 val /= PM10_EXTERNAL_VREF_CROSSVAL;
108
109 /* Applied constant conversion from UAir project
110 * to obtain value in ppm (value in mV * 0.28)
111 */
112 val *= 28;
113 val /= 1000;
114
115 /* Clear pulse wave pin */
116 GPIO_CLR_PIN(PM10_SENSOR_PORT_BASE, PM10_SENSOR_PIN_MASK);
117
118 return (uint16_t)val;
119}
120/*---------------------------------------------------------------------------*/
121SENSORS_SENSOR(pm10, PM10_SENSOR, value, configure, NULL);
122/*---------------------------------------------------------------------------*/
123/** @} */
Header file for the Zoul ADC sensors API.
Header file for the Zoul ADC interface.
Header file with register and macro declarations for the cc2538 GPIO module.
void clock_delay_usec(uint16_t dt)
Delay a given number of microseconds.
Definition clock.c:150
#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_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
void ioc_set_over(uint8_t port, uint8_t pin, uint8_t over)
Set Port:Pin override function.
Definition ioc.c:54
#define IOC_OVERRIDE_DIS
Override Disabled.
Definition ioc.h:226
Header file with declarations for the I/O Control module.
GP2Y1010AU0F PM10 sensor driver.
Header file for the cc2538 System Control driver.
Implementation of a generic module controlling Zoul sensors.