Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[components][SPI][spi-bit-ops]修复可能的异常操作 #9177

Merged
merged 2 commits into from
Jul 12, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 2 additions & 32 deletions bsp/stm32/libraries/HAL_Drivers/drivers/drv_soft_spi.c
Original file line number Diff line number Diff line change
@@ -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
*
Expand Down Expand Up @@ -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);
Expand All @@ -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,
};

Expand Down
55 changes: 25 additions & 30 deletions components/drivers/spi/spi-bit-ops.c
Original file line number Diff line number Diff line change
@@ -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
*
Expand Down Expand Up @@ -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);
Expand All @@ -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 */
Expand All @@ -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 =
Expand All @@ -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);
}
Loading