diff --git a/bsp/stm32/libraries/HAL_Drivers/drv_qspi.c b/bsp/stm32/libraries/HAL_Drivers/drv_qspi.c index 3efaf2a4a6d91a5cb8c2f9f86e191b1f93993dff..a5b8eafa13e73fc91f369a2a2b388ca1c6b62f7c 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 12536c79529a87af02295705e0e6db932d1e962f..f3d5d5340148e190c569ccac4e25a591a7643d3c 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 7e3842d76ee543eb922f2928061ad10e90a247c2..7f7c2cedccdc3a19c96a92efe3b49c9c161ea895 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 eb6df1590ad30c25c8e6af8cb28d145538b253a4..d03e5007cfe8da36d8be51cb1b00525db7fcd23a 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 fca542d9bb6374417414f933ab943410011877bd..c5e769db10fd39a449728953be7cdc9d3830b590 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; }