How to run RTC wihtout using an external oscillator?

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

How to run RTC wihtout using an external oscillator?

1,504 Views
gauravbanyal
Contributor IV

Hello,

I have a custom board for my project using an MKL03Z32. It does not have an on-board external oscillator. So I want to use the RTC without the external oscillator.

The example project for the FRDM-KL03Z board seems to use the external oscillator. I confirmed this by removing R1 and R3 from the board. 

I am running the core clock at 8MHZ. Please let me know how to configure the RTC in this situation. 

PS: I was able to get 1 second interrupts from the RTC block by running it with LPO source clock. The problem is however, in this approach, every time you get the interrupt, you have to configure the RTC prescaler to a value (32768-1000) to get the next 1s interrupt. This is not what I desire. In my design, I just want to run the RTC so that the TSR increments every second in RUN mode and in VLLS3 mode and continues to run in the transitions between these modes. The use case is that the RTC is started while in RUN mode, MCU sleeps in VLLS3 mode, wakes up with an external interrupt source to RUN mode, reads the RTC->TSR and stores the value in the flash and sleeps again in VLLS3 mode. I am using the VLPR for other things in my application, so can't use even that.

Best regards,

Gaurav.

Tags (1)
0 Kudos
7 Replies

1,034 Views
xiangjun_rong
NXP TechSupport
NXP TechSupport

Hi, Gaurav,

The FRDM-KL03Z board has integrated the Y1 external crystal with 32.768KHz, I suppose that the example for RTC use external 32.768KHz as clock source.

If you board do not integrate any crystal or clock source, you have to use internal LPO clock as the RTC clock source.

pastedImage_1.png

This is the setting of RTC clock source:

pastedImage_2.png

so it is okay to set SIM_SOPT1|=3<<18; with the code, the RTC will use LPO as clock source, in the case, you should set the RTC_TPR as 1K, once you set the RTC_TPR when the RTC is disabled, you do not need update the RTC_TPR anymore. You can raed the RTC_TSR register anytime.

Hope it can help you

BR

Xiangjun Rong

0 Kudos

1,034 Views
gauravbanyal
Contributor IV

Hello Xiangjun,

This is exactly what I tried to do expecting the outcome that you have illustrated. But it does not work that way.

Following is my code.

=====================

void init_rtc(){

rtc_config_t rtcConfig;
RTC_GetDefaultConfig(&rtcConfig);
RTC_Init(RTC, &rtcConfig);

/* Select RTC clock source */
BOARD_SetRtcClockSource();
}

void BOARD_SetRtcClockSource(void)
{
SIM->SOPT1 &= ~SIM_SOPT1_OSC32KSEL_MASK;
SIM->SOPT1 |= SIM_SOPT1_OSC32KSEL(3); // 1kHz LPO
RTC_StopTimer(RTC);
RTC->TPR = (32768 - 1000); // use 1kHz internal clock to generate 1s time base by adjusting the prescaler value

uint32_t currSeconds = RTC->TSR;

PRINTF("\r\n TIME NOW = %d", rtc_get_seconds());
RTC_StartTimer(RTC);
}

=====================

In a while(1) loop in the main(), I am reading and printing the value of RTC->TSR.

The first increment happens in 1 second but the next increment of this register happens always only after 32 seconds.

Could you please try it out by modifying the example SDK_2.0_MKL03Z32xxx4_16.02.2017\boards\frdmkl03z\driver_examples\rtc?

Will it continue to increment if the MCU then goes to VLLS3 state and comes back to RUN state?

Appreciate your help.

0 Kudos

1,034 Views
xiangjun_rong
NXP TechSupport
NXP TechSupport

Hi, Gaurav,

I think the code is incorrect:RTC->TPR = (32768 - 1000); // use 1kHz internal clock to generate 1s time base by adjusting the prescaler value.

Pls use the code:

void BOARD_SetRtcClockSource(void)
{
SIM->SOPT1 &= ~SIM_SOPT1_OSC32KSEL_MASK;
SIM->SOPT1 |= SIM_SOPT1_OSC32KSEL(3); // 1kHz LPO
RTC_StopTimer(RTC);
RTC->TPR = 1000; // use 1kHz internal clock to generate 1s time base by adjusting the prescaler value

 

uint32_t currSeconds = RTC->TSR;

PRINTF("\r\n TIME NOW = %d", rtc_get_seconds());
RTC_StartTimer(RTC);
}

Because you use 1KHz instead of 32.768KHz, so the RTC->TPR should be 1000 instead of 32768 so that it can genertae 1Hz clock.

Hope it can help you

BR

Xiangjun Rong

0 Kudos

1,034 Views
gauravbanyal
Contributor IV

Hello Xiangjun,

Could you kindly comment on my previous response please?

 

Kind regards,

Gaurav.

0 Kudos

1,034 Views
xiangjun_rong
NXP TechSupport
NXP TechSupport

Hi, Gaurav,

I am sorry for the delay. Pls refer to the ticket in community:

https://community.nxp.com/message/343370 

If you use LPO, you can only get the second interrupt or RTC_CLK with 32s instead of 1s, because the RTC_TPR register is read-only. I have tried various method to change the register, but fail.

BR

Xiangjun Rong

1,034 Views
gauravbanyal
Contributor IV

Hello Xiangjun,

I believe that the value of the pre-scaler that you are proposing is not correct. The pre-scaler seems to be a count-up rather than a count down. From the Reference Manual: "The TSR[TSR] increments when bit 14 of the TPR transitions from a logic one to a logic zero." Bit 14 will transition from 1 to zero (making bit 15 go from zero to 1) when the count on the pre-scaler increments from decimal 32767 to 32768.

When you are using a 32.768kHz clock source for the RTC, the pre-scaler should be zero so that the TSR is incrementer after exactly 32768 clock cycles =>1second

When you are using the LPO clock source, you have a 1kHz clock. So, to get exactly 1 seconds increment on the TSR, you need to set the TPR to (32768-1000) so that it increments to 32768 in exactly 1 second (1000 output clock cycles of LPO). 

Hope this clarifies. Please feedback if I have mis-interpreted something from the reference manual.

And what about using the RTC clocked with LPO in VLLS3 state and transitions between VLLS3 and RUN?

Kind regards,

Gaurav.

0 Kudos

1,034 Views
gauravbanyal
Contributor IV

Hello Xiangjun,

Could you kindly comment on my previous response please?

Kind regards,

Gaurav.

0 Kudos