Contiki-NG
tmp102.c
Go to the documentation of this file.
1/*
2 * Copyright (c) 2015, Zolertia - http://www.zolertia.com
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 */
30/*---------------------------------------------------------------------------*/
31/**
32 * \addtogroup zoul-tmp102-sensor
33 * @{
34 *
35 * \file
36 * Driver for the TMP102 temperature sensor
37 */
38/*---------------------------------------------------------------------------*/
39#include <stdio.h>
40#include "contiki.h"
41#include "dev/i2c.h"
42#include "tmp102.h"
43
44void
46{
47 i2c_init(I2C_SDA_PORT, I2C_SDA_PIN, I2C_SCL_PORT, I2C_SCL_PIN, I2C_SCL_NORMAL_BUS_SPEED);
48}
49/*---------------------------------------------------------------------------*/
50
51uint8_t
52tmp102_read(uint16_t *data)
53{
54 uint8_t buf[2];
55 uint16_t temp;
56
57 /* Write to the temperature register to trigger a reading */
58 if(i2c_single_send(TMP102_ADDR, TMP102_TEMP) == I2C_MASTER_ERR_NONE) {
59 /* Read two bytes only */
60 if(i2c_burst_receive(TMP102_ADDR, buf, 2) == I2C_MASTER_ERR_NONE) {
61 /* 12-bit value, TMP102 SBOS397F Table 8-9 */
62 temp = (buf[0] << 4) + (buf[1] >> 4);
63 if(temp > 2047) {
64 temp -= (1 << 12);
65 }
66 temp *= 0.625;
67 *data = temp;
68 return I2C_MASTER_ERR_NONE;
69 }
70 }
71 return i2c_master_error();
72}
73/*---------------------------------------------------------------------------*/
74/** @} */
void i2c_init(uint8_t port_sda, uint8_t pin_sda, uint8_t port_scl, uint8_t pin_scl, uint32_t bus_speed)
Initialize the I2C peripheral and pins.
Definition: i2c.c:49
uint8_t i2c_master_error(void)
Return the status register if error occurred during last communication.
Definition: i2c.c:147
uint8_t i2c_burst_receive(uint8_t slave_addr, uint8_t *data, uint8_t len)
Perform all operations to receive multiple bytes from a slave.
Definition: i2c.c:218
uint8_t i2c_single_send(uint8_t slave_addr, uint8_t data)
Perform all operations to send a byte to a slave.
Definition: i2c.c:159
#define TMP102_ADDR
TMP102 slave address.
Definition: tmp102.h:59
uint8_t tmp102_read(uint16_t *data)
Get a temperature reading from the TMP102 sensor.
Definition: tmp102.c:52
#define TMP102_TEMP
TMP102 temperature data register.
Definition: tmp102.h:60
void tmp102_init(void)
Initialiser for the TMP102 sensor driver.
Definition: tmp102.c:59
Header file for the TMP102 Sensor Driver.