Skip to content

Enable ICACHE, LPTICKER, TRNG and WATCHDOG for STM32H5 #465

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

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
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
1 change: 1 addition & 0 deletions targets/TARGET_STM/TARGET_STM32H5/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ target_sources(mbed-stm32h5
serial_device.c
spi_api.c
pwmout_device.c
cache.c
)

target_include_directories(mbed-stm32h5
Expand Down
64 changes: 64 additions & 0 deletions targets/TARGET_STM/TARGET_STM32H5/cache.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
/* mbed Microcontroller Library
* SPDX-License-Identifier: BSD-3-Clause
******************************************************************************
*
* Copyright (c) 2015-2021 STMicroelectronics.
* All rights reserved.
*
* This software component is licensed by ST under BSD 3-Clause license,
* the "License"; You may not use this file except in compliance with the
* License. You may obtain a copy of the License at:
* opensource.org/licenses/BSD-3-Clause
*
******************************************************************************
*/
#include "stm32h5xx.h"
#include "mbed_error.h"

/**
* @brief Enable ICACHE and define a MPU region to avoid HardFaults when accessing OTP and RO regions
* @param None
* @retval None
*/

void Cache_Init()
{
MPU_Attributes_InitTypeDef attr;
MPU_Region_InitTypeDef region;

/* Disable MPU before perloading and config update */
HAL_MPU_Disable();

/* Configure 0x00000000-0x08FFF7FF as Read Only, Executable and Cacheable */
region.Enable = MPU_REGION_ENABLE;
region.Number = MPU_REGION_NUMBER0;
region.AttributesIndex = MPU_ATTRIBUTES_NUMBER0;
region.BaseAddress = 0x00000000;
region.LimitAddress = 0x08FFF7FF;
region.AccessPermission = MPU_REGION_ALL_RO;
region.DisableExec = MPU_INSTRUCTION_ACCESS_ENABLE;
region.IsShareable = MPU_ACCESS_NOT_SHAREABLE;
HAL_MPU_ConfigRegion(&region);

/* Define cacheable memory via MPU */
attr.Number = MPU_ATTRIBUTES_NUMBER5;
attr.Attributes = INNER_OUTER(MPU_NOT_CACHEABLE);
HAL_MPU_ConfigMemoryAttributes(&attr);

/* Configure 0x08FFF800-0X0FFFFFFF as Read Only, Not Executable and Non-cacheable */
region.Enable = MPU_REGION_ENABLE;
region.Number = MPU_REGION_NUMBER5;
region.AttributesIndex = MPU_ATTRIBUTES_NUMBER5;
region.BaseAddress = 0x08FFF800;
region.LimitAddress = MBED_CONF_TARGET_MPU_ROM_END;
region.AccessPermission = MPU_REGION_ALL_RO;
region.DisableExec = MPU_INSTRUCTION_ACCESS_DISABLE;
region.IsShareable = MPU_ACCESS_NOT_SHAREABLE;
HAL_MPU_ConfigRegion(&region);

/* Enable the MPU */
HAL_MPU_Enable(MPU_PRIVILEGED_DEFAULT);

/* Enable ICACHE */
HAL_ICACHE_Enable();
}
4 changes: 2 additions & 2 deletions targets/TARGET_STM/TARGET_STM32H5/clock_cfg/system_clock.c
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
/**
* This file configures the system clock as follows:
*--------------------------------------------------------------------
* System clock source | 1- USE_PLL_HSE_EXTC
* System clock source | 1- USE_PLL_HSE_EXTC
* | 2- USE_PLL_HSE_XTAL
* | 3- USE_PLL_HSI (internal 64 MHz clock)
*--------------------------------------------------------------------
Expand Down Expand Up @@ -108,7 +108,7 @@ MBED_WEAK uint8_t SetSysClock_PLL_HSE(uint8_t bypass)
#endif
if(HSE_VALUE % 2000000 == 0)
{
RCC_OscInitStruct.PLL.PLLM = HSE_VALUE / 2000000; // Divide down input clock to 2MHz
RCC_OscInitStruct.PLL.PLLM = HSE_VALUE / 2000000; // Divide down input clock to 2MHz
RCC_OscInitStruct.PLL.PLLN = 250; // Multiply up to 500MHz VCO clock
RCC_OscInitStruct.PLL.PLLRGE = RCC_PLL1_VCIRANGE_1;
}
Expand Down
1 change: 1 addition & 0 deletions targets/TARGET_STM/TARGET_STM32U5/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ target_sources(mbed-stm32u5
i2c_device.c
serial_device.c
spi_api.c
cache.c
)

target_link_libraries(mbed-stm32u5 INTERFACE mbed-stm mbed-stm32u5cube-fw)
Original file line number Diff line number Diff line change
Expand Up @@ -156,11 +156,6 @@ uint8_t SetSysClock_PLL_HSI(void)
return 0; // FAIL
}

/** Enable ICACHE
*/
HAL_ICACHE_ConfigAssociativityMode(ICACHE_1WAY);
HAL_ICACHE_Enable();

return 1; // OK
}
#endif /* ((CLOCK_SOURCE) & USE_PLL_HSI) */
Expand Down Expand Up @@ -224,11 +219,6 @@ MBED_WEAK uint8_t SetSysClock_PLL_MSI(void)
HAL_RCCEx_EnableMSIPLLModeSelection(RCC_MSIKPLL_MODE_SEL);
HAL_RCCEx_EnableMSIPLLMode();

