Skip to content

Commit

Permalink
Merge pull request #8673 from aabadie/pr/drivers/params/tsl2561
Browse files Browse the repository at this point in the history
drivers/tsl2561: apply unified params definition scheme + cleanup
  • Loading branch information
aabadie authored and dylad committed Jul 11, 2018
2 parents 4608210 + d3a922c commit 992da8d
Show file tree
Hide file tree
Showing 7 changed files with 72 additions and 99 deletions.
5 changes: 5 additions & 0 deletions drivers/Makefile.dep
Original file line number Diff line number Diff line change
Expand Up @@ -393,3 +393,8 @@ ifneq (,$(filter tja1042,$(USEMODULE)))
USEMODULE += can_trx
FEATURES_REQUIRED += periph_gpio
endif

ifneq (,$(filter tsl2561,$(USEMODULE)))
FEATURES_REQUIRED += periph_i2c
USEMODULE += xtimer
endif
18 changes: 8 additions & 10 deletions drivers/include/tsl2561.h
Original file line number Diff line number Diff line change
Expand Up @@ -65,35 +65,33 @@ extern "C" {
/** @} */

/**
* @brief Device descriptor for the TSL2561 sensor
* @brief Device initialization parameters
*/
typedef struct {
i2c_t i2c_dev; /**< I2C device which is used */
uint8_t addr; /**< address on I2C bus */
uint8_t gain; /**< gain */
uint8_t integration; /**< integration time */
} tsl2561_t;
} tsl2561_params_t;

/**
* @brief Device initialization parameters
* @brief Device descriptor for the TSL2561 sensor
*/
typedef tsl2561_t tsl2561_params_t;
typedef struct {
tsl2561_params_t params; /**< device initialization parameters */
} tsl2561_t;

/**
* @brief Initialize the given TSL2561 device
*
* @param[out] dev Initialized device descriptor of BMP180 device
* @param[in] i2c I2C bus the sensor is connected to
* @param[in] addr I2C address of the sensor on the I2C bus
* @param[in] gain TSL2561 gain
* @param[in] integration TSL2561 integration time
* @param[in] params Initialization parameters
*
* @return 0 on success
* @return -1 if given I2C is not available
* @return -2 if not a TSL2561 sensor
*/
int tsl2561_init(tsl2561_t *dev, i2c_t i2c, uint8_t addr,
uint8_t gain, uint8_t integration);
int tsl2561_init(tsl2561_t *dev, const tsl2561_params_t *params);

/**
* @brief Read illuminance value from the given TSL2561 device, returned in lx.
Expand Down
17 changes: 9 additions & 8 deletions drivers/tsl2561/include/tsl2561_params.h
Original file line number Diff line number Diff line change
Expand Up @@ -44,30 +44,31 @@ extern "C" {
#define TSL2561_PARAM_INTEGRATION TSL2561_INTEGRATIONTIME_402MS
#endif

#define TSL2561_PARAMS_DEFAULT { .i2c_dev = TSL2561_PARAM_I2C_DEV, \
#ifndef TSL2561_PARAMS
#define TSL2561_PARAMS { .i2c_dev = TSL2561_PARAM_I2C_DEV, \
.addr = TSL2561_PARAM_ADDR, \
.gain = TSL2561_PARAM_GAIN, \
.integration = TSL2561_PARAM_INTEGRATION }
#endif
#ifndef TSL2561_SAUL_INFO
#define TSL2561_SAUL_INFO { .name= "tsl2561" }
#endif
/**@}*/

/**
* @brief Configure TSL2561
*/
static const tsl2561_params_t tsl2561_params[] =
{
#ifdef TSL2561_PARAMS_CUSTOM
TSL2561_PARAMS_CUSTOM,
#else
TSL2561_PARAMS_DEFAULT,
#endif
TSL2561_PARAMS
};

/**
* @brief Allocate and configure entries to the SAUL registry
*/
saul_reg_info_t tsl2561_saul_reg_info[] =
saul_reg_info_t tsl2561_saul_info[] =
{
{ .name= "tsl2561" }
TSL2561_SAUL_INFO
};

#ifdef __cplusplus
Expand Down
78 changes: 37 additions & 41 deletions drivers/tsl2561/tsl2561.c
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,11 @@
#define ENABLE_DEBUG (0)
#include "debug.h"

