Author Topic: STM32G0 Programmable Voltage Detector  (Read 1614 times)

0 Members and 1 Guest are viewing this topic.

Offline Red_MicroTopic starter

  • Regular Contributor
  • *
  • Posts: 121
  • Country: ca
STM32G0 Programmable Voltage Detector
« on: February 25, 2022, 01:30:21 am »
I'm checking the PVD example from STM32G0 repository in CubeMx and something looks backwards to me unless I'm misunderstanding it. I'm expecting the rising callback when the voltage is ABOVE the threshold not below like the code shows. Likewise, I'm expecting the falling callback when the voltage is BELOW threshold. But the code behaves the opposite, that is, the falling callback comes in when the voltage is above ~2.6V and rising callback when is below ~2.5V.

Here is part of the code:

Code: [Select]
  /* Configure the PVD Level to 3 and generate an interrupt on rising and falling
     edges(PVD detection level set to 2.5V, refer to the electrical characteristics
     of you device datasheet for more details) */
  sConfigPVD.PVDLevel = PWR_PVDLEVEL_3;
  sConfigPVD.Mode = PWR_PVD_MODE_IT_RISING_FALLING;

Code: [Select]
void HAL_PWREx_PVD_PVM_Rising_Callback(void)
{
  /* Turn Off LED1 as voltage is below threshold */
  BSP_LED_Off(LED1);

  /* Set uwToggleOn global variable to zero to disable toggle */
  uwToggleOn = 0u;
}


void HAL_PWREx_PVD_PVM_Falling_Callback(void)
{
  /* Turn On LED1 as voltage is above threshold */
  BSP_LED_On(LED1);

  /* Set uwToggleOn global variable to one to disable toggle */
  uwToggleOn = 1u;
}
 

Offline thm_w

  • Super Contributor
  • ***
  • Posts: 6389
  • Country: ca
  • Non-expert
Re: STM32G0 Programmable Voltage Detector
« Reply #1 on: February 25, 2022, 10:49:10 pm »
Code: [Select]
void HAL_PWREx_PVD_IRQHandler(void)
{
  /* Check PWR exti Rising flag */
  if (__HAL_PWR_PVD_EXTI_GET_RISING_FLAG() != 0x0U)
  {
    /* Clear PVD exti pending bit */
    __HAL_PWR_PVD_EXTI_CLEAR_RISING_FLAG();

    /* PWR PVD interrupt rising user callback */
    HAL_PWREx_PVD_Rising_Callback();
  }

  /* Check PWR exti fallling flag */
  if (__HAL_PWR_PVD_EXTI_GET_FALLING_FLAG() != 0x0U)
  {
    /* Clear PVD exti pending bit */
    __HAL_PWR_PVD_EXTI_CLEAR_FALLING_FLAG();

    /* PWR PVD interrupt falling user callback */
    HAL_PWREx_PVD_Falling_Callback();
  }
}

#define __HAL_PWR_PVD_EXTI_GET_RISING_FLAG() (EXTI->RPR1 & PWR_EXTI_LINE_PVD)
#define __HAL_PWR_PVD_EXTI_GET_FALLING_FLAG() (EXTI->FPR1 & PWR_EXTI_LINE_PVD)

Are you slowly raising the voltage with a clean power supply?
Maybe set some breakpoints in there? see if registers are what you expect.


Where is the code where you are setting the levels?

Quote
Programmable voltage detector (PVD)
The PVD can be used to monitor the VDD power supply by comparing it to the thresholds selected through PVDRT[2:0] bits (rising thresholds) and PVDFT[2:0] bits (falling
thresholds) in the Power control register 2 (PWR_CR2). VPVDFx should always be set to a lower voltage level than VPVDRx .
The PVD is enabled by setting the PVDE bit. A PVDO flag is available in the Power status register 2 (PWR_SR2). It indicates if VDD is higher or lower than the PVD threshold. This event is internally connected to the EXTI line16 and can generate an interrupt if enabled through the EXTI registers. The PVD output interrupt can be generated when V DD drops below the PVD threshold and/or when VDD
rises above the PVD threshold depending on EXTI line16 rising/falling edge configuration. As an example, the service routine could perform emergency shutdown tasks.
« Last Edit: February 25, 2022, 10:51:50 pm by thm_w »
Profile -> Modify profile -> Look and Layout ->  Don't show users' signatures
 
The following users thanked this post: Red_Micro

Offline Red_MicroTopic starter

  • Regular Contributor
  • *
  • Posts: 121
  • Country: ca
Re: STM32G0 Programmable Voltage Detector
« Reply #2 on: March 04, 2022, 12:53:23 pm »
Are you slowly raising the voltage with a clean power supply?
Maybe set some breakpoints in there? see if registers are what you expect.
Where is the code where you are setting the levels?

Yes, I'm using a clean power supply. I've set the level 6:

Code: [Select]
  /** PVD Configuration
  */
  sConfigPVD.PVDLevel = PPWR_PVDLEVEL_6;
  sConfigPVD.Mode = PWR_PVD_MODE_IT_RISING_FALLING;
  HAL_PWREx_ConfigPVD(&sConfigPVD);
  /** Enable the PVD Output
  */
  HAL_PWREx_EnablePVD();


Basically the falling callback is being called during the rising edge (3.01V) and the rising callback during the falling edge (2.91V), not sure if it's how it should work.
Code: [Select]
void HAL_PWREx_PVD_PVM_Rising_Callback(void){
 
   /* Turn green LED as voltage is above threshold */
 
}

void HAL_PWREx_PVD_PVM_Falling_Callback(void){

/* Turn red LED as voltage is above threshold */
 
}


 

Offline cv007

  • Frequent Contributor
  • **
  • Posts: 828
Re: STM32G0 Programmable Voltage Detector
« Reply #3 on: March 04, 2022, 06:03:31 pm »
I have an stm32g031 board, and am no expert but...

Forget rising/falling naming in PVD, will just call it 'setpoint' here and just pretend its just one value

PVD output (PVDO) is what controls EXTI-
PVDO low = Vdd above setpoint
PVDO high = Vdd below setpoint

when going from low voltage to high voltage (crossing setpoint from below)-
PVDO high -> PVDO low = falling interrupt (not falling voltage)
when going from high voltage to low voltage (crossing setpoint from above)-
PVDO low -> PVDO high = rising interrupt (not rising voltage)

So you have clashing names that mean opposite things. I would come up with a way to make this a 'one time think' (via functions) where you can then use pvd  without having to do mental gymnastics each time.
 


Share me

Digg  Facebook  SlashDot  Delicious  Technorati  Twitter  Google  Yahoo
Smf