FTM PWM setup problem

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

FTM PWM setup problem

1,225 Views
roger5
Contributor III

I have a problem setting up PWM on MK22

The device has a backlight LED on an LCD display, which is on PORTC pin 4

Simple GPIO works fine, but I cant seem to get PWM working

Using the SDK example as a template, I've set the port/pin as

    PORT_SetPinMux(PORTC,4, kPORT_MuxAlt4);/* Configured as PWM FTM0_CH4 */

But I'm not sure if kPOST_MuxAlt4 is OK for Port C, since the example LED pin was on Port D

I've setup the FTM as per the template, except I'm only using 1kHz instead of 24kHz

    ftm_config_t ftmInfo;
    ftm_chnl_pwm_signal_param_t ftmParam;
    ftmParam.chnlNumber = BOARD_FTM_CHANNEL;
    ftmParam.level = kFTM_LowTrue;
    ftmParam.dutyCyclePercent = 99;
    ftmParam.firstEdgeDelayPercent = 0U;

    FTM_GetDefaultConfig(&ftmInfo);

    FTM_Init(BOARD_FTM_BASEADDR, &ftmInfo);/* Initialize FTM module */
    FTM_SetupPwm(BOARD_FTM_BASEADDR, &ftmParam, 1U, kFTM_CenterAlignedPwm, 1000U, FTM_SOURCE_CLOCK);

    FTM_StartTimer(BOARD_FTM_BASEADDR, kFTM_SystemClock);

But I don't seem to be getting any PWM from the pin.

I also tried setting the pin as output

GPIO_PinInit(PORTC, 4, &pin_config_output);

But that didn't resolve the problem.

So I don't know what I've missed or have done wrong.

Thanks

Labels (1)
5 Replies

1,052 Views
nxf51211
NXP Employee
NXP Employee

Hi Roger,

I would like to ask you if you enabled the clock for the PORTC. This could be the reason why you are not getting a response from the PWM (This is usually set inside the BOARD_InitPins function from the pinmux.c file):

/* Port C Clock Gate Control: Clock enabled */
CLOCK_EnableClock(kCLOCK_PortC)‍‍‍‍;

Best Regards,

Ricardo Delsordo

0 Kudos

1,052 Views
roger5
Contributor III

Thanks

I already had

    CLOCK_EnableClock(kCLOCK_PortC);

Because I was already controlling the pin using simple GPIO. i.e.

This is my entire code, using a define for DISPLAY_LED_PWM so I can compile either for the old ON/OFF using GPIO or the new enhanced version which will have PWM dimming of the display LED.

   #ifdef DISPLAY_LED_PWM
    PORT_SetPinMux(Port_Display_Light, Pin_Display_Light, kPORT_MuxAlt4);/* Configured as PWM FTM0_CH4 */
#else
    PORT_SetPinMux(Port_Display_Light, Pin_Display_Light, kPORT_MuxAsGpio);
#endif    
    
#ifdef DISPLAY_LED_PWM
    ftm_config_t ftmInfo;
    ftm_chnl_pwm_signal_param_t ftmParam;
    ftm_pwm_level_select_t pwmLevel = kFTM_LowTrue;

    /* Configure ftm params with frequency 24kHZ */
    ftmParam.chnlNumber = BOARD_FTM_CHANNEL;
    ftmParam.level = pwmLevel;
    ftmParam.dutyCyclePercent = 0x80;
    ftmParam.firstEdgeDelayPercent = 0U;
    FTM_GetDefaultConfig(&ftmInfo);
    FTM_Init(BOARD_FTM_BASEADDR, &ftmInfo);/* Initialize FTM module */
    FTM_SetupPwm(BOARD_FTM_BASEADDR, &ftmParam, 1U, kFTM_CenterAlignedPwm, 24000U, FTM_SOURCE_CLOCK);
    FTM_EnableInterrupts(BOARD_FTM_BASEADDR, FTM_CHANNEL_INTERRUPT_ENABLE);    /* Enable channel interrupt flag.*/
    EnableIRQ(FTM_INTERRUPT_NUMBER);    /* Enable at the NVIC */
    FTM_StartTimer(BOARD_FTM_BASEADDR, kFTM_SystemClock);

