提交 e737514f 编写于 作者: B bernard.xiong@gmail.com

add device check in register function, which is provided by Rob <rdent@iinet.net.au>.

git-svn-id: https://rt-thread.googlecode.com/svn/trunk@2355 bbd45198-f89e-11dd-88c7-29a3b14d5316
上级 2bbd1496
...@@ -11,6 +11,8 @@ ...@@ -11,6 +11,8 @@
* Date Author Notes * Date Author Notes
* 2007-01-21 Bernard the first version * 2007-01-21 Bernard the first version
* 2010-05-04 Bernard add rt_device_init implementation * 2010-05-04 Bernard add rt_device_init implementation
* 2012-10-20 Bernard add device check in register function,
* provided by Rob <rdent@iinet.net.au>
*/ */
#include <rtthread.h> #include <rtthread.h>
...@@ -31,6 +33,9 @@ rt_err_t rt_device_register(rt_device_t dev, const char *name, rt_uint16_t flags ...@@ -31,6 +33,9 @@ rt_err_t rt_device_register(rt_device_t dev, const char *name, rt_uint16_t flags
if (dev == RT_NULL) if (dev == RT_NULL)
return -RT_ERROR; return -RT_ERROR;
if (rt_device_find(name) != RT_NULL)
return -RT_ERROR;
rt_object_init(&(dev->parent), RT_Object_Class_Device, name); rt_object_init(&(dev->parent), RT_Object_Class_Device, name);
dev->flag = flags; dev->flag = flags;
...@@ -197,6 +202,8 @@ rt_err_t rt_device_open(rt_device_t dev, rt_uint16_t oflag) ...@@ -197,6 +202,8 @@ rt_err_t rt_device_open(rt_device_t dev, rt_uint16_t oflag)
/* if device is not initialized, initialize it. */ /* if device is not initialized, initialize it. */
if (!(dev->flag & RT_DEVICE_FLAG_ACTIVATED)) if (!(dev->flag & RT_DEVICE_FLAG_ACTIVATED))
{
if (dev->init != RT_NULL )
{ {
result = dev->init(dev); result = dev->init(dev);
if (result != RT_EOK) if (result != RT_EOK)
...@@ -206,10 +213,9 @@ rt_err_t rt_device_open(rt_device_t dev, rt_uint16_t oflag) ...@@ -206,10 +213,9 @@ rt_err_t rt_device_open(rt_device_t dev, rt_uint16_t oflag)
return result; return result;
} }
else
{
dev->flag |= RT_DEVICE_FLAG_ACTIVATED;
} }
dev->flag |= RT_DEVICE_FLAG_ACTIVATED;
} }
/* device is a stand alone device and opened */ /* device is a stand alone device and opened */
...@@ -225,7 +231,7 @@ rt_err_t rt_device_open(rt_device_t dev, rt_uint16_t oflag) ...@@ -225,7 +231,7 @@ rt_err_t rt_device_open(rt_device_t dev, rt_uint16_t oflag)
else else
{ {
/* no this interface in device driver */ /* no this interface in device driver */
result = -RT_ENOSYS; /* result = -RT_ENOSYS; not set errno */
} }
/* set open flag */ /* set open flag */
...@@ -259,7 +265,7 @@ rt_err_t rt_device_close(rt_device_t dev) ...@@ -259,7 +265,7 @@ rt_err_t rt_device_close(rt_device_t dev)
else else
{ {
/* no this interface in device driver */ /* no this interface in device driver */
result = -RT_ENOSYS; /* result = -RT_ENOSYS; not set errno */
} }
/* set open flag */ /* set open flag */
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册