Sunday, 3 August 2014

Microsecond delays using the LPC810's Multi Rate Timer (MRT).

A Situation that occurs frequently is the need to delay the CPU for a specific number of microseconds or clock cycles. An ideal way to do this with the with the Multi Rate Timer of the LPC810 is with a special mode called One-shot bus stall mode.

The Multi Rate Timer comprises of Four individual 31 bit timers capable of generating a repetitive interrupt similar to the SysTick Timer or counting down to zero and generating a single shot interrupt. All the four timers will trigger the same interrupt routine which will need to determine which of the interrupt enabled timers triggered it.

There is however another mode of operation which does not use interrupts and is known as the One-shot bus stall mode. This mode  will instead stall the bus interface of the System Clock for a specified number of clock cycles. This mode effectively stops all CPU activity until the MRT has finished counting down to zero and is ideal for creating a microsecond or fixed cycle delay functions as in the time the MRT is counting down to zero the CPU consumes very little power as opposed to a tradition delay loop or a polled timer delay loop.

We need to write the number of clock cycles to stall the bus to the INTVAL register of the timer, The bus is actually stalled for INTVAL+3 cycles as some time is needed by the MRT to actually stall the bus.

First lets use channel 3 of the MRT and initialize it to bus stall mode somewhere in our main routine.

LPC_MRT->Channel[3].CTRL = (0x02 << 1) ; //MRT3 (halt_clk) bus stall mode , no interrupts

Next lets define a macro that we can use to halt the CPU for a number of clock cycles, This macro works providing that the number of clock cycles is greater that 3.

#define halt_clk(clk) LPC_MRT->Channel[3].INTVAL = (clk-3)

For Instance

halt_clk(120); /* Gives a 10us delay with 12MHZ clock */

No comments:

Post a Comment