// File: Reset.c
#include "Reset.h"
#include "ad5940.h"
#include <stdio.h>

/**
 * @brief  Checks and prints the cause of the last reset.
 * @note   The RSTSTA register is sticky. It must be cleared manually.
 */
void AD5940_CheckResetStatus(void)
{
  uint32_t rststa = AD5940_ReadReg(REG_ALLON_RSTSTA);
  
  // printf(">> Reset Status (0x%04X): ", rststa);
  
  if(rststa & 0x01) printf("POR ");
  if(rststa & 0x02) printf("EXT ");
  if(rststa & 0x04) printf("WDT ");
  if(rststa & 0x08) printf("MMR ");
  if(rststa == 0)   printf("None");
  
  // printf("\n");

  // Clear the reset status (Write 1 to clear)
  AD5940_WriteReg(REG_ALLON_RSTSTA, 0xF);
}

/**
 * @brief  Performs a Software Reset (MMR Reset).
 * @return AD5940Err
 */
AD5940Err AD5940_SoftReset(void)
{
  // Trigger Software Reset
  AD5940_SoftRst();
  
  // Wait for the chip to reboot (essential)
  AD5940_Delay10us(100); // 1ms wait
  
  // Re-initialize the AD5940 driver internal state (SPI, etc.)
  // This function is required after any reset.
  AD5940_Initialize();
  
  // Check and clear the reset flag (Should show MMR)
  AD5940_CheckResetStatus();
  
  return AD5940ERR_OK;
}