Ladies and Gentlemen,
I am trying to set up CTIMER0 for a periodic interrupt. Unfortunately it doesn't work and I'm getting confused.
My questions are:
1- In the Reference Manual -> SYSCON capital, page 223, AHBCLKCTRL1 register has bit26, called TIMER0. Is this bit the clock enable for CTIMER0? Is TIMER0 and CTIMER0 the same thing?
2- when I turn AHBCLKCTRLSET[1], bit26 to 1, I immediately loose the connection between MCUXpresso and MCULINK PRO. That means, I cannot debug anymore. I have to close the debug perspective and open it again.
3- There's another enable bit: SYSCON->CTIMERGLOBALSTARTEN, bit0. Here I can also enable CTIMER0 CLK. Are both enables necessary?
4- In case I don't turn AHBCLKCTRLSET[1], 26 bit to 1, I can debug the chip, but all the instructions to set in the CTIMER0 section, they have no effect at all. I single step through them, but there's no change in the registers.
5- I also can't set to 0 the CTIMERCLKDIV[0] register - I'd like to enable the clock divider
6- CTIMERCLKSEL[0] also cannot select FRO1M, if I try to write this register 0x04, there's no effect.
Since CTIMER0 doesn't work, of course, I don't get any interrupt on it.
Here's the part of my code, which must configure CTIMER0:
// Set up Ctimer0 to generate a periodic interrupt
SYSCON -> CLKUNLOCK =0x01 ; // enables configuration of clocks
SYSCON -> CLOCK_CTRL |= FRO1MHZ_CLK_ENA ; // enable the 1MHz RC oscillator
SYSCON -> AHBCLKCTRLSET[1] |= TIMER0_CLK_EN ; //
SYSCON -> CTIMERCLKDIV[0] = 0x0 ; // ** divider clock runs, division by 1
SYSCON -> CTIMERCLKSEL[0] = FRO1M; // ** CTimer0 clock is 1MHz from FRO
SYSCON -> CLKUNLOCK = 0x00 ; // disables configuration of clocks
SYSCON -> PRESETCTRLCLR[1] |= TIMER0_RST; // reset timer 0
SYSCON -> CTIMERGLOBALSTARTEN |= CTIMER0_CLK_EN; // Ctimer 0 gets clock
CTIMER0 -> TCR |= CEN ; // CTIMER0 Counter enable
CTIMER0 -> PR = 1000U ; // Prescaler set to 1000. 1MHz : 1000 = 1kHz to timer0
CTIMER0 -> MSR[0] = 1000U ; // Match shadow register, also 1000, results in 1sec timer0
CTIMER0 -> MR[0] = 1000U ; // beginning value
CTIMER0 -> MCR |= MR0S | MR0RL | MR0I ; // stop on match, reload MR0 from shadow, generate IT
NVIC -> ISER[0] |= 1U<<10 ; // Enable Interrupt No 10 (Timer0)
7- I got bit 10 from the file LPC5536.h, line 93. I didn't find any other documentation, if this interrupt is really from CTIMER0. Is it right?
Other parts of my - at the moment still primitive - code are properly working, I can debug them - whenever the debugger works.
解決済! 解決策の投稿を見る。
Hi,
Pls try the following code, it works on my EVK board
#define TIMER0_RST 1<<26
#define TIMER0_CLK_EN 1<<26
#define FRO1MHZ_CLK_ENA 1<<6
#define FRO1M 0x04
void CTimerInit(void)
{
// Set up Ctimer0 to generate a periodic interrupt
SYSCON->PRESETCTRLSET[1] = TIMER0_RST; // reset timer 0
SYSCON->PRESETCTRLCLR[1] = TIMER0_RST; // reset timer 0
//SYSCON->CLKUNLOCK =0x01 ; // enables configuration of clocks
SYSCON->CLOCK_CTRL |= FRO1MHZ_CLK_ENA ; // enable the 1MHz RC oscillator
SYSCON->AHBCLKCTRLSET[1] |= TIMER0_CLK_EN ; //
SYSCON->CTIMERCLKDIV[0] = 0x0 ; // ** divider clock runs, division by 1
SYSCON->CTIMERCLKSEL[0] = FRO1M; // ** CTimer0 clock is 1MHz from FRO
//SYSCON->CLKUNLOCK = 0x01 ; // disables configuration of clocks
CTIMER0->PR = 1000U ; // Prescaler set to 1000. 1MHz : 1000 = 1kHz to timer0
CTIMER0->MSR[0] = 1000U ; // Match shadow register, also 1000, results in 1sec timer0
CTIMER0->MR[0] = 1000U ; // beginning value
CTIMER0->CTCR=0x00;
CTIMER0->EMR=0x00;
CTIMER0->PWMC=0x00;
CTIMER0->MCR |= 1<<24|3<<0; // stop on match, reload MR0 from shadow, generate IT
NVIC->ISER[0] |= 1U<<10 ; // Enable Interrupt No 10 (Timer0)
NVIC->ICPR[0] |= 1U<<10 ;
NVIC->IPR[10]=0x00;
//SYSCON->CTIMERGLOBALSTARTEN |= CTIMER0_CLK_EN; // Ctimer 0 gets clock
CTIMER0->TCR |= 0x01; // CTIMER0 Counter enable
}
void CTIMER0_IRQHandler(void)
{
//clear flag
CTIMER0->IR|=1<<0;
GPIO_PortToggle(GPIO, BOARD_LED_PORT, 1u << BOARD_LED_PIN);
}
Hope it can help you
Dear XiangJun Rong,
I discovered, that there's an error in the attached code. I corrected already from line 113 to 117, I access CTIMER0 now.
Now, my code compiles properly, runs until line 103 properly. From line 103 I single step. When I reach line 113 (CTIMER0->TCR |= CEN
How can I debug this code? Is it impossible?
My questions are:
- How can I be sure, that CTIMER0 is properly configured and it counts?
- How can I avoid the lost of connection between MCUXpresso and MCU-LINK Pro?
Dear XiangJun Rong,
thank You for the suggestions. Unfortunately, I am still in trouble.
As I understand Your answer, CTIMER0 and TIMER0 are the same thing, only the terminology is different in the documentation. It's OK then.
The root of my problem is: I need to have a periodic interrupt in about every second. I try to set it up, but without a success.
My idea is: CTIMER0 runs, counts about 1 sec long and generates the necessary interrupt and starts all over again.
This interrupt doesn't happen, and I am trying to find out why. Maybe the counter doesn't work, maybe it is misconfigured, maybe I set the interrupt the wrong way or something else?
First, I try to check the counter.
TIMER0_CLK_EN is defined as 26 in a header file, it is not a problem.
My code runs until the breakpoint in line 103 without any problem. The first instruction is in line 77, there are only includes and comments before. The IRQ service routine is at the end of my code.
As I try to debug my code, I single step after line 103. Everything is fine, until I try to see the CTIMER0 registers. When the command in line 108 turns the clock ON, I immediately loose the connection between MCUXpresso and MCU-LINK PRO.
In case I don't watch the CTIMER0 registers in debug perspective, than I am able to single step through my code (IRQ doesn't come whether I single step or I let it run full speed).
If I stop the clock to CTIMER0 later in the code, I am able to look into the register-bank of CTIMER0, but I only see all zeroes there, like it wouldn't be configured at all.
Hi,
Pls refer to SDK example of CTimer.
You can download SDK from the link:
https://mcuxpresso.nxp.com/en/welcome
I suppose that the CTimer registers writing has to follow up a sequence.
After you enable the CTimer gated clock and set up the CTimer clock source, then follow up the sequence.
CTIMER4->CTCR=0x00;
CTIMER4->PR=0x00;
CTIMER4->MCR=0x03;
CTIMER4->PWMC=0x00;
CTIMER4->MR[0]=0x2000;
CTIMER4->MSR[0]=0x2000;
Pls have a try
BR
XiangJun Rong
Dear xiangjun_rong,
I tried to implement Your code. The only modification is, I modified to CTIMER0 instead of CTIMER4.
When the clock of CTIMER0 is enabled, line 128, I immediately loose the connection between MCUXpresso and MCU-LINK Pro.
I cannot find out, how to resolve this problem:
The only workaround is not to enable the clock for CTIMER0, by commenting line 128 out - see the screenshot please.
Of course, without clock, it is not possible to configure and run the CTIMER0. But, there's no loss of connection, the other parts of my code works.
I don't know what to do. Please recommend me a solution.
Hi,
Pls try the following code, it works on my EVK board
#define TIMER0_RST 1<<26
#define TIMER0_CLK_EN 1<<26
#define FRO1MHZ_CLK_ENA 1<<6
#define FRO1M 0x04
void CTimerInit(void)
{
// Set up Ctimer0 to generate a periodic interrupt
SYSCON->PRESETCTRLSET[1] = TIMER0_RST; // reset timer 0
SYSCON->PRESETCTRLCLR[1] = TIMER0_RST; // reset timer 0
//SYSCON->CLKUNLOCK =0x01 ; // enables configuration of clocks
SYSCON->CLOCK_CTRL |= FRO1MHZ_CLK_ENA ; // enable the 1MHz RC oscillator
SYSCON->AHBCLKCTRLSET[1] |= TIMER0_CLK_EN ; //
SYSCON->CTIMERCLKDIV[0] = 0x0 ; // ** divider clock runs, division by 1
SYSCON->CTIMERCLKSEL[0] = FRO1M; // ** CTimer0 clock is 1MHz from FRO
//SYSCON->CLKUNLOCK = 0x01 ; // disables configuration of clocks
CTIMER0->PR = 1000U ; // Prescaler set to 1000. 1MHz : 1000 = 1kHz to timer0
CTIMER0->MSR[0] = 1000U ; // Match shadow register, also 1000, results in 1sec timer0
CTIMER0->MR[0] = 1000U ; // beginning value
CTIMER0->CTCR=0x00;
CTIMER0->EMR=0x00;
CTIMER0->PWMC=0x00;
CTIMER0->MCR |= 1<<24|3<<0; // stop on match, reload MR0 from shadow, generate IT
NVIC->ISER[0] |= 1U<<10 ; // Enable Interrupt No 10 (Timer0)
NVIC->ICPR[0] |= 1U<<10 ;
NVIC->IPR[10]=0x00;
//SYSCON->CTIMERGLOBALSTARTEN |= CTIMER0_CLK_EN; // Ctimer 0 gets clock
CTIMER0->TCR |= 0x01; // CTIMER0 Counter enable
}
void CTIMER0_IRQHandler(void)
{
//clear flag
CTIMER0->IR|=1<<0;
GPIO_PortToggle(GPIO, BOARD_LED_PORT, 1u << BOARD_LED_PIN);
}
Hope it can help you
Dear XiangJun Rong,
Thank You very much, the code, You sent seems to work perfect.
Shall I give another question please?
When I let the code run in debug perspective on full speed, it is OK.
But, when I try to single step, at the moment I enable the clock for CTimer0, debugging is not more possible. The connection between MCUXpresso and MCU-Link Pro is broken:
It only happens when I try to watch the CTimer0 registers.
Is this the normal behavior of this setup, or there's still something wrong?
Hi,
I have tried to run the void CTimerInit(void) code on my LPC5536-EVK board based on MCUXPrresso tools step by step,
I have not problem.
Maybe it is related to probe
BR
XiangJun Rong
Hi,
I suppose that the line is incorrect.
#define TIMER0_CLK_EN 1<<26
SYSCON -> AHBCLKCTRLSET[1] |= TIMER0_CLK_EN ;
You can write it as:
SYSCON -> AHBCLKCTRLSET[1] = TIMER0_CLK_EN ;
or
SYSCON -> AHBCLKCTRL[1] |= TIMER0_CLK_EN ;
After you set the bit 26 of SYSCON -> AHBCLKCTRL[1] , you can access the CTimer0 register.
The target of SYSCON->CTIMERGLOBALSTARTEN register is that all the CTimer modules can synchronize if they use the same clock as tick.
Ctimerx clock diagram for LPC553x:
Hope it can help you
BR
XiangJun Rong