Hi
When I want to generate a 20KHz,50% duty cycle PWM by FTM Module(FTM0,CH0,PTB12),and in the main loop there is a 50ms slave watchdog-feeding SPI transfer, the PWM wave is abnormal as below:
(The wave is a MOSFET driver's output(12V) that follows the MCU FTM output)
(I guess once the SPI is transmitting, the PWM is stopped or is pulled high)
However, when I comment the SPI transfer code, as that there is no code in for(;;), the PWM becomes normal:
So could someone tell me the reason or how to send SPI frames without disturbing the PWM output?
I use functions from the S32 DS example projects, following is partial code of the project.
main loop:
for(;;)
{
LPSPI0_transmit_24bits( GENSTAT_read_command);
GENSTAT_read_result = LPSPI0_receive_24bits();
printf("GENSTAT :0x%06x \n\r",GENSTAT_read_result);
GENSTAT_read_result = 0;
}
SPI init:
void LPSPI0_init_master(void) {
PCC->PCCn[PCC_LPSPI0_INDEX] = 0; /* Disable clocks to modify PCS ( default) */
PCC->PCCn[PCC_LPSPI0_INDEX] = 0xC6000000; /* Enable PCS=SPLL_DIV2 (40 MHz func'l clock) */
LPSPI0->CR = 0x00000000;
LPSPI0->IER = 0x00000000;
// LPSPI0->IER = 0x00000003;
LPSPI0->DER = 0x00000000;
LPSPI0->CFGR0 = 0x00000000;
LPSPI0->CFGR1 = 0x00000001;
LPSPI0->TCR = 0x51000017; /* 16-bit data */
LPSPI0->CCR = 0x04090808;
LPSPI0->FCR = 0x00000003;
LPSPI0->CR = 0x00000009;
}
FTM init:
void FTM0_init(void)
{
PCC->PCCn[PCC_FTM0_INDEX] &= ~PCC_PCCn_CGC_MASK; /* Ensure clk disabled for config */
PCC->PCCn[PCC_FTM0_INDEX] |= PCC_PCCn_PCS(0b010) /* Clock src=1, 8 MHz SIRCDIV1_CLK */
| PCC_PCCn_CGC_MASK; /* Enable clock for FTM regs */
/*!
* FTM0 Initialization:
* ===================================================
*/
FTM0->MODE |= FTM_MODE_WPDIS_MASK; /* Write protect to registers disabled (default) */
FTM0->SC = FTM_SC_PWMEN0_MASK|FTM_SC_PS(2); /* TOIE (Timer Overflow Interrupt Ena) = 0 (default) */
/* CPWMS (Center aligned PWM Select) = 0 (default, up count) */
/* CLKS (Clock source) = 0 (default, no clock; FTM disabled) */
/* PS (Prescaler factor) = 7. Prescaler = 128 */
FTM0->COMBINE = 0x00000000; /* FTM mode settings used: DECAPENx, MCOMBINEx, COMBINEx=0 */
FTM0->POL = 0x00000000; /* Polarity for all channels is active high (default) */
FTM0->MOD = 100-1 ;
}
FTM configuration and start:
void FTM0_CH0_PWM_init(void)
{
FTM0->CONTROLS[0].CnSC = FTM_CnSC_MSB_MASK
|FTM_CnSC_ELSB_MASK;
FTM0->CONTROLS[0].CnV = 50; /* FTM0 ch1 compare value (~75% duty cycle) */
}
void start_FTM0_counter (void)
{
FTM0->SC |= FTM_SC_CLKS(3);
}
Great thanks.
Solved! Go to Solution.
I find that the problem is from the PRINTF function. As I comment this line, the PWM output is always normal and is not influenced by SPI transfer. The project is configurated of ewl_c Console mode, so I can see the information printed by printf in the S32DS console window. I think the printf occupies CPU core, but I do not know the reason. Perhaps someone can tell me why.
I had the same problem with LPUART and PWM.
Removing printf also worked, thanks!
Best regads,
Lauch_Spargel
Hi Carlos,
Have you try to debug the project and find any issue? LPSPI should not interfere with the PWM waveform. Would you please attached a simple test project here? So that I can check it on S32K EVB.
Best Regards,
Robin
I find that the problem is from the PRINTF function. As I comment this line, the PWM output is always normal and is not influenced by SPI transfer. The project is configurated of ewl_c Console mode, so I can see the information printed by printf in the S32DS console window. I think the printf occupies CPU core, but I do not know the reason. Perhaps someone can tell me why.