1919#include "platform/mbed_toolchain.h"
2020#include "platform/mbed_wait_api.h"
2121
22+ #include "hal/lp_ticker_api.h"
23+ #include "hal/us_ticker_api.h"
24+ #include "hal/ticker_api.h"
25+
2226// This implementation of the wait functions will be compiled only
2327// if the RTOS is not present.
2428#ifndef MBED_CONF_RTOS_PRESENT
2529
26- #include "hal/lp_ticker_api.h"
27- #include "hal/us_ticker_api.h"
28-
2930void wait (float s )
3031{
3132 wait_ms (s * 1000.0f );
@@ -42,24 +43,55 @@ void wait_ms(int ms)
4243#endif
4344}
4445
46+ #endif // #ifndef MBED_CONF_RTOS_PRESENT
47+
48+ // This wait_us is used by both RTOS and non-RTOS builds
49+ /* The actual time delay may be 1 less usec */
50+
51+ #if DEVICE_USTICKER
52+
53+ #if defined US_TICKER_PERIOD_NUM
54+ /* Real definition for binary compatibility with binaries not using the new macro */
55+ void (wait_us )(int us )
56+ {
57+ wait_us (us );
58+ }
59+
60+ /* External definition for the inline function */
61+ extern void _wait_us_inline (unsigned int us );
62+
63+ void _wait_us_ticks (uint32_t ticks )
64+ {
65+ const uint32_t start = us_ticker_read ();
66+ while (((us_ticker_read () - start ) & US_TICKER_MASK ) < ticks );
67+ }
68+
69+ void _wait_us_generic (unsigned int us )
70+ #else
4571void wait_us (int us )
72+ #endif
4673{
47- #if DEVICE_USTICKER
74+ // Generic version using full ticker, allowing for initialization, scaling and widening of timer
4875 const ticker_data_t * const ticker = get_us_ticker_data ();
49- uint32_t start = ticker_read (ticker );
76+ const uint32_t start = ticker_read (ticker );
5077 while ((ticker_read (ticker ) - start ) < (uint32_t )us );
51- #else // fallback to wait_ns for targets without usticker
52- while (us > 1000 ) {
53- us -= 1000 ;
54- wait_ns (1000000 );
78+ }
79+
80+ #else // DEVICE_USTICKER
81+
82+ // fallback to wait_ns for targets without usticker
83+ void wait_us (int us )
84+ {
85+ while (us > 1024 ) {
86+ us -= 1024 ;
87+ wait_ns (1024000 );
5588 }
5689 if (us > 0 ) {
5790 wait_ns (us * 1000 );
5891 }
59- #endif // DEVICE_USTICKER
6092}
6193
62- #endif // #ifndef MBED_CONF_RTOS_PRESENT
94+ #endif // DEVICE_USTICKER
6395
6496// This wait_ns is used by both RTOS and non-RTOS builds
6597
@@ -110,7 +142,7 @@ static const uint16_t delay_loop_code[] = {
110142};
111143
112144/* Take the address of the code, set LSB to indicate Thumb, and cast to void() function pointer */
113- #define delay_loop ((void(*)()) ((uintptr_t) delay_loop_code | 1))
145+ #define delay_loop ((void(*)()) ((uintptr_t) delay_loop_code + 1))
114146
115147/* Some targets may not provide zero-wait-state flash performance. Export this function
116148 * to be overridable for targets to provide more accurate implementation like locating
0 commit comments