/** Enable ICACHE
*/
HAL_ICACHE_ConfigAssociativityMode(ICACHE_1WAY);
HAL_ICACHE_Enable();

return 1; // OK
}
#endif /* ((CLOCK_SOURCE) & USE_PLL_MSI) */
Original file line number Diff line number Diff line change
Expand Up @@ -274,9 +274,6 @@ MBED_WEAK uint8_t SetSysClock_PLL_MSI(void)
return 0; // FAIL
}

HAL_ICACHE_ConfigAssociativityMode(ICACHE_1WAY);
HAL_ICACHE_Enable();

return 1; // OK
}
#endif /* ((CLOCK_SOURCE) & USE_PLL_MSI) */
Original file line number Diff line number Diff line change
Expand Up @@ -167,8 +167,6 @@ MBED_WEAK uint8_t SetSysClock_PLL_MSI(void)
return 0; // FAIL
}

HAL_ICACHE_ConfigAssociativityMode(ICACHE_1WAY);
HAL_ICACHE_Enable();

return 1; // OK
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -167,9 +167,6 @@ MBED_WEAK uint8_t SetSysClock_PLL_MSI(void)
return 0; // FAIL
}

HAL_ICACHE_ConfigAssociativityMode(ICACHE_1WAY);
HAL_ICACHE_Enable();

return 1; // OK
}
#endif /* ((CLOCK_SOURCE) & USE_PLL_MSI) */
Original file line number Diff line number Diff line change
Expand Up @@ -156,11 +156,6 @@ uint8_t SetSysClock_PLL_HSI(void)
return 0; // FAIL
}

/** Enable ICACHE
*/
HAL_ICACHE_ConfigAssociativityMode(ICACHE_1WAY);
HAL_ICACHE_Enable();

return 1; // OK
}
#endif /* ((CLOCK_SOURCE) & USE_PLL_HSI) */
Expand Down Expand Up @@ -225,11 +220,6 @@ MBED_WEAK uint8_t SetSysClock_PLL_MSI(void)
HAL_RCCEx_EnableMSIPLLModeSelection(RCC_MSIKPLL_MODE_SEL);
HAL_RCCEx_EnableMSIPLLMode();

/** Enable ICACHE
*/
HAL_ICACHE_ConfigAssociativityMode(ICACHE_1WAY);
HAL_ICACHE_Enable();

return 1; // OK
}
#endif /* ((CLOCK_SOURCE) & USE_PLL_MSI) */
28 changes: 28 additions & 0 deletions targets/TARGET_STM/TARGET_STM32U5/cache.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
/* mbed Microcontroller Library
* SPDX-License-Identifier: BSD-3-Clause
******************************************************************************
*
* Copyright (c) 2015-2021 STMicroelectronics.
* All rights reserved.
*
* This software component is licensed by ST under BSD 3-Clause license,
* the "License"; You may not use this file except in compliance with the
* License. You may obtain a copy of the License at:
* opensource.org/licenses/BSD-3-Clause
*
******************************************************************************
*/
#include "stm32u5xx.h"
#include "mbed_error.h"

/**
* @brief Enable ICACHE
* @param None
* @retval None
*/

void Cache_Init()
{
HAL_ICACHE_ConfigAssociativityMode(ICACHE_1WAY);
HAL_ICACHE_Enable();
}
2 changes: 1 addition & 1 deletion targets/TARGET_STM/lp_ticker.c
Original file line number Diff line number Diff line change
Expand Up @@ -266,7 +266,7 @@ void lp_ticker_init(void)
#if defined (LPTIM_ACTIVEEDGE_FALLING)
LptimHandle.Init.Trigger.ActiveEdge = LPTIM_ACTIVEEDGE_FALLING;
#endif
#if defined(TARGET_STM32U5) || defined(TARGET_STM32U0)
#if defined(TARGET_STM32U5) || defined(TARGET_STM32H5) || defined(TARGET_STM32U0)
LptimHandle.Init.Period = 0xFFFF;
#endif
#if defined (LPTIM_TRIGSAMPLETIME_DIRECTTRANSITION)
Expand Down
38 changes: 26 additions & 12 deletions targets/TARGET_STM/mbed_overrides.c
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,31 @@ MBED_WEAK void TargetBSP_Init(void)
/** Do nothing */
}

