Clock Measuring using the Signal Frequency Analyzer (SFA) module for KW45/KW47/MCXW71/MCXW72

キャンセル
次の結果を表示 
表示  限定  | 次の代わりに検索 
もしかして: 

Clock Measuring using the Signal Frequency Analyzer (SFA) module for KW45/KW47/MCXW71/MCXW72

Clock Measuring using the Signal Frequency Analyzer (SFA) module for KW45/KW47/MCXW71/MCXW72

Using the Signal Frequency Analyzer (SFA) to Measure the FRO 6M Frequency

Overview

The Signal Frequency Analyzer (SFA) is a specialized hardware peripheral available in NXP’s KW45, MCXW71, KW47, and MCXW72 microcontrollers. It is designed to provide precise, real-time measurement and analysis of digital signal characteristics, including frequency, period, and timing intervals. This makes it a valuable tool for applications requiring accurate timing diagnostics, signal validation, and system debugging.

By utilizing internal 32-bit counters and configurable trigger mechanisms, the SFA enables high-resolution capture of signal transitions, supporting robust system monitoring and fault detection.

Functional Capabilities of the SFA

The SFA module supports the following measurements:

  • Clock signal frequency of a Clock Under Test (CUT)
  • Clock signal period

It operates using two 32-bit counters:

  • One for the Reference Clock (REF)
  • One for the Clock Under Test (CUT)

Measurement is performed by comparing the counts of both clocks until predefined target values are reached.

FRO 6M Frequency Failure Scenarios

The 6 MHz Free Running Oscillator (FRO6M) may occasionally output an incorrect frequency under certain conditions:

  • When the device exits reset
  • When the device wakes from low-power modes

To mitigate potential issues caused by incorrect FRO6M output, it is the application developer’s responsibility to verify the oscillator’s frequency and apply corrective measures as needed.

Monitoring the FRO 6M Using the SFA

To monitor the FRO6M signal, the following configuration is recommended:

SFA Configuration Parameters

  • Reference Clock (REF): CPU Clock (e.g., 96 MHz)
  • Clock Under Test (CUT): FRO6M routed via CLKOUT
  • Interrupt Mode: Enabled for asynchronous measurement completion

Code Implementation

The presented functions are meant to be implemented in users application, the inner functions are part of the implementations of the SFA driver from the NXP’s SDK.

It can be used on MCXW71, MCXW72, KW45, kKW47, just make sure

  1. SFA Peripheral Initialization 
void init_SFA_peripheral(void)
{

	/* Enable SFA interrupt. */
    EnableIRQ(SFA_IRQn);

    /* Set SFA interrupt priority. */
    NVIC_SetPriority(SFA_IRQn, 1);

    SFA_Init(DEMO_SFA_BASEADDR);
    SFA_InstallCallback(DEMO_SFA_BASEADDR, EXAMPLE_SFA_CALLBACK);
}
  1. SFA Callback Function
void EXAMPLE_SFA_CALLBACK(status_t status)
{
    if (status == kStatus_SFA_MeasurementCompleted)
    {
        SfaMeasureFinished = true;
    }

    sfa_callback_status = status;
}
  1. Frequency Measurement Function

This function sets up the measurement of the FRO6M signal using the CPU clock as the reference.

uint8_t SFA_freq_measurement_6M_FRO(void)
{
	uint8_t ratio = 0;
    uint32_t freq = 0UL;
    sfa_config_t config;
    CLOCK_SetClkOutSel(kClockClkoutSelSirc);    //set clokout to SIRC
    SFA_GetDefaultConfig(&config);              //Get SFA default config
    config.mode         = kSFA_FrequencyMeasurement0;
    config.refSelect =  kSFA_REFSelect1;        //Set CPU clk as ref clk
    config.cutSelect = kSFA_CUTSelect1;         //Set clkout as CUT
	config.refTarget    = 0xFFFFFFUL;
	config.cutTarget    = 0xFFFFUL;
	config.enableCUTPin = true;
	freq = get_ref_freq_value(CPU_CLK);
    SFA_SetMeasureConfig(DEMO_SFA_BASEADDR, &config);
    SFA_MeasureNonBlocking(DEMO_SFA_BASEADDR);
    while (1)
    {
        if (SfaMeasureFinished)
        {
            SfaMeasureFinished = false;
        	if(kStatus_SFA_MeasurementCompleted == sfa_callback_status)
        	{
				freq               = SFA_CalculateFrequencyOrPeriod(DEMO_SFA_BASEADDR, freq);//Calculate the FRO freq
				if(FREQ_6MHZ + TOLERANCE <= freq )
				{
					ratio = 1;
				}
				else
				{
					if(FREQ_3MHZ + TOLERANCE <= freq)
					{
						ratio = 2;
					}
					else
					{
						if(FREQ_2MHZ + TOLERANCE <= freq)
						{
							ratio = 3;
						}
						else
						{
							ratio = 4;
						}
					}
				}
				break;
        	}

        }
        else
        {
            __WFI();
        }
    }
    return ratio;
}

Result Interpretation and Usage

To test the FRO 6M after adding the above functions the FRO can be tested after executing:

  • init_SFA_peripheral();
  • SFA_freq_measurement_6M_FRO();

