chrislynch

K70-TWR RTC

Discussion created by chrislynch on Mar 4, 2013
Latest reply on Mar 5, 2013 by Monica Arvizu

Hi,

      I am having issues getting the RTC->TSR & RTC->TPR registers to increment on a K70-TWR board i just received.

 

      I have had no problem getting a K60-TWR RTC to work correctly and the code below is identical (apart from enabling RTC->IER), I am using Keil uVision.

 

void rtc_init(void)

{

  unsigned 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 oscillator*/

  RTC->CR |= RTC_CR_OSCE_MASK | RTC_CR_SC16P_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++);

    for(i=0;i<0x6000000;i++);//120MHz clock, so 838ms long delay for test

     

     /*Set time compensation parameters*/

    RTC->TCR = RTC_TCR_CIR(0) | RTC_TCR_TCR(0);

 

    /*Configure the timer seconds and alarm registers*/

    RTC->TSR = 0x0F;

  RTC->TAR = 0xFFFFFFFF;

   

    /*Enable Interrupts*/

    RTC->IER= RTC_IER_TIIE_MASK | RTC_IER_TOIE_MASK;

   

    /*Enable NVIC RTC interrupt*/

    NVIC_EnableIRQ(RTC_IRQn);

   

  /*Enable the counter*/

  RTC->SR |= RTC_SR_TCE_MASK;

}

 

 

 

void RTC_IRQHandler(void)

{

      

   if((RTC->SR & RTC_SR_TIF_MASK)== 0x01)

     {

       //printf("SRTC time invalid interrupt entered...\r\n");

          RTC->SR &= 0x07;  //clear TCE, or RTC_TSR can  not be written

          RTC->TSR = 0x00000000;  //clear TIF

     }   

   else if((RTC->SR & RTC_SR_TOF_MASK) == 0x02)

   {

          //printf("SRTC time overflow interrupt entered...\r\n");

          RTC->SR &= 0x07;  //clear TCE, or RTC_TSR can  not be written

          RTC->TSR = 0x00000000;  //clear TOF

   }        

   else if((RTC->SR & RTC_SR_TAF_MASK) == 0x04)

   {

         // printf("SRTC alarm interrupt entered...\r\n");

      //  printf("Time Seconds Register value is: %i\n", RTC_TSR);

      RTC->TAR += 1;// Write new alarm value, to generate an alarm every second add 1

   }   

   else

   {

       //printf("No valid Flag was set!\n");

   }

   return;

}

 

Attachments

Outcomes