Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
AdRainty
rt-thread
提交
2acfc700
R
rt-thread
项目概览
AdRainty
/
rt-thread
与 Fork 源项目一致
Fork自
RT-Thread / rt-thread
通知
1
Star
0
Fork
0
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
0
列表
看板
标记
里程碑
合并请求
0
DevOps
流水线
流水线任务
计划
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
R
rt-thread
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
0
Issue
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
Pages
DevOps
DevOps
流水线
流水线任务
计划
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
流水线任务
提交
Issue看板
体验新版 GitCode,发现更多精彩内容 >>
未验证
提交
2acfc700
编写于
6月 17, 2019
作者:
B
Bernard Xiong
提交者:
GitHub
6月 17, 2019
浏览文件
操作
浏览文件
下载
差异文件
Merge pull request #2775 from Guozhanxin/sensor_fix
修复 sensor 设备关闭后分配的内存不释放的问题
上级
a6a01da3
7d6fa238
变更
2
显示空白变更内容
内联
并排
Showing
2 changed file
with
64 addition
and
83 deletion
+64
-83
components/drivers/sensors/sensor.c
components/drivers/sensors/sensor.c
+63
-82
components/drivers/sensors/sensor_cmd.c
components/drivers/sensors/sensor_cmd.c
+1
-1
未找到文件。
components/drivers/sensors/sensor.c
浏览文件 @
2acfc700
...
...
@@ -116,30 +116,18 @@ static rt_err_t rt_sensor_irq_init(rt_sensor_t sensor)
return
0
;
}
/* Sensor interrupt enable */
static
void
rt_sensor_irq_enable
(
rt_sensor_t
sensor
)
{
if
(
sensor
->
config
.
irq_pin
.
pin
!=
RT_PIN_NONE
)
{
rt_pin_irq_enable
(
sensor
->
config
.
irq_pin
.
pin
,
RT_TRUE
);
}
}
/* Sensor interrupt disable */
static
void
rt_sensor_irq_disable
(
rt_sensor_t
sensor
)
{
if
(
sensor
->
config
.
irq_pin
.
pin
!=
RT_PIN_NONE
)
{
rt_pin_irq_enable
(
sensor
->
config
.
irq_pin
.
pin
,
RT_FALSE
);
}
}
/* RT-Thread Device Interface */
static
rt_err_t
rt_sensor_init
(
rt_device_t
dev
)
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
;
if
(
sensor
->
module
)
{
/* take the module mutex */
rt_mutex_take
(
sensor
->
module
->
lock
,
RT_WAITING_FOREVER
);
}
if
(
sensor
->
module
!=
RT_NULL
&&
sensor
->
info
.
fifo_max
>
0
&&
sensor
->
data_buf
==
RT_NULL
)
{
...
...
@@ -147,60 +135,46 @@ static rt_err_t rt_sensor_init(rt_device_t dev)
sensor
->
data_buf
=
rt_malloc
(
sizeof
(
struct
rt_sensor_data
)
*
sensor
->
info
.
fifo_max
);
if
(
sensor
->
data_buf
==
RT_NULL
)
{
return
-
RT_ENOMEM
;
res
=
-
RT_ENOMEM
;
goto
__exit
;
}
}
return
RT_EOK
;
}
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
);
if
(
sensor
->
module
)
{
/* take the module mutex */
rt_mutex_take
(
sensor
->
module
->
lock
,
RT_WAITING_FOREVER
);
}
if
(
oflag
&
RT_DEVICE_FLAG_RDONLY
&&
dev
->
flag
&
RT_DEVICE_FLAG_RDONLY
)
{
/* If polling mode is supported, configure it to polling mode */
if
(
sensor
->
ops
->
control
(
sensor
,
RT_SENSOR_CTRL_SET_MODE
,
(
void
*
)
RT_SENSOR_MODE_POLLING
)
==
RT_EOK
)
if
(
sensor
->
ops
->
control
!=
RT_NULL
)
{
sensor
->
config
.
mode
=
RT_SENSOR_MODE_POLLING
;
/* 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
;
}
else
if
(
oflag
&
RT_DEVICE_FLAG_INT_RX
&&
dev
->
flag
&
RT_DEVICE_FLAG_INT_RX
)
{
/* If interrupt mode is supported, configure it to interrupt mode */
if
(
sensor
->
ops
->
control
(
sensor
,
RT_SENSOR_CTRL_SET_MODE
,
(
void
*
)
RT_SENSOR_MODE_INT
)
==
RT_EOK
)
if
(
sensor
->
ops
->
control
!=
RT_NULL
)
{
sensor
->
config
.
mode
=
RT_SENSOR_MODE_INT
;
/* 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
;
}
else
if
(
oflag
&
RT_DEVICE_FLAG_FIFO_RX
&&
dev
->
flag
&
RT_DEVICE_FLAG_FIFO_RX
)
{
/* If fifo mode is supported, configure it to fifo mode */
if
(
sensor
->
ops
->
control
(
sensor
,
RT_SENSOR_CTRL_SET_MODE
,
(
void
*
)
RT_SENSOR_MODE_FIFO
)
==
RT_EOK
)
if
(
sensor
->
ops
->
control
!=
RT_NULL
)
{
sensor
->
config
.
mode
=
RT_SENSOR_MODE_FIFO
;
/* 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
;
}
else
{
if
(
sensor
->
module
)
{
/* release the module mutex */
rt_mutex_release
(
sensor
->
module
->
lock
);
}
return
-
RT_EINVAL
;
res
=
-
RT_EINVAL
;
goto
__exit
;
}
/* Configure power mode to normal mode */
...
...
@@ -209,18 +183,21 @@ static rt_err_t rt_sensor_open(rt_device_t dev, rt_uint16_t oflag)
sensor
->
config
.
power
=
RT_SENSOR_POWER_NORMAL
;
}
__exit:
if
(
sensor
->
module
)
{
/* release the module mutex */
rt_mutex_release
(
sensor
->
module
->
lock
);
}
return
RT_EOK
;
return
res
;
}
static
rt_err_t
rt_sensor_close
(
rt_device_t
dev
)
{
rt_sensor_t
sensor
=
(
rt_sensor_t
)
dev
;
int
i
;
RT_ASSERT
(
dev
!=
RT_NULL
);
if
(
sensor
->
module
)
...
...
@@ -235,8 +212,31 @@ static rt_err_t rt_sensor_close(rt_device_t dev)
}
/* Sensor disable interrupt */
rt_sensor_irq_disable
(
sensor
);
if
(
sensor
->
config
.
irq_pin
.
pin
!=
RT_PIN_NONE
)
{
rt_pin_irq_enable
(
sensor
->
config
.
irq_pin
.
pin
,
RT_FALSE
);
}
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
++
)
{
if
(
sensor
->
module
->
sen
[
i
]
->
data_buf
!=
RT_NULL
)
{
rt_free
(
sensor
->
module
->
sen
[
i
]
->
data_buf
);
sensor
->
module
->
sen
[
i
]
->
data_buf
=
RT_NULL
;
}
}
}
__exit:
if
(
sensor
->
module
)
{
rt_mutex_release
(
sensor
->
module
->
lock
);
...
...
@@ -334,25 +334,6 @@ static rt_err_t rt_sensor_control(rt_device_t dev, int cmd, void *args)
LOG_D
(
"set odr %d"
,
sensor
->
config
.
odr
);
}
break
;
case
RT_SENSOR_CTRL_SET_MODE
:
/* Configuration sensor work mode */
result
=
sensor
->
ops
->
control
(
sensor
,
RT_SENSOR_CTRL_SET_MODE
,
args
);
if
(
result
==
RT_EOK
)
{
sensor
->
config
.
mode
=
(
rt_uint32_t
)
args
&
0xFF
;
LOG_D
(
"set work mode code:"
,
sensor
->
config
.
mode
);
if
(
sensor
->
config
.
mode
==
RT_SENSOR_MODE_POLLING
)
{
rt_sensor_irq_disable
(
sensor
);
}
else
if
(
sensor
->
config
.
mode
==
RT_SENSOR_MODE_INT
||
sensor
->
config
.
mode
==
RT_SENSOR_MODE_FIFO
)
{
rt_sensor_irq_enable
(
sensor
);
}
}
break
;
case
RT_SENSOR_CTRL_SET_POWER
:
/* Configuration sensor power mode */
...
...
@@ -383,7 +364,7 @@ static rt_err_t rt_sensor_control(rt_device_t dev, int cmd, void *args)
#ifdef RT_USING_DEVICE_OPS
const
static
struct
rt_device_ops
rt_sensor_ops
=
{
rt_sensor_init
,
RT_NULL
,
rt_sensor_open
,
rt_sensor_close
,
rt_sensor_read
,
...
...
@@ -434,7 +415,7 @@ int rt_hw_sensor_register(rt_sensor_t sensor,
#ifdef RT_USING_DEVICE_OPS
device
->
ops
=
&
rt_sensor_ops
;
#else
device
->
init
=
rt_sensor_init
;
device
->
init
=
RT_NULL
;
device
->
open
=
rt_sensor_open
;
device
->
close
=
rt_sensor_close
;
device
->
read
=
rt_sensor_read
;
...
...
components/drivers/sensors/sensor_cmd.c
浏览文件 @
2acfc700
...
...
@@ -55,7 +55,7 @@ static void sensor_show_data(rt_size_t num, rt_sensor_t sensor, struct rt_sensor
}
}
rt_err_t
rx_callback
(
rt_device_t
dev
,
rt_size_t
size
)
static
rt_err_t
rx_callback
(
rt_device_t
dev
,
rt_size_t
size
)
{
rt_sem_release
(
sensor_rx_sem
);
return
0
;
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录