diff options
author | Bogdan Davidoaia <bogdan.m.davidoaia@intel.com> | 2016-05-09 12:01:04 +0300 |
---|---|---|
committer | Anas Nashif <nashif@linux.intel.com> | 2016-05-12 12:11:04 +0000 |
commit | 2064f4a9cc0a418737ae0aa5e9e8cd451293f2e5 (patch) | |
tree | 890f8753648d8564a3a296d6eff3fa457a2e42b7 | |
parent | 62c7a1338a042e757c57b44a7c9162a30c7772b0 (diff) |
grove: add temperature sensor
Add driver for the Grove Temperature Sensor which measures ambient
temperature.
Sensor reference page:
http://www.seeedstudio.com/wiki/Grove_-_Temperature_Sensor
Change-Id: I4f56224be5fa7a968749d5d4df8cb99a462c3c21
Origin: original
Signed-off-by: Bogdan Davidoaia <bogdan.m.davidoaia@intel.com>
-rw-r--r-- | drivers/grove/Kconfig | 67 | ||||
-rw-r--r-- | drivers/grove/Makefile | 1 | ||||
-rw-r--r-- | drivers/grove/temperature_sensor.c | 107 |
3 files changed, 175 insertions, 0 deletions
diff --git a/drivers/grove/Kconfig b/drivers/grove/Kconfig index 3a99865c1..ef76c4e79 100644 --- a/drivers/grove/Kconfig +++ b/drivers/grove/Kconfig @@ -111,3 +111,70 @@ config GROVE_LIGHT_SENSOR_INIT_PRIORITY Device driver initialization priority. As the device is connected to an ADC device, its driver has to be initialized after the ADC device. + +config GROVE_TEMPERATURE_SENSOR + bool + prompt "Enable the Seeed Grove Temperature Sensor" + depends on GROVE && ADC && NEWLIB_LIBC + default n + help + Setting this value will enable dirver support for the Grove + Temperature Sensor. + +config GROVE_TEMPERATURE_SENSOR_NAME + string + prompt "Driver name" + depends on GROVE_TEMPERATURE_SENSOR + default "GROVE_TEMPERATURE_SENSOR" + help + Specify the device name with which the Grove Temperature Sensor + is identified. + +choice + prompt "Sensor version" + depends on GROVE_TEMPERATURE_SENSOR + default GROVE_TEMPERATURE_SENSOR_V1_0 + help + Choose the version of the Grove Temperature Sensor. + +config GROVE_TEMPERATURE_SENSOR_V1_0 + bool + prompt "v1.0" + help + Version 1.0 + +config GROVE_TEMPERATURE_SENSOR_V1_X + bool + prompt "v1.1/v1.2" + help + Version 1.1 or 1.2 + +endchoice + +config GROVE_TEMPERATURE_SENSOR_ADC_DEV_NAME + string + prompt "ADC where Grove Temperature Sensor is connected" + depends on GROVE_TEMPERATURE_SENSOR + default "ADC_0" + help + Specify the device name of the ADC to which the Grove Temperature + Sensor is connected. + +config GROVE_TEMPERATURE_SENSOR_ADC_CHANNEL + int + prompt "ADC channel used by Grove Temperature Sensor" + depends on GROVE_TEMPERATURE_SENSOR + default 10 + help + Specify the channel of the ADC to which the Grove Temperature Sensor + is connected. + +config GROVE_TEMPERATURE_SENSOR_INIT_PRIORITY + int + prompt "Init priority" + default 70 + depends on GROVE_TEMPERATURE_SENSOR + help + Device driver initialization priority. + As the device is connected to an ADC device, its driver has to be + initialized after the ADC device. diff --git a/drivers/grove/Makefile b/drivers/grove/Makefile index cafde1859..3b42ceca5 100644 --- a/drivers/grove/Makefile +++ b/drivers/grove/Makefile @@ -1,2 +1,3 @@ obj-$(CONFIG_GROVE_LCD_RGB) += lcd_rgb.o obj-$(CONFIG_GROVE_LIGHT_SENSOR) += light_sensor.o +obj-$(CONFIG_GROVE_TEMPERATURE_SENSOR) += temperature_sensor.o diff --git a/drivers/grove/temperature_sensor.c b/drivers/grove/temperature_sensor.c new file mode 100644 index 000000000..16d833ff5 --- /dev/null +++ b/drivers/grove/temperature_sensor.c @@ -0,0 +1,107 @@ +/* + * Copyright (c) 2016 Intel Corporation + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include <adc.h> +#include <device.h> +#include <math.h> +#include <sensor.h> +#include <zephyr.h> + +#define SYS_LOG_DOMAIN "GROVE_TEMPERATURE_SENSOR" +#define SYS_LOG_LEVEL CONFIG_GROVE_SYS_LOG_LEVEL +#include <misc/sys_log.h> + +/* 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; + uint8_t adc_buffer[4]; +}; + +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; + uint16_t analog_val; + + /* rescale sample from 12bit (Zephyr) to 10bit (Grove) */ + analog_val = ((uint16_t)drv_data->adc_buffer[1] << 8) | + 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 + */ + val->type = SENSOR_VALUE_TYPE_DOUBLE; + val->dval = 1 / (log(1023.0 / analog_val - 1.0) / B_CONST + + 1 / 298.15) - 273.15; + + return 0; +} + +static struct sensor_driver_api gts_api = { + .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; +} + +struct gts_data gts_data; + +DEVICE_INIT(gts_dev, CONFIG_GROVE_TEMPERATURE_SENSOR_NAME, >s_init, >s_data, + NULL, SECONDARY, CONFIG_GROVE_TEMPERATURE_SENSOR_INIT_PRIORITY); |