# Functions¶

group group_sysclk_calclk_funcs

Functions

cy_en_sysclk_status_t Cy_SysClk_StartClkMeasurementCounters(cy_en_meas_clks_t clock1, uint32_t count1, cy_en_meas_clks_t clock2)

Assigns clocks to the clock measurement counters, and starts counting.

The counters let you measure a clock frequency using another clock as a reference. There are two counters.

• One counter (counter1), which is clocked by clock1, is loaded with an initial value and counts down to zero.

• The second counter (counter2), which is clocked by clock2, counts up until the first counter reaches zero.

Either clock1 or clock2 can be a reference clock; the other clock becomes the measured clock. The reference clock frequency is always known.

After calling this function, call

Cy_SysClk_ClkMeasurementCountersDone() to determine when counting is done; that is, counter1 has counted down to zero. Then call Cy_SysClk_ClkMeasurementCountersGetFreq() to calculate the frequency of the measured clock.

note

The counters are both 24-bit, so the maximum value of count1 is 0xFFFFFF. If clock2 frequency is greater than clock1, make sure that count1 is low enough that counter2 does not overflow before counter1 reaches zero.

note

The time to complete a measurement is count1 / clock1 frequency.

note

The clocks for both counters must have a nonzero frequency, or Cy_SysClk_ClkMeasurementCountersGetFreq() incorrectly reports the result of the previous measurement.

note

Do not enter a device low power mode (Sleep, Deep Sleep) while doing a measurement; the measured clock frequency may not be accurate.

Function Usage

    /* Scenario: ILO frequency may be drifting. Measure the ILO frequency using
the clock measurement counters, with the IMO as the reference. */

#define  IMO_FREQ       8000000UL   /* 8 MHz IMO */
#define  ILO_NOMINAL    32768UL     /* 32.768 kHz ILO */

/* Start the ILO clock measurement using the IMO */
(void)Cy_SysClk_StartClkMeasurementCounters(CY_SYSCLK_MEAS_CLK_ILO,     /* Counter 1 clock  = ILO */
0x7FUL,                     /* Counter 1 period = 128 */
CY_SYSCLK_MEAS_CLK_IMO);    /* Counter 2 clock  = IMO */
/* Wait for counter 1 to reach 0 */
while(!Cy_SysClk_ClkMeasurementCountersDone()){}

/* Measure clock 1 with the IMO clock cycles (counter 2) */
uint32_t measuredFreq = Cy_SysClk_ClkMeasurementCountersGetFreq(false, IMO_FREQ);

if(measuredFreq != ILO_NOMINAL)
{
/* Take appropriate action such as trimming the ILO or changing the LFCLK source */
}


Parameters
• clock1 – The clock for counter1

• count1 – The initial value for counter1, from which counter1 counts down to zero.

• clock2 – The clock for counter2

Returns

Error / status code:

CY_SYSCLK_INVALID_STATE if already doing a measurement

CY_SYSCLK_BAD_PARAM if invalid clock input parameter

else CY_SYSCLK_SUCCESS CY_SYSCLK_INVALID_STATE - ECO already enabled For the PSoC 64 devices there are possible situations when function returns the PRA error status code. This is because for PSoC 64 devices the function uses the PRA driver to change the protected registers. Refer to

cy_en_pra_status_t for more details.

uint32_t Cy_SysClk_ClkMeasurementCountersGetFreq(bool measuredClock, uint32_t refClkFreq)

Calculates the frequency of the indicated measured clock (clock1 or clock2).

• If clock1 is the measured clock, its frequency is: clock1 frequency = (count1 / count2) * clock2 frequency

• If clock2 is the measured clock, its frequency is: clock2 frequency = (count2 / count1) * clock1 frequency

Call this function only after counting is done; see Cy_SysClk_ClkMeasurementCountersDone().

warning

The function returns zero, if during measurement device was in the Deep Sleep or partially blocking flash operation occurred. It means that current measurement is not valid and you should call the Cy_SysClk_StartClkMeasurementCounters() function once again.

Function Usage

Refer to the Cy_SysClk_StartClkMeasurementCounters() function usage.

Parameters
• measuredClock – False (0) if the measured clock is clock1; true (1) if the measured clock is clock2.

• refClkFreq – The reference clock frequency (clock1 or clock2).

Returns

The frequency of the measured clock, in Hz.

bool Cy_SysClk_ClkMeasurementCountersDone(void)

Checks if clock measurement counting is done, that is, counter1 has counted down to zero.

Call Cy_SysClk_StartClkMeasurementCounters() before calling this function.

Function Usage

Refer to the Cy_SysClk_StartClkMeasurementCounters() function usage.

Returns

Status of calibration counters:

true = done

false = not done