aboutsummaryrefslogtreecommitdiff
path: root/doc/develop/cyclic.rst
blob: 893c269099a58af8b27c5b2b4044160f86dd3bec (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
.. SPDX-License-Identifier: GPL-2.0+

Cyclic functions
================

The cyclic function execution infrastruture provides a way to periodically
execute code, e.g. every 100ms. Examples for such functions might be LED
blinking etc. The functions that are hooked into this cyclic list should
be small timewise as otherwise the execution of the other code that relies
on a high frequent polling (e.g. UART rx char ready check) might be
delayed too much. To detect cyclic functions with an excessive execution
time, the Kconfig option `CONFIG_CYCLIC_MAX_CPU_TIME_US` was introduced.
It defines the maximum allowable execution time for such a cyclic function. The
first time the execution of a cyclic function exceeds this interval, a warning
will be displayed indicating the problem to the user.

Registering a cyclic function
-----------------------------

To register a cyclic function, use something like this::

    struct donkey {
        struct cyclic_info cyclic;
        void (*say)(const char *s);
    };

    static void cyclic_demo(struct cyclic_info *c)
    {
        struct donkey *donkey = container_of(c, struct donkey, cyclic);

        donkey->say("Are we there yet?");
    }

    int donkey_init(void)
    {
        struct donkey *donkey;

        /* Initialize donkey ... */

        /* Register demo cyclic function */
        cyclic_register(&donkey->cyclic, cyclic_demo, 10 * 1000, "cyclic_demo");
        
        return 0;
    }

This will register the function `cyclic_demo()` to be periodically
executed all 10ms.

How is this cyclic functionality integrated /  executed?
--------------------------------------------------------

The cyclic infrastructure integrates the main function responsible for
calling all registered cyclic functions cyclic_run() into the common
WATCHDOG_RESET macro. This guarantees that cyclic_run() is executed
very often, which is necessary for the cyclic functions to get scheduled
and executed at their configured periods.