Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: drivers: tcs3430: add ATIME and AGAIN configs #24

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 26 additions & 4 deletions drivers/sensor/tcs3430/tcs3430.c
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@ LOG_MODULE_REGISTER(tcs3430, CONFIG_SENSOR_LOG_LEVEL);

#define TCS3430_CONTROL_REG 0x8F

#define TCS3430_CFG1_REG 0x90

#define TCS3430_ID_REG 0x92
#define TCS3430_ID 0xDC

Expand Down Expand Up @@ -62,6 +64,8 @@ LOG_MODULE_REGISTER(tcs3430, CONFIG_SENSOR_LOG_LEVEL);
struct tcs3430_config {
struct i2c_dt_spec i2c;
struct gpio_dt_spec int_gpio;
uint16_t integration_cycles;
uint8_t gain;
};

struct tcs3430_data {
Expand Down Expand Up @@ -187,17 +191,27 @@ static int tcs3430_attr_set(const struct device *dev, enum sensor_channel chan,
int ret;
uint8_t reg_val;

switch (attr) {
switch ((enum sensor_attribute_tcs3430)attr) {
case SENSOR_ATTR_TCS3430_INTEGRATION_CYCLES:
if (!IN_RANGE(val->val1, 1, 256)) {
return -EINVAL;
}
reg_val = UINT8_MAX - val->val1 + 1;
reg_val = (uint8_t)(val->val1-1);
ret = i2c_reg_write_byte_dt(&cfg->i2c, TCS3430_ATIME_REG, reg_val);
if (ret) {
return ret;
}
break;
case SENSOR_ATTR_TCS3430_GAIN:
if (!IN_RANGE(val->val1, 0, 3)) {
return -EINVAL;
}
reg_val = (uint8_t)val->val1;
ret = i2c_reg_write_byte_dt(&cfg->i2c, TCS3430_CFG1_REG, reg_val);
if (ret) {
return ret;
}
break;
default:
return -ENOTSUP;
}
Expand All @@ -210,17 +224,23 @@ static int tcs3430_sensor_setup(const struct device *dev)
const struct tcs3430_config *cfg = dev->config;
uint8_t chip_id;
int ret;
if (!IN_RANGE(cfg->integration_cycles, 1, 256)) {
LOG_ERR("Invalid integration cycles: %d", cfg->integration_cycles);
return -EINVAL;
}
uint8_t atime_default = (uint8_t)(cfg->integration_cycles-1);
struct {
uint8_t reg_addr;
uint8_t value;
} reset_regs[] = {
{TCS3430_ENABLE_REG, TCS3430_DEFAULT_ENABLE},
{TCS3430_AICLEAR_REG, TCS3430_AICLEAR_RESET},
{TCS3430_ATIME_REG, TCS3430_DEFAULT_ATIME},
{TCS3430_ATIME_REG, atime_default},
{TCS3430_PERS_REG, TCS3430_DEFAULT_PERS},
{TCS3430_CONFIG_REG, TCS3430_DEFAULT_CONFIG},
{TCS3430_CONTROL_REG, TCS3430_DEFAULT_CONTROL},
{TCS3430_INTENAB_REG, TCS3430_DEFAULT_INTENAB},
{TCS3430_CFG1_REG, cfg->gain},
{TCS3430_CFG2_REG, TCS3430_DEFAULT_CFG2},
};

Expand Down Expand Up @@ -270,7 +290,7 @@ static int tcs3430_init(const struct device *dev)

ret = tcs3430_sensor_setup(dev);
if (ret < 0) {
LOG_ERR("Failed to setup device");
LOG_ERR("Failed to setup device: %d", ret);
return ret;
}

Expand Down Expand Up @@ -301,6 +321,8 @@ static int tcs3430_init(const struct device *dev)
static const struct tcs3430_config tcs3430_config_##n = { \
.i2c = I2C_DT_SPEC_INST_GET(n), \
.int_gpio = GPIO_DT_SPEC_INST_GET(n, int_gpios), \
.integration_cycles = DT_INST_PROP(n, integration_cycles), \
.gain = DT_INST_PROP(n, gain), \
}; \
SENSOR_DEVICE_DT_INST_DEFINE(n, &tcs3430_init, NULL, &tcs3430_data_##n, \
&tcs3430_config_##n, POST_KERNEL, \
Expand Down
15 changes: 15 additions & 0 deletions dts/bindings/sensor/ams,tcs3430.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,18 @@ properties:
required: true
description: |
INT pin GPIO identifier, open-drain, active low.
integration-cycles:
type: int
default: 256
description: |
Initial ADC integration cycle setting from 1 to 256.
1 cycle = 2.78ms.
gain:
type: int
default: 0
description: |
ALS Gain Control:
0 - 1x
1 - 4x
2 - 16x
3 - 64x
2 changes: 2 additions & 0 deletions include/zephyr/drivers/sensor/tcs3430.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@
enum sensor_attribute_tcs3430 {
/** XYZB Integration Cycles */
SENSOR_ATTR_TCS3430_INTEGRATION_CYCLES = SENSOR_ATTR_PRIV_START,
/** Gain setting */
SENSOR_ATTR_TCS3430_GAIN,
};

#endif /* ZEPHYR_INCLUDE_DRIVERS_SENSOR_TCS3430_H_ */
Loading