Using RTC module on FRDM-KL25Z

Document created by Adrian Sanchez Cano Employee on May 3, 2013
Version 1Show Document
  • View in full screen mode

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.

RTC 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.

RTC 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.

RTC 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.

1 person found this helpful

Outcomes