10 years, 11 months ago.

How do I access the KL25Z clock registers?

Can anyone help with Mbed code to route the 32KHz internal reference oscillator to the RTC Clock Input. I have found some information on the Freescale web site where someone has done this using Code Warrior.

https://community.freescale.com/message/308994#308994

The idea is to route the 32KHz ref osc out on pin PTC3 and connect this to pin PTC1 as below:

•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".

..........Further to this someone has suggested this:

•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

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

1.define CLOCK_PERIOD (0.000030517578125)

  1. define CLOCK_PERIOD (0.01) <- 100Hz

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 }

..............I do not know how to access the resisters for the KL25Z. I have written similar code to access the spare RTC registers using this I found from another Mbed member and it works.

LPC_RTC->GPREG0 = tz; (to store the variable tz)

tz=LPC_RTC->GPREG0; (to read back to the variable tz)

...............So I'm hopping to be able to control the clock generator registers in a similar way to give me a 32kHz without using an external source.

Any suggestions would be helpful.

Thank you

2 Answers

9 years, 10 months ago.

Hello,

Be aware that if you don´t enable clock to PORTC before calling set_CLKOUT32k() the code will compile but no run further that the instruccion trying to acces PORTC.

The cause of this is that not all of the peripherals are powered up during the startup of the mbed device. PORTC is one of these unpowered devices. So for the freescale code to go on you must power Portc first. Sometimes we make it without knowing just by adding a

DigitalIn input(PORTC0);

because we are using this port for something else....

We can make this happen by enabling clock to PORTC before running the freescale code.

SIM->SCGC5|=SIM_SCGC5_PORTC_MASK;

Or put it in the code before attemping to write PORTC_PCR. I have changed Alfredo Espinoza´s function to enable this (just in case we are not using the PORTC for something else).

Ouput

void set_CLKOUT32k(void){   
    MCG->C1 |= MCG_C1_IRCLKEN_MASK; // Enable the internal reference clock. MCGIRCLK is active.
    MCG->C2 &= ~(MCG_C2_IRCS_MASK); // Select the slow internal reference clock source.
    //SIM->SOPT1 |= SIM_SOPT1_OSC32KSEL(0x2); // Select 32 KHz clock source as RTC_CLKIN
    SIM->SCGC5|=SIM_SCGC5_PORTC_MASK;       //Enable Clock to Port C 
    SIM->SOPT2 |= SIM_SOPT2_CLKOUTSEL(0x4); // Set PTC3 as CLKOUT pin for MCGIRCLK
    PORTC->PCR[3] |= (PORT_PCR_MUX(0x5)); // Select the MCGIRCLK clock to output on the CLKOUT pin.
}

10 years, 2 months ago.

Hello Paul, In regards to your question I am attempting to do the same thing for a data logger run only at 3.3V. Apparently there is some work already done on this front. Take a look at:

RTC Library working in KL25z

I will test and report back to see if it works.

Regards

Confirmed working. The code from Pete Eisenhuth works. I added it ad the begging of the Time test programs and works perfectly. Just add the wire between PTC1 and PTC3. The code for getting the internal reference out into the work is:

Ouput 32KHz clock function

void set_CLKOUT32k(void){   
    MCG->C1 |= MCG_C1_IRCLKEN_MASK; // Enable the internal reference clock. MCGIRCLK is active.
    MCG->C2 &= ~(MCG_C2_IRCS_MASK); // Select the slow internal reference clock source.
    //SIM->SOPT1 |= SIM_SOPT1_OSC32KSEL(0x2); // Select 32 KHz clock source as RTC_CLKIN
    SIM->SOPT2 |= SIM_SOPT2_CLKOUTSEL(0x4); // Set PTC3 as CLKOUT pin for MCGIRCLK
    PORTC->PCR[3] |= (PORT_PCR_MUX(0x5)); // Select the MCGIRCLK clock to output on the CLKOUT pin.
}
posted by Alfredo Espinoza Rhoton 18 Jan 2014

Hi Alfredo, I'll give this a try. Problem is the RTC accuracy will be lost in sleep modes due to the external crystal not being used. I assume the RTC will run in deep sleep mode, have you tried? I have been using an external RTC clock chip, deep sleep runs at 4uA including RTC chip and a low Iq 3.3v regulator. Paul

posted by Paul Staron 19 Jan 2014

From what I've read the internal clock stability in the KL25z is around +-10%. I haven't tried running it in deep sleep mode to see if it continues working. I tried it using the mbed RTOS, which should put the whole microcontroller to sleep and it still works. The documentation says that if there is no thread in the ready state it puts it in a sleep mode. I'll measure the current consumption of the board to see if it really is powering down. I'll also try using this library by Erik Olieman:

http://mbed.org/users/Sissors/code/WakeUp/

It says that it's compatible with the KL25z board, though I haven't measured the current consuption with it yet.

posted by Alfredo Espinoza Rhoton 20 Jan 2014

Erik's deep sleep / wake works as it should on the KL25z, KL46z and KL05z <4uA in deep sleep, but the problem will be RTC accuracy if not using a crystal clock source. That's okay if accuracy is not an issue, even though Erik's library does have the 'calibrate' function, long tern accuracy will be poor using IRC for the reference clock due to temperature drift. Also a hardware reset will effect the accuracy. That's why I use an external crystal time base RTC clock chip for portable data logging. However saying that the KLxx series MCU's do need to have an RTC function that generates its own clocking without external input. So still very important work, no point in an RTC unless it can work in a stand alone configuration. I'm busy with the KL46z sLCD driver for the moment but will get back to the RTC issue in the next few days we could add an RTC clock function added to the Mbed library that can incorporate this.

You may want to take a look at the KL05Z, now added to the Platforms, here is a link to what I have found, the RTC is very much usable.

http://mbed.org/forum/platform-40-FRDM-KL05Z-community/topic/4750/?page=1#comment-23586

posted by Paul Staron 20 Jan 2014