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

drivers/tsl2561: rework API for consistency

This commit is contained in:
Alexandre Abadie 2018-02-28 16:24:31 +01:00 committed by dylad
parent d92b4c6759
commit 95a4d8d4e7
2 changed files with 38 additions and 38 deletions

View File

@ -65,35 +65,33 @@ extern "C" {
/** @} */ /** @} */
/** /**
* @brief Device descriptor for the TSL2561 sensor * @brief Device initialization parameters
*/ */
typedef struct { typedef struct {
i2c_t i2c_dev; /**< I2C device which is used */ i2c_t i2c_dev; /**< I2C device which is used */
uint8_t addr; /**< address on I2C bus */ uint8_t addr; /**< address on I2C bus */
uint8_t gain; /**< gain */ uint8_t gain; /**< gain */
uint8_t integration; /**< integration time */ 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 * @brief Initialize the given TSL2561 device
* *
* @param[out] dev Initialized device descriptor of BMP180 device * @param[out] dev Initialized device descriptor of BMP180 device
* @param[in] i2c I2C bus the sensor is connected to * @param[in] params Initialization parameters
* @param[in] addr I2C address of the sensor on the I2C bus
* @param[in] gain TSL2561 gain
* @param[in] integration TSL2561 integration time
* *
* @return 0 on success * @return 0 on success
* @return -1 if given I2C is not available * @return -1 if given I2C is not available
* @return -2 if not a TSL2561 sensor * @return -2 if not a TSL2561 sensor
*/ */
int tsl2561_init(tsl2561_t *dev, i2c_t i2c, uint8_t addr, int tsl2561_init(tsl2561_t *dev, const tsl2561_params_t *params);
uint8_t gain, uint8_t integration);
/** /**
* @brief Read illuminance value from the given TSL2561 device, returned in lx. * @brief Read illuminance value from the given TSL2561 device, returned in lx.

View File

@ -30,6 +30,11 @@
#define ENABLE_DEBUG (0) #define ENABLE_DEBUG (0)
#include "debug.h" #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 */ /* internal helpers */
static void _enable(const tsl2561_t *dev); static void _enable(const tsl2561_t *dev);
static void _disable(const tsl2561_t *dev); static void _disable(const tsl2561_t *dev);
@ -40,17 +45,14 @@ static void _print_init_info(const tsl2561_t *dev);
* TSL2561 Core API * * TSL2561 Core API *
*---------------------------------------------------------------------------*/ *---------------------------------------------------------------------------*/
int tsl2561_init(tsl2561_t *dev, int tsl2561_init(tsl2561_t *dev, const tsl2561_params_t *params)
i2c_t i2c, uint8_t addr, uint8_t gain, uint8_t integration)
{ {
dev->i2c_dev = i2c; dev->params = *params;
dev->addr = addr;
dev->gain = gain;
dev->integration = integration;
_print_init_info(dev); _print_init_info(dev);
/* Initialize I2C interface */ /* Initialize I2C interface */
if (i2c_init_master(dev->i2c_dev, I2C_SPEED_NORMAL)) { if (i2c_init_master(DEV_I2C, I2C_SPEED_NORMAL)) {
DEBUG("[Error] I2C device not enabled\n"); DEBUG("[Error] I2C device not enabled\n");
return TSL2561_NOI2C; return TSL2561_NOI2C;
} }
@ -58,13 +60,13 @@ int tsl2561_init(tsl2561_t *dev,
DEBUG("[Info] I2C device initialized with success!\n"); DEBUG("[Info] I2C device initialized with success!\n");
/* Acquire exclusive access */ /* Acquire exclusive access */
i2c_acquire(dev->i2c_dev); i2c_acquire(DEV_I2C);
DEBUG("[Info] Access acquired !\n"); DEBUG("[Info] Access acquired !\n");
/* Verify sensor ID */ /* Verify sensor ID */
uint8_t id; uint8_t id;
i2c_read_reg(dev->i2c_dev, dev->addr, i2c_read_reg(DEV_I2C, DEV_ADDR,
TSL2561_COMMAND_MODE | TSL2561_REGISTER_ID, &id); TSL2561_COMMAND_MODE | TSL2561_REGISTER_ID, &id);
DEBUG("[Info] ID ? %d\n", id); DEBUG("[Info] ID ? %d\n", id);
if (id != TSL2561_ID ) { if (id != TSL2561_ID ) {
@ -75,16 +77,16 @@ int tsl2561_init(tsl2561_t *dev,
_enable(dev); _enable(dev);
/* configuring gain and integration time */ /* 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, TSL2561_COMMAND_MODE | TSL2561_REGISTER_TIMING,
dev->integration | dev->gain); DEV_INTEGRATION | DEV_GAIN);
#if ENABLE_DEBUG #if ENABLE_DEBUG
uint8_t timing; uint8_t timing;
i2c_read_reg(dev->i2c_dev, dev->addr, i2c_read_reg(DEV_I2C, DEV_ADDR,
TSL2561_COMMAND_MODE | TSL2561_REGISTER_TIMING, &timing); TSL2561_COMMAND_MODE | TSL2561_REGISTER_TIMING, &timing);
DEBUG("[Info] Timing ? %d (expected: %d)\n", DEBUG("[Info] Timing ? %d (expected: %d)\n",
timing, dev->integration | dev->gain); timing, DEV_INTEGRATION | DEV_GAIN);
#endif #endif
_disable(dev); _disable(dev);
@ -107,7 +109,7 @@ uint16_t tsl2561_read_illuminance(const tsl2561_t *dev)
uint32_t channel_1; uint32_t channel_1;
uint32_t channel_0; uint32_t channel_0;
switch (dev->integration) { switch (DEV_INTEGRATION) {
case TSL2561_INTEGRATIONTIME_13MS: case TSL2561_INTEGRATIONTIME_13MS:
channel_scale = TSL2561_CHSCALE_TINT0; channel_scale = TSL2561_CHSCALE_TINT0;
break; break;
@ -122,7 +124,7 @@ uint16_t tsl2561_read_illuminance(const tsl2561_t *dev)
} }
/* Scale for gain (1x or 16x) */ /* Scale for gain (1x or 16x) */
if (!dev->gain) { if (!DEV_GAIN) {
channel_scale = channel_scale << 4; channel_scale = channel_scale << 4;
} }
@ -187,12 +189,12 @@ uint16_t tsl2561_read_illuminance(const tsl2561_t *dev)
static void _enable(const tsl2561_t *dev) static void _enable(const tsl2561_t *dev)
{ {
/* enabling device */ /* enabling device */
i2c_write_reg(dev->i2c_dev, dev->addr, i2c_write_reg(DEV_I2C, DEV_ADDR,
TSL2561_COMMAND_MODE | TSL2561_REGISTER_CONTROL, TSL2561_COMMAND_MODE | TSL2561_REGISTER_CONTROL,
TSL2561_CONTROL_POWERON); TSL2561_CONTROL_POWERON);
#if ENABLE_DEBUG #if ENABLE_DEBUG
uint8_t en; uint8_t en;
i2c_read_reg(dev->i2c_dev, dev->addr, i2c_read_reg(DEV_I2C, DEV_ADDR,
TSL2561_COMMAND_MODE | TSL2561_REGISTER_CONTROL, &en); TSL2561_COMMAND_MODE | TSL2561_REGISTER_CONTROL, &en);
DEBUG("[Info] Enabled ? %s\n", en == 3 ? "true" : "false"); DEBUG("[Info] Enabled ? %s\n", en == 3 ? "true" : "false");
#endif #endif
@ -202,13 +204,13 @@ static void _enable(const tsl2561_t *dev)
static void _disable(const tsl2561_t *dev) static void _disable(const tsl2561_t *dev)
{ {
/* disabling device */ /* disabling device */
i2c_write_reg(dev->i2c_dev, dev->addr, i2c_write_reg(DEV_I2C, DEV_ADDR,
TSL2561_COMMAND_MODE | TSL2561_REGISTER_CONTROL, TSL2561_COMMAND_MODE | TSL2561_REGISTER_CONTROL,
TSL2561_CONTROL_POWEROFF ); TSL2561_CONTROL_POWEROFF );
#if ENABLE_DEBUG #if ENABLE_DEBUG
uint8_t dis; uint8_t dis;
i2c_read_reg(dev->i2c_dev, dev->addr, i2c_read_reg(DEV_I2C, DEV_ADDR,
TSL2561_COMMAND_MODE | TSL2561_REGISTER_CONTROL, &dis); TSL2561_COMMAND_MODE | TSL2561_REGISTER_CONTROL, &dis);
DEBUG("[Info] Disabled ? %s\n", dis == 0 ? "true": "false"); DEBUG("[Info] Disabled ? %s\n", dis == 0 ? "true": "false");
#endif #endif
@ -220,7 +222,7 @@ static void _read_data(const tsl2561_t *dev, uint16_t *full, uint16_t *ir)
_enable(dev); _enable(dev);
/* Wait integration time in ms for ADC to complete */ /* Wait integration time in ms for ADC to complete */
switch (dev->integration) { switch (DEV_INTEGRATION) {
case TSL2561_INTEGRATIONTIME_13MS: case TSL2561_INTEGRATIONTIME_13MS:
xtimer_usleep(13700); xtimer_usleep(13700);
break; break;
@ -236,7 +238,7 @@ static void _read_data(const tsl2561_t *dev, uint16_t *full, uint16_t *ir)
char buffer[2] = { 0 }; char buffer[2] = { 0 };
/* Read full spectrum channel */ /* 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, TSL2561_COMMAND_MODE | TSL2561_COMMAND_WORD | TSL2561_REGISTER_CHAN0,
buffer, 2); buffer, 2);
*full = (buffer[1] << 8) | buffer[0]; *full = (buffer[1] << 8) | buffer[0];
@ -244,7 +246,7 @@ static void _read_data(const tsl2561_t *dev, uint16_t *full, uint16_t *ir)
memset(buffer, 0, sizeof(buffer)); memset(buffer, 0, sizeof(buffer));
/* Read infrared spectrum channel */ /* 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, TSL2561_COMMAND_MODE | TSL2561_COMMAND_WORD | TSL2561_REGISTER_CHAN1,
buffer, 2); buffer, 2);
*ir = (buffer[1] << 8) | buffer[0]; *ir = (buffer[1] << 8) | buffer[0];
@ -255,9 +257,9 @@ static void _read_data(const tsl2561_t *dev, uint16_t *full, uint16_t *ir)
static void _print_init_info(const tsl2561_t *dev) static void _print_init_info(const tsl2561_t *dev)
{ {
DEBUG("[Info] I2C device: %d\n", dev->i2c_dev); DEBUG("[Info] I2C device: %d\n", DEV_I2C);
DEBUG("[Info] Address: %d\n", dev->addr); DEBUG("[Info] Address: %d\n", DEV_ADDR);
switch(dev->gain) { switch(DEV_GAIN) {
case TSL2561_GAIN_1X: case TSL2561_GAIN_1X:
DEBUG("[Info] Gain: 1X\n"); DEBUG("[Info] Gain: 1X\n");
break; break;
@ -267,11 +269,11 @@ static void _print_init_info(const tsl2561_t *dev)
break; break;
default: default:
DEBUG("[Info] Invalid gain %d\n", dev->gain); DEBUG("[Info] Invalid gain %d\n", DEV_GAIN);
break; break;
} }
switch(dev->integration) { switch(DEV_INTEGRATION) {
case TSL2561_INTEGRATIONTIME_13MS: case TSL2561_INTEGRATIONTIME_13MS:
DEBUG("[Info] Integration time: 13ms\n"); DEBUG("[Info] Integration time: 13ms\n");
break; break;
@ -285,7 +287,7 @@ static void _print_init_info(const tsl2561_t *dev)
DEBUG("[Info] Integration time: n/a\n"); DEBUG("[Info] Integration time: n/a\n");
break; break;
default: default:
DEBUG("[Info] Invalid integration time %d\n", dev->integration); DEBUG("[Info] Invalid integration time %d\n", DEV_INTEGRATION);
break; break;
} }
} }