RTC time base

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

RTC time base

3,782 Views
antoineodonne
Contributor II

Hello,

I would like to use the KL25 Freedom board and running the RTC.

But the clock sources for the RTC are RTC_CLKIN, or 32KHz crystal which need external component, or the LPO 1KHz signal.

Is there a way to get 1 second time base with the LPO? I don't know how to configure the RTC to manage this not 32KHz frequency.

Thank you for your help.

Antoine

Tags (1)
10 Replies

1,998 Views
marcinmarecki
Contributor II

Hi Antoine,

I met the same issue.

The clock source should be selected by OSC32KSEL(19-18 bits) of SIM_SOPT1 (page 194 of KL25 Sub-Family Reference Manual). But it doesn't work. For each set of multiplekser the RTC_CLKIN source is selected.

Let me know if someone has better observation.

Regards,

Marcin

my code compiled by mbed.org:

    printf("\rSOPT1:0x%.8x:0x%.8x\r",SIM,SIM->SOPT1);

    SIM->SOPT1 &= ~SIM_SOPT1_OSC32KSEL_MASK;

    SIM->SOPT1 |= SIM_SOPT1_OSC32KSEL(3); //LPO 1kHz

    printf("\rSOPT1:0x%.8x:0x%.8x\r",&(SIM->SOPT1),SIM->SOPT1);

    printf("SIM_SOPT1_OSC32KSEL_MASK:0x%.8x\r",SIM_SOPT1_OSC32KSEL_MASK);

    printf("SIM_SOPT1_OSC32KSEL(3):0x%.8x\r",SIM_SOPT1_OSC32KSEL(3));

    printf("SIM_SOPT1_OSC32KSEL(2):0x%.8x\r",SIM_SOPT1_OSC32KSEL(2));

as work around I drived the RTC_CLKIN by external source or PWM.

0 Kudos
Reply

1,998 Views
heliosfa
Contributor II

I know this thread is a couple of months old, but I have found a way to get the RTC working properly without needing to swap crystals, etc.  By default, there is no link between the internal 32K reference and the RTC clock input HOWEVER it is possible to route the internal 32K out to PTC3 and bring it back in on the RTC clock input on PTC1.

You can configure the required settings in Codewarrior/Processor Expert on the CPU component (in expert mode) as follows:

  • set RTC Clock Input to "Enabled".  This sets PTC1 as an input for a clock source.
  • set MCGIRCLK source to "slow" in "Clock Source Settings" > "Clock Source Setting 0" > "Internal Reference Clock"
  • set ERCLK32K Clock Source to "RTC Clock Input" in "Clock Source Settings" > "Clock Source Setting 0" > "External Reference Clock"
  • Enable "CLKOUT pin control" in "Internal Peripherals" > "System Integration Module"
    • set "CLKOUT pin" to "PTC3/LLWU_P7/..."
    • Set "CLKOUT pin output" to "MCGIRCLK"
    • This routes the internal 32K reference oscillator out to pin PTC3

On the RTC, set the "Clock Source" to "ERCLK32K".

And don't forget to put a wire link between PTC1 and PTC3 :smileyhappy:

Low and behold, you have an internally-sourced 32K signal to run the RTC on the Freedom board without needing any external devices or needing to change any crystals.


1,998 Views
marcinmarecki
Contributor II

Perfect. It works.

Thanks,

Marcin

0 Kudos
Reply

1,998 Views
mickkelo
Contributor II
  • set RTC Clock Input to "Enabled".  This sets PTC1 as an input for a clock source.
  • set MCGIRCLK source to "slow" in "Clock Source Settings" > "Clock Source Setting 0" > "Internal Reference Clock"
  • set ERCLK32K Clock Source to "RTC Clock Input" in "Clock Source Settings" > "Clock Source Setting 0" > "External Reference Clock"
  • Enable "CLKOUT pin control" in "Internal Peripherals" > "System Integration Module"
    • set "CLKOUT pin" to "PTC3/LLWU_P7/..."
    • Set "CLKOUT pin output" to "MCGIRCLK"
    • This routes the internal 32K reference oscillator out to pin PTC3

On the RTC, set the "Clock Source" to "ERCLK32K".

And don't forget to put a wire link between PTC1 and PTC3

