I am trying to create a custom sensor for CT1711 temperature sensor. I have created the sensor driver code and device tree binding, but it won't start (initialize) and the device is not ready. Do I need to add something to the sensor driver, or is there something missing?
drivers/sensor/ct1711/ct1711.c
#define DT_DRV_COMPAT zephyr_ct1711
#include <zephyr/kernel.h>
#include <zephyr/device.h>
#include <zephyr/drivers/gpio.h>
#include <zephyr/drivers/sensor.h>
#include <zephyr/logging/log.h>
LOG_MODULE_REGISTER(ct1711, CONFIG_SENSOR_LOG_LEVEL);
...
static int ct1711_sample_fetch(const struct device *dev, enum sensor_channel chan)
{
if (chan != CT1711_SENSOR_CHANNELS)
{
LOG_ERR("Supported channel is SENSOR_CHAN_AMBIENT_TEMP (%d), but %d was requested", CT1711_SENSOR_CHANNELS, chan);
return -ENOTSUP;
}
struct ct1711_data *data = dev->data;
data->temperature = ct1711_read_temperature(dev);
if (data->temperature < CT1711_LOWER_THRESHOLD || data->temperature > CT1711_UPPER_THRESHOLD)
{
LOG_ERR("Invalid temperature reading: %f", (double)data->temperature);
return -EIO;
}
LOG_DBG("Temperature fetched: %f", (double)data->temperature);
return 0;
}
static int ct1711_channel_get(const struct device *dev, enum sensor_channel chan, struct sensor_value *val)
{
if (chan != CT1711_SENSOR_CHANNELS)
{
LOG_ERR("Supported channel is SENSOR_CHAN_AMBIENT_TEMP (%d), but %d was requested", CT1711_SENSOR_CHANNELS, chan);
return -ENOTSUP;
}
struct ct1711_data *data = dev->data;
val->val1 = (int32_t)data->temperature;
val->val2 = (int32_t)((data->temperature - val->val1) * 1000000);
LOG_DBG("Temperature value: %d.%06d", val->val1, val->val2);
return 0;
}
static const struct sensor_driver_api ct1711_driver_api = {
.sample_fetch = ct1711_sample_fetch,
.channel_get = ct1711_channel_get,
};
static int ct1711_init(const struct device *dev)
{
LOG_DBG("Initializing CT1711 sensor");
const struct ct1711_config *config = dev->config;
if (!device_is_ready(config->dio.port))
{
LOG_ERR("GPIO device %s is not ready", config->dio.port->name);
return -ENODEV;
}
int ret;
ret = gpio_pin_configure_dt(&config->dio, GPIO_INPUT);
if (ret < 0)
{
LOG_ERR("Failed to configure GPIO pin %d as input: %d", config->dio.pin, ret);
return ret;
}
return 0;
}
#define CT1711_DEFINE(inst) \
static struct ct1711_data ct1711_data_##inst; \
static const struct ct1711_config ct1711_config_##inst = { \
.dio = GPIO_DT_SPEC_INST_GET(inst, dio_gpios), \
}; \
SENSOR_DEVICE_DT_INST_DEFINE(inst, ct1711_init, NULL, \
&ct1711_data_##inst, &ct1711_config_##inst, \
POST_KERNEL, CONFIG_SENSOR_INIT_PRIORITY, \
&ct1711_driver_api);
DT_INST_FOREACH_STATUS_OKAY(CT1711_DEFINE)
dts/bindings/sensor/zephyr,ct1711.yaml
description: CT1711 temperature sensor
compatible: "zephyr,ct1711"
include: sensor-device.yaml
properties:
dio-gpios:
type: phandle-array
required: true
description: DIO GPIOs for the CT1711 sensor.
boards/nr52832.overlay
/ {
ct1711: ct1711 {
compatible = "zephyr,ct1711";
dio-gpios = <&gpio0 25 GPIO_ACTIVE_HIGH>;
status = "okay";
};
};
I tried using `device_is_ready()` and it returned an error with the device name "V".