wdenk | 3863585 | 2002-08-27 05:55:31 +0000 | [diff] [blame] | 1 | /* |
| 2 | * (C) Copyright 2001 |
| 3 | * Wolfgang Denk, DENX Software Engineering, wd@denx.de. |
| 4 | * |
Wolfgang Denk | 1a45966 | 2013-07-08 09:37:19 +0200 | [diff] [blame] | 5 | * SPDX-License-Identifier: GPL-2.0+ |
wdenk | 3863585 | 2002-08-27 05:55:31 +0000 | [diff] [blame] | 6 | */ |
| 7 | |
| 8 | /* |
| 9 | * Misc functions |
| 10 | */ |
| 11 | #include <common.h> |
| 12 | #include <command.h> |
Simon Glass | 24b852a | 2015-11-08 23:47:45 -0700 | [diff] [blame] | 13 | #include <console.h> |
wdenk | 3863585 | 2002-08-27 05:55:31 +0000 | [diff] [blame] | 14 | |
Kim Phillips | 088f1b1 | 2012-10-29 13:34:31 +0000 | [diff] [blame] | 15 | static int do_sleep(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[]) |
wdenk | 3863585 | 2002-08-27 05:55:31 +0000 | [diff] [blame] | 16 | { |
Stefan Roese | c4c13df | 2005-10-20 16:36:44 +0200 | [diff] [blame] | 17 | ulong start = get_timer(0); |
mario.six@gdsys.cc | c497463 | 2016-07-19 16:20:13 +0200 | [diff] [blame] | 18 | ulong mdelay = 0; |
wdenk | 3863585 | 2002-08-27 05:55:31 +0000 | [diff] [blame] | 19 | ulong delay; |
mario.six@gdsys.cc | c497463 | 2016-07-19 16:20:13 +0200 | [diff] [blame] | 20 | char *frpart; |
wdenk | 3863585 | 2002-08-27 05:55:31 +0000 | [diff] [blame] | 21 | |
Wolfgang Denk | 47e26b1 | 2010-07-17 01:06:04 +0200 | [diff] [blame] | 22 | if (argc != 2) |
Simon Glass | 4c12eeb | 2011-12-10 08:44:01 +0000 | [diff] [blame] | 23 | return CMD_RET_USAGE; |
wdenk | 3863585 | 2002-08-27 05:55:31 +0000 | [diff] [blame] | 24 | |
Jean-Christophe PLAGNIOL-VILLARD | 6d0f6bc | 2008-10-16 15:01:15 +0200 | [diff] [blame] | 25 | delay = simple_strtoul(argv[1], NULL, 10) * CONFIG_SYS_HZ; |
wdenk | 3863585 | 2002-08-27 05:55:31 +0000 | [diff] [blame] | 26 | |
mario.six@gdsys.cc | c497463 | 2016-07-19 16:20:13 +0200 | [diff] [blame] | 27 | frpart = strchr(argv[1], '.'); |
| 28 | |
| 29 | if (frpart) { |
| 30 | uint mult = CONFIG_SYS_HZ / 10; |
| 31 | for (frpart++; *frpart != '\0' && mult > 0; frpart++) { |
| 32 | if (*frpart < '0' || *frpart > '9') { |
| 33 | mdelay = 0; |
| 34 | break; |
| 35 | } |
| 36 | mdelay += (*frpart - '0') * mult; |
| 37 | mult /= 10; |
| 38 | } |
| 39 | } |
| 40 | |
| 41 | delay += mdelay; |
| 42 | |
Stefan Roese | c4c13df | 2005-10-20 16:36:44 +0200 | [diff] [blame] | 43 | while (get_timer(start) < delay) { |
Kim Phillips | 088f1b1 | 2012-10-29 13:34:31 +0000 | [diff] [blame] | 44 | if (ctrlc()) |
Stefan Roese | c4c13df | 2005-10-20 16:36:44 +0200 | [diff] [blame] | 45 | return (-1); |
Wolfgang Denk | 47e26b1 | 2010-07-17 01:06:04 +0200 | [diff] [blame] | 46 | |
Kim Phillips | 088f1b1 | 2012-10-29 13:34:31 +0000 | [diff] [blame] | 47 | udelay(100); |
wdenk | 3863585 | 2002-08-27 05:55:31 +0000 | [diff] [blame] | 48 | } |
Stefan Roese | c4c13df | 2005-10-20 16:36:44 +0200 | [diff] [blame] | 49 | |
wdenk | 3863585 | 2002-08-27 05:55:31 +0000 | [diff] [blame] | 50 | return 0; |
| 51 | } |
Stefan Roese | c4c13df | 2005-10-20 16:36:44 +0200 | [diff] [blame] | 52 | |
wdenk | 0d49839 | 2003-07-01 21:06:45 +0000 | [diff] [blame] | 53 | U_BOOT_CMD( |
Detlev Zundel | 9912121 | 2007-05-23 19:02:41 +0200 | [diff] [blame] | 54 | sleep , 2, 1, do_sleep, |
Peter Tyser | 2fb2604 | 2009-01-27 18:03:12 -0600 | [diff] [blame] | 55 | "delay execution for some time", |
wdenk | 8bde7f7 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 56 | "N\n" |
mario.six@gdsys.cc | c497463 | 2016-07-19 16:20:13 +0200 | [diff] [blame] | 57 | " - delay execution for N seconds (N is _decimal_ and can be\n" |
| 58 | " fractional)" |
wdenk | 8bde7f7 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 59 | ); |
Joe Hershberger | da83bcd | 2012-10-03 12:14:57 +0000 | [diff] [blame] | 60 | |
| 61 | #ifdef CONFIG_CMD_TIMER |
| 62 | static int do_timer(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[]) |
| 63 | { |
| 64 | static ulong start; |
| 65 | |
| 66 | if (argc != 2) |
| 67 | return CMD_RET_USAGE; |
| 68 | |
| 69 | if (!strcmp(argv[1], "start")) |
| 70 | start = get_timer(0); |
| 71 | |
| 72 | if (!strcmp(argv[1], "get")) { |
| 73 | ulong msecs = get_timer(start) * 1000 / CONFIG_SYS_HZ; |
| 74 | printf("%ld.%03d\n", msecs / 1000, (int)(msecs % 1000)); |
| 75 | } |
| 76 | |
| 77 | return 0; |
| 78 | } |
| 79 | |
| 80 | U_BOOT_CMD( |
| 81 | timer, 2, 1, do_timer, |
| 82 | "access the system timer", |
| 83 | "start - Reset the timer reference.\n" |
| 84 | "timer get - Print the time since 'start'." |
| 85 | ); |
| 86 | #endif |