The FRDM-KW40Z includes an RTC module with a 32 kHz crystal oscillator. This module generates a 32 kHz clock source for the MCU whilst running on very low power mode. This oscillator includes a set of programmable capacitors used as the CLOAD. Changing the value of these capacitors can modify the frequency the oscillator provides.
This configurable capacitance ranges from 1 pF (which is effectively two 2 pF capacitors in series) all the way to 15 pF (30 pF capacitors in series). These values are obtained by combining the enabled capacitors. The values available are 2 pF, 4 pF, 8 pF and 16 pF. Any combination between these four can be done. It is recommended that these internal capacitors are disabled if the external capacitors are available.
Figure 1. External capacitors for the 32 kHz crystal
To adjust the frequency provided by the oscillator, you must first be able to measure the frequency. Using a frequency counter would be ideal, as it provides a more precise measurement than an oscilloscope. You will also need to output the oscillator frequency. To output the oscillator frequency, using any of the Bluetooth demo applications as an example, you should do the following:
1. Since the RTC module is going to be used to output the oscillator frequency, the RTC_CLKOUT will be the output signal. The output pin for RTC_CLKOUT is PTB3. To configure PTB3 as the oscillator output use the function configure_rtc_pins(RTC instance). Port B clock must be enabled first.
Figure 2. PTB3 pin mux
/* hardware_init.c */
/* enable clock for PORTs */
/* RTC CLKOUT */
configure_rtc_pins(0); //This function changes the pin mux to select RTC_CLKOUT. It is included in the pin_mux.c file.
2. Modify the System Options Register 1 (SIM_SOPT1) to select the clock source and to allow the selected clock source to be output in PTB3. The field you should change is OSC32KOUT. To select the clock source to be output on PTB3, use the function CLOCK_HAL_SetExternalRefClock32kSrc(). Remember to include the fsl_sim_hal.h file.
Figure 3. SIM_SOPT1 register fields
/* hardware_init.c */
/* RTC CLKOUT */
SIM_Type *simBase = g_simBase;
SIM_SOPT1 = SIM_SOPT1_OSC32KOUT(kClockRtcoutSrc32kHz); // This field in register SIM_SOPT1 allows a clock source to be output to PTB3
CLOCK_HAL_SetExternalRefClock32kSrc(simBase, kClockEr32kSrcOsc0); // This function chooses the clock source for the RTC
3. Make sure the oscillator is enabled. The RTC external clock configurations can be found in the board.h file. This is also where the internal capacitors are enabled.
/* board.h */
/* RTC external clock configuration. */
#define RTC_XTAL_FREQ 32768U
#define RTC_SC2P_ENABLE_CONFIG false // 2 pF capacitors enable
#define RTC_SC4P_ENABLE_CONFIG false // 4 pF capacitors enable
#define RTC_SC8P_ENABLE_CONFIG false // 8 pF capacitors enable
#define RTC_SC16P_ENABLE_CONFIG false // 16 pF capacitors enable
#define RTC_OSC_ENABLE_CONFIG true // Oscillator enable
4. You should now be able to measure the oscillator frequency through the PTB3 pin. Measurements should be done with a frequency counter, as change in the output can be very subtle, and an oscilloscope might not be able to pick it up.
Remember that these capacitors give you the option to use the 32 kHz oscillator when you do not have the external capacitors. They are not supposed to be used when the external capacitors are being used.
Now, to change the internal capacitance, you can simply change the macros contained in the board.h file (step 4). It is important that the oscillator is disabled before making any changes to the enabled capacitors. The fsl_clock_manager.c file already contains the function CLOCK_SYS_RtcOscInit() that configures the oscillator with the values established in the previously mentioned macros, however, the oscillator is not disabled before attempting to change the capacitors (and therefore, no changes are made). To fix this, you can use the RTC_HAL_SetOscillatorCmd() function to disable the oscillator before making changes with the capacitors.
/* fsl_clock_manager.c */
// Disable the oscillator in case any changes are made to the capacitors
// If the oscillator is not enabled and should be enabled.
if ((!RTC_HAL_IsOscillatorEnabled(RTC)) && (config->enableOsc))
/* Enable the desired capacitors */
/* Re-enable the oscillator */
The fsl_clock_manager.c file can be found in:
Some reference measurements with different values for the internal capacitance:
With external capacitors (internal capacitors disabled)
KW40Z – rev. C
C46 & C47
KW40Z – rev. B
C46 & C47
With internal capacitors (external capacitors removed)
SC2P, SC4P, SC8P
SC2P, SC4P, SC16P
SC4P, SC8P, SC16P
SC2P, SC4P, SC8P, SC16P
Please note that the capacitance is not only composed of the enabled internal capacitance, but also the parasitic capacitances found in the package, bond wires, bond pad and the PCB traces. So, while the reference measurements given before should be close to the actual value, you should also make measurements with your board, to ensure that the frequency is trimmed specifically to your board and layout.