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
This commit is contained in:
parent
2bbd1496a3
commit
e737514f86
28
src/device.c
28
src/device.c
|
@ -11,6 +11,8 @@
|
|||
* Date Author Notes
|
||||
* 2007-01-21 Bernard the first version
|
||||
* 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>
|
||||
|
@ -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)
|
||||
return -RT_ERROR;
|
||||
|
||||
if (rt_device_find(name) != RT_NULL)
|
||||
return -RT_ERROR;
|
||||
|
||||
rt_object_init(&(dev->parent), RT_Object_Class_Device, name);
|
||||
dev->flag = flags;
|
||||
|
||||
|
@ -198,18 +203,19 @@ rt_err_t rt_device_open(rt_device_t dev, rt_uint16_t oflag)
|
|||
/* if device is not initialized, initialize it. */
|
||||
if (!(dev->flag & RT_DEVICE_FLAG_ACTIVATED))
|
||||
{
|
||||
result = dev->init(dev);
|
||||
if (result != RT_EOK)
|
||||
if (dev->init != RT_NULL )
|
||||
{
|
||||
rt_kprintf("To initialize device:%s failed. The error code is %d\n",
|
||||
dev->parent.name, result);
|
||||
result = dev->init(dev);
|
||||
if (result != RT_EOK)
|
||||
{
|
||||
rt_kprintf("To initialize device:%s failed. The error code is %d\n",
|
||||
dev->parent.name, result);
|
||||
|
||||
return result;
|
||||
}
|
||||
else
|
||||
{
|
||||
dev->flag |= RT_DEVICE_FLAG_ACTIVATED;
|
||||
return result;
|
||||
}
|
||||
}
|
||||
|
||||
dev->flag |= RT_DEVICE_FLAG_ACTIVATED;
|
||||
}
|
||||
|
||||
/* 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)
|
|||
else
|
||||
{
|
||||
/* no this interface in device driver */
|
||||
result = -RT_ENOSYS;
|
||||
/* result = -RT_ENOSYS; not set errno */
|
||||
}
|
||||
|
||||
/* set open flag */
|
||||
|
@ -259,7 +265,7 @@ rt_err_t rt_device_close(rt_device_t dev)
|
|||
else
|
||||
{
|
||||
/* no this interface in device driver */
|
||||
result = -RT_ENOSYS;
|
||||
/* result = -RT_ENOSYS; not set errno */
|
||||
}
|
||||
|
||||
/* set open flag */
|
||||
|
|
Loading…
Reference in New Issue