Stefan Roese | 2702b27 | 2022-09-02 13:57:51 +0200 | [diff] [blame] | 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 <sr@denx.de> |
| 9 | */ |
| 10 | |
Stefan Roese | 2702b27 | 2022-09-02 13:57:51 +0200 | [diff] [blame] | 11 | #include <command.h> |
| 12 | #include <cyclic.h> |
| 13 | #include <div64.h> |
| 14 | #include <malloc.h> |
Tom Rini | 5c1444f | 2024-04-27 08:10:59 -0600 | [diff] [blame] | 15 | #include <time.h> |
| 16 | #include <vsprintf.h> |
Stefan Roese | 2702b27 | 2022-09-02 13:57:51 +0200 | [diff] [blame] | 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 | { |
Rasmus Villemoes | 85c2aa8 | 2022-10-28 13:50:53 +0200 | [diff] [blame] | 65 | struct cyclic_info *cyclic; |
| 66 | struct hlist_node *tmp; |
Stefan Roese | 2702b27 | 2022-09-02 13:57:51 +0200 | [diff] [blame] | 67 | u64 cnt, freq; |
| 68 | |
Rasmus Villemoes | 85c2aa8 | 2022-10-28 13:50:53 +0200 | [diff] [blame] | 69 | hlist_for_each_entry_safe(cyclic, tmp, cyclic_get_list(), list) { |
Stefan Roese | 2702b27 | 2022-09-02 13:57:51 +0200 | [diff] [blame] | 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 | |
Tom Rini | 03f146c | 2023-10-07 15:13:08 -0400 | [diff] [blame] | 80 | U_BOOT_LONGHELP(cyclic, |
Alexander Dahl | 81c24f2 | 2023-08-04 17:53:23 +0200 | [diff] [blame] | 81 | "demo <cycletime_ms> <delay_us> - register cyclic demo function\n" |
Tom Rini | 03f146c | 2023-10-07 15:13:08 -0400 | [diff] [blame] | 82 | "cyclic list - list cyclic functions\n"); |
Stefan Roese | 2702b27 | 2022-09-02 13:57:51 +0200 | [diff] [blame] | 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)); |