Monitoring power supply with STM32 peripherals

Revision as of 18:05, 17 September 2024 by Registered User
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)

1. Introduction

The quality of the power supply is crucial for the reliability of electronic applications. Monitoring power supply during lifetime of an application can help to identify failure and operate maintenance or evaluate power source quality. The purpose of this article is to propose solutions to detect in real-time a degradation in the quality of the power supply or a disturbance that could alter the proper functioning of the application or its reliability. These solutions are based on STM32 embedded peripherals, thus avoiding the addition of external components.

Embedded Monitoring

In this article, some examples are presented using comparator, ADC, operational amplifier, internal reference, NVIC, TIMER, DMA. These are integrated in a case study to illustrate their potential use.

2. Case Study: Data Center Ventilation Management

The system studied is the management of the ventilation of a data center. This system is critical because a failure could lead to severe material damage or unnecessary electricity consumption. This system must be robust against potential failures and detect unexpected behaviors to report it to maintenance team.

2.1. Operation

File:case study block.png
Functional diagram of the case study

Temperature sensors transmit the measurements to the microcontroller via an analog signal. A state machine manages the turning on and off of the ventilation based on the sensor values. Additionally, the station sends operation logs and alerts of anomalies to a server. The functional diagram illustrates the overall operation of the system.

The code includes a critical routine

  • Temperature measurement: A power supply disturbance can distort the measured value.

2.2. Risks and Threats

  • Power supply circuit alteration: Time, mechanical wear, and thermal cycles can lead to the failure of power supply circuit components. A failure can lead to a degradation of the power supply circuit's performance, a total shutdown, or even damage to the application. If it is possible to detect signs of degradation, maintenance can be planned and carried out to avoid a total and unexpected shutdown of the application. In our case study, the shutdown of the ventilation system presents a risk of server degradation or forced shutdown.
  • Measurement disturbance: The reliability and accuracy of analog measurements depend on a reference voltage that must be very stable and precise. The presence of fast transients on the power supply voltages can cause a disturbance of this reference voltage and disrupt the analog measurement. In our case study, the disturbance of the temperature measurement can alter the decision-making process.

2.3. Verifications to Implement

To ensure the proper functioning of the system, several types of verifications can be implemented:

  • Detect power supply loss: Monitoring upstream power source to detect loss of power supply and execute a shutdown routine.
  • Detection of temporary transients: Detecting transient to repeat potentially false analog measurements.
  • Detection of power supply voltage drifts: Prevent malfunction if the power supply can no longer provide the necessary energy.
  • Continuous measurements with the ADC: Use signal processing models to identify potential unexpected behaviors.

2.4. Reaction Speed

When detecting a phenomenon, the reaction speed can be critical, for example, to perform some actions when detecting power supply shutdown. The first step is detection; for this, it is preferable to prioritize asynchronous mechanisms and those independent of the CPU that trigger an interrupt, such as the comparator rather than the ADC. Then, the speed of information processing by the CPU is crucial. To develop STM32 code, the effective and recommended way is to use the software library provided by ST called HAL. The HAL simplifies the interaction between the user program and the STM32 hardware peripherals. It offers two elements that will attract our attention:

  • An interrupt management mechanism using "interrupt handlers" that automatically handle verification and flag lowering and then execute the corresponding user code.
  • A set of data structures and functions that simplify the configuration of peripherals and reading of useful data.

2.4.1. Interrupt Management

Functional diagram of HAL interrupt management

The NVIC is the hardware peripheral that handles interrupts. All interrupt signals from the various peripherals arrive directly at the NVIC. It is this that orders the CPU to interrupt the current program execution and provides it with the address of the IRQ Handler (Interrupt ReQuest Handler). The IRQ Handler (or ISR Interrupt Service Routine) is the function directly executed that must handle the interrupt.

As illustrated in figure interrupt_hal_block, the HAL provides a function called by the IRQ Handler, which checks all the configuration registers and interrupt flags to determine the source of the interrupt and handles lowering the corresponding flag. It then calls a callback function that contains the user code. Therefore, the first line of user code is executed only after 3 context saves and dozens of conditional tests.

Although offering many advantages such as ease of use, readability, and portability, these procedures are computationally expensive and can be too heavy in some cases. The solution is to specify that the interrupt handler should not be used and to write the content of the IRQ Handler yourself. In this case, it is necessary to remember the necessary verifications and to lower the interrupt flag.

