【STM32】The code jumps to an abnormal interrupt after changing the clock frequency when using STM32Cube.IDE

1 Introduction

This is a problem that was accidentally discovered during the project. In fact, there are equally more complex projects that can be run, but later I found that a new simple project cannot be run, but the same more complex project can be run, which means that my colleagues originally did not know where to find it. I found a solution to the problem, but later I forgot it and couldn't find it. But since I encountered it again and solved it, I hereby record it to prevent forgetting it again.

2. Problems and reproduction methods

Just create a new project, configure the clock to a slightly higher value such as 40MHz, and then configure any peripherals. After generating the code, it will not run and it will get stuck.

if (HAL_PWREx_ControlVoltageScaling(PWR_REGULATOR_VOLTAGE_SCALE4) != HAL_OK)
  {
    
    
    Error_Handler();
  }

Can not operate

3. The process of solving the problem

When using the STM32Cube.IDE software, you can conveniently configure the clock tree in Clock Configuration to change the main frequency.

Insert image description here

The default main frequency of the clock tree is 4M (for the chip I am using, the default clock tree configuration of different chips may be different). When it is modified to a main frequency greater than 40M, the program will run away. When DeBug runs down step by step When I found that the program was stuck in this function

Insert image description here

Although we don’t know the meaning, we can probably guess that it configures the output voltage of the main internal voltage regulator. We can jump in and take a look.

  if (HAL_PWREx_ControlVoltageScaling(PWR_REGULATOR_VOLTAGE_SCALE4) != HAL_OK)
  {
    
    
    Error_Handler();
  }

In fact, this function is also introduced in detail in the HAL library.

Insert image description here
Insert image description here

Here is a translation for everyone (I am a scumbag who has not passed CET-4, so I translated orz with Youdao Dictionary)

===============================================================================

#####电源控制功能#####

===============================================================================

[. .]

介绍控制电源的功能。



[. .]

(+) STM32U5系列器件嵌入两个稳压器:一个LDO(线性

电压调节器)和一个SMPS(降压转换器)并联

为数字外设SRAM1, SRAM2, SRAM3提供VCORE电源

SRAM4和嵌入式闪存。



(+) SMPS允许降低功耗,但有些

外围设备可能会受到SMPS产生的噪声的干扰,

要求应用程序在运行时切换到LDO

外围设备才能达到最佳性能。



(+) LDO和SMPS稳压器有两种模式:主稳压器模式

(当需要性能时使用),和低功率调节器模式。LDO

或SMPS可用于所有电压缩放范围,并在所有停止

模式。



(+)复位后,稳压器为LDO,范围为4。切换到SMPS

提供更低的消耗,特别是在高VDD电压。它是

可以从LDO切换到SMPS,或者从SMPS切换到LDO

任何范围,通过配置REGSEL位。建议进行切换

在改变电压范围之前,首先要到SMPS。



(+)当退出停止或待机模式时,稳压器与

进入低功耗模式时。电压量程为范围4(+)两个稳压器可以提供四种不同的电压(电压缩放)

并可在停止模式下运行。

电压缩放范围可以是以下值之一:

(++)电压输出刻度1:1V2

=;当系统时钟频率高达160mhz时使用

(++)电压输出刻度2:1V1

=;当系统时钟频率不超过100mhz时使用

(++)电压输出刻度3:1V0

=;当系统时钟频率高达50mhz时使用

(++)电压输出刻度4:0V9

=;当系统时钟频率高达24mhz时使用



@endverbatim

* @ {
    
    

* /



/ * *

* @brief配置主内部稳压器输出电压来实现

*性能和功耗之间的权衡。

* @param voltagscaling:指定稳压器输出电压刻度。

*该参数可以是以下值之一:

* @arg @ref PWR_REGULATOR_VOLTAGE_SCALE1:稳压输出刻度1*提供1.2 V的典型输出电压。

*当系统时钟频率高达160mhz时使用。

* @arg @ref PWR_REGULATOR_VOLTAGE_SCALE2:稳压输出刻度2*提供1.1 V的典型输出电压。

*当系统时钟频率高达100mhz时使用。

* @arg @ref PWR_REGULATOR_VOLTAGE_SCALE3:稳压器电压输出刻度3*提供1.0 V的典型输出电压。

*当系统时钟频率高达50mhz时使用。

* @arg @ref PWR_REGULATOR_VOLTAGE_SCALE4:稳压输出刻度4*提供0.9 V的典型输出电压。

*当系统时钟频率高达24mhz时使用。

* @注意在移动到电压缩放2之前,必须确保

*系统频率在50mhz到100mhz之间。

* @注意在移动到电压缩放3之前,必须确保

*系统频率在24mhz到50mhz之间。

* @注意在移动到电压缩放4之前,必须确保

*系统频率低于24mhz。

* @检索HAL状态。

* /

 

No problem has been found here for the time being, but when we change the system frequency to 40, we will find that the rang value in the code automatically generated by the IDE becomes 1, but according to the instructions in the library, it should be 3

Insert image description here

This means that we didn't actually succeed in changing the main frequency, and discovered a strange problem in the process

Insert image description here
SystemPower_Config This function is actually emptyPlease add image description

Is this an unconfigured system power supply? Therefore, changing the main frequency failed. There is no code written here, it is automatically generated by the IDE. For new functions, the IDE may make errors, but the most basic reason is that there is no reason to make errors, so I think there must be a certain configuration step missing. So I searched in the configuration and finally found the most possible configuration option.

Insert image description here
It probably means power security and permissions? After trying to open it, I found that the program would not freeze and could run smoothly, so I thought maybe this permission is needed to change the main frequency. We just need to open it manually, but when we returned to the code, we found that, in fact,

Insert image description here

This function is still empty. Comparing the existing runnable projects, we found that it has content.
Insert image description here

After comparing the specific configuration, it was found that the original project configured the low-power switch in this place, so it is speculated that SystemPower_Configthis function is specially used to control low-power consumption. It has nothing to do with the initial conjecture, and it may be from the beginning. Because when I was looking for how to configure low power, I Privilege attributeschecked this option after seeing others check it. I didn't encounter this problem by accident, but fortunately it is solved now.

Insert image description here

And for the other part of the rang range, when the main frequency of our system is set to 40M, it is stillPWR_REGULATOR_VOLTAGE_SCALE1

Insert image description here
When the test changes multiple ranges, the values ​​will only change between 1 and 4, and will not jump to 2/3. Specifically, there are no results found online. If anyone knows the reason, please share the discussion.

Guess you like

Origin blog.csdn.net/Daears/article/details/130501173