Skip to content

Commit 5d8b496

Browse files
facchinmsoburi
authored andcommitted
zephyrCommon: improve delay* accurancy
experimental results here arduino#332 (comment) On boards with different clock speed, function call overhead etc. the results may vary but will likely be consistent, given that all the boards share CONFIG_SYS_CLOCK_TICKS_PER_SEC Co-authored-by: Martino Facchin <m.facchin@arduino.cc> Signed-off-by: TOKITA Hiroshi <tokita.hiroshi@gmail.com>
1 parent ce662c6 commit 5d8b496

File tree

1 file changed

+7
-4
lines changed

1 file changed

+7
-4
lines changed

cores/arduino/zephyrCommon.cpp

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -338,12 +338,15 @@ void noTone(pin_size_t pinNumber) {
338338
gpio_pin_set_dt(&arduino_pins[pinNumber], 0);
339339
}
340340

341-
void delay(unsigned long ms) {
342-
k_sleep(K_MSEC(ms));
341+
__attribute__((always_inline)) void delay(unsigned long ms) {
342+
k_sleep(K_MSEC(ms));
343343
}
344344

345-
void delayMicroseconds(unsigned int us) {
346-
k_busy_wait(us);
345+
__attribute__((always_inline)) void delayMicroseconds(unsigned int us) {
346+
if (us == 0) {
347+
return;
348+
}
349+
k_busy_wait(us - 1);
347350
}
348351

349352
unsigned long micros(void) {

0 commit comments

Comments
 (0)