CODE
#include <stdio.h>
#include "co.h"
#ifdef IMPULSE_C_TARGET // If we're building on the target platform,
#include "system.h" // include Nios II-specific header files in order
#include "sys/alt_timestamp.h" // to use the timestamp facility.
#endif
int main(int argc, char ** argv)
{
// ...
#ifdef IMPULSE_C_TARGET
alt_u32 elapsed_time;
if ( -1 == alt_timestamp_start() ) {
printf("Error initializing timestamp facility\n");
}
elapsed_time = alt_timestamp();
#endif
my_arch = co_initialize(NULL);
co_execute(my_arch);
#ifdef IMPULSE_C_TARGET
elapsed_time = alt_timestamp() - elapsed_time;
printf("Elapsed time: %lu ms\n", elapsed_time/(alt_timestamp_freq()/1000));
#endif
// ...
}
#include "co.h"
#ifdef IMPULSE_C_TARGET // If we're building on the target platform,
#include "system.h" // include Nios II-specific header files in order
#include "sys/alt_timestamp.h" // to use the timestamp facility.
#endif
int main(int argc, char ** argv)
{
// ...
#ifdef IMPULSE_C_TARGET
alt_u32 elapsed_time;
if ( -1 == alt_timestamp_start() ) {
printf("Error initializing timestamp facility\n");
}
elapsed_time = alt_timestamp();
#endif
my_arch = co_initialize(NULL);
co_execute(my_arch);
#ifdef IMPULSE_C_TARGET
elapsed_time = alt_timestamp() - elapsed_time;
printf("Elapsed time: %lu ms\n", elapsed_time/(alt_timestamp_freq()/1000));
#endif
// ...
}
alt_timestamp_start() resets the timestamp counter and should be called before using alt_timestamp().
alt_timestamp() returns the current timestamp count (in clock ticks).
alt_timestamp_freq() returns the number of ticks per second.
To use the timestamp functions, your hardware system will need to include a timer to generate timestamps. Normally, an SOPC Builder system will include a periodic system timer (named "system_timer"), but you will need to a second "Interval timer" peripheral for use as a timestamp timer (let's call it "timestamp_timer").
Once you've generated the SOPC Builder system with both timers, you must specify which timer to use for timestamping when building the software system library. Create a Nios II IDE software project as usual and make sure you're in the C/C++ Projects perspective. Right-click your project's system library and select "Properties" from the context menu. Select "System Library" from the tree on the left side of dialog box that appears. In the section of the dialog marked "System Library Contents", open the drop-down box next to "Timestamp timer" and choose the name of your timestamp peripheral ("TIMESTAMP_TIMER").
Now you can build and run the software project as usual.
The simple timer described here is a good way to easily gather performance information as you iteratively refine your Impulse C design.












