pebble
  • Tutorials
  • Get the SDK
  • Guides
  • Documentation
  • PebbleOS
  • Mobile App
  • Examples
  • Index 01
  • Community
  • Blog
  • More
Privacy
Cookies
Publish

PebbleOS

  • Overview
  • Start here
    • Getting started guide
    • Playground
  • Development
    • The pbl CLI
    • Build system
    • Samples
      • Threads
      • Mutexes
      • Semaphores
      • Message queues
      • Poll groups
      • Timers
      • Work queues
      • Delayed work
      • Heaps
      • Time
      • Idle states
      • Dining philosophers
      • Bounded buffer
      • Readers and writers
      • Sleeping barber
      • Priority inversion
      • Barrier
      • CRC
      • SHA-256
      • AES
      • Files
      • Settings
      • Translations
      • Input events
      • Loader
      • Log levels
      • Profiler
      • Shell commands
      • Task watchdog
      • Accelerometer
      • Ambient light
      • Backlight
      • Battery
      • Display
      • Flash
      • GPU
      • Magnetometer
      • Microphone
      • External RAM
      • Random numbers
      • Real-time clock
      • Speaker
      • Die temperature
      • Touchscreen
      • Vibration
      • Rust
      • Kernel benchmark
    • Emulator and simulator
    • Testing
    • Debugging
    • Conventions
  • System
    • Architecture overview
    • Kernel
    • Subsystems
    • Drivers
    • Services
    • Libraries
    • Third-party
    • Application SDK
  • Hardware
    • Boards
    • Programmers
  • Community
    • Contribution Guidelines
  • Reference
    • Reference
    • Kconfig options

Task watchdog

This sample watches its main thread with the task watchdog, on a channel that expires after a second without a feed.

Work that feeds the channel in time is never reported, and neither is waiting for work once the thread marks itself as waiting. Then the thread gets stuck on a semaphore nobody gives: the watchdog logs the stuck thread and calls the channel callback, which frees it. The sample overrides the pbl_task_wdt_stalled() and pbl_task_wdt_recovered() hooks to print what happens; the system uses them for its own reports.

On boards with a hardware watchdog, a channel that stayed expired for the grace period would reset the system. Here the callback recovers the thread first.

Run:

pbl samples samples/subsys/task_wdt/stall

Boards:

every board

Sources

src/main.c
#include <stdio.h>

#include <pbl/kernel/delay.h>
#include <pbl/kernel/sem.h>
#include <pbl/task_wdt/task_wdt.h>

#define TIMEOUT_MS 1000U
#define STEP_MS    200U
#define STEPS      10U

/* What the main thread waits on when it gets stuck; only the watchdog callback gives it. */
static PBL_SEM_DEFINE(s_resource, 0, 1);
static PBL_SEM_DEFINE(s_recovered, 0, 1);
static PBL_SEM_DEFINE(s_work, 0, 1);

/* Runs on the watchdog thread on each check that finds the channel expired. */
static void *prv_expired(int channel_id, void *user_data) {
  printf("task_wdt: channel %d expired, freeing what main waits on\n", channel_id);
  pbl_sem_give(&s_resource);
  return nullptr;
}

/* Hooks the system can override: here they print, the defaults do nothing. */
void pbl_task_wdt_stalled(const struct pbl_task_wdt_stall *stall) {
  printf("task_wdt: stalled, channels in use 0x%02x, fed in time 0x%02x\n", stall->active_mask,
         stall->fed_mask);
}

void pbl_task_wdt_recovered(void) {
  printf("task_wdt: recovered\n");
  pbl_sem_give(&s_recovered);
}

int main(void) {
  int channel;

  channel = pbl_task_wdt_add(nullptr, TIMEOUT_MS, prv_expired, nullptr);
  if (channel < 0) {
    fprintf(stderr, "task_wdt: no channel (%d)\n", channel);
    return channel;
  }
  printf("task_wdt: main watched on channel %d, fed every %u ms at least\n", channel, TIMEOUT_MS);

  /* Busy work that feeds in time is never reported. */
  for (unsigned int i = 0; i < STEPS; i++) {
    pbl_sleep_ms(STEP_MS);
    pbl_task_wdt_feed(channel);
  }
  printf("task_wdt: %u ms of work fed in time\n", STEPS * STEP_MS);

  /* Waiting for work is no stall: the channel does not expire meanwhile. */
  pbl_task_wdt_set_waiting(true);
  (void)pbl_sem_take(&s_work, PBL_MSEC(3 * TIMEOUT_MS));
  pbl_task_wdt_set_waiting(false);
  printf("task_wdt: waited %u ms for work, not reported\n", 3 * TIMEOUT_MS);

  /* Stuck on something that never comes, until the callback frees it. */
  printf("task_wdt: main gets stuck\n");
  (void)pbl_sem_take(&s_resource, PBL_FOREVER);
  pbl_task_wdt_feed(channel);
  printf("task_wdt: main unblocked, fed again\n");

  (void)pbl_sem_take(&s_recovered, PBL_FOREVER);

  pbl_task_wdt_delete(channel);

  printf("Sample done\n");

  return 0;
}

Output

task_wdt: main watched on channel \d+, fed every 1000 ms at least
task_wdt: 2000 ms of work fed in time
task_wdt: waited 3000 ms for work, not reported
task_wdt: main gets stuck
<KernelMain> not fed for \d+ ms
task_wdt: channel \d+ expired, freeing what main waits on
task_wdt: stalled, channels in use 0x[0-9a-f]{2}, fed in time 0x[0-9a-f]{2}
task_wdt: recovered
Sample done

Generated from PebbleOS 7753e801d. This page is maintained in the pebbleos repository: docs/samples/subsys/task_wdt/stall.md.

Overview

  • Sources
  • Output