2015-02-04 22:13:44 +01:00
|
|
|
/*
|
|
|
|
* Copyright (c) 2015, Zolertia - http://www.zolertia.com
|
|
|
|
* Copyright (c) 2015, University of Bristol - http://www.bristol.ac.uk
|
|
|
|
* 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 16:29:41 +01:00
|
|
|
* \addtogroup zoul-adc-sensors
|
2015-02-04 22:13:44 +01:00
|
|
|
* @{
|
|
|
|
*
|
|
|
|
* \file
|
2015-11-12 16:29:41 +01:00
|
|
|
* Generic driver for the Zoul ADC sensors
|
2015-02-04 22:13:44 +01:00
|
|
|
*/
|
|
|
|
/*---------------------------------------------------------------------------*/
|
|
|
|
#include "contiki.h"
|
|
|
|
#include "sys/clock.h"
|
|
|
|
#include "dev/ioc.h"
|
|
|
|
#include "dev/gpio.h"
|
|
|
|
#include "dev/adc.h"
|
2015-11-12 16:29:41 +01:00
|
|
|
#include "adc-sensors.h"
|
|
|
|
#include "zoul-sensors.h"
|
2016-01-12 01:04:21 +01:00
|
|
|
#include <stdio.h>
|
2015-02-04 22:13:44 +01:00
|
|
|
#include <stdint.h>
|
|
|
|
/*---------------------------------------------------------------------------*/
|
2016-01-12 01:04:21 +01:00
|
|
|
#define DEBUG 1
|
|
|
|
#if DEBUG
|
|
|
|
#define PRINTF(...) printf(__VA_ARGS__)
|
|
|
|
#else
|
|
|
|
#define PRINTF(...)
|
|
|
|
#endif
|
|
|
|
/*---------------------------------------------------------------------------*/
|
2015-02-04 22:13:44 +01:00
|
|
|
static uint8_t decimation_rate;
|
2015-11-12 16:29:41 +01:00
|
|
|
static uint8_t enabled_channels;
|
2015-02-04 22:13:44 +01:00
|
|
|
/*---------------------------------------------------------------------------*/
|
|
|
|
static int
|
|
|
|
set_decimation_rate(uint8_t rate)
|
|
|
|
{
|
|
|
|
switch(rate) {
|
|
|
|
case SOC_ADC_ADCCON_DIV_64:
|
|
|
|
case SOC_ADC_ADCCON_DIV_128:
|
|
|
|
case SOC_ADC_ADCCON_DIV_256:
|
|
|
|
case SOC_ADC_ADCCON_DIV_512:
|
|
|
|
decimation_rate = rate;
|
|
|
|
break;
|
|
|
|
default:
|
2015-11-12 16:29:41 +01:00
|
|
|
return ZOUL_SENSORS_ERROR;
|
2015-02-04 22:13:44 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return decimation_rate;
|
|
|
|
}
|
|
|
|
/*---------------------------------------------------------------------------*/
|
|
|
|
static int
|
2015-11-12 16:29:41 +01:00
|
|
|
get_channel_pin(int type)
|
|
|
|
{
|
|
|
|
if((ZOUL_SENSORS_ADC1) && (type == ZOUL_SENSORS_ADC1)) {
|
|
|
|
return SOC_ADC_ADCCON_CH_AIN0 + ADC_SENSORS_ADC1_PIN;
|
|
|
|
}
|
|
|
|
if((ZOUL_SENSORS_ADC2) && (type == ZOUL_SENSORS_ADC2)) {
|
|
|
|
return SOC_ADC_ADCCON_CH_AIN0 + ADC_SENSORS_ADC2_PIN;
|
|
|
|
}
|
|
|
|
if((ZOUL_SENSORS_ADC3) && (type == ZOUL_SENSORS_ADC3)) {
|
|
|
|
return SOC_ADC_ADCCON_CH_AIN0 + ADC_SENSORS_ADC3_PIN;
|
|
|
|
}
|
2016-01-12 01:04:21 +01:00
|
|
|
if((ZOUL_SENSORS_ADC4) && (type == ZOUL_SENSORS_ADC4)) {
|
|
|
|
return SOC_ADC_ADCCON_CH_AIN0 + ADC_SENSORS_ADC4_PIN;
|
|
|
|
}
|
|
|
|
if((ZOUL_SENSORS_ADC5) && (type == ZOUL_SENSORS_ADC5)) {
|
|
|
|
return SOC_ADC_ADCCON_CH_AIN0 + ADC_SENSORS_ADC5_PIN;
|
|
|
|
}
|
2015-11-12 16:29:41 +01:00
|
|
|
return ZOUL_SENSORS_ERROR;
|
|
|
|
}
|
|
|
|
/*---------------------------------------------------------------------------*/
|
|
|
|
static int
|
2015-02-04 22:13:44 +01:00
|
|
|
value(int type)
|
|
|
|
{
|
2015-11-12 16:29:41 +01:00
|
|
|
int channel;
|
2015-02-04 22:13:44 +01:00
|
|
|
int16_t res;
|
|
|
|
|
2015-11-12 16:29:41 +01:00
|
|
|
if(!(type & enabled_channels)) {
|
2016-01-12 01:04:21 +01:00
|
|
|
PRINTF("ADC: channel not enabled\n");
|
2015-11-12 16:29:41 +01:00
|
|
|
return ZOUL_SENSORS_ERROR;
|
2015-02-04 22:13:44 +01:00
|
|
|
}
|
|
|
|
|
2015-11-12 16:29:41 +01:00
|
|
|
channel = get_channel_pin(type);
|
|
|
|
|
|
|
|
if(channel == ZOUL_SENSORS_ERROR) {
|
2016-01-12 01:04:21 +01:00
|
|
|
PRINTF("ADC: pin not active\n");
|
2015-11-12 16:29:41 +01:00
|
|
|
return ZOUL_SENSORS_ERROR;
|
|
|
|
}
|
2015-02-04 22:13:44 +01:00
|
|
|
|
2016-01-12 01:04:21 +01:00
|
|
|
res = adc_get(channel, ADC_SENSORS_REFERENCE, decimation_rate);
|
|
|
|
|
|
|
|
/* Only allow negative values if using differential input */
|
|
|
|
if((ADC_SENSORS_REFERENCE != SOC_ADC_ADCCON_REF_EXT_DIFF) && (res < 0)) {
|
|
|
|
res = 0;
|
|
|
|
}
|
|
|
|
|
2015-02-04 22:13:44 +01:00
|
|
|
return res;
|
|
|
|
}
|
|
|
|
/*---------------------------------------------------------------------------*/
|
|
|
|
static int
|
|
|
|
configure(int type, int value)
|
|
|
|
{
|
|
|
|
switch(type) {
|
|
|
|
case SENSORS_HW_INIT:
|
|
|
|
|
2015-11-12 16:29:41 +01:00
|
|
|
/* This should filter out disabled sensors as its value should be zero */
|
|
|
|
if((value < ZOUL_SENSORS_ADC_MIN) || (value > ZOUL_SENSORS_ADC_ALL)) {
|
2016-01-12 01:04:21 +01:00
|
|
|
PRINTF("ADC: invalid adc pin mask (0x%02X)\n", value);
|
2015-11-12 16:29:41 +01:00
|
|
|
return ZOUL_SENSORS_ERROR;
|
|
|
|
}
|
|
|
|
|
2015-11-24 13:09:23 +01:00
|
|
|
if((value != ZOUL_SENSORS_ADC1) && (value != ZOUL_SENSORS_ADC2) &&
|
2016-01-12 01:04:21 +01:00
|
|
|
(value != ZOUL_SENSORS_ADC3) && (value != ZOUL_SENSORS_ADC4) &&
|
|
|
|
(value != ZOUL_SENSORS_ADC5)) {
|
|
|
|
PRINTF("ADC: invalid adc pin mask\n");
|
2015-11-12 16:29:41 +01:00
|
|
|
return ZOUL_SENSORS_ERROR;
|
|
|
|
}
|
|
|
|
|
|
|
|
GPIO_SOFTWARE_CONTROL(GPIO_A_BASE, value);
|
|
|
|
GPIO_SET_INPUT(GPIO_A_BASE, value);
|
|
|
|
|
|
|
|
if(value & ZOUL_SENSORS_ADC1) {
|
|
|
|
ioc_set_over(GPIO_A_NUM, ADC_SENSORS_ADC1_PIN, IOC_OVERRIDE_ANA);
|
|
|
|
}
|
|
|
|
if(value & ZOUL_SENSORS_ADC2) {
|
|
|
|
ioc_set_over(GPIO_A_NUM, ADC_SENSORS_ADC2_PIN, IOC_OVERRIDE_ANA);
|
|
|
|
}
|
|
|
|
if(value & ZOUL_SENSORS_ADC3) {
|
|
|
|
ioc_set_over(GPIO_A_NUM, ADC_SENSORS_ADC3_PIN, IOC_OVERRIDE_ANA);
|
|
|
|
}
|
2016-01-12 01:04:21 +01:00
|
|
|
if(value & ZOUL_SENSORS_ADC4) {
|
|
|
|
ioc_set_over(GPIO_A_NUM, ADC_SENSORS_ADC4_PIN, IOC_OVERRIDE_ANA);
|
|
|
|
}
|
|
|
|
if(value & ZOUL_SENSORS_ADC5) {
|
|
|
|
ioc_set_over(GPIO_A_NUM, ADC_SENSORS_ADC5_PIN, IOC_OVERRIDE_ANA);
|
|
|
|
}
|
2015-02-04 22:13:44 +01:00
|
|
|
adc_init();
|
|
|
|
set_decimation_rate(SOC_ADC_ADCCON_DIV_512);
|
2015-11-12 16:29:41 +01:00
|
|
|
enabled_channels = value;
|
2015-02-04 22:13:44 +01:00
|
|
|
break;
|
2015-11-12 16:29:41 +01:00
|
|
|
|
|
|
|
case ZOUL_SENSORS_CONFIGURE_TYPE_DECIMATION_RATE:
|
2015-02-04 22:13:44 +01:00
|
|
|
return set_decimation_rate((uint8_t)value);
|
2015-11-12 16:29:41 +01:00
|
|
|
|
|
|
|
default:
|
|
|
|
return ZOUL_SENSORS_ERROR;
|
2015-02-04 22:13:44 +01:00
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
/*---------------------------------------------------------------------------*/
|
|
|
|
static int
|
|
|
|
status(int type)
|
|
|
|
{
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
/*---------------------------------------------------------------------------*/
|
2015-11-12 16:29:41 +01:00
|
|
|
SENSORS_SENSOR(adc_sensors, ADC_SENSORS, value, configure, status);
|
2015-02-04 22:13:44 +01:00
|
|
|
/*---------------------------------------------------------------------------*/
|
|
|
|
/** @} */
|
2015-11-12 16:29:41 +01:00
|
|
|
|