summaryrefslogtreecommitdiff
path: root/src/device.c
diff options
context:
space:
mode:
authorWBI\nxf44444 <[email protected]>2022-01-26 01:43:05 +0800
committerWBI\nxf44444 <[email protected]>2022-01-26 01:43:05 +0800
commit8b0f50b7e211ee32ab08ba085ecd74588c3a3c63 (patch)
tree75440be5ecd30cc1403a9387851c0530ed057981 /src/device.c
parent3be35fecae513b07c35bd6f3992bf7824434bdd6 (diff)
parent32a5668593c578b85560338c29d76f3a252c8dbc (diff)
Update i2c driver, add read ACK and BUS status check
Diffstat (limited to 'src/device.c')
-rw-r--r--src/device.c157
1 files changed, 84 insertions, 73 deletions
diff --git a/src/device.c b/src/device.c
index a4f004fb69..9bfaf776d6 100644
--- a/src/device.c
+++ b/src/device.c
@@ -1,5 +1,5 @@
/*
- * Copyright (c) 2006-2018, RT-Thread Development Team
+ * Copyright (c) 2006-2021, RT-Thread Development Team
*
* SPDX-License-Identifier: Apache-2.0
*
@@ -12,12 +12,13 @@
* 2012-12-25 Bernard return RT_EOK if the device interface not exist.
* 2013-07-09 Grissiom add ref_count support
* 2016-04-02 Bernard fix the open_flag initialization issue.
+ * 2021-03-19 Meco Man remove rt_device_init_all()
*/
#include <rtthread.h>
-#if defined(RT_USING_POSIX)
+#ifdef RT_USING_POSIX_DEVIO
#include <rtdevice.h> /* for wqueue_init */
-#endif
+#endif /* RT_USING_POSIX_DEVIO */
#ifdef RT_USING_DEVICE
@@ -35,14 +36,16 @@
#define device_read (dev->read)
#define device_write (dev->write)
#define device_control (dev->control)
-#endif
+#endif /* RT_USING_DEVICE_OPS */
/**
- * This function registers a device driver with specified name.
+ * @brief This function registers a device driver with a specified name.
*
- * @param dev the pointer of device driver structure
- * @param name the device driver's name
- * @param flags the capabilities flag of device
+ * @param dev is the pointer of device driver structure.
+ *
+ * @param name is the device driver's name.
+ *
+ * @param flags is the capabilities flag of device.
*
* @return the error code, RT_EOK on initialization successfully.
*/
@@ -61,24 +64,25 @@ rt_err_t rt_device_register(rt_device_t dev,
dev->ref_count = 0;
dev->open_flag = 0;
-#if defined(RT_USING_POSIX)
+#ifdef RT_USING_POSIX_DEVIO
dev->fops = RT_NULL;
rt_wqueue_init(&(dev->wait_queue));
-#endif
+#endif /* RT_USING_POSIX_DEVIO */
return RT_EOK;
}
RTM_EXPORT(rt_device_register);
/**
- * This function removes a previously registered device driver
+ * @brief This function removes a previously registered device driver.
*
- * @param dev the pointer of device driver structure
+ * @param dev is the pointer of device driver structure.
*
* @return the error code, RT_EOK on successfully.
*/
rt_err_t rt_device_unregister(rt_device_t dev)
{
+ /* parameter check */
RT_ASSERT(dev != RT_NULL);
RT_ASSERT(rt_object_get_type(&dev->parent) == RT_Object_Class_Device);
RT_ASSERT(rt_object_is_systemobject(&dev->parent));
@@ -90,22 +94,9 @@ rt_err_t rt_device_unregister(rt_device_t dev)
RTM_EXPORT(rt_device_unregister);
/**
- * This function initializes all registered device driver
- *
- * @return the error code, RT_EOK on successfully.
- *
- * @deprecated since 1.2.x, this function is not needed because the initialization
- * of a device is performed when application opens it.
- */
-rt_err_t rt_device_init_all(void)
-{
- return RT_EOK;
-}
-
-/**
- * This function finds a device driver by specified name.
+ * @brief This function finds a device driver by specified name.
*
- * @param name the device driver's name
+ * @param name is the device driver's name.
*
* @return the registered device driver on successful, or RT_NULL on failure.
*/
@@ -117,10 +108,11 @@ RTM_EXPORT(rt_device_find);
#ifdef RT_USING_HEAP
/**
- * This function creates a device object with user data size.
+ * @brief This function creates a device object with user data size.
+ *
+ * @param type is the type of the device object.
*
- * @param type, the kind type of this device object.
- * @param attach_size, the size of user data.
+ * @param attach_size is the size of user data.
*
* @return the allocated device object, or RT_NULL when failed.
*/
@@ -146,12 +138,13 @@ rt_device_t rt_device_create(int type, int attach_size)
RTM_EXPORT(rt_device_create);
/**
- * This function destroy the specific device object.
+ * @brief This function destroy the specific device object.
*
- * @param dev, the specific device object.
+ * @param dev is a specific device object.
*/
void rt_device_destroy(rt_device_t dev)
{
+ /* parameter check */
RT_ASSERT(dev != RT_NULL);
RT_ASSERT(rt_object_get_type(&dev->parent) == RT_Object_Class_Device);
RT_ASSERT(rt_object_is_systemobject(&dev->parent) == RT_FALSE);
@@ -162,14 +155,14 @@ void rt_device_destroy(rt_device_t dev)
rt_free(dev);
}
RTM_EXPORT(rt_device_destroy);
-#endif
+#endif /* RT_USING_HEAP */
/**
- * This function will initialize the specified device
+ * @brief This function will initialize the specified device.
*
- * @param dev the pointer of device driver structure
+ * @param dev is the pointer of device driver structure.
*
- * @return the result
+ * @return the result, RT_EOK on successfully.
*/
rt_err_t rt_device_init(rt_device_t dev)
{
@@ -199,17 +192,19 @@ rt_err_t rt_device_init(rt_device_t dev)
}
/**
- * This function will open a device
+ * @brief This function will open a device.
*
- * @param dev the pointer of device driver structure
- * @param oflag the flags for device open
+ * @param dev is the pointer of device driver structure.
*
- * @return the result
+ * @param oflag is the flags for device open.
+ *
+ * @return the result, RT_EOK on successfully.
*/
rt_err_t rt_device_open(rt_device_t dev, rt_uint16_t oflag)
{
rt_err_t result = RT_EOK;
+ /* parameter check */
RT_ASSERT(dev != RT_NULL);
RT_ASSERT(rt_object_get_type(&dev->parent) == RT_Object_Class_Device);
@@ -265,16 +260,17 @@ rt_err_t rt_device_open(rt_device_t dev, rt_uint16_t oflag)
RTM_EXPORT(rt_device_open);
/**
- * This function will close a device
+ * @brief This function will close a device.
*
- * @param dev the pointer of device driver structure
+ * @param dev is the pointer of device driver structure.
*
- * @return the result
+ * @return the result, RT_EOK on successfully.
*/
rt_err_t rt_device_close(rt_device_t dev)
{
rt_err_t result = RT_EOK;
+ /* parameter check */
RT_ASSERT(dev != RT_NULL);
RT_ASSERT(rt_object_get_type(&dev->parent) == RT_Object_Class_Device);
@@ -301,12 +297,15 @@ rt_err_t rt_device_close(rt_device_t dev)
RTM_EXPORT(rt_device_close);
/**
- * This function will read some data from a device.
+ * @brief This function will read some data from a device.
+ *
+ * @param dev is the pointer of device driver structure.
+ *
+ * @param pos is the position when reading.
*
- * @param dev the pointer of device driver structure
- * @param pos the position of reading
- * @param buffer the data buffer to save read data
- * @param size the size of buffer
+ * @param buffer is a data buffer to save the read data.
+ *
+ * @param size is the size of buffer.
*
* @return the actually read size on successful, otherwise negative returned.
*
@@ -317,6 +316,7 @@ rt_size_t rt_device_read(rt_device_t dev,
void *buffer,
rt_size_t size)
{
+ /* parameter check */
RT_ASSERT(dev != RT_NULL);
RT_ASSERT(rt_object_get_type(&dev->parent) == RT_Object_Class_Device);
@@ -340,12 +340,15 @@ rt_size_t rt_device_read(rt_device_t dev,
RTM_EXPORT(rt_device_read);
/**
- * This function will write some data to a device.
+ * @brief This function will write some data to a device.
+ *
+ * @param dev is the pointer of device driver structure.
+ *
+ * @param pos is the position when writing.
*
- * @param dev the pointer of device driver structure
- * @param pos the position of written
- * @param buffer the data buffer to be written to device
- * @param size the size of buffer
+ * @param buffer is the data buffer to be written to device.
+ *
+ * @param size is the size of buffer.
*
* @return the actually written size on successful, otherwise negative returned.
*
@@ -356,6 +359,7 @@ rt_size_t rt_device_write(rt_device_t dev,
const void *buffer,
rt_size_t size)
{
+ /* parameter check */
RT_ASSERT(dev != RT_NULL);
RT_ASSERT(rt_object_get_type(&dev->parent) == RT_Object_Class_Device);
@@ -379,16 +383,19 @@ rt_size_t rt_device_write(rt_device_t dev,
RTM_EXPORT(rt_device_write);
/**
- * This function will perform a variety of control functions on devices.
+ * @brief This function will perform a variety of control functions on devices.
+ *
+ * @param dev is the pointer of device driver structure.
+ *
+ * @param cmd is the command sent to device.
*
- * @param dev the pointer of device driver structure
- * @param cmd the command sent to device
- * @param arg the argument of command
+ * @param arg is the argument of command.
*
- * @return the result
+ * @return the result, -RT_ENOSYS for failed.
*/
rt_err_t rt_device_control(rt_device_t dev, int cmd, void *arg)
{
+ /* parameter check */
RT_ASSERT(dev != RT_NULL);
RT_ASSERT(rt_object_get_type(&dev->parent) == RT_Object_Class_Device);
@@ -403,18 +410,20 @@ rt_err_t rt_device_control(rt_device_t dev, int cmd, void *arg)
RTM_EXPORT(rt_device_control);
/**
- * This function will set the reception indication callback function. This callback function
- * is invoked when this device receives data.
+ * @brief This function will set the reception indication callback function. This callback function
+ * is invoked when this device receives data.
*
- * @param dev the pointer of device driver structure
- * @param rx_ind the indication callback function
+ * @param dev is the pointer of device driver structure.
+ *
+ * @param rx_ind is the indication callback function.
*
* @return RT_EOK
*/
-rt_err_t
-rt_device_set_rx_indicate(rt_device_t dev,
- rt_err_t (*rx_ind)(rt_device_t dev, rt_size_t size))
+rt_err_t rt_device_set_rx_indicate(rt_device_t dev,
+ rt_err_t (*rx_ind)(rt_device_t dev,
+ rt_size_t size))
{
+ /* parameter check */
RT_ASSERT(dev != RT_NULL);
RT_ASSERT(rt_object_get_type(&dev->parent) == RT_Object_Class_Device);
@@ -425,18 +434,20 @@ rt_device_set_rx_indicate(rt_device_t dev,
RTM_EXPORT(rt_device_set_rx_indicate);
/**
- * This function will set the indication callback function when device has
- * written data to physical hardware.
+ * @brief This function will set a callback function. The callback function
+ * will be called when device has written data to physical hardware.
+ *
+ * @param dev is the pointer of device driver structure.
*
- * @param dev the pointer of device driver structure
- * @param tx_done the indication callback function
+ * @param tx_done is the indication callback function.
*
* @return RT_EOK
*/
-rt_err_t
-rt_device_set_tx_complete(rt_device_t dev,
- rt_err_t (*tx_done)(rt_device_t dev, void *buffer))
+rt_err_t rt_device_set_tx_complete(rt_device_t dev,
+ rt_err_t (*tx_done)(rt_device_t dev,
+ void *buffer))
{
+ /* parameter check */
RT_ASSERT(dev != RT_NULL);
RT_ASSERT(rt_object_get_type(&dev->parent) == RT_Object_Class_Device);
@@ -446,4 +457,4 @@ rt_device_set_tx_complete(rt_device_t dev,
}
RTM_EXPORT(rt_device_set_tx_complete);
-#endif
+#endif /* RT_USING_DEVICE */