From 93d572dee6dadae93f9e5a4781a765cb4d198bad Mon Sep 17 00:00:00 2001 From: liYangYang <941843540@qq.com> Date: Mon, 27 Feb 2023 10:09:07 +0800 Subject: [PATCH] =?UTF-8?q?[spi][5.0.0]=20=E4=BF=AE=E6=AD=A3SPI=E8=AE=BE?= =?UTF-8?q?=E5=A4=87=E6=A1=86=E6=9E=B6=E4=B8=AD=EF=BC=8C=E5=AF=B9=E5=87=BD?= =?UTF-8?q?=E6=95=B0=E8=BF=94=E5=9B=9E=E5=80=BC=E7=B1=BB=E5=9E=8B=E4=BD=BF?= =?UTF-8?q?=E7=94=A8=E4=B8=8D=E6=81=B0=E5=BD=93=E7=9A=84=E6=83=85=E5=86=B5?= =?UTF-8?q?=20(#6937)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- bsp/stm32/libraries/HAL_Drivers/drv_qspi.c | 14 +-- bsp/stm32/libraries/HAL_Drivers/drv_spi.c | 8 +- components/drivers/include/drivers/spi.h | 17 ++-- components/drivers/spi/spi-bit-ops.c | 12 +-- components/drivers/spi/spi_core.c | 105 ++++++++++----------- 5 files changed, 81 insertions(+), 75 deletions(-) diff --git a/bsp/stm32/libraries/HAL_Drivers/drv_qspi.c b/bsp/stm32/libraries/HAL_Drivers/drv_qspi.c index 3efaf2a4a6..a5b8eafa13 100644 --- a/bsp/stm32/libraries/HAL_Drivers/drv_qspi.c +++ b/bsp/stm32/libraries/HAL_Drivers/drv_qspi.c @@ -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); } -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->bus != RT_NULL); @@ -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) { - len = length; + result = length; } else { LOG_E("QSPI send data failed(%d)!", qspi_bus->QSPI_Handler.ErrorCode); qspi_bus->QSPI_Handler.State = HAL_QSPI_STATE_READY; + result = -RT_ERROR; goto __exit; } } else { - len = 1; + result = 1; } } else if (rcvb)/* recv data */ @@ -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) #endif { - len = length; + result = length; #ifdef BSP_QSPI_USING_DMA while (qspi_bus->QSPI_Handler.RxXferCount != 0); #endif @@ -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); qspi_bus->QSPI_Handler.State = HAL_QSPI_STATE_READY; + result = -RT_ERROR; goto __exit; } } @@ -267,7 +269,7 @@ __exit: rt_pin_write(device->parent.cs_pin, PIN_HIGH); } #endif - return len; + return result; } static rt_err_t qspi_configure(struct rt_spi_device *device, struct rt_spi_configuration *configuration) diff --git a/bsp/stm32/libraries/HAL_Drivers/drv_spi.c b/bsp/stm32/libraries/HAL_Drivers/drv_spi.c index 12536c7952..f3d5d53401 100644 --- a/bsp/stm32/libraries/HAL_Drivers/drv_spi.c +++ b/bsp/stm32/libraries/HAL_Drivers/drv_spi.c @@ -281,9 +281,9 @@ static rt_err_t stm32_spi_init(struct stm32_spi *spi_drv, struct rt_spi_configur 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_uint16_t send_length; rt_uint8_t *recv_buf; @@ -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); } + if(state != HAL_OK) + { + return -RT_ERROR; + } return message->length; } 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 +#define DBG_TAG "spi.core" +#define DBG_LVL DBG_INFO +#include + 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; } -- GitLab