User manual

LwWDG library is very simple and easy to use. LwWDG was designed to implement software watchdog functionality, primarly 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 does not reload hardware watchdog anymore, and therefore hardware will trigger the hardware reset.

Platform migration

Library requires atomicity in the processing function, and a milliseconds time. These should be implemented as macro. Checkout the configuration window.

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 */
25void
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    while (1) {
33        /* Periodic reloads... */
34        lwwdg_reload(&wdg);
35    }
36}
37
38/* Task 1 */
39void
40task2(void* arg) {
41    static lwwdg_wdg_t wdg;
42    (void)arg;
43
44    printf("%8u: Task 2 started...\r\n", (unsigned)sys_get_tick());
45    lwwdg_add(&wdg, 5000);
46    while (1) {
47        /* No reload anymore... */
48        Sleep(1000);
49    }
50}
51
52/**
53 * \brief           Example main function
54 */
55void
56example_win32(void) {
57    DWORD id;
58
59    QueryPerformanceFrequency(&freq);
60    QueryPerformanceCounter(&sys_start_time);
61
62    /* Create lock for lwwdg */
63    lwwdg_mutex = CreateMutex(NULL, 0, NULL);
64    lwwdg_init(); /* Init library */
65
66    /* Start other tasks */
67    CreateThread(0, 0, (LPTHREAD_START_ROUTINE)task1, NULL, 0, &id);
68    CreateThread(0, 0, (LPTHREAD_START_ROUTINE)task2, NULL, 0, &id);
69
70    /* Main task... */
71    while (1) {
72        /* Check if all tasks are OK */
73        if (lwwdg_process()) {
74            printf("%8u: Refreshing hardware watchdog...\r\n", (unsigned)sys_get_tick());
75            /* TODO: This is where you should reload hardware watchdog */
76        } else {
77            printf("%8u: At least one task is out of window -> no refresh of hardware watchdog anymore...\r\n",
78                   (unsigned)sys_get_tick());
79        }
80        Sleep(500); /* Make some sleep to offload messages in the WIN32 example */
81    }
82    return 0;
83}