#else
    GPIO_PinInit(GPIO_Display_Light, Pin_Display_Light, &pin_config_output);
    GPIO_PinWrite(GPIO_Display_Light, Pin_Display_Light, 0);
#endif

I don't have the FTM_LED_HANDLER() function in my code, and I don't think its actually used in the twrk22fm120m_ftm_simple_pwm  example (from the SDK examples), because I can remove  FTM_LED_HANDLER from the example and it still builds without an errors

What seems odd is there does not seem to be anything equivalent to GPIO_PinInit to set the pin as an output, except perhaps thats what FTM_SetupPwm() does

I can't find where  the pin mux for kPORT_MuxAlt4 is documented. All the SDK headers just list it as "device specific"

But there must be some documentation somewhere which describes what kPORT_MuxAlt4 means on the K22 series devices

0 Kudos

1,052 Views
nxf51211
NXP Employee
NXP Employee

Hi Roger,

What Mark says is correct, you need to either change the pin you are using so it matches FTM0_CH4, or just try your project with FTM0_CH3:

pastedImage_2.png

pastedImage_1.png

pastedImage_3.png

Best Regard,

Ricardo Delsordo.

0 Kudos

1,052 Views
roger5
Contributor III

Thanks guys

The documentation for this is really hard to find.

It looks like page 220 in Document Number: K22P121M120SF7RM  Rev. 4, 08/2016 kinda has this information buried in with the technical descriptions of the pins.

I mistakenly assumed that if the example in the SDK used Port D pin 4 and used FTM0_CH4 and alt_mux4, then it would be the same for my LED pin which just happens to be on the same pin on Port C.

Its all working now.

0 Kudos

1,052 Views
mjbcswitzerland
Specialist V

Hello Roger

What value do you have for

BOARD_FTM_CHANNEL?

If you want PWM output on PTC4 it must be 3 and the following comment would be presently incorrect since ALT4 on PTC4 is FTM0_CH3 and not FTM0_CH4.

PORT_SetPinMux(Port_Display_Light, Pin_Display_Light, kPORT_MuxAlt4);/* Configured as PWM FTM0_CH4 */

As reference I have shown a simulated K22 running in the uTasker project with PWM on your output:

pastedImage_1.png

uTasker reference code:

    PWM_INTERRUPT_SETUP pwm_setup;
    pwm_setup.int_type = PWM_INTERRUPT;
    pwm_setup.pwm_mode = (PWM_SYS_CLK | PWM_PRESCALER_16 | PWM_EDGE_ALIGNED); // clock PWM timer from the system clock with /16 pre-scaler
    pwm_setup.int_handler = 0;                                           // no user interrupt call-back on PWM cycle
    pwm_setup.pwm_reference = (_TIMER_0 | 3);                            // timer module 0, channel 3
    pwm_setup.pwm_frequency = PWM_FREQUENCY(1000, 16);                   // generate 1000Hz on PWM output
    pwm_setup.pwm_value   = _PWM_PERCENT(20, pwm_setup.pwm_frequency);   // 20% PWM (high/low)
    _CONFIG_PERIPHERAL(C, 4, (PC_4_FTM0_CH3 | (PORT_SRE_FAST | PORT_DSE_HIGH))); // FTM0_CH3 on PC.4 (alt. function 4)
    fnConfigureInterrupt((void *)&pwm_setup);                            // enter configuration for PWM test

kPORT_MuxAlt4 is the same for all parts since it is just the value 4, to select the ALT4 function of the respective pin. It is marked in the FSL code as chip specific due to the fact that the MUX function it selects is chip (and port) dependent and not that its value is chip dependent.

Note that in the uTasker library a higher level of extraction is used (eg. PC_4_FTM0_CH3) so that it is not possible to make mistakes between ALT function and pin mapping (this will be 4 for a K22 and may equate to a different value on chips that need different ALT values).

Also its chip simulator indicates when code has not set up peripherals correctly in order to develop and debug with highest efficiency.

Regards

Mark

Complete Kinetis solutions for professional needs, training and support:http://www.utasker.com/kinetis.html
uTasker: supporting >1'000 registered Kinetis users get products faster and cheaper to market
Request Free emergency remote desk-top consulting at http://www.utasker.com/services.html