LwWDG v1.1.1 documentation

Welcome to the documentation for version v1.1.1.

LwWDG is lightweight watchdog library, primarily targeting operating systems, to watch multiple threads and reset system if one of them fails.

_images/logo.svg

Features

  • Written in ANSI C99

  • Easy to use - very little platform dependency

  • Written for operating systems in mind

Requirements

  • C compiler

  • Few kB of non-volatile memory

Contribute

Fresh contributions are always welcome. Simple instructions to proceed:

  1. Fork Github repository

  2. Respect C style & coding rules used by the library

  3. Create a pull request to develop branch with new features or bug fixes

Alternatively you may:

  1. Report a bug

  2. Ask for a feature request

License

MIT License

Copyright (c) 2023 Tilen MAJERLE

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.

Table of contents

Getting started

Getting started may be the most challenging part of every new library. This guide is describing how to start with the library quickly and effectively

Download library

Library is primarily hosted on Github.

You can get it by:

  • Downloading latest release from releases area on Github

  • Cloning main branch for latest stable version

  • Cloning develop branch for latest development

Download from releases

All releases are available on Github releases area.

Clone from Github
First-time clone

This is used when you do not have yet local copy on your machine.

  • Make sure git is installed.

  • Open console and navigate to path in the system to clone repository to. Use command cd your_path

  • Clone repository with one of available options below

    • Run git clone --recurse-submodules https://github.com/MaJerle/lwwdg command to clone entire repository, including submodules

    • Run git clone --recurse-submodules --branch develop https://github.com/MaJerle/lwwdg to clone development branch, including submodules

    • Run git clone --recurse-submodules --branch main https://github.com/MaJerle/lwwdg to clone latest stable branch, including submodules

  • Navigate to examples directory and run favourite example

Update cloned to latest version
  • Open console and navigate to path in the system where your repository is located. Use command cd your_path

  • Run git pull origin main command to get latest changes on main branch

  • Run git pull origin develop command to get latest changes on develop branch

  • Run git submodule update --init --remote to update submodules to latest version

Note

This is preferred option to use when you want to evaluate library and run prepared examples. Repository consists of multiple submodules which can be automatically downloaded when cloning and pulling changes from root repository.

Add library to project

At this point it is assumed that you have successfully download library, either cloned it or from releases page. Next step is to add the library to the project, by means of source files to compiler inputs and header files in search path

  • Copy lwwdg folder to your project, it contains library files

  • Add lwwdg/src/include folder to include path of your toolchain. This is where C/C++ compiler can find the files during compilation process. Usually using -I flag

  • Add source files from lwwdg/src/ folder to toolchain build. These files are built by C/C++ compiler. CMake configuration comes with the library, allows users to include library in the project as subdirectory and library.

  • Copy lwwdg/src/include/lwwdg/lwwdg_opts_template.h to project folder and rename it to lwwdg_opts.h

  • Build the project

Configuration file

Configuration file is used to overwrite default settings defined for the essential use case. Library comes with template config file, which can be modified according to needs. and it should be copied (or simply renamed in-place) and named lwwdg_opts.h

Note

Default configuration template file location: lwwdg/src/include/lwwdg/lwwdg_opts_template.h. File must be renamed to lwwdg_opts.h first and then copied to the project directory where compiler include paths have access to it by using #include "lwwdg_opts.h".

List of configuration options are available in the Configuration section. If any option is about to be modified, it should be done in configuration file