/**
* @brief Enable cache if the target CPU has cache
*
* @note The default implementation works on STM32F7/H7 series with L1 cache.
* This declaration is weak so it may be overridden for other STM32 series
*
* @param None
* @retval None
*/
MBED_WEAK void Cache_Init(void)
{
#if defined(__ICACHE_PRESENT) /* STM32F7/H7 */
// This function can be called either during cold boot or during
// application boot after bootloader has been executed.
// In case the bootloader has already enabled the cache,
// is is needed to not enable it again.
if ((SCB->CCR & (uint32_t)SCB_CCR_IC_Msk) == 0) { // If ICache is disabled
SCB_EnableICache();
}
if ((SCB->CCR & (uint32_t)SCB_CCR_DC_Msk) == 0) { // If DCache is disabled
SCB_EnableDCache();
}
#endif /* __ICACHE_PRESENT */
}

#ifndef MBED_DEBUG
#if MBED_CONF_TARGET_GPIO_RESET_AT_INIT
void GPIO_Full_Init(void)
Expand Down Expand Up @@ -160,18 +185,7 @@ void GPIO_Full_Init(void)
// This function is called after RAM initialization and before main.
void mbed_sdk_init()
{
#if defined(__ICACHE_PRESENT) /* STM32F7 */
// The mbed_sdk_init can be called either during cold boot or during
// application boot after bootloader has been executed.
// In case the bootloader has already enabled the cache,
// is is needed to not enable it again.
if ((SCB->CCR & (uint32_t)SCB_CCR_IC_Msk) == 0) { // If ICache is disabled
SCB_EnableICache();
}
if ((SCB->CCR & (uint32_t)SCB_CCR_DC_Msk) == 0) { // If DCache is disabled
SCB_EnableDCache();
}
#endif /* __ICACHE_PRESENT */
Cache_Init();

#if defined(DUAL_CORE) && (TARGET_STM32H7)
/* HW semaphore Clock enable*/
Expand Down
2 changes: 1 addition & 1 deletion targets/TARGET_STM/trng_api.c
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ void trng_init(trng_t *obj)
}
}

#elif defined(TARGET_STM32G4) || defined(TARGET_STM32U0)
#elif defined(TARGET_STM32G4) || defined(TARGET_STM32H5) || defined(TARGET_STM32U0)
/* RNG and USB clocks have the same HSI48 source which has been enabled in SetSysClock */
RCC_PeriphCLKInitTypeDef PeriphClkInitStruct;

Expand Down
2 changes: 1 addition & 1 deletion targets/TARGET_STM/watchdog_api.c
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,7 @@ watchdog_features_t hal_watchdog_get_platform_features(void)
features.clock_max_frequency = 47000;
#elif defined(STM32F0) || defined(STM32F3)
features.clock_max_frequency = 50000;
#elif defined(STM32H7) || defined(STM32L4) || defined(STM32U5)
#elif defined(STM32H7) || defined(STM32L4) || defined(STM32U5) || defined(STM32H5)
features.clock_max_frequency = 33600;
#elif defined(STM32G0) || defined(STM32L5) || defined(STM32G4) || defined(STM32WB) || defined(STM32WL) || defined(STM32U0)
features.clock_max_frequency = 34000;
Expand Down
15 changes: 7 additions & 8 deletions targets/targets.json5
Original file line number Diff line number Diff line change
Expand Up @@ -3060,7 +3060,7 @@ mode is recommended for target MCUs with small amounts of flash and RAM.",
"STM32G4A1xx"
]
},

// STM32H5 Targets -------------------------------------------------------------------------------------------------
"MCU_STM32H5": {
"inherits": [
Expand Down Expand Up @@ -3104,14 +3104,13 @@ mode is recommended for target MCUs with small amounts of flash and RAM.",
"device_has_add": [
"MPU",
"ANALOGOUT",
"SPI_32BIT_WORDS"
"SPI_32BIT_WORDS",
"TRNG"
],
"device_has_remove": [
"FLASH",
"LPTICKER",
"CAN",
"SERIAL_FC",
"WATCHDOG"
"SERIAL_FC"
],
"is_mcu_family_target": true
},
Expand Down Expand Up @@ -3193,7 +3192,7 @@ mode is recommended for target MCUs with small amounts of flash and RAM.",
"device_name": "STM32H563ZITx",
"image_url": "https://www.st.com/bin/ecommerce/api/image.PF274337.en.feature-description-include-personalized-no-cpn-medium.jpg"
},

// STM32H7 Targets -------------------------------------------------------------------------------------------------
"MCU_STM32H7": {
"inherits": [
Expand Down Expand Up @@ -3435,7 +3434,7 @@ mode is recommended for target MCUs with small amounts of flash and RAM.",
],
"device_name": "STM32H745ZITx"
},

// These targets contain the extra bits to add to the MCU_STM32H745xI target to set it for the
// CM4 or CM7 core.
"MCU_STM32H745xI_CM4": {
Expand Down Expand Up @@ -4894,7 +4893,7 @@ mode is recommended for target MCUs with small amounts of flash and RAM.",
],
"image_url": "https://www.st.com/bin/ecommerce/api/image.PF273876.en.feature-description-include-personalized-no-cpn-large.jpg"
},

// STM32U5 Targets -------------------------------------------------------------------------------------------------
"MCU_STM32U5": {
"inherits": [
Expand Down