Skip to content

Commit 83aec11

Browse files
pillo79soburi
authored andcommitted
Arduino.h: properly inline delay and delayMicroseconds
Place the implementation for delay() and delayMicroseconds() in a separate header file, and include it from Arduino.h. This allows the functions to be properly inlined, which is important for performance, especially for delayMicroseconds(). Signed-off-by: Luca Burelli <l.burelli@arduino.cc> Signed-off-by: TOKITA Hiroshi <tokita.hiroshi@gmail.com>
1 parent b36fbfb commit 83aec11

File tree

3 files changed

+29
-11
lines changed

3 files changed

+29
-11
lines changed

cores/arduino/Arduino.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -116,6 +116,8 @@ int digitalPinToInterrupt(pin_size_t pin);
116116
#define LED_BUILTIN ZARD_LED_BUILTIN
117117
#endif // LED_BUILTIN
118118

119+
#include <inlines.h>
120+
119121
#ifdef __cplusplus
120122
#include <zephyrSerial.h>
121123
#endif // __cplusplus

cores/arduino/inlines.h

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
/*
2+
* Copyright (c) Arduino s.r.l. and/or its affiliated companies
3+
*
4+
* SPDX-License-Identifier: Apache-2.0
5+
*/
6+
7+
/*
8+
* Provide implementations for inline functions.
9+
*/
10+
11+
#ifndef __INLINES_H__
12+
#define __INLINES_H__
13+
14+
#include <zephyr/kernel.h>
15+
16+
inline __attribute__((always_inline)) void delay(unsigned long ms) {
17+
k_sleep(K_MSEC(ms));
18+
}
19+
20+
inline __attribute__((always_inline)) void delayMicroseconds(unsigned int us) {
21+
if (us == 0) {
22+
return;
23+
}
24+
k_busy_wait(us - 1);
25+
}
26+
27+
#endif /* __INLINES_H__ */

cores/arduino/zephyrCommon.cpp

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

341-
__attribute__((always_inline)) void delay(unsigned long ms) {
342-
k_sleep(K_MSEC(ms));
343-
}
344-
345-
__attribute__((always_inline)) void delayMicroseconds(unsigned int us) {
346-
if (us == 0) {
347-
return;
348-
}
349-
k_busy_wait(us - 1);
350-
}
351-
352341
unsigned long micros(void) {
353342
return k_cyc_to_us_floor32(k_cycle_get_32());
354343
}

0 commit comments

Comments
 (0)