3. Power loss detection

This solution is designed to detect that the power source is dripping and probably shut down. Reflection is carried out to react as quickly as possible if actions are to be taken at the time of detection.

3.1. Operation

The source voltage is generally higher than the MCU operating voltage, this leaves time between power source drop and VDD drop. Keep in mind that the falling rate highly depend on current consumption, the amount of decoupling capacitors or the presence of output discharge feature on power supplies for example. In this example, Vin is 11 V. In this example this leaves 890µs.

power loss delay

This means that if the circuit detect a voltage drop with a threshold of 10 V, it provides anticipation to execute power loss routine before the system's complete extinction. In the present example this consist of saving a specific value into backup registers.

Configuration schematic

Use a comparator to compare the voltage with an internal reference voltage. An interrupt is triggered at falling and rising edges.

The structure is described with this strate chart.

State chart

3.2. Cube MX configuration

Configure the comparator in interrupt mod rising and falling edge. Select Internal VRef as negative input.

Comparator configuration

Activate the RTC.

RTC configuration

Unselect code generation of the Call HAL Handler for comparator interrupt.

NVIC configuration

3.3. Source code

  • In main.h define some aliases these have to be changed according to your application.
/* USER CODE BEGIN Private defines */
#define COMP_DETECT_PL COMP1 // replace COMP1 by the chosen one
#define COMP_DETECT_PL_EXTI_LINE COMP_EXTI_LINE_COMP1 // Use the right ETI line
#define COMP_DETECT_PL_EXTI_CLF_MACRO LL_EXTI_ClearFlag_0_31 // Use the right ETI clear flag macro
/* USER CODE END Private defines */
  • In main.c function int main(void); At boot, check if a power loss occurred, if so execute specific instruction as send data to server for example.
/* USER CODE BEGIN 2 */
// At boot, check for backup register to know if a power loss occurred
// Be careful to check before enable comparator interrupt!!
if (HAL_RTCEx_BKUPRead(&hrtc, RTC_BKP_DR1) != 0xEBEB) // check for power loss code in backup registers
{
    if (HAL_RTCEx_BKUPRead(&hrtc, RTC_BKP_DR1) == 0xBEBE)
    {
        HAL_PWR_EnableBkUpAccess();
        HAL_RTCEx_BKUPWrite(&hrtc, RTC_BKP_DR1, 0xEBEB);// remove shutdown code in backup registers
        HAL_PWR_DisableBkUpAccess();

        GPIOB->BSRR = (uint32_t)GPIO_PIN_0;// Debug : set PB0 pin
        /*
        * insert USER code that react to a reboot after power loss
        */
    }
}
HAL_COMP_Start(&hcomp1);
/* USER CODE END 2 */
  • In stm32g4xx_it.c function void COMP1_2_3_IRQHandler(void); Interrupt ReQest function executed at rising and falling edge of the comparator. Discriminate one of another with a condition and save the right number in the backup register depending on power loss occurring or not.
/* USER CODE BEGIN COMP1_2_3_IRQn 0 */
COMP_DETECT_PL_EXTI_CLF_MACRO(COMP_DETECT_PL_EXTI_LINE); // Clear interrupt flag

if(  (COMP_DETECT_PL->CSR & (COMP_CSR_VALUE)) == 0  )// if Comparator is low,  power loss probably occurring
{
    GPIOA->BSRR = (uint32_t)GPIO_PIN_4;// Debug : set PA4 pin
    HAL_PWR_EnableBkUpAccess();
    HAL_RTCEx_BKUPWrite(&hrtc, RTC_BKP_DR1, 0xBEBE);// write  power loss  code in backup registers
    HAL_PWR_DisableBkUpAccess();

    /*
    * insert USER code that react to a power loss
    */

}
else // if Comparator is high, no power loss occurred
{
    GPIOA->BRR = (uint32_t)GPIO_PIN_4;// Debug : reset PA4 pin
    HAL_PWR_EnableBkUpAccess();
    HAL_RTCEx_BKUPWrite(&hrtc, RTC_BKP_DR1, 0xEBEB);// remove  power loss  code in backup registers
    HAL_PWR_DisableBkUpAccess();
}
/* USER CODE END COMP1_2_3_IRQn 0 */

3.4. Result