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
48
src/device.c
48
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,12 +33,15 @@ 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;
|
||||
|
||||
return RT_EOK;
|
||||
}
|
||||
RTM_EXPORT(rt_device_register);
|
||||
RTM_EXPORT(rt_device_register);
|
||||
|
||||
/**
|
||||
* This function removes a previously registered device driver
|
||||
|
@ -53,7 +58,7 @@ rt_err_t rt_device_unregister(rt_device_t dev)
|
|||
|
||||
return RT_EOK;
|
||||
}
|
||||
RTM_EXPORT(rt_device_unregister);
|
||||
RTM_EXPORT(rt_device_unregister);
|
||||
|
||||
/**
|
||||
* This function initializes all registered device driver
|
||||
|
@ -138,7 +143,7 @@ rt_device_t rt_device_find(const char *name)
|
|||
/* not found */
|
||||
return RT_NULL;
|
||||
}
|
||||
RTM_EXPORT(rt_device_find);
|
||||
RTM_EXPORT(rt_device_find);
|
||||
|
||||
/**
|
||||
* This function will initialize the specified device
|
||||
|
@ -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 */
|
||||
|
@ -234,7 +240,7 @@ rt_err_t rt_device_open(rt_device_t dev, rt_uint16_t oflag)
|
|||
|
||||
return result;
|
||||
}
|
||||
RTM_EXPORT(rt_device_open);
|
||||
RTM_EXPORT(rt_device_open);
|
||||
|
||||
/**
|
||||
* This function will close a device
|
||||
|
@ -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 */
|
||||
|
@ -268,7 +274,7 @@ rt_err_t rt_device_close(rt_device_t dev)
|
|||
|
||||
return result;
|
||||
}
|
||||
RTM_EXPORT(rt_device_close);
|
||||
RTM_EXPORT(rt_device_close);
|
||||
|
||||
/**
|
||||
* This function will read some data from a device.
|
||||
|
@ -300,7 +306,7 @@ rt_size_t rt_device_read(rt_device_t dev, rt_off_t pos, void *buffer, rt_size_t
|
|||
|
||||
return 0;
|
||||
}
|
||||
RTM_EXPORT(rt_device_read);
|
||||
RTM_EXPORT(rt_device_read);
|
||||
|
||||
/**
|
||||
* This function will write some data to a device.
|
||||
|
@ -332,7 +338,7 @@ rt_size_t rt_device_write(rt_device_t dev, rt_off_t pos, const void *buffer, rt_
|
|||
|
||||
return 0;
|
||||
}
|
||||
RTM_EXPORT(rt_device_write);
|
||||
RTM_EXPORT(rt_device_write);
|
||||
|
||||
/**
|
||||
* This function will perform a variety of control functions on devices.
|
||||
|
@ -358,7 +364,7 @@ rt_err_t rt_device_control(rt_device_t dev, rt_uint8_t cmd, void *arg)
|
|||
|
||||
return -RT_ENOSYS;
|
||||
}
|
||||
RTM_EXPORT(rt_device_control);
|
||||
RTM_EXPORT(rt_device_control);
|
||||
|
||||
/**
|
||||
* This function will set the indication callback function when device receives
|
||||
|
@ -377,7 +383,7 @@ rt_err_t rt_device_set_rx_indicate(rt_device_t dev, rt_err_t (*rx_ind)(rt_device
|
|||
|
||||
return RT_EOK;
|
||||
}
|
||||
RTM_EXPORT(rt_device_set_rx_indicate);
|
||||
RTM_EXPORT(rt_device_set_rx_indicate);
|
||||
|
||||
/**
|
||||
* This function will set the indication callback function when device has written
|
||||
|
@ -396,6 +402,6 @@ rt_err_t rt_device_set_tx_complete(rt_device_t dev, rt_err_t (*tx_done)(rt_devic
|
|||
|
||||
return RT_EOK;
|
||||
}
|
||||
RTM_EXPORT(rt_device_set_tx_complete);
|
||||
RTM_EXPORT(rt_device_set_tx_complete);
|
||||
|
||||
#endif
|
||||
|
|
Loading…
Reference in New Issue