Using RTC module on FRDM-KL25Z

cancel
Showing results for 
Search instead for 
Did you mean: 

Using RTC module on FRDM-KL25Z

No ratings

Using RTC module on FRDM-KL25Z

The Real Time Clock (RTC) module is the right tool when we want to keep tracking the current time for our applications.

For the Freedom Platform (KL25Z) the RTC module features include:

  • 32-bit seconds counter with roll-over protection and 32-bit alarm
  • 16-bit prescaler with compensation that can correct errors between 0.12 ppm and 3906 ppm.
  • Register write protection.
    • Lock register requires POR or software reset to enable write access.
  • 1 Hz square wave output.

This document describes how to implement the module configuration. Also, how to modify the hardware in order feed a 32 KHz frequency to RTC module (it is just a simple wire link).

 

 

Hardware.

The RTC module needs a source clock of 32 KHz. This source is not wired on the board; hence we need to wire it. Do not be afraid of this, it is just a simple

wire between PTC3 and PTC1 and the good news are that these pins are external.

4110_4110.pngRTC wire frdm.png

 

  • PTC1 is configured as the RTC_CLKIN it means that this is the input of source clock.

 

 

  • PTC3 is configured as CLKOUT (several options of clock frequency can be selected in SIM_SOPT2[CLKOUTSEL] register). For this application we need to select the 32 Khz clock frequency.

 

 

 

 

 

 

 

 

 

 

 

 

RTC configuration using Processor Expert.

First of all we need to set the configurations above-mentioned in Component Inspector of CPU component.

  1. Enable RTC clock input and select PTC1 in Pin Name field. This selects PTC1 as RTC clock input.
  2. MCGIRCLK source as slow in Clock Source Settings > Clock Source Setting 0 > Internal reference clock > MCGIRCLK source. This selects the 32 KHz clock frequency.
  3. Set ERCLK32K Clock Source to RTC Clock Input in Clock Source Settings > Clock Source Setting 0 > External reference clock > ERCLK32K Clock Source. This sets the RTC_CLKIN as the 32 KHz input for RTC module.
  4. Select PTC3 as the CLKOUT pin and the CLKOUT pin output as MCGIRCLK in Internal peripherals > System Integration Module > CLKOUT pin control.

4111_4111.pngRTC pex conf.png

With this procedure we have a frequency of 32 KHz on PTC3 and PTC1 configured as RTC clock-in source. The MCG mode configurations in this case is PEE mode: 96 MHz PLL clock, 48 MHz Core Clock and 24 MHz Bus clock.

 

For the RTC_LDD component the only important thing is to select the ERCKL32K as the Clock Source. The image below shows the RTC_LDD component configuration for this application.

4112_4112.pngRTC clck source.png

 

After this you only need to Generate Processor Expert Code and write your application.  The code of this example application can be found in the attachments of the post. The application prints every second the current time.

 

 

RTC bare-metal configuration.

For a non-PEx application we need to do the same configurations above.

  • Enable the internal reference clock. MCGIRCLK is active.

         MCG_C1 |= MCG_C1_IRCLKEN_MASK;

  • Select the slow internal reference clock source.

         MCG_C2 &= ~(MCG_C2_IRCS_MASK);

  • Set PTC1 as RTC_CLKIN and select 32 KHz clock source for the RTC module.

         PORTC_PCR1 |= (PORT_PCR_MUX(0x1));         

    SIM_SOPT1 |= SIM_SOPT1_OSC32KSEL(0b10);

  • Set PTC3 as CLKOUT pin and selects the MCGIRCLK clock to output on the CLKOUT pin.

    SIM_SOPT2 |= SIM_SOPT2_CLKOUTSEL(0b100);

    PORTC_PCR3 |= (PORT_PCR_MUX(0x5));


 

And the RTC module configuration could be as follows (this is the basic configuration just with seconds interrupt):

  • Enable software access and interrupts to the RTC module.

    SIM_SCGC6 |= SIM_SCGC6_RTC_MASK;

  • Clear all RTC registers.

 

RTC_CR = RTC_CR_SWR_MASK;

RTC_CR &= ~RTC_CR_SWR_MASK;

 

if (RTC_SR & RTC_SR_TIF_MASK){

     RTC_TSR = 0x00000000;

}

  • Set time compensation parameters. (These parameters can be different for each application)

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

  • Enable time seconds interrupt for the module and enable its irq.

enable_irq(INT_RTC_Seconds - 16);

RTC_IER |= RTC_IER_TSIE_MASK;

  • Enable time counter.

RTC_SR |= RTC_SR_TCE_MASK;

  • Write to Time Seconds Register.

RTC_TSR = 0xFF;

 

After this configurations you can write your application, do not forget to add you Interrupt Service Routine to the vector table and implement an ISR code.

 

In the attachments you can find two zip files: PEx application and non-PEx application.

 

I hope this could be useful for you,

 

Adrián Sánchez Cano.

Original Attachment has been moved to: FRDM-KL25Z-RTC-TEST.zip

Original Attachment has been moved to: FRDM-KL25Z-PEx-RTC.zip

Comments

Could I use OSC32KCLK as RTC clock source?

HOW?Is it possible that OSC32KCLK output 32.768KHz? HOW?

it's possible to use only system oscillator without external connection?

thanks

A good tutorial.

I am using LPTMR (1ms) to generate 1 second interrupt as my RTC.

Hi Adrián

Why did you not use the internal OSC32KCLK as RTC source, rather than generating the clock on RTC_CLKOUT and looping it back to RTC_CLKIN?

Also, by using a 1s interrupt and priming RTC_TPR with (32768 - 1000) gives a 1s time base from the LPO 1kHz clock (may be not as accurate as the ERCLK32K socurce but can also be compensated).

Regards

Mark

RTC_TPR with (32768 - 1000) this base is not continued in LLs mode..... in LLs mode it is taking whole cycle from 0 to 32768 to over flow....so is there any way to solve this..??

H

See the following: µTasker KL RTC Support "Using LPO to achieve RTC functionality"

The RTC will run about 32x slower in LLD mode but it is possible to compensate by saving the count and prescale value before entering the LLS state and then compensating the time from the values after wakeup.

Regards

Mark

Hi Mark,

I can answer that question for you.  Adrian didn't use OSC32KCLK as the RTC source because he wanted to run the Oscillator in a higher range mode to be able to use the PLL.  If he had used OSC32KCLK then there would have had to be a 32 kHz crystal on the XTAL/EXTAL pins. 

Regards,

Chris

How to configure timer1 and timer2 in frdm kl25z..??

Hello, I was using the code you uploaded but it didn't work properly, I added this line because you need to enable the oscillator.

This is what you have:

RTC_CR = RTC_CR_SWR_MASK;

  RTC_CR &= ~RTC_CR_SWR_MASK;

This is what I added after the previous line :

  RTC_CR |= RTC_CR_OSCE_MASK;

Now it works correctly.

if I made a mistake please let me know, Thanks.

Version history
Revision #:
1 of 1
Last update:
‎05-03-2013 08:53 AM
Updated by: