summaryrefslogtreecommitdiff
path: root/components
diff options
context:
space:
mode:
authorliYangYang <[email protected]>2023-02-27 10:09:07 +0800
committerGitHub <[email protected]>2023-02-26 21:09:07 -0500
commit93d572dee6dadae93f9e5a4781a765cb4d198bad (patch)
treeff7c4d655aa37b97f6a65884375e6704ffd31513 /components
parentf295d3df7880f37eb7a7cd0fc18c2827e40420e6 (diff)
[spi][5.0.0] 修正SPI设备框架中,对函数返回值类型使用不恰当的情况 (#6937)
Diffstat (limited to 'components')
-rw-r--r--components/drivers/include/drivers/spi.h17
-rw-r--r--components/drivers/spi/spi-bit-ops.c12
-rw-r--r--components/drivers/spi/spi_core.c105
3 files changed, 67 insertions, 67 deletions
diff --git a/components/drivers/include/drivers/spi.h b/components/drivers/include/drivers/spi.h
index 7e3842d76e..7f7c2cedcc 100644
--- a/components/drivers/include/drivers/spi.h
+++ b/components/drivers/include/drivers/spi.h
@@ -1,5 +1,5 @@
/*
- * Copyright (c) 2006-2022, RT-Thread Development Team
+ * Copyright (c) 2006-2023, RT-Thread Development Team
*
* SPDX-License-Identifier: Apache-2.0
*
@@ -100,7 +100,7 @@ struct rt_spi_bus
struct rt_spi_ops
{
rt_err_t (*configure)(struct rt_spi_device *device, struct rt_spi_configuration *configuration);
- rt_uint32_t (*xfer)(struct rt_spi_device *device, struct rt_spi_message *message);
+ rt_ssize_t (*xfer)(struct rt_spi_device *device, struct rt_spi_message *message);
};
/**
@@ -237,8 +237,9 @@ rt_err_t rt_spi_send_then_send(struct rt_spi_device *device,
const void *send_buf2,
rt_size_t send_length2);
-rt_uint16_t rt_spi_sendrecv16(struct rt_spi_device *device,
- rt_uint16_t data);
+rt_err_t rt_spi_sendrecv16(struct rt_spi_device *device,
+ rt_uint16_t senddata,
+ rt_uint16_t *recvdata);
/**
* This function transmits data to SPI device.
@@ -250,10 +251,10 @@ rt_uint16_t rt_spi_sendrecv16(struct rt_spi_device *device,
*
* @return the actual length of transmitted.
*/
-rt_size_t rt_spi_transfer(struct rt_spi_device *device,
- const void *send_buf,
- void *recv_buf,
- rt_size_t length);
+rt_ssize_t rt_spi_transfer(struct rt_spi_device *device,
+ const void *send_buf,
+ void *recv_buf,
+ rt_size_t length);
/**
* This function transfers a message list to the SPI device.
diff --git a/components/drivers/spi/spi-bit-ops.c b/components/drivers/spi/spi-bit-ops.c
index eb6df1590a..d03e5007cf 100644
--- a/components/drivers/spi/spi-bit-ops.c
+++ b/components/drivers/spi/spi-bit-ops.c
@@ -1,5 +1,5 @@
/*
- * Copyright (c) 2006-2022, RT-Thread Development Team
+ * Copyright (c) 2006-2023, RT-Thread Development Team
*
* SPDX-License-Identifier: Apache-2.0
*
@@ -48,7 +48,7 @@ rt_inline void spi_delay2(struct rt_spi_bit_ops *ops)
#define MISO_IN(ops) DIR_MISO(ops, 1)
#define MISO_OUT(ops) DIR_MISO(ops, 0)
-rt_inline rt_size_t spi_xfer_4line_data8(struct rt_spi_bit_ops *ops,
+rt_inline rt_ssize_t spi_xfer_4line_data8(struct rt_spi_bit_ops *ops,
struct rt_spi_configuration *config,
const void *send_buf,
void *recv_buf,
@@ -111,7 +111,7 @@ rt_inline rt_size_t spi_xfer_4line_data8(struct rt_spi_bit_ops *ops,
return length;
}
-rt_inline rt_size_t spi_xfer_4line_data16(struct rt_spi_bit_ops *ops,
+rt_inline rt_ssize_t spi_xfer_4line_data16(struct rt_spi_bit_ops *ops,
struct rt_spi_configuration *config,
const void *send_buf,
void *recv_buf,
@@ -174,7 +174,7 @@ rt_inline rt_size_t spi_xfer_4line_data16(struct rt_spi_bit_ops *ops,
return length;
}
-rt_inline rt_size_t spi_xfer_3line_data8(struct rt_spi_bit_ops *ops,
+rt_inline rt_ssize_t spi_xfer_3line_data8(struct rt_spi_bit_ops *ops,
struct rt_spi_configuration *config,
const void *send_buf,
void *recv_buf,
@@ -275,7 +275,7 @@ rt_inline rt_size_t spi_xfer_3line_data8(struct rt_spi_bit_ops *ops,
return length;
}
-rt_inline rt_size_t spi_xfer_3line_data16(struct rt_spi_bit_ops *ops,
+rt_inline rt_ssize_t spi_xfer_3line_data16(struct rt_spi_bit_ops *ops,
struct rt_spi_configuration *config,
const void *send_buf,
void *recv_buf,
@@ -412,7 +412,7 @@ rt_err_t spi_bit_configure(struct rt_spi_device *device, struct rt_spi_configura
return RT_EOK;
}
-rt_uint32_t spi_bit_xfer(struct rt_spi_device *device, struct rt_spi_message *message)
+rt_ssize_t spi_bit_xfer(struct rt_spi_device *device, struct rt_spi_message *message)
{
struct rt_spi_bit_obj *obj = rt_container_of(device->bus, struct rt_spi_bit_obj, bus);
struct rt_spi_bit_ops *ops = obj->ops;
diff --git a/components/drivers/spi/spi_core.c b/components/drivers/spi/spi_core.c
index fca542d9bb..c5e769db10 100644
--- a/components/drivers/spi/spi_core.c
+++ b/components/drivers/spi/spi_core.c
@@ -15,6 +15,10 @@
#include <drivers/spi.h>
+#define DBG_TAG "spi.core"
+#define DBG_LVL DBG_INFO
+#include <rtdbg.h>
+
extern rt_err_t rt_spi_bus_device_init(struct rt_spi_bus *bus, const char *name);
extern rt_err_t rt_spidev_device_init(struct rt_spi_device *dev, const char *name);
@@ -148,7 +152,7 @@ rt_err_t rt_spi_send_then_send(struct rt_spi_device *device,
else
{
/* configure SPI bus failed */
- result = -RT_EIO;
+ LOG_E("SPI device %s configuration failed", device->parent.parent.name);
goto __exit;
}
}
@@ -162,9 +166,9 @@ rt_err_t rt_spi_send_then_send(struct rt_spi_device *device,
message.next = RT_NULL;
result = device->bus->ops->xfer(device, &message);
- if (result == 0)
+ if (result < 0)
{
- result = -RT_EIO;
+ LOG_E("SPI device %s transfer failed", device->parent.parent.name);
goto __exit;
}
@@ -177,9 +181,9 @@ rt_err_t rt_spi_send_then_send(struct rt_spi_device *device,
message.next = RT_NULL;
result = device->bus->ops->xfer(device, &message);
- if (result == 0)
+ if (result < 0)
{
- result = -RT_EIO;
+ LOG_E("SPI device %s transfer failed", device->parent.parent.name);
goto __exit;
}
@@ -223,7 +227,7 @@ rt_err_t rt_spi_send_then_recv(struct rt_spi_device *device,
else
{
/* configure SPI bus failed */
- result = -RT_EIO;
+ LOG_E("SPI device %s configuration failed", device->parent.parent.name);
goto __exit;
}
}
@@ -237,9 +241,9 @@ rt_err_t rt_spi_send_then_recv(struct rt_spi_device *device,
message.next = RT_NULL;
result = device->bus->ops->xfer(device, &message);
- if (result == 0)
+ if (result < 0)
{
- result = -RT_EIO;
+ LOG_E("SPI device %s transfer failed", device->parent.parent.name);
goto __exit;
}
@@ -252,9 +256,9 @@ rt_err_t rt_spi_send_then_recv(struct rt_spi_device *device,
message.next = RT_NULL;
result = device->bus->ops->xfer(device, &message);
- if (result == 0)
+ if (result < 0)
{
- result = -RT_EIO;
+ LOG_E("SPI device %s transfer failed", device->parent.parent.name);
goto __exit;
}
@@ -271,12 +275,12 @@ __exit:
return result;
}
-rt_size_t rt_spi_transfer(struct rt_spi_device *device,
- const void *send_buf,
- void *recv_buf,
- rt_size_t length)
+rt_ssize_t rt_spi_transfer(struct rt_spi_device *device,
+ const void *send_buf,
+ void *recv_buf,
+ rt_size_t length)
{
- rt_err_t result;
+ rt_ssize_t result;
struct rt_spi_message message;
RT_ASSERT(device != RT_NULL);
@@ -297,8 +301,7 @@ rt_size_t rt_spi_transfer(struct rt_spi_device *device,
else
{
/* configure SPI bus failed */
- rt_set_errno(-RT_EIO);
- result = 0;
+ LOG_E("SPI device %s configuration failed", device->parent.parent.name);
goto __exit;
}
}
@@ -313,16 +316,15 @@ rt_size_t rt_spi_transfer(struct rt_spi_device *device,
/* transfer message */
result = device->bus->ops->xfer(device, &message);
- if (result == 0)
+ if (result < 0)
{
- rt_set_errno(-RT_EIO);
+ LOG_E("SPI device %s transfer failed", device->parent.parent.name);
goto __exit;
}
}
else
{
- rt_set_errno(-RT_EIO);
- return 0;
+ return -RT_EIO;
}
__exit:
@@ -331,27 +333,32 @@ __exit:
return result;
}
-rt_uint16_t rt_spi_sendrecv16(struct rt_spi_device *device,
- rt_uint16_t data)
+rt_err_t rt_spi_sendrecv16(struct rt_spi_device *device,
+ rt_uint16_t senddata,
+ rt_uint16_t *recvdata)
{
- rt_uint16_t value = 0;
+ rt_err_t result;
rt_uint16_t tmp;
if (device->config.mode & RT_SPI_MSB)
{
- tmp = ((data & 0xff00) >> 8) | ((data & 0x00ff) << 8);
- data = tmp;
+ tmp = ((senddata & 0xff00) >> 8) | ((senddata & 0x00ff) << 8);
+ senddata = tmp;
}
- rt_spi_send_then_recv(device, &data, 2, &value, 2);
+ result = rt_spi_send_then_recv(device, &senddata, 2, recvdata, 2);
+ if(result != RT_EOK)
+ {
+ return result;
+ }
if (device->config.mode & RT_SPI_MSB)
{
- tmp = ((value & 0xff00) >> 8) | ((value & 0x00ff) << 8);
- value = tmp;
+ tmp = ((*recvdata & 0xff00) >> 8) | ((*recvdata & 0x00ff) << 8);
+ *recvdata = tmp;
}
- return value;
+ return result;
}
struct rt_spi_message *rt_spi_transfer_message(struct rt_spi_device *device,
@@ -370,14 +377,9 @@ struct rt_spi_message *rt_spi_transfer_message(struct rt_spi_device *device,
result = rt_mutex_take(&(device->bus->lock), RT_WAITING_FOREVER);
if (result != RT_EOK)
{
- rt_set_errno(-RT_EBUSY);
-
return index;
}
- /* reset errno */
- rt_set_errno(RT_EOK);
-
/* configure SPI bus */
if (device->bus->owner != device)
{
@@ -391,7 +393,6 @@ struct rt_spi_message *rt_spi_transfer_message(struct rt_spi_device *device,
else
{
/* configure SPI bus failed */
- rt_set_errno(-RT_EIO);
goto __exit;
}
}
@@ -401,9 +402,8 @@ struct rt_spi_message *rt_spi_transfer_message(struct rt_spi_device *device,
{
/* transmit SPI message */
result = device->bus->ops->xfer(device, index);
- if (result == 0)
+ if (result < 0)
{
- rt_set_errno(-RT_EIO);
break;
}
@@ -427,14 +427,9 @@ rt_err_t rt_spi_take_bus(struct rt_spi_device *device)
result = rt_mutex_take(&(device->bus->lock), RT_WAITING_FOREVER);
if (result != RT_EOK)
{
- rt_set_errno(-RT_EBUSY);
-
return -RT_EBUSY;
}
- /* reset errno */
- rt_set_errno(RT_EOK);
-
/* configure SPI bus */
if (device->bus->owner != device)
{
@@ -448,11 +443,9 @@ rt_err_t rt_spi_take_bus(struct rt_spi_device *device)
else
{
/* configure SPI bus failed */
- rt_set_errno(-RT_EIO);
- /* release lock */
rt_mutex_release(&(device->bus->lock));
- return -RT_EIO;
+ return result;
}
}
@@ -466,14 +459,12 @@ rt_err_t rt_spi_release_bus(struct rt_spi_device *device)
RT_ASSERT(device->bus->owner == device);
/* release lock */
- rt_mutex_release(&(device->bus->lock));
-
- return RT_EOK;
+ return rt_mutex_release(&(device->bus->lock));
}
rt_err_t rt_spi_take(struct rt_spi_device *device)
{
- rt_err_t result;
+ rt_ssize_t result;
struct rt_spi_message message;
RT_ASSERT(device != RT_NULL);
@@ -483,13 +474,17 @@ rt_err_t rt_spi_take(struct rt_spi_device *device)
message.cs_take = 1;
result = device->bus->ops->xfer(device, &message);
+ if(result < 0)
+ {
+ return (rt_err_t)result;
+ }
- return result;
+ return RT_EOK;
}
rt_err_t rt_spi_release(struct rt_spi_device *device)
{
- rt_err_t result;
+ rt_ssize_t result;
struct rt_spi_message message;
RT_ASSERT(device != RT_NULL);
@@ -499,6 +494,10 @@ rt_err_t rt_spi_release(struct rt_spi_device *device)
message.cs_release = 1;
result = device->bus->ops->xfer(device, &message);
+ if(result < 0)
+ {
+ return (rt_err_t)result;
+ }
- return result;
+ return RT_EOK;
}