watchdog.c

nRF52840-MDK (id boards.json: nrf52840-mdk) · Archivo : watchdog.c · Lenguaje : C · Uso : Surveiller le programme avec le watchdog matériel

El WDT del nRF52840 reinicia el chip si el firmware no "alimenta" su canal a tiempo: bucles congelados, bloqueos y fallos software se convierten en reinicios automáticos. Imprescindible en una caja sellada.

Hardware

Una placa nRF52840-MDK (Nordic nRF52840, Cortex-M4F @ 64 MHz, 1 MB flash / 256 KB RAM, BLE 5 + 802.15.4) con depurador integrado, programada en C con nRF5 SDK v17 y SoftDevice S140. nRF52840-MDK sola; un monitor serie opcional para observar los reinicios.

Cableado

Sin cableado; LED P0.06 como testigo de vida (se congela cuando el programa lo hace).

Funcionamiento

El canal asignado debe alimentarse antes del timeout (~8 s); en modo RUN_SLEEP el temporizador sigue durante el sueño. Expiración → reset del sistema, la ejecución vuelve a main().

Ejecución

Abre el ejemplo equivalente del nRF5 SDK v17 (objetivo custom_board), pega este código en main.c, compila con Segger Embedded Studio o el Makefile de gcc y programa por el depurador integrado.

Variantes

Alimentar el watchdog solo cuando cada subtarea haya girado (supervisión fina), o registrar la causa del reset (PFFRESETREAS) en flash.

watchdog.c

// nRF52840-MDK : watchdog matériel WDT — reset si la boucle principale gèle.
#include "nrf_drv_wdt.h"
#include "nrf_delay.h"

static nrf_drv_wdt_channel_id canal;

void wdt_evt(void) { }        // optionnel : avertir avant reset (PPI/LPCOMP)

int main(void) {
  nrf_drv_wdt_config_t cfg = NRF_DRV_WDT_DEAFULT_CONFIG;
  cfg.behaviour = NRF_WDT_BEHAVIOUR_RUN_SLEEP;   // tourne aussi en sommeil
  cfg.timeout   = WDT_TIMEOUT;                    // 8x32768 ticks ~ 8 s (adapter)
  nrf_drv_wdt_init(&cfg, wdt_evt);
  nrf_drv_wdt_channel_alloc(&canal);
  nrf_drv_wdt_enable();

  uint32_t tour = 0;
  for (;;) {
    nrf_drv_wdt_channel_feed(canal);   // "j'ai tourné" — sans ça : reset
    tour++;
    nrf_gpio_pin_toggle(6);
    nrf_delay_ms(200);

    // Démo : bloquer volontairement au tour 50 pour voir le reset.
    if (tour == 50) { while (1) { } }
  }
}

← Volver a la ficha