Skip to content

Commit 5a5d159

Browse files
authored
Merge pull request #4666 from chrissnow/tests-mbed_hal-flash-Timing_fix
Fix timing issues found in "Flash - clock and cache test"
2 parents 43a3612 + 8365d9d commit 5a5d159

File tree

2 files changed

+64
-3
lines changed

2 files changed

+64
-3
lines changed

TESTS/mbed_hal/flash/functional_tests/main.cpp

Lines changed: 43 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -52,17 +52,57 @@ static void erase_range(flash_t *flash, uint32_t addr, uint32_t size)
5252
size = size > sector_size ? size - sector_size : 0;
5353
}
5454
}
55+
#ifdef __CC_ARM
56+
MBED_NOINLINE
57+
__asm static void delay_loop(uint32_t count)
58+
{
59+
1
60+
SUBS a1, a1, #1
61+
BCS %BT1
62+
BX lr
63+
}
64+
#elif defined (__ICCARM__)
65+
MBED_NOINLINE
66+
static void delay_loop(uint32_t count)
67+
{
68+
__asm volatile(
69+
"loop: \n"
70+
" SUBS %0, %0, #1 \n"
71+
" BCS.n loop\n"
72+
: "+r" (count)
73+
:
74+
: "cc"
75+
);
76+
}
77+
#else // GCC
78+
MBED_NOINLINE
79+
static void delay_loop(uint32_t count)
80+
{
81+
__asm__ volatile (
82+
"%=:\n\t"
83+
#if defined(__thumb__) && !defined(__thumb2__)
84+
"SUB %0, #1\n\t"
85+
#else
86+
"SUBS %0, %0, #1\n\t"
87+
#endif
88+
"BCS %=b\n\t"
89+
: "+l" (count)
90+
:
91+
: "cc"
92+
);
93+
}
94+
#endif
5595

96+
MBED_NOINLINE
5697
static int time_cpu_cycles(uint32_t cycles)
5798
{
5899
Timer timer;
59100
timer.start();
60101

61102
int timer_start = timer.read_us();
62103

63-
volatile uint32_t delay = (volatile uint32_t)cycles;
64-
while (delay--);
65-
104+
uint32_t delay = cycles;
105+
delay_loop(delay);
66106
int timer_end = timer.read_us();
67107

68108
timer.stop();

platform/mbed_toolchain.h

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -137,6 +137,27 @@
137137
#endif
138138
#endif
139139

140+
/** MBED_NOINLINE
141+
* Declare a function that must not be inlined.
142+
*
143+
* @code
144+
* #include "mbed_toolchain.h"
145+
*
146+
* MBED_NOINLINE void foo() {
147+
*
148+
* }
149+
* @endcode
150+
*/
151+
#ifndef MBED_NOINLINE
152+
#if defined(__GNUC__) || defined(__clang__) || defined(__CC_ARM)
153+
#define MBED_NOINLINE __attribute__((noinline))
154+
#elif defined(__ICCARM__)
155+
#define MBED_NOINLINE _Pragma("inline=never")
156+
#else
157+
#define MBED_NOINLINE
158+
#endif
159+
#endif
160+
140161
/** MBED_FORCEINLINE
141162
* Declare a function that must always be inlined. Failure to inline
142163
* such a function will result in an error.

0 commit comments

Comments
 (0)