blob: 40e966de9aa537c5565f4df44c29fdfc69e4a772 [file] [log] [blame]
Stefan Roese2702b272022-09-02 13:57:51 +02001// 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 <sr@denx.de>
9 */
10
Stefan Roese2702b272022-09-02 13:57:51 +020011#include <command.h>
12#include <cyclic.h>
13#include <div64.h>
14#include <malloc.h>
Tom Rinidec7ea02024-05-20 13:35:03 -060015#include <time.h>
16#include <vsprintf.h>
Stefan Roese2702b272022-09-02 13:57:51 +020017#include <linux/delay.h>
18
19struct cyclic_demo_info {
20 uint delay_us;
21};
22
23static 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
31static 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
62static int do_cyclic_list(struct cmd_tbl *cmdtp, int flag, int argc,
63 char *const argv[])
64{
Rasmus Villemoes85c2aa82022-10-28 13:50:53 +020065 struct cyclic_info *cyclic;
66 struct hlist_node *tmp;
Stefan Roese2702b272022-09-02 13:57:51 +020067 u64 cnt, freq;
68
Rasmus Villemoes85c2aa82022-10-28 13:50:53 +020069 hlist_for_each_entry_safe(cyclic, tmp, cyclic_get_list(), list) {
Stefan Roese2702b272022-09-02 13:57:51 +020070 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
Tom Rini03f146c2023-10-07 15:13:08 -040080U_BOOT_LONGHELP(cyclic,
Alexander Dahl81c24f22023-08-04 17:53:23 +020081 "demo <cycletime_ms> <delay_us> - register cyclic demo function\n"
Tom Rini03f146c2023-10-07 15:13:08 -040082 "cyclic list - list cyclic functions\n");
Stefan Roese2702b272022-09-02 13:57:51 +020083
84U_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));