Timer Resources

08 Jan 2010

Hello:

I want to use some of the other timer resources in the LPC17xx device and need find out which hardware resources the MBED timer calls (Timer/Timeout/Ticker) used?  The Repetitive, interrupt timer? The System Tick Timer?  Timer0/1/2/3?

I just want to make sure I am not trashing the nice mbed functions when I start writing to the registers....

 

Thanks,

Eli Hughes

08 Jan 2010

Hi Eli,

The only timer we use is Timer 3 on the LPC1768 and LPC2368 (shared by wait, Timer, Ticker, Timeout implementations). All others are free for your own use!

Simon

13 Jan 2010

Hi,

I'm trying to use timer0 and timer1. When I compile

//TIMER0 initialize // PCLK/4 = 24MHz // desired value: 100uSec void timer0_init(void) { LPC_PCONP |= 1; // Timer0 Power On LPC_T0_MR0 = 2398; // Match count for 100uS LPC_T0_MCR = 3; // Interrupt and Reset on Match LPC_T0_TCR = 1; // Enable Timer0 }

none of the register names are recognized. Should I include something like LPC17xx.h? If so where is it?

13 Jan 2010

LPC17xx.h is included, but it's changed from the old LPC format. Now all registers for a peripheral are put into a structure. So you need to do:

// TIMER0 initialize
// PCLK/4 = 24MHz
// desired value: 100uSec
void timer0_init(void)
{ 
  LPC_SC->PCONP |= 1<<1; // Timer0 Power On
  LPC_TIM0->MR0 = 2398;  // Match count for 100uS
  LPC_TIM0->MCR = 3;     // Interrupt and Reset on Match
  LPC_TIM0->TCR = 1;     // Enable Timer0
}

