Quick example code for power-off "last gasp" interrupt routine, SAMD51

1 post / 0 new
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Here is a quick snippet of code that triggers an interrupt routine for a "last gasp" operation as the device is powering down.  I use this to write a bunch of registers to the SmartEEPROM before the power falls below the BOD level.




    //Activate the AC clock:
    hri_gclk_write_PCHCTRL_reg(GCLK, AC_GCLK_ID, 0x1 | (1 << GCLK_PCHCTRL_CHEN_Pos));

    delay_ms(1); //overkill, but I am in no hurry at startup.
    AC->CTRLA.bit.ENABLE =0x1;  //Enable
        delay_ms(1); //overkill, but I am in no hurry at startup.
    AC->COMPCTRL[0].bit.ENABLE = 0x0;
        delay_ms(1);//overkill, but I am in no hurry at startup.
    AC->COMPCTRL[0].bit.SINGLE =0x0;
    AC ->COMPCTRL->bit.FLEN = 0x2; //Try max filtering.t
    AC ->INTENSET.bit.COMP0 = 0x1;
    AC ->COMPCTRL->bit.INTSEL = 0x2; //0x1 = Interrupt on rising edge //0x2= falling
    //Compare Vscale to Bandgap voltages
    AC->COMPCTRL[0].bit.MUXPOS = 0x4; //0x3 = Pin 3, PA07, POT3 //0x4 = Vscale    
    AC->COMPCTRL[0].bit.MUXNEG = 0x6; //0x5 = vscale    //0x6 = bandgap
    //Set the Vscale value.  22 triggers the interrupt at around 3.0V.    
    AC-> SCALER[0].bit.VALUE = 22 ; // 21 works good, try 24 for margin?
    //    Set up the interrupt
        NVIC_SetPriority(AC_IRQn , 1);
        AC->COMPCTRL[0].bit.ENABLE = 0x1;



And here is the interrupt routine:


void AC_Handler() {

//Do Last Gasp here.