Hello Friends,
I have an unusual problem :smileyhappy:
I have designed 3 boards (variants of he same app) using the KL04 chip. Now the code used is this -
#include "derivative.h" /* include peripheral declarations */
//void UART0_IRQHandler(void);
// extern void LPTimer_IRQ(void);
// void LPTimer_IRQ(void);
int main(void)
{
int count;
uint16_t countTemp = 0;
// asm(" CPSIE i");
MCG_C1 |= 0x02;
SIM_SOPT1 = 0;
SIM_SCGC5 |= (SIM_SCGC5_LPTMR_MASK | SIM_SCGC5_PORTB_MASK);//PORTC_PCR1 |= PORT_PCR_MUX(1);//select RTC_CLKIN function
PORTB_PCR3 |= 0x00000100; // alternative pin mux selection //
GPIOB_PDDR |= 0x00000008; // pin direction //
PORTB_PCR4 |= 0x00000100; // alternative pin mux selection //
GPIOB_PDDR |= 0x00000010; // pin direction //
NVIC_ICPR = 1 << (28);
NVIC_ISER = 1 << (28);
SIM_SCGC5|=SIM_SCGC5_LPTMR_MASK;
/* Reset LPTMR settings */
SIM_SOPT1 = 0;
SIM_SCGC5 |= (SIM_SCGC5_LPTMR_MASK | SIM_SCGC5_PORTB_MASK);//PORTC_PCR1 |= PORT_PCR_MUX(1);//select RTC_CLKIN function
LPTMR0_CSR=0;
LPTMR0_CSR |= 0x00000040;
LPTMR0_CMR = 600;
GPIOB_PTOR |= (1 << 3);
GPIOB_PTOR |= (1 << 4);
GPIOB_PTOR |= (1 << 3);
GPIOB_PTOR |= (1 << 4);
/* Start the timer */
LPTMR0_CSR |= LPTMR_CSR_TEN_MASK;
/* Wait for counter to reach compare value */
asm("nop");
while(1){
asm("nop");
asm("nop");
asm("nop");
asm("nop");
// if((LPTMR0_CSR & 0x00000080) == 0x00000080){
// LPTMR0_CSR &= ~LPTMR_CSR_TEN_MASK;
// LPTMR0_CSR |= 0x00000080;
// LPTMR0_CSR |= LPTMR_CSR_TEN_MASK;
// GPIOB_PTOR |= (1 << 3);
// GPIOB_PTOR |= (1 << 4);
// }
}
}
void LPTimer_IRQHandler(void){
asm("nop");
asm("nop");
asm("nop");
asm("nop");
if((LPTMR0_CSR & 0x00000080) == 0x00000080){
LPTMR0_CSR &= ~LPTMR_CSR_TEN_MASK;
LPTMR0_CSR |= 0x00000080;
LPTMR0_CSR |= LPTMR_CSR_TEN_MASK;
GPIOB_PTOR |= (1 << 3);
GPIOB_PTOR |= (1 << 4);
}
}
Ideally the controller should be interrupted and the control should go to the ISR (void LPTimer_IRQHandler(void)). What I see here is that in the 1st and the 2nd version of the board the ISR works well. But in the 3rd it doesnt. So, to test the 3rd board I made a pin toggle which is executed fine making me feel that this is a purely software issue.
I am a bit confused as to how this is being done.
I am stating the part number in all the 3 boards -
1) - M04R4V
4EYIDK
2) M04R5V
4EYIDM
3) M04R5V
ASAUK
Can anyone help me pls
Hi, Vinod,
Regarding your question, I have not the FRDM-KL04 board on hand, but I have FRDM-KL02Z board, I have tested the code on my FRDM-KL02Z board, I found out that the LPTMR0_PSR[PCS]=11 in binary for unknown reason in default state, in the case, it selects the OSCERCLK as clock source, in the case, you should set the OSC32KSEL in SIM_SOPT1 as 10 in binary, in the setting, the CLK source for LPTMR will be from external RTC_CLKIN as Mr Yasuhiko pointed out, pls refer to section 3.8.3.3 LPTMR prescaler/glitch filter clocking options in RM of KL04. But if you use RTC_CLKIN pin, you have to enable PORTA clock in SIM_SCGC5 and set the PORTB_PCR4 = 0x00000100; and set the SIM_SOPT1 =0x80000;
I have developed the code based on the FRDM-KL02 board, with which I can enter the LPTimer_IRQHandler(void) with 1KHz LPO clock source, you can copy the code directly for KL04.
I attach the code, hope it can help you.
Hello Xiangjun,
I understand ur point. But then how come it works in the other board but not i the new one.
Will nmi_b pin cause any mischief.
Vinod
Hello Friends,
Thank you very much indeed for your responses.
I sincerely appreciate it.
SO with my code what I am doing is I am making the clock for the LPTMR come from RTC_CLKIN. And since I dont have such a clock its not working. So its all software.
But then how can it be explained that the LPTMR works for my older boards which also doesnt have the RTC_CLKIN. The code is completely the same.
Vinod.
Hi, Vinod,
as you know, multiple clock source can drive the LPTMR, pls check the LPTMR0_PSR[PCS] to determine the clock source the LPTMR uses for your old code, I guess the LPTMR uses LPO, whatever.
Pls refer to the section I copied from UM of KL04.
BR
XiangJun Rong
Hi vinod,
I guess your board has neither EXTAL0 nor XTAL0 oscillator. In the case, the setting SIM_SOPT1 0 would not select any clock for LPTMR. How about 0x000c0000 (i.e. LPO) as SIM_SOPT1 value? Also you should better make the first LPTMR0_CSR setting not 0 but 0x80 to clear the interrupt.
Best regards,
Yasuhiko Koumoto.