Skip to content

Commit eaf1198

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

File tree

1 file changed

+18
-8
lines changed

1 file changed

+18
-8
lines changed

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

Lines changed: 18 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -443,10 +443,17 @@ rt_ssize_t spi_bit_xfer(struct rt_spi_device *device, struct rt_spi_message *mes
443443
#endif
444444

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

452459
/* spi phase */
@@ -497,10 +504,17 @@ rt_ssize_t spi_bit_xfer(struct rt_spi_device *device, struct rt_spi_message *mes
497504
}
498505

499506
/* release CS */
500-
if (message->cs_release && (cs_pin != PIN_NONE))
507+
if (message->cs_take && !(device->config.mode & RT_SPI_NO_CS) && (cs_pin != PIN_NONE))
501508
{
502509
spi_delay(ops);
503-
rt_pin_write(cs_pin, PIN_HIGH);
510+
if (device->config.mode & RT_SPI_CS_HIGH)
511+
{
512+
rt_pin_write(cs_pin, PIN_LOW);
513+
}
514+
else
515+
{
516+
rt_pin_write(cs_pin, PIN_HIGH);
517+
}
504518
LOG_I("spi release cs\n");
505519
}
506520

@@ -522,9 +536,5 @@ rt_err_t rt_spi_bit_add_bus(struct rt_spi_bit_obj *obj,
522536
obj->config.max_hz = 1 * 1000 * 1000;
523537
obj->config.mode = RT_SPI_MASTER | RT_SPI_MSB | RT_SPI_MODE_0;
524538

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

0 commit comments

Comments
 (0)