|
1 | 1 | /* |
2 | | - * Copyright (c) 2006-2024, RT-Thread Development Team |
| 2 | + * Copyright (c) 2006-2025 RT-Thread Development Team |
3 | 3 | * |
4 | 4 | * SPDX-License-Identifier: Apache-2.0 |
5 | 5 | * |
|
26 | 26 | #include "drv_config.h" |
27 | 27 | #include <string.h> |
28 | 28 |
|
29 | | -// #define DRV_DEBUG |
| 29 | +/*#define DRV_DEBUG*/ |
30 | 30 | #define LOG_TAG "drv.spi" |
31 | 31 | #include <drv_log.h> |
32 | 32 |
|
@@ -360,9 +360,14 @@ static rt_ssize_t spixfer(struct rt_spi_device *device, struct rt_spi_message *m |
360 | 360 | rt_uint8_t *aligned_send_buf = RT_NULL; |
361 | 361 | rt_uint8_t *aligned_recv_buf = RT_NULL; |
362 | 362 |
|
363 | | - const rt_bool_t dma_eligible = (send_length >= DMA_TRANS_MIN_LEN); |
364 | | - const rt_bool_t use_tx_dma = dma_eligible && (spi_drv->spi_dma_flag & SPI_USING_TX_DMA_FLAG); |
365 | | - const rt_bool_t use_rx_dma = dma_eligible && (spi_drv->spi_dma_flag & SPI_USING_RX_DMA_FLAG); |
| 363 | + rt_bool_t dma_eligible = (send_length >= DMA_TRANS_MIN_LEN); |
| 364 | + rt_bool_t use_tx_dma = dma_eligible && (spi_drv->spi_dma_flag & SPI_USING_TX_DMA_FLAG); |
| 365 | + rt_bool_t use_rx_dma = dma_eligible && (spi_drv->spi_dma_flag & SPI_USING_RX_DMA_FLAG); |
| 366 | + |
| 367 | + if (!rt_scheduler_is_available()) |
| 368 | + { |
| 369 | + use_rx_dma = use_tx_dma = dma_eligible = RT_FALSE; |
| 370 | + } |
366 | 371 |
|
367 | 372 | if (dma_eligible) |
368 | 373 | { |
@@ -478,18 +483,21 @@ static rt_ssize_t spixfer(struct rt_spi_device *device, struct rt_spi_message *m |
478 | 483 | } |
479 | 484 | else |
480 | 485 | { |
481 | | - rt_uint32_t timeout = timeout_ms; |
482 | | - while (HAL_SPI_GetState(spi_handle) != HAL_SPI_STATE_READY) |
| 486 | + if (rt_scheduler_is_available()) |
483 | 487 | { |
484 | | - if(timeout-- > 0) |
485 | | - { |
486 | | - rt_thread_mdelay(1); |
487 | | - } |
488 | | - else |
| 488 | + rt_uint32_t timeout = timeout_ms; |
| 489 | + while (HAL_SPI_GetState(spi_handle) != HAL_SPI_STATE_READY) |
489 | 490 | { |
490 | | - LOG_E("timeout! SPI state did not become READY."); |
491 | | - state = HAL_TIMEOUT; |
492 | | - break; |
| 491 | + if(timeout-- > 0) |
| 492 | + { |
| 493 | + rt_thread_mdelay(1); |
| 494 | + } |
| 495 | + else |
| 496 | + { |
| 497 | + LOG_E("timeout! SPI state did not become READY."); |
| 498 | + state = HAL_TIMEOUT; |
| 499 | + break; |
| 500 | + } |
493 | 501 | } |
494 | 502 | } |
495 | 503 | } |
@@ -682,6 +690,47 @@ rt_err_t rt_hw_spi_device_attach(const char *bus_name, const char *device_name, |
682 | 690 | return result; |
683 | 691 | } |
684 | 692 |
|
| 693 | +/** |
| 694 | + * Detach the spi device from SPI bus. |
| 695 | + * |
| 696 | + * @param device_name the name of the spi device to be detached. |
| 697 | + */ |
| 698 | +rt_err_t rt_hw_spi_device_detach(const char *device_name) |
| 699 | +{ |
| 700 | + RT_ASSERT(device_name != RT_NULL); |
| 701 | + |
| 702 | + rt_err_t result; |
| 703 | + struct rt_spi_device *spi_device; |
| 704 | + |
| 705 | + rt_device_t device = rt_device_find(device_name); |
| 706 | + if (device == RT_NULL) |
| 707 | + { |
| 708 | + LOG_E("SPI device %s not found.", device_name); |
| 709 | + return -RT_ERROR; |
| 710 | + } |
| 711 | + |
| 712 | + if (device->type != RT_Device_Class_SPIDevice) |
| 713 | + { |
| 714 | + LOG_E("%s is not an SPI device.", device_name); |
| 715 | + return -RT_ERROR; |
| 716 | + } |
| 717 | + |
| 718 | + spi_device = (struct rt_spi_device *)device; |
| 719 | + |
| 720 | + result = rt_spi_bus_detach_device_cspin(spi_device); |
| 721 | + if (result != RT_EOK) |
| 722 | + { |
| 723 | + LOG_E("Failed to detach %s from its bus, error code: %d", device_name, result); |
| 724 | + return result; |
| 725 | + } |
| 726 | + |
| 727 | + rt_free(spi_device); |
| 728 | + |
| 729 | + LOG_D("SPI device %s has been detached.", device_name); |
| 730 | + |
| 731 | + return RT_EOK; |
| 732 | +} |
| 733 | + |
685 | 734 | #if defined(BSP_SPI1_TX_USING_DMA) || defined(BSP_SPI1_RX_USING_DMA) |
686 | 735 | void SPI1_IRQHandler(void) |
687 | 736 | { |
|
0 commit comments