LPTMR0 activation on FRDM-KL03Z board

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

LPTMR0 activation on FRDM-KL03Z board

1,982 Views
beaurogers
Contributor II

Hello, I have been trying for a very long time to try and get the Low Power Timer going on the FRDM-KL03Z board and have had no luck. I have been trying to find where the 32k crystal is being output to but I can't see the signal nor can I measure continuity to the pin. As EXTAL is not on the developement board, I would assume it would output to the RTC_CLK_IN pin (PTA5) My code is seen below:

////////////////////Port Set up/////////////////////////////
PORT_BWR_PCR_MUX(PORTA, 5, 0b001); //Set the Alt1 muxing on Port A, pin 5 for RTC_CLK_IN (SiT1532_OUT) (32 kHz clock)
GPIO_SET_PDDR(GPIOA, 0U<<5); //Set PORTA pin 5 to input

///////////////////////////////////////////////LPTMR initialization///////////////////////////////////////////////////

SIM_BWR_SCGC5_LPTMR(SIM_BASE_PTR, 1); //enabling software access to low power timer module
SIM_BWR_SOPT1_OSC32KSEL(SIM_BASE_PTR,0b00); //Selects the clock source (ERCLK32K) for LPTMR (00), RTC_CLKIN (10)
SIM_BWR_SOPT1_OSC32KOUT(SIM_BASE_PTR,0b01); // (01)Set the clock ERCLK32K output on pin13
SIM_BWR_SOPT2_CLKOUTSEL(SIM_BASE_PTR,0b110); // Clock to output on the CLKOUT pin (110) OCERCLK

MCG_BWR_C2_EREFS0(MCG, 1); //Selects the source for the external reference clock to be external clock.
MCG_BWR_C2_IRCS(MCG,1); //Selects LIRC to 8Mhz
OSC_BWR_CR_ERCLKEN(OSC, 1); //Enables the external reference clock (OSCERCLK)

MCG_BWR_C1_CLKS(MCG, 0b10); //Selects external clock as the main clock source. This is EXT mode

//Set-up the LPTMR
//Set the LPTMR PSR Register
LPTMR_BWR_CSR_TEN(LPTMR0, 0); //LPTMR is disabled
LPTMR_BWR_PSR_PCS(LPTMR0, 0b10); //Sets LPTMR clock to MCGIRCLK (00), LPO Timer (01), ERCLK32K (10)
LPTMR_BWR_PSR_PBYP(LPTMR0, 0); //(1) LPTMR prescale is disabled
LPTMR_BWR_PSR_PRESCALE(LPTMR0, 0b0000); //Sets the prescale to divide by 2

// Set the LPTMR Working Mode for Time Counter
LPTMR_BWR_CSR_TMS(LPTMR0, 0); // Sets the mode to Time counter (0) or Pulse counter (1)
LPTMR_BWR_CSR_TFC(LPTMR0, 1); // Set the CNR to reset on overflow (1), reset when TCF is set (0)

freq=4096;
tick_count=49151; //3s with freq=32768, ps=2

LPTMR_BWR_CMR_COMPARE(LPTMR0, tick_count); //Sets the compare value that will trigger TCF to be set when CNR matches CMR

//Initialize the LPTMR interrupt
LPTMR_BWR_CSR_TIE(LPTMR0, 1); //Enable the timer interrupt (1)

//////////////////////////////////////////////////////Interrupt Routine////////////////////////////////////////////////////////

void LPTMR0_IRQHandler(void)
{
LPTMR_BWR_CSR_TCF(LPTMR0, 1); //Clear the interrupt flag
GPIO_WR_PTOR(GPIOB, 1U<<2); //Toggle pin2 on PortB to turn on an LED RED

}

Any help would be much appreciated, thank you.

-Beau

Tags (2)
0 Kudos
Reply
7 Replies

1,491 Views
mjbcswitzerland
Specialist V

Beau

I tried to follow what you are doing but am not really sure that I understand what is needed.

For example, the FRDM-KL03Z has a 32kHz crystal on EXTAL which can be used to clock the RTC and the LPTMR so I don't understand why you write that EXTAL (pin 5) is not on the development board (?)

Looking at the code I think that you are selecting the RTC_CLKIN as clock for RTC and LPTMR use but do you actually have a separate clock on this input?

I see that you choose OSCERCLK to be output on CLKOUT but you haven't configured PTA12 to the CLKOUT function, which means that it won't be output.

