Contiki 2.6
|
00001 /* 00002 * Copyright (c) 2010, Swedish Institute of Computer Science. 00003 * All rights reserved. 00004 * 00005 * Redistribution and use in source and binary forms, with or without 00006 * modification, are permitted provided that the following conditions 00007 * are met: 00008 * 1. Redistributions of source code must retain the above copyright 00009 * notice, this list of conditions and the following disclaimer. 00010 * 2. Redistributions in binary form must reproduce the above copyright 00011 * notice, this list of conditions and the following disclaimer in the 00012 * documentation and/or other materials provided with the distribution. 00013 * 3. Neither the name of the Institute nor the names of its contributors 00014 * may be used to endorse or promote products derived from this software 00015 * without specific prior written permission. 00016 * 00017 * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND 00018 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 00019 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 00020 * ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE 00021 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 00022 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 00023 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 00024 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 00025 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 00026 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 00027 * SUCH DAMAGE. 00028 * 00029 * This file is part of the Contiki operating system. 00030 * 00031 */ 00032 00033 /** 00034 * \file 00035 * Device drivers for light ziglet sensor in Zolertia Z1. 00036 * \author 00037 * Antonio Lignan, Zolertia <alinan@zolertia.com> 00038 * Marcus Lundén, SICS <mlunden@sics.se> 00039 */ 00040 00041 00042 #include <stdio.h> 00043 #include "contiki.h" 00044 #include "i2cmaster.h" 00045 #include "light-ziglet.h" 00046 00047 #if 0 00048 #define PRINTFDEBUG(...) printf(__VA_ARGS__) 00049 #else 00050 #define PRINTFDEBUG(...) 00051 #endif 00052 00053 /* Bitmasks and bit flag variable for keeping track of tmp102 status. */ 00054 enum TSL2563_STATUSTYPES 00055 { 00056 /* must be a bit and not more, not using 0x00. */ 00057 INITED = 0x01, 00058 RUNNING = 0x02, 00059 STOPPED = 0x04, 00060 }; 00061 00062 static enum TSL2563_STATUSTYPES _TSL2563_STATUS = 0x00; 00063 00064 uint16_t 00065 calculateLux(uint16_t *buffer) 00066 { 00067 uint32_t ch0, ch1 = 0; 00068 uint32_t aux = (1<<14); 00069 uint32_t ratio, lratio, tmp = 0; 00070 00071 ch0 = (buffer[0]*aux) >> 10; 00072 ch1 = (buffer[1]*aux) >> 10; 00073 00074 PRINTFDEBUG("B0 %u, B1 %u\n", buffer[0], buffer[1]); 00075 PRINTFDEBUG("ch0 %lu, ch1 %lu\n", ch0, ch1); 00076 00077 ratio = (ch1 << 10); 00078 ratio = ratio/ch0; 00079 lratio = (ratio+1) >> 1; 00080 00081 PRINTFDEBUG("ratio %lu, lratio %lu\n", ratio, lratio); 00082 00083 if ((lratio >= 0) && (lratio <= K1T)) 00084 tmp = (ch0*B1T) - (ch1*M1T); 00085 else if (lratio <= K2T) 00086 tmp = (ch0*B2T) - (ch1*M2T); 00087 else if (lratio <= K3T) 00088 tmp = (ch0*B3T) - (ch1*M3T); 00089 else if (lratio <= K4T) 00090 tmp = (ch0*B4T) - (ch1*M4T); 00091 else if (lratio <= K5T) 00092 tmp = (ch0*B5T) - (ch1*M5T); 00093 else if (lratio <= K6T) 00094 tmp = (ch0*B6T) - (ch1*M6T); 00095 else if (lratio <= K7T) 00096 tmp = (ch0*B7T) - (ch1*M7T); 00097 else if (lratio > K8T) 00098 tmp = (ch0*B8T) - (ch1*M8T); 00099 00100 if (tmp < 0) tmp = 0; 00101 00102 tmp += (1<<13); 00103 00104 PRINTFDEBUG("tmp %lu\n", tmp); 00105 00106 return (tmp >> 14); 00107 } 00108 00109 /*---------------------------------------------------------------------------*/ 00110 /* Init the light ziglet sensor: ports, pins, registers, interrupts (none enabled), I2C, 00111 default threshold values etc. */ 00112 00113 void 00114 light_ziglet_init (void) 00115 { 00116 if (!(_TSL2563_STATUS & INITED)) 00117 { 00118 PRINTFDEBUG ("light ziglet init\n"); 00119 _TSL2563_STATUS |= INITED; 00120 00121 /* Set up ports and pins for I2C communication */ 00122 i2c_enable (); 00123 return; 00124 } 00125 } 00126 00127 /*---------------------------------------------------------------------------*/ 00128 /* Write to a 16-bit register. 00129 args: 00130 reg register to write to 00131 val value to write 00132 */ 00133 00134 void 00135 tsl2563_write_reg (uint8_t reg, uint16_t val) 00136 { 00137 uint8_t tx_buf[] = { reg, 0x00, 0x00 }; 00138 00139 tx_buf[1] = (uint8_t) (val >> 8); 00140 tx_buf[2] = (uint8_t) (val & 0x00FF); 00141 00142 i2c_transmitinit (TSL2563_ADDR); 00143 while (i2c_busy ()); 00144 PRINTFDEBUG ("I2C Ready to TX\n"); 00145 00146 i2c_transmit_n (3, tx_buf); 00147 while (i2c_busy ()); 00148 PRINTFDEBUG ("WRITE_REG 0x%04X @ reg 0x%02X\n", val, reg); 00149 } 00150 00151 /*---------------------------------------------------------------------------*/ 00152 /* Read register. 00153 args: 00154 reg what register to read 00155 returns the value of the read register type uint16_t 00156 */ 00157 00158 uint16_t 00159 tsl2563_read_reg (uint8_t reg) 00160 { 00161 uint16_t readBuf[] = {0x00, 0x00}; 00162 uint8_t buf[] = { 0x00, 0x00, 0x00, 0x00}; 00163 uint16_t retVal = 0; 00164 uint8_t rtx = reg; 00165 00166 // Transmit the register to read 00167 i2c_transmitinit (TSL2563_ADDR); 00168 while (i2c_busy ()); 00169 i2c_transmit_n (1, &rtx); 00170 while (i2c_busy ()); 00171 00172 // Receive the data 00173 i2c_receiveinit (TSL2563_ADDR); 00174 while (i2c_busy ()); 00175 i2c_receive_n (4, &buf[0]); 00176 while (i2c_busy ()); 00177 00178 PRINTFDEBUG("\nb0 %u, b1 %u, b2 %u, b3 %u\n", buf[0], buf[1], buf[2], buf[3]); 00179 00180 readBuf[0] = (buf[1] << 8 | (buf[0])); 00181 readBuf[1] = (buf[3] << 8 | (buf[2])); 00182 00183 /* XXX Quick hack, was receiving dups bytes */ 00184 00185 if(readBuf[0] == readBuf[1]){ 00186 tsl2563_read_reg(TSL2563_READ); 00187 return; 00188 } else { 00189 retVal = calculateLux(&readBuf); 00190 return retVal; 00191 } 00192 } 00193 00194 uint16_t 00195 light_ziglet_on(void) 00196 { 00197 uint16_t data; 00198 uint8_t regon[] = { 0x00, TSL2563_PWRN }; 00199 // Turn on the sensor 00200 i2c_transmitinit (TSL2563_ADDR); 00201 while (i2c_busy ()); 00202 i2c_transmit_n (2, ®on); 00203 while (i2c_busy ()); 00204 data = (uint16_t) tsl2563_read_reg (TSL2563_READ); 00205 return data; 00206 } 00207 00208 void 00209 light_ziglet_off(void) 00210 { 00211 uint8_t regoff = 0x00; 00212 // Turn off the sensor 00213 i2c_transmitinit (TSL2563_ADDR); 00214 while (i2c_busy ()); 00215 i2c_transmit_n (1, ®off); 00216 while (i2c_busy ()); 00217 return; 00218 } 00219 00220 00221 /*---------------------------------------------------------------------------*/ 00222 /* Read light ziglet sensor 00223 */ 00224 00225 uint16_t 00226 light_ziglet_read(void) 00227 { 00228 uint16_t lux = 0; 00229 lux = light_ziglet_on(); 00230 light_ziglet_off(); 00231 return lux; 00232 } 00233