From 4ee8b07e9cb134b52127b12b2d1cd9d87e566d67 Mon Sep 17 00:00:00 2001 From: wdfk-prog <1425075683@qq.com> Date: Tue, 2 Jul 2024 09:27:13 +0800 Subject: [PATCH 1/2] =?UTF-8?q?[components][SPI][spi-bit-ops]=E4=BF=AE?= =?UTF-8?q?=E5=A4=8D=E5=8F=AF=E8=83=BD=E7=9A=84=E5=BC=82=E5=B8=B8=E6=93=8D?= =?UTF-8?q?=E4=BD=9C=20*=20=E7=A7=BB=E9=99=A4=E5=88=9D=E5=A7=8B=E5=8C=96?= =?UTF-8?q?=E6=97=B6=E6=9C=AA=E8=BF=9B=E8=A1=8C=E5=BC=95=E8=84=9A=E5=88=9D?= =?UTF-8?q?=E5=A7=8B=E5=8C=96=E5=B0=B1=E8=BF=9B=E8=A1=8C=E5=BC=95=E8=84=9A?= =?UTF-8?q?=E8=AE=BE=E7=BD=AE=E5=8F=AF=E8=83=BD=E5=AF=BC=E8=87=B4=E7=9A=84?= =?UTF-8?q?=E5=BC=82=E5=B8=B8=20*=20CS=E5=BC=95=E8=84=9A=E9=85=8D=E7=BD=AE?= =?UTF-8?q?=E5=88=A4=E6=96=AD=E5=AE=8C=E5=96=84=20*=20xfer=E8=BF=94?= =?UTF-8?q?=E5=9B=9E=E5=80=BC=E4=BC=98=E5=8C=96?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- components/drivers/spi/spi-bit-ops.c | 55 +++++++++++++--------------- 1 file changed, 25 insertions(+), 30 deletions(-) diff --git a/components/drivers/spi/spi-bit-ops.c b/components/drivers/spi/spi-bit-ops.c index f551741873b..679ee44f0cb 100644 --- a/components/drivers/spi/spi-bit-ops.c +++ b/components/drivers/spi/spi-bit-ops.c @@ -1,5 +1,5 @@ /* - * Copyright (c) 2006-2023, RT-Thread Development Team + * Copyright (c) 2006-2024, RT-Thread Development Team * * SPDX-License-Identifier: Apache-2.0 * @@ -423,6 +423,7 @@ rt_ssize_t spi_bit_xfer(struct rt_spi_device *device, struct rt_spi_message *mes struct rt_spi_bit_ops *ops = obj->ops; struct rt_spi_configuration *config = &obj->config; rt_base_t cs_pin = device->cs_pin; + rt_ssize_t length = 0; RT_ASSERT(device != NULL); RT_ASSERT(message != NULL); @@ -443,10 +444,17 @@ rt_ssize_t spi_bit_xfer(struct rt_spi_device *device, struct rt_spi_message *mes #endif /* take CS */ - if (message->cs_take && (cs_pin != PIN_NONE)) + if (message->cs_take && !(device->config.mode & RT_SPI_NO_CS) && (cs_pin != PIN_NONE)) { LOG_I("spi take cs\n"); - rt_pin_write(cs_pin, PIN_LOW); + if (device->config.mode & RT_SPI_CS_HIGH) + { + rt_pin_write(cs_pin, PIN_HIGH); + } + else + { + rt_pin_write(cs_pin, PIN_LOW); + } spi_delay(ops); /* spi phase */ @@ -461,50 +469,41 @@ rt_ssize_t spi_bit_xfer(struct rt_spi_device *device, struct rt_spi_message *mes { if (config->data_width <= 8) { - spi_xfer_3line_data8(ops, - config, - message->send_buf, - message->recv_buf, - message->length); + length = spi_xfer_3line_data8(ops, config, message->send_buf, message->recv_buf, message->length); } else if (config->data_width <= 16) { - spi_xfer_3line_data16(ops, - config, - message->send_buf, - message->recv_buf, - message->length); + length = spi_xfer_3line_data16(ops, config, message->send_buf, message->recv_buf, message->length); } } else { if (config->data_width <= 8) { - spi_xfer_4line_data8(ops, - config, - message->send_buf, - message->recv_buf, - message->length); + length = spi_xfer_4line_data8(ops, config, message->send_buf, message->recv_buf, message->length); } else if (config->data_width <= 16) { - spi_xfer_4line_data16(ops, - config, - message->send_buf, - message->recv_buf, - message->length); + length = spi_xfer_4line_data16(ops, config, message->send_buf, message->recv_buf, message->length); } } /* release CS */ - if (message->cs_release && (cs_pin != PIN_NONE)) + if (message->cs_take && !(device->config.mode & RT_SPI_NO_CS) && (cs_pin != PIN_NONE)) { spi_delay(ops); - rt_pin_write(cs_pin, PIN_HIGH); + if (device->config.mode & RT_SPI_CS_HIGH) + { + rt_pin_write(cs_pin, PIN_LOW); + } + else + { + rt_pin_write(cs_pin, PIN_HIGH); + } LOG_I("spi release cs\n"); } - return message->length; + return length; } static const struct rt_spi_ops spi_bit_bus_ops = @@ -522,9 +521,5 @@ rt_err_t rt_spi_bit_add_bus(struct rt_spi_bit_obj *obj, obj->config.max_hz = 1 * 1000 * 1000; obj->config.mode = RT_SPI_MASTER | RT_SPI_MSB | RT_SPI_MODE_0; - /* idle status */ - if (obj->config.mode & RT_SPI_CPOL) SCLK_H(ops); - else SCLK_L(ops); - return rt_spi_bus_register(&obj->bus, bus_name, &spi_bit_bus_ops); } From 1e675f7526e529c227cf5d8ffaa990a545da6d85 Mon Sep 17 00:00:00 2001 From: wdfk-prog <1425075683@qq.com> Date: Tue, 2 Jul 2024 09:27:58 +0800 Subject: [PATCH 2/2] =?UTF-8?q?[STM32][SPI]=E7=A7=BB=E9=99=A4=E5=86=85?= =?UTF-8?q?=E9=83=A8=E7=BC=96=E5=86=99=E7=9A=84=E5=BB=B6=E6=97=B6=E5=87=BD?= =?UTF-8?q?=E6=95=B0=E4=BD=BF=E7=94=A8=E7=BB=9F=E4=B8=80=E5=BB=B6=E6=97=B6?= =?UTF-8?q?=E6=8E=A5=E5=8F=A3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../HAL_Drivers/drivers/drv_soft_spi.c | 34 ++----------------- 1 file changed, 2 insertions(+), 32 deletions(-) diff --git a/bsp/stm32/libraries/HAL_Drivers/drivers/drv_soft_spi.c b/bsp/stm32/libraries/HAL_Drivers/drivers/drv_soft_spi.c index 912e7098204..ff9e12473d8 100644 --- a/bsp/stm32/libraries/HAL_Drivers/drivers/drv_soft_spi.c +++ b/bsp/stm32/libraries/HAL_Drivers/drivers/drv_soft_spi.c @@ -1,5 +1,5 @@ /* - * Copyright (c) 2006-2023, RT-Thread Development Team + * Copyright (c) 2006-2024, RT-Thread Development Team * * SPDX-License-Identifier: Apache-2.0 * @@ -156,36 +156,6 @@ void stm32_dir_miso(void *data, rt_int32_t state) } } -static void stm32_udelay(rt_uint32_t us) -{ - rt_uint32_t ticks; - rt_uint32_t told, tnow, tcnt = 0; - rt_uint32_t reload = SysTick->LOAD; - - ticks = us * reload / (1000000UL / RT_TICK_PER_SECOND); - told = SysTick->VAL; - while (1) - { - tnow = SysTick->VAL; - if (tnow != told) - { - if (tnow < told) - { - tcnt += told - tnow; - } - else - { - tcnt += reload - tnow + told; - } - told = tnow; - if (tcnt >= ticks) - { - break; - } - } - } -} - static void stm32_pin_init(void) { rt_size_t obj_num = sizeof(spi_obj) / sizeof(struct stm32_soft_spi); @@ -209,7 +179,7 @@ static struct rt_spi_bit_ops stm32_soft_spi_ops = .get_miso = stm32_get_miso, .dir_mosi = stm32_dir_mosi, .dir_miso = stm32_dir_miso, - .udelay = stm32_udelay, + .udelay = rt_hw_us_delay, .delay_us = 1, };