Skip to content

Commit 13a6c45

Browse files
Nicolas Pitrenashif
authored andcommitted
kernel: crude k_busy_wait() implementation
This allows for builds with CONFIG_SYS_CLOCK_EXISTS=n in which case busy waits are achieved with a crude CPU loop. If ever accuracy is needed even with such a configuration then implementing arch_busy_wait() should be considered. Signed-off-by: Nicolas Pitre <[email protected]>
1 parent b157031 commit 13a6c45

File tree

2 files changed

+24
-1
lines changed

2 files changed

+24
-1
lines changed

kernel/Kconfig

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -755,6 +755,18 @@ config SYS_CLOCK_MAX_TIMEOUT_DAYS
755755
algorithm is selected for conversion if maximum timeout represented in
756756
source frequency domain multiplied by target frequency fits in 64 bits.
757757

758+
config BUSYWAIT_CPU_LOOPS_PER_USEC
759+
int "Number of CPU loops per microsecond for crude busy looping"
760+
depends on !SYS_CLOCK_EXISTS && !ARCH_HAS_CUSTOM_BUSY_WAIT
761+
default 500
762+
help
763+
Calibration for crude CPU based busy loop duration. The default
764+
is assuming 1 GHz CPU and 2 cycles per loop. Reality is certainly
765+
much worse but all we want here is a ball-park figure that ought
766+
to be good enough for the purpose of being able to configure out
767+
system timer support. If accuracy is very important then
768+
implementing arch_busy_wait() should be considered.
769+
758770
config XIP
759771
bool "Execute in place"
760772
help

kernel/busy_wait.c

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ void z_impl_k_busy_wait(uint32_t usec_to_wait)
1919

2020
#if defined(CONFIG_ARCH_HAS_CUSTOM_BUSY_WAIT)
2121
arch_busy_wait(usec_to_wait);
22-
#else
22+
#elif defined(CONFIG_SYS_CLOCK_EXISTS)
2323
uint32_t start_cycles = k_cycle_get_32();
2424

2525
/* use 64-bit math to prevent overflow when multiplying */
@@ -37,6 +37,17 @@ void z_impl_k_busy_wait(uint32_t usec_to_wait)
3737
break;
3838
}
3939
}
40+
#else
41+
/*
42+
* Crude busy loop for the purpose of being able to configure out
43+
* system timer support.
44+
*/
45+
unsigned int loops_per_usec = CONFIG_BUSYWAIT_CPU_LOOPS_PER_USEC;
46+
unsigned int loops = loops_per_usec * usec_to_wait;
47+
48+
while (loops-- > 0) {
49+
arch_nop();
50+
}
4051
#endif
4152

4253
SYS_PORT_TRACING_FUNC_EXIT(k_thread, busy_wait, usec_to_wait);

0 commit comments

Comments
 (0)