s32k144 LPIT timer period problem

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

s32k144 LPIT timer period problem

875 Views
fedora
Contributor III

Hello,

    My goal is to generate a waveform with LPIT of S32K144, the LPIT initial and interrupt handler as below:

//-------------------------------------------------------------

LPIT_DRV_Init(INST_LPIT1, &lpit1_InitConfig);
LPIT_DRV_InitChannel(INST_LPIT1, LPIT_CHANNEL, &lpit1_ChnConfig0);
INT_SYS_InstallHandler(LPIT0_Ch0_IRQn, &lpit_level_ctl, (isr_t *) 0);
INT_SYS_SetPriority(LPIT0_Ch0_IRQn, 3U);
INT_SYS_EnableIRQ(LPIT0_Ch0_IRQn);

//-------------------------------------------------------------

static void lpit_level_ctl(void)  //timer period is 2us
{
    PINS_DRV_WritePin(PTA,0,levelA[counter]);  //levelA is waveform data array
    counter++;
    if(counter == data_len{  //the data_len is a global varible  , and data_len is equal to 100
        counter = 0;
        LPIT_DRV_StopTimerChannels(INST_LPIT1, 1U << LPIT_CHANNEL);
    }
    LPIT_DRV_ClearInterruptFlagTimerChannels(INST_LPIT1, 1 << LPIT_CHANNEL);
}
//-------------------------------------------------------------
 
And PE LPIT configuration : Timer period is 2us (need 500k frequency)
fedora_0-1744977860790.png

Clock configuration: Core clock is 48MHz

fedora_1-1744978083421.png

 

At this, I have encountered a problem,  in lpit_level_ctl() function, if the data_len variable is used, then waveform timing generated will be incorrect; But I used a constant (such as 100)  instead of the data_len, and waveform is good.

I would like to know the reason for this issue?  How to solve it that ensure a frequency of 500k?

Thank you for your help!

 

Best regards,

fedora

 

 

0 Kudos
Reply
2 Replies

828 Views
Julián_AragónM
NXP TechSupport
NXP TechSupport

Hi @fedora,

I imagine this is because of the interrupt routine. It is good practice to avoid complex operations in the ISR. You should instead use flags and process everything else in the main code. For example:

 

static void lpit_level_ctl(void)  //timer period is 2us
{
    bFlag = 1;
    LPIT_DRV_ClearInterruptFlagTimerChannels(INST_LPIT1, 1 << LPIT_CHANNEL);
}

main()
{
    if(1 == bFlag)
    {
        bFlag = 0;
        PINS_DRV_WritePin(PTA,0,levelA[counter]);  //levelA is waveform data array
        counter++;
        if(counter == data_len) {  //the data_len is a global varible  , and data_len is equal to 100
            counter = 0;
            LPIT_DRV_StopTimerChannels(INST_LPIT1, 1U << LPIT_CHANNEL);
        }
    }
}

 

I can't see exactly how you are declaring data_len, but make sure it is a global variable, and that the counter is declared as volatile to avoid optimization.

With all that said, you could also use the oc_pal_s32k144 example which uses the output compare to control an output instead.

Best regards,
Julián

0 Kudos
Reply

810 Views
fedora
Contributor III

Thank you,  Julián  !

I understand now and I will continue to research it.

 

Best regards,

fedora

 

0 Kudos
Reply