📄 adcrefm.nc
字号:
// $Id: ADCREFM.nc,v 1.1.4.6 2003/08/26 09:08:16 cssharp Exp $/* tab:4 * "Copyright (c) 2000-2003 The Regents of the University of California. * All rights reserved. * * Permission to use, copy, modify, and distribute this software and its * documentation for any purpose, without fee, and without written agreement is * hereby granted, provided that the above copyright notice, the following * two paragraphs and the author appear in all copies of this software. * * IN NO EVENT SHALL THE UNIVERSITY OF CALIFORNIA BE LIABLE TO ANY PARTY FOR * DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES ARISING OUT * OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN IF THE UNIVERSITY OF * CALIFORNIA HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. * * THE UNIVERSITY OF CALIFORNIA SPECIFICALLY DISCLAIMS ANY WARRANTIES, * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY * AND FITNESS FOR A PARTICULAR PURPOSE. THE SOFTWARE PROVIDED HEREUNDER IS * ON AN "AS IS" BASIS, AND THE UNIVERSITY OF CALIFORNIA HAS NO OBLIGATION TO * PROVIDE MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS." * * Copyright (c) 2002-2003 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. *//* * * Authors: Jason Hill, David Gay, Philip Levis, Phil Buonadonna * Version: $Id: ADCREFM.nc,v 1.1.4.6 2003/08/26 09:08:16 cssharp Exp $ * *//* OS component abstraction of the analog to digital converter using a * fixed reference input. I assumes the presence of a TOS_ADC_BANDGAP_PORT * to provide that referenced reading. This module was designed to * accomodate platforms that use varying/unstable ADC references. It also * works around limitations where the measured variable cannot be larger than * the actual ADC reference * * The conversion result is given by the equation: * * ADC = (Vport * 1024) / Vref * * Where Vport can be between zero and (2^6-1)*Vref (I.E. Vport CAN be larger * than Vref) * * Note: On the ATmega128, Vref (using this module) is 1.23 Volts *//* ADC_INIT command initializes the device *//* ADC_GET_DATA command initiates acquiring a sensor reading. *//* It returns immediately. *//* ADC_DATA_READY is signaled, providing data, when it becomes *//* available. *//* Access to the sensor is performed in the background by a separate *//* TOS task. *//** * @author Jason Hill * @author David Gay * @author Philip Levis * @author Phil Buonadonna */module ADCREFM { provides { interface ADC[uint8_t port]; interface ADCControl; } uses interface HPLADC;}implementation{ enum { IDLE = 0, SINGLE_CONVERSION = 1, CONTINUOUS_CONVERSION = 2, }; uint16_t ReqPort; uint16_t ReqVector; uint16_t ContReqMask; uint32_t RefVal; command result_t ADCControl.init() { atomic { ReqPort = 0; ReqVector = ContReqMask = 0; RefVal = 381; // Reference value assuming 3.3 Volt power source } dbg(DBG_BOOT, ("ADC initialized.\n")); return call HPLADC.init(); } command result_t ADCControl.setSamplingRate(uint8_t rate) { return call HPLADC.setSamplingRate(rate); } command result_t ADCControl.bindPort(uint8_t port, uint8_t adcPort) { return call HPLADC.bindPort(port, adcPort); } default async event result_t ADC.dataReady[uint8_t port](uint16_t data) { return FAIL; // ensures ADC is disabled if no handler } async event result_t HPLADC.dataReady(uint16_t data) { uint16_t doneValue = data; uint8_t donePort; uint8_t nextPort = 0xff; result_t Result = SUCCESS; if (ReqPort == TOS_ADC_BANDGAP_PORT) { RefVal = data; } // BEGIN atomic atomic { donePort = ReqPort; // Check to see if this port has requested continous conversion if (((1<<donePort) & ContReqMask) == 0) { ReqVector ^= (1<<donePort); } if (ReqVector) { // Always ensure we rotate through the reference port //ReqVector |= (1<<TOS_ADC_BANDGAP_PORT); do { ReqPort++; ReqPort = (ReqPort == TOSH_ADC_PORTMAPSIZE) ? 0 : ReqPort; } while (((1<<ReqPort) & ReqVector) == 0); nextPort = ReqPort; } } // END atomic if (nextPort != 0xff) { call HPLADC.samplePort(nextPort); // This function is interupt-safe } dbg(DBG_ADC, "adc_tick: port %d with value %i \n", donePort, (int)data); if (donePort != TOS_ADC_BANDGAP_PORT) { uint32_t tmp = (uint32_t) data; tmp = tmp << 10; // data * 1024 tmp = (tmp / RefVal); // doneValue = data * 1024/ref doneValue = (uint16_t) tmp; Result = signal ADC.dataReady[donePort](doneValue); } atomic { if ((Result == FAIL) && (ContReqMask & (1<<donePort))) { ContReqMask ^= (1<<donePort); } } return SUCCESS; } result_t startGet(uint8_t newState, uint8_t port) { uint16_t PortMask, oldReqVector = 1; result_t Result = SUCCESS; if (port > TOSH_ADC_PORTMAPSIZE) { return FAIL; } PortMask = (1<<port); // BEGIN atomic atomic { if ((PortMask & ReqVector) != 0) { // Already a pending request Result = FAIL; } else { oldReqVector = ReqVector; ReqVector |= PortMask; if (newState == CONTINUOUS_CONVERSION) { ContReqMask |= PortMask; } if (oldReqVector == 0) { // ADC was idle. Initiate a port sample ReqVector |= (1 << TOS_ADC_BANDGAP_PORT); ReqPort = TOS_ADC_BANDGAP_PORT; //port; } } } // END atomic if (oldReqVector == 0) { Result = call HPLADC.samplePort(TOS_ADC_BANDGAP_PORT /*port*/); } return Result; } async command result_t ADC.getData[uint8_t port]() { return startGet(SINGLE_CONVERSION, port); } async command result_t ADC.getContinuousData[uint8_t port]() { return startGet(CONTINUOUS_CONVERSION, port); }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -