Skip to content

Add support for nRF52 Watchdog #11684

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Oct 22, 2019
Merged

Conversation

manchoz
Copy link
Contributor

@manchoz manchoz commented Oct 14, 2019

Description

Add the Watchdog driver implementation for the nRF52 targets using the nRF5 SDK WDT API.

To enable the Watchdog driver for the nRF2 targets, you need to activate it adding the macro NRFX_WDT_ENABLED=1 in the macro section of mbed_app.json configuration file and adding the "WATCHDOG" key to the list of the device's drivers. Example file:

{
    "macros": [
        "MBED_HEAP_STATS_ENABLED=1",
        "MBED_STACK_STATS_ENABLED=1",
        "MBED_MEM_TRACING_ENABLED=1",
        "NRFX_GPIOTE_CONFIG_NUM_OF_LOW_POWER_EVENTS=8",
        "NRFX_WDT_ENABLED=1"
    ],
    "target_overrides": {
        "*": {
        "platform.stdio-buffered-serial": true,
        "platform.stdio-baud-rate": 115200,
        "platform.default-serial-baud-rate": 115200
    },
    "ARDUINO_NANO33BLE": {
        "target.device_has_add": ["WATCHDOG"]
    }
}

Pull request type

[ ] Fix
[ ] Refactor
[X] Target update
[] Functionality change
[ ] Docs update
[ ] Test update
[ ] Breaking change

@ciarmcom
Copy link
Member

@manchoz, thank you for your changes.
@ARMmbed/mbed-os-maintainers please review.

@ciarmcom ciarmcom requested a review from a team October 14, 2019 15:00
@manchoz manchoz force-pushed the nrf52_watchdog_api branch 2 times, most recently from 017c8d2 to c9370f3 Compare October 15, 2019 08:38
@0xc0170
Copy link
Contributor

0xc0170 commented Oct 21, 2019

@manchoz Thanks for the contribution. I edited the comment above. There is no need for release notes (as this is just a target update). I'll review the code shortly

Copy link
Contributor

@0xc0170 0xc0170 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Questions:

  • how was this tested
  • why watchdog is not enabled for any current supported targets

/*
* Copyright (c) 2019 Trampoline SRL
* Copyright (c) 2019 Giampaolo Mancini <[email protected]>
*
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

SPDX identifiers please to new files

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added SPDX for Apache-2.0.

@manchoz manchoz force-pushed the nrf52_watchdog_api branch from c851a06 to c30ca66 Compare October 21, 2019 13:41
@0xc0170
Copy link
Contributor

0xc0170 commented Oct 21, 2019

Remaining questions:

  • how was this tested
  • why watchdog is not enabled for any current supported targets

@manchoz
Copy link
Contributor Author

manchoz commented Oct 21, 2019

@0xc0170

Implementation has been tested on several Arduino Nano33Ble boards adapting the Watchdog example for the Arduino platform. The following sketch was used:

#include "mbed.h"
#include "drivers/Watchdog.h"

using namespace mbed;

REDIRECT_STDOUT_TO(Serial)

// Connect momentary button beetwen Pin 2 and GND
const byte interruptPin = 2;

// Maximum Watchdog timoute in seconds for nRF52840
// const int INIT_COUNTDOWN = 131071;

const int INIT_COUNTDOWN = 10;
int countdown = INIT_COUNTDOWN;

const uint32_t TIMEOUT_MS = countdown * 1000;

uint32_t now = millis();
const uint32_t interval = 1000;

uint32_t nowLed = millis();
uint32_t ledInterval = 1000;

void trigger()
{
    Watchdog::get_instance().kick();
    countdown = INIT_COUNTDOWN;
    ledInterval = 1000;
}

void setup()
{
    Serial.begin(115200);

    // Wait for connection on serial port
    while (!Serial)
        ;

    Serial.println();
    Serial.println("Target started ");
    Serial.println();

    pinMode(interruptPin, INPUT_PULLUP);
    attachInterrupt(digitalPinToInterrupt(interruptPin), trigger, FALLING);

    Watchdog &watchdog = Watchdog::get_instance();

    Serial.print("Enabling Watchdog. (");
    Serial.print((uint32_t)TIMEOUT_MS);
    Serial.println(" ms).");

    bool enabled = watchdog.start(TIMEOUT_MS);
    Serial.print("Status: ");
    Serial.println(enabled ? "Enabled": "Not Enabled");

    if (!enabled) {
        Serial.println("Timer not enabled. Invalid timeout parameter.");
        delay(2000);
        while (true);
    }

    uint32_t watchdog_timeout = watchdog.get_timeout();
    Serial.print("Watchdog initialized to ");
    Serial.print(watchdog_timeout);
    Serial.println(" ms.");

    Serial.print("Press BUTTON at least once every ");
    Serial.print(watchdog_timeout);
    Serial.println(" ms to kick the watchdog and prevent system reset");

    nowLed = millis();
    now = millis();
}

void loop()
{
    if (millis() - now > interval - (float)ledInterval / INIT_COUNTDOWN) {
        printf("\r%3i", countdown--);
        fflush(stdout);
        now = millis();
    }

    if (millis() - nowLed > ledInterval) {
        // Blink LED faster and faster as timout approaches
        digitalWrite(LED_BUILTIN, HIGH);
        delay(50);
        digitalWrite(LED_BUILTIN, LOW);
        ledInterval -= (float)ledInterval / INIT_COUNTDOWN;
        nowLed = millis();
    }
}

I really don't know why the watchdog for nRF52x have not been implemented yet on Mbed: it's implemented on almost any other RTOS.

@0xc0170
Copy link
Contributor

0xc0170 commented Oct 21, 2019

I see, on Arduino Nano33Ble that does not support daplink (cant be tested with our infrastructure, correct?).

Looks fine to me and even better if we can enable this for a common nrf target that can be tested?

@manchoz
Copy link
Contributor Author

manchoz commented Oct 21, 2019

@0xc0170

I have run a few tests with a Makerdiary nRF52850-MDK board (which supports DAPlink) in a standard Mbed OS scenario and everything seems ok.

Test code and instructions are at https://github.com/manchoz/mbed-os-example-watchdog-nrf52840.

(Please note that the repo contains also the support for the nRF52850-MDK board).

@0xc0170
Copy link
Contributor

0xc0170 commented Oct 22, 2019

CI started

@mbed-ci
Copy link

mbed-ci commented Oct 22, 2019

Test run: SUCCESS

Summary: 11 of 11 test jobs passed
Build number : 1
Build artifacts

@manchoz
Copy link
Contributor Author

manchoz commented Oct 22, 2019

@0xc0170 @ARMmbed/mbed-os-maintainers Thank you so much!

@manchoz manchoz deleted the nrf52_watchdog_api branch May 25, 2023 16:10
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants