-/* $Id$
- * Copyright (c) 2007 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.
- */
-/**
- * MTS300 photo and temp sensor ADC configuration.
- * @author David Gay <david.e.gay@intel.com>
- */
-module PhotoTempP
-{
- provides interface Atm128AdcConfig;
- uses interface MicaBusAdc as PhotoTempAdc;
-}
-implementation
-{
- async command uint8_t Atm128AdcConfig.getChannel() {
- return call PhotoTempAdc.getChannel();
- }
-
- async command uint8_t Atm128AdcConfig.getRefVoltage() {
- return ATM128_ADC_VREF_OFF;
- }
-
- async command uint8_t Atm128AdcConfig.getPrescaler() {
- return ATM128_ADC_PRESCALE;
- }
-}
+/**\r
+ * Copyright (c) 2005-2006 Crossbow Technology, Inc.\r
+ * All rights reserved.\r
+ *\r
+ * Permission to use, copy, modify, and distribute this software and its\r
+ * documentation for any purpose, without fee, and without written\r
+ * agreement is hereby granted, provided that the above copyright\r
+ * notice, the (updated) modification history and the author appear in\r
+ * all copies of this source code.\r
+ *\r
+ * Permission is also granted to distribute this software under the\r
+ * standard BSD license as contained in the TinyOS distribution.\r
+ *\r
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS `AS IS'\r
+ * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE\r
+ * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE\r
+ * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS OR CONTRIBUTORS\r
+ * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR\r
+ * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, LOSS OF USE, DATA,\r
+ * OR PROFITS) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN\r
+ * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)\r
+ * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF\r
+ * THE POSSIBILITY OF SUCH DAMAGE.\r
+ *\r
+ * @author Martin Turon <mturon@xbow.com>\r
+ * @author Hu Siquan <husq@xbow.com>\r
+ *\r
+ * $Id$\r
+ */\r
+\r
+#include "Timer.h"\r
+\r
+module PhotoTempP\r
+{\r
+ provides\r
+ {\r
+ interface Init; //!< Standard Initialization\r
+ interface StdControl; //!< Start/Stop for Temp Sensor's Power Management\r
+ interface ResourceConfigure as TempResourceConfigure;\r
+ interface ResourceConfigure as PhotoResourceConfigure;\r
+ interface Atm128AdcConfig as TempAtm128AdcConfig;\r
+ interface Atm128AdcConfig as PhotoAtm128AdcConfig;\r
+ }\r
+ uses\r
+ {\r
+ interface GeneralIO as TempPower;\r
+ interface GeneralIO as LightPower;\r
+ interface MicaBusAdc as SensorAdc;\r
+ interface Leds as DebugLeds;\r
+ }\r
+}\r
+implementation\r
+{\r
+/*\r
+ enum\r
+ {\r
+ STATE_IDLE = 0,\r
+ STATE_LIGHT_WARMING, //!< Powering on sensor\r
+ STATE_LIGHT_READY, //!< Power up of sensor complete\r
+ STATE_LIGHT_SAMPLING, //!< Sampling sensor\r
+ STATE_TEMP_WARMING, //!< Powering on sensor\r
+ STATE_TEMP_READY, //!< Power up of sensor complete\r
+ STATE_TEMP_SAMPLING, //!< Sampling sensor\r
+ };\r
+\r
+ /// Yes, this could be a simple uint8_t. There used to be more bits here,\r
+ /// but they were optimized out and removed.\r
+ union\r
+ {\r
+ uint8_t flat;\r
+ struct\r
+ {\r
+ uint8_t state : 4; //!< sensorboard state\r
+ } bits;\r
+ } g_flags;\r
+*/\r
+\r
+ void inline TOSH_uwait(int u_sec)\r
+ {\r
+ /* In most cases (constant arg), the test is elided at compile-time */\r
+ if (u_sec)\r
+ /* loop takes 4 cycles, aka 1us */\r
+ asm volatile (\r
+ "1: sbiw %0,1\n"\r
+ " brne 1b" : "+w" (u_sec));\r
+ }\r
+ /**\r
+ * Initialize this component. Initialization should not assume that\r
+ * any component is running: init() cannot call any commands besides\r
+ * those that initialize other components.\r
+ *\r
+ */\r
+ command error_t Init.init()\r
+ {\r
+ return SUCCESS;\r
+ }\r
+\r
+\r
+ /**\r
+ * Start the component and its subcomponents.\r
+ *\r
+ * @return SUCCESS if the component was successfully started.\r
+ */\r
+ command error_t StdControl.start()\r
+ {\r
+ call TempPower.makeOutput();\r
+ call TempPower.clr();\r
+ call LightPower.makeOutput();\r
+ call LightPower.clr();\r
+ \r
+ return SUCCESS;\r
+ }\r
+\r
+ /**\r
+ * Stop the component and pertinent subcomponents (not all\r
+ * subcomponents may be turned off due to wakeup timers, etc.).\r
+ *\r
+ * @return SUCCESS if the component was successfully stopped.\r
+ */\r
+ command error_t StdControl.stop()\r
+ {\r
+ call TempPower.clr();\r
+ call LightPower.clr();\r
+ call TempPower.makeInput();\r
+ call LightPower.makeInput();\r
+ \r
+ return SUCCESS;\r
+ }\r
+\r
+ async command uint8_t TempAtm128AdcConfig.getChannel() \r
+ {\r
+ return call SensorAdc.getChannel();\r
+ }\r
+ \r
+ async command uint8_t TempAtm128AdcConfig.getRefVoltage() \r
+ {\r
+ return ATM128_ADC_VREF_OFF;\r
+ }\r
+ \r
+ async command uint8_t TempAtm128AdcConfig.getPrescaler() \r
+ {\r
+ return ATM128_ADC_PRESCALE;\r
+ }\r
+ \r
+ async command uint8_t PhotoAtm128AdcConfig.getChannel() \r
+ {\r
+ return call SensorAdc.getChannel();\r
+ }\r
+ \r
+ async command uint8_t PhotoAtm128AdcConfig.getRefVoltage() \r
+ {\r
+ return ATM128_ADC_VREF_OFF;\r
+ }\r
+ \r
+ async command uint8_t PhotoAtm128AdcConfig.getPrescaler() \r
+ {\r
+ return ATM128_ADC_PRESCALE;\r
+ }\r
+ \r
+ async command void TempResourceConfigure.configure() \r
+ {\r
+ call DebugLeds.led0On();\r
+ call LightPower.clr();\r
+ call LightPower.makeInput();\r
+ call TempPower.makeOutput();\r
+ call TempPower.set();\r
+ TOSH_uwait(1000);\r
+ }\r
+ \r
+ async command void TempResourceConfigure.unconfigure() \r
+ {\r
+ call DebugLeds.led0Off();\r
+ call TempPower.clr();\r
+ call TempPower.makeInput();\r
+ }\r
+ \r
+ /** Turns on the light sensor and turns the thermistor off. */\r
+ async command void PhotoResourceConfigure.configure() \r
+ {\r
+ call DebugLeds.led1On();\r
+ call TempPower.clr();\r
+ call TempPower.makeInput();\r
+ call LightPower.makeOutput();\r
+ call LightPower.set();\r
+ TOSH_uwait(1000);\r
+ }\r
+ \r
+ async command void PhotoResourceConfigure.unconfigure() \r
+ {\r
+ call DebugLeds.led1Off();\r
+ call LightPower.clr();\r
+ call LightPower.makeInput();\r
+ }\r
+}\r