Hello Everyone,
I've been experiencing some strangeness w.r.t. using clock() in Kinetis projects. I'm using MCUXpresso IDE 11.3.0 with a FRDM-KE15Z & FRDM-KL26Z in C++. I'm using clock() as a means of measuring processing work. When I call it while running the program on the board using the IDE and onboard debug probe through GDB, everything's just fine—I get meaningful results from clock(). When I disconnect the probe and run the program on its own, but view output using the IDE's Terminal tab, clock() ALWAYS returns 32.
This is odd behaviour! But it kind-of looks like a feature: you can tell if you've got an active probe into the program this way.
Do you know what's going on?
To reproduce this you'll need a Kinetis board like I'm using. Create a New C++ project in MCUXpresso using the wizard. Use the defaults for everything, except choose "C++ Project" and then: "NewlibNano (semihost)" in the Advanced project settings.
Replace the PRINTF… line in main() to:
printf("Hello World, let's check clock()\n");
DbgConsole_Printf("Dbg:Hello World, let's check clock()\r\n");
while(1) {
clock_t cpuTime=clock();
if (cpuTime==32) {
printf("clock is thirty-two!\n");
DbgConsole_Printf("Dbg:clock is thirty-two!\r\n");
} else {
printf("clock is:%ld\n",cpuTime);
DbgConsole_Printf("Dbg:clock is:%ld\r\n",cpuTime);
}
}
Make sure SDK_DEBUGCONSOLE is defined as 1 somewhere so DbgConsole_Printf will work. (Near the very top of fsl_debug_console.h is a good place.) Finally, add: #include <time.h> at the top of the file with main().
Before running it, hook up the board to your computer and run Terminal with the IDE. Connect it to the board and keep the window handy. Now run the app using the IDE and show the console. You'll see something that looks a whole lot like the picture below—both Console & Terminal are in perfect sync. Once you have that, stop the debugger in the IDE by hitting the red square "stop" button. Don't reset the board or anything like that—the program is still running. The console stops printing, but the Terminal continues. Except now the terminal prints only "Dbg:clock is thirty two!" If you disconnect the board and shut down the IDE and use some other terminal program attached to the now fully power-cycled board, you'll also ONLY see "Dbg:clock is thirty two!" as shown in the second picture below.
I'd like to get clock() working as it should. But I can't figure this one out.
Your help is REALLY appreciated.
Thank you very much,
Andre.