RTC TIMER ON K60

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

RTC TIMER ON K60

1,993 Views
Karts
Contributor I

Can someone please help me in configuring the RTC timer on K60 to be able to generate an interrupt after 1 sec.The following is the code snippet that i used to configure the RTC.However,on calling "enable_irq(66)" instruction,the control is switched to ISR routine immediately without even executing further instructions that are supposed to get executed.Even on using this instruction "enable_irq(66)" at the end of the function at the end of the function "void rsi_mcu_rtc_init(uint32 time_in_seconds)",the control switches to ISR immediately. which i measure it to about 500ms.

 

Here i am measuring time on an oscilloscope by toggling a GPIO pin using "enable_test_gpio()" and "disable_test_gpio()" which wont show an expected time of 1sec.

 

So,what i expect is that only after enabling the counter should the timer start ticking and after 1 sec only the control should switch to the ISR routine.

 

Appreciate your help in helping me solve this issue.

 

Code:

 

uint8 rsi_rtc_interrupt_flag = 0;

void rsi_mcu_rtc_init(uint32 time_in_seconds)
{
    int i;
    
    /*enable the clock to SRTC module register space*/
    SIM_SCGC6 |= SIM_SCGC6_RTC_MASK;
    
    /*Only VBAT_POR has an effect on the SRTC, RESET to the part does not, so you must manually reset the SRTC to make sure everything is in a known state*/
    /*clear the software reset bit*/
    RTC_CR  = RTC_CR_SWR_MASK;
    RTC_CR  &= ~RTC_CR_SWR_MASK;  


     /*Enable the interrupt*/
     enable_irq(66);


    /*Enable the oscillator*/
    RTC_CR |= RTC_CR_OSCE_MASK;
      
    /*Wait to all the 32 kHz to stabilize, refer to the crystal startup time in the crystal datasheet*/
    for(i=0;i<0x600000;i++);
      
    /*Set time compensation parameters*/
    //RTC_TCR = RTC_TCR_CIR(0) | RTC_TCR_TCR(0);
      
    /*Configure the timer seconds and alarm registers*/
    RTC_TSR = time_in_seconds;
    //RTC_TPR = 0x8008;
    RTC_TAR = 0x00;
    
    enable_test_gpio();  // Drive the  GPIO pin  HIGH (to measure time)


    /*Enable the counter*/
    RTC_SR |= RTC_SR_TCE_MASK;
        
    while(rsi_rtc_interrupt_flag == 0);

 

    disable_test_gpio();  // Drive the GPIO pin LOW (to measure time)

     
}

void rsi_rtctimer_ISR(void)
{
    rsi_rtc_interrupt_flag = 1;
    RTC_IER = (0<<RTC_IER_TOIE_SHIFT)& RTC_IER_TOIE_MASK;
    printf("ISR\n");
}


 

0 Kudos
3 Replies

636 Views
mjbcswitzerland
Specialist V

Hi

 

I suspect that the overflow interrupt is immediately firing because the count value is set to 0. The startup time can be up to 1s so the dela yloop looks too fast and maybe the interrupt immediately firing after the oscillator starst is teh delay that you are measuring (?)

 

I would use the TAIE interrupt.

Set the RTC_TSR value and then set RTC_TAR = (RTC_TSR);

This fires after one second count.

 

To get a periodic interrupt the following is done in the interrupt handler:

    register unsigned long ulNewSeconds = RTC_TSR;                       // reset interrupt flag
    RTC_TAR = ulNewSeconds;                                              // set timer alarm interrupt to next second

 

You can get full RTC code from the uTasker project including Gregorian time calender support and an interface compatible with the Coldfire RTC (with stopwatch, alarm, minutes, hours etc.). Brief details at http://www.utasker.com/forum/index.php?topic=1656.0

 

Regards

 

Mark

 

 

0 Kudos

636 Views
Karts
Contributor I

Hi Mark,

 

What i am trying to measure is the time of 1 sec that the RTC takes to generate an interrupt after it really starts ticking.I modified the code as you suggested.However,i still cannot see that 1 sec timing on my oscilloscope which is an indication to me that the RTC fires up interrupt after 1 second.

 

The strange behaviour that i am unable to comprehend is that,as soon as "enable_irq(66) is executed,immediately the CPU control switches to ISR routine.And only after the ISR is executed the rest of the code which invloves setting the TSR register,setting the oscillator etc are executed.So,i used "Cpu_DisableInt()" before calling "enable_irq(66)".This did stop the CPU control from going into the ISR immediately.However,this still did not help me indentify the timing aspect of 1sec. after which the interrupt is expected to fire up.

 

Apppreciate if you could help me resolve this issue.

 

Code:


void rsi_mcu_rtc_init(uint32 time_in_seconds)
{
    int i;
    
    /*enable the clock to SRTC module register space*/
    SIM_SCGC6 |= SIM_SCGC6_RTC_MASK;
    
    /*Only VBAT_POR has an effect on the SRTC, RESET to the part does not, so you must manually reset the SRTC to make sure everything is in a known state*/
    /*clear the software reset bit*/
    RTC_CR  = RTC_CR_SWR_MASK;
    RTC_CR  &= ~RTC_CR_SWR_MASK;
    
    //Cpu_DisableInt();
    
    /* Enable RTC interrupt in NVIC */
    enable_irq(66);
    
    /*Enable the oscillator*/
    RTC_CR |= RTC_CR_OSCE_MASK;
      
    /*Wait to all the 32 kHz to stabilize, refer to the crystal startup time in the crystal datasheet*/
    for(i=0;i<0x600000;i++);
      

    /* Set seconds register */
    RTC_TSR = 0x01;
    
    /* Set alarm register */
    RTC_TAR = RTC_TSR;
    
    /* To calculate RTC time */
    enable_test_gpio();
    
    /* Enable the counter */
    RTC_SR |= RTC_SR_TCE_MASK;
    
    //Cpu_EnableInt();


    while(rsi_rtc_interrupt_flag == 0);
    
    /* To calculate RTC time */
    disable_test_gpio();
}

void rsi_rtctimer_ISR(void)
{
    rsi_rtc_interrupt_flag = 1;
    RTC_SR &= 0x07;  //clear TCE, or RTC_TSR can  not be written
    RTC_TSR = 0x00000000;  //clear TIF
    RTC_IER &= ~RTC_IER_TAIE_MASK;
    printf("ISR\n");

}

 

Thanks.

 

Regards,

Karthik

 

0 Kudos

636 Views
mjbcswitzerland
Specialist V

Hi Karthik

 

If you are getting an interrupt as soon as the RTC interrupt is enabled it means that there is a pending interrupt that needs to be first cleared.

 

I suggest not enabling interrupts until the RTC is stable and completely configured.

The pending interrupt is cleared by a read from RTC_TSR so the RTC interrupt should certainly not be enabled until after that is done.

 

Regards

 

Mark

 

0 Kudos