I've done everything that heliosfa said and still doesn't work. If I write RTC1_Init(UserDataPtrr, FALSE)  the date restarts every time is reconnected to the USB, and if I write RTC1_Init(UserDataPtrr, TRUE) the date doesn't change.


0 Kudos
Reply

1,998 Views
marcinmarecki
Contributor II

Here is my workaround:

It required connection PTD1->PTC1

Example:

/*

* Clock Generator generates wave on PTD1,

* the frequency is set up by CLOCK_PERIOD constant,

* the wave is symmetric 50/50.

*/

void clockGenerator(void)

{

    //0.000030517578125 = 1/32768

    //#define CLOCK_PERIOD (1/(2^15))

    #define CLOCK_PERIOD (0.000030517578125)

    //#define CLOCK_PERIOD (0.01) <- 100Hz :smileyhappy:

    PwmOut out(PTD1); /* PTD1 is the same address as LED1 */

    out.period(CLOCK_PERIOD);          // out requires a 2ms period

    out.pulsewidth((CLOCK_PERIOD/2)); // out position determined by a pulsewidth between 1ms

}

0 Kudos
Reply

1,998 Views
mickkelo
Contributor II

So, I need the RTC component and a PWM_LDD, not?

When I add the PWM_LDD component, how should I configure it? I've some errors.

http://i.imgur.com/l0iJN47.png

And, once included the PWM_LDD I have to add a TimerUnit_LDD:

http://i.imgur.com/AjDql17.png

I assume that all of this is whether the steps taken by Graeme Bragg.

0 Kudos
Reply

1,998 Views
mickkelo
Contributor II

Marcin Marecki escribió:

Here is my workaround:

It required connection PTD1->PTC1

Example:

/*

* Clock Generator generates wave on PTD1,

* the frequency is set up by CLOCK_PERIOD constant,

* the wave is symmetric 50/50.

*/

void clockGenerator(void)

{

    //0.000030517578125 = 1/32768

    //#define CLOCK_PERIOD (1/(2^15))

    #define CLOCK_PERIOD (0.000030517578125)

    //#define CLOCK_PERIOD (0.01) <- 100Hz :smileyhappy:

    PwmOut out(PTD1); /* PTD1 is the same address as LED1 */

    out.period(CLOCK_PERIOD);          // out requires a 2ms period

    out.pulsewidth((CLOCK_PERIOD/2)); // out position determined by a pulsewidth between 1ms

}

But, How can I use that with Codewarrior?

0 Kudos
Reply

1,998 Views
marcinmarecki
Contributor II

I'm using MQXLite.

From CodeWarrior window: Component Library, add PWM_LDD component (logic driver).

It is named PWM1 (in my project).

I "Component Inspector" select "Expert" page.

Output pin: select from list PTC1,

Period: 10ms (for 100Hz generator)

Starting pulse width: 5ms,

Auto initialisation: yes.

It starts in my project. I checked it by frequency meter.

Let me know if you have some another questions and if it works in your project,

Marcin

0 Kudos
Reply

1,998 Views
LucioCanche
Contributor II

Hi Graeme B.

I did as you said, but it doesn't run. The time shown doesn't change. I think that the reason could be inside the code:

...

...

...

   LDD_TDeviceData *MyRTCPtr;;

   LDD_RTC_TTime TimePtr;

   LDD_TUserData *UserDataPtrr;

   //bool SoftInit;

...

...

...

int main(void)

{

...

...

...

  /* Write your code here */

  //SoftInit=FALSE;

  MyRTCPtr=RTC1_Init((LDD_TUserData *)NULL, FALSE); //   MyRTCPtr=RTC1_Init(UserDataPtrr, FALSE);

  byte secc;

  byte minn;

  byte horr;

  for(;;) {

  

   RTC1_GetTime(MyRTCPtr,&TimePtr);

   secc=TimePtr.Second;

   minn=TimePtr.Minute;

   horr=TimePtr.Hour;  

          

//Code for showing the Time (Printf, UART, LCD, etc)

...

...

...


    //WAIT1_Waitms(100);

  }

...

...

...

}

Is there something wrong?

0 Kudos
Reply

1,998 Views
adriansc
Contributor IV

Hi,

You can check the example driver for RTC configuration: FRDM-KL25Z Product Summary Page

Download the FRDM-KL25Z_QSP: FRDM-KL25Z Quick start package and check drivers folder.

Hope this helps.

0 Kudos
Reply