优化ops的检测代码
This commit is contained in:
parent
613f89a372
commit
58bb9f5b2e
|
@ -121,12 +121,30 @@ static rt_err_t rt_sensor_irq_init(rt_sensor_t sensor)
|
||||||
return 0;
|
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 */
|
/* RT-Thread Device Interface */
|
||||||
static rt_err_t rt_sensor_open(rt_device_t dev, rt_uint16_t oflag)
|
static rt_err_t rt_sensor_open(rt_device_t dev, rt_uint16_t oflag)
|
||||||
{
|
{
|
||||||
rt_sensor_t sensor = (rt_sensor_t)dev;
|
rt_sensor_t sensor = (rt_sensor_t)dev;
|
||||||
RT_ASSERT(dev != RT_NULL);
|
RT_ASSERT(dev != RT_NULL);
|
||||||
rt_err_t res = RT_EOK;
|
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)
|
if (sensor->module)
|
||||||
{
|
{
|
||||||
|
@ -144,38 +162,37 @@ static rt_err_t rt_sensor_open(rt_device_t dev, rt_uint16_t oflag)
|
||||||
goto __exit;
|
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 (oflag & RT_DEVICE_FLAG_RDONLY && dev->flag & RT_DEVICE_FLAG_RDONLY)
|
||||||
{
|
|
||||||
if ((sensor->ops != RT_NULL) && (sensor->ops->control != RT_NULL))
|
|
||||||
{
|
{
|
||||||
/* If polling mode is supported, configure it to polling mode */
|
/* If polling mode is supported, configure it to polling mode */
|
||||||
sensor->ops->control(sensor, RT_SENSOR_CTRL_SET_MODE, (void *)RT_SENSOR_MODE_POLLING);
|
local_ctrl(sensor, RT_SENSOR_CTRL_SET_MODE, (void *)RT_SENSOR_MODE_POLLING);
|
||||||
}
|
|
||||||
sensor->config.mode = RT_SENSOR_MODE_POLLING;
|
|
||||||
}
|
}
|
||||||
else if (oflag & RT_DEVICE_FLAG_INT_RX && dev->flag & RT_DEVICE_FLAG_INT_RX)
|
else if (oflag & RT_DEVICE_FLAG_INT_RX && dev->flag & RT_DEVICE_FLAG_INT_RX)
|
||||||
{
|
|
||||||
if ((sensor->ops != RT_NULL) && (sensor->ops->control != RT_NULL))
|
|
||||||
{
|
{
|
||||||
/* If interrupt mode is supported, configure it to interrupt mode */
|
/* If interrupt mode is supported, configure it to interrupt mode */
|
||||||
sensor->ops->control(sensor, RT_SENSOR_CTRL_SET_MODE, (void *)RT_SENSOR_MODE_INT);
|
if (RT_EOK== local_ctrl(sensor, RT_SENSOR_CTRL_SET_MODE, (void *)RT_SENSOR_MODE_INT))
|
||||||
}
|
{
|
||||||
/* Initialization sensor interrupt */
|
/* Initialization sensor interrupt */
|
||||||
rt_sensor_irq_init(sensor);
|
rt_sensor_irq_init(sensor);
|
||||||
sensor->config.mode = RT_SENSOR_MODE_INT;
|
sensor->config.mode = RT_SENSOR_MODE_INT;
|
||||||
}
|
}
|
||||||
|
}
|
||||||
else if (oflag & RT_DEVICE_FLAG_FIFO_RX && dev->flag & RT_DEVICE_FLAG_FIFO_RX)
|
else if (oflag & RT_DEVICE_FLAG_FIFO_RX && dev->flag & RT_DEVICE_FLAG_FIFO_RX)
|
||||||
{
|
|
||||||
if ((sensor->ops != RT_NULL) && (sensor->ops->control != RT_NULL))
|
|
||||||
{
|
{
|
||||||
/* If fifo mode is supported, configure it to fifo mode */
|
/* If fifo mode is supported, configure it to fifo mode */
|
||||||
sensor->ops->control(sensor, RT_SENSOR_CTRL_SET_MODE, (void *)RT_SENSOR_MODE_FIFO);
|
if (RT_EOK == local_ctrl(sensor, RT_SENSOR_CTRL_SET_MODE, (void *)RT_SENSOR_MODE_FIFO))
|
||||||
}
|
{
|
||||||
/* Initialization sensor interrupt */
|
/* Initialization sensor interrupt */
|
||||||
rt_sensor_irq_init(sensor);
|
rt_sensor_irq_init(sensor);
|
||||||
sensor->config.mode = RT_SENSOR_MODE_FIFO;
|
sensor->config.mode = RT_SENSOR_MODE_FIFO;
|
||||||
}
|
}
|
||||||
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
res = -RT_EINVAL;
|
res = -RT_EINVAL;
|
||||||
|
@ -183,14 +200,7 @@ static rt_err_t rt_sensor_open(rt_device_t dev, rt_uint16_t oflag)
|
||||||
}
|
}
|
||||||
|
|
||||||
/* Configure power mode to normal mode */
|
/* Configure power mode to normal mode */
|
||||||
if ((sensor->ops != RT_NULL) && (sensor->ops->control != RT_NULL))
|
if (RT_EOK == local_ctrl(sensor, RT_SENSOR_CTRL_SET_POWER, (void *)RT_SENSOR_POWER_NORMAL))
|
||||||
{
|
|
||||||
if (sensor->ops->control(sensor, RT_SENSOR_CTRL_SET_POWER, (void *)RT_SENSOR_POWER_NORMAL) == RT_EOK)
|
|
||||||
{
|
|
||||||
sensor->config.power = RT_SENSOR_POWER_NORMAL;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
{
|
||||||
sensor->config.power = RT_SENSOR_POWER_NORMAL;
|
sensor->config.power = RT_SENSOR_POWER_NORMAL;
|
||||||
}
|
}
|
||||||
|
@ -209,6 +219,7 @@ static rt_err_t rt_sensor_close(rt_device_t dev)
|
||||||
{
|
{
|
||||||
rt_sensor_t sensor = (rt_sensor_t)dev;
|
rt_sensor_t sensor = (rt_sensor_t)dev;
|
||||||
int i;
|
int i;
|
||||||
|
rt_err_t (*local_ctrl)(struct rt_sensor_device * sensor, int cmd, void *arg) = local_control;
|
||||||
|
|
||||||
RT_ASSERT(dev != RT_NULL);
|
RT_ASSERT(dev != RT_NULL);
|
||||||
|
|
||||||
|
@ -216,16 +227,13 @@ static rt_err_t rt_sensor_close(rt_device_t dev)
|
||||||
{
|
{
|
||||||
rt_mutex_take(sensor->module->lock, RT_WAITING_FOREVER);
|
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 */
|
/* Configure power mode to power down mode */
|
||||||
if ((sensor->ops != RT_NULL) && (sensor->ops->control != RT_NULL))
|
if (RT_EOK == local_ctrl(sensor, RT_SENSOR_CTRL_SET_POWER, (void *)RT_SENSOR_POWER_DOWN))
|
||||||
{
|
|
||||||
if (sensor->ops->control(sensor, RT_SENSOR_CTRL_SET_POWER, (void *)RT_SENSOR_POWER_DOWN) == RT_EOK)
|
|
||||||
{
|
|
||||||
sensor->config.power = RT_SENSOR_POWER_DOWN;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
{
|
||||||
sensor->config.power = RT_SENSOR_POWER_DOWN;
|
sensor->config.power = RT_SENSOR_POWER_DOWN;
|
||||||
}
|
}
|
||||||
|
@ -293,11 +301,14 @@ static rt_size_t rt_sensor_read(rt_device_t dev, rt_off_t pos, void *buf, rt_siz
|
||||||
sensor->data_len = 0;
|
sensor->data_len = 0;
|
||||||
result = len;
|
result = len;
|
||||||
}
|
}
|
||||||
else if ((sensor->ops != RT_NULL) && (sensor->ops->fetch_data != RT_NULL))
|
else
|
||||||
{
|
{
|
||||||
/* If the buffer is empty read the data */
|
/* If the buffer is empty read the data */
|
||||||
|
if (sensor->ops->fetch_data != RT_NULL)
|
||||||
|
{
|
||||||
result = sensor->ops->fetch_data(sensor, buf, len);
|
result = sensor->ops->fetch_data(sensor, buf, len);
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
if (sensor->module)
|
if (sensor->module)
|
||||||
{
|
{
|
||||||
|
@ -312,21 +323,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_sensor_t sensor = (rt_sensor_t)dev;
|
||||||
rt_err_t result = RT_EOK;
|
rt_err_t result = RT_EOK;
|
||||||
RT_ASSERT(dev != RT_NULL);
|
RT_ASSERT(dev != RT_NULL);
|
||||||
|
rt_err_t (*local_ctrl)(struct rt_sensor_device * sensor, int cmd, void *arg) = local_control;
|
||||||
|
|
||||||
if (sensor->module)
|
if (sensor->module)
|
||||||
{
|
{
|
||||||
rt_mutex_take(sensor->module->lock, RT_WAITING_FOREVER);
|
rt_mutex_take(sensor->module->lock, RT_WAITING_FOREVER);
|
||||||
}
|
}
|
||||||
|
if (sensor->ops->control != RT_NULL)
|
||||||
|
{
|
||||||
|
local_ctrl = sensor->ops->control;
|
||||||
|
}
|
||||||
|
|
||||||
switch (cmd)
|
switch (cmd)
|
||||||
{
|
{
|
||||||
case RT_SENSOR_CTRL_GET_ID:
|
case RT_SENSOR_CTRL_GET_ID:
|
||||||
if (args)
|
if (args)
|
||||||
{
|
{
|
||||||
if ((sensor->ops != RT_NULL) && (sensor->ops->control != RT_NULL))
|
result = local_ctrl(sensor, RT_SENSOR_CTRL_GET_ID, args);
|
||||||
{
|
|
||||||
result = sensor->ops->control(sensor, RT_SENSOR_CTRL_GET_ID, args);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
case RT_SENSOR_CTRL_GET_INFO:
|
case RT_SENSOR_CTRL_GET_INFO:
|
||||||
|
@ -336,57 +349,42 @@ static rt_err_t rt_sensor_control(rt_device_t dev, int cmd, void *args)
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
case RT_SENSOR_CTRL_SET_RANGE:
|
case RT_SENSOR_CTRL_SET_RANGE:
|
||||||
if ((sensor->ops != RT_NULL) && (sensor->ops->control != RT_NULL))
|
|
||||||
{
|
|
||||||
/* Configuration measurement 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)
|
if (result == RT_EOK)
|
||||||
{
|
{
|
||||||
sensor->config.range = (rt_int32_t)args;
|
sensor->config.range = (rt_int32_t)args;
|
||||||
LOG_D("set range %d", sensor->config.range);
|
LOG_D("set range %d", sensor->config.range);
|
||||||
}
|
}
|
||||||
}
|
|
||||||
break;
|
break;
|
||||||
case RT_SENSOR_CTRL_SET_ODR:
|
case RT_SENSOR_CTRL_SET_ODR:
|
||||||
if ((sensor->ops != RT_NULL) && (sensor->ops->control != RT_NULL))
|
|
||||||
{
|
|
||||||
/* Configuration data output rate */
|
/* 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)
|
if (result == RT_EOK)
|
||||||
{
|
{
|
||||||
sensor->config.odr = (rt_uint32_t)args & 0xFFFF;
|
sensor->config.odr = (rt_uint32_t)args & 0xFFFF;
|
||||||
LOG_D("set odr %d", sensor->config.odr);
|
LOG_D("set odr %d", sensor->config.odr);
|
||||||
}
|
}
|
||||||
}
|
|
||||||
break;
|
break;
|
||||||
case RT_SENSOR_CTRL_SET_POWER:
|
case RT_SENSOR_CTRL_SET_POWER:
|
||||||
if ((sensor->ops != RT_NULL) && (sensor->ops->control != RT_NULL))
|
|
||||||
{
|
|
||||||
/* Configuration sensor power mode */
|
/* 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)
|
if (result == RT_EOK)
|
||||||
{
|
{
|
||||||
sensor->config.power = (rt_uint32_t)args & 0xFF;
|
sensor->config.power = (rt_uint32_t)args & 0xFF;
|
||||||
LOG_D("set power mode code:", sensor->config.power);
|
LOG_D("set power mode code:", sensor->config.power);
|
||||||
}
|
}
|
||||||
}
|
|
||||||
break;
|
break;
|
||||||
case RT_SENSOR_CTRL_SELF_TEST:
|
case RT_SENSOR_CTRL_SELF_TEST:
|
||||||
if ((sensor->ops != RT_NULL) && (sensor->ops->control != RT_NULL))
|
|
||||||
{
|
|
||||||
/* Device 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;
|
break;
|
||||||
default:
|
default:
|
||||||
|
|
||||||
if (cmd > RT_SENSOR_CTRL_USER_CMD_START)
|
if (cmd > RT_SENSOR_CTRL_USER_CMD_START)
|
||||||
{
|
|
||||||
if ((sensor->ops != RT_NULL) && (sensor->ops->control != RT_NULL))
|
|
||||||
{
|
{
|
||||||
/* Custom commands */
|
/* Custom commands */
|
||||||
result = sensor->ops->control(sensor, cmd, args);
|
result = local_ctrl(sensor, cmd, args);
|
||||||
}
|
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
|
@ -415,6 +413,7 @@ const static struct rt_device_ops rt_sensor_ops =
|
||||||
};
|
};
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* sensor register
|
* sensor register
|
||||||
*/
|
*/
|
||||||
|
@ -429,6 +428,11 @@ int rt_hw_sensor_register(rt_sensor_t sensor,
|
||||||
|
|
||||||
char *sensor_name = RT_NULL, *device_name = RT_NULL;
|
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 */
|
/* Add a type name for the sensor device */
|
||||||
sensor_name = sensor_name_str[sensor->info.type];
|
sensor_name = sensor_name_str[sensor->info.type];
|
||||||
device_name = (char *)rt_calloc(1, rt_strlen(sensor_name) + 1 + rt_strlen(name));
|
device_name = (char *)rt_calloc(1, rt_strlen(sensor_name) + 1 + rt_strlen(name));
|
||||||
|
|
Loading…
Reference in New Issue