Template configuration file
 1/**
 2 * \file            lwwdg_opts_template.h
 3 * \brief           LwWDG configuration file
 4 */
 5
 6/*
 7 * Copyright (c) 2023 Tilen MAJERLE
 8 *
 9 * Permission is hereby granted, free of charge, to any person
10 * obtaining a copy of this software and associated documentation
11 * files (the "Software"), to deal in the Software without restriction,
12 * including without limitation the rights to use, copy, modify, merge,
13 * publish, distribute, sublicense, and/or sell copies of the Software,
14 * and to permit persons to whom the Software is furnished to do so,
15 * subject to the following conditions:
16 *
17 * The above copyright notice and this permission notice shall be
18 * included in all copies or substantial portions of the Software.
19 *
20 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
21 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
22 * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE
23 * AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
24 * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
25 * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
26 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
27 * OTHER DEALINGS IN THE SOFTWARE.
28 *
29 * This file is part of LWWDG - Lightweight watchdog for RTOS in embedded systems.
30 *
31 * Author:          Tilen MAJERLE <tilen@majerle.eu>
32 * Version:         v1.1.1
33 */
34#ifndef LWWDG_OPTS_HDR_H
35#define LWWDG_OPTS_HDR_H
36
37/* Rename this file to "lwwdg_opts.h" for your application */
38
39/*
40 * Open "include/lwwdg/lwwdg_opt.h" and
41 * copy & replace here settings you want to change values
42 */
43
44#endif /* LWWDG_OPTS_HDR_H */

Note

If you prefer to avoid using configuration file, application must define a global symbol LWWDG_IGNORE_USER_OPTS, visible across entire application. This can be achieved with -D compiler option.

User manual

LwWDG library is very simple and easy to use. LwWDG was designed to implement software watchdog functionality, primarily used in the operating systems.

Each task in the system defines its very own Watchdog structure, and is responsible to periodically call reload function, while one of the tasks, (it can be) called master task checks the processing function, and reloads hardware watchdog, if everything is fine.

When one of the software watchdogs isn’t reloaded within maximum timeout window, main task is not supposed to reload hardware timer anymore. As a consequence, hardware watchdog will reset the system.

Note

This library is designed for operating system, where operations are splitted in the tasks. To ensure stable operation, each task should have a monitoring system.

Since microcontrollers typical utilize single independent watchdog, such solution must be implemented as a combination of hardware and software components.

Platform migration

Library requires atomicity in the processing function, and a milliseconds time source. These should be implemented as macros in the configuration file. Checkout the configuration window or follow to example mentioned below.

Example

A simple example to illustrate functionality.

WIN32 example code
 1#include "lwwdg/lwwdg.h"
 2#include "windows.h"
 3#include <stdio.h>
 4#include <stdlib.h>
 5
 6HANDLE lwwdg_mutex;                        /* Mutex to simulate interrupt lock */
 7static LARGE_INTEGER freq, sys_start_time; /* Milliseconds time variable */
 8
 9/**
10 * \brief           Get current tick in ms from start of program
11 * \return          uint32_t: Tick in ms
12 */
13uint32_t
14sys_get_tick(void) {
15    LONGLONG ret;
16    LARGE_INTEGER now;
17
18    QueryPerformanceFrequency(&freq);
19    QueryPerformanceCounter(&now);
20    ret = now.QuadPart - sys_start_time.QuadPart;
21    return (uint32_t)((ret * 1000) / freq.QuadPart);
22}
23
24/* Task 1 */
25static void
26task1(void* arg) {
27    static lwwdg_wdg_t wdg;
28    (void)arg;
29
30    printf("%8u: Task 1 started...\r\n", (unsigned)sys_get_tick());
31    lwwdg_add(&wdg, 3000);
32    lwwdg_set_name(&wdg, "task_1_wdg");
33    while (1) {
34        /* Periodic reloads... */
35        lwwdg_reload(&wdg);
36    }
37}
38
39/* Task 1 */
40static void
41task2(void* arg) {
42    static lwwdg_wdg_t wdg;
43    (void)arg;
44
45    printf("%8u: Task 2 started...\r\n", (unsigned)sys_get_tick());
46    lwwdg_add(&wdg, 5000);
47    lwwdg_set_name(&wdg, "task_2_wdg");
48    while (1) {
49        /* No reload in this task -> consider it failed to run properly */
50        Sleep(1000);
51    }
52}
53
54/**
55 * \brief           Example main function
56 */
57void
58example_win32(void) {
59    DWORD id;
60
61    QueryPerformanceFrequency(&freq);
62    QueryPerformanceCounter(&sys_start_time);
63
64    /* Create lock for lwwdg */
65    lwwdg_mutex = CreateMutex(NULL, 0, NULL);
66    lwwdg_init(); /* Init library */
67
68    /* Start other tasks */
69    CreateThread(0, 0, (LPTHREAD_START_ROUTINE)task1, NULL, 0, &id);
70    CreateThread(0, 0, (LPTHREAD_START_ROUTINE)task2, NULL, 0, &id);
71
72    /* Main task... */
73    while (1) {
74        /* Check if all tasks are OK */
75        if (lwwdg_process()) {
76            printf("%8u: Refreshing hardware watchdog...\r\n", (unsigned)sys_get_tick());
77            /* TODO: This is where you should reload hardware watchdog */
78        } else {
79            printf("%8u: At least one task is out of window -> HW watchdog should not be reloaded anymore\r\n",
80                   (unsigned)sys_get_tick());
81            break;
82        }
83        Sleep(500); /* Make some sleep to offload messages in the WIN32 example */
84    }
85    printf("List of expired watchdogs\r\n");
86    lwwdg_print_expired();
87    printf("Example completed - a hardware should reset the system now...\r\n");
88}

