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 | dec7ea0 | 2024-05-20 13:35:03 -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> |
Rasmus Villemoes | ea36ada | 2024-05-21 10:46:52 +0200 | [diff] [blame] | 18 | #include <linux/kernel.h> |
Stefan Roese | 2702b27 | 2022-09-02 13:57:51 +0200 | [diff] [blame] | 19 | |
| 20 | struct cyclic_demo_info { |
Rasmus Villemoes | ea36ada | 2024-05-21 10:46:52 +0200 | [diff] [blame] | 21 | struct cyclic_info cyclic; |
Stefan Roese | 2702b27 | 2022-09-02 13:57:51 +0200 | [diff] [blame] | 22 | uint delay_us; |
| 23 | }; |
| 24 | |
Rasmus Villemoes | ea36ada | 2024-05-21 10:46:52 +0200 | [diff] [blame] | 25 | static void cyclic_demo(struct cyclic_info *c) |
Stefan Roese | 2702b27 | 2022-09-02 13:57:51 +0200 | [diff] [blame] | 26 | { |
Rasmus Villemoes | ea36ada | 2024-05-21 10:46:52 +0200 | [diff] [blame] | 27 | struct cyclic_demo_info *info = container_of(c, struct cyclic_demo_info, cyclic); |
Stefan Roese | 2702b27 | 2022-09-02 13:57:51 +0200 | [diff] [blame] | 28 | |
| 29 | /* Just a small dummy delay here */ |
| 30 | udelay(info->delay_us); |
| 31 | } |
| 32 | |
| 33 | static int do_cyclic_demo(struct cmd_tbl *cmdtp, int flag, int argc, |
| 34 | char *const argv[]) |
| 35 | { |
| 36 | struct cyclic_demo_info *info; |
Stefan Roese | 2702b27 | 2022-09-02 13:57:51 +0200 | [diff] [blame] | 37 | uint time_ms; |
| 38 | |
| 39 | if (argc < 3) |
| 40 | return CMD_RET_USAGE; |
| 41 | |
| 42 | info = malloc(sizeof(struct cyclic_demo_info)); |
| 43 | if (!info) { |
| 44 | printf("out of memory\n"); |
| 45 | return CMD_RET_FAILURE; |
| 46 | } |
| 47 | |
| 48 | time_ms = simple_strtoul(argv[1], NULL, 0); |
| 49 | info->delay_us = simple_strtoul(argv[2], NULL, 0); |
| 50 | |
| 51 | /* Register demo cyclic function */ |
Rasmus Villemoes | ea36ada | 2024-05-21 10:46:52 +0200 | [diff] [blame] | 52 | cyclic_register(&info->cyclic, cyclic_demo, time_ms * 1000, "cyclic_demo"); |
Stefan Roese | 2702b27 | 2022-09-02 13:57:51 +0200 | [diff] [blame] | 53 | |
| 54 | printf("Registered function \"%s\" to be executed all %dms\n", |
| 55 | "cyclic_demo", time_ms); |
| 56 | |
| 57 | return 0; |
| 58 | } |
| 59 | |
| 60 | static int do_cyclic_list(struct cmd_tbl *cmdtp, int flag, int argc, |
| 61 | char *const argv[]) |
| 62 | { |
Rasmus Villemoes | 85c2aa8 | 2022-10-28 13:50:53 +0200 | [diff] [blame] | 63 | struct cyclic_info *cyclic; |
| 64 | struct hlist_node *tmp; |
Stefan Roese | 2702b27 | 2022-09-02 13:57:51 +0200 | [diff] [blame] | 65 | u64 cnt, freq; |
| 66 | |
Rasmus Villemoes | 85c2aa8 | 2022-10-28 13:50:53 +0200 | [diff] [blame] | 67 | hlist_for_each_entry_safe(cyclic, tmp, cyclic_get_list(), list) { |
Stefan Roese | 2702b27 | 2022-09-02 13:57:51 +0200 | [diff] [blame] | 68 | cnt = cyclic->run_cnt * 1000000ULL * 100ULL; |
| 69 | freq = lldiv(cnt, timer_get_us() - cyclic->start_time_us); |
| 70 | printf("function: %s, cpu-time: %lld us, frequency: %lld.%02d times/s\n", |
| 71 | cyclic->name, cyclic->cpu_time_us, |
| 72 | lldiv(freq, 100), do_div(freq, 100)); |
| 73 | } |
| 74 | |
| 75 | return 0; |
| 76 | } |
| 77 | |
Tom Rini | 03f146c | 2023-10-07 15:13:08 -0400 | [diff] [blame] | 78 | U_BOOT_LONGHELP(cyclic, |
Alexander Dahl | 81c24f2 | 2023-08-04 17:53:23 +0200 | [diff] [blame] | 79 | "demo <cycletime_ms> <delay_us> - register cyclic demo function\n" |
Tom Rini | 03f146c | 2023-10-07 15:13:08 -0400 | [diff] [blame] | 80 | "cyclic list - list cyclic functions\n"); |
Stefan Roese | 2702b27 | 2022-09-02 13:57:51 +0200 | [diff] [blame] | 81 | |
| 82 | U_BOOT_CMD_WITH_SUBCMDS(cyclic, "Cyclic", cyclic_help_text, |
| 83 | U_BOOT_SUBCMD_MKENT(demo, 3, 1, do_cyclic_demo), |
| 84 | U_BOOT_SUBCMD_MKENT(list, 1, 1, do_cyclic_list)); |