1
0
mirror of https://github.com/RIOT-OS/RIOT.git synced 2024-12-29 04:50:03 +01:00

drivers/si70xx: cleanup and use pseudomodules

This commit is contained in:
Alexandre Abadie 2017-07-03 00:00:47 +02:00
parent e83248154b
commit 6f43e98b95
12 changed files with 267 additions and 237 deletions

View File

@ -212,9 +212,10 @@ ifneq (,$(filter sht11,$(USEMODULE)))
USEMODULE += xtimer
endif
ifneq (,$(filter si70xx,$(USEMODULE)))
ifneq (,$(filter si70%,$(USEMODULE)))
USEMODULE += xtimer
FEATURES_REQUIRED += periph_i2c
USEMODULE += si70xx
endif
ifneq (,$(filter srf02,$(USEMODULE)))

View File

@ -103,7 +103,7 @@ endif
ifneq (,$(filter xbee,$(USEMODULE)))
USEMODULE_INCLUDES += $(RIOTBASE)/drivers/xbee/include
endif
ifneq (,$(filter si70xx,$(USEMODULE)))
ifneq (,$(filter si70%,$(USEMODULE)))
USEMODULE_INCLUDES += $(RIOTBASE)/drivers/si70xx/include
endif
ifneq (,$(filter hdc1000,$(USEMODULE)))

View File