A corresponding options file, tested for WIN32.

WIN32 LwWDG options file
 1/**
 2 * \file            lwwdg_opts_template.h
 3 * \brief           LwWDG configuration file
 4 */
 5
 6/*
 7 * Copyright (c) 2023 Tilen MAJERLE
 8 *
 9 * Permission is hereby granted, free of charge, to any person
10 * obtaining a copy of this software and associated documentation
11 * files (the "Software"), to deal in the Software without restriction,
12 * including without limitation the rights to use, copy, modify, merge,
13 * publish, distribute, sublicense, and/or sell copies of the Software,
14 * and to permit persons to whom the Software is furnished to do so,
15 * subject to the following conditions:
16 *
17 * The above copyright notice and this permission notice shall be
18 * included in all copies or substantial portions of the Software.
19 *
20 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
21 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
22 * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE
23 * AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
24 * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
25 * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
26 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
27 * OTHER DEALINGS IN THE SOFTWARE.
28 *
29 * This file is part of LWWDG - Lightweight watchdog for RTOS in embedded systems.
30 *
31 * Author:          Tilen MAJERLE <tilen@majerle.eu>
32 * Version:         v1.1.1
33 */
34#ifndef LWWDG_HDR_OPTS_H
35#define LWWDG_HDR_OPTS_H
36
37#include <stdio.h>
38
39/* Win32 port */
40#include "windows.h"
41extern uint32_t sys_get_tick(void);   /* Milliseconds tick is available externally */
42extern HANDLE lwwdg_mutex;            /* Mutex is defined and initialized externally */
43
44#define LWWDG_CRITICAL_SECTION_DEFINE /* Nothing to do here... */
45#define LWWDG_CRITICAL_SECTION_LOCK()                                                                                  \
46    do {                                                                                                               \
47        WaitForSingleObject(lwwdg_mutex, INFINITE);                                                                    \
48    } while (0)
49#define LWWDG_CRITICAL_SECTION_UNLOCK()     ReleaseMutex(lwwdg_mutex)
50#define LWWDG_GET_TIME()                    sys_get_tick()
51
52#define LWWDG_CFG_ENABLE_WDG_NAME           1
53#define LWWDG_CFG_WDG_NAME_ERR_DEBUG(_wdg_) printf("Watchdog %s failed to reload in time!\r\n", (_wdg_))
54
55#endif /* LWWDG_HDR_OPTS_H */

API reference

List of all the modules:

LwWDG

group LWWDG

Lightweight watchdog for RTOS in embedded systems.

Functions

uint8_t lwwdg_init(void)

