@@ -107,33 +107,52 @@ extern "C" void PVD_IRQHandler() {
107107 pinWrite (powerPin, 0 );
108108
109109 // Blink red LED "pretty fast"
110+ // Only blink for a limited time because we don't want to cause a denial of service
111+ // if we're powered from a different source (which?)
110112 uint32_t leds = 0 ;
111- while ( true ) {
113+ for ( int blink = 0 ; blink < 10 ; blink++ ) {
112114 leds ^= CoprocReq_LedsEnum_L3;
113115 setLeds (leds);
114116
115117 for (volatile int i = 0 ; i < 200000 ; i++)
116118 ;
117119 }
120+
121+ // Reset program if power still present (powered from elsewhere)
122+ HAL_NVIC_SystemReset ();
118123}
119124
120125void powerEarlyInit () {
126+ pinWrite (powerPin, true );
121127 __HAL_RCC_PWR_CLK_ENABLE ();
122128 __HAL_RCC_BKP_CLK_ENABLE ();
123129
124- // Enable PVD - will generate EXTI16(PVD_IRQn) when VDD drops.
125- // Done before disabling ALARM output to eliminate race
130+ // Enable PVD - we want EXTI16(PVD_IRQn) interrupt when VDD drops.
131+ // Enabled before disabling ALARM output to eliminate race
126132 // that could result in ALARM deactivation.
127133 LL_PWR_EnableBkUpAccess ();
128- LL_PWR_SetPVDLevel (LL_PWR_PVDLEVEL_7);
134+ // Level 7 is the highest and seems too intolerant.
135+ LL_PWR_SetPVDLevel (LL_PWR_PVDLEVEL_4);
129136 LL_PWR_EnablePVD ();
130137
138+ setLeds (0x1 );
139+
140+ // Attempt to survive a transient power-up period when PVD output may fluctuate.
141+ while (LL_PWR_IsActiveFlag_PVDO ())
142+ ;
143+ __HAL_PWR_PVD_EXTI_CLEAR_FLAG ();
144+ __HAL_PWR_PVD_EXTI_ENABLE_RISING_EDGE ();
145+ __HAL_PWR_PVD_EXTI_ENABLE_IT ();
146+
147+ HAL_NVIC_ClearPendingIRQ (PVD_IRQn);
131148 HAL_NVIC_SetPriority (PVD_IRQn, 0 , 0 );
132149 HAL_NVIC_EnableIRQ (PVD_IRQn);
133150
151+ setLeds (0x3 );
152+
134153 // Disable potential RTC ALARM -> powerPin override.
135154 // This way we keep VCC powered upon board power-up.
136- // This must be done very early at power-up.
155+ // This must be done early at power-up.
137156 LL_RTC_SetOutputSource (BKP, LL_RTC_CALIB_OUTPUT_NONE);
138157}
139158
0 commit comments