From 6ac09a6db02e9772886d5f76163f534f959934cf Mon Sep 17 00:00:00 2001 From: liYangYang <941843540@qq.com> Date: Thu, 1 Sep 2022 12:36:45 +0800 Subject: [PATCH] [spi]fix some bug for rt_spi_sendrecv16 (#6360) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 修复 rt_spi_sendrecv16 api 对最高有效位(MSB or LSB)的处理不当造成的一些问题。 https://github.com/stm32duino/Arduino_Core_STM32/blob/main/libraries/SPI/src/SPI.cpp#L273 --- components/drivers/include/drivers/spi.h | 16 +++++----------- components/drivers/spi/spi_core.c | 23 +++++++++++++++++++++++ 2 files changed, 28 insertions(+), 11 deletions(-) diff --git a/components/drivers/include/drivers/spi.h b/components/drivers/include/drivers/spi.h index 5bc46b3c1b..0bcac3aac0 100644 --- a/components/drivers/include/drivers/spi.h +++ b/components/drivers/include/drivers/spi.h @@ -1,5 +1,5 @@ /* - * Copyright (c) 2006-2021, RT-Thread Development Team + * Copyright (c) 2006-2022, RT-Thread Development Team * * SPDX-License-Identifier: Apache-2.0 * @@ -7,6 +7,7 @@ * Date Author Notes * 2012-11-23 Bernard Add extern "C" * 2020-06-13 armink fix the 3 wires issue + * 2022-09-01 liYony fix api rt_spi_sendrecv16 about MSB and LSB bug */ #ifndef __SPI_H__ @@ -227,6 +228,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); + /** * This function transmits data to SPI device. * @@ -278,16 +282,6 @@ rt_inline rt_uint8_t rt_spi_sendrecv8(struct rt_spi_device *device, return value; } -rt_inline rt_uint16_t rt_spi_sendrecv16(struct rt_spi_device *device, - rt_uint16_t data) -{ - rt_uint16_t value = 0; - - rt_spi_send_then_recv(device, &data, 2, &value, 2); - - return value; -} - /** * This function appends a message to the SPI message list. * diff --git a/components/drivers/spi/spi_core.c b/components/drivers/spi/spi_core.c index ccfaecf387..6bc875fccd 100644 --- a/components/drivers/spi/spi_core.c +++ b/components/drivers/spi/spi_core.c @@ -309,6 +309,29 @@ __exit: return result; } +rt_uint16_t rt_spi_sendrecv16(struct rt_spi_device *device, + rt_uint16_t data) +{ + rt_uint16_t value = 0; + rt_uint16_t tmp; + + if (device->config.mode & RT_SPI_MSB) + { + tmp = ((data & 0xff00) >> 8) | ((data & 0x00ff) << 8); + data = tmp; + } + + rt_spi_send_then_recv(device, &data, 2, &value, 2); + + if (device->config.mode & RT_SPI_MSB) + { + tmp = ((value & 0xff00) >> 8) | ((value & 0x00ff) << 8); + value = tmp; + } + + return value; +} + struct rt_spi_message *rt_spi_transfer_message(struct rt_spi_device *device, struct rt_spi_message *message) {