Step 4: Add Application Code to the Project
This example application on the PIC32CM LS00 Curiosity Nano+ Touch Evaluation Kit demonstrates how to measure execution cycles using the SysTick system timer. In this demonstration, application code is added to the project's main.c source file to implement the SysTick counter for measuring the execution cycles of various arithmetic operations. The following code can be used to count CPU execution cycles with the SysTick system timer and compare the performance of different operations.
Application Code for the Project
Open the main.c source file in the non-secure project. Include the <stdio.h> header file, and then declare the global variable Counter_Value and the Buffer array immediately after the header file inclusions.
uint32_t Counter_Value;
volatile float Buffer[250] = {};

Insert the SYSTICK_TimerStart() function immediately after SYS_Initialize(NULL); within the int main(void) function. The SYSTICK_TimerStart() function initializes and starts the SysTick system timer in the microcontroller. Use the printf function to display a start message on the terminal.
printf("SysTick Periodic check\n\r");
Add the Function_1 function above the int main(void) function to assign values to the Buffer array. Within Function_1, call SYSTICK_TimerRestart() to restart the SysTick counter, then use a for loop to assign values to the Buffer array. After the loop, read the execution cycle count using SYSTICK_TimerCounterGet() and store the result in the Counter_Value variable. Finally, use printf to display the measured execution cycle count.
{
SYSTICK_TimerRestart();
for(int i = 0; i < 250; i++)
{
Buffer[i] = i;
}
Counter_Value = SYSTICK_TimerCounterGet();
printf("\n\rExecution time for assign 250 values = %d \n\r", Counter_Value);
}

Add Function_2 in a manner similar to Function_1, with the only difference being the operation performed inside the for loop. In Function_2, multiply each element of the Buffer array by a generated value within the loop to measure the CPU execution time required for the multiplication operation.
{
SYSTICK_TimerRestart();
for(int i = 0; i < 250; i++)
{
Buffer[i]*= 5;
}
Counter_Value = SYSTICK_TimerCounterGet();
printf("\n\rExecution time for multiply 250 values = %d \n\r", Counter_Value);
}

Add Function_3 in a manner similar to Function_1, with the only modification being the use of the division operation inside the for loop. In Function_3, perform a division operation on each element of the Buffer array and use the SYSTICK_TimerCounterGet() function to measure the execution time required for the division operation.
{
SYSTICK_TimerRestart();
for(int i = 0; i < 250; i++)
{
Buffer[i]/= 5;
}
Counter_Value = SYSTICK_TimerCounterGet();
printf("\n\rExecution time for divide 250 values = %d \n\r", Counter_Value);
}

Within the int main(void) function, call the three functions that have been defined.
Function_2();
Function_3();
