drivers/ds75lx: add basic driver
This commit is contained in:
parent
6b56a104b7
commit
35a3c4ddfe
@ -128,6 +128,11 @@ ifneq (,$(filter ds3234,$(USEMODULE)))
|
||||
FEATURES_REQUIRED += periph_spi
|
||||
endif
|
||||
|
||||
ifneq (,$(filter ds75lx,$(USEMODULE)))
|
||||
USEMODULE += xtimer
|
||||
FEATURES_REQUIRED += periph_i2c
|
||||
endif
|
||||
|
||||
ifneq (,$(filter dsp0401,$(USEMODULE)))
|
||||
USEMODULE += xtimer
|
||||
FEATURES_REQUIRED += periph_gpio
|
||||
|
||||
@ -70,6 +70,10 @@ ifneq (,$(filter ds3234,$(USEMODULE)))
|
||||
USEMODULE_INCLUDES += $(RIOTBASE)/drivers/ds3234/include
|
||||
endif
|
||||
|
||||
ifneq (,$(filter ds75lx,$(USEMODULE)))
|
||||
USEMODULE_INCLUDES += $(RIOTBASE)/drivers/ds75lx/include
|
||||
endif
|
||||
|
||||
ifneq (,$(filter dsp0401,$(USEMODULE)))
|
||||
USEMODULE_INCLUDES += $(RIOTBASE)/drivers/dsp0401/include
|
||||
endif
|
||||
|
||||
1
drivers/ds75lx/Makefile
Normal file
1
drivers/ds75lx/Makefile
Normal file
@ -0,0 +1 @@
|
||||
include $(RIOTBASE)/Makefile.base
|
||||
140
drivers/ds75lx/ds75lx.c
Normal file
140
drivers/ds75lx/ds75lx.c
Normal file
@ -0,0 +1,140 @@
|
||||
/*
|
||||
* Copyright (C) 2019 Inria
|
||||
*
|
||||
* 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_ds75lx
|
||||
* @{
|
||||
*
|
||||
* @file
|
||||
* @brief Device driver implementation for the DS75LX temperature sensor.
|
||||
*
|
||||
* @author Alexandre Abadie <alexandre.abadie@inria.fr>
|
||||
*
|
||||
* @}
|
||||
*/
|
||||
|
||||
#include <stdio.h>
|
||||
|
||||
#include "xtimer.h"
|
||||
|
||||
#include "ds75lx.h"
|
||||
#include "ds75lx_internals.h"
|
||||
#include "ds75lx_params.h"
|
||||
#include "periph/i2c.h"
|
||||
|
||||
#define ENABLE_DEBUG (0)
|
||||
#include "debug.h"
|
||||
|
||||
#define DEV_I2C (dev->params.i2c)
|
||||
#define DEV_ADDR (dev->params.addr)
|
||||
|
||||
static int _update_configuration_bits(const ds75lx_t *dev, uint8_t bit,
|
||||
uint8_t mask, bool set)
|
||||
{
|
||||
/* Acquire exclusive access */
|
||||
i2c_acquire(DEV_I2C);
|
||||
|
||||
uint8_t config;
|
||||
if (i2c_read_reg(DEV_I2C, DEV_ADDR, DS75LX_REG_CONFIGURATION, &config, 0) < 0) {
|
||||
DEBUG("[ds75lx] error reading configuration register\n");
|
||||
/* Release I2C device */
|
||||
i2c_release(DEV_I2C);
|
||||
|
||||
return -DS75LX_ERR_I2C;
|
||||
}
|
||||
|
||||
DEBUG("[ds75lx] initial configuration register value: 0x%02X\n", config);
|
||||
|
||||
/* clear bits */
|
||||
config &= ~mask;
|
||||
|
||||
/* set bits if required */
|
||||
if (set) {
|
||||
config |= bit;
|
||||
}
|
||||
|
||||
DEBUG("[ds75lx] configuration register value: 0x%02X\n", config);
|
||||
|
||||
if (i2c_write_reg(DEV_I2C, DEV_ADDR, DS75LX_REG_CONFIGURATION, config, 0) < 0) {
|
||||
DEBUG("[ds75lx] error writing configuration register\n");
|
||||
/* Release I2C device */
|
||||
i2c_release(DEV_I2C);
|
||||
|
||||
return -DS75LX_ERR_I2C;
|
||||
}
|
||||
|
||||
/* Release I2C device */
|
||||
i2c_release(DEV_I2C);
|
||||
|
||||
return DS75LX_OK;
|
||||
}
|
||||
|
||||
int ds75lx_init(ds75lx_t *dev, const ds75lx_params_t *params)
|
||||
{
|
||||
dev->params = *params;
|
||||
|
||||
/* Set resolution bits + force shutdown of sensor */
|
||||
return _update_configuration_bits(dev,
|
||||
(dev->params.resolution << DS75LX_CONF_R0_POS) | (1 << DS75LX_CONF_SD_POS),
|
||||
(DS75LX_CONF_R0_MASK | (1 << DS75LX_CONF_SD_POS)), true);
|
||||
}
|
||||
|
||||
int ds75lx_read_temperature(const ds75lx_t *dev, int16_t *temperature)
|
||||
{
|
||||
/* Acquire exclusive access */
|
||||
i2c_acquire(DEV_I2C);
|
||||
|
||||
uint8_t tmp[2];
|
||||
uint16_t temp;
|
||||
|
||||
if (i2c_read_regs(DEV_I2C, DEV_ADDR, DS75LX_REG_TEMPERATURE, tmp, 2, 0) < 0) {
|
||||
DEBUG("[ds75lx] error reading temperature register\n");
|
||||
/* Release I2C device */
|
||||
i2c_release(DEV_I2C);
|
||||
|
||||
return -DS75LX_ERR_I2C;
|
||||
}
|
||||
|
||||
/* Release I2C device */
|
||||
i2c_release(DEV_I2C);
|
||||
|
||||
temp = (tmp[0] << 8) | tmp[1];
|
||||
DEBUG("[ds75lx] temperature register content 0x%04X\n", temp);
|
||||
|
||||
/* isolate integer part of the temperature */
|
||||
int8_t temp_int = (temp & 0xff00) >> 8;
|
||||
/* compute fractional part of the temperature, the LSB bits 3 to 0 are
|
||||
always zero and not used in the conversion */
|
||||
uint8_t temp_frac = (temp & 0x00f0) >> 4;
|
||||
|
||||
/* fractional part is a multiple of 0.0625. Temperature is returned in c°C */
|
||||
*temperature = (temp_int * 100 + ((uint16_t)temp_frac * 100 >> 4));
|
||||
|
||||
return DS75LX_OK;
|
||||
}
|
||||
|
||||
int ds75lx_wakeup(const ds75lx_t *dev)
|
||||
{
|
||||
/* disable shutdown bit in configuration register */
|
||||
int ret = _update_configuration_bits(dev, (1 << DS75LX_CONF_SD_POS),
|
||||
(1 << DS75LX_CONF_SD_POS), false);
|
||||
|
||||
if (ret == DS75LX_OK) {
|
||||
/* Wait max conversion time (depends on resolution) */
|
||||
xtimer_usleep((DS75LX_MAX_CONVERSION_TIME << dev->params.resolution) * US_PER_MS);
|
||||
}
|
||||
|
||||
return ret;
|
||||
}
|
||||
|
||||
int ds75lx_shutdown(const ds75lx_t *dev)
|
||||
{
|
||||
/* enable shutdown bit in configuration register */
|
||||
return _update_configuration_bits(dev, (1 << DS75LX_CONF_SD_POS),
|
||||
(1 << DS75LX_CONF_SD_POS), true);
|
||||
}
|
||||
64
drivers/ds75lx/include/ds75lx_internals.h
Normal file
64
drivers/ds75lx/include/ds75lx_internals.h
Normal file
@ -0,0 +1,64 @@
|
||||
/*
|
||||
* Copyright (C) 2019 Inria
|
||||
*
|
||||
* 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_ds75lx
|
||||
* @{
|
||||
*
|
||||
* @file
|
||||
* @brief Internal addresses, registers, constants for the DS75LX sensor.
|
||||
*
|
||||
* @author Alexandre Abadie <alexandre.abadie@inria.fr>
|
||||
*/
|
||||
|
||||
#ifndef DS75LX_INTERNALS_H
|
||||
#define DS75LX_INTERNALS_H
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
/**
|
||||
* @brief DS75LX I2C address (7 bit address)
|
||||
*/
|
||||
#define DS75LX_ADDR (0x48)
|
||||
|
||||
/**
|
||||
* @name DS75LX registers
|
||||
* @{
|
||||
*/
|
||||
#define DS75LX_REG_TEMPERATURE (0x00)
|
||||
#define DS75LX_REG_CONFIGURATION (0x01)
|
||||
#define DS75LX_REG_T_HYST (0x02)
|
||||
#define DS75LX_REG_T_OS (0x03)
|
||||
/** @} */
|
||||
|
||||
/**
|
||||
* @name Configuration register bits
|
||||
* @{
|
||||
*/
|
||||
#define DS75LX_CONF_SD_POS (0)
|
||||
#define DS75LX_CONF_TM_POS (1)
|
||||
#define DS75LX_CONF_POL_POS (2)
|
||||
#define DS75LX_CONF_F0_POS (3)
|
||||
#define DS75LX_CONF_F0_MASK (0x18)
|
||||
#define DS75LX_CONF_R0_POS (5)
|
||||
#define DS75LX_CONF_R0_MASK (0x60)
|
||||
/** @} */
|
||||
|
||||
/**
|
||||
* @brief Max conversion time unit (ms)
|
||||
*/
|
||||
#define DS75LX_MAX_CONVERSION_TIME (25U)
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
||||
#endif /* DS75LX_INTERNALS_H */
|
||||
/** @} */
|
||||
65
drivers/ds75lx/include/ds75lx_params.h
Normal file
65
drivers/ds75lx/include/ds75lx_params.h
Normal file
@ -0,0 +1,65 @@
|
||||
/*
|
||||
* Copyright (C) 2019 Inria
|
||||
*
|
||||
* 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_ds75lx
|
||||
*
|
||||
* @{
|
||||
* @file
|
||||
* @brief Default configuration for DS75LX
|
||||
*
|
||||
* @author Alexandre Abadie <alexandre.abadie@inria.fr>
|
||||
*/
|
||||
|
||||
#ifndef DS75LX_PARAMS_H
|
||||
#define DS75LX_PARAMS_H
|
||||
|
||||
#include "board.h"
|
||||
#include "ds75lx.h"
|
||||
#include "ds75lx_internals.h"
|
||||
#include "saul_reg.h"
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
/**
|
||||
* @name Set default configuration parameters for the DS75LX
|
||||
* @{
|
||||
*/
|
||||
#ifndef DS75LX_PARAM_I2C_DEV
|
||||
#define DS75LX_PARAM_I2C_DEV I2C_DEV(0)
|
||||
#endif
|
||||
#ifndef DS75LX_PARAM_I2C_ADDR
|
||||
#define DS75LX_PARAM_I2C_ADDR DS75LX_ADDR
|
||||
#endif
|
||||
#ifndef DS75LX_PARAM_RESOLUTION
|
||||
#define DS75LX_PARAM_RESOLUTION DS75LX_RESOLUTION_10
|
||||
#endif
|
||||
|
||||
#ifndef DS75LX_PARAMS
|
||||
#define DS75LX_PARAMS { .i2c = DS75LX_PARAM_I2C_DEV, \
|
||||
.addr = DS75LX_PARAM_I2C_ADDR, \
|
||||
.resolution = DS75LX_PARAM_RESOLUTION }
|
||||
#endif
|
||||
/**@}*/
|
||||
|
||||
/**
|
||||
* @brief Configure DS75LX
|
||||
*/
|
||||
static const ds75lx_params_t ds75lx_params[] =
|
||||
{
|
||||
DS75LX_PARAMS
|
||||
};
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
||||
#endif /* DS75LX_PARAMS_H */
|
||||
/** @} */
|
||||
112
drivers/include/ds75lx.h
Normal file
112
drivers/include/ds75lx.h
Normal file
@ -0,0 +1,112 @@
|
||||
/*
|
||||
* Copyright (C) 2019 Inria
|
||||
*
|
||||
* 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.
|
||||
*/
|
||||
|
||||
/**
|
||||
* @defgroup drivers_ds75lx Maxim DS75LX temperature sensor
|
||||
* @ingroup drivers_sensors
|
||||
* @brief Device driver interface for the Maxim DS75LX temperature sensor
|
||||
*
|
||||
* @{
|
||||
*
|
||||
* @file
|
||||
*
|
||||
* @author Alexandre Abadie <alexandre.abadie@inria.fr>
|
||||
*/
|
||||
|
||||
#ifndef DS75LX_H
|
||||
#define DS75LX_H
|
||||
|
||||
#include "saul.h"
|
||||
#include "periph/i2c.h"
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
/**
|
||||
* @brief Thermometer resolution
|
||||
*/
|
||||
typedef enum {
|
||||
DS75LX_RESOLUTION_9 = 0, /**< 9 bits resolution, 25ms max conversion time */
|
||||
DS75LX_RESOLUTION_10, /**< 10 bits resolution, 50ms max conversion time */
|
||||
DS75LX_RESOLUTION_11, /**< 11 bits resolution, 100ms max conversion time */
|
||||
DS75LX_RESOLUTION_12, /**< 12 bits resolution, 200ms max conversion time */
|
||||
} ds75lx_resolution_t;
|
||||
|
||||
/**
|
||||
* @brief Device initialization parameters
|
||||
*/
|
||||
typedef struct {
|
||||
i2c_t i2c; /**< I2C device which is used */
|
||||
uint8_t addr; /**< I2C address */
|
||||
ds75lx_resolution_t resolution; /**< Thermometer resolution */
|
||||
} ds75lx_params_t;
|
||||
|
||||
/**
|
||||
* @brief Device descriptor for the DS75LX sensor
|
||||
*/
|
||||
typedef struct {
|
||||
ds75lx_params_t params; /**< Device initialization parameters */
|
||||
} ds75lx_t;
|
||||
|
||||
/**
|
||||
* @brief Status and error return codes
|
||||
*/
|
||||
enum {
|
||||
DS75LX_OK = 0, /**< everything was fine */
|
||||
DS75LX_ERR_I2C, /**< error when reading/writing I2C bus */
|
||||
};
|
||||
|
||||
/**
|
||||
* @brief Initialize the given DS75LX device
|
||||
*
|
||||
* @param[out] dev Initialized device descriptor of DS75LX device
|
||||
* @param[in] params Initialization parameters
|
||||
*
|
||||
* @return DS75LX_OK on success
|
||||
* @return -DS75LX_ERR_I2C if an error occured when reading/writing
|
||||
*/
|
||||
int ds75lx_init(ds75lx_t *dev, const ds75lx_params_t *params);
|
||||
|
||||
/**
|
||||
* @brief Read temperature value from the given DS75LX device, returned in c°C
|
||||
*
|
||||
* @param[in] dev Device descriptor of DS75LX device
|
||||
* @param[out] temperature Temperature in c°C
|
||||
*
|
||||
* @return DS75LX_OK on success
|
||||
* @return -DS75LX_ERR_I2C if an error occured when reading/writing
|
||||
*/
|
||||
int ds75lx_read_temperature(const ds75lx_t *dev, int16_t *temperature);
|
||||
|
||||
/**
|
||||
* @brief Wakeup the sensor
|
||||
*
|
||||
* @param[in] dev Device descriptor of DS75LX device
|
||||
*
|
||||
* @return DS75LX_OK on success
|
||||
* @return -DS75LX_ERR_I2C if an error occured when reading/writing
|
||||
*/
|
||||
int ds75lx_wakeup(const ds75lx_t *dev);
|
||||
|
||||
/**
|
||||
* @brief Shutdown the sensor
|
||||
*
|
||||
* @param[in] dev Device descriptor of DS75LX device
|
||||
*
|
||||
* @return DS75LX_OK on success
|
||||
* @return -DS75LX_ERR_I2C if an error occured when reading/writing
|
||||
*/
|
||||
int ds75lx_shutdown(const ds75lx_t *dev);
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
||||
#endif /* DS75LX_H */
|
||||
/** @} */
|
||||
Loading…
x
Reference in New Issue
Block a user