Search code examples
arduinowatchdognanointerruption

How to configure watchdog for the arduino Nano 33 BLE Sense board?


For the arduino Nano 33 BLE Sense board, the standard avr/wdt.h is not available. And it seems that no standard library provides it. How to use the watchdog system for this board ? I found no full information about it.

I've found the page https://www.mysensors.org/apidocs/group__avr__watchdog.html which allow to configure the reboot mode. And it works. But no way to configure the interruption mode with ISR() function. Moreover, there's no explanation about the manipulation of used register/variables for any fine configuration.

Simple code example with regular asynchronous stuff using the watchdog ISR() mechanism. It which works well with ATmega328 (e.g.UNO). But I do not find equivalent configuration for the Nano 33 BLE using the nRF52840.

# include <avr/wdt.h>

volatile byte led;
int k;

ISR(WDT_vect) {
  Serial.println("Asynchronous stuff in ISR() function");
  digitalWrite(LED_BUILTIN,led);
  led=!led;
}

void setup() {
  pinMode(LED_BUILTIN,OUTPUT);  
  led=0;
  
  Serial.begin(9600);
  while(!Serial) {}
  Serial.println("== R E B O O T ==");

  WDTCSR = ( 1 << WDE ) | ( 1 << WDCE );
  WDTCSR = ( 1 << WDP2 ) | ( 1 << WDP0 ) | ( 1 << WDIE ) ; // Interruption and timeout  1/2 s
}

void loop() {
  Serial.print("Loop #");
  Serial.println(k);
  if (k++%2) {
    Serial.println("Some stuff (even branch)");
    delay(1200);
  }
  else {
    Serial.println("Some stuff (odd branch)");
    delay(4800);
  }
}

Thks.


Solution

  • In fact, for the RF52940, I'm not sure that the watchdog can provide the needed interruption mechanism despite it exists the INTENSET bit for interruption according to the chipset doc https://content.arduino.cc/assets/Nano_BLE_MCU-nRF52840_PS_v1.1.pdf). But the good way is for sure the use of timers (and one can use more than just one, quite good).

    For a similar behaviour as the code given for the UNO before, here is the code one can write for the Nano BLE using a timer interrupt (see https://github.com/khoih-prog/NRF52_MBED_TimerInterrupt for full and usefull doc).

    #include "NRF52_MBED_TimerInterrupt.h"
    
    volatile byte led;
    volatile uint32_t count=0;
    NRF52_MBED_Timer myTimer(NRF_TIMER_1); 
    unsigned k=0;
    
    void myHandler() {
      digitalWrite(LED_BUILTIN,led);
      led=!led;
      count++;
    }
    
    void setup() {
      pinMode(LED_BUILTIN,OUTPUT);  
      led=0;
      
      Serial.begin(9600);
      while(!Serial) {}
      Serial.println("== R E B O O T ==");
    
      if (myTimer.attachInterruptInterval(500*1000, myHandler)) // each 1/2 second
        Serial.println("myTimer launched");
      else
        Serial.println("Can not set the timer");
    }
    
    void loop() {
      Serial.print("Loop #");
      Serial.print(k);
      Serial.print(" and nb times in Handler : ");
      Serial.println(count);
      if (k++%2) {
        Serial.println("Some stuff (even branch)");
        delay(1200);
      }
      else {
        Serial.println("Some stuff (odd branch)");
        delay(4800);
      }
    }