Contiki-NG
Loading...
Searching...
No Matches
light-ziglet.c
Go to the documentation of this file.
1/*
2 * Copyright (c) 2010, Swedish Institute of Computer Science.
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 * 3. Neither the name of the Institute nor the names of its contributors
14 * may be used to endorse or promote products derived from this software
15 * without specific prior written permission.
16 *
17 * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 * ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27 * SUCH DAMAGE.
28 *
29 * This file is part of the Contiki operating system.
30 *
31 */
32
33/**
34 * \file
35 * Device drivers for light ziglet sensor in Zolertia Z1.
36 * It is recommended to use with a 100KHz data rate
37 * \author
38 * Antonio Lignan, Zolertia <alinan@zolertia.com>
39 * Marcus Lundén, SICS <mlunden@sics.se>
40 */
41
42#include <stdio.h>
43#include "contiki.h"
44#include "i2cmaster.h"
45#include "light-ziglet.h"
46
47#if 0
48#define PRINTFDEBUG(...) printf(__VA_ARGS__)
49#else
50#define PRINTFDEBUG(...)
51#endif
52
53/* Bitmasks and bit flag variable for keeping track of tmp102 status. */
54enum TSL2563_STATUSTYPES {
55 /* must be a bit and not more, not using 0x00. */
56 INITED = 0x01,
57 RUNNING = 0x02,
58 STOPPED = 0x04,
59};
60
61static enum TSL2563_STATUSTYPES _TSL2563_STATUS = 0x00;
62
63uint16_t
64calculateLux(uint16_t *buffer)
65{
66 uint32_t ch0, ch1 = 0;
67 uint32_t aux = (1 << 14);
68 uint32_t ratio, lratio, tmp = 0;
69
70 ch0 = (buffer[0] * aux) >> 10;
71 ch1 = (buffer[1] * aux) >> 10;
72
73 PRINTFDEBUG("B0 %u, B1 %u\n", buffer[0], buffer[1]);
74 PRINTFDEBUG("ch0 %lu, ch1 %lu\n", ch0, ch1);
75
76 ratio = (ch1 << 10);
77 ratio = ratio / ch0;
78 lratio = (ratio + 1) >> 1;
79
80 PRINTFDEBUG("ratio %lu, lratio %lu\n", ratio, lratio);
81
82 if((lratio >= 0) && (lratio <= K1T)) {
83 tmp = (ch0 * B1T) - (ch1 * M1T);
84 } else if(lratio <= K2T) {
85 tmp = (ch0 * B2T) - (ch1 * M2T);
86 } else if(lratio <= K3T) {
87 tmp = (ch0 * B3T) - (ch1 * M3T);
88 } else if(lratio <= K4T) {
89 tmp = (ch0 * B4T) - (ch1 * M4T);
90 } else if(lratio <= K5T) {
91 tmp = (ch0 * B5T) - (ch1 * M5T);
92 } else if(lratio <= K6T) {
93 tmp = (ch0 * B6T) - (ch1 * M6T);
94 } else if(lratio <= K7T) {
95 tmp = (ch0 * B7T) - (ch1 * M7T);
96 } else if(lratio > K8T) {
97 tmp = (ch0 * B8T) - (ch1 * M8T);
98 }
99
100 if(tmp < 0) {
101 tmp = 0;
102 }
103
104 tmp += (1 << 13);
105
106 PRINTFDEBUG("tmp %lu\n", tmp);
107
108 return tmp >> 14;
109}
110/*---------------------------------------------------------------------------*/
111/* Init the light ziglet sensor: ports, pins, registers, interrupts (none enabled), I2C,
112 default threshold values etc. */
113
114void
115light_ziglet_init(void)
116{
117 if(!(_TSL2563_STATUS & INITED)) {
118 PRINTFDEBUG("light ziglet init\n");
119 _TSL2563_STATUS |= INITED;
120
121 /* Set up ports and pins for I2C communication */
122 i2c_enable();
123 return;
124 }
125}
126/*---------------------------------------------------------------------------*/
127/* Write to a 16-bit register.
128 args:
129 reg register to write to
130 val value to write
131 */
132
133void
134tsl2563_write_reg(uint8_t reg, uint16_t val)
135{
136 uint8_t tx_buf[] = { reg, 0x00, 0x00 };
137
138 tx_buf[1] = (uint8_t)(val >> 8);
139 tx_buf[2] = (uint8_t)(val & 0x00FF);
140
141 i2c_transmitinit(TSL2563_ADDR);
142 while(i2c_busy());
143 PRINTFDEBUG("I2C Ready to TX\n");
144
145 i2c_transmit_n(3, tx_buf);
146 while(i2c_busy());
147 PRINTFDEBUG("WRITE_REG 0x%04X @ reg 0x%02X\n", val, reg);
148}
149/*---------------------------------------------------------------------------*/
150/* Read register.
151 args:
152 reg what register to read
153 returns the value of the read register type uint16_t
154 */
155
156uint16_t
157tsl2563_read_reg(uint8_t reg)
158{
159 uint16_t readBuf[] = { 0x00, 0x00 };
160 uint8_t buf[] = { 0x00, 0x00, 0x00, 0x00 };
161 uint16_t retVal = 0;
162 uint8_t rtx = reg;
163
164 /* Transmit the register to read */
165 i2c_transmitinit(TSL2563_ADDR);
166 while(i2c_busy());
167 i2c_transmit_n(1, &rtx);
168 while(i2c_busy());
169
170 /* Receive the data */
171 i2c_receiveinit(TSL2563_ADDR);
172 while(i2c_busy());
173 i2c_receive_n(4, buf);
174 while(i2c_busy());
175
176 PRINTFDEBUG("\nb0 %u, b1 %u, b2 %u, b3 %u\n", buf[0], buf[1], buf[2], buf[3]);
177
178 readBuf[0] = (buf[1] << 8 | (buf[0]));
179 readBuf[1] = (buf[3] << 8 | (buf[2]));
180
181 retVal = calculateLux(readBuf);
182 return retVal;
183}
184uint16_t
185light_ziglet_on(void)
186{
187 uint16_t data;
188 uint8_t regon = TSL2563_PWRN;
189 /* Turn on the sensor */
190 i2c_transmitinit(TSL2563_ADDR);
191 while(i2c_busy());
192 i2c_transmit_n(1, &regon);
193 while(i2c_busy());
194 data = (uint16_t)tsl2563_read_reg(TSL2563_READ);
195 return data;
196}
197void
198light_ziglet_off(void)
199{
200 uint8_t regoff = 0x00;
201 /* Turn off the sensor */
202 i2c_transmitinit(TSL2563_ADDR);
203 while(i2c_busy());
204 i2c_transmit_n(1, &regoff);
205 while(i2c_busy());
206 return;
207}
208/*---------------------------------------------------------------------------*/
209/* Read light ziglet sensor
210 */
211
212uint16_t
213light_ziglet_read(void)
214{
215 uint16_t lux = 0;
216 lux = light_ziglet_on();
217 light_ziglet_off();
218 return lux;
219}
I2C communication device driver header file for Zolertia Z1 sensor node.
Device drivers header file for light ziglet sensor in Zolertia Z1 WSN Platform.