What clock are you using to clock the processor itself?

Although it probably won't help at the moment I have attached a binary file for the FRDM-KL03Z which runs the core from the HIRC and the LPTMR from the 32kHz OSCERCLK - and drives the clock to the PTA12 (CLKOUT) output. It toggles the red LED at about 3ms rate from the LPTMR interrupt (and the green LED at 2.5Hz).

I have made a very quick and dirty video showing the code being stepped in the uTasker simulator in case you do see anything relevant in the code snippets involved: https://youtu.be/qRzbwW4-QQg

Otherwise, specify the exact details so that I can repeat for the exact setup that you need and then you will be able to see any deviation between the operating code and yours.
[1: What clock source, core speed, bus clock speed and flash clock speed are needed]
[2: What LPTMR source]

Regards

Mark

0 Kudos
Reply

1,491 Views
beaurogers
Contributor II

Apologies for the poor description as my question was written in the midst of frustration. My end goal is to use the 32 kHz crystal on the FRDM-KL0Z3 to toggle the LPTMR and utilize the interrupt routine. I seem to have been mixing up OSC32KCLK with CLKOUT, but this image below is essentially what I am trying to achieve.

pastedImage_2.png

I have been trying to get the CLKOUT function executed primarily for trouble shooting purposes but I still am not seeing a signal. There is no mention on EXTAL or any description of how to setup requirements in the FRDM-KLZ3 manual so I was not too sure where or if I was getting a signal as I could not measure it on an oscilloscope. 

So basically I am trying to use the 32kHz OSC as the source for my LPTMR.

0 Kudos
Reply

1,491 Views
Robin_Shen
NXP TechSupport
NXP TechSupport

Hi Beau,

There is a 32.768kHz crystal on FRDM-KL03Z. But the RTC_CLKIN is floating.
I think you can select OSCERCLK as the clock source of LPTMR:

LPTMR_BWR_PSR_PCS(LPTMR0, 0b11); //Sets LPTMR clock to MCGIRCLK (00), LPO Timer (01), ERCLK32K (10),OSCERCLK (11)

Figure 5-1. Clocking diagram.png

5.7.5 LPTMR clocking.png

The PTA12 can be configured as CLKOUT function and then output OSCERCLK clock.

PTA12 CLKOUT.png

SIM_SOPT2[CLKOUTSEL].png

Best Regards,

Robin

 

-----------------------------------------------------------------------------------------------------------------------
Note: If this post answers your question, please click the Correct Answer button. Thank you!
-----------------------------------------------------------------------------------------------------------------------

0 Kudos
Reply

1,491 Views
beaurogers
Contributor II

Robin,

I did do that as well and I configured the CLKOUT pin to output the OSC but I consistently saw nothing. I measured the 32 kHz crystal directly and could see a voltage but no signal so that indicates to me there was no signal coming from it. I don't know if there is something else that needs to be done to activate the crystal but I did not see any kind of signal. However, I very quickly saw results when I inputted a 32 678 Hz input from a signal generator. 

Regards,

Beau

0 Kudos
Reply

1,491 Views
mjbcswitzerland
Specialist V

Beau

If you are still having problem with using the 32kHz crystal (and that is what you finally need), check that you have enabled it in the OSC_CR register.

These are the only two commands needed to start it and switch it through to OSCERCLK:

    MCG_C2 = (MCG_C2_EREFS | MCG_C2_IRCS);   // select oscillator as external clock source
    OSC0_CR = (OSC_CR_ERCLKEN | OSC_CR_EREFSTEN); // enable the oscillator and allow it to continue oscillating in stop mode

Regards

Mark

0 Kudos
Reply

1,491 Views
mjbcswitzerland
Specialist V

Hi Beau

Read this for some clocking details: http://www.utasker.com/kinetis/KL_RTC.html

In fact you are configuring the way that I showed in the video, therefore you should be able to check the code used to see what is missing in yours.
I suspect a couple of details that you haven't done:
1. Enable the external reference clock in OSC_CR
2. Configured the pin Mux for PTA12 for CLKOUT operation

Regards

Mark

0 Kudos
Reply

1,491 Views
beaurogers
Contributor II

Thanks Mark, you are in fact correct but it turns out that my oscillator clock was simply not working. After configuring the LPTMR to RTC_CLKIN and running a function generator at 32 768 Hz, I began seeing my output. Thank you for the help.

-Beau

0 Kudos
Reply