2019-02-12 10:15:26 +08:00
|
|
|
/*
|
2021-03-08 18:19:04 +08:00
|
|
|
* Copyright (c) 2006-2021, RT-Thread Development Team
|
2019-02-12 10:15:26 +08:00
|
|
|
*
|
|
|
|
* SPDX-License-Identifier: Apache-2.0
|
|
|
|
*
|
|
|
|
* Change Logs:
|
|
|
|
* Date Author Notes
|
|
|
|
* 2019-01-31 flybreak first version
|
2020-02-22 22:43:14 +08:00
|
|
|
* 2020-02-22 luhuadong support custom commands
|
2022-12-22 10:24:51 +08:00
|
|
|
* 2022-12-17 Meco Man re-implement sensor framework
|
2019-02-12 10:15:26 +08:00
|
|
|
*/
|
|
|
|
|
2022-11-07 09:06:21 +08:00
|
|
|
#include <drivers/sensor.h>
|
2019-02-12 10:15:26 +08:00
|
|
|
|
2019-04-12 10:18:57 +08:00
|
|
|
#define DBG_TAG "sensor"
|
|
|
|
#define DBG_LVL DBG_INFO
|
2019-02-12 10:15:26 +08:00
|
|
|
#include <rtdbg.h>
|
|
|
|
|
|
|
|
#include <string.h>
|
|
|
|
|
|
|
|
static char *const sensor_name_str[] =
|
|
|
|
{
|
2022-11-09 10:38:38 +08:00
|
|
|
"None",
|
|
|
|
"ac-", /* Accelerometer */
|
|
|
|
"gy-", /* Gyroscope */
|
|
|
|
"ma-", /* Magnetometer */
|
|
|
|
"tm-", /* Temperature */
|
|
|
|
"hm-", /* Relative Humidity */
|
|
|
|
"br-", /* Barometer */
|
|
|
|
"li-", /* Ambient light */
|
|
|
|
"pr-", /* Proximity */
|
|
|
|
"hr-", /* Heart Rate */
|
|
|
|
"tv-", /* TVOC Level */
|
|
|
|
"ni-", /* Noise Loudness */
|
|
|
|
"st-", /* Step sensor */
|
|
|
|
"fr-", /* Force sensor */
|
|
|
|
"du-", /* Dust sensor */
|
|
|
|
"ec-", /* eCO2 sensor */
|
|
|
|
"gn-", /* GPS/GNSS sensor */
|
|
|
|
"tf-", /* TOF sensor */
|
|
|
|
"sp-", /* SpO2 sensor */
|
|
|
|
"ia-", /* IAQ sensor */
|
|
|
|
"et-", /* EtOH sensor */
|
|
|
|
"bp-" /* Blood Pressure */
|
2019-02-12 10:15:26 +08:00
|
|
|
};
|
|
|
|
|
2022-11-07 13:33:49 +08:00
|
|
|
/* sensor interrupt handler function */
|
|
|
|
static void _sensor_cb(rt_sensor_t sen)
|
2019-02-12 10:15:26 +08:00
|
|
|
{
|
|
|
|
if (sen->parent.rx_indicate == RT_NULL)
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
2019-06-13 15:10:02 +08:00
|
|
|
|
2019-03-08 10:32:36 +08:00
|
|
|
if (sen->irq_handle != RT_NULL)
|
|
|
|
{
|
|
|
|
sen->irq_handle(sen);
|
|
|
|
}
|
2019-02-12 10:15:26 +08:00
|
|
|
|
|
|
|
/* The buffer is not empty. Read the data in the buffer first */
|
|
|
|
if (sen->data_len > 0)
|
|
|
|
{
|
|
|
|
sen->parent.rx_indicate(&sen->parent, sen->data_len / sizeof(struct rt_sensor_data));
|
|
|
|
}
|
2022-12-22 10:24:51 +08:00
|
|
|
else if (RT_SENSOR_MODE_GET_FETCH(sen->info.mode) == RT_SENSOR_MODE_FETCH_INT)
|
2019-02-12 10:15:26 +08:00
|
|
|
{
|
|
|
|
/* The interrupt mode only produces one data at a time */
|
|
|
|
sen->parent.rx_indicate(&sen->parent, 1);
|
|
|
|
}
|
2022-12-22 10:24:51 +08:00
|
|
|
else if (RT_SENSOR_MODE_GET_FETCH(sen->info.mode) == RT_SENSOR_MODE_FETCH_FIFO)
|
2019-02-12 10:15:26 +08:00
|
|
|
{
|
|
|
|
sen->parent.rx_indicate(&sen->parent, sen->info.fifo_max);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* ISR for sensor interrupt */
|
2022-11-07 13:33:49 +08:00
|
|
|
static void _irq_callback(void *args)
|
2019-02-12 10:15:26 +08:00
|
|
|
{
|
2019-06-18 20:09:19 +08:00
|
|
|
rt_sensor_t sensor = (rt_sensor_t)args;
|
2019-02-12 10:15:26 +08:00
|
|
|
rt_uint8_t i;
|
|
|
|
|
|
|
|
if (sensor->module)
|
|
|
|
{
|
|
|
|
/* Invoke a callback for all sensors in the module */
|
|
|
|
for (i = 0; i < sensor->module->sen_num; i++)
|
|
|
|
{
|
2022-11-07 13:33:49 +08:00
|
|
|
_sensor_cb(sensor->module->sen[i]);
|
2019-02-12 10:15:26 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2022-11-07 13:33:49 +08:00
|
|
|
_sensor_cb(sensor);
|
2019-02-12 10:15:26 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Sensor interrupt initialization function */
|
2022-11-07 13:33:49 +08:00
|
|
|
static rt_err_t _sensor_irq_init(rt_sensor_t sensor)
|
2019-02-12 10:15:26 +08:00
|
|
|
{
|
|
|
|
if (sensor->config.irq_pin.pin == RT_PIN_NONE)
|
|
|
|
{
|
|
|
|
return -RT_EINVAL;
|
|
|
|
}
|
|
|
|
|
|
|
|
rt_pin_mode(sensor->config.irq_pin.pin, sensor->config.irq_pin.mode);
|
|
|
|
|
|
|
|
if (sensor->config.irq_pin.mode == PIN_MODE_INPUT_PULLDOWN)
|
|
|
|
{
|
2022-11-07 13:33:49 +08:00
|
|
|
rt_pin_attach_irq(sensor->config.irq_pin.pin, PIN_IRQ_MODE_RISING, _irq_callback, (void *)sensor);
|
2019-02-12 10:15:26 +08:00
|
|
|
}
|
|
|
|
else if (sensor->config.irq_pin.mode == PIN_MODE_INPUT_PULLUP)
|
|
|
|
{
|
2022-11-07 13:33:49 +08:00
|
|
|
rt_pin_attach_irq(sensor->config.irq_pin.pin, PIN_IRQ_MODE_FALLING, _irq_callback, (void *)sensor);
|
2019-02-12 10:15:26 +08:00
|
|
|
}
|
|
|
|
else if (sensor->config.irq_pin.mode == PIN_MODE_INPUT)
|
|
|
|
{
|
2022-11-07 13:33:49 +08:00
|
|
|
rt_pin_attach_irq(sensor->config.irq_pin.pin, PIN_IRQ_MODE_RISING_FALLING, _irq_callback, (void *)sensor);
|
2019-02-12 10:15:26 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
rt_pin_irq_enable(sensor->config.irq_pin.pin, RT_TRUE);
|
|
|
|
|
|
|
|
LOG_I("interrupt init success");
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2022-11-07 13:33:49 +08:00
|
|
|
/* sensor local ops */
|
2022-11-09 11:50:27 +08:00
|
|
|
static rt_ssize_t _local_fetch_data(rt_sensor_t sensor, rt_sensor_data_t buf, rt_size_t len)
|
2021-03-17 13:40:44 +08:00
|
|
|
{
|
|
|
|
LOG_D("Undefined fetch_data");
|
2022-11-09 08:08:31 +08:00
|
|
|
return -RT_EINVAL;
|
2021-03-17 13:40:44 +08:00
|
|
|
}
|
2022-11-07 13:33:49 +08:00
|
|
|
static rt_err_t _local_control(rt_sensor_t sensor, int cmd, void *arg)
|
2021-03-17 13:40:44 +08:00
|
|
|
{
|
|
|
|
LOG_D("Undefined control");
|
2022-11-09 08:08:31 +08:00
|
|
|
return -RT_EINVAL;
|
2021-03-17 13:40:44 +08:00
|
|
|
}
|
2021-11-04 20:31:55 +08:00
|
|
|
static struct rt_sensor_ops local_ops =
|
|
|
|
{
|
2022-11-07 13:33:49 +08:00
|
|
|
.fetch_data = _local_fetch_data,
|
|
|
|
.control = _local_control
|
2021-03-17 13:40:44 +08:00
|
|
|
};
|
|
|
|
|
2019-02-12 10:15:26 +08:00
|
|
|
/* RT-Thread Device Interface */
|
2022-11-07 13:33:49 +08:00
|
|
|
static rt_err_t _sensor_open(rt_device_t dev, rt_uint16_t oflag)
|
2019-02-12 10:15:26 +08:00
|
|
|
{
|
|
|
|
rt_sensor_t sensor = (rt_sensor_t)dev;
|
|
|
|
RT_ASSERT(dev != RT_NULL);
|
2019-06-13 16:01:20 +08:00
|
|
|
rt_err_t res = RT_EOK;
|
2022-11-07 13:33:49 +08:00
|
|
|
rt_err_t (*local_ctrl)(rt_sensor_t sensor, int cmd, void *arg) = _local_control;
|
2019-02-12 10:15:26 +08:00
|
|
|
|
2019-06-13 15:10:02 +08:00
|
|
|
if (sensor->module)
|
|
|
|
{
|
|
|
|
/* take the module mutex */
|
|
|
|
rt_mutex_take(sensor->module->lock, RT_WAITING_FOREVER);
|
|
|
|
}
|
|
|
|
|
2019-02-12 10:15:26 +08:00
|
|
|
if (sensor->module != RT_NULL && sensor->info.fifo_max > 0 && sensor->data_buf == RT_NULL)
|
|
|
|
{
|
|
|
|
/* Allocate memory for the sensor buffer */
|
|
|
|
sensor->data_buf = rt_malloc(sizeof(struct rt_sensor_data) * sensor->info.fifo_max);
|
|
|
|
if (sensor->data_buf == RT_NULL)
|
|
|
|
{
|
2019-06-13 16:01:20 +08:00
|
|
|
res = -RT_ENOMEM;
|
|
|
|
goto __exit;
|
2019-02-12 10:15:26 +08:00
|
|
|
}
|
|
|
|
}
|
2021-03-17 13:40:44 +08:00
|
|
|
if (sensor->ops->control != RT_NULL)
|
|
|
|
{
|
|
|
|
local_ctrl = sensor->ops->control;
|
|
|
|
}
|
2019-02-12 10:15:26 +08:00
|
|
|
|
|
|
|
if (oflag & RT_DEVICE_FLAG_RDONLY && dev->flag & RT_DEVICE_FLAG_RDONLY)
|
|
|
|
{
|
2021-03-17 13:40:44 +08:00
|
|
|
/* If polling mode is supported, configure it to polling mode */
|
2022-12-22 10:24:51 +08:00
|
|
|
if (local_ctrl(sensor, RT_SENSOR_CTRL_SET_FETCH_MODE, (void *)RT_SENSOR_MODE_FETCH_POLLING) == RT_EOK)
|
|
|
|
{
|
|
|
|
RT_SENSOR_MODE_SET_FETCH(sensor->info.mode, RT_SENSOR_MODE_FETCH_POLLING);
|
|
|
|
}
|
2019-02-12 10:15:26 +08:00
|
|
|
}
|
|
|
|
else if (oflag & RT_DEVICE_FLAG_INT_RX && dev->flag & RT_DEVICE_FLAG_INT_RX)
|
|
|
|
{
|
2021-03-17 13:40:44 +08:00
|
|
|
/* If interrupt mode is supported, configure it to interrupt mode */
|
2022-12-22 10:24:51 +08:00
|
|
|
if (local_ctrl(sensor, RT_SENSOR_CTRL_SET_FETCH_MODE, (void *)RT_SENSOR_MODE_FETCH_INT) == RT_EOK)
|
2019-02-12 10:15:26 +08:00
|
|
|
{
|
2021-03-17 13:40:44 +08:00
|
|
|
/* Initialization sensor interrupt */
|
2022-11-07 13:33:49 +08:00
|
|
|
_sensor_irq_init(sensor);
|
2022-12-22 10:24:51 +08:00
|
|
|
RT_SENSOR_MODE_SET_FETCH(sensor->info.mode, RT_SENSOR_MODE_FETCH_INT);
|
2019-02-12 10:15:26 +08:00
|
|
|
}
|
|
|
|
}
|
2019-02-15 10:39:48 +08:00
|
|
|
else if (oflag & RT_DEVICE_FLAG_FIFO_RX && dev->flag & RT_DEVICE_FLAG_FIFO_RX)
|
2019-02-12 10:15:26 +08:00
|
|
|
{
|
2021-03-17 13:40:44 +08:00
|
|
|
/* If fifo mode is supported, configure it to fifo mode */
|
2022-12-22 10:24:51 +08:00
|
|
|
if (local_ctrl(sensor, RT_SENSOR_CTRL_SET_FETCH_MODE, (void *)RT_SENSOR_MODE_FETCH_FIFO) == RT_EOK)
|
2019-02-12 10:15:26 +08:00
|
|
|
{
|
2021-03-17 13:40:44 +08:00
|
|
|
/* Initialization sensor interrupt */
|
2022-11-07 13:33:49 +08:00
|
|
|
_sensor_irq_init(sensor);
|
2022-12-22 10:24:51 +08:00
|
|
|
RT_SENSOR_MODE_SET_FETCH(sensor->info.mode, RT_SENSOR_MODE_FETCH_FIFO);
|
2019-02-12 10:15:26 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2019-06-13 16:01:20 +08:00
|
|
|
res = -RT_EINVAL;
|
|
|
|
goto __exit;
|
2019-02-12 10:15:26 +08:00
|
|
|
}
|
|
|
|
|
2022-12-22 10:24:51 +08:00
|
|
|
/* Configure power mode to highest mode */
|
|
|
|
if (local_ctrl(sensor, RT_SENSOR_CTRL_SET_POWER_MODE, (void *)RT_SENSOR_MODE_POWER_HIGHEST) == RT_EOK)
|
2019-02-12 10:15:26 +08:00
|
|
|
{
|
2022-12-22 10:24:51 +08:00
|
|
|
RT_SENSOR_MODE_SET_POWER(sensor->info.mode, RT_SENSOR_MODE_POWER_HIGHEST);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Configure accuracy mode to highest mode */
|
|
|
|
if (local_ctrl(sensor, RT_SENSOR_CTRL_SET_ACCURACY_MODE, (void *)RT_SENSOR_MODE_ACCURACY_HIGHEST) == RT_EOK)
|
|
|
|
{
|
|
|
|
RT_SENSOR_MODE_SET_ACCURACY(sensor->info.mode, RT_SENSOR_MODE_ACCURACY_HIGHEST);
|
2019-02-12 10:15:26 +08:00
|
|
|
}
|
|
|
|
|
2019-06-13 16:01:20 +08:00
|
|
|
__exit:
|
2019-02-12 10:15:26 +08:00
|
|
|
if (sensor->module)
|
|
|
|
{
|
|
|
|
/* release the module mutex */
|
|
|
|
rt_mutex_release(sensor->module->lock);
|
|
|
|
}
|
|
|
|
|
2019-06-13 16:01:20 +08:00
|
|
|
return res;
|
2019-02-12 10:15:26 +08:00
|
|
|
}
|
|
|
|
|
2022-11-07 13:33:49 +08:00
|
|
|
static rt_err_t _sensor_close(rt_device_t dev)
|
2019-02-12 10:15:26 +08:00
|
|
|
{
|
|
|
|
rt_sensor_t sensor = (rt_sensor_t)dev;
|
2019-06-13 15:10:02 +08:00
|
|
|
int i;
|
2022-11-07 13:33:49 +08:00
|
|
|
rt_err_t (*local_ctrl)(rt_sensor_t sensor, int cmd, void *arg) = _local_control;
|
2019-06-13 15:10:02 +08:00
|
|
|
|
2019-02-12 10:15:26 +08:00
|
|
|
RT_ASSERT(dev != RT_NULL);
|
|
|
|
|
|
|
|
if (sensor->module)
|
|
|
|
{
|
|
|
|
rt_mutex_take(sensor->module->lock, RT_WAITING_FOREVER);
|
|
|
|
}
|
2021-03-17 13:40:44 +08:00
|
|
|
if (sensor->ops->control != RT_NULL)
|
2021-03-16 01:15:58 +08:00
|
|
|
{
|
2021-03-17 13:40:44 +08:00
|
|
|
local_ctrl = sensor->ops->control;
|
2021-03-16 01:15:58 +08:00
|
|
|
}
|
2021-03-17 13:40:44 +08:00
|
|
|
|
|
|
|
/* Configure power mode to power down mode */
|
2022-12-22 10:24:51 +08:00
|
|
|
if (local_ctrl(sensor, RT_SENSOR_CTRL_SET_POWER_MODE, (void *)RT_SENSOR_MODE_POWER_DOWN) == RT_EOK)
|
2019-02-12 10:15:26 +08:00
|
|
|
{
|
2022-12-22 10:24:51 +08:00
|
|
|
RT_SENSOR_MODE_SET_POWER(sensor->info.mode, RT_SENSOR_MODE_POWER_DOWN);
|
2019-02-12 10:15:26 +08:00
|
|
|
}
|
|
|
|
|
2019-06-13 15:10:02 +08:00
|
|
|
if (sensor->module != RT_NULL && sensor->info.fifo_max > 0 && sensor->data_buf != RT_NULL)
|
|
|
|
{
|
|
|
|
for (i = 0; i < sensor->module->sen_num; i ++)
|
|
|
|
{
|
|
|
|
if (sensor->module->sen[i]->parent.ref_count > 0)
|
|
|
|
goto __exit;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Free memory for the sensor buffer */
|
|
|
|
for (i = 0; i < sensor->module->sen_num; i ++)
|
|
|
|
{
|
2022-11-09 11:50:27 +08:00
|
|
|
if (sensor->module->sen[i]->data_buf)
|
2019-06-13 15:10:02 +08:00
|
|
|
{
|
|
|
|
rt_free(sensor->module->sen[i]->data_buf);
|
|
|
|
sensor->module->sen[i]->data_buf = RT_NULL;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2022-12-22 10:24:51 +08:00
|
|
|
if (RT_SENSOR_MODE_GET_FETCH(sensor->info.mode) != RT_SENSOR_MODE_FETCH_POLLING)
|
2019-12-17 17:53:16 +08:00
|
|
|
{
|
2021-03-17 17:42:43 +08:00
|
|
|
/* Sensor disable interrupt */
|
|
|
|
if (sensor->config.irq_pin.pin != RT_PIN_NONE)
|
|
|
|
{
|
|
|
|
rt_pin_irq_enable(sensor->config.irq_pin.pin, RT_FALSE);
|
|
|
|
}
|
2019-12-17 17:53:16 +08:00
|
|
|
}
|
2019-06-13 15:10:02 +08:00
|
|
|
|
|
|
|
__exit:
|
2019-02-12 10:15:26 +08:00
|
|
|
if (sensor->module)
|
|
|
|
{
|
|
|
|
rt_mutex_release(sensor->module->lock);
|
|
|
|
}
|
|
|
|
|
|
|
|
return RT_EOK;
|
|
|
|
}
|
|
|
|
|
2022-11-07 13:33:49 +08:00
|
|
|
static rt_size_t _sensor_read(rt_device_t dev, rt_off_t pos, void *buf, rt_size_t len)
|
2019-02-12 10:15:26 +08:00
|
|
|
{
|
|
|
|
rt_sensor_t sensor = (rt_sensor_t)dev;
|
|
|
|
rt_size_t result = 0;
|
|
|
|
RT_ASSERT(dev != RT_NULL);
|
|
|
|
|
|
|
|
if (buf == NULL || len == 0)
|
|
|
|
{
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (sensor->module)
|
|
|
|
{
|
|
|
|
rt_mutex_take(sensor->module->lock, RT_WAITING_FOREVER);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* The buffer is not empty. Read the data in the buffer first */
|
|
|
|
if (sensor->data_len > 0)
|
|
|
|
{
|
|
|
|
if (len > sensor->data_len / sizeof(struct rt_sensor_data))
|
|
|
|
{
|
|
|
|
len = sensor->data_len / sizeof(struct rt_sensor_data);
|
|
|
|
}
|
|
|
|
|
|
|
|
rt_memcpy(buf, sensor->data_buf, len * sizeof(struct rt_sensor_data));
|
|
|
|
|
|
|
|
/* Clear the buffer */
|
|
|
|
sensor->data_len = 0;
|
|
|
|
result = len;
|
|
|
|
}
|
2021-03-17 13:40:44 +08:00
|
|
|
else
|
2019-02-12 10:15:26 +08:00
|
|
|
{
|
2022-11-09 11:50:27 +08:00
|
|
|
/* If the buffer is empty, read the data */
|
|
|
|
if (sensor->ops->fetch_data)
|
2021-03-17 13:40:44 +08:00
|
|
|
{
|
|
|
|
result = sensor->ops->fetch_data(sensor, buf, len);
|
2021-11-04 20:31:55 +08:00
|
|
|
}
|
2019-02-12 10:15:26 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
if (sensor->module)
|
|
|
|
{
|
|
|
|
rt_mutex_release(sensor->module->lock);
|
|
|
|
}
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2022-11-07 13:33:49 +08:00
|
|
|
static rt_err_t _sensor_control(rt_device_t dev, int cmd, void *args)
|
2019-02-12 10:15:26 +08:00
|
|
|
{
|
|
|
|
rt_sensor_t sensor = (rt_sensor_t)dev;
|
|
|
|
rt_err_t result = RT_EOK;
|
|
|
|
RT_ASSERT(dev != RT_NULL);
|
2022-11-07 13:33:49 +08:00
|
|
|
rt_err_t (*local_ctrl)(rt_sensor_t sensor, int cmd, void *arg) = _local_control;
|
2019-02-12 10:15:26 +08:00
|
|
|
|
|
|
|
if (sensor->module)
|
|
|
|
{
|
|
|
|
rt_mutex_take(sensor->module->lock, RT_WAITING_FOREVER);
|
|
|
|
}
|
2021-03-17 13:40:44 +08:00
|
|
|
if (sensor->ops->control != RT_NULL)
|
|
|
|
{
|
|
|
|
local_ctrl = sensor->ops->control;
|
|
|
|
}
|
2019-02-12 10:15:26 +08:00
|
|
|
|
|
|
|
switch (cmd)
|
|
|
|
{
|
2019-02-13 15:00:06 +08:00
|
|
|
case RT_SENSOR_CTRL_GET_ID:
|
2019-02-12 10:15:26 +08:00
|
|
|
if (args)
|
|
|
|
{
|
2021-03-17 13:40:44 +08:00
|
|
|
result = local_ctrl(sensor, RT_SENSOR_CTRL_GET_ID, args);
|
2019-02-12 10:15:26 +08:00
|
|
|
}
|
|
|
|
break;
|
2022-12-22 10:24:51 +08:00
|
|
|
case RT_SENSOR_CTRL_SET_ACCURACY_MODE:
|
|
|
|
/* Configuration sensor power mode */
|
|
|
|
result = local_ctrl(sensor, RT_SENSOR_CTRL_SET_ACCURACY_MODE, args);
|
2021-03-17 13:40:44 +08:00
|
|
|
if (result == RT_EOK)
|
2019-02-12 10:15:26 +08:00
|
|
|
{
|
2022-12-22 10:24:51 +08:00
|
|
|
RT_SENSOR_MODE_SET_ACCURACY(sensor->info.mode, (rt_uint32_t)args & 0x0F);
|
|
|
|
LOG_D("set accuracy mode code: %d", RT_SENSOR_MODE_GET_ACCURACY(sensor->info.mode));
|
2021-11-04 20:31:55 +08:00
|
|
|
}
|
2019-02-12 10:15:26 +08:00
|
|
|
break;
|
2022-12-22 10:24:51 +08:00
|
|
|
case RT_SENSOR_CTRL_SET_POWER_MODE:
|
|
|
|
/* Configuration sensor power mode */
|
|
|
|
result = local_ctrl(sensor, RT_SENSOR_CTRL_SET_POWER_MODE, args);
|
2021-03-17 13:40:44 +08:00
|
|
|
if (result == RT_EOK)
|
2019-02-12 10:15:26 +08:00
|
|
|
{
|
2022-12-22 10:24:51 +08:00
|
|
|
RT_SENSOR_MODE_SET_POWER(sensor->info.mode, (rt_uint32_t)args & 0x0F);
|
|
|
|
LOG_D("set power mode code: %d", RT_SENSOR_MODE_GET_POWER(sensor->info.mode));
|
2019-02-12 10:15:26 +08:00
|
|
|
}
|
|
|
|
break;
|
2022-12-22 10:24:51 +08:00
|
|
|
case RT_SENSOR_CTRL_SET_FETCH_MODE:
|
2021-03-17 13:40:44 +08:00
|
|
|
/* Configuration sensor power mode */
|
2022-12-22 10:24:51 +08:00
|
|
|
result = local_ctrl(sensor, RT_SENSOR_CTRL_SET_FETCH_MODE, args);
|
2021-03-17 13:40:44 +08:00
|
|
|
if (result == RT_EOK)
|
2019-02-12 10:15:26 +08:00
|
|
|
{
|
2022-12-22 10:24:51 +08:00
|
|
|
RT_SENSOR_MODE_SET_FETCH(sensor->info.mode, (rt_uint32_t)args & 0x0F);
|
|
|
|
LOG_D("set fetch mode code: %d", RT_SENSOR_MODE_GET_FETCH(sensor->info.mode));
|
2019-02-12 10:15:26 +08:00
|
|
|
}
|
|
|
|
break;
|
2019-02-13 15:00:06 +08:00
|
|
|
case RT_SENSOR_CTRL_SELF_TEST:
|
2022-12-22 10:24:51 +08:00
|
|
|
/* device self test */
|
2021-03-17 13:40:44 +08:00
|
|
|
result = local_ctrl(sensor, RT_SENSOR_CTRL_SELF_TEST, args);
|
2019-02-12 10:15:26 +08:00
|
|
|
break;
|
2022-12-22 10:24:51 +08:00
|
|
|
case RT_SENSOR_CTRL_SOFT_RESET:
|
|
|
|
/* device soft reset */
|
|
|
|
result = local_ctrl(sensor, RT_SENSOR_CTRL_SOFT_RESET, args);
|
|
|
|
break;
|
2019-02-12 10:15:26 +08:00
|
|
|
default:
|
2020-03-16 11:03:32 +08:00
|
|
|
if (cmd > RT_SENSOR_CTRL_USER_CMD_START)
|
|
|
|
{
|
2021-03-17 13:40:44 +08:00
|
|
|
/* Custom commands */
|
|
|
|
result = local_ctrl(sensor, cmd, args);
|
2020-03-16 11:03:32 +08:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
result = -RT_ERROR;
|
|
|
|
}
|
2020-02-22 21:00:35 +08:00
|
|
|
break;
|
2019-02-12 10:15:26 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
if (sensor->module)
|
|
|
|
{
|
|
|
|
rt_mutex_release(sensor->module->lock);
|
|
|
|
}
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
#ifdef RT_USING_DEVICE_OPS
|
|
|
|
const static struct rt_device_ops rt_sensor_ops =
|
|
|
|
{
|
2019-06-13 15:10:02 +08:00
|
|
|
RT_NULL,
|
2022-11-07 13:33:49 +08:00
|
|
|
_sensor_open,
|
|
|
|
_sensor_close,
|
|
|
|
_sensor_read,
|
2019-02-12 10:15:26 +08:00
|
|
|
RT_NULL,
|
2022-11-07 13:33:49 +08:00
|
|
|
_sensor_control
|
2019-02-12 10:15:26 +08:00
|
|
|
};
|
|
|
|
#endif
|
|
|
|
|
2021-03-17 13:40:44 +08:00
|
|
|
|
2019-02-12 10:15:26 +08:00
|
|
|
/*
|
|
|
|
* sensor register
|
|
|
|
*/
|
2022-11-18 07:29:24 +08:00
|
|
|
int rt_hw_sensor_register(rt_sensor_t sensor,
|
|
|
|
const char *name,
|
|
|
|
rt_uint32_t flag,
|
|
|
|
void *data)
|
2019-02-12 10:15:26 +08:00
|
|
|
{
|
|
|
|
rt_int8_t result;
|
|
|
|
rt_device_t device;
|
|
|
|
RT_ASSERT(sensor != RT_NULL);
|
|
|
|
|
|
|
|
char *sensor_name = RT_NULL, *device_name = RT_NULL;
|
|
|
|
|
2021-03-17 13:40:44 +08:00
|
|
|
if (sensor->ops == RT_NULL)
|
|
|
|
{
|
|
|
|
sensor->ops = &local_ops;
|
|
|
|
}
|
|
|
|
|
2019-02-12 10:15:26 +08:00
|
|
|
/* Add a type name for the sensor device */
|
|
|
|
sensor_name = sensor_name_str[sensor->info.type];
|
2019-06-18 20:09:19 +08:00
|
|
|
device_name = (char *)rt_calloc(1, rt_strlen(sensor_name) + 1 + rt_strlen(name));
|
2019-02-12 10:15:26 +08:00
|
|
|
if (device_name == RT_NULL)
|
|
|
|
{
|
|
|
|
LOG_E("device_name calloc failed!");
|
|
|
|
return -RT_ERROR;
|
|
|
|
}
|
|
|
|
|
|
|
|
rt_memcpy(device_name, sensor_name, rt_strlen(sensor_name) + 1);
|
|
|
|
strcat(device_name, name);
|
|
|
|
|
|
|
|
if (sensor->module != RT_NULL && sensor->module->lock == RT_NULL)
|
|
|
|
{
|
|
|
|
/* Create a mutex lock for the module */
|
2021-11-18 04:57:15 +08:00
|
|
|
sensor->module->lock = rt_mutex_create(name, RT_IPC_FLAG_PRIO);
|
2019-02-12 10:15:26 +08:00
|
|
|
if (sensor->module->lock == RT_NULL)
|
|
|
|
{
|
|
|
|
rt_free(device_name);
|
|
|
|
return -RT_ERROR;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
device = &sensor->parent;
|
|
|
|
|
|
|
|
#ifdef RT_USING_DEVICE_OPS
|
|
|
|
device->ops = &rt_sensor_ops;
|
|
|
|
#else
|
2019-06-13 15:10:02 +08:00
|
|
|
device->init = RT_NULL;
|
2022-11-07 13:33:49 +08:00
|
|
|
device->open = _sensor_open;
|
|
|
|
device->close = _sensor_close;
|
|
|
|
device->read = _sensor_read;
|
2019-02-12 10:15:26 +08:00
|
|
|
device->write = RT_NULL;
|
2022-11-07 13:33:49 +08:00
|
|
|
device->control = _sensor_control;
|
2019-02-12 10:15:26 +08:00
|
|
|
#endif
|
|
|
|
device->type = RT_Device_Class_Sensor;
|
|
|
|
device->rx_indicate = RT_NULL;
|
|
|
|
device->tx_complete = RT_NULL;
|
|
|
|
device->user_data = data;
|
|
|
|
|
|
|
|
result = rt_device_register(device, device_name, flag | RT_DEVICE_FLAG_STANDALONE);
|
|
|
|
if (result != RT_EOK)
|
|
|
|
{
|
2022-11-09 10:38:38 +08:00
|
|
|
LOG_E("sensor[%s] register err code: %d", device_name, result);
|
2019-11-11 16:46:43 +08:00
|
|
|
rt_free(device_name);
|
2019-02-12 10:15:26 +08:00
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2022-11-09 10:38:38 +08:00
|
|
|
LOG_I("sensor[%s] init success", device_name);
|
2021-12-24 10:10:59 +08:00
|
|
|
rt_free(device_name);
|
2022-12-24 12:00:26 +08:00
|
|
|
|
|
|
|
/* set sensor accuracy and power as the hightest */
|
|
|
|
rt_device_control(device, RT_SENSOR_CTRL_SET_ACCURACY_MODE, RT_SENSOR_MODE_ACCURACY_HIGHEST);
|
|
|
|
rt_device_control(device, RT_SENSOR_CTRL_SET_POWER_MODE, RT_SENSOR_MODE_POWER_HIGHEST);
|
|
|
|
|
2019-02-12 10:15:26 +08:00
|
|
|
return RT_EOK;
|
|
|
|
}
|