@ -28,61 +28,17 @@ extern "C" {
#endif
/**
* @name Si70xx chip addresses.
* @{
* @brief Driver return codes
*/
#define SI70XX_ADDRESS_SI7006 (0x80)
#define SI70XX_ADDRESS_SI7013 (0x80)
#define SI70XX_ADDRESS_SI7013_ALT (0x81)
#define SI70XX_ADDRESS_SI7020 (0x80)
#define SI70XX_ADDRESS_SI7021 (0x80)
/** @} */
enum {
SI70XX_OK, /**< All OK */
SI70XX_ERR_NOI2C, /**< An error occurred when initializing I2C bus */
SI70XX_ERR_NODEV, /**< No valid device found on I2C bus */
SI70XX_ERR_I2C, /**< An error occured when reading/writing on I2C bus */
};
/**
* @name Si70xx device commands.
* @{
*/
#define SI70XX_MEASURE_RH_HOLD (0xE5)
#define SI70XX_MEASURE_RH (0xF5)
#define SI70XX_MEASURE_TEMP_HOLD (0xE3)
#define SI70XX_MEASURE_TEMP (0xF3)
#define SI70XX_MEASURE_TEMP_PREV (0xE0)
#define SI70XX_RESET (0xFE)
#define SI70XX_WRITE_USER_REG (0xE6)
#define SI70XX_READ_USER_REG (0xE7)
#define SI70XX_WRITE_HEATER_REG (0x51)
#define SI70XX_READ_HEATER_REG (0x11)
#define SI70XX_READ_ID_FIRST_A (0xFA)
#define SI70XX_READ_ID_FIRST_B (0x0F)
#define SI70XX_READ_ID_SECOND_A (0xFC)
#define SI70XX_READ_ID_SECOND_B (0xC9)
#define SI70XX_READ_REVISION_A (0x84)
#define SI70XX_READ_REVISION_B (0xB8)
/** @} */
/**
* @name Si70xx register values.
* @{
*/
#define SI70XX_ID_SI7006 (0x06)
#define SI70XX_ID_SI7013 (0x0D)
#define SI70XX_ID_SI7020 (0x14)
#define SI70XX_ID_SI7021 (0x15)
#define SI70XX_REVISION_1 (0xFF)
#define SI70XX_REVISION_2 (0x20)
/** @} */
/**
* @brief Si70xx device descriptor.
*/
typedef struct {
i2c_t i2c_dev; /**< I2C bus the sensors is connected to */
uint8_t address; /**< sensor address */
} si70xx_t;
/**
* @brief Device initialization parameters.
* @brief Device initialization parameters.
*/
typedef struct {
i2c_t i2c_dev; /**< I2C bus the sensor is connected to */
@ -90,24 +46,24 @@ typedef struct {
} si70xx_params_t;
/**
* @brief Test if the device id and revision number are as expected.
*
* @param[in] dev device descriptor
* @return zero on succesful test
* @return non-zero on unsuccesfull test.
* @brief Si70xx device descriptor.
*/
int si70xx_test(const si70xx_t *dev);
typedef struct {
si70xx_params_t params; /**< Device parameters */
} si70xx_t;
/**
* @brief Initialize and reset the sensor.
*
* @param[in] dev device descriptor
* @param[in] i2c_dev i2c device to use
* @param[in] address device address (depends on the chip)
* @return zero on succesful initialization.
* @return non-zero on error
* @param[in] params initialization parameters
*
* @return SI70XX_OK on successful initialization
* @return -SI70XX_ERR_NOI2C on I2C initialization error
* @return -SI70XX_ERR_NODEV on device test error
* @return -SI70XX_ERR_I2C on I2C bus error
*/
int si70xx_init(si70xx_t *dev, i2c_t i2c_dev, uint8_t address);
int si70xx_init(si70xx_t *dev, const si70xx_params_t *params);
/**
* @brief Read the relative humidity from the sensor. Uses clock streching.

View File

@ -1,3 +1 @@
MODULE = si70xx
include $(RIOTBASE)/Makefile.base

View File

@ -0,0 +1,78 @@
/*
* Copyright (C) 2016 Bas Stottelaar <basstottelaar@gmail.com>
*
* This file is subject to the terms and conditions of the GNU Lesser
* General Public License v2.1. See the file LICENSE in the top level
* directory for more details.
*/
/**
* @ingroup drivers_si70xx
*
* @{
* @file
* @brief Internal definitions for Si7006/13/20/21
*
* @author Bas Stottelaar <basstottelaar@gmail.com>
*/
#ifndef SI70XX_INTERNALS_H
#define SI70XX_INTERNALS_H
#ifdef __cplusplus
extern "C" {
#endif
/**
* @brief Si70xx chip addresses.
*/
#define SI70XX_I2C_ADDRESS (0x40)
/**
* @name Si70xx device commands.
* @{
*/
#define SI70XX_MEASURE_RH_HOLD (0xE5)
#define SI70XX_MEASURE_RH (0xF5)
#define SI70XX_MEASURE_TEMP_HOLD (0xE3)
#define SI70XX_MEASURE_TEMP (0xF3)
#define SI70XX_MEASURE_TEMP_PREV (0xE0)
#define SI70XX_RESET (0xFE)
#define SI70XX_WRITE_USER_REG (0xE6)
#define SI70XX_READ_USER_REG (0xE7)
#define SI70XX_WRITE_HEATER_REG (0x51)
#define SI70XX_READ_HEATER_REG (0x11)
#define SI70XX_READ_ID_FIRST_A (0xFA)
#define SI70XX_READ_ID_FIRST_B (0x0F)
#define SI70XX_READ_ID_SECOND_A (0xFC)
#define SI70XX_READ_ID_SECOND_B (0xC9)
#define SI70XX_READ_REVISION_A (0x84)
#define SI70XX_READ_REVISION_B (0xB8)
/** @} */
/**
* @name Si70xx register values.
* @{
*/
#if defined(MODULE_SI7006)
#define SI70XX_ID (0x06)
#elif defined(MODULE_SI7013)
#define SI70XX_ID (0x0D)
#elif defined(MODULE_SI7020)
#define SI70XX_ID (0x14)
#elif defined(MODULE_SI7021)
#define SI70XX_ID (0x15)
#else
#error "Please provide a valid Si70xx variant (Si7006/13/20/21)"
#endif
#define SI70XX_REVISION_1 (0xFF)
#define SI70XX_REVISION_2 (0x20)
/** @} */
#ifdef __cplusplus
}
#endif
#endif /* SI70XX_INTERNALS_H */
/** @} */

View File

@ -24,6 +24,7 @@
#include "board.h"
#include "si70xx.h"
#include "saul_reg.h"
#include "si70xx_internals.h"
#ifdef __cplusplus
extern "C" {
@ -37,11 +38,16 @@ extern "C" {
#define SI70XX_PARAM_I2C_DEV I2C_DEV(0)
#endif
#ifndef SI70XX_PARAM_ADDR
#define SI70XX_PARAM_ADDR (0x80)
#define SI70XX_PARAM_ADDR SI70XX_I2C_ADDRESS
#endif
#define SI70XX_PARAMS_DEFAULT { .i2c_dev = SI70XX_PARAM_I2C_DEV, \
#ifndef SI70XX_PARAMS
#define SI70XX_PARAMS { .i2c_dev = SI70XX_PARAM_I2C_DEV, \
.address = SI70XX_PARAM_ADDR }
#endif
#ifndef SI70XX_SAUL_INFO
#define SI70XX_SAUL_INFO { .name = "si70xx" }
#endif
/**@}*/
/**
@ -49,11 +55,7 @@ extern "C" {
*/
static const si70xx_params_t si70xx_params[] =
{
#ifdef SI70XX_PARAMS_CUSTOM
SI70XX_PARAMS_CUSTOM,
#else
SI70XX_PARAMS_DEFAULT,
#endif
SI70XX_PARAMS
};
/**
@ -61,7 +63,7 @@ static const si70xx_params_t si70xx_params[] =
*/
static const saul_reg_info_t si70xx_saul_reg_info[] =
{
{ .name = "si70xx" }
SI70XX_SAUL_INFO
};
#ifdef __cplusplus

View File

@ -18,82 +18,176 @@
* @}
*/
#include <string.h>
#include "xtimer.h"
#include "si70xx_internals.h"
#include "si70xx.h"
#define ENABLE_DEBUG (0)
#include "debug.h"
#define SI70XX_I2C (dev->params.i2c_dev)
#define SI70XX_ADDR (dev->params.address)
/**
* @brief Utility method to perform and reconstruct a measurement.
* @brief Internal helper function to perform and reconstruct a measurement.
*/
static uint32_t si70xx_measure(const si70xx_t *dev, uint8_t command)
static uint16_t _do_measure(const si70xx_t *dev, uint8_t command)
{
uint8_t result[2];
i2c_acquire(dev->i2c_dev);
i2c_write_byte(dev->i2c_dev, dev->address, command);
i2c_read_bytes(dev->i2c_dev, dev->address, result, 2);
i2c_release(dev->i2c_dev);
i2c_acquire(SI70XX_I2C);
if (i2c_write_byte(SI70XX_I2C, SI70XX_ADDR, command) != 1) {
DEBUG("[ERROR] Cannot write command '%d' to I2C.\n", command);
}
if (i2c_read_bytes(SI70XX_I2C, SI70XX_ADDR, result, 2) != 2) {
DEBUG("[ERROR] Cannot read command '%d' result from I2C.\n", command);
}
i2c_release(SI70XX_I2C);
/* reconstruct raw result */
return ((uint32_t)result[0] << 8) + (result[1] & 0xfc);
return ((uint16_t)result[0] << 8) + (result[1] & 0xfc);
}
int si70xx_test(const si70xx_t *dev)
/**
* @brief Internal helper function that reads the device serial number.
*/
static uint64_t _get_serial(const si70xx_t *dev)
{
uint8_t revision = si70xx_get_revision(dev);
uint8_t out[2];
uint8_t in_first[8] = { 0 };
uint8_t in_second[8] = { 0 };
/* read the lower bytes */
out[0] = SI70XX_READ_ID_FIRST_A;
out[1] = SI70XX_READ_ID_FIRST_B;
if (i2c_write_bytes(SI70XX_I2C, SI70XX_ADDR, out, 2) != 2) {
DEBUG("[ERROR] Cannot write command 'READ_ID_FIRST' to I2C.\n");
}
if (i2c_read_bytes(SI70XX_I2C, SI70XX_ADDR, in_first, 8) != 8) {
DEBUG("[ERROR] Cannot read device first ID from I2C.\n");
}
/* read the higher bytes */
out[0] = SI70XX_READ_ID_SECOND_A;
out[1] = SI70XX_READ_ID_SECOND_B;
if (i2c_write_bytes(SI70XX_I2C, SI70XX_ADDR, out, 2) != 2) {
DEBUG("[ERROR] Cannot write command 'READ_ID_SECOND' to I2C.\n");
}
if (i2c_read_bytes(SI70XX_I2C, SI70XX_ADDR, in_second, 8) != 8) {
DEBUG("[ERROR] Cannot read device second ID from I2C.\n");
}
/* calculate the ID */
uint32_t id_first = ((uint32_t)in_first[0] << 24) + ((uint32_t)in_first[2] << 16) +
(in_first[4] << 8) + (in_first[6] << 0);
uint32_t id_second = ((uint32_t)in_second[0] << 24) + ((uint32_t)in_second[2] << 16) +
(in_second[4] << 8) + (in_second[6] << 0);
return (((uint64_t) id_first) << 32) + id_second;
}
/**
* @brief Internal helper function that reads the device identifier.
*/
static uint8_t _get_id(const si70xx_t *dev)
{
return (_get_serial(dev) >> 24) & 0xff;
}
/**
* @brief Internal helper function that reads the device serial revision.
*/
static uint8_t _get_revision(const si70xx_t *dev)
{
uint8_t out[2];
uint8_t in = 0;
/* read the revision number */
out[0] = SI70XX_READ_REVISION_A;
out[1] = SI70XX_READ_REVISION_B;
if (i2c_write_bytes(SI70XX_I2C, SI70XX_ADDR, out, 2) != 2) {
DEBUG("[ERROR] Cannot write command 'READ_REVISION' to I2C.\n");
}
if (i2c_read_byte(SI70XX_I2C, SI70XX_ADDR, &in) != 1) {
DEBUG("[ERROR] Cannot read device revision from I2C.\n");
}
return in;
}
static int _test_device(const si70xx_t *dev)
{
uint8_t revision = _get_revision(dev);
if (revision != SI70XX_REVISION_1 && revision != SI70XX_REVISION_2) {
return -1;
DEBUG("[ERROR] Bad device revision (%d).\n", revision);
return -SI70XX_ERR_NODEV;
}
uint8_t id = si70xx_get_id(dev);
uint8_t id = _get_id(dev);
if (id != SI70XX_ID_SI7006 && id != SI70XX_ID_SI7013 &&
id != SI70XX_ID_SI7020 && id != SI70XX_ID_SI7021) {
return -2;
if (id != SI70XX_ID) {
DEBUG("[ERROR] Not a valid Si7006/13/20/21 device\n");
return -SI70XX_ERR_NODEV;;
}
return 0;
return SI70XX_OK;
}
int si70xx_init(si70xx_t *dev, i2c_t i2c_dev, uint8_t address)
int si70xx_init(si70xx_t *dev, const si70xx_params_t *params)
{
dev->i2c_dev = i2c_dev;
dev->address = address;
/* initialize the device descriptor */
memcpy(&dev->params, params, sizeof(si70xx_params_t));
/* setup the i2c bus */
i2c_acquire(dev->i2c_dev);
int result = i2c_init_master(dev->i2c_dev, I2C_SPEED_NORMAL);
i2c_release(dev->i2c_dev);
i2c_acquire(SI70XX_I2C);
if (result != 0) {
return result;
if (i2c_init_master(SI70XX_I2C, I2C_SPEED_NORMAL) != 0) {
DEBUG("[ERROR] Cannot initialize I2C bus.\n");
i2c_release(SI70XX_I2C);
return -SI70XX_ERR_NOI2C;
}
result = si70xx_test(dev);
if (result < 0) {
return result;
if (_test_device(dev) != SI70XX_OK) {
DEBUG("[ERROR] No valid device found.\n");
i2c_release(SI70XX_I2C);
return SI70XX_ERR_NODEV;
}
/* initialize the peripheral */
i2c_acquire(dev->i2c_dev);
i2c_write_byte(dev->i2c_dev, dev->address, SI70XX_RESET);
i2c_release(dev->i2c_dev);
if (i2c_write_byte(SI70XX_I2C, SI70XX_ADDR, SI70XX_RESET) != 1) {
DEBUG("[ERROR] Cannot reset device.\n");
i2c_release(SI70XX_I2C);
return SI70XX_ERR_I2C;
}
i2c_release(SI70XX_I2C);
/* sensor is ready after at most 25 ms */
xtimer_usleep(25 * US_PER_MS);
return 0;
DEBUG("[DEBUG] Device initialized with success.\n");
return SI70XX_OK;
}
uint16_t si70xx_get_relative_humidity(const si70xx_t *dev)
{
uint32_t raw;
uint16_t raw;
int32_t humidity;
/* perform measurement */
raw = si70xx_measure(dev, SI70XX_MEASURE_RH_HOLD);
raw = _do_measure(dev, SI70XX_MEASURE_RH_HOLD);
humidity = ((12500 * raw) / 65536) - 600;
@ -111,76 +205,23 @@ uint16_t si70xx_get_relative_humidity(const si70xx_t *dev)
int16_t si70xx_get_temperature(const si70xx_t *dev)
{
uint32_t raw;
uint16_t raw;
/* perform measurement */
raw = si70xx_measure(dev, SI70XX_MEASURE_TEMP_HOLD);
raw = _do_measure(dev, SI70XX_MEASURE_TEMP_HOLD);
return ((17572 * raw) / 65536) - 4685;
}
void si70xx_get_both(const si70xx_t *dev, uint16_t *humidity, int16_t *temperature)
{
uint32_t raw;
uint16_t raw;
/* read the humidity the normal way */
*humidity = si70xx_get_relative_humidity(dev);
/* read the temperature using the data from the previous measurement */
raw = si70xx_measure(dev, SI70XX_MEASURE_TEMP_PREV);
raw = _do_measure(dev, SI70XX_MEASURE_TEMP_PREV);
*temperature = ((17572 * raw) / 65536) - 4685;
}
uint64_t si70xx_get_serial(const si70xx_t *dev)
{
uint8_t out[2];
uint8_t in_first[8] = { 0 };
uint8_t in_second[8] = { 0 };
/* read the lower bytes */
out[0] = SI70XX_READ_ID_FIRST_A;
out[1] = SI70XX_READ_ID_FIRST_B;
i2c_acquire(dev->i2c_dev);
i2c_write_bytes(dev->i2c_dev, dev->address, out, 2);
i2c_read_bytes(dev->i2c_dev, dev->address, in_first, 8);
/* read the higher bytes */
out[0] = SI70XX_READ_ID_SECOND_A;
out[1] = SI70XX_READ_ID_SECOND_B;
i2c_write_bytes(dev->i2c_dev, dev->address, out, 2);
i2c_read_bytes(dev->i2c_dev, dev->address, in_second, 8);
i2c_release(dev->i2c_dev);
/* calculate the ID */
uint32_t id_first = ((uint32_t)in_first[0] << 24) + ((uint32_t)in_first[2] << 16) +
(in_first[4] << 8) + (in_first[6] << 0);
uint32_t id_second = ((uint32_t)in_second[0] << 24) + ((uint32_t)in_second[2] << 16) +
(in_second[4] << 8) + (in_second[6] << 0);
return (((uint64_t) id_first) << 32) + id_second;
}
uint8_t si70xx_get_id(const si70xx_t *dev)
{
return (si70xx_get_serial(dev) >> 24) & 0xff;
}
uint8_t si70xx_get_revision(const si70xx_t *dev)
{
uint8_t out[2];
uint8_t in = 0;
/* read the revision number */
out[0] = SI70XX_READ_REVISION_A;
out[1] = SI70XX_READ_REVISION_B;
i2c_acquire(dev->i2c_dev);
i2c_write_bytes(dev->i2c_dev, dev->address, out, 2);
i2c_read_byte(dev->i2c_dev, dev->address, &in);
i2c_release(dev->i2c_dev);
return in;
}

View File

@ -86,6 +86,12 @@ PSEUDOMODULES += adc121c
PSEUDOMODULES += sx1272
PSEUDOMODULES += sx1276
# include variants of Si70xx drivers as pseudo modules
PSEUDOMODULES += si7006
PSEUDOMODULES += si7013
PSEUDOMODULES += si7020
PSEUDOMODULES += si7021
# add all pseudo random number generator variants as pseudomodules
PSEUDOMODULES += prng_%

View File

@ -40,6 +40,11 @@ static si70xx_t si70xx_devs[SI70XX_NUMOF];
*/
static saul_reg_t saul_entries[SI70XX_NUMOF * 2];
/**
* @brief Define the number of saul info
*/
#define SI70XX_INFO_NUMOF (sizeof(si70xx_saul_reg_info) / sizeof(si70xx_saul_reg_info[0]))
/**
* @brief Reference the driver structs.
* @{
@ -50,13 +55,12 @@ extern const saul_driver_t si70xx_relative_humidity_saul_driver;
void auto_init_si70xx(void)
{
assert(SI70XX_INFO_NUMOF == SI70XX_NUMOF);
for (unsigned i = 0; i < SI70XX_NUMOF; i++) {
LOG_DEBUG("[auto_init_saul] initializing SI70xx #%u\n", i);
int res = si70xx_init(&si70xx_devs[i],
si70xx_params[i].i2c_dev,
si70xx_params[i].address);
if (res < 0) {
if (si70xx_init(&si70xx_devs[i], &si70xx_params[i]) != SI70XX_OK) {
LOG_ERROR("[auto_init_saul] error initializing SI70xx #%i\n", i);
continue;
}

View File

@ -1,17 +1,7 @@
APPLICATION = driver_si70xx
include ../Makefile.tests_common
USEMODULE += si70xx
USEMODULE += xtimer
# set default device parameters in case they are undefined
TEST_I2C ?= 0
TEST_I2C_ADDR ?= 0x80
TEST_PIN_EN ?= GPIO_PIN\(0,0\)
# export parameters
CFLAGS += -DTEST_I2C=$(TEST_I2C)
CFLAGS += -DTEST_I2C_ADDR=$(TEST_I2C_ADDR)
CFLAGS += -DTEST_PIN_EN=$(TEST_PIN_EN)
# This test should also work with Si7006, Si7013 and Si7020 variants.
USEMODULE += si7021
include $(RIOTBASE)/Makefile.include

View File

@ -1,14 +1,10 @@
# Si70xx Driver Test
## Introduction
This test will test if the Si7006/13/20/21 temperature and humidity sensor is working.
## Configuration
There are three parameters to configure:
* `TEST_I2C` &mdash; I2C device to use.
* `TEST_I2C_ADDR` &mdash; The sensor address (usually 0x80 or 0x81).
* `TEST_PIN_EN` &mdash; If required, toggle the enable pin via this GPIO pin (see `GPIO_PIN` macro for your board).
This test checks if the Si7006/13/20/21 temperature and humidity sensor
is working.
## Expected result
The sensor should continuously (every 1 sec) output the humidity and temperature. The precision should be two digits.
After initialization, the sensor continuously (every 1 sec) displays the
relative humidity and temperature. The precision is two digits.

View File

@ -18,72 +18,30 @@
* @}
*/
#ifndef TEST_I2C
#error "TEST_I2C not defined"
#endif
#ifndef TEST_I2C_ADDR
#error "TEST_I2C_ADDR not defined"
#endif
#ifndef TEST_PIN_EN
#error "TEST_PIN_EN not defined"
#endif
#include <stdio.h>
#include "periph/gpio.h"
#include "xtimer.h"
#include "si70xx_params.h"
#include "si70xx.h"
int main(void)
{
si70xx_t dev;
puts("SI7021 temperature and humidity sensor test application\n");
/* enable the sensor if test pin given */
if (TEST_PIN_EN != GPIO_UNDEF) {
printf("Toggling enable pin...");
if (gpio_init(TEST_PIN_EN, GPIO_OUT) == 0) {
puts("[OK]\n");
}
else {
puts("[Failed]\n");
return 1;
}
gpio_set(TEST_PIN_EN);
}
puts("SI7021 temperature and humidity sensor test application");
/* initialize the sensor */
printf("Initializing sensor...");
printf("Initializing sensor... ");
if (si70xx_init(&dev, TEST_I2C, TEST_I2C_ADDR) == 0) {
puts("[OK]\n");
if (si70xx_init(&dev, &si70xx_params[0]) == 0) {
puts("[OK]");
}
else {
puts("[Failed]");
return 1;
}
/* run sensor test */
printf("Testing sensor communication...");
if (si70xx_test(&dev) == 0) {
puts("[OK]\n");
}
else {
puts("[Failed]");
return 1;
}
/* print device id */
printf("Identified sensor as the Si70%02i\n", si70xx_get_id(&dev));
/* read temperature and humidity every 1 seconds */
bool both = false;