Reading Counter value in LPTMR?

cancel
Showing results for 
Search instead for 
Did you mean: 

Reading Counter value in LPTMR?

869 Views
drsmith_yorku
Contributor III

Hi,

I'm trying to simply read (polling) the timer counter value in the LPTMR on the FRDM KL43Z board.  Thought that I'd try that before trying interrupts or anything fancy.  Having read through the documentation I'm aware that I have to enable the clock source to the LPTMR and I read that I have to write a bogus value to the CNR register prior to reading it.  However, all I seem to get is a 0 value from CNR when I step through the code in debug mode.

I'm using MCUXpresso 10.1 and trying to use a CMSIS-style approach to accessing the registers.  Monitoring the register values as I step through the code, the registers appear to be changing as they are supposed to.

Below is the code that I'm using.  Any hints as to what I might be doing wrong?

thanks!

James

int main(void) {


/* Init board hardware. */
BOARD_InitBootPins();
BOARD_InitBootClocks();
BOARD_InitBootPeripherals();
/* Init FSL debug console. */
BOARD_InitDebugConsole();

/* Set up low power timer */

// System Register 5 has the Low Power Timer in it.
// Enable clock on the lower power timer.
// Bit 0 is the LPTMR clock gate enable bit. 1 for enable.
SIM->SCGC5 |= SIM_SCGC5_LPTMR_MASK; /* enable clock: SIM_SCGC5: LPTMR=1 */

// Disable the Low Power Timer
LPTMR0->CSR &= ~LPTMR_CSR_TEN_MASK; // invert and AND.

// CNR overflow reset, Time counter; don't enable yet.
LPTMR0->CSR = 0b0000000011001100;

// Prescale: use 8MHz clock; full prescale enabled.
LPTMR0->PSR = 0b0000000001111000;

// Last Step... enable timer
LPTMR0->CSR |= LPTMR_CSR_TEN_MASK; // Enable the timer via a mask.


printf("Hello World\n");

/* Force the counter to be placed into memory. */
volatile static int i = 0 ;
/* Enter an infinite loop, just incrementing a counter. */
while(1) {
// write to the CNR to tell the counter that you want to read it.
LPTMR0->CNR = 1u;
// now read the CNR
i=LPTMR0->CNR;

printf("current counter %d\r\n",i);
}
return 0 ;
}

Labels (1)
0 Kudos
1 Reply

284 Views
kerryzhou
NXP TechSupport
NXP TechSupport

Hi James Smith,

  I have tested the LPTMR CNR register read in our SDK2.2-FRDM-KL43 LPTMR project, it works OK on my side.

This is the test result:

pastedImage_1.png

You can find, after I add this code:

    uint32_t currentCNR = 0U;

        LPTMR0->CNR = 0X11;
        currentCNR = LPTMR0->CNR;
        PRINTF("LPTMR CNR= %X \r\n", currentCNR);

I can find the CNR register is increase in the serial tool.

You also can use the official sample code to test it on your FRDM-KL43.

SDK can be downloaded form this link:

Welcome | MCUXpresso SDK Builder 

Please use the LPTMR project, folder:SDK_2.2_FRDM-KL43Z\boards\frdmkl43z\driver_examples\lptmr

Wish it helps you!


Have a great day,
Kerry

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

0 Kudos