(note also that Timer 0's bit number in PCONP is 1, not 0.)

14 Jan 2010

Thanks. That moves things along quite a bit.

I found a copy of LPC17xx.h and added it as a file in my project. Is this necessary, or will just a #include "LPC17xx.h" find it somewhere in the mbed file structure?

14 Jan 2010

mbed.h includes LPC17xx.h (via cmsis.h).

14 Jan 2010

The LPC17xx.h file is part of the mbed library, so as Igor says, it gets included if you #include "mbed.h".

If you want to browse the file itself just follow the link in the library in the IDE, or go directly here:

btw. You could also #include "LPC17xx.h" if you wanted, but then it won't "just work" for a LPC2368 board.

Simon

14 Jan 2010

The light slowly dawns. I plowed through LPC17xx.h to find the timer32 structure and the assigned names for the timer0 and timer1 copies. Is there an easier way to find this sort of information? Like "CMSIS and the LPC1768 for Dummies"

14 Jan 2010

There is a tutorial by Doulos but it's pretty short and doesn't explain much. The LPC17xx CMSIS package from NXP includes a CHM file with automatically-generated contents from the source code. It's rather big and unwieldy but it does have a list of register typedefs. You can also use helper headers from it for easier control of peripherals. See examples in the zip file (e.g. Examples\TIMER\Interrupt_Match).

15 Jan 2010

Hi Thanks for the links and I used both, but could not pinpoint any help with my next problem, How to set up the VIC to link the timer interrupt to the timer ISR. Remember I am porting code from the LPC2103.

I think also I need to assign some interrupt priority. I'm used to writing something like

 

VICVectAddr4=(unsigned)timer0_isr;

VICVectCntl4 = 0x20 | 4;

VICIntEnable = 0x00000010;

but now what do I do? Also how do I do it without screwing up the mbed library stuff?

15 Jan 2010 . Edited: 19 Jan 2010

In Cortex-M3, IRQ handlers are placed in the vector table at the linking time. The dummy handlers of the standard startup code are declared WEAK, so any handlers you declare yourself will take priority. You just need to make sure you use the correct name.

extern "C" void TIMER0_IRQHandler(void)
{
  // handle interrupt  
}

If you prefer to do it at runtime, you can use NVIC_SetVector(TIMER0_IRQn, uint32_t(MyTimer0Int)). To enable or disable IRQ, use NVIC_EnableIRQ/NVIC_DisableIRQ. For priority, NVIC_SetPriority().

mbed library uses only timer 3, so using 0/1/2 should not be a problem.

Here's the list of standard handler names:

Reset_Handler
NMI_Handler
HardFault_Handler
MemManage_Handler
BusFault_Handler
UsageFault_Handler
SVC_Handler
DebugMon_Handler
PendSV_Handler
SysTick_Handler
ADC_IRQHandler
BOD_IRQHandler
CAN_IRQHandler
DMA_IRQHandler
EINT0_IRQHandler
EINT1_IRQHandler
EINT2_IRQHandler
EINT3_IRQHandler
ENET_IRQHandler
I2C0_IRQHandler
I2C1_IRQHandler
I2C2_IRQHandler
I2S_IRQHandler
MCPWM_IRQHandler
PLL0_IRQHandler
PLL1_IRQHandler
PWM1_IRQHandler
QEI_IRQHandler
RIT_IRQHandler
RTC_IRQHandler
SPI_IRQHandler
SSP0_IRQHandler
SSP1_IRQHandler
TIMER0_IRQHandler
TIMER1_IRQHandler
TIMER2_IRQHandler
TIMER3_IRQHandler
UART0_IRQHandler
UART1_IRQHandler
UART2_IRQHandler
UART3_IRQHandler
USB_IRQHandler
WDT_IRQHandler

Edit: added extern "C" to the handler declaration.

19 Jan 2010

Hi Igor

Looked everywhere I can think of, but I cannot find any documentation on the  NVIC_SetVector(), or NVIC_SetPriority().

In your example  NVIC_SetVector(TIMER0_IRQn, uint32_t(MyTimer0Int)) is MyTimer0Int the name of the ISR?

Like:

void MyTimer0Int (void)

{

// Service interrupt

}

Also if I do not do a NVIC_SetPriority(). then are there default priorities? If so what are they for the timers?

19 Jan 2010

NVIC_SetVector() is defined in cmsis_nvic.h:

void NVIC_SetVector(IRQn_Type IRQn, uint32_t vector);
uint32_t NVIC_GetVector(IRQn_Type IRQn);

vector is the address of the new handler. You will need to cast it to an uint32_t. As for priorities, it seems the default priority is 0 for all IRQs.

16 Mar 2010

Igor,

I'm back working on my timer interrupt routines after studying the CMSIS example. The example includes:

if(TIM_GetIntStatus(LPC_Tim0,0)

{

TIM_ClearIntPending(LPC_TIM0,0)

...............

}

Neither of these instructions are recognized by the mbed compiler, so am I missing some includes, or are there equivalent instructions that can be used with mbed?

16 Mar 2010

Those look like functions from the DriverLibrary.

16 Mar 2010

Hi Igor,

Your comment on the link "had to fix a lot of warnings and found a couple of pretty obvious bugs, so the chances are there are more." is not very encouraging. Is there a way to directly access registers to get the Interrupt Status, and Clear a Pending Interrupt?

The LPC17xx User manual tells of a register T0IR and setting the bits to zero should clear the interrupt. I tried T0IR= 0; and LPC_T0IR = 0; but the compiler would not buy either of them.

The disconnect between register names in the User Manual and the names that actually work in the code is very frustrating. Not like the good old days of the LPC2103

17 Mar 2010

When in doubt, check LPC17xx.h. In your case, you need to access LPC_TIM0->IR.

You can also check the implementation of TIM_ClearIntPending in lpc17xx_timer.c.

08 Jun 2010

hi,

Regarding timer3 usage, is there any way to make Mbed use another timer by default?

My problem is that I need timer 3 for the MAT3.0 output pin. Since I'm already using timer 2 and PWM, I need another timer that has a MAT output in Mbed (since my PWM signal has a different period I can't use another PWM channel).

 

Thanks,

Einat.  

26 Nov 2010

Hi! I have question regarding Timer's CAP register. Hope you can help me:

I'm trying to use CAP registers but not as a counter, but to use it to detect a rising or falling edge in one of the CAPn.0 or CAPn.1 so basically what it does is that whenever an event like this happens, a capture register is loaded with the value of the Timer Counter so you have the precision time of the event. However, in page 497 of the user manual, says the following:

21.6.10 Capture Control Register (T[0/1/2/3]CCR - 0x40004028, 0x40008028,
0x40090028, 0x40094028)
The Capture Control Register is used to control whether one of the four Capture Registers
is loaded with the value in the Timer Counter when the capture event occurs, and whether
an interrupt is generated by the capture event. Setting both the rising and falling bits at the
same time is a valid configuration, resulting in a capture event for both edges. In the
description below, "n" represents the Timer number, 0 or 1.

It calls my attention that n in CAPn.0 and CAPn.1 only applies for 0 or 1, (CAP0.[0/1] or CAP1.[0/1]) for the purpose of detecting the exact time of an event. However, if you take a look at the mbed schematic CAP0.x and CAP1.x its not connected to any DIP pin that we can use. So we can't use it, right?

Basically what I want is to capture the exact time when in a certain pin a rising edge or falling edge happens. Exactly what the description says, but according to the description it only works for CAP1.x or CAP0.x. CAP2.x and CAP3.x are indeed available, but not as event detectors.

Do you have any advice for me?

14 Sep 2014

Hi,

I'm looking at possibly using Timer3 for my application as I'm running out of timer DIP pins and the Timer3 CAP pins happen to be available as DIP pins. So I was wondering if anyone here has tried running the mbed.h functions off another Timer like Timer0

Any thoughts? Or suggestions of where to start looking?

14 Sep 2014

Since the mbed lib is open source now (which wasn't the case when this topic was made), it is very easy to do. Delete your mbed lib from your project, and import http://mbed.org/users/mbed_official/code/mbed-src/ instead (same as mbed lib, only the not precompiled version, also it is kinda the beta branch of the mbed lib). Now go to this file: http://mbed.org/users/mbed_official/code/mbed-src/file/711b2ef89052/targets/hal/TARGET_NXP/TARGET_LPC176X/us_ticker.c, and just changing the two defines at the top of the code to another timer should do the job.

14 Sep 2014

Works like a charm! Thank you Erik! That was a great tip!