3 stopping the stopwatch timer, 4 converting cycles to actual time, Stopping the stopwatch timer – Motorola ONCE SC140 User Manual

Page 10: Converting cycles to actual time

Advertising
background image

6

Using the SC140 Enhanced OnCE Stopwatch Timer

3.2.2

Counter Registers

ECNT_VAL is a countdown counter, and ECNT_EXT is a countup counter. ECNT_VAL is decremented
on each occurrence of an event, as specified in the control register. ECNT_EXT is incremented each time
there is an underflow in ECNT_VAL.

For maximum cycle counting capacity, the stopwatch timer implementation initializes ECNT_VAL to the
largest possible value which is 4294967295, or 0xffffffff.

ECNT_EXT is initialized to zero.

3.3

Stopping the Stopwatch Timer

The C code to stop the stopwatch timer is shown in Code 3.

Code 3. C Code to Stop the Stopwatch Timer

#include “EOnCE_registers.h”

void EOnCE_stopwatch_timer_stop(unsigned long *clock_ext, unsigned long *clock_val)
{
WRITE_IOREG(ECNT_CTRL,0);

/* Disable event counter */

READ_IOREG(ECNT_VAL,*clock_val); /* Save ECNT_VAL in program variable */
READ_IOREG(ECNT_EXT,*clock_ext); /* Save ECNT_EXT in program variable */
*clock_val = (MAX_32_BIT-*clock_val); /* Adjust for countdown */
}

To stop the stopwatch timer, the ECNT_CTRL register is set to zero. After stopping the stopwatch timer,
the routine copies the values of ECNT_VAL and ECNT_EXT registers into program variables. This
allows putting the stopwatch timer into use again without losing the result of the previous measurement.

Because ECNT_VAL contains the result of a countdown process, the routine converts that result into the
actual number of cycles elapsed by subtracting the ECNT_VAL from the value to which it was initialized,
specifically, 4294967295.

3.4

Converting Cycles to Actual Time

The stopwatch timer measures durations in units of core clock cycles. Most often the units of interest are
units of absolute time, such as milliseconds or microseconds.

Conversion from core clock cycles, as measured by the event counter registers, to milliseconds is
computed in Equation 1.

Eqn. 1

In this equation, EXT is the value in ECNT_EXT, VAL is the value in ECNT_VAL, and Clock Speed is
measured in hertz.

Code 4 on page -7 shows the C code for clock-cycle-to-time conversion.

The C code depends on setting the value of the constant CLOCK_SPEED in EOnCE_stopwatch.c to match
the clock speed as set in the PLL.

The conversion routine distinguishes between three different output units: seconds, milliseconds, and
microseconds. Handling each unit separately allows the computations to be performed using integer
arithmetic without loss of accuracy.

Time ms

(

)

EXT

0xffffffff

×

VAL

+

(

)

1000

ClockSpeed

--------------------------------

Ч

=

Advertising