2016-05-09 17:01:04 +08:00
|
|
|
/*
|
|
|
|
* Copyright (c) 2016 Intel Corporation
|
|
|
|
*
|
2017-01-19 09:01:01 +08:00
|
|
|
* SPDX-License-Identifier: Apache-2.0
|
2016-05-09 17:01:04 +08:00
|
|
|
*/
|
|
|
|
|
|
|
|
#include <adc.h>
|
|
|
|
#include <device.h>
|
|
|
|
#include <math.h>
|
|
|
|
#include <sensor.h>
|
|
|
|
#include <zephyr.h>
|
|
|
|
|
|
|
|
#define SYS_LOG_DOMAIN "GROVE_TEMPERATURE_SENSOR"
|
2016-10-08 05:38:37 +08:00
|
|
|
#define SYS_LOG_LEVEL CONFIG_SYS_LOG_GROVE_LEVEL
|
2016-12-18 01:56:56 +08:00
|
|
|
#include <logging/sys_log.h>
|
2016-05-09 17:01:04 +08:00
|
|
|
|
|
|
|
/* thermistor Nominal B-Constant */
|
|
|
|
#if defined(CONFIG_GROVE_TEMPERATURE_SENSOR_V1_0)
|
|
|
|
#define B_CONST 3975
|
|
|
|
#elif defined(CONFIG_GROVE_TEMPERATURE_SENSOR_V1_X)
|
|
|
|
#define B_CONST 4250
|
|
|
|
#endif
|
|
|
|
|
|
|
|
struct gts_data {
|
|
|
|
struct device *adc;
|
|
|
|
struct adc_seq_entry sample;
|
|
|
|
struct adc_seq_table adc_table;
|
2017-04-21 23:03:20 +08:00
|
|
|
u8_t adc_buffer[4];
|
2016-05-09 17:01:04 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
static int gts_sample_fetch(struct device *dev, enum sensor_channel chan)
|
|
|
|
{
|
|
|
|
struct gts_data *drv_data = dev->driver_data;
|
|
|
|
|
|
|
|
return adc_read(drv_data->adc, &drv_data->adc_table);
|
|
|
|
}
|
|
|
|
|
|
|
|
static int gts_channel_get(struct device *dev,
|
|
|
|
enum sensor_channel chan,
|
|
|
|
struct sensor_value *val)
|
|
|
|
{
|
|
|
|
struct gts_data *drv_data = dev->driver_data;
|
2017-04-21 23:03:20 +08:00
|
|
|
u16_t analog_val;
|
2016-12-09 18:57:20 +08:00
|
|
|
double dval;
|
2016-05-09 17:01:04 +08:00
|
|
|
|
|
|
|
/* rescale sample from 12bit (Zephyr) to 10bit (Grove) */
|
2017-04-21 23:03:20 +08:00
|
|
|
analog_val = ((u16_t)drv_data->adc_buffer[1] << 8) |
|
2016-05-09 17:01:04 +08:00
|
|
|
drv_data->adc_buffer[0];
|
|
|
|
analog_val = analog_val >> 2;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* The formula for converting the analog value to degrees Celisus
|
|
|
|
* is taken from the sensor reference page:
|
|
|
|
* http://www.seeedstudio.com/wiki/Grove_-_Temperature_Sensor
|
|
|
|
*/
|
2016-12-09 18:57:20 +08:00
|
|
|
dval = 1 / (log(1023.0 / analog_val - 1.0) / B_CONST +
|
|
|
|
1 / 298.15) - 273.15;
|
|
|
|
|
2017-04-21 23:03:20 +08:00
|
|
|
val->val1 = (s32_t)dval;
|
|
|
|
val->val2 = ((s32_t)(dval * 1000000)) % 1000000;
|
2016-05-09 17:01:04 +08:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2016-10-24 15:24:37 +08:00
|
|
|
static const struct sensor_driver_api gts_api = {
|
2016-05-09 17:01:04 +08:00
|
|
|
.sample_fetch = >s_sample_fetch,
|
|
|
|
.channel_get = >s_channel_get,
|
|
|
|
};
|
|
|
|
|
|
|
|
static int gts_init(struct device *dev)
|
|
|
|
{
|
|
|
|
struct gts_data *drv_data = dev->driver_data;
|
|
|
|
|
|
|
|
drv_data->adc = device_get_binding(
|
|
|
|
CONFIG_GROVE_TEMPERATURE_SENSOR_ADC_DEV_NAME);
|
|
|
|
if (drv_data->adc == NULL) {
|
|
|
|
SYS_LOG_ERR("Failed to get ADC device.");
|
|
|
|
return -EINVAL;
|
|
|
|
}
|
|
|
|
|
|
|
|
drv_data->sample.sampling_delay = 12;
|
|
|
|
drv_data->sample.channel_id =
|
|
|
|
CONFIG_GROVE_TEMPERATURE_SENSOR_ADC_CHANNEL;
|
|
|
|
drv_data->sample.buffer = drv_data->adc_buffer;
|
|
|
|
drv_data->sample.buffer_length = 4;
|
|
|
|
|
|
|
|
drv_data->adc_table.entries = &drv_data->sample;
|
|
|
|
drv_data->adc_table.num_entries = 1;
|
|
|
|
|
|
|
|
adc_enable(drv_data->adc);
|
|
|
|
|
|
|
|
dev->driver_api = >s_api;
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2016-10-26 12:53:08 +08:00
|
|
|
static struct gts_data gts_data;
|
2016-05-09 17:01:04 +08:00
|
|
|
|
|
|
|
DEVICE_INIT(gts_dev, CONFIG_GROVE_TEMPERATURE_SENSOR_NAME, >s_init, >s_data,
|
2016-11-09 03:06:55 +08:00
|
|
|
NULL, POST_KERNEL, CONFIG_SENSOR_INIT_PRIORITY);
|