The measured FRO6M frequency is printed to the serial terminal for human-readable diagnostics. Developers can use this result to:

  • Adapt peripheral clocking if the FRO6M frequency is incorrect
  • Trigger corrective actions such as  switching to an alternate clock source

Steps to Reconfigure Peripheral Clocking When FRO6M output frequency is lower

  1. Detect the Faulty FRO6M Output

Use the SFA measurement as described earlier to determine if the FRO6M is operating below its expected frequency (6 MHz). If the result is significantly lower, proceed to reconfigure.

  1. Choose an Alternative Clock Source

Most NXP MCUs offer multiple internal and external clock sources. Common alternatives include:

  • FRO 192M
  • OSC RF 32M
  • Sys OSC
  • RTC OSC

Choose one that is:

  • Stable
  • Available in your current power mode
  • Compatible with the peripheral’s timing requirements
    • You can add more clock divers if needed to make a higher frequency clock reach a certain lower frequency.
  1. Reconfigure the Peripheral Clock Source

Use the SDK’s CLOCK_Set... APIs to change the clock source.

You may also need to:

  • Adjust dividers to match the required baud rate or timing
  • Reinitialize the peripheral with the new clock settings

Example Scenario: Measuring the FRO and Adjusting UART Based on Frequency Ratio

Imagine your application relies on the 6 MHz Free Running Oscillator (FRO), and its accuracy directly affects UART communication. To ensure reliable operation, you can use the System Frequency Adjustment (SFA) feature to monitor the FRO output and dynamically adjust the UART configuration.

After measuring the 6 MHz FRO using the recommended method, the system returns a frequency ratio value. This value ranges from 1 to 4, where:

  • 1 indicates the frequency is within expected limits (no issues),
  • 2 to 4 represent varying degrees of deviation from the expected frequency.

Using this ratio, you can initialize and configure the UART peripheral and its driver to compensate for any frequency variation, ensuring stable and accurate communication.

 */
int main(void)
{
    BOARD_InitHardware();
    uint8_t ch = 0;
    uint8_t FRO_ratio = 0;
    init_SFA_peripheral();
    /*Measure FRO6M output frequency*/
    FRO_ratio = SFA_freq_measurment_6M_FRO();
    /*Init debug console and compensate in case a different frequency is output */
    if(0 == FRO_ratio)
    {
    	assert(0);//this user defined return value means something went wrong while measuring 6Mz FRO
    }
    uint32_t uartClkSrcFreq = BOARD_DEBUG_UART_CLK_FREQ/FRO_ratio;//Compensate the src frequency set for uart module
    CLOCK_EnableClock(kCLOCK_Lpuart1);	
    CLOCK_SetIpSrc(kCLOCK_Lpuart1, kCLOCK_IpSrcFro6M);
    DbgConsole_Init(BOARD_DEBUG_UART_INSTANCE, BOARD_DEBUG_UART_BAUDRATE, BOARD_DEBUG_UART_TYPE, uartClkSrcFreq);

......

}

SDK 25.0.00 Enhancements for FRO6M Calibration

To address known reliability issues with the 6 MHz Free Running Oscillator (FRO6M), particularly during transitions from low-power modes, SDK version 25.06.00 introduces a set of software enhancements aimed at improving oscillator validation and calibration.

Key Features Introduced

  1. FRO6M Calibration API

Two new functions have been added to facilitate runtime verification of the FRO6M frequency:

  • PLATFORM_StartFro6MCalibration()
    Initializes the calibration process by enabling the cycle counter, capturing a timestamp, and preparing the system to measure elapsed time using both the CPU and the FRO6M-based timestamp counter.
  • PLATFORM_EndFro6MCalibration()
    Completes the calibration by comparing the time measured via CPU cycles and the FRO6M timestamp counter. This comparison determines whether the oscillator is operating at the expected 6 MHz or has erroneously locked to a lower frequency (e.g., 2 MHz). The result is stored in a global ratio variable (fwk_platform_FRO6MHz_ratio) for use by the system.

These functions provide a lightweight and efficient mechanism to detect and respond to oscillator misbehavior, ensuring system stability and timing accuracy.

  1. Configuration Macro
  • gPlatformEnableFro6MCalLowpower_d
    This macro enables automatic FRO6M frequency verification upon exiting low-power modes. When defined, the system will invoke the calibration functions to validate the oscillator before resuming normal operation.
  1. Default Integration
  • The calibration mechanism is enabled by default in the SDK configuration file fwk_config.h, ensuring that all applications benefit from this safeguard without requiring manual setup.

Use Case and Benefits

These enhancements are particularly valuable in applications where:

  • Precise timing is critical (e.g., wireless communication, sensor sampling).
  • The system frequently enters and exits low-power states.
  • Clock source integrity must be guaranteed to avoid peripheral misbehavior or timing faults.

By integrating these calibration routines, developers can proactively detect and correct FRO6M frequency anomalies, improving overall system robustness and reducing the risk of runtime errors due to clock instability.

 

評価なし
バージョン履歴
最終更新日:
3 週間前
更新者: