Call a function or task every 10us on Vybrid M4

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

Call a function or task every 10us on Vybrid M4

581 Views
israelcass
Contributor I

Hi guys.

I'm need to make a implementation thats call a function(or task) every 10us on a M4 Vybrid.

Looking on manuals and Internet I realized that the TIMER functions won't help me because the tick is 10ms.

Looking more I discovered GPT and PIT but I won't capable of discover how can I use it.

Are you have some documents or examples to help me ?

I find this example: Timer example not working for timer value in micro seconds on MQX 3.6.1 MCF52259evb 

and this: https://community.nxp.com/thread/116150 

but I didn't understand at all.

Can someone help me? I need, somehow, to call a function or task, both to me are fine, every 10us.

Thanks.

0 Kudos
1 Reply

402 Views
danielchen
NXP TechSupport
NXP TechSupport

Hi

Please refer to following code, it is taken from below thread

Hardware interruptions 

Just a brief explanation here:

First , configure and start a PIT timer, please see function SetPIT/Reset PIT / Start PIT/ StopPIT   ;

Next, install the PIT isr  to RAM, use _int_install_kernel_isr() to bypass MQX RTOS and let the interrupt be serviced immediately

Next, you can call your function in the PIT isr, see PIT_tick_isr.

One note is, you can't call MQX functions in the isr.

#define PIT_TIMER                   (2)

#define PIT_TMR_INTVEC  (INT_PIT0 + PIT_TIMER)
//some utilities

/*******************************************************************************/
/* void  SetPit(uint_16 rate)                                           */
/* Description:-                                                               */
/* Calculate Hardware timer count on Kinetis for appropriate f (in Hz) */
/* And jam into PIT register = which resets the PIT counter */
/* In general, the Peripheral bus clock which is used to drive PIT module      */
/* runs at half the main clock speed. (i.e. around 48MHz)                      */
/*******************************************************************************/
void  SetPIT(uint_16 rate)
{
   PITTimerCounts = BSP_BUS_CLOCK/rate;
   PIT_LDVAL(PIT_TIMER) = PITTimerCounts;
}
/*******************************************************************************/
/* void  ResetPIT(void)                                           */
/* Description:-                                                               */
/* jam precalculated count into PIT register = which resets the PIT counter */
/*******************************************************************************/
void  ResetPIT(void)
{
   PIT_LDVAL(PIT_TIMER) = PITTimerCounts;
}
/*******************************************************************************/
/* void StartPIT(void)                                                     */
/* Description:-                                                               */
/* jam precalculated count into PIT register = which resets the PIT counter */
/* Turn ON PIT in Kinetis and clear state flag */
/*******************************************************************************/
void  StartPIT(void)
{
  PIT_LDVAL(PIT_TIMER) = PITTimerCounts;
  PIT_TCTRL(PIT_TIMER) |= PIT_TCTRL_TEN_MASK;//turn on timer
  PIT_Stopped = FALSE;
}
/*******************************************************************************/
/* void StopPRF(void)                                                     */
/* Description:-                                                               */
/* Turn off PIT in Kinetis and set state flag */
/*******************************************************************************/
void  StopPIT(void)
{
  PIT_TCTRL(PIT_TIMER) &= ~PIT_TCTRL_TEN_MASK;//turn off timer
  PIT_Stopped = TRUE;
}

//here is the interrupt handler

#if MQX_ROM_VECTORS
//MQX_ISR takes a parameter
void PRF_tick_isr( pointer user_isr_ptr )
#else
//kernel ISR is void void
void PIT_tick_isr( void )
#endif
{
volatile uint_32 tmp;


   tick++;//do stuff here


   PIT_TFLG(PRF_TIMER) = PIT_TFLG_TIF_MASK; //finally, write a 1 to clear the interrupt
   tmp = PIT_CVAL(PRF_TIMER);//see errata
}
/*
Kinetis e2682: PIT: Does not generate a subsequent interrupt after clearing the interrupt flag
Errata type: Errata
Description: The PIT does not generate a subsequent interrupt after clearing the interrupt flag in the ISR
upon receiving the first interrupt. Thus, the second interrupt will never be triggered.
Workaround: In order to enable the use of subsequent interrupts from the PIT, the user must access any PIT
register after clearing the interrupt flag in the ISR.
*/
//here the routine to install the interrupt

void  InstallTimerInterrupt(void)
{
    StopTimer();//for safety, make sure the timer is disabled.
#if MQX_ROM_VECTORS
    _int_install_isr(PIT_TMR_INTVEC, PIT_tick_isr, NULL);
#else
     _int_install_kernel_isr(PIT_TMR_INTVEC, PIT_tick_isr);
#endif   
    PIT_MCR = PIT_MCR_FRZ_MASK;//enable timers but stop when in debug mode

    PIT_TCTRL(PRF_TIMER) = PIT_TCTRL_TIE_MASK;//turn on interrupt, but do not enable timer

    _bsp_int_init(PRF_TMR_INTVEC,3,0,TRUE);//also need to enable the interrupt controller
    //this bit is hidden deep in the guts of the ARM cortex documentation.
}

 

Regards

Daniel

0 Kudos