Using external oscillator for PWM KL17/27

cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

Using external oscillator for PWM KL17/27

1,376 Views
mitch1
Contributor II

I need to generate slow PWM across the range of 20-150 Hz. I've found that the IRC of 2Mhz (kMCGLITE_Lirc2M) is still too fast to get that slow. So I'm attempting to use an external oscillator. I've used the NXP Clocks tool to generate my clock init. However I do not see any ouput when I switch the clock source. Attached are oscope snaps showing my 32khz external clock is working, and that pwm works when using internal clocks. I've attempted this on my own board as well as the KL27 Freedom Board.

/*******************************************************************************
* Code for BOARD_BootClockRUN configuration
******************************************************************************/
void BOARD_BootClockRUN(void)
{
/* Set the system clock dividers in SIM to safe value. */
CLOCK_SetSimSafeDivs();
/* Initializes OSC0 according to board configuration. */
CLOCK_InitOsc0(&oscConfig_BOARD_BootClockRUN);
CLOCK_SetXtal0Freq(oscConfig_BOARD_BootClockRUN.freq);
/* Set MCG to HIRC mode. */
CLOCK_SetMcgliteConfig(&mcgliteConfig_BOARD_BootClockRUN);
/* Set the clock configuration in SIM module. */
CLOCK_SetSimConfig(&simConfig_BOARD_BootClockRUN);
/* Set SystemCoreClock variable. */
SystemCoreClock = BOARD_BOOTCLOCKRUN_CORE_CLOCK;
}

PWM Code:

#define TPM_SOURCE_CLOCK CLOCK_GetFreq(kCLOCK_Osc0ErClk) // HERE
#define BOARD_TPM_BASEADDR TPM0
#define BOARD_FIRST_TPM_CHANNEL 3U

#define IRC_CLOCK_MUX 0x3
#define OSCERCLOCK_MUX 0x2
#define MCGPCLK 0x1

tpmParam.chnlNumber = (tpm_chnl_t)BOARD_FIRST_TPM_CHANNEL;

tpmParam.level = kTPM_HighTrue;

tpmParam.dutyCyclePercent = 40;
CLOCK_SetTpmClock(OSCERCLOCK_MUX); // HERE MCGPCLK
TPM_GetDefaultConfig(&tpmInfo);
TPM_Init(BOARD_TPM_BASEADDR, &tpmInfo);

TPM_SetupPwm(BOARD_TPM_BASEADDR, &tpmParam, 1U, kTPM_EdgeAlignedPwm, frequency_hz, TPM_SOURCE_CLOCK);
TPM_StartTimer(BOARD_TPM_BASEADDR, kTPM_ExternalClock); // kTPM_SystemClock // HERE

TPM_UpdatePwmDutycycle(BOARD_TPM_BASEADDR, (tpm_chnl_t)BOARD_FIRST_TPM_CHANNEL, kTPM_EdgeAlignedPwm,
40);

while(1)

{

}
Labels (1)
0 Kudos
3 Replies

1,369 Views
mjbcswitzerland
Specialist V

Hi

Are you sure that you have a problem with MCGIRCK?
If using the 2MHz IRC as source directly you should be able to generate frequencies of down to 0.24Hz if you set the TPM's pre-scaler to 128.

In addition, you can divide IRC by up to 128 by using FCRDIV and by a further 128 by using LIRC_DIV2. That means that 0.00188Hz frequencies are possible.

Your range 20Hz..150Hz is this easily achievable without needing to add additional oscillators.

 

If you prefer to use an external oscillator are you preferring to using a 32kHz oscillator on the OSC input or as an input to the TPM via a TPM_EXTCK pin?

Regards

Mark
uTasker project developer for Kinetis and i.MX RT]
Contact me by personal message or on the uTasker web site to discuss professional training or product development requirements

KL17/27 for professionals:
https://www.utasker.com/kinetis/FRDM-KL27Z.html
https://www.utasker.com/kinetis/Capuccino-KL27.html

 

1,364 Views
mitch1
Contributor II

As usual, you save the day. Facepalm. That pre-scalar is all I need, and indeed works well. I'm still curious why the external clock didn't/doesn't work. However, it appears not to be necessary. Thank you. The intent was to use the external clock as OSCERCLK .

0 Kudos

1,343 Views
mjbcswitzerland
Specialist V

Hi

The OSCERCLK is not the same as the ERCLK32K which I think you were supplying:

mjbcswitzerland_0-1601332708130.png

which may explain the difficulties you had.

Regards

Mark

0 Kudos