|
@@ -121,12 +121,30 @@ static rt_err_t rt_sensor_irq_init(rt_sensor_t sensor)
|
|
|
return 0;
|
|
|
}
|
|
|
|
|
|
+// local rt_sensor_ops
|
|
|
+
|
|
|
+static rt_size_t local_fetch_data(struct rt_sensor_device *sensor, void *buf, rt_size_t len)
|
|
|
+{
|
|
|
+ LOG_D("Undefined fetch_data");
|
|
|
+ return 0;
|
|
|
+}
|
|
|
+static rt_err_t local_control(struct rt_sensor_device *sensor, int cmd, void *arg)
|
|
|
+{
|
|
|
+ LOG_D("Undefined control");
|
|
|
+ return RT_ERROR;
|
|
|
+}
|
|
|
+static struct rt_sensor_ops local_ops = {
|
|
|
+ .fetch_data = local_fetch_data,
|
|
|
+ .control = local_control
|
|
|
+};
|
|
|
+
|
|
|
/* RT-Thread Device Interface */
|
|
|
static rt_err_t rt_sensor_open(rt_device_t dev, rt_uint16_t oflag)
|
|
|
{
|
|
|
rt_sensor_t sensor = (rt_sensor_t)dev;
|
|
|
RT_ASSERT(dev != RT_NULL);
|
|
|
rt_err_t res = RT_EOK;
|
|
|
+ rt_err_t (*local_ctrl)(struct rt_sensor_device *sensor, int cmd, void *arg) = local_control;
|
|
|
|
|
|
if (sensor->module)
|
|
|
{
|
|
@@ -144,37 +162,36 @@ static rt_err_t rt_sensor_open(rt_device_t dev, rt_uint16_t oflag)
|
|
|
goto __exit;
|
|
|
}
|
|
|
}
|
|
|
+ if (sensor->ops->control != RT_NULL)
|
|
|
+ {
|
|
|
+ local_ctrl = sensor->ops->control;
|
|
|
+ }
|
|
|
|
|
|
+ sensor->config.mode = RT_SENSOR_MODE_POLLING;
|
|
|
if (oflag & RT_DEVICE_FLAG_RDONLY && dev->flag & RT_DEVICE_FLAG_RDONLY)
|
|
|
{
|
|
|
- if (sensor->ops->control != RT_NULL)
|
|
|
- {
|
|
|
- /* If polling mode is supported, configure it to polling mode */
|
|
|
- sensor->ops->control(sensor, RT_SENSOR_CTRL_SET_MODE, (void *)RT_SENSOR_MODE_POLLING);
|
|
|
- }
|
|
|
- sensor->config.mode = RT_SENSOR_MODE_POLLING;
|
|
|
+ /* If polling mode is supported, configure it to polling mode */
|
|
|
+ local_ctrl(sensor, RT_SENSOR_CTRL_SET_MODE, (void *)RT_SENSOR_MODE_POLLING);
|
|
|
}
|
|
|
else if (oflag & RT_DEVICE_FLAG_INT_RX && dev->flag & RT_DEVICE_FLAG_INT_RX)
|
|
|
{
|
|
|
- if (sensor->ops->control != RT_NULL)
|
|
|
+ /* If interrupt mode is supported, configure it to interrupt mode */
|
|
|
+ if (local_ctrl(sensor, RT_SENSOR_CTRL_SET_MODE, (void *)RT_SENSOR_MODE_INT) == RT_EOK)
|
|
|
{
|
|
|
- /* If interrupt mode is supported, configure it to interrupt mode */
|
|
|
- sensor->ops->control(sensor, RT_SENSOR_CTRL_SET_MODE, (void *)RT_SENSOR_MODE_INT);
|
|
|
+ /* Initialization sensor interrupt */
|
|
|
+ rt_sensor_irq_init(sensor);
|
|
|
+ sensor->config.mode = RT_SENSOR_MODE_INT;
|
|
|
}
|
|
|
- /* Initialization sensor interrupt */
|
|
|
- rt_sensor_irq_init(sensor);
|
|
|
- sensor->config.mode = RT_SENSOR_MODE_INT;
|
|
|
}
|
|
|
else if (oflag & RT_DEVICE_FLAG_FIFO_RX && dev->flag & RT_DEVICE_FLAG_FIFO_RX)
|
|
|
{
|
|
|
- if (sensor->ops->control != RT_NULL)
|
|
|
+ /* If fifo mode is supported, configure it to fifo mode */
|
|
|
+ if (local_ctrl(sensor, RT_SENSOR_CTRL_SET_MODE, (void *)RT_SENSOR_MODE_FIFO) == RT_EOK)
|
|
|
{
|
|
|
- /* If fifo mode is supported, configure it to fifo mode */
|
|
|
- sensor->ops->control(sensor, RT_SENSOR_CTRL_SET_MODE, (void *)RT_SENSOR_MODE_FIFO);
|
|
|
+ /* Initialization sensor interrupt */
|
|
|
+ rt_sensor_irq_init(sensor);
|
|
|
+ sensor->config.mode = RT_SENSOR_MODE_FIFO;
|
|
|
}
|
|
|
- /* Initialization sensor interrupt */
|
|
|
- rt_sensor_irq_init(sensor);
|
|
|
- sensor->config.mode = RT_SENSOR_MODE_FIFO;
|
|
|
}
|
|
|
else
|
|
|
{
|
|
@@ -183,7 +200,7 @@ static rt_err_t rt_sensor_open(rt_device_t dev, rt_uint16_t oflag)
|
|
|
}
|
|
|
|
|
|
/* Configure power mode to normal mode */
|
|
|
- if (sensor->ops->control(sensor, RT_SENSOR_CTRL_SET_POWER, (void *)RT_SENSOR_POWER_NORMAL) == RT_EOK)
|
|
|
+ if (local_ctrl(sensor, RT_SENSOR_CTRL_SET_POWER, (void *)RT_SENSOR_POWER_NORMAL) == RT_EOK)
|
|
|
{
|
|
|
sensor->config.power = RT_SENSOR_POWER_NORMAL;
|
|
|
}
|
|
@@ -202,6 +219,7 @@ static rt_err_t rt_sensor_close(rt_device_t dev)
|
|
|
{
|
|
|
rt_sensor_t sensor = (rt_sensor_t)dev;
|
|
|
int i;
|
|
|
+ rt_err_t (*local_ctrl)(struct rt_sensor_device * sensor, int cmd, void *arg) = local_control;
|
|
|
|
|
|
RT_ASSERT(dev != RT_NULL);
|
|
|
|
|
@@ -209,9 +227,13 @@ static rt_err_t rt_sensor_close(rt_device_t dev)
|
|
|
{
|
|
|
rt_mutex_take(sensor->module->lock, RT_WAITING_FOREVER);
|
|
|
}
|
|
|
+ if (sensor->ops->control != RT_NULL)
|
|
|
+ {
|
|
|
+ local_ctrl = sensor->ops->control;
|
|
|
+ }
|
|
|
|
|
|
/* Configure power mode to power down mode */
|
|
|
- if (sensor->ops->control(sensor, RT_SENSOR_CTRL_SET_POWER, (void *)RT_SENSOR_POWER_DOWN) == RT_EOK)
|
|
|
+ if (local_ctrl(sensor, RT_SENSOR_CTRL_SET_POWER, (void *)RT_SENSOR_POWER_DOWN) == RT_EOK)
|
|
|
{
|
|
|
sensor->config.power = RT_SENSOR_POWER_DOWN;
|
|
|
}
|
|
@@ -234,10 +256,13 @@ static rt_err_t rt_sensor_close(rt_device_t dev)
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
- /* Sensor disable interrupt */
|
|
|
- if (sensor->config.irq_pin.pin != RT_PIN_NONE)
|
|
|
+ if (sensor->config.mode != RT_SENSOR_MODE_POLLING)
|
|
|
{
|
|
|
- rt_pin_irq_enable(sensor->config.irq_pin.pin, RT_FALSE);
|
|
|
+ /* Sensor disable interrupt */
|
|
|
+ if (sensor->config.irq_pin.pin != RT_PIN_NONE)
|
|
|
+ {
|
|
|
+ rt_pin_irq_enable(sensor->config.irq_pin.pin, RT_FALSE);
|
|
|
+ }
|
|
|
}
|
|
|
|
|
|
__exit:
|
|
@@ -282,7 +307,10 @@ static rt_size_t rt_sensor_read(rt_device_t dev, rt_off_t pos, void *buf, rt_siz
|
|
|
else
|
|
|
{
|
|
|
/* If the buffer is empty read the data */
|
|
|
- result = sensor->ops->fetch_data(sensor, buf, len);
|
|
|
+ if (sensor->ops->fetch_data != RT_NULL)
|
|
|
+ {
|
|
|
+ result = sensor->ops->fetch_data(sensor, buf, len);
|
|
|
+ }
|
|
|
}
|
|
|
|
|
|
if (sensor->module)
|
|
@@ -298,18 +326,23 @@ static rt_err_t rt_sensor_control(rt_device_t dev, int cmd, void *args)
|
|
|
rt_sensor_t sensor = (rt_sensor_t)dev;
|
|
|
rt_err_t result = RT_EOK;
|
|
|
RT_ASSERT(dev != RT_NULL);
|
|
|
+ rt_err_t (*local_ctrl)(struct rt_sensor_device * sensor, int cmd, void *arg) = local_control;
|
|
|
|
|
|
if (sensor->module)
|
|
|
{
|
|
|
rt_mutex_take(sensor->module->lock, RT_WAITING_FOREVER);
|
|
|
}
|
|
|
+ if (sensor->ops->control != RT_NULL)
|
|
|
+ {
|
|
|
+ local_ctrl = sensor->ops->control;
|
|
|
+ }
|
|
|
|
|
|
switch (cmd)
|
|
|
{
|
|
|
case RT_SENSOR_CTRL_GET_ID:
|
|
|
if (args)
|
|
|
{
|
|
|
- result = sensor->ops->control(sensor, RT_SENSOR_CTRL_GET_ID, args);
|
|
|
+ result = local_ctrl(sensor, RT_SENSOR_CTRL_GET_ID, args);
|
|
|
}
|
|
|
break;
|
|
|
case RT_SENSOR_CTRL_GET_INFO:
|
|
@@ -319,19 +352,17 @@ static rt_err_t rt_sensor_control(rt_device_t dev, int cmd, void *args)
|
|
|
}
|
|
|
break;
|
|
|
case RT_SENSOR_CTRL_SET_RANGE:
|
|
|
-
|
|
|
/* Configuration measurement range */
|
|
|
- result = sensor->ops->control(sensor, RT_SENSOR_CTRL_SET_RANGE, args);
|
|
|
+ result = local_ctrl(sensor, RT_SENSOR_CTRL_SET_RANGE, args);
|
|
|
if (result == RT_EOK)
|
|
|
{
|
|
|
sensor->config.range = (rt_int32_t)args;
|
|
|
LOG_D("set range %d", sensor->config.range);
|
|
|
- }
|
|
|
+ }
|
|
|
break;
|
|
|
case RT_SENSOR_CTRL_SET_ODR:
|
|
|
-
|
|
|
/* Configuration data output rate */
|
|
|
- result = sensor->ops->control(sensor, RT_SENSOR_CTRL_SET_ODR, args);
|
|
|
+ result = local_ctrl(sensor, RT_SENSOR_CTRL_SET_ODR, args);
|
|
|
if (result == RT_EOK)
|
|
|
{
|
|
|
sensor->config.odr = (rt_uint32_t)args & 0xFFFF;
|
|
@@ -339,9 +370,8 @@ static rt_err_t rt_sensor_control(rt_device_t dev, int cmd, void *args)
|
|
|
}
|
|
|
break;
|
|
|
case RT_SENSOR_CTRL_SET_POWER:
|
|
|
-
|
|
|
/* Configuration sensor power mode */
|
|
|
- result = sensor->ops->control(sensor, RT_SENSOR_CTRL_SET_POWER, args);
|
|
|
+ result = local_ctrl(sensor, RT_SENSOR_CTRL_SET_POWER, args);
|
|
|
if (result == RT_EOK)
|
|
|
{
|
|
|
sensor->config.power = (rt_uint32_t)args & 0xFF;
|
|
@@ -349,16 +379,15 @@ static rt_err_t rt_sensor_control(rt_device_t dev, int cmd, void *args)
|
|
|
}
|
|
|
break;
|
|
|
case RT_SENSOR_CTRL_SELF_TEST:
|
|
|
-
|
|
|
/* Device self-test */
|
|
|
- result = sensor->ops->control(sensor, RT_SENSOR_CTRL_SELF_TEST, args);
|
|
|
+ result = local_ctrl(sensor, RT_SENSOR_CTRL_SELF_TEST, args);
|
|
|
break;
|
|
|
default:
|
|
|
|
|
|
if (cmd > RT_SENSOR_CTRL_USER_CMD_START)
|
|
|
{
|
|
|
/* Custom commands */
|
|
|
- result = sensor->ops->control(sensor, cmd, args);
|
|
|
+ result = local_ctrl(sensor, cmd, args);
|
|
|
}
|
|
|
else
|
|
|
{
|
|
@@ -387,6 +416,7 @@ const static struct rt_device_ops rt_sensor_ops =
|
|
|
};
|
|
|
#endif
|
|
|
|
|
|
+
|
|
|
/*
|
|
|
* sensor register
|
|
|
*/
|
|
@@ -401,6 +431,11 @@ int rt_hw_sensor_register(rt_sensor_t sensor,
|
|
|
|
|
|
char *sensor_name = RT_NULL, *device_name = RT_NULL;
|
|
|
|
|
|
+ if (sensor->ops == RT_NULL)
|
|
|
+ {
|
|
|
+ sensor->ops = &local_ops;
|
|
|
+ }
|
|
|
+
|
|
|
/* Add a type name for the sensor device */
|
|
|
sensor_name = sensor_name_str[sensor->info.type];
|
|
|
device_name = (char *)rt_calloc(1, rt_strlen(sensor_name) + 1 + rt_strlen(name));
|
|
@@ -444,12 +479,12 @@ int rt_hw_sensor_register(rt_sensor_t sensor,
|
|
|
result = rt_device_register(device, device_name, flag | RT_DEVICE_FLAG_STANDALONE);
|
|
|
if (result != RT_EOK)
|
|
|
{
|
|
|
+ LOG_E("rt_sensor[%s] register err code: %d", device_name, result);
|
|
|
rt_free(device_name);
|
|
|
- LOG_E("rt_sensor register err code: %d", result);
|
|
|
return result;
|
|
|
}
|
|
|
|
|
|
rt_free(device_name);
|
|
|
- LOG_I("rt_sensor init success");
|
|
|
+ LOG_I("rt_sensor[%s] init success", device_name);
|
|
|
return RT_EOK;
|
|
|
}
|