2011-01-20 10:28:00 +00:00
|
|
|
/*
|
|
|
|
* Copyright (c) 2010, Swedish Institute of Computer Science.
|
2016-02-22 09:34:48 +00:00
|
|
|
* Copyright (c) 2016, Zolertia <http://www.zolertia.com>
|
2011-01-20 10:28:00 +00:00
|
|
|
* All rights reserved.
|
|
|
|
*
|
|
|
|
* Redistribution and use in source and binary forms, with or without
|
|
|
|
* modification, are permitted provided that the following conditions
|
|
|
|
* are met:
|
|
|
|
* 1. Redistributions of source code must retain the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer.
|
|
|
|
* 2. 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.
|
|
|
|
* 3. Neither the name of the Institute 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 INSTITUTE 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 INSTITUTE 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.
|
|
|
|
*
|
|
|
|
* This file is part of the Contiki operating system.
|
|
|
|
*
|
|
|
|
*/
|
2016-02-22 09:34:48 +00:00
|
|
|
/*---------------------------------------------------------------------------*/
|
2011-01-20 10:28:00 +00:00
|
|
|
/**
|
|
|
|
* \file
|
|
|
|
* Device drivers for tmp102 temperature sensor in Zolertia Z1.
|
|
|
|
* \author
|
|
|
|
* Enric M. Calvo, Zolertia <ecalvo@zolertia.com>
|
|
|
|
* Marcus Lundén, SICS <mlunden@sics.se>
|
2016-02-22 09:34:48 +00:00
|
|
|
* Antonio Lignan, Zolertia <alinan@zolertia.com>
|
2011-01-20 10:28:00 +00:00
|
|
|
*/
|
2016-02-22 09:34:48 +00:00
|
|
|
/*---------------------------------------------------------------------------*/
|
2011-01-20 10:28:00 +00:00
|
|
|
#include <stdio.h>
|
|
|
|
#include "contiki.h"
|
|
|
|
#include "i2cmaster.h"
|
|
|
|
#include "tmp102.h"
|
2016-02-22 09:34:48 +00:00
|
|
|
#include "lib/sensors.h"
|
2011-01-20 10:28:00 +00:00
|
|
|
/*---------------------------------------------------------------------------*/
|
2016-02-22 09:34:48 +00:00
|
|
|
#define DEBUG 0
|
|
|
|
#if DEBUG
|
|
|
|
#define PRINTF(...) printf(__VA_ARGS__)
|
|
|
|
#else
|
|
|
|
#define PRINTF(...)
|
|
|
|
#endif
|
|
|
|
/*---------------------------------------------------------------------------*/
|
|
|
|
static uint8_t enabled;
|
2011-01-20 10:28:00 +00:00
|
|
|
/*---------------------------------------------------------------------------*/
|
|
|
|
void
|
2014-10-16 15:59:52 +00:00
|
|
|
tmp102_init(void)
|
2011-01-20 10:28:00 +00:00
|
|
|
{
|
2016-02-22 09:34:48 +00:00
|
|
|
/* Power Up TMP102 via pin */
|
|
|
|
TMP102_PWR_DIR |= TMP102_PWR_PIN;
|
|
|
|
TMP102_PWR_SEL &= ~TMP102_PWR_SEL;
|
|
|
|
TMP102_PWR_SEL2 &= ~TMP102_PWR_SEL;
|
|
|
|
TMP102_PWR_REN &= ~TMP102_PWR_SEL;
|
|
|
|
TMP102_PWR_OUT |= TMP102_PWR_PIN;
|
2014-10-16 15:59:52 +00:00
|
|
|
|
2016-02-22 09:34:48 +00:00
|
|
|
/* Set up ports and pins for I2C communication */
|
|
|
|
i2c_enable();
|
|
|
|
|
|
|
|
enabled = 1;
|
|
|
|
}
|
|
|
|
/*---------------------------------------------------------------------------*/
|
|
|
|
void
|
|
|
|
tmp102_stop(void)
|
|
|
|
{
|
|
|
|
/* Power off */
|
|
|
|
TMP102_PWR_OUT &= ~TMP102_PWR_PIN;
|
|
|
|
enabled = 0;
|
2011-01-20 10:28:00 +00:00
|
|
|
}
|
|
|
|
/*---------------------------------------------------------------------------*/
|
|
|
|
void
|
2014-10-16 15:59:52 +00:00
|
|
|
tmp102_write_reg(uint8_t reg, uint16_t val)
|
2011-01-20 10:28:00 +00:00
|
|
|
{
|
2012-02-20 19:32:05 +00:00
|
|
|
uint8_t tx_buf[] = { reg, 0x00, 0x00 };
|
2011-01-20 10:28:00 +00:00
|
|
|
|
2014-10-16 15:59:52 +00:00
|
|
|
tx_buf[1] = (uint8_t)(val >> 8);
|
|
|
|
tx_buf[2] = (uint8_t)(val & 0x00FF);
|
2011-01-20 10:28:00 +00:00
|
|
|
|
2014-10-16 15:59:52 +00:00
|
|
|
i2c_transmitinit(TMP102_ADDR);
|
|
|
|
while(i2c_busy());
|
2016-02-22 09:34:48 +00:00
|
|
|
PRINTF("I2C Ready to TX\n");
|
2011-01-20 10:28:00 +00:00
|
|
|
|
2014-10-16 15:59:52 +00:00
|
|
|
i2c_transmit_n(3, tx_buf);
|
|
|
|
while(i2c_busy());
|
2016-02-22 09:34:48 +00:00
|
|
|
PRINTF("WRITE_REG 0x%04X @ reg 0x%02X\n", val, reg);
|
2011-01-20 10:28:00 +00:00
|
|
|
}
|
|
|
|
/*---------------------------------------------------------------------------*/
|
2012-02-20 19:32:05 +00:00
|
|
|
uint16_t
|
2014-10-16 15:59:52 +00:00
|
|
|
tmp102_read_reg(uint8_t reg)
|
2011-01-20 10:28:00 +00:00
|
|
|
{
|
2012-02-20 19:32:05 +00:00
|
|
|
uint8_t buf[] = { 0x00, 0x00 };
|
|
|
|
uint16_t retVal = 0;
|
|
|
|
uint8_t rtx = reg;
|
2016-02-22 09:34:48 +00:00
|
|
|
PRINTF("READ_REG 0x%02X\n", reg);
|
2011-01-20 10:28:00 +00:00
|
|
|
|
2014-10-16 15:59:52 +00:00
|
|
|
/* transmit the register to read */
|
|
|
|
i2c_transmitinit(TMP102_ADDR);
|
|
|
|
while(i2c_busy());
|
|
|
|
i2c_transmit_n(1, &rtx);
|
|
|
|
while(i2c_busy());
|
2011-01-20 10:28:00 +00:00
|
|
|
|
2014-10-16 15:59:52 +00:00
|
|
|
/* receive the data */
|
|
|
|
i2c_receiveinit(TMP102_ADDR);
|
|
|
|
while(i2c_busy());
|
|
|
|
i2c_receive_n(2, &buf[0]);
|
|
|
|
while(i2c_busy());
|
2011-01-20 10:28:00 +00:00
|
|
|
|
2014-10-16 15:59:52 +00:00
|
|
|
retVal = (uint16_t)(buf[0] << 8 | (buf[1]));
|
2011-01-20 10:28:00 +00:00
|
|
|
|
|
|
|
return retVal;
|
|
|
|
}
|
|
|
|
/*---------------------------------------------------------------------------*/
|
2012-02-20 19:32:05 +00:00
|
|
|
uint16_t
|
2014-10-16 15:59:52 +00:00
|
|
|
tmp102_read_temp_raw(void)
|
2011-01-20 10:28:00 +00:00
|
|
|
{
|
2012-02-20 19:32:05 +00:00
|
|
|
uint16_t rd = 0;
|
2014-10-16 15:59:52 +00:00
|
|
|
rd = tmp102_read_reg(TMP102_TEMP);
|
2011-01-20 10:28:00 +00:00
|
|
|
return rd;
|
|
|
|
}
|
2016-02-22 09:34:48 +00:00
|
|
|
/*---------------------------------------------------------------------------*/
|
2012-01-03 18:45:03 +00:00
|
|
|
int16_t
|
|
|
|
tmp102_read_temp_x100(void)
|
2011-01-20 10:28:00 +00:00
|
|
|
{
|
|
|
|
int16_t raw = 0;
|
|
|
|
int16_t sign = 1;
|
|
|
|
int16_t abstemp, temp_int;
|
|
|
|
|
2014-10-16 15:59:52 +00:00
|
|
|
raw = (int16_t)tmp102_read_reg(TMP102_TEMP);
|
2011-06-30 16:16:35 +00:00
|
|
|
if(raw < 0) {
|
|
|
|
abstemp = (raw ^ 0xFFFF) + 1;
|
|
|
|
sign = -1;
|
|
|
|
} else {
|
|
|
|
abstemp = raw;
|
|
|
|
}
|
2011-01-20 10:28:00 +00:00
|
|
|
|
2012-01-03 18:45:03 +00:00
|
|
|
/* Integer part of the temperature value and percents*/
|
|
|
|
temp_int = (abstemp >> 8) * sign * 100;
|
|
|
|
temp_int += ((abstemp & 0xff) * 100) / 0x100;
|
|
|
|
return temp_int;
|
|
|
|
}
|
|
|
|
/*---------------------------------------------------------------------------*/
|
|
|
|
int8_t
|
2014-10-16 15:59:52 +00:00
|
|
|
tmp102_read_temp_simple(void)
|
2012-01-03 18:45:03 +00:00
|
|
|
{
|
2015-05-22 14:40:12 +00:00
|
|
|
/* Casted to int8_t: We don't expect temperatures outside -128 to 127 C */
|
|
|
|
return tmp102_read_temp_x100() / 100;
|
2011-01-20 10:28:00 +00:00
|
|
|
}
|
2016-02-22 09:34:48 +00:00
|
|
|
/*---------------------------------------------------------------------------*/
|
|
|
|
static int
|
|
|
|
configure(int type, int value)
|
|
|
|
{
|
|
|
|
if(type != SENSORS_ACTIVE) {
|
|
|
|
return TMP102_ERROR;
|
|
|
|
}
|
|
|
|
if(value) {
|
|
|
|
tmp102_init();
|
|
|
|
} else {
|
|
|
|
tmp102_stop();
|
|
|
|
}
|
|
|
|
enabled = value;
|
|
|
|
return TMP102_SUCCESS;
|
|
|
|
}
|
|
|
|
/*---------------------------------------------------------------------------*/
|
|
|
|
static int
|
|
|
|
status(int type)
|
|
|
|
{
|
|
|
|
switch(type) {
|
|
|
|
case SENSORS_ACTIVE:
|
|
|
|
case SENSORS_READY:
|
|
|
|
return enabled;
|
|
|
|
}
|
|
|
|
return TMP102_SUCCESS;
|
|
|
|
}
|
|
|
|
/*---------------------------------------------------------------------------*/
|
|
|
|
static int
|
|
|
|
value(int type)
|
|
|
|
{
|
|
|
|
return (int)tmp102_read_temp_x100();
|
|
|
|
}
|
|
|
|
/*---------------------------------------------------------------------------*/
|
|
|
|
SENSORS_SENSOR(tmp102, TMP102_SENSOR, value, configure, status);
|
|
|
|
/*---------------------------------------------------------------------------*/
|