AnsweredAssumed Answered

RTC with VBAT - Bare Metal

Question asked by weblar on Oct 5, 2012
Latest reply on Oct 9, 2012 by zhaohuiliu

I'm trying to get the RTC working on a Kinetis K60 which uses a coin cell for battery backup.

 

The problem I'm having is that when I reset the system, each time I attempt to read the RTC_TSR using the rtc_get_time function, I always get a TIF (time invalid) flag set in the status register. The only way this can work is if I write RTC_TSR = 0 in the initialization routine, which doesn't seem correct (its meant to be a battery backed-up register, no?).

 

I'm using the following code...

 

void rtc_init() {

 

  // Reset the RTC by setting and clearing the SWR bit in the control register

  RTC_CR |= RTC_CR_SWR_MASK;

  RTC_CR &= ~RTC_CR_SWR_MASK;

 

  RTC_TAR = 0xFFFFFFFF;

 

  // Enable the oscillator

  RTC_CR |= RTC_CR_OSCE_MASK;

 

  delay_ms(125);

 

  RTC_TCR = RTC_TCR_CIR(0) | RTC_TCR_TCR(0);

  //RTC_TSR = 0;

  RTC_TAR = 0;

  RTC_TPR = 0;

 

  // Enable the time counter

  RTC_SR |= RTC_SR_TCE_MASK;

 

}

 

 

void rtc_set_time(uint32_t ticks) {

 

  // Disable the RTC

  RTC_SR &= ~RTC_SR_TCE_MASK;

 

  // Set the new time since UNIX epoch (01/01/1970)

  RTC_TSR = ticks;

 

  // Enable the RTC

  RTC_SR |= RTC_SR_TCE_MASK;

 

}

 

 

void rtc_get_time(uint32_t *ticks) {

 

  uint32_t sr = RTC_SR;

 

  if (sr & RTC_SR_TIF_MASK) {

  // Time Invalid flag set

  }

 

  *ticks = RTC_TSR;

 

}

 

I'm well aware that I'm probably doing something stupid but I can't for the life of me figure it out.

 

Any suggestions are very welcome.

 

Thanks,

Kev

 

Outcomes