KL25 - RTC and system oscillator

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

KL25 - RTC and system oscillator

3,221 Views
silviopistolesi
Contributor II

I'm trying to run the RTC of a KL25 using the internal oscillato but it does not work. If I use an external CLK works. I am using an 8MHz quartz. can anyone help me?

Labels (1)
0 Kudos
14 Replies

1,347 Views
silviopistolesi
Contributor II

I just find a solution but it is not 'elegant'. If set the LPO option the RTC_TPR begin increment and RTC_TSR an increment every 32 seconds.

The code is the following:

void rtc_init_def(void)

{

  /*enable the clock to SRTC module register space*/

  SIM_SCGC6 |= SIM_SCGC6_RTC_MASK;

  RTC_SR &= ~RTC_SR_TCE_MASK;

   

  //Configure the TSR and TAR

  RTC_TSR =  0x00000000; //RTC Time Seconds Register

  RTC_TPR =  0x00007c18; //valore da calibrare

  RTC_TAR = RTC_TSR + ALARM_TIME; //RTC Time Alarm Register

  /* enable the RTC_CLKIN function */

  SIM_SOPT1 &= SIM_SOPT1_OSC32KSEL_MASK;

  SIM_SOPT1 |= SIM_SOPT1_OSC32KSEL(3); /* Selects the 1 kHz clock source (LPO) for RTC */

 

  RTC_IER |= RTC_IER_TSIE_MASK | RTC_IER_TAIE_MASK | RTC_IER_TOIE_MASK | RTC_IER_TIIE_MASK;

  RTC_SR  |= RTC_SR_TCE_MASK;       //Enable RTC_SR_TCE    

  #ifdef CMSIS

  NVIC_EnableIRQ(RTC_IRQn);

  NVIC_EnableIRQ(RTC_Seconds_IRQn);

  #else

  enable_irq(INT_RTC-16);

  enable_irq(INT_RTC_Seconds-16);

  #endif

}

#ifdef CMSIS

void RTC_Seconds_IRQHandler(void)

#else

void rtc_isrv_seconds(void)

#endif

{

  RTC_SR = 0x00000000;

  RTC_TPR = 0x00007C18;

  RTC_SR = 0x00000010;

  rtc_seconds_isrv_count++;

}

#ifdef CMSIS

void RTC_IRQHandler(void)

#else

void rtc_isrv(void)

#endif

{

    uint32 rtc_sr = RTC_SR;

    if(rtc_sr & RTC_SR_TAF_MASK) // RTC timer alarm flag is set

    {

      LED1_TOGGLE;

      RTC_TAR = RTC_TAR + ALARM_TIME;     // write new value to TAR to clear TAF

      seconds_count++;     

    }

   

    if(rtc_sr & RTC_SR_TOF_MASK) // RTC timer Overlow flag is set

    {

     RTC_SR |= RTC_SR_TOF_MASK;

    }

   

    if (rtc_sr & RTC_SR_TIF_MASK) // Timer Invalid flag

    {

        RTC_SR &= ~RTC_SR_TCE_MASK;  //Disable timer

        RTC_TSR = 0x00; // write to clear TOF or TIF

        RTC_SR |= RTC_SR_TCE_MASK;   //re-enable timer

    }

}

0 Kudos

1,347 Views
Hui_Ma
NXP TechSupport
NXP TechSupport

Hi,

When the RTC clock is LPO (1KHz), the second register will increase 1 every 32.768 seconds.

Thank you for the attention.

0 Kudos

1,347 Views
silviopistolesi
Contributor II

every 32,768 seconds, i know.

if I set SIM_SOPT1 |= SIM_SOPT1_OSC32KSEL(0); the RTC don't work!

why?

Thank you for attention.
0 Kudos

1,347 Views
Hui_Ma
NXP TechSupport
NXP TechSupport

Hi,

When you select to use SIM_SOPT1_OSC32KSEL(0) setting, first of all it need external 32KHz crystal or oscillator connect with system oscillator. Then the KL25 MCG work mode should be in FEI or FEE mode (external 32KHz clock can't be PLL reference clock, so PLL can't be enabled).

You can use below code to enable OSC32KCLK as RTC clock:

    SIM_SOPT1 |= SIM_SOPT1_OSC32KSEL(0);  //select system osciallto as RTC clock source

    OSC0_CR |= OSC_CR_ERCLKEN_MASK;  //external reference clock is enabled

    RTC_CR |= (RTC_CR_OSCE_MASK);   //32.768KHz oscillator is enabled

Wish it helps.

0 Kudos

1,347 Views
silviopistolesi
Contributor II

For my application I must use the cristal oscillator at 8Mhz and I can't use the 32Khz.

below look my program for the initializatione because the istruction   RTC_CR |= (RTC_CR_OSCE_MASK);  crash the program.

