1
0
mirror of https://github.com/trezor/trezor-firmware.git synced 2025-07-30 02:18:16 +00:00

perf(core): save flash in by not using HAL for oscillator configuration

[no changelog]
This commit is contained in:
tychovrahe 2025-07-16 14:07:04 +02:00 committed by TychoVrahe
parent f97ead8bb5
commit f552569292
2 changed files with 32 additions and 17 deletions

View File

@ -25,6 +25,8 @@
#include <io/rgb_led.h>
#include "sys/systick.h"
#define LED_SWITCHING_FREQUENCY_HZ 20000
#define TIMER_PERIOD (16000000 / LED_SWITCHING_FREQUENCY_HZ)
@ -64,12 +66,16 @@ void rgb_led_init(void) {
rgb_led_set_default_pin_state();
// enable LSE clock
RCC_OscInitTypeDef RCC_OscInitStruct = {0};
RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSI;
RCC_OscInitStruct.HSIState = RCC_HSI_ON;
RCC_OscInitStruct.PLL.PLLState = RCC_PLL_NONE;
HAL_RCC_OscConfig(&RCC_OscInitStruct);
uint32_t deadline = ticks_timeout(HSI_TIMEOUT_VALUE);
// enable HSI clock
RCC->CR |= RCC_CR_HSION;
// wait until the HSI is on
while ((RCC->CR & RCC_CR_HSIRDY) != RCC_CR_HSIRDY) {
if (ticks_expired(deadline)) {
return;
}
}
// select LSE as LPTIM clock source
RCC_PeriphCLKInitTypeDef PeriphClkInitStruct = {0};

View File

@ -26,6 +26,7 @@
#include <stm32u5xx_hal_cryp.h>
#include <sec/secure_aes.h>
#include <sys/systick.h>
#if NORCOW_MIN_VERSION <= 5
#include "secure_aes_unpriv.h"
@ -221,13 +222,16 @@ secbool secure_aes_ecb_decrypt_hw(const uint8_t* input, size_t size,
}
secbool secure_aes_init(void) {
RCC_OscInitTypeDef osc_init_def = {0};
osc_init_def.OscillatorType = RCC_OSCILLATORTYPE_SHSI;
osc_init_def.SHSIState = RCC_SHSI_ON;
// Enable SHSI clock
if (HAL_RCC_OscConfig(&osc_init_def) != HAL_OK) {
goto cleanup;
__HAL_RCC_SHSI_ENABLE();
uint32_t deadline = ticks_timeout(HSI_TIMEOUT_VALUE);
// Wait till SHSI is ready
while (READ_BIT(RCC->CR, RCC_CR_SHSIRDY) == 0U) {
if (ticks_expired(deadline)) {
goto cleanup;
}
}
// Enable SAES peripheral clock
@ -247,12 +251,17 @@ void secure_aes_deinit(void) {
__HAL_RCC_SAES_FORCE_RESET();
__HAL_RCC_SAES_RELEASE_RESET();
RCC_OscInitTypeDef osc_init_def = {0};
osc_init_def.OscillatorType = RCC_OSCILLATORTYPE_SHSI;
osc_init_def.SHSIState = RCC_SHSI_OFF;
// Disable the Secure Internal High Speed oscillator (SHSI)
__HAL_RCC_SHSI_DISABLE();
// Disable SHSI clock
HAL_RCC_OscConfig(&osc_init_def);
uint32_t deadline = ticks_timeout(2);
// Wait till SHSI is off
while (READ_BIT(RCC->CR, RCC_CR_SHSIRDY) != 0U) {
if (ticks_expired(deadline)) {
return;
}
}
}
#endif // SECURE_MODE