未验证 提交 93d572de 编写于 作者: L liYangYang 提交者: GitHub

[spi][5.0.0] 修正SPI设备框架中,对函数返回值类型使用不恰当的情况 (#6937)

上级 f295d3df
...@@ -195,9 +195,9 @@ static void qspi_send_cmd(struct stm32_qspi_bus *qspi_bus, struct rt_qspi_messag ...@@ -195,9 +195,9 @@ static void qspi_send_cmd(struct stm32_qspi_bus *qspi_bus, struct rt_qspi_messag
HAL_QSPI_Command(&qspi_bus->QSPI_Handler, &Cmdhandler, 5000); HAL_QSPI_Command(&qspi_bus->QSPI_Handler, &Cmdhandler, 5000);
} }
static rt_uint32_t qspixfer(struct rt_spi_device *device, struct rt_spi_message *message) static rt_ssize_t qspixfer(struct rt_spi_device *device, struct rt_spi_message *message)
{ {
rt_size_t len = 0; rt_ssize_t result = 0;
RT_ASSERT(device != RT_NULL); RT_ASSERT(device != RT_NULL);
RT_ASSERT(device->bus != RT_NULL); RT_ASSERT(device->bus != RT_NULL);
...@@ -224,18 +224,19 @@ static rt_uint32_t qspixfer(struct rt_spi_device *device, struct rt_spi_message ...@@ -224,18 +224,19 @@ static rt_uint32_t qspixfer(struct rt_spi_device *device, struct rt_spi_message
{ {
if (HAL_QSPI_Transmit(&qspi_bus->QSPI_Handler, (rt_uint8_t *)sndb, 5000) == HAL_OK) if (HAL_QSPI_Transmit(&qspi_bus->QSPI_Handler, (rt_uint8_t *)sndb, 5000) == HAL_OK)
{ {
len = length; result = length;
} }
else else
{ {
LOG_E("QSPI send data failed(%d)!", qspi_bus->QSPI_Handler.ErrorCode); LOG_E("QSPI send data failed(%d)!", qspi_bus->QSPI_Handler.ErrorCode);
qspi_bus->QSPI_Handler.State = HAL_QSPI_STATE_READY; qspi_bus->QSPI_Handler.State = HAL_QSPI_STATE_READY;
result = -RT_ERROR;
goto __exit; goto __exit;
} }
} }
else else
{ {
len = 1; result = 1;
} }
} }
else if (rcvb)/* recv data */ else if (rcvb)/* recv data */
...@@ -247,7 +248,7 @@ static rt_uint32_t qspixfer(struct rt_spi_device *device, struct rt_spi_message ...@@ -247,7 +248,7 @@ static rt_uint32_t qspixfer(struct rt_spi_device *device, struct rt_spi_message
if (HAL_QSPI_Receive(&qspi_bus->QSPI_Handler, rcvb, 5000) == HAL_OK) if (HAL_QSPI_Receive(&qspi_bus->QSPI_Handler, rcvb, 5000) == HAL_OK)
#endif #endif
{ {
len = length; result = length;
#ifdef BSP_QSPI_USING_DMA #ifdef BSP_QSPI_USING_DMA
while (qspi_bus->QSPI_Handler.RxXferCount != 0); while (qspi_bus->QSPI_Handler.RxXferCount != 0);
#endif #endif
...@@ -256,6 +257,7 @@ static rt_uint32_t qspixfer(struct rt_spi_device *device, struct rt_spi_message ...@@ -256,6 +257,7 @@ static rt_uint32_t qspixfer(struct rt_spi_device *device, struct rt_spi_message
{ {
LOG_E("QSPI recv data failed(%d)!", qspi_bus->QSPI_Handler.ErrorCode); LOG_E("QSPI recv data failed(%d)!", qspi_bus->QSPI_Handler.ErrorCode);
qspi_bus->QSPI_Handler.State = HAL_QSPI_STATE_READY; qspi_bus->QSPI_Handler.State = HAL_QSPI_STATE_READY;
result = -RT_ERROR;
goto __exit; goto __exit;
} }
} }
...@@ -267,7 +269,7 @@ __exit: ...@@ -267,7 +269,7 @@ __exit:
rt_pin_write(device->parent.cs_pin, PIN_HIGH); rt_pin_write(device->parent.cs_pin, PIN_HIGH);
} }
#endif #endif
return len; return result;
} }
static rt_err_t qspi_configure(struct rt_spi_device *device, struct rt_spi_configuration *configuration) static rt_err_t qspi_configure(struct rt_spi_device *device, struct rt_spi_configuration *configuration)
......
...@@ -281,9 +281,9 @@ static rt_err_t stm32_spi_init(struct stm32_spi *spi_drv, struct rt_spi_configur ...@@ -281,9 +281,9 @@ static rt_err_t stm32_spi_init(struct stm32_spi *spi_drv, struct rt_spi_configur
return RT_EOK; return RT_EOK;
} }
static rt_uint32_t spixfer(struct rt_spi_device *device, struct rt_spi_message *message) static rt_ssize_t spixfer(struct rt_spi_device *device, struct rt_spi_message *message)
{ {
HAL_StatusTypeDef state; HAL_StatusTypeDef state = HAL_OK;;
rt_size_t message_length, already_send_length; rt_size_t message_length, already_send_length;
rt_uint16_t send_length; rt_uint16_t send_length;
rt_uint8_t *recv_buf; rt_uint8_t *recv_buf;
...@@ -438,6 +438,10 @@ static rt_uint32_t spixfer(struct rt_spi_device *device, struct rt_spi_message * ...@@ -438,6 +438,10 @@ static rt_uint32_t spixfer(struct rt_spi_device *device, struct rt_spi_message *
rt_pin_write(device->cs_pin, PIN_HIGH); rt_pin_write(device->cs_pin, PIN_HIGH);
} }
if(state != HAL_OK)
{
return -RT_ERROR;
}
return message->length; return message->length;
} }
......
/* /*
* Copyright (c) 2006-2022, RT-Thread Development Team * Copyright (c) 2006-2023, RT-Thread Development Team
* *
* SPDX-License-Identifier: Apache-2.0 * SPDX-License-Identifier: Apache-2.0
* *
...@@ -100,7 +100,7 @@ struct rt_spi_bus ...@@ -100,7 +100,7 @@ struct rt_spi_bus
struct rt_spi_ops struct rt_spi_ops
{ {
rt_err_t (*configure)(struct rt_spi_device *device, struct rt_spi_configuration *configuration); 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, ...@@ -237,8 +237,9 @@ rt_err_t rt_spi_send_then_send(struct rt_spi_device *device,
const void *send_buf2, const void *send_buf2,
rt_size_t send_length2); rt_size_t send_length2);
rt_uint16_t rt_spi_sendrecv16(struct rt_spi_device *device, rt_err_t rt_spi_sendrecv16(struct rt_spi_device *device,
rt_uint16_t data); rt_uint16_t senddata,
rt_uint16_t *recvdata);
/** /**
* This function transmits data to SPI device. * This function transmits data to SPI device.
...@@ -250,10 +251,10 @@ rt_uint16_t rt_spi_sendrecv16(struct rt_spi_device *device, ...@@ -250,10 +251,10 @@ rt_uint16_t rt_spi_sendrecv16(struct rt_spi_device *device,
* *
* @return the actual length of transmitted. * @return the actual length of transmitted.
*/ */
rt_size_t rt_spi_transfer(struct rt_spi_device *device, rt_ssize_t rt_spi_transfer(struct rt_spi_device *device,
const void *send_buf, const void *send_buf,
void *recv_buf, void *recv_buf,
rt_size_t length); rt_size_t length);
/** /**
* This function transfers a message list to the SPI device. * This function transfers a message list to the SPI device.
......
/* /*
* Copyright (c) 2006-2022, RT-Thread Development Team * Copyright (c) 2006-2023, RT-Thread Development Team
* *
* SPDX-License-Identifier: Apache-2.0 * SPDX-License-Identifier: Apache-2.0
* *
...@@ -48,7 +48,7 @@ rt_inline void spi_delay2(struct rt_spi_bit_ops *ops) ...@@ -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_IN(ops) DIR_MISO(ops, 1)
#define MISO_OUT(ops) DIR_MISO(ops, 0) #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, struct rt_spi_configuration *config,
const void *send_buf, const void *send_buf,
void *recv_buf, void *recv_buf,
...@@ -111,7 +111,7 @@ rt_inline rt_size_t spi_xfer_4line_data8(struct rt_spi_bit_ops *ops, ...@@ -111,7 +111,7 @@ rt_inline rt_size_t spi_xfer_4line_data8(struct rt_spi_bit_ops *ops,
return length; 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, struct rt_spi_configuration *config,
const void *send_buf, const void *send_buf,
void *recv_buf, void *recv_buf,
...@@ -174,7 +174,7 @@ rt_inline rt_size_t spi_xfer_4line_data16(struct rt_spi_bit_ops *ops, ...@@ -174,7 +174,7 @@ rt_inline rt_size_t spi_xfer_4line_data16(struct rt_spi_bit_ops *ops,
return length; 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, struct rt_spi_configuration *config,
const void *send_buf, const void *send_buf,
void *recv_buf, void *recv_buf,
...@@ -275,7 +275,7 @@ rt_inline rt_size_t spi_xfer_3line_data8(struct rt_spi_bit_ops *ops, ...@@ -275,7 +275,7 @@ rt_inline rt_size_t spi_xfer_3line_data8(struct rt_spi_bit_ops *ops,
return length; 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, struct rt_spi_configuration *config,
const void *send_buf, const void *send_buf,
void *recv_buf, void *recv_buf,
...@@ -412,7 +412,7 @@ rt_err_t spi_bit_configure(struct rt_spi_device *device, struct rt_spi_configura ...@@ -412,7 +412,7 @@ rt_err_t spi_bit_configure(struct rt_spi_device *device, struct rt_spi_configura
return RT_EOK; 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_obj *obj = rt_container_of(device->bus, struct rt_spi_bit_obj, bus);
struct rt_spi_bit_ops *ops = obj->ops; struct rt_spi_bit_ops *ops = obj->ops;
......
...@@ -15,6 +15,10 @@ ...@@ -15,6 +15,10 @@
#include <drivers/spi.h> #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_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); 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, ...@@ -148,7 +152,7 @@ rt_err_t rt_spi_send_then_send(struct rt_spi_device *device,
else else
{ {
/* configure SPI bus failed */ /* configure SPI bus failed */
result = -RT_EIO; LOG_E("SPI device %s configuration failed", device->parent.parent.name);
goto __exit; goto __exit;
} }
} }
...@@ -162,9 +166,9 @@ rt_err_t rt_spi_send_then_send(struct rt_spi_device *device, ...@@ -162,9 +166,9 @@ rt_err_t rt_spi_send_then_send(struct rt_spi_device *device,
message.next = RT_NULL; message.next = RT_NULL;
result = device->bus->ops->xfer(device, &message); 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; goto __exit;
} }
...@@ -177,9 +181,9 @@ rt_err_t rt_spi_send_then_send(struct rt_spi_device *device, ...@@ -177,9 +181,9 @@ rt_err_t rt_spi_send_then_send(struct rt_spi_device *device,
message.next = RT_NULL; message.next = RT_NULL;
result = device->bus->ops->xfer(device, &message); 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; goto __exit;
} }
...@@ -223,7 +227,7 @@ rt_err_t rt_spi_send_then_recv(struct rt_spi_device *device, ...@@ -223,7 +227,7 @@ rt_err_t rt_spi_send_then_recv(struct rt_spi_device *device,
else else
{ {
/* configure SPI bus failed */ /* configure SPI bus failed */
result = -RT_EIO; LOG_E("SPI device %s configuration failed", device->parent.parent.name);
goto __exit; goto __exit;
} }
} }
...@@ -237,9 +241,9 @@ rt_err_t rt_spi_send_then_recv(struct rt_spi_device *device, ...@@ -237,9 +241,9 @@ rt_err_t rt_spi_send_then_recv(struct rt_spi_device *device,
message.next = RT_NULL; message.next = RT_NULL;
result = device->bus->ops->xfer(device, &message); 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; goto __exit;
} }
...@@ -252,9 +256,9 @@ rt_err_t rt_spi_send_then_recv(struct rt_spi_device *device, ...@@ -252,9 +256,9 @@ rt_err_t rt_spi_send_then_recv(struct rt_spi_device *device,
message.next = RT_NULL; message.next = RT_NULL;
result = device->bus->ops->xfer(device, &message); 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; goto __exit;
} }
...@@ -271,12 +275,12 @@ __exit: ...@@ -271,12 +275,12 @@ __exit:
return result; return result;
} }
rt_size_t rt_spi_transfer(struct rt_spi_device *device, rt_ssize_t rt_spi_transfer(struct rt_spi_device *device,
const void *send_buf, const void *send_buf,
void *recv_buf, void *recv_buf,
rt_size_t length) rt_size_t length)
{ {
rt_err_t result; rt_ssize_t result;
struct rt_spi_message message; struct rt_spi_message message;
RT_ASSERT(device != RT_NULL); RT_ASSERT(device != RT_NULL);
...@@ -297,8 +301,7 @@ rt_size_t rt_spi_transfer(struct rt_spi_device *device, ...@@ -297,8 +301,7 @@ rt_size_t rt_spi_transfer(struct rt_spi_device *device,
else else
{ {
/* configure SPI bus failed */ /* configure SPI bus failed */
rt_set_errno(-RT_EIO); LOG_E("SPI device %s configuration failed", device->parent.parent.name);
result = 0;
goto __exit; goto __exit;
} }
} }
...@@ -313,16 +316,15 @@ rt_size_t rt_spi_transfer(struct rt_spi_device *device, ...@@ -313,16 +316,15 @@ rt_size_t rt_spi_transfer(struct rt_spi_device *device,
/* transfer message */ /* transfer message */
result = device->bus->ops->xfer(device, &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; goto __exit;
} }
} }
else else
{ {
rt_set_errno(-RT_EIO); return -RT_EIO;
return 0;
} }
__exit: __exit:
...@@ -331,27 +333,32 @@ __exit: ...@@ -331,27 +333,32 @@ __exit:
return result; return result;
} }
rt_uint16_t rt_spi_sendrecv16(struct rt_spi_device *device, rt_err_t rt_spi_sendrecv16(struct rt_spi_device *device,
rt_uint16_t data) rt_uint16_t senddata,
rt_uint16_t *recvdata)
{ {
rt_uint16_t value = 0; rt_err_t result;
rt_uint16_t tmp; rt_uint16_t tmp;
if (device->config.mode & RT_SPI_MSB) if (device->config.mode & RT_SPI_MSB)
{ {
tmp = ((data & 0xff00) >> 8) | ((data & 0x00ff) << 8); tmp = ((senddata & 0xff00) >> 8) | ((senddata & 0x00ff) << 8);
data = tmp; 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) if (device->config.mode & RT_SPI_MSB)
{ {
tmp = ((value & 0xff00) >> 8) | ((value & 0x00ff) << 8); tmp = ((*recvdata & 0xff00) >> 8) | ((*recvdata & 0x00ff) << 8);
value = tmp; *recvdata = tmp;
} }
return value; return result;
} }
struct rt_spi_message *rt_spi_transfer_message(struct rt_spi_device *device, 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, ...@@ -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); result = rt_mutex_take(&(device->bus->lock), RT_WAITING_FOREVER);
if (result != RT_EOK) if (result != RT_EOK)
{ {
rt_set_errno(-RT_EBUSY);
return index; return index;
} }
/* reset errno */
rt_set_errno(RT_EOK);
/* configure SPI bus */ /* configure SPI bus */
if (device->bus->owner != device) if (device->bus->owner != device)
{ {
...@@ -391,7 +393,6 @@ struct rt_spi_message *rt_spi_transfer_message(struct rt_spi_device *device, ...@@ -391,7 +393,6 @@ struct rt_spi_message *rt_spi_transfer_message(struct rt_spi_device *device,
else else
{ {
/* configure SPI bus failed */ /* configure SPI bus failed */
rt_set_errno(-RT_EIO);
goto __exit; goto __exit;
} }
} }
...@@ -401,9 +402,8 @@ struct rt_spi_message *rt_spi_transfer_message(struct rt_spi_device *device, ...@@ -401,9 +402,8 @@ struct rt_spi_message *rt_spi_transfer_message(struct rt_spi_device *device,
{ {
/* transmit SPI message */ /* transmit SPI message */
result = device->bus->ops->xfer(device, index); result = device->bus->ops->xfer(device, index);
if (result == 0) if (result < 0)
{ {
rt_set_errno(-RT_EIO);
break; break;
} }
...@@ -427,14 +427,9 @@ rt_err_t rt_spi_take_bus(struct rt_spi_device *device) ...@@ -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); result = rt_mutex_take(&(device->bus->lock), RT_WAITING_FOREVER);
if (result != RT_EOK) if (result != RT_EOK)
{ {
rt_set_errno(-RT_EBUSY);
return -RT_EBUSY; return -RT_EBUSY;
} }
/* reset errno */
rt_set_errno(RT_EOK);
/* configure SPI bus */ /* configure SPI bus */
if (device->bus->owner != device) if (device->bus->owner != device)
{ {
...@@ -448,11 +443,9 @@ rt_err_t rt_spi_take_bus(struct rt_spi_device *device) ...@@ -448,11 +443,9 @@ rt_err_t rt_spi_take_bus(struct rt_spi_device *device)
else else
{ {
/* configure SPI bus failed */ /* configure SPI bus failed */
rt_set_errno(-RT_EIO);
/* release lock */
rt_mutex_release(&(device->bus->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) ...@@ -466,14 +459,12 @@ rt_err_t rt_spi_release_bus(struct rt_spi_device *device)
RT_ASSERT(device->bus->owner == device); RT_ASSERT(device->bus->owner == device);
/* release lock */ /* release lock */
rt_mutex_release(&(device->bus->lock)); return rt_mutex_release(&(device->bus->lock));
return RT_EOK;
} }
rt_err_t rt_spi_take(struct rt_spi_device *device) rt_err_t rt_spi_take(struct rt_spi_device *device)
{ {
rt_err_t result; rt_ssize_t result;
struct rt_spi_message message; struct rt_spi_message message;
RT_ASSERT(device != RT_NULL); RT_ASSERT(device != RT_NULL);
...@@ -483,13 +474,17 @@ rt_err_t rt_spi_take(struct rt_spi_device *device) ...@@ -483,13 +474,17 @@ rt_err_t rt_spi_take(struct rt_spi_device *device)
message.cs_take = 1; message.cs_take = 1;
result = device->bus->ops->xfer(device, &message); 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 rt_spi_release(struct rt_spi_device *device)
{ {
rt_err_t result; rt_ssize_t result;
struct rt_spi_message message; struct rt_spi_message message;
RT_ASSERT(device != RT_NULL); RT_ASSERT(device != RT_NULL);
...@@ -499,6 +494,10 @@ rt_err_t rt_spi_release(struct rt_spi_device *device) ...@@ -499,6 +494,10 @@ rt_err_t rt_spi_release(struct rt_spi_device *device)
message.cs_release = 1; message.cs_release = 1;
result = device->bus->ops->xfer(device, &message); result = device->bus->ops->xfer(device, &message);
if(result < 0)
{
return (rt_err_t)result;
}
return result; return RT_EOK;
} }
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册