1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256
|
/* $Id: Atm128AdcP.nc,v 1.8 2010-06-29 22:07:43 scipio Exp $
* Copyright (c) 2000-2003 The Regents of the University of California.
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* - Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* - 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.
* - Neither the name of the University of California 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.
*
* Copyright (c) 2002-2005 Intel Corporation
* All rights reserved.
*
* This file is distributed under the terms in the attached INTEL-LICENSE
* file. If you do not find these files, copies can be found by writing to
* Intel Research Berkeley, 2150 Shattuck Avenue, Suite 1300, Berkeley, CA,
* 94704. Attention: Intel License Inquiry.
*
* Copyright (c) 2004-2005 Crossbow Technology, Inc. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* - Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* - 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.
* - Neither the name of Crossbow Technology 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.
*/
#include "Atm128Adc.h"
/**
* Internal component of the Atmega128 A/D HAL.
*
* @author Jason Hill
* @author David Gay
* @author Philip Levis
* @author Phil Buonadonna
* @author Hu Siquan <husq@xbow.com>
* @author Janos Sallai <janos.sallai@vanderbilt.edu>
* @author Andras Biro <bbandi86@gmail.com>
*/
module Atm128AdcP @safe()
{
provides {
interface Init;
interface AsyncStdControl;
interface Atm128AdcSingle;
interface Atm128AdcMultiple;
}
uses {
interface HplAtm128Adc;
interface Atm128Calibrate;
}
}
implementation
{
/* State for the current and next (multiple-sampling only) conversion */
struct {
bool multiple : 1; /* single and multiple-sampling mode */
bool precise : 1; /* is this result going to be precise? */
uint8_t channel : 6; /* what channel did this sample come from? */
} f, nextF;
command error_t Init.init() {
atomic
{
Atm128Adcsra_t adcsr;
adcsr.aden = ATM128_ADC_ENABLE_OFF;
adcsr.adsc = ATM128_ADC_START_CONVERSION_OFF;
adcsr.adate = ATM128_ADC_FREE_RUNNING_OFF;
adcsr.adif = ATM128_ADC_INT_FLAG_OFF;
adcsr.adie = ATM128_ADC_INT_ENABLE_OFF;
adcsr.adps = ATM128_ADC_PRESCALE_2;
call HplAtm128Adc.setAdcsra(adcsr);
}
return SUCCESS;
}
/* We enable the A/D when start is called, and disable it when stop is
* called. This drops A/D conversion latency by a factor of two (but
* increases idle mode power consumption a little).
*/
async command error_t AsyncStdControl.start() {
atomic call HplAtm128Adc.enableAdc();
return SUCCESS;
}
async command error_t AsyncStdControl.stop() {
atomic call HplAtm128Adc.disableAdc();
return SUCCESS;
}
/* Return TRUE if switching to 'channel' with reference voltage 'refVoltage'
* will give a precise result (the first sample after changing reference
* voltage or switching to/between a differential channel is imprecise)
*/
inline bool isPrecise(uint8_t currentChannel, uint8_t currentRefVoltage, uint8_t channel, uint8_t refVoltage) {
return refVoltage == currentRefVoltage && (channel <= ATM128_ADC_SNGL_ADC7 || channel >= ATM128_ADC_SNGL_1_23 || channel == currentChannel);
}
async event void HplAtm128Adc.dataReady(uint16_t data) {
bool precise, multiple;
uint8_t channel;
atomic
{
channel = f.channel;
precise = f.precise;
multiple = f.multiple;
}
if (!multiple)
{
/* A single sample. Disable the ADC interrupt to avoid starting
* a new sample at the next "sleep" instruction. */
call HplAtm128Adc.disableInterruption();
signal Atm128AdcSingle.dataReady(data, precise);
}
else
{
/* Multiple sampling. The user can:
* - tell us to stop sampling
* - or, to continue sampling on a new channel, possibly with a
* new reference voltage; however this change applies not to
* the next sample (the hardware has already started working on
* that), but on the one after.
*/
bool cont;
uint8_t nextChannel, nextVoltage;
atomic
{
nextVoltage = call HplAtm128Adc.getRef();
nextChannel = call HplAtm128Adc.getChannel();
}
cont = signal Atm128AdcMultiple.dataReady(data, precise, channel, &nextChannel, &nextVoltage);
atomic
if (cont)
{
/* Switch channels and update our internal channel+precision
* tracking state (f and nextF). Note that this tracking will
* be incorrect if we take too long to get to this point. */
call HplAtm128Adc.setChannel(nextChannel);
call HplAtm128Adc.setRef(nextVoltage);
f = nextF;
nextF.channel = nextChannel;
nextF.precise = isPrecise(call HplAtm128Adc.getChannel(), call HplAtm128Adc.getRef(), nextChannel, nextVoltage);
}
else
call HplAtm128Adc.cancel();
}
}
/* Start sampling based on request parameters */
void getData(uint8_t channel, uint8_t refVoltage, bool leftJustify, uint8_t prescaler) {
Atm128Adcsra_t adcsr;
f.precise = isPrecise(call HplAtm128Adc.getChannel(), call HplAtm128Adc.getRef(), channel, refVoltage);
f.channel = channel;
call HplAtm128Adc.setChannel(channel);
call HplAtm128Adc.setAdlar(leftJustify);
call HplAtm128Adc.setRef(refVoltage);
adcsr.aden = ATM128_ADC_ENABLE_ON;
adcsr.adsc = ATM128_ADC_START_CONVERSION_ON;
adcsr.adate = f.multiple;
adcsr.adif = ATM128_ADC_INT_FLAG_ON; // clear any stale flag
adcsr.adie = ATM128_ADC_INT_ENABLE_ON;
if (prescaler == ATM128_ADC_PRESCALE)
prescaler = call Atm128Calibrate.adcPrescaler();
adcsr.adps = prescaler;
call HplAtm128Adc.setAdcsra(adcsr);
}
async command bool Atm128AdcSingle.getData(uint8_t channel, uint8_t refVoltage, bool leftJustify, uint8_t prescaler) {
atomic
{
f.multiple = FALSE;
getData(channel, refVoltage, leftJustify, prescaler);
return f.precise;
}
}
async command bool Atm128AdcSingle.cancel() {
/* There is no Atm128AdcMultiple.cancel, for reasons discussed in that
* interface */
return call HplAtm128Adc.cancel();
}
async command bool Atm128AdcMultiple.getData(uint8_t channel, uint8_t refVoltage, bool leftJustify, uint8_t prescaler) {
atomic
{
f.multiple = TRUE;
getData(channel, refVoltage, leftJustify, prescaler);
nextF = f;
/* We assume the 2nd sample is precise */
nextF.precise = TRUE;
return f.precise;
}
}
default async event void Atm128AdcSingle.dataReady(uint16_t data, bool precise) {
}
default async event bool Atm128AdcMultiple.dataReady(uint16_t data, bool precise, uint8_t channel, uint8_t *newChannel, uint8_t *newRefVoltage) {
return FALSE; // stop conversion if we somehow end up here.
}
}
|