Initialize watchdog module.

Returns

1 on success, 0 otherwise

uint8_t lwwdg_add(lwwdg_wdg_t *wdg, uint32_t timeout)

Add new watchdog timer instance to internal linked list.

Parameters
  • wdg – Watchdog handle. Must not be local variable

  • timeout – Max allowed timeout in milliseconds

Returns

1 on success, 0 otherwise

uint8_t lwwdg_remove(lwwdg_wdg_t *wdg)

Remove watchdog from the list.

This function is typically used if a task is killed by the scheduler. A user must manually call the function and can later clean wdg memory

Parameters

wdg – Watchdog handle to remove from list

Returns

1 if removed, 0 otherwise

uint8_t lwwdg_reload(lwwdg_wdg_t *wdg)

Reload thread watchdog.

Note

Reload will not be successful, if there was a timeout before. This will ensure that main thread won’t reload hardware watchdog, resulting system to reset

Parameters

wdg – Watchdog handle to reload

Returns

1 on success, 0 otherwise

uint8_t lwwdg_process(void)

Process and check system timers.

Function will check all timers and will return OK, if all timers are within max timeout state

Returns

1 if hardware watchdog can be reset, 0 if at least one timer hasn’t been reloaded within maximum timeout

void lwwdg_set_name(lwwdg_wdg_t *wdg, const char *name)

Set the watchdog name for debug reasons.

Note

Available only when LWWDG_CFG_ENABLE_WDG_NAME is enabled

Parameters
  • wdg – Watchdog instance

  • name – Pointer to the constant string for the name. String is not copied, rather only pointer is set

void lwwdg_print_expired(void)

Print all expired watchdogs.

Note

LWWDG_CFG_ENABLE_WDG_NAME and LWWDG_CFG_WDG_NAME_ERR_DEBUG must be enabled and implemented

struct lwwdg_wdg_t
#include <lwwdg.h>

Watchdog structure.

Public Members

struct lwwdg_wdg *next

Next entry on a list

uint32_t timeout

Timeout in milliseconds

uint32_t last_reload_time

Last reload time in milliseconds

const char *name

Pointer to constant string indicating watchdog name

Configuration

This is the default configuration of the middleware. When any of the settings shall be modified, it shall be done in dedicated application config lwwdg_opts.h file.

Note

Check Getting started to create configuration file.

group LWWDG_OPT

Default configuration setup.

Defines

LWWDG_CFG_ENABLE_WDG_NAME

Enables or disables field in wdg structure to contain watchdog name.

This can be useful for debugging purposes

LWWDG_CFG_WDG_NAME_ERR_DEBUG(_wdg_name_)

Macro called if LWWDG_CFG_ENABLE_WDG_NAME is enabled and if watchdog error occurs.

It can be overwritten by the application to print watchdog name.

Parameters
  • <em>wdg_name</em>[in] Watchdog name as defined by lwwdg_set_name function

LWWDG_GET_TIME()

Get system time in milliseconds.

It is required to keep reload time

LWWDG_CRITICAL_SECTION_DEFINE

Define the critical section.

Used at the beinning of the function, to define potential local variable to keep status of critical section (if already locked)

Note

Default implementation is for Cortex-M

LWWDG_CRITICAL_SECTION_LOCK()

Lock the critical section.

Critical section should prevent other tasks or interrupt, to access to the same core.

Easiest is to simply disable the interrupt, since task is normally pretty quick.

Note

Default implementation is for Cortex-M

LWWDG_CRITICAL_SECTION_UNLOCK()

Unlock the critical section.

Note

Default implementation is for Cortex-M

Changelog

# Changelog

## Develop

## v1.1.1

- Fix wrong time check

## v1.1.0

- Print erro0 message only on one trial
- Add print option to list all expired watchdogs

## v1.0.0

- Added option to remove wdg from the list
- Added option to set watchdog name and print its name on error

## v0.0.1

- First version
****