Skip to content

Commit 64b57dc

Browse files
committed
[components][SPI][spi-bit-ops]修复可能的异常操作
* 移除初始化时未进行引脚初始化就进行引脚设置可能导致的异常 * CS引脚配置判断完善 * xfer返回值优化
1 parent f1832fe commit 64b57dc

File tree

1 file changed

+25
-30
lines changed

1 file changed

+25
-30
lines changed

components/drivers/spi/spi-bit-ops.c

Lines changed: 25 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2006-2023, RT-Thread Development Team
2+
* Copyright (c) 2006-2024, RT-Thread Development Team
33
*
44
* SPDX-License-Identifier: Apache-2.0
55
*
@@ -423,6 +423,7 @@ rt_ssize_t spi_bit_xfer(struct rt_spi_device *device, struct rt_spi_message *mes
423423
struct rt_spi_bit_ops *ops = obj->ops;
424424
struct rt_spi_configuration *config = &obj->config;
425425
rt_base_t cs_pin = device->cs_pin;
426+
rt_ssize_t length = 0;
426427

427428
RT_ASSERT(device != NULL);
428429
RT_ASSERT(message != NULL);
@@ -443,10 +444,17 @@ rt_ssize_t spi_bit_xfer(struct rt_spi_device *device, struct rt_spi_message *mes
443444
#endif
444445

445446
/* take CS */
446-
if (message->cs_take && (cs_pin != PIN_NONE))
447+
if (message->cs_take && !(device->config.mode & RT_SPI_NO_CS) && (cs_pin != PIN_NONE))
447448
{
448449
LOG_I("spi take cs\n");
449-
rt_pin_write(cs_pin, PIN_LOW);
450+
if (device->config.mode & RT_SPI_CS_HIGH)
451+
{
452+
rt_pin_write(cs_pin, PIN_HIGH);
453+
}
454+
else
455+
{
456+
rt_pin_write(cs_pin, PIN_LOW);
457+
}
450458
spi_delay(ops);
451459

452460
/* spi phase */
@@ -461,50 +469,41 @@ rt_ssize_t spi_bit_xfer(struct rt_spi_device *device, struct rt_spi_message *mes
461469
{
462470
if (config->data_width <= 8)
463471
{
464-
spi_xfer_3line_data8(ops,
465-
config,
466-
message->send_buf,
467-
message->recv_buf,
468-
message->length);
472+
length = spi_xfer_3line_data8(ops, config, message->send_buf, message->recv_buf, message->length);
469473
}
470474
else if (config->data_width <= 16)
471475
{
472-
spi_xfer_3line_data16(ops,
473-
config,
474-
message->send_buf,
475-
message->recv_buf,
476-
message->length);
476+
length = spi_xfer_3line_data16(ops, config, message->send_buf, message->recv_buf, message->length);
477477
}
478478
}
479479
else
480480
{
481481
if (config->data_width <= 8)
482482
{
483-
spi_xfer_4line_data8(ops,
484-
config,
485-
message->send_buf,
486-
message->recv_buf,
487-
message->length);
483+
length = spi_xfer_4line_data8(ops, config, message->send_buf, message->recv_buf, message->length);
488484
}
489485
else if (config->data_width <= 16)
490486
{
491-
spi_xfer_4line_data16(ops,
492-
config,
493-
message->send_buf,
494-
message->recv_buf,
495-
message->length);
487+
length = spi_xfer_4line_data16(ops, config, message->send_buf, message->recv_buf, message->length);
496488
}
497489
}
498490

499491
/* release CS */
500-
if (message->cs_release && (cs_pin != PIN_NONE))
492+
if (message->cs_take && !(device->config.mode & RT_SPI_NO_CS) && (cs_pin != PIN_NONE))
501493
{
502494
spi_delay(ops);
503-
rt_pin_write(cs_pin, PIN_HIGH);
495+
if (device->config.mode & RT_SPI_CS_HIGH)
496+
{
497+
rt_pin_write(cs_pin, PIN_LOW);
498+
}
499+
else
500+
{
501+
rt_pin_write(cs_pin, PIN_HIGH);
502+
}
504503
LOG_I("spi release cs\n");
505504
}
506505

507-
return message->length;
506+
return length;
508507
}
509508

510509
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,
522521
obj->config.max_hz = 1 * 1000 * 1000;
523522
obj->config.mode = RT_SPI_MASTER | RT_SPI_MSB | RT_SPI_MODE_0;
524523

525-
/* idle status */
526-
if (obj->config.mode & RT_SPI_CPOL) SCLK_H(ops);
527-
else SCLK_L(ops);
528-
529524
return rt_spi_bus_register(&obj->bus, bus_name, &spi_bit_bus_ops);
530525
}

0 commit comments

Comments
 (0)