2025-09-28 1:47 PM
I have a question about PVD.
I have this main.c file. My interrupt function void TM_PVD_Handler(uint8_t status) won't execute when the power goes off. Why? In this case, I'm sending a message to the UART to see if my computer recieves the message before the CPU dies due to the lack of power. Have I forgot something? According to this example below, it's made for STM32F7, STM32F4 and STM32F0 series. I think this will work for STM32F1 as well. Correct me if I'm wrong.
I'm using STM32F1 series.
/* USER CODE BEGIN Header */
/**
******************************************************************************
* @file : main.c
* @brief : Main program body
******************************************************************************
* @attention
*
* Copyright (c) 2025 STMicroelectronics.
* All rights reserved.
*
* This software is licensed under terms that can be found in the LICENSE file
* in the root directory of this software component.
* If no LICENSE file comes with this software, it is provided AS-IS.
*
******************************************************************************
*/
/* USER CODE END Header */
/* Includes ------------------------------------------------------------------*/
#include "main.h"
/* Private includes ----------------------------------------------------------*/
/* USER CODE BEGIN Includes */
#include "tm_stm32_pvd.h"
/* USER CODE END Includes */
/* Private typedef -----------------------------------------------------------*/
/* USER CODE BEGIN PTD */
/* USER CODE END PTD */
/* Private define ------------------------------------------------------------*/
/* USER CODE BEGIN PD */
/* USER CODE END PD */
/* Private macro -------------------------------------------------------------*/
/* USER CODE BEGIN PM */
/* USER CODE END PM */
/* Private variables ---------------------------------------------------------*/
/* USER CODE BEGIN PV */
/* USER CODE END PV */
/* Private function prototypes -----------------------------------------------*/
void SystemClock_Config(void);
/* USER CODE BEGIN PFP */
/* USER CODE END PFP */
/* Private user code ---------------------------------------------------------*/
/* USER CODE BEGIN 0 */
void TM_PVD_Handler(uint8_t status){
/* Do things when the power goes off */
}
/* USER CODE END 0 */
/**
* @brief The application entry point.
* @retval int
*/
int main(void)
{
/* USER CODE BEGIN 1 */
/* USER CODE END 1 */
/* MCU Configuration--------------------------------------------------------*/
/* Reset of all peripherals, Initializes the Flash interface and the Systick. */
HAL_Init();
/* USER CODE BEGIN Init */
/* USER CODE END Init */
/* Configure the system clock */
SystemClock_Config();
/* USER CODE BEGIN SysInit */
/* USER CODE END SysInit */
/* Initialize all configured peripherals */
/* USER CODE BEGIN 2 */
TM_PVD_Enable(TM_PVD_Level_7, TM_PVD_Trigger_Falling);
/* USER CODE END 2 */
/* Infinite loop */
/* USER CODE BEGIN WHILE */
while (1)
{
/* USER CODE END WHILE */
/* USER CODE BEGIN 3 */
}
/* USER CODE END 3 */
}
/**
* @brief System Clock Configuration
* @retval None
*/
void SystemClock_Config(void)
{
RCC_OscInitTypeDef RCC_OscInitStruct = {0};
RCC_ClkInitTypeDef RCC_ClkInitStruct = {0};
/** Initializes the RCC Oscillators according to the specified parameters
* in the RCC_OscInitTypeDef structure.
*/
RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSI;
RCC_OscInitStruct.HSIState = RCC_HSI_ON;
RCC_OscInitStruct.HSICalibrationValue = RCC_HSICALIBRATION_DEFAULT;
RCC_OscInitStruct.PLL.PLLState = RCC_PLL_NONE;
if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK)
{
Error_Handler();
}
/** Initializes the CPU, AHB and APB buses clocks
*/
RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_HCLK|RCC_CLOCKTYPE_SYSCLK
|RCC_CLOCKTYPE_PCLK1|RCC_CLOCKTYPE_PCLK2;
RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_HSI;
RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1;
RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV1;
RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV1;
if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_0) != HAL_OK)
{
Error_Handler();
}
}
/* USER CODE BEGIN 4 */
/* USER CODE END 4 */
/**
* @brief This function is executed in case of error occurrence.
* @retval None
*/
void Error_Handler(void)
{
/* USER CODE BEGIN Error_Handler_Debug */
/* User can add his own implementation to report the HAL error return state */
__disable_irq();
while (1)
{
}
/* USER CODE END Error_Handler_Debug */
}
#ifdef USE_FULL_ASSERT
/**
* @brief Reports the name of the source file and the source line number
* where the assert_param error has occurred.
* @PAram file: pointer to the source file name
* @PAram line: assert_param error line source number
* @retval None
*/
void assert_failed(uint8_t *file, uint32_t line)
{
/* USER CODE BEGIN 6 */
/* User can add his own implementation to report the file name and line number,
ex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */
/* USER CODE END 6 */
}
#endif /* USE_FULL_ASSERT */
And this tm_stm32_pvd.c file
/**
* |----------------------------------------------------------------------
* | Copyright (c) 2016 Tilen Majerle
* |
* | Permission is hereby granted, free of charge, to any person
* | obtaining a copy of this software and associated documentation
* | files (the "Software"), to deal in the Software without restriction,
* | including without limitation the rights to use, copy, modify, merge,
* | publish, distribute, sublicense, and/or sell copies of the Software,
* | and to permit persons to whom the Software is furnished to do so,
* | subject to the following conditions:
* |
* | The above copyright notice and this permission notice shall be
* | included in all copies or substantial portions of the Software.
* |
* | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
* | EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
* | OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE
* | AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
* | HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
* | WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
* | FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
* | OTHER DEALINGS IN THE SOFTWARE.
* |----------------------------------------------------------------------
*/
#include "tm_stm32_pvd.h"
void TM_PVD_Enable(TM_PVD_Level_t Level, TM_PVD_Trigger_t Trigger) {
PWR_PVDTypeDef ConfigPVD;
/* Enable PWR clock */
__HAL_RCC_PWR_CLK_ENABLE();
/* Set interrupt to NVIC */
HAL_NVIC_SetPriority(PVD_IRQn, PVD_NVIC_PRIORITY, PVD_NVIC_SUBPRIORITY);
/* Enable interrupt */
HAL_NVIC_EnableIRQ(PVD_IRQn);
/* Set level and mode */
ConfigPVD.PVDLevel = (uint32_t)Level;
ConfigPVD.Mode = Trigger;
/* Config and enable PVD */
HAL_PWR_ConfigPVD(&ConfigPVD);
/* Enable PVD */
HAL_PWR_EnablePVD();
}
void TM_PVD_Disable(void) {
/* Disable PVD */
HAL_PWR_DisablePVD();
/* Disable EXTI interrupt for PVD */
__HAL_PWR_PVD_EXTI_DISABLE_IT();
/* Disable NVIC */
NVIC_DisableIRQ(PVD_IRQn);
}
/*****************************************************************/
/* PVD INTERRUPT USER CALLBACK */
/*****************************************************************/
__weak void TM_PVD_Handler(uint8_t status) {
/* NOTE: This function should not be modified, when the callback is needed,
the TM_PVD_Handler could be implemented in the user file
*/
}
/*****************************************************************/
/* PVD INTERRUPT HANDLER */
/*****************************************************************/
void PVD_IRQHandler(void) {
/* Call user function if needed */
if (__HAL_PWR_PVD_EXTI_GET_FLAG() != RESET) {
#if defined(PWR_CSR_PVDO)
/* Call user function with status */
TM_PVD_Handler((PWR->CSR & PWR_CSR_PVDO) ? 1 : 0);
#endif
#if defined(PWR_CSR1_PVDO)
/* Call user function with status */
TM_PVD_Handler((PWR->CSR1 & PWR_CSR1_PVDO) ? 1 : 0);
#endif
/* Clear PWR EXTI pending bit */
__HAL_PWR_PVD_EXTI_CLEAR_FLAG();
}
}
And this tm_stm32_pvd.h file
/**
* @author Tilen Majerle
* @email tilen@majerle.eu
* @website http://stm32f4-discovery.net
* @link http://stm32f4-discovery.net/2015/09/hal-library-26-power-voltage-detector-pvd-for-stm32fxxx/
* @version v1.0
* @ide Keil uVision
* @license MIT
* @brief PVD Voltage detector for STM32Fxxx
*
\verbatim
----------------------------------------------------------------------
Copyright (c) 2016 Tilen Majerle
Permission is hereby granted, free of charge, to any person
obtaining a copy of this software and associated documentation
files (the "Software"), to deal in the Software without restriction,
including without limitation the rights to use, copy, modify, merge,
publish, distribute, sublicense, and/or sell copies of the Software,
and to permit persons to whom the Software is furnished to do so,
subject to the following conditions:
The above copyright notice and this permission notice shall be
included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE
AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
OTHER DEALINGS IN THE SOFTWARE.
----------------------------------------------------------------------
\endverbatim
*/
#ifndef TM_PVD_H
#define TM_PVD_H 100
/* C++ detection */
#ifdef __cplusplus
extern "C" {
#endif
/**
* @addtogroup TM_STM32Fxxx_HAL_Libraries
* @{
*/
/**
* @defgroup TM_PVD
* @brief PVD - Power Voltage Detector for STM32Fxxx - http://stm32f4-discovery.net/2015/09/hal-library-26-power-voltage-detector-pvd-for-stm32fxxx/
* @{
*
* PVD can detect voltage changes in your system. It features 8 different voltage levels between 2 and 3 volts. For detailed voltages, check device datasheet.
*
* Library supports STM32F4xx, STM32F7xx and STM32F0xx devices. For F0xx series, not all devices are supported!
*
* \par Change configuration
*
* PVD uses interrupts for voltage detection. For this purpose, NVIC configuration must be used.
* I made default NVIC configuration, but if you need to change it, defines.h file is for you.
*
\code
//NVIC preemption priority
#define PVD_NVIC_PRIORITY 0x04
//NVIC Subpriority
#define PVD_NVIC_SUBPRIORITY 0x00
\endcode
*
* \par Changelog
*
\verbatim
Version 1.0
- First release
\endverbatim
*
* \par Dependencies
*
\verbatim
- STM32Fxxx HAL
- defines.h
- attributes.h
\endverbatim
*/
#include "main.h"
/**
* @defgroup TM_PVD_Macros
* @brief Library defines
* @{
*/
/* NVIC preemption priority */
#ifndef PVD_NVIC_PRIORITY
#define PVD_NVIC_PRIORITY 0x04
#endif
/* NVIC subpriority */
#ifndef PVD_NVIC_SUBPRIORITY
#define PVD_NVIC_SUBPRIORITY 0x00
#endif
/**
* @}
*/
/**
* @defgroup TM_PVD_Typedefs
* @brief Library Typedefs
* @{
*/
/**
* @brief PVD Triggers for interrupt
*/
typedef enum {
TM_PVD_Trigger_Rising = PWR_PVD_MODE_IT_RISING, /*!< PVD will trigger interrupt when voltage rises above treshold */
TM_PVD_Trigger_Falling = PWR_PVD_MODE_IT_FALLING, /*!< PVD will trigger interrupt when voltage falls below treshold */
TM_PVD_Trigger_Rising_Falling = PWR_PVD_MODE_IT_RISING_FALLING /*!< PVD will trigger interrupt when voltage rises or falls above/below treshold */
} TM_PVD_Trigger_t;
/**
* @brief PVD levels for interrupt triggering
* @note Check datasheets for proper values for voltages
*/
typedef enum {
TM_PVD_Level_0 = PWR_PVDLEVEL_0, /*!< PVD level 0 is used as treshold value */
TM_PVD_Level_1 = PWR_PVDLEVEL_1, /*!< PVD level 1 is used as treshold value */
TM_PVD_Level_2 = PWR_PVDLEVEL_2, /*!< PVD level 2 is used as treshold value */
TM_PVD_Level_3 = PWR_PVDLEVEL_3, /*!< PVD level 3 is used as treshold value */
TM_PVD_Level_4 = PWR_PVDLEVEL_4, /*!< PVD level 4 is used as treshold value */
TM_PVD_Level_5 = PWR_PVDLEVEL_5, /*!< PVD level 5 is used as treshold value */
TM_PVD_Level_6 = PWR_PVDLEVEL_6, /*!< PVD level 6 is used as treshold value */
TM_PVD_Level_7 = PWR_PVDLEVEL_7 /*!< PVD level 7 is used as treshold value */
} TM_PVD_Level_t;
/**
* @}
*/
/**
* @defgroup TM_PVD_Functions
* @brief Library Functions
* @{
*/
/**
* @brief Enables PVD feature for interrupt generation when voltage changes are detected
* @PAram Level: Level voltage when interrupt will happen. This parameter can be a value of @ref TM_PVD_Level_t enumeration
* @PAram Trigger: Trigger option when interrupt will be generated. This parameter can be a value of @ref TM_PVD_Trigger_t enumeration
* @retval None
*/
void TM_PVD_Enable(TM_PVD_Level_t Level, TM_PVD_Trigger_t Trigger);
/**
* @brief Disables PVD feature, disables NVIC and EXTI interrupt
* @PAram None
* @retval None
*/
void TM_PVD_Disable(void);
/**
* @brief Callback for handling PVD interrupts. This function is automatically called from interrupt service routine when needed.
* @PAram status: Status about voltage detected
* - 0: Voltage is above treshold level
* - > 0: Voltage is below treshold value
* @retval None
* @note With __weak parameter to prevent link errors if not defined by user
*/
void TM_PVD_Handler(uint8_t status);
/**
* @}
*/
/**
* @}
*/
/**
* @}
*/
/* C++ detection */
#ifdef __cplusplus
}
#endif
#endif
2025-09-28 3:11 PM
Dear @DMårt ,
Can you please let us know what the absolute VDD/VDDA Voltage desired to trig the PVD Interrupt? and what is the VDD/VDDA voltage of your application with STM32F1 series.
Here are the levels of PVD that we can configure:
Level7 used in the code above - corresponds to 2,66V to 3,0Volts either on Falling or Rising level
Let us know.
STOne-32.