如何为Arduino Nano 33 BLE Sense板配置看门狗?

huangapple go评论79阅读模式
英文:

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

问题

对于Arduino Nano 33 BLE Sense板,标准的avr/wdt.h库不可用。似乎也没有标准库提供此功能。如何在这个板上使用看门狗系统?我找不到完整的信息。

我找到了这个页面https://www.mysensors.org/apidocs/group__avr__watchdog.html,可以配置重启模式。它有效。但没有办法使用ISR()函数配置中断模式。此外,并没有解释如何操作寄存器/变量进行精细配置。

以下是使用看门狗的常规异步操作的简单代码示例,该代码可以在ATmega328(例如UNO)上使用ISR()机制运行良好。但我找不到相当于Nano 33 BLE使用nRF52840的等效配置。

英文:

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.

答案1

得分: 0

实际上,对于RF52940,我并不确定看门狗是否能提供所需的中断机制,尽管根据芯片文档存在用于中断的INTENSET位 https://content.arduino.cc/assets/Nano_BLE_MCU-nRF52840_PS_v1.1.pdf)。 但肯定的好方法是使用定时器(而且可以使用不止一个,相当好)。

要实现与之前给出的UNO代码类似的行为,可以使用定时器中断编写Nano BLE的以下代码(请参阅 https://github.com/khoih-prog/NRF52_MBED_TimerInterrupt 获取完整且有用的文档)。

#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)) // 每0.5秒一次
    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);
  }
}
英文:

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);
  }
}

huangapple
  • 本文由 发表于 2023年2月10日 04:53:00
  • 转载请务必保留本文链接:https://go.coder-hub.com/75404319.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定