From fa48d9e972858e0834b41db4ef7a30b0e332ed5f Mon Sep 17 00:00:00 2001 From: liwenxiang1 Date: Wed, 9 Oct 2024 19:28:45 +0800 Subject: [PATCH] drivers/timers:Arch_alarm and arch_timer add up_ndelay interface Signed-off-by: liwenxiang1 --- drivers/timers/arch_alarm.c | 23 +++++++++++++++++++---- drivers/timers/arch_timer.c | 15 +++++++++++++++ include/nuttx/arch.h | 1 + 3 files changed, 35 insertions(+), 4 deletions(-) diff --git a/drivers/timers/arch_alarm.c b/drivers/timers/arch_alarm.c index d353a6d87a..759b266a35 100644 --- a/drivers/timers/arch_alarm.c +++ b/drivers/timers/arch_alarm.c @@ -50,14 +50,14 @@ static clock_t g_current_tick; * Private Functions ****************************************************************************/ -static void udelay_accurate(useconds_t microseconds) +static void ndelay_accurate(unsigned long nanoseconds) { struct timespec now; struct timespec end; struct timespec delta; ONESHOT_CURRENT(g_oneshot_lower, &now); - clock_nsec2time(&delta, (uint64_t)microseconds * NSEC_PER_USEC); + clock_nsec2time(&delta, nanoseconds); clock_timespec_add(&now, &delta, &end); while (clock_timespec_compare(&now, &end) < 0) @@ -426,13 +426,28 @@ void weak_function up_mdelay(unsigned int milliseconds) ****************************************************************************/ void weak_function up_udelay(useconds_t microseconds) +{ + up_ndelay(NSEC_PER_USEC * microseconds); +} + +/**************************************************************************** + * Name: up_ndelay + * + * Description: + * Delay inline for the requested number of nanoseconds. + * + * *** NOT multi-tasking friendly *** + * + ****************************************************************************/ + +void weak_function up_ndelay(unsigned long nanoseconds) { if (g_oneshot_lower != NULL) { - udelay_accurate(microseconds); + ndelay_accurate(nanoseconds); } else /* Oneshot timer hasn't been initialized yet */ { - udelay_coarse(microseconds); + udelay_coarse((nanoseconds + NSEC_PER_USEC - 1) / NSEC_PER_USEC); } } diff --git a/drivers/timers/arch_timer.c b/drivers/timers/arch_timer.c index 850777c010..782eb7a39c 100644 --- a/drivers/timers/arch_timer.c +++ b/drivers/timers/arch_timer.c @@ -466,3 +466,18 @@ void weak_function up_udelay(useconds_t microseconds) udelay_coarse(microseconds); } } + +/**************************************************************************** + * Name: up_ndelay + * + * Description: + * Delay inline for the requested number of nanoseconds. + * + * *** NOT multi-tasking friendly *** + * + ****************************************************************************/ + +void weak_function up_ndelay(unsigned long nanoseconds) +{ + up_udelay((nanoseconds + NSEC_PER_USEC - 1) / NSEC_PER_USEC); +} diff --git a/include/nuttx/arch.h b/include/nuttx/arch.h index e661ee2db0..16994ef714 100644 --- a/include/nuttx/arch.h +++ b/include/nuttx/arch.h @@ -2363,6 +2363,7 @@ char up_romgetc(FAR const char *ptr); void up_mdelay(unsigned int milliseconds); void up_udelay(useconds_t microseconds); +void up_ndelay(unsigned long nanoseconds); /**************************************************************************** * These are standard interfaces that are exported by the OS for use by the