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

drivers/ad7746: Add SAUL integration

This commit is contained in:
Leandro Lanzieri 2019-02-15 13:34:57 +01:00
parent afeb5c1e04
commit a13ab5ee08
3 changed files with 192 additions and 0 deletions

View File

@ -0,0 +1,95 @@
/*
* Copyright (C) 2019 HAW Hamburg
*
* 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_ad7746
* @{
*
* @file
* @brief AD7746 adaption to the RIOT actuator/sensor interface
*
* @author Leandro Lanzieri <leandro.lanzieri@haw-hamburg.de>
*
* @}
*/
#include <string.h>
#include <stdio.h>
#include "saul.h"
#include "ad7746.h"
static int _read_cap(const void *dev, phydat_t *res)
{
int val;
if (ad7746_read_capacitance_1((ad7746_t *)dev, &val)) {
return -ECANCELED;
}
res->val[0] = val;
res->unit = UNIT_F;
res->scale = -15;
return 1;
}
static int _read_temp(const void *dev, phydat_t *res)
{
int val;
int result;
do {
result = ad7746_read_temperature_int((ad7746_t *)dev, &val);
} while (result == AD7746_NODATA);
if (result == AD7746_OK) {
res->val[0] = val;
res->unit = UNIT_TEMP_C;
res->scale = 0;
return 1;
}
else {
return -ECANCELED;
}
}
static int _read_volt(const void *dev, phydat_t *res)
{
int val;
int result;
do {
result = ad7746_read_voltage_in((ad7746_t *)dev, &val);
} while (result == AD7746_NODATA);
if (result == AD7746_OK) {
res->val[0] = val;
res->unit = UNIT_V;
res->scale = -3;
return 1;
}
else {
return -ECANCELED;
}
}
const saul_driver_t ad7746_saul_driver_cap = {
.read = _read_cap,
.write = saul_notsup,
.type = SAUL_SENSE_CAPACITANCE,
};
const saul_driver_t ad7746_saul_driver_temp = {
.read = _read_temp,
.write = saul_notsup,
.type = SAUL_SENSE_TEMP
};
const saul_driver_t ad7746_saul_driver_volt = {
.read = _read_volt,
.write = saul_notsup,
.type = SAUL_SENSE_VOLTAGE
};

View File

@ -321,6 +321,10 @@ void auto_init(void)
extern void auto_init_gpio(void);
auto_init_gpio();
#endif
#ifdef MODULE_AD7746
extern void auto_init_ad7746(void);
auto_init_ad7746();
#endif
#ifdef MODULE_ADCXX1C
extern void auto_init_adcxx1c(void);
auto_init_adcxx1c();

View File

@ -0,0 +1,93 @@
/*
* Copyright (C) 2019 HAW Hamburg
*
* 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 sys_auto_init_saul
* @{
*
* @file
* @brief Auto initialization of AD7746
*
* @author Leandro Lanzieri <leandro.lanzieri@haw-hamburg.de>
*
* @}
*/
#ifdef MODULE_AD7746
#include "assert.h"
#include "log.h"
#include "saul_reg.h"
#include "ad7746.h"
#include "ad7746_params.h"
/**
* @brief Define the number of configured sensors
*/
#define AD7746_NUM (sizeof(ad7746_params) / sizeof(ad7746_params[0]))
/**
* @brief Allocate memory for the device descriptors.
*/
static ad7746_t ad7746_devs[AD7746_NUM];
/**
* @brief Memory for the SAUL registry entries. Each device provides
* 3 SAUL entries (capacitance, temperature and voltage)
*/
static saul_reg_t saul_entries[AD7746_NUM * 3];
/**
* @brief Define the number of saul info
*/
#define AD7746_INFO_NUM (sizeof(ad7746_saul_info) / sizeof(ad7746_saul_info[0]))
/**
* @brief Reference the driver structs
* @{
*/
extern saul_driver_t ad7746_saul_driver_cap;
extern saul_driver_t ad7746_saul_driver_temp;
extern saul_driver_t ad7746_saul_driver_volt;
/** @} */
void auto_init_ad7746(void)
{
assert(AD7746_INFO_NUM == AD7746_NUM);
for (unsigned i = 0; i < AD7746_NUM; i++) {
LOG_DEBUG("[auto_init_saul] initializing ad7746 #%d\n", i);
if (ad7746_init(&ad7746_devs[i], &ad7746_params[i]) < 0) {
LOG_ERROR("[auto_init_saul] error initializing ad7746 #%d\n", i);
continue;
}
/* add capacitance driver */
saul_entries[i].dev = &(ad7746_devs[i]);
saul_entries[i].name = ad7746_saul_info[i].name;
saul_entries[i].driver = &ad7746_saul_driver_cap;
saul_reg_add(&(saul_entries[i]));
/* add temperature driver */
saul_entries[i + 1].dev = &(ad7746_devs[i]);
saul_entries[i + 1].name = ad7746_saul_info[i].name;
saul_entries[i + 1].driver = &ad7746_saul_driver_temp;
saul_reg_add(&(saul_entries[i + 1]));
/* add voltage driver */
saul_entries[i + 2].dev = &(ad7746_devs[i]);
saul_entries[i + 2].name = ad7746_saul_info[i].name;
saul_entries[i + 2].driver = &ad7746_saul_driver_volt;
saul_reg_add(&(saul_entries[i + 2]));
}
}
#else
typedef int dont_be_pedantic;
#endif /* MODULE_AD7746 */