Skip to content

Commit 73aea2e

Browse files
committed
updated sleep functions
1 parent 31d62b2 commit 73aea2e

File tree

1 file changed

+18
-15
lines changed

1 file changed

+18
-15
lines changed

core/platform/lf_STM32f4_support.c

Lines changed: 18 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -92,38 +92,42 @@ int _lf_clock_now(instant_t *t)
9292
}
9393

9494
/**
95-
* Make the STM32 go honk shoo mimimi for set nanoseconds
96-
* I essentially stole this from the lf_nrf52 support
95+
* Make the STM32 sleep for set nanoseconds
96+
* Based on the lf_nrf52 support implementation
9797
*/
9898
int lf_sleep(interval_t sleep_duration) {
9999
instant_t target_time;
100100
instant_t current_time;
101101

102102
_lf_clock_now(&current_time);
103103
target_time = current_time + sleep_duration;
104+
105+
// HAL_Delay only supports miliseconds. We try to use that for as long as possible
106+
// before switching to another meothd for finer tuned delay times
107+
long delaytime_ms = sleep_duration / 1000000;
108+
HAL_Delay(delaytime_ms);
109+
104110
while (current_time <= target_time)
105111
_lf_clock_now(&current_time);
106112

107113
return 0;
108114
}
109115

110116
/**
111-
* Make the STM32 go honk shoo honk shoo for set nanoseconds
112-
* This one uses a do-while loop. :)
113-
* I essentially stole this from the lf_nrf52 support
117+
* Make the STM32 sleep for set nanoseconds
118+
* Based on the lf_nrf52 support implementation
114119
*/
115120
static void lf_busy_wait_until(instant_t wakeup_time) {
116-
instant_t now;
117-
do {
118-
_lf_clock_now(&now);
119-
} while (now < wakeup_time);
121+
instant_t current_time;
122+
_lf_clock_now(&current_time);
123+
124+
// We repurpose the lf_sleep function here, just to better streamline the code
125+
interval_t sleep_duration = wakeup_time - current_time;
126+
lf_sleep(sleep_duration);
120127
}
121128

122129
// I am pretty sure this function doesnt work
123-
// Ill try to fix it once i know what the fuck its supposed to do, LOL
124-
/* sleep until wakeup time
125-
But, wake up if there is an async event
126-
130+
/* sleep until wakeup time but wake up if there is an async event
127131
*/
128132
int _lf_interruptable_sleep_until_locked(environment_t *env, instant_t wakeup_time) {
129133
// Get the current time and sleep time
@@ -134,7 +138,7 @@ int _lf_interruptable_sleep_until_locked(environment_t *env, instant_t wakeup_ti
134138
// Edge case handling for super small duration
135139
if (duration <= 0) {
136140
return 0;
137-
} else if (duration < 10) {
141+
} else if (duration < LF_MIN_SLEEP_NS) {
138142
lf_busy_wait_until(wakeup_time);
139143
return 0;
140144
}
@@ -145,7 +149,6 @@ int _lf_interruptable_sleep_until_locked(environment_t *env, instant_t wakeup_ti
145149

146150
do {
147151
_lf_clock_now(&now);
148-
149152
// Exit when the timer is up or there is an exception
150153
} while (!_lf_async_event && (now < wakeup_time));
151154

0 commit comments

Comments
 (0)