thanks

void rtc_init_def2(void)

{

  /*enable the clock to SRTC module register space*/

  SIM_SCGC6 |= SIM_SCGC6_RTC_MASK;

  RTC_SR &= ~RTC_SR_TCE_MASK;

   

  //Configure the TSR and TAR

  RTC_TSR =  0x00000000; //RTC Time Seconds Register

  RTC_TAR = RTC_TSR + ALARM_TIME; //RTC Time Alarm Register

  /* enable the RTC_CLKIN function */

  SIM_SOPT1 &= SIM_SOPT1_OSC32KSEL_MASK;

  SIM_SOPT1 |= SIM_SOPT1_OSC32KSEL(0); /* Selects the 1 kHz clock source (LPO) for RTC */

 

  OSC0_CR |= OSC_CR_ERCLKEN_MASK;  //external reference clock is enabled

  RTC_CR |= (RTC_CR_OSCE_MASK);   //32.768KHz oscillator is enabled !!! CRASH THE PROGRAM !!!

  RTC_IER |= RTC_IER_TSIE_MASK | RTC_IER_TAIE_MASK | RTC_IER_TOIE_MASK | RTC_IER_TIIE_MASK;

  RTC_SR  |= RTC_SR_TCE_MASK;       //Enable RTC_SR_TCE    

  #ifdef CMSIS

  NVIC_EnableIRQ(RTC_IRQn);

  NVIC_EnableIRQ(RTC_Seconds_IRQn);

  #else

  enable_irq(INT_RTC-16);

  enable_irq(INT_RTC_Seconds-16);

  #endif

}

0 Kudos

1,347 Views
chris_brown
NXP Employee
NXP Employee

Silvio,

What you are trying to do is impossible.  If you want to use the system oscillator to clock the RTC (SIM_SOPT1[OSC32KSEL] = 0), then the system oscillator must be configured to use a 32 KHz source.  There is no way to use an 8 MHz crystal and clock the RTC from OSC32KCLK.

Also, to answer your claim that RTC_CR |= (RTC_CR_OSCE_MASK); crashes the program.  I assume you are using an 8 MHz oscillator to clock the system clock.  In which case, I would expect this to crash the program.  From the KL25 reference manual:

RTC_CR[OSCE] can override the configuration of the System OSC, configuring the

OSC for 32kHz crystal operation in all power modes (except VLLS0) and through any

System Reset. When OSCE is enabled, the RTC also overrides the capacitor

configurations.

So when you set OSCE bit, you ARE re-configuring the System OSC for 32KHz crystal operation. We apologize for any inconvenience this may have caused. 

Sincerely,

Chris 

1,347 Views
chris_brown
NXP Employee
NXP Employee

Silvio,

You could also try what Graeme Bragg has suggested in this post.

FRDM-KL25Z how to get RTC work properly

This should work for you.  You will have to a little external wiring but shouldn't be too hard to blue wire your board for this.

Hope this helps,

Chris

1,347 Views
bobpaddock
Senior Contributor III

"external 32KHz clock can't be PLL reference clock, so PLL can't be enabled"

Anyway to get around that indirectly if the chip can't do it?

Such as calibrating one of the the clocks the PLL can use from this external reference oscillator?

The lowest power temperature compensated external oscillators from MicroCrystal and Maxim are both 32kHz ones.

What I really would like to see is how to run USB from 32kHz external osc like this old part could:

http://www.freescale.com/files/32bit/doc/app_note/AN2539.pdf

0 Kudos

1,347 Views
silviopistolesi
Contributor II

ALARM_TIME = 59

0 Kudos

1,347 Views
lupogrigio
Contributor III

Hi Silvio,

Maybe this thread can help you:

KL25 RTC OSCILLATOR SELECTION

Best Regards

0 Kudos

1,347 Views
silviopistolesi
Contributor II

thanks but i need the system oscillator and not the external oscillator and for my HW it is impossible connect PTC... port.

0 Kudos

1,347 Views
Hui_Ma
NXP TechSupport
NXP TechSupport

Hi,

The KL25 RTC module could use three clock source: LPO, RTC_CLKIN pin input clock and OSC32KCLK (system oscillator 32KHz output). You could get more detailed at RM chapter 5.7.3 RTC clocking. RTC can't use on-chip 32KHz IRC as clock source.

Wish it helps.

RTC clock.jpg

0 Kudos

1,347 Views
silviopistolesi
Contributor II

thanks but i need the system oscillator and not the external oscillator and for my HW it is impossible connect PTC... port.

0 Kudos

1,347 Views
silviopistolesi
Contributor II

Yes, I know but when use the system oscillator (OSC32KSEL) the RTC_TPR not increment

0 Kudos