]> Git Repo - J-u-boot.git/blob - cmd/cyclic.c
Merge patch series "Add AM64x Support to PRUSS and PRU_RPROC driver"
[J-u-boot.git] / cmd / cyclic.c
1 // SPDX-License-Identifier: GPL-2.0+
2 /*
3  * A general-purpose cyclic execution infrastructure, to allow "small"
4  * (run-time wise) functions to be executed at a specified frequency.
5  * Things like LED blinking or watchdog triggering are examples for such
6  * tasks.
7  *
8  * Copyright (C) 2022 Stefan Roese <[email protected]>
9  */
10
11 #include <command.h>
12 #include <cyclic.h>
13 #include <div64.h>
14 #include <malloc.h>
15 #include <time.h>
16 #include <vsprintf.h>
17 #include <linux/delay.h>
18
19 struct cyclic_demo_info {
20         uint delay_us;
21 };
22
23 static void cyclic_demo(void *ctx)
24 {
25         struct cyclic_demo_info *info = ctx;
26
27         /* Just a small dummy delay here */
28         udelay(info->delay_us);
29 }
30
31 static int do_cyclic_demo(struct cmd_tbl *cmdtp, int flag, int argc,
32                           char *const argv[])
33 {
34         struct cyclic_demo_info *info;
35         struct cyclic_info *cyclic;
36         uint time_ms;
37
38         if (argc < 3)
39                 return CMD_RET_USAGE;
40
41         info = malloc(sizeof(struct cyclic_demo_info));
42         if (!info) {
43                 printf("out of memory\n");
44                 return CMD_RET_FAILURE;
45         }
46
47         time_ms = simple_strtoul(argv[1], NULL, 0);
48         info->delay_us = simple_strtoul(argv[2], NULL, 0);
49
50         /* Register demo cyclic function */
51         cyclic = cyclic_register(cyclic_demo, time_ms * 1000, "cyclic_demo",
52                                  info);
53         if (!cyclic)
54                 printf("Registering of cyclic_demo failed\n");
55
56         printf("Registered function \"%s\" to be executed all %dms\n",
57                "cyclic_demo", time_ms);
58
59         return 0;
60 }
61
62 static int do_cyclic_list(struct cmd_tbl *cmdtp, int flag, int argc,
63                           char *const argv[])
64 {
65         struct cyclic_info *cyclic;
66         struct hlist_node *tmp;
67         u64 cnt, freq;
68
69         hlist_for_each_entry_safe(cyclic, tmp, cyclic_get_list(), list) {
70                 cnt = cyclic->run_cnt * 1000000ULL * 100ULL;
71                 freq = lldiv(cnt, timer_get_us() - cyclic->start_time_us);
72                 printf("function: %s, cpu-time: %lld us, frequency: %lld.%02d times/s\n",
73                        cyclic->name, cyclic->cpu_time_us,
74                        lldiv(freq, 100), do_div(freq, 100));
75         }
76
77         return 0;
78 }
79
80 U_BOOT_LONGHELP(cyclic,
81         "demo <cycletime_ms> <delay_us> - register cyclic demo function\n"
82         "cyclic list - list cyclic functions\n");
83
84 U_BOOT_CMD_WITH_SUBCMDS(cyclic, "Cyclic", cyclic_help_text,
85         U_BOOT_SUBCMD_MKENT(demo, 3, 1, do_cyclic_demo),
86         U_BOOT_SUBCMD_MKENT(list, 1, 1, do_cyclic_list));
This page took 0.030052 seconds and 4 git commands to generate.