#define DEV_I2C (dev->params.i2c_dev)
#define DEV_ADDR (dev->params.addr)
#define DEV_GAIN (dev->params.gain)
#define DEV_INTEGRATION (dev->params.integration)

/* internal helpers */
static void _enable(const tsl2561_t *dev);
static void _disable(const tsl2561_t *dev);
Expand All @@ -40,32 +45,23 @@ static void _print_init_info(const tsl2561_t *dev);
* TSL2561 Core API *
*---------------------------------------------------------------------------*/

int tsl2561_init(tsl2561_t *dev,
i2c_t i2c, uint8_t addr, uint8_t gain, uint8_t integration)
int tsl2561_init(tsl2561_t *dev, const tsl2561_params_t *params)
{
dev->i2c_dev = i2c;
dev->addr = addr;
dev->gain = gain;
dev->integration = integration;
_print_init_info(dev);
dev->params = *params;

/* Initialize I2C interface */
if (i2c_init_master(dev->i2c_dev, I2C_SPEED_NORMAL)) {
DEBUG("[Error] I2C device not enabled\n");
return TSL2561_NOI2C;
}
_print_init_info(dev);

DEBUG("[Info] I2C device initialized with success!\n");

/* Acquire exclusive access */
i2c_acquire(dev->i2c_dev);
i2c_acquire(DEV_I2C);

DEBUG("[Info] Access acquired !\n");

/* Verify sensor ID */
uint8_t id;
i2c_read_reg(dev->i2c_dev, dev->addr,
TSL2561_COMMAND_MODE | TSL2561_REGISTER_ID, &id);
i2c_read_reg(DEV_I2C, DEV_ADDR,
TSL2561_COMMAND_MODE | TSL2561_REGISTER_ID, &id, 0);
DEBUG("[Info] ID ? %d\n", id);
if (id != TSL2561_ID ) {
DEBUG("[Error] not a TSL2561 sensor\n");
Expand All @@ -75,16 +71,16 @@ int tsl2561_init(tsl2561_t *dev,
_enable(dev);

/* configuring gain and integration time */
i2c_write_reg(dev->i2c_dev, dev->addr,
i2c_write_reg(DEV_I2C, DEV_ADDR,
TSL2561_COMMAND_MODE | TSL2561_REGISTER_TIMING,
dev->integration | dev->gain);
DEV_INTEGRATION | DEV_GAIN, 0);

#if ENABLE_DEBUG
uint8_t timing;
i2c_read_reg(dev->i2c_dev, dev->addr,
TSL2561_COMMAND_MODE | TSL2561_REGISTER_TIMING, &timing);
i2c_read_reg(DEV_I2C, DEV_ADDR,
TSL2561_COMMAND_MODE | TSL2561_REGISTER_TIMING, &timing, 0);
DEBUG("[Info] Timing ? %d (expected: %d)\n",
timing, dev->integration | dev->gain);
timing, DEV_INTEGRATION | DEV_GAIN);
#endif

_disable(dev);
Expand All @@ -107,7 +103,7 @@ uint16_t tsl2561_read_illuminance(const tsl2561_t *dev)
uint32_t channel_1;
uint32_t channel_0;

switch (dev->integration) {
switch (DEV_INTEGRATION) {
case TSL2561_INTEGRATIONTIME_13MS:
channel_scale = TSL2561_CHSCALE_TINT0;
break;
Expand All @@ -122,7 +118,7 @@ uint16_t tsl2561_read_illuminance(const tsl2561_t *dev)
}

/* Scale for gain (1x or 16x) */
if (!dev->gain) {
if (!DEV_GAIN) {
channel_scale = channel_scale << 4;
}

Expand Down Expand Up @@ -187,13 +183,13 @@ uint16_t tsl2561_read_illuminance(const tsl2561_t *dev)
static void _enable(const tsl2561_t *dev)
{
/* enabling device */
i2c_write_reg(dev->i2c_dev, dev->addr,
i2c_write_reg(DEV_I2C, DEV_ADDR,
TSL2561_COMMAND_MODE | TSL2561_REGISTER_CONTROL,
TSL2561_CONTROL_POWERON);
TSL2561_CONTROL_POWERON, 0);
#if ENABLE_DEBUG
uint8_t en;
i2c_read_reg(dev->i2c_dev, dev->addr,
TSL2561_COMMAND_MODE | TSL2561_REGISTER_CONTROL, &en);
i2c_read_reg(DEV_I2C, DEV_ADDR,
TSL2561_COMMAND_MODE | TSL2561_REGISTER_CONTROL, &en, 0);
DEBUG("[Info] Enabled ? %s\n", en == 3 ? "true" : "false");
#endif
}
Expand All @@ -202,14 +198,14 @@ static void _enable(const tsl2561_t *dev)
static void _disable(const tsl2561_t *dev)
{
/* disabling device */
i2c_write_reg(dev->i2c_dev, dev->addr,
i2c_write_reg(DEV_I2C, DEV_ADDR,
TSL2561_COMMAND_MODE | TSL2561_REGISTER_CONTROL,
TSL2561_CONTROL_POWEROFF );
TSL2561_CONTROL_POWEROFF, 0);

#if ENABLE_DEBUG
uint8_t dis;
i2c_read_reg(dev->i2c_dev, dev->addr,
TSL2561_COMMAND_MODE | TSL2561_REGISTER_CONTROL, &dis);
i2c_read_reg(DEV_I2C, DEV_ADDR,
TSL2561_COMMAND_MODE | TSL2561_REGISTER_CONTROL, &dis, 0);
DEBUG("[Info] Disabled ? %s\n", dis == 0 ? "true": "false");
#endif
}
Expand All @@ -220,7 +216,7 @@ static void _read_data(const tsl2561_t *dev, uint16_t *full, uint16_t *ir)
_enable(dev);

/* Wait integration time in ms for ADC to complete */
switch (dev->integration) {
switch (DEV_INTEGRATION) {
case TSL2561_INTEGRATIONTIME_13MS:
xtimer_usleep(13700);
break;
Expand All @@ -236,17 +232,17 @@ static void _read_data(const tsl2561_t *dev, uint16_t *full, uint16_t *ir)

char buffer[2] = { 0 };
/* Read full spectrum channel */
i2c_read_regs(dev->i2c_dev, dev->addr,
i2c_read_regs(DEV_I2C, DEV_ADDR,
TSL2561_COMMAND_MODE | TSL2561_COMMAND_WORD | TSL2561_REGISTER_CHAN0,
buffer, 2);
buffer, 2, 0);
*full = (buffer[1] << 8) | buffer[0];

memset(buffer, 0, sizeof(buffer));

/* Read infrared spectrum channel */
i2c_read_regs(dev->i2c_dev, dev->addr,
i2c_read_regs(DEV_I2C, DEV_ADDR,
TSL2561_COMMAND_MODE | TSL2561_COMMAND_WORD | TSL2561_REGISTER_CHAN1,
buffer, 2);
buffer, 2, 0);
*ir = (buffer[1] << 8) | buffer[0];

/* Turn the device off to save power */
Expand All @@ -255,9 +251,9 @@ static void _read_data(const tsl2561_t *dev, uint16_t *full, uint16_t *ir)

static void _print_init_info(const tsl2561_t *dev)
{
DEBUG("[Info] I2C device: %d\n", dev->i2c_dev);
DEBUG("[Info] Address: %d\n", dev->addr);
switch(dev->gain) {
DEBUG("[Info] I2C device: %d\n", DEV_I2C);
DEBUG("[Info] Address: %d\n", DEV_ADDR);
switch(DEV_GAIN) {
case TSL2561_GAIN_1X:
DEBUG("[Info] Gain: 1X\n");
break;
Expand All @@ -267,11 +263,11 @@ static void _print_init_info(const tsl2561_t *dev)
break;

default:
DEBUG("[Info] Invalid gain %d\n", dev->gain);
DEBUG("[Info] Invalid gain %d\n", DEV_GAIN);
break;
}

switch(dev->integration) {
switch(DEV_INTEGRATION) {
case TSL2561_INTEGRATIONTIME_13MS:
DEBUG("[Info] Integration time: 13ms\n");
break;
Expand All @@ -285,7 +281,7 @@ static void _print_init_info(const tsl2561_t *dev)
DEBUG("[Info] Integration time: n/a\n");
break;
default:
DEBUG("[Info] Invalid integration time %d\n", dev->integration);
DEBUG("[Info] Invalid integration time %d\n", DEV_INTEGRATION);
break;
}
}
16 changes: 9 additions & 7 deletions sys/auto_init/saul/auto_init_tsl2561.c
Original file line number Diff line number Diff line change
Expand Up @@ -40,30 +40,32 @@ static tsl2561_t tsl2561_devs[TSL2561_NUMOF];
*/
static saul_reg_t saul_entries[TSL2561_NUMOF];

/**
* @brief Define the number of saul info
*/
#define TSL2561_INFO_NUMOF (sizeof(tsl2561_saul_info) / sizeof(tsl2561_saul_info[0]))

/**
* @brief Reference the driver structs.
* @{
*/
extern const saul_driver_t tsl2561_illuminance_saul_driver;
/** @} */

void auto_init_tsl2561(void)
{
assert(TSL2561_NUMOF == TSL2561_INFO_NUMOF);

for (unsigned i = 0; i < TSL2561_NUMOF; i++) {
LOG_DEBUG("[auto_init_saul] initializing tsl2561 #%u\n", i);

if (tsl2561_init(&tsl2561_devs[i],
tsl2561_params[i].i2c_dev,
tsl2561_params[i].addr,
tsl2561_params[i].gain,
tsl2561_params[i].integration) != TSL2561_OK) {
&tsl2561_params[i]) != TSL2561_OK) {
LOG_ERROR("[auto_init_saul] error initializing tsl2561 #%u\n", i);
continue;
}

/* illuminance */
saul_entries[i].dev = &(tsl2561_devs[i]);
saul_entries[i].name = tsl2561_saul_reg_info[i].name;
saul_entries[i].name = tsl2561_saul_info[i].name;
saul_entries[i].driver = &tsl2561_illuminance_saul_driver;

/* register to saul */
Expand Down
14 changes: 0 additions & 14 deletions tests/driver_tsl2561/Makefile
Original file line number Diff line number Diff line change
@@ -1,20 +1,6 @@
include ../Makefile.tests_common

FEATURES_REQUIRED = periph_i2c

USEMODULE += tsl2561
USEMODULE += xtimer

# set default device parameters in case they are undefined
TEST_I2C ?= I2C_DEV\(0\)
TEST_ADDR ?= TSL2561_ADDR_FLOAT
TEST_GAIN ?= TSL2561_GAIN_1X
TEST_INTEGRATION_TIME ?= TSL2561_INTEGRATIONTIME_402MS

# export parameters
CFLAGS += -DTEST_I2C=$(TEST_I2C)
CFLAGS += -DTEST_ADDR=$(TEST_ADDR)
CFLAGS += -DTEST_GAIN=$(TEST_GAIN)
CFLAGS += -DTEST_INTEGRATION_TIME=$(TEST_INTEGRATION_TIME)

include $(RIOTBASE)/Makefile.include
23 changes: 4 additions & 19 deletions tests/driver_tsl2561/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -18,29 +18,15 @@
* @}
*/

#ifndef TEST_I2C
#error "TEST_I2C not defined"
#endif

#ifndef TEST_ADDR
#error "TEST_ADDR not defined"
#endif

#ifndef TEST_GAIN
#error "TEST_GAIN not defined"
#endif

#ifndef TEST_INTEGRATION_TIME
#error "TEST_INTEGRATION_TIME not defined"
#endif

#include <stdio.h>
#include <inttypes.h>

#include "tsl2561.h"
#include "xtimer.h"
#include "board.h"

#include "tsl2561.h"
#include "tsl2561_params.h"

#define SLEEP_1S (1 * 1000 * 1000u) /* 1 second delay between printf */

int main(void)
Expand All @@ -51,8 +37,7 @@ int main(void)

printf("+------------Initializing------------+\n");

switch(tsl2561_init(&dev, TEST_I2C, TEST_ADDR,
TEST_GAIN, TEST_INTEGRATION_TIME)) {
switch(tsl2561_init(&dev, &tsl2561_params[0])) {
case TSL2561_NOI2C:
puts("[Error] I2C not working: cannot initialize the sensor.\n");
break;
Expand Down

0 comments on commit 992da8d

Please sign in to comment.