Dear @MartinL, Thank you so much for your VALUABLE and PRECISE HINT. I have been able to sort out all the issues , I was facing previous to your advice.
I´m leaving the code I prepared, so anybody having issues with a SAMD21 SLEEPING requirement, can be efficient with the use of time. I felt this morning as I was MONKEYING arround, instead of aiming at the correct solution. A little nested though, but it works as expected.
Special comment - Something I discovered, that perhaps I certainly read on the fly, is that the Serial Monitor for Arduino IDE version 1.X is not able to restart when any sleep interrupt is placed into the MicroController. Hence, each time you want to check the Serial Monitor you will have to stop the Serial Monitor, before you press the Interrupt Button, and rapidly restart a new Serial monitor connection.
Thanks again - Best wishes for a week starting shortly!
THE CODE
/*
* Skectch to test the deep sleep characteristic of an Arduino IoT33 (SAMD21 microcontroller family)
* Author - JPVP
* May 21-2023
* Enjoy!
*/
volatile boolean flag = false;
int RoundCounter = 0;
int MicroSW_time = 0;
int last_MicroSW_time = 0;
void setup()
{
while(!Serial);
Serial.begin(9600);
Serial.println("Sketch ID - TaestLateNight1.2");
Serial.println("+++++++++++++++++++++++++++++");
Serial.println("");
Serial.println("Starting the Test for the sleepe Arduino Nano 33 IOT ( SAMD21 )... ");
delay(1000);
pinMode(13,OUTPUT);
pinMode(3,INPUT);
for ( int n = 0; n < 2; n++)
{
Serial.println("Checking the LED Counting up to 2 ... ");
digitalWrite(LED_BUILTIN, HIGH);
delay(400);
digitalWrite(LED_BUILTIN, LOW);
delay(200);
}
attachInterrupt(digitalPinToInterrupt(3), interrupt, HIGH);
Serial.flush();
// -------------------------------- Set Up Deep Sleep Mode -------------------------------
SCB->SCR |= SCB_SCR_SLEEPDEEP_Msk; // Set up the CPU to enter low power STANDBY (Deep Sleep) mode on running the WFI() function (Wait For Interrupt)
NVMCTRL->CTRLB.reg |= NVMCTRL_CTRLB_SLEEPPRM_DISABLED; // Disable auto power reduction during sleep - SAMD21 Errata 1.14.2
NVMCTRL->CTRLB.bit.SLEEPPRM = NVMCTRL_CTRLB_SLEEPPRM_DISABLED_Val; // Prevent the flash memory from powering down in sleep mode
// ---------------------------------------------------------------------------------------
USBDevice.detach(); // Consider deleting when fully in operation
// ------------------------- Place the microcontroller into deep sleep ------------------
// Due to a hardware bug on the SAMD21, the SysTick interrupts become active before the flash has powered up from sleep, causing a hard fault
// To prevent this the SysTick interrupts are disabled before entering sleep mode
SysTick->CTRL &= ~SysTick_CTRL_TICKINT_Msk; // Disable SysTick interrupts
__WFI(); // Put the SAMD21 into deep sleep, Zzzzzzzz ...
SysTick->CTRL |= SysTick_CTRL_TICKINT_Msk; // Enable SysTick interrupts
// ---------------------------------------------------------------------------------------
USBDevice.attach(); // Consider deleting when fully in operation
}
void loop()
{
Serial.println("Within the Main Loop ... ");
//SerialUSB.println("Printing thtrough the SerialUSB");
for ( int z = 0; z < 3; z++)
{
Serial.println("Checking the LED Counting up to 3 arround the Main Loop ... ");
digitalWrite(LED_BUILTIN, HIGH);
delay(200);
digitalWrite(LED_BUILTIN, LOW);
delay(1000);
}
delay(800);
Serial.print("Flag is ... ");Serial.println(flag);
if(flag)
{
Serial.begin(9600);
Serial.flush();
//SerialUSB.begin(9600);
//SerialUSB.flush();
digitalWrite(LED_BUILTIN, LOW);
Serial.println("If here, ISR is complete! ");
for ( int r = 0; r < 4; r++)
{
Serial.println("Checking the LED Counting up to 4 within the ISR ... ");
digitalWrite(LED_BUILTIN, HIGH);
delay(100);
digitalWrite(LED_BUILTIN, LOW);
delay(300);
}
Serial.println("----------------------------------------------------");
flag=false;
delay(1000);
Serial.print("Flag just before going to sleep 'zzzzz' is ... ");Serial.println(flag);
Serial.print("Interrupt RoundCounter [N°of times BUTTON at GPIO D3 is pressed] ... ");Serial.println(RoundCounter);
USBDevice.detach(); // Consider deleting when full in operation
// ------------------------- Place the microcontroller into deep sleep ------------------
// Due to a hardware bug on the SAMD21, the SysTick interrupts become active before the flash has powered up from sleep, causing a hard fault
// To prevent this the SysTick interrupts are disabled before entering sleep mode
SysTick->CTRL &= ~SysTick_CTRL_TICKINT_Msk; // Disable SysTick interrupts
__WFI(); // Put the SAMD21 into deep sleep, Zzzzzzzz...
SysTick->CTRL |= SysTick_CTRL_TICKINT_Msk; // Enable SysTick interrupts
// ---------------------------------------------------------------------------------------
USBDevice.attach(); // Consider deleting when full in operation
}
}
void interrupt()
{
flag = true;
// ----------- ISR debouncer ----------
MicroSW_time = millis();
if(MicroSW_time - last_MicroSW_time > 150)
{
RoundCounter = RoundCounter +1;
last_MicroSW_time = MicroSW_time;
}
}