py32l020-watchdog-safety-6.ino
Activa el Watchdog Independiente (IWDG) para reiniciar el módulo si el software no se refresca dentro del tiempo establecido.
El PY32L020 utiliza un watchdog basado en el reloj LSI interno. Una vez iniciado, no se puede detener, solo refrescar. Verifica las direcciones de registro del IWDG en la documentación técnica del PY32L020. El refresco debe realizarse periódicamente en el bucle principal.
py32l020-watchdog-safety-6.ino
/*
* M5Stamp Timer Power 2 (PY32L020) - Independent Watchdog (IWDG)
* Platform: Arduino (PY32L020 Core) / HAL
* Purpose: Ensure system recovery from hangs.
*/
#include <Arduino.h>
// PY32L020 specific Watchdog handling might depend on Core version
// Using standard Arduino watchdog if implemented, or direct register access
void feedWatchdog() {
#ifdef HAL_WDG_MODULE_ENABLED
HAL_WDG_Clear(&hw_wdg_handle); // If HAL is available
#else
// Direct register write for PY32L020 IWDG
// Refresh Register address: 0x40003018 (Check PY32L020 Reference Manual)
*(volatile uint32_t*)0x40003018 = 0xAAAA;
#endif
}
void setup() {
Serial.begin(115200);
Serial.println("Starting System with Watchdog");
// Initialize and Start Watchdog
// Timeout depends on LSI clock (~40kHz) and prescaler/reload values
// Example: Setup for ~2 seconds timeout
// RCC Enable LSI (needed for IWDG)
RCC->CSR |= RCC_CSR_LSION;
while (!(RCC->CSR & RCC_CSR_LSIRDY));
// Enable IWDG Clock
RCC->APB1ENR |= RCC_APB1ENR_IWDGEN;
// Unlock IWDG
IWDG->KR = 0x5555;
// Set Prescaler (e.g., 64) and Reload Value
IWDG->PR = 0x06; // Prescaler 64
IWDG->RLR = 0xFFF; // Max reload
// Start IWDG
IWDG->KR = 0xCCCC;
Serial.println("Watchdog Started");
}
void loop() {
Serial.println("Loop Running...");
// Simulate work
delay(500);
// CRITICAL: Feed the Watchdog
feedWatchdog();
// Simulation of a hang (Uncomment to test reset)
// while(true);
delay(500);
// Feed again
feedWatchdog();
}