Hi again,
So I dove in & tried to adapt KL28Z SDK lptmr project & changed it from being LPTMR timer counter mode to be in pulse counter mode taking input pulses from PTC5 setup as LPTMR0_ALT2 input function.
I ran this modified code but the CNR register count is always 0 even when I turn on the flowmeter simulator gadget that is attached to PTC5 which is generating square wave pulses in different duty cycles which should trigger pulse counts.
Original code sets up an lptmr timer to trigger interrupt every second & display a declared timer variable; NOT from lptmr CNR counter register.
When you have a chance, kindly review this code snippets & see what I'm missing or where I am wrong.
NOTE: I am using my custom prototype board with the KL28Z as its MCU.
I setup a millisecond TPM timer such that every 30 seconds (half a minute), the system will dump a string on debug console that displays what is read from the CNR (lptmr count register), using function call:
fsl_lptmr.c/LPTMR_GetCurrentTimerCount(LPTMR0); // correctly writes 0 to CNR first before reading...
OUTPUT: (even after I turn on the flowmeter pulse generator, counter remains zero.)
Low Power Timer Pulse Counter 7/22/19
Pulse Count after half a minute = 0
Pulse Count after half a minute = 0
Pulse Count after half a minute = 0
Pulse Count after half a minute = 0
Pulse Count after half a minute = 0
Pulse Count after half a minute = 0
Pulse Count after half a minute = 0
Pulse Count after half a minute = 0
...
*************************************************
CODE:
*****pinmux.c/BOARD_InitPins();
void BOARD_InitPins(void)
{
/* Clock Gate Control: 0x01u */
CLOCK_EnableClock(kCLOCK_PortA);
/* Clock Gate Control: 0x01u */
CLOCK_EnableClock(kCLOCK_PortC);
/* Clock Gate Control: 0x01u */
CLOCK_EnableClock(kCLOCK_PortE);
// v3 5/30/19 mi ELMNT debug console
// PORTA1 (pin 35) is configured as LPUART0_RX
PORT_SetPinMux(PORTA, 1U, kPORT_MuxAlt2);
// PORTA2 (pin 36) is configured as LPUART0_TX
PORT_SetPinMux(PORTA, 2U, kPORT_MuxAlt2);
// PORTC5 (pin 77) is configured as PTC5
PORT_SetPinMux(PORTC, 5U, kPORT_MuxAlt3); // v1 7/22/19 mi LPTMR_ALT2 pulse counter input
// PORTE26 (pin 33) is configured as PTE26
PORT_SetPinMux(PORTE, 26U, kPORT_MuxAsGpio); // v1 7/22/19 mi Filter RGB Green to blink off/on
}
****** lptmr.c - NOTE: I COMMENTED OUT SetTimer...() because the pulse counter flowmeter external input should be triggering timer count in this code modification. Am I right to comment out those lines?
main()
{
. . .
LPTMR_GetDefaultConfig(&lptmrConfig);
/* Initialize the LPTMR */
LPTMR_Init(DEMO_LPTMR_BASE, &lptmrConfig);
/*
* Set timer period.
* Note : the parameter "ticks" of LPTMR_SetTimerPeriod should be equal or greater than 1.
*/
// LPTMR_SetTimerPeriod(DEMO_LPTMR_BASE, USEC_TO_COUNT(LPTMR_USEC_COUNT, LPTMR_SOURCE_CLOCK));
/* Enable timer interrupt */
// LPTMR_EnableInterrupts(DEMO_LPTMR_BASE, kLPTMR_TimerInterruptEnable);
/* Enable at the NVIC */
EnableIRQ(DEMO_LPTMR_IRQn); // should I enable this code to execute? or comment it out too?
PRINTF("Low Power Timer Pulse Counter 7/22/19\r\n");
. . .
}
The 3rd code snippet at the end is the lptmr initialization configuration. Did I do it right? And note: I'm bypassing both prescaler & also glitch filter in this pulse counter mode.
***** fsl_lptmr.c IS SETUP HERE CORRECT ???
void LPTMR_GetDefaultConfig(lptmr_config_t *config)
{
assert(config);
/* Initializes the configure structure to zero. */
memset(config, 0, sizeof(*config));
// v1 7/22/19 mi modified to behave in pulse counter mode
/* Use time counter mode */
config->timerMode = kLPTMR_TimerModePulseCounter; // set as pulse counter
/* Use input 0 as source in pulse counter mode */
config->pinSelect = kLPTMR_PinSelectInput_2; // input 2, LPTMR0_ALT2
/* Pulse input pin polarity is active-high */
config->pinPolarity = kLPTMR_PinPolarityActiveHigh;
/* Counter CNR resets on overflow */
config->enableFreeRunning = true; // reset on CNR overflow? instead of when CNR = CMR
/* Bypass the prescaler */
config->bypassPrescaler = true; // yes, bypass prescaler
/* LPTMR clock source */
config->prescalerClockSource = kLPTMR_PrescalerClock_2; // LPTMR0_ALT2? or don't care
/* Divide the prescaler clock by 2 */
config->value = kLPTMR_Prescale_Glitch_0; // don't care?
}
Any questions or what other code you want me to post here for clarity?
Thanks for the help.
MI