From e3a09b27916af61542abff58013473ca432eed9d Mon Sep 17 00:00:00 2001 From: liYangYang <941843540@qq.com> Date: Wed, 7 Dec 2022 01:52:37 +0800 Subject: [PATCH] =?UTF-8?q?[STM32][SPI]=E4=BC=98=E5=8C=96APBx=E6=97=B6?= =?UTF-8?q?=E9=92=9F=E9=85=8D=E7=BD=AE=E4=BB=A3=E7=A0=81=20(#6698)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * [STM32][SPI]优化APBx时钟配置代码 --- bsp/stm32/libraries/HAL_Drivers/drv_spi.c | 35 +++-------------------- 1 file changed, 4 insertions(+), 31 deletions(-) diff --git a/bsp/stm32/libraries/HAL_Drivers/drv_spi.c b/bsp/stm32/libraries/HAL_Drivers/drv_spi.c index 28616c5651..db0d63e534 100644 --- a/bsp/stm32/libraries/HAL_Drivers/drv_spi.c +++ b/bsp/stm32/libraries/HAL_Drivers/drv_spi.c @@ -142,41 +142,14 @@ static rt_err_t stm32_spi_init(struct stm32_spi *spi_drv, struct rt_spi_configur uint32_t SPI_APB_CLOCK; - /* special series */ -#if defined(SOC_SERIES_STM32F0) || defined(SOC_SERIES_STM32G0) +/* Some series may only have APBPERIPH_BASE, but don't have HAL_RCC_GetPCLK2Freq */ +#if defined(APBPERIPH_BASE) SPI_APB_CLOCK = HAL_RCC_GetPCLK1Freq(); - - /* normal series */ -#else - /* SPI1, SPI4 and SPI5 on APB2 */ - if(spi_drv->config->Instance == SPI1 - #ifdef SPI4 - || spi_drv->config->Instance == SPI4 - #endif - #ifdef SPI5 - || spi_drv->config->Instance == SPI5 - #endif - ) +#elif defined(APB1PERIPH_BASE) || defined(APB2PERIPH_BASE) + if ((rt_uint32_t)spi_drv->config->Instance >= APB2PERIPH_BASE) { SPI_APB_CLOCK = HAL_RCC_GetPCLK2Freq(); } - - /* SPI2 and SPI3 on APB1 */ - #ifdef SPI2 - else if(spi_drv->config->Instance == SPI2 - #ifdef SPI3 - || spi_drv->config->Instance == SPI3 - #endif - ) - { - SPI_APB_CLOCK = HAL_RCC_GetPCLK1Freq(); - } - #endif - - /* SPI6 get the input clk from APB4(such as on STM32H7). However, there is no HAL_RCC_GetPCLK4Freq api provided. - APB4 has same prescale factor as APB1 from HPRE Clock by default in CubeMx, so we assign APB1 to it. - if you change the default prescale factor of APB4, please modify SPI_APB_CLOCK accordingly. - */ else { SPI_APB_CLOCK = HAL_RCC_GetPCLK1Freq();