1
0
mirror of https://github.com/RIOT-OS/RIOT.git synced 2025-01-18 12:52:44 +01:00

drivers/dht: updated auto-init + some enhancements

- unified auto-init to use same style as other SAUl devices
- make use of named return values
- added some asserts
This commit is contained in:
Hauke Petersen 2017-01-30 16:47:26 +01:00
parent 8edb69a702
commit 3afc3eb10b
5 changed files with 97 additions and 43 deletions

View File

@ -1,7 +1,7 @@
/*
* Copyright 2015 Ludwig Knüpfer
* 2015 Christian Mehlis
* 2016 Freie Universität Berlin
* 2016-2017 Freie Universität Berlin
*
* 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
@ -27,6 +27,7 @@
#include <string.h>
#include "log.h"
#include "assert.h"
#include "xtimer.h"
#include "timex.h"
#include "periph/gpio.h"
@ -39,11 +40,6 @@
#define PULSE_WIDTH_THRESHOLD (40U)
/**
* @brief Allocation of memory for device descriptors
*/
dht_t dht_devs[DHT_NUMOF];
static uint16_t read(gpio_t pin, int bits)
{
uint16_t res = 0;
@ -65,36 +61,23 @@ static uint16_t read(gpio_t pin, int bits)
return res;
}
void dht_auto_init(void)
{
for (unsigned i = 0; i < DHT_NUMOF; i++) {
if (dht_init(&dht_devs[i], &dht_params[i]) < 0) {
LOG_ERROR("Unable to initialize DHT sensor #%i\n", i);
}
#ifdef MODULE_SAUL_REG
for (int j = 0; j < 2; j++) {
dht_saul_reg[i][j].dev = &dht_devs[i];
saul_reg_add(&dht_saul_reg[i][j]);
}
#endif
}
}
int dht_init(dht_t *dev, const dht_params_t *params)
{
DEBUG("dht_init\n");
/* check parameters and configuration */
assert(dev && params &&
((dev->type == DHT11) || (dev->type == DHT22) || (dev->type == DHT21)));
memcpy(dev, params, sizeof(dht_t));
if (gpio_init(dev->pin, GPIO_OUT) == -1) {
return -1;
}
gpio_init(dev->pin, GPIO_OUT);
gpio_set(dev->pin);
xtimer_usleep(2000 * US_PER_MS);
DEBUG("dht_init: success\n");
return 0;
return DHT_OK;
}
int dht_read(dht_t *dev, int16_t *temp, int16_t *hum)
@ -102,6 +85,8 @@ int dht_read(dht_t *dev, int16_t *temp, int16_t *hum)
uint8_t csum, sum;
uint16_t raw_hum, raw_temp;
assert(dev && temp && hum);
/* send init signal to device */
gpio_clear(dev->pin);
xtimer_usleep(20 * US_PER_MS);
@ -133,7 +118,7 @@ int dht_read(dht_t *dev, int16_t *temp, int16_t *hum)
sum = (raw_temp >> 8) + (raw_temp & 0xff) + (raw_hum >> 8) + (raw_hum & 0xff);
if ((sum != csum) || (csum == 0)) {
DEBUG("error: checksum invalid\n");
return -1;
return DHT_NOCSUM;
}
/* parse the RAW values */
@ -154,8 +139,8 @@ int dht_read(dht_t *dev, int16_t *temp, int16_t *hum)
}
break;
default:
return -2;
return DHT_NODEV; /* this should never be reached */
}
return 0;
return DHT_OK;
}

View File

@ -67,17 +67,11 @@ static const dht_params_t dht_params[] =
/**
* @brief Allocate and configure entries to the SAUL registry
*/
saul_reg_t dht_saul_reg[][2] =
static const saul_reg_info_t dht_saul_info[][2] =
{
{
{
.name = "dht-temp",
.driver = &dht_temp_saul_driver
},
{
.name = "dht-hum",
.driver = &dht_hum_saul_driver
}
{ .name = "dht-temp" },
{ .name = "dht-hum" }
}
};
#endif

View File

@ -1,7 +1,7 @@
/*
* Copyright 2015 Ludwig Knüpfer,
* 2015 Christian Mehlis
* 2016 Freie Universität Berlin
* 2016-2017 Freie Universität Berlin
*
* 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
@ -37,6 +37,15 @@
extern "C" {
#endif
/**
* @brief possible return codes
*/
enum {
DHT_OK = 0, /**< all good */
DHT_NOCSUM = -1, /**< checksum error */
DHT_NODEV = -2 /**< device type not defined */
};
/**
* @brief data type for storing DHT sensor readings
*/

View File

@ -160,11 +160,6 @@ void auto_init(void)
DEBUG("Auto init TCP module\n");
gnrc_tcp_init();
#endif
#ifdef MODULE_DHT
DEBUG("Auto init DHT devices.\n");
extern void dht_auto_init(void);
dht_auto_init();
#endif
#ifdef MODULE_LWIP
DEBUG("Bootstraping lwIP.\n");
lwip_bootstrap();
@ -319,6 +314,10 @@ void auto_init(void)
extern void auto_init_hdc1000(void);
auto_init_hdc1000();
#endif
#ifdef MODULE_DHT
extern void auto_init_dht(void);
auto_init_dht();
#endif
#endif /* MODULE_AUTO_INIT_SAUL */

View File

@ -0,0 +1,67 @@
/*
* Copyright (C) 2017 Freie Universität Berlin
*
* 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 auto_init_saul
* @{
*
* @file
* @brief Auto initialization for DHT temperature/humidity sensors
*
* @author Hauke Petersen <hauke.petersen@fu-berlin.de>
*
* @}
*/
#ifdef MODULE_DHT
#include "log.h"
#include "saul_reg.h"
#include "dht.h"
#include "dht_params.h"
/**
* @brief Define the number of configured sensors
*/
#define DHT_NUM (sizeof(dht_params) / sizeof(dht_params[0]))
/**
* @brief Allocate memory for the device descriptors
*/
static dht_t dht_devs[DHT_NUM];
/**
* @brief Memory for the SAUL registry entries
*/
static saul_reg_t saul_entries[DHT_NUM * 2];
void auto_init_dht(void)
{
for (unsigned int i = 0; i < DHT_NUM; i++) {
LOG_DEBUG("[auto_init_saul] initializing dht #%u\n", i);
if (dht_init(&dht_devs[i], &dht_params[i]) != DHT_OK) {
LOG_ERROR("[auto_init_saul] error initializing dht #%u\n", i);
}
else {
saul_entries[(i * 2)].dev = &(dht_devs[i]);
saul_entries[(i * 2)].name = dht_saul_info[i][0].name;
saul_entries[(i * 2)].driver = &dht_temp_saul_driver;
saul_entries[(i * 2) + 1].dev = &(dht_devs[i]);
saul_entries[(i * 2) + 1].name = dht_saul_info[i][1].name;
saul_entries[(i * 2) + 1].driver = &dht_hum_saul_driver;
saul_reg_add(&(saul_entries[(i * 2)]));
saul_reg_add(&(saul_entries[(i * 2) + 1]));
}
}
}
#else
typedef int dont_be_pedantic;
#endif /* MODULE_DHT */