2015-02-04 21:13:44 +00:00
|
|
|
/*
|
2016-01-17 22:04:34 +00:00
|
|
|
* Copyright (c) 2016, Zolertia - http://www.zolertia.com
|
2015-02-04 21:13:44 +00:00
|
|
|
* All rights reserved.
|
|
|
|
*
|
|
|
|
* Redistribution and use in source and binary forms, with or without
|
|
|
|
* modification, are permitted provided that the following conditions
|
|
|
|
* are met:
|
|
|
|
* 1. Redistributions of source code must retain the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer.
|
|
|
|
* 2. Redistributions in binary form must reproduce the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer in the
|
|
|
|
* documentation and/or other materials provided with the distribution.
|
|
|
|
*
|
|
|
|
* 3. Neither the name of the copyright holder nor the names of its
|
|
|
|
* contributors may be used to endorse or promote products derived
|
|
|
|
* from this software without specific prior written permission.
|
|
|
|
*
|
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
|
|
|
|
* ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
|
|
|
|
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
|
|
|
|
* FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
|
|
|
|
* COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
|
|
|
|
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
|
|
|
|
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
|
|
|
|
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
|
|
|
|
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
|
|
|
|
* STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
|
|
|
|
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
|
|
|
|
* OF THE POSSIBILITY OF SUCH DAMAGE.
|
|
|
|
*/
|
|
|
|
/*---------------------------------------------------------------------------*/
|
|
|
|
/**
|
2015-11-12 15:29:41 +00:00
|
|
|
* \addtogroup zoul-adc-sensors
|
2015-02-04 21:13:44 +00:00
|
|
|
* @{
|
|
|
|
*
|
|
|
|
* \file
|
2016-01-17 22:04:34 +00:00
|
|
|
* Generic driver for the Zoul ADC wrapper for analogue sensors
|
2015-02-04 21:13:44 +00:00
|
|
|
*/
|
|
|
|
/*---------------------------------------------------------------------------*/
|
|
|
|
#include "contiki.h"
|
|
|
|
#include "sys/clock.h"
|
|
|
|
#include "dev/ioc.h"
|
|
|
|
#include "dev/gpio.h"
|
|
|
|
#include "dev/adc.h"
|
2015-11-12 15:29:41 +00:00
|
|
|
#include "adc-sensors.h"
|
2016-01-17 22:04:34 +00:00
|
|
|
#include "adc-zoul.h"
|
2015-11-12 15:29:41 +00:00
|
|
|
#include "zoul-sensors.h"
|
2016-01-12 00:04:21 +00:00
|
|
|
#include <stdio.h>
|
2015-02-04 21:13:44 +00:00
|
|
|
#include <stdint.h>
|
|
|
|
/*---------------------------------------------------------------------------*/
|
2016-01-19 21:28:09 +00:00
|
|
|
#define DEBUG 0
|
2016-01-12 00:04:21 +00:00
|
|
|
#if DEBUG
|
|
|
|
#define PRINTF(...) printf(__VA_ARGS__)
|
|
|
|
#else
|
|
|
|
#define PRINTF(...)
|
|
|
|
#endif
|
|
|
|
/*---------------------------------------------------------------------------*/
|
2016-01-17 22:04:34 +00:00
|
|
|
typedef struct {
|
|
|
|
int type;
|
|
|
|
uint8_t pin_mask;
|
|
|
|
uint8_t vdd3;
|
|
|
|
} adc_info_t;
|
|
|
|
|
|
|
|
typedef struct {
|
|
|
|
uint8_t sensors_num;
|
|
|
|
uint8_t sensors_ports;
|
|
|
|
adc_info_t sensor[ADC_SENSORS_MAX];
|
|
|
|
} adc_wrapper_t;
|
|
|
|
|
|
|
|
static adc_wrapper_t sensors;
|
2015-02-04 21:13:44 +00:00
|
|
|
/*---------------------------------------------------------------------------*/
|
2016-01-17 22:04:34 +00:00
|
|
|
static uint16_t
|
|
|
|
convert_to_value(uint8_t index)
|
2015-02-04 21:13:44 +00:00
|
|
|
{
|
2016-01-17 22:04:34 +00:00
|
|
|
uint32_t value;
|
|
|
|
value = adc_zoul.value(sensors.sensor[index].pin_mask);
|
|
|
|
|
|
|
|
if(value == ZOUL_SENSORS_ERROR) {
|
|
|
|
PRINTF("ADC sensors: failed retrieving data\n");
|
|
|
|
return ADC_WRAPPER_ERROR;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Default voltage divisor relation is 5/3 aprox, change at adc_wrapper.h,
|
|
|
|
* calculations below assume a decimation rate of 512 (12 bits ENOB) and
|
|
|
|
* AVVD5 voltage reference of 3.3V
|
|
|
|
*/
|
|
|
|
|
|
|
|
if(!sensors.sensor[index].vdd3) {
|
|
|
|
value *= ADC_WRAPPER_EXTERNAL_VREF;
|
|
|
|
value /= ADC_WRAPPER_EXTERNAL_VREF_CROSSVAL;
|
|
|
|
}
|
|
|
|
|
|
|
|
switch(sensors.sensor[index].type) {
|
|
|
|
case ANALOG_GROVE_LIGHT:
|
|
|
|
/* Light dependant resistor (LDR) resistance value*/
|
|
|
|
value = (10230 - (value * 10)) / value;
|
|
|
|
/* TODO: With the resistance we could calculate the lux as 63*R^(-0.7) */
|
|
|
|
return (uint16_t)value;
|
|
|
|
|
|
|
|
case ANALOG_GROVE_LOUDNESS:
|
|
|
|
/* Based on the LM2904 amplifier (blue version with potentiometer) */
|
|
|
|
return (uint16_t)value;
|
|
|
|
|
|
|
|
case ANALOG_PHIDGET_ROTATION_1109:
|
|
|
|
/* Linear sensor with 0-300º, 300/33000 = 0.00909 */
|
|
|
|
value *= 909;
|
|
|
|
value /= 100000;
|
|
|
|
return (uint16_t)value;
|
|
|
|
|
2016-01-22 11:15:33 +00:00
|
|
|
/* VDD+5 sensors */
|
2016-01-20 14:50:56 +00:00
|
|
|
case ANALOG_VAC_SENSOR:
|
|
|
|
/* Linear sensor from 0 to 5 V; 0.0088 resolution*/
|
|
|
|
value *= 88;
|
|
|
|
value /= 10000;
|
|
|
|
return (uint16_t)value;
|
|
|
|
|
2016-01-21 14:24:47 +00:00
|
|
|
case ANALOG_AAC_SENSOR:
|
|
|
|
/* Linear sensor from 0 to 5 V;*/
|
|
|
|
return (uint16_t)value;
|
|
|
|
|
2015-02-04 21:13:44 +00:00
|
|
|
default:
|
2016-01-17 22:04:34 +00:00
|
|
|
return ADC_WRAPPER_ERROR;
|
2015-02-04 21:13:44 +00:00
|
|
|
}
|
|
|
|
|
2016-01-17 22:04:34 +00:00
|
|
|
return ADC_WRAPPER_ERROR;
|
2015-02-04 21:13:44 +00:00
|
|
|
}
|
|
|
|
/*---------------------------------------------------------------------------*/
|
2016-01-17 22:04:34 +00:00
|
|
|
static uint8_t
|
|
|
|
is_sensor_in_list(int type)
|
2015-11-12 15:29:41 +00:00
|
|
|
{
|
2016-01-17 22:04:34 +00:00
|
|
|
uint8_t i;
|
|
|
|
|
|
|
|
for(i = 0; i <= sensors.sensors_num; i++) {
|
|
|
|
if(sensors.sensor[i].type == type) {
|
|
|
|
return i + 1;
|
|
|
|
}
|
2016-01-12 00:04:21 +00:00
|
|
|
}
|
2016-01-17 22:04:34 +00:00
|
|
|
return 0;
|
2015-11-12 15:29:41 +00:00
|
|
|
}
|
|
|
|
/*---------------------------------------------------------------------------*/
|
|
|
|
static int
|
2015-02-04 21:13:44 +00:00
|
|
|
value(int type)
|
|
|
|
{
|
2016-01-17 22:04:34 +00:00
|
|
|
uint8_t index;
|
|
|
|
uint16_t sensor_value;
|
2015-02-04 21:13:44 +00:00
|
|
|
|
2016-01-17 22:04:34 +00:00
|
|
|
index = is_sensor_in_list(type);
|
2015-11-12 15:29:41 +00:00
|
|
|
|
2016-01-17 22:04:34 +00:00
|
|
|
if(!index) {
|
|
|
|
PRINTF("ADC sensors: sensor not registered\n");
|
|
|
|
return ADC_WRAPPER_SUCCESS;
|
2015-11-12 15:29:41 +00:00
|
|
|
}
|
2015-02-04 21:13:44 +00:00
|
|
|
|
2016-01-17 22:04:34 +00:00
|
|
|
/* Restore index value after the check */
|
|
|
|
index -= 1;
|
|
|
|
sensor_value = convert_to_value(index);
|
2016-01-12 00:04:21 +00:00
|
|
|
|
2016-01-17 22:04:34 +00:00
|
|
|
return sensor_value;
|
2015-02-04 21:13:44 +00:00
|
|
|
}
|
|
|
|
/*---------------------------------------------------------------------------*/
|
|
|
|
static int
|
|
|
|
configure(int type, int value)
|
|
|
|
{
|
2016-01-17 22:04:34 +00:00
|
|
|
uint8_t pin_mask = GPIO_PIN_MASK(value);
|
2015-02-04 21:13:44 +00:00
|
|
|
|
2016-01-17 22:04:34 +00:00
|
|
|
if((type != ANALOG_GROVE_LIGHT) && (type != ANALOG_PHIDGET_ROTATION_1109) &&
|
2016-02-15 13:45:26 +00:00
|
|
|
(type != ANALOG_GROVE_LOUDNESS) && (type != ANALOG_VAC_SENSOR) &&
|
|
|
|
(type != ANALOG_AAC_SENSOR) ) {
|
2016-01-17 22:04:34 +00:00
|
|
|
PRINTF("ADC sensors: sensor not supported, check adc_wrapper.h header\n");
|
|
|
|
return ADC_WRAPPER_ERROR;
|
|
|
|
}
|
2015-11-12 15:29:41 +00:00
|
|
|
|
2016-01-17 22:04:34 +00:00
|
|
|
if(sensors.sensors_num >= ADC_SENSORS_MAX) {
|
|
|
|
PRINTF("ADC sensors: all adc channels available have been assigned\n");
|
|
|
|
return ADC_WRAPPER_ERROR;
|
|
|
|
}
|
2015-11-12 15:29:41 +00:00
|
|
|
|
2016-04-04 08:48:43 +00:00
|
|
|
if((value < 0x01) || (value > 0x07) || ((value == BUTTON_USER_PIN) && (ADC_SENSORS_ADC6_PIN < 0))) {
|
2016-01-17 22:04:34 +00:00
|
|
|
PRINTF("ADC sensors: invalid pin value, (PA0-PA1, PA3) are reserved\n");
|
|
|
|
return ADC_WRAPPER_ERROR;
|
|
|
|
}
|
2015-11-12 15:29:41 +00:00
|
|
|
|
2016-01-17 22:04:34 +00:00
|
|
|
if(sensors.sensors_ports & pin_mask) {
|
|
|
|
PRINTF("ADC sensors: a sensor has been already assigned to this pin\n");
|
|
|
|
return ADC_WRAPPER_ERROR;
|
|
|
|
}
|
|
|
|
|
|
|
|
switch(type) {
|
|
|
|
/* V+3.3 sensors */
|
|
|
|
case ANALOG_GROVE_LIGHT:
|
|
|
|
case ANALOG_GROVE_LOUDNESS:
|
|
|
|
case ANALOG_PHIDGET_ROTATION_1109:
|
|
|
|
if(adc_zoul.configure(SENSORS_HW_INIT, pin_mask) == ZOUL_SENSORS_ERROR) {
|
|
|
|
return ADC_WRAPPER_ERROR;
|
2016-01-12 00:04:21 +00:00
|
|
|
}
|
2016-01-17 22:04:34 +00:00
|
|
|
sensors.sensor[sensors.sensors_num].type = type;
|
|
|
|
sensors.sensor[sensors.sensors_num].pin_mask = pin_mask;
|
|
|
|
sensors.sensor[sensors.sensors_num].vdd3 = 1;
|
2015-02-04 21:13:44 +00:00
|
|
|
break;
|
2015-11-12 15:29:41 +00:00
|
|
|
|
2016-01-22 11:15:33 +00:00
|
|
|
/*V+5 sensors*/
|
2016-01-21 14:24:47 +00:00
|
|
|
case ANALOG_VAC_SENSOR:
|
|
|
|
case ANALOG_AAC_SENSOR:
|
|
|
|
if(adc_zoul.configure(SENSORS_HW_INIT, pin_mask) == ZOUL_SENSORS_ERROR) {
|
|
|
|
return ADC_WRAPPER_ERROR;
|
|
|
|
}
|
|
|
|
sensors.sensor[sensors.sensors_num].type = type;
|
|
|
|
sensors.sensor[sensors.sensors_num].pin_mask = pin_mask;
|
|
|
|
sensors.sensor[sensors.sensors_num].vdd3 = 0;
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
2015-11-12 15:29:41 +00:00
|
|
|
default:
|
2016-01-17 22:04:34 +00:00
|
|
|
return ADC_WRAPPER_ERROR;
|
2015-02-04 21:13:44 +00:00
|
|
|
}
|
2016-01-17 22:04:34 +00:00
|
|
|
|
|
|
|
PRINTF("ADC sensors: type %u mask 0x%02X vdd3 %u\n",
|
|
|
|
sensors.sensor[sensors.sensors_num].type,
|
|
|
|
sensors.sensor[sensors.sensors_num].pin_mask,
|
|
|
|
sensors.sensor[sensors.sensors_num].vdd3);
|
|
|
|
|
|
|
|
sensors.sensors_num++;
|
|
|
|
sensors.sensors_ports |= pin_mask;
|
|
|
|
|
|
|
|
return ADC_WRAPPER_SUCCESS;
|
2015-02-04 21:13:44 +00:00
|
|
|
}
|
|
|
|
/*---------------------------------------------------------------------------*/
|
2016-01-17 22:04:34 +00:00
|
|
|
SENSORS_SENSOR(adc_sensors, ADC_SENSORS, value, configure, NULL);
|
2015-02-04 21:13:44 +00:00
|
|
|
/*---------------------------------------------------------------------------*/
|
|
|
|
/** @} */
|
2015-11-12 15:29:41 +00:00
|
|
|
|