Skip to content
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

Add support for nRF52 Watchdog #11684

Merged
merged 5 commits into from Oct 22, 2019

Conversation

@manchoz
Copy link
Contributor

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

This comment has been minimized.

Copy link
Member

ciarmcom commented Oct 14, 2019

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

@ciarmcom ciarmcom requested a review from ARMmbed/mbed-os-maintainers Oct 14, 2019
@manchoz manchoz force-pushed the manchoz:nrf52_watchdog_api branch 2 times, most recently from 017c8d2 to c9370f3 Oct 14, 2019
@0xc0170

This comment has been minimized.

Copy link
Member

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
Member

0xc0170 left a comment

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 <giampaolo@trampolineup.com>
*

This comment has been minimized.

Copy link
@0xc0170

0xc0170 Oct 21, 2019

Member

SPDX identifiers please to new files

This comment has been minimized.

Copy link
@manchoz

manchoz Oct 21, 2019

Author Contributor

Added SPDX for Apache-2.0.

@0xc0170 0xc0170 added needs: work and removed needs: review labels Oct 21, 2019
@manchoz manchoz force-pushed the manchoz:nrf52_watchdog_api branch from c851a06 to c30ca66 Oct 21, 2019
@0xc0170

This comment has been minimized.

Copy link
Member

0xc0170 commented Oct 21, 2019

Remaining questions:

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

This comment has been minimized.

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

This comment has been minimized.

Copy link
Member

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

This comment has been minimized.

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 0xc0170 added needs: CI and removed needs: work labels Oct 22, 2019
@0xc0170

This comment has been minimized.

Copy link
Member

0xc0170 commented Oct 22, 2019

CI started

@mbed-ci

This comment has been minimized.

Copy link

mbed-ci commented Oct 22, 2019

Test run: SUCCESS

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

@0xc0170 0xc0170 merged commit 5d330bf into ARMmbed:master Oct 22, 2019
25 checks passed
25 checks passed
continuous-integration/jenkins/pr-head This commit looks good
Details
jenkins-ci/build-ARM Success
Details
jenkins-ci/build-GCC_ARM Success
Details
jenkins-ci/build-IAR Success
Details
jenkins-ci/cloud-client-test Success
Details
jenkins-ci/dynamic-memory-usage RTOS ROM(+0 bytes) RAM(+0 bytes)
Details
jenkins-ci/exporter Success
Details
jenkins-ci/greentea-test Success
Details
jenkins-ci/mbed2-build-ARM Success
Details
jenkins-ci/mbed2-build-GCC_ARM Success
Details
jenkins-ci/mbed2-build-IAR Success
Details
jenkins-ci/unittests Success
Details
travis-ci/astyle Success!
Details
travis-ci/docs Success!
Details
travis-ci/doxy-spellcheck Success!
Details
travis-ci/events Success! Runtime is 8729 cycles.
Details
travis-ci/gitattributestest Success!
Details
travis-ci/include_check Success!
Details
travis-ci/licence_check Success!
Details
travis-ci/littlefs Success! Code size is 8464B.
Details
travis-ci/psa-autogen Success!
Details
travis-ci/tools-py2.7 Success!
Details
travis-ci/tools-py3.5 Success!
Details
travis-ci/tools-py3.6 Success!
Details
travis-ci/tools-py3.7 Success!
Details
@manchoz

This comment has been minimized.

Copy link
Contributor Author

manchoz commented Oct 22, 2019

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
4 participants
You can’t perform that action at this time.