summaryrefslogtreecommitdiff
path: root/lib
diff options
context:
space:
mode:
authorAdnan Rahman Chowdhury <[email protected]>2022-06-30 10:46:35 +0100
committerAnup Patel <[email protected]>2022-07-08 14:51:24 +0530
commit994c8cfb29d0b53bbcc774a728cad778aeece6ac (patch)
tree78169571385306969a1b507d6fa045879e3a30a2 /lib
parent4489876e933d8ba0d8bc6c64bae71e295d45faac (diff)
lib: sbi_timer: Added a conditional wait function which can timeout
Motivation: Suppose a peripheral needs to be configured to transmit data. There is an SFR bit which indicates that the peripheral is ready to transmit. The firmware should check the bit and will only transmit data when the peripheral is ready. When the firmware starts polling the SFR, the peripheral could be busy transmitting/receiving other data so the firmware must wait till that completes. Assuming that there is no other way, the firmware shouldn't wait indefinitely. The function sbi_timer_waitms_until() will constantly check whether a certain condition is satisfied, or timeout occurs. It can be used for the cases when a timeout is required. Signed-off-by: Adnan Rahman Chowdhury <[email protected]> Reviewed-by: Xiang W <[email protected]> Reviewed-by: Anup Patel <[email protected]>
Diffstat (limited to 'lib')
-rw-r--r--lib/sbi/sbi_timer.c13
1 files changed, 13 insertions, 0 deletions
diff --git a/lib/sbi/sbi_timer.c b/lib/sbi/sbi_timer.c
index b0a6e63f..4b24cbeb 100644
--- a/lib/sbi/sbi_timer.c
+++ b/lib/sbi/sbi_timer.c
@@ -76,6 +76,19 @@ void sbi_timer_delay_loop(ulong units, u64 unit_freq,
delay_fn(opaque);
}
+bool sbi_timer_waitms_until(bool (*predicate)(void *), void *arg,
+ uint64_t timeout_ms)
+{
+ uint64_t start_time = sbi_timer_value();
+ uint64_t ticks =
+ (sbi_timer_get_device()->timer_freq / 1000) *
+ timeout_ms;
+ while(!predicate(arg))
+ if (sbi_timer_value() - start_time >= ticks)
+ return false;
+ return true;
+}
+
u64 sbi_timer_value(void)
{
if (get_time_val)