1 /**************************************************************************/
4 @author K. Townsend (microBuilder.eu)
10 Generic code for 32-bit timers. By default, the timers are configured
11 to generate an interrupt once every 100 microseconds, incrementing a
12 global variable once per tick.
14 @warning Please note that the ROM-based USB drivers on the LPC1343
15 require the use of 32-bit Timer 1. If you plan on using the
16 ROM-based USB functionality, you should restrict your timer
17 usage to 32-bit timer 0.
22 #include "/core/cpu/cpu.h"
23 #include "/core/timer32/timer32.h"
27 // Initialise 32-bit timer 0 with 100uS ticks
28 timer32Init(0, TIMER32_DEFAULTINTERVAL);
33 // Cause a blocking delay for 1 second (1000mS)
34 timer32Delay(0, TIMER32_DELAY_1MS * 1000);
39 Software License Agreement (BSD License)
41 Copyright (c) 2010, microBuilder SARL
44 Redistribution and use in source and binary forms, with or without
45 modification, are permitted provided that the following conditions are met:
46 1. Redistributions of source code must retain the above copyright
47 notice, this list of conditions and the following disclaimer.
48 2. Redistributions in binary form must reproduce the above copyright
49 notice, this list of conditions and the following disclaimer in the
50 documentation and/or other materials provided with the distribution.
51 3. Neither the name of the copyright holders nor the
52 names of its contributors may be used to endorse or promote products
53 derived from this software without specific prior written permission.
55 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS ''AS IS'' AND ANY
56 EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
57 WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
58 DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER BE LIABLE FOR ANY
59 DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
60 (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
61 LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
62 ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
63 (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
64 SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
66 /**************************************************************************/
70 volatile uint32_t timer32_0_counter
= 0;
71 volatile uint32_t timer32_1_counter
= 0;
72 void (*interruptHandler0
)(void) = NULL
;
74 /**************************************************************************/
76 @brief Causes a blocking delay for the specified number of
77 timer ticks. The duration of each 'tick' is determined by
78 the 'timerInterval' property supplied to timer32Init.
81 The 32-bit timer to user (0..1)
83 The number of counter increments to wait
85 /**************************************************************************/
86 void timer32Delay(uint8_t timerNum
, uint32_t delay
)
92 curTicks
= timer32_0_counter
;
93 if (curTicks
> 0xFFFFFFFF - delay
)
95 // Rollover will occur during delay
96 while (timer32_0_counter
>= curTicks
)
98 while (timer32_0_counter
< (delay
- (0xFFFFFFFF - curTicks
)));
103 while ((timer32_0_counter
- curTicks
) < delay
);
107 else if (timerNum
== 1)
109 curTicks
= timer32_1_counter
;
110 if (curTicks
> 0xFFFFFFFF - delay
)
112 // Rollover will occur during delay
113 while (timer32_1_counter
>= curTicks
)
115 while (timer32_1_counter
< (delay
- (0xFFFFFFFF - curTicks
)));
120 while ((timer32_1_counter
- curTicks
) < delay
);
127 /**************************************************************************/
129 @brief Interrupt handler for 32-bit timer 0
131 /**************************************************************************/
132 void TIMER32_0_IRQHandler(void)
134 /* Call the callback function if required */
135 if(NULL
!= interruptHandler0
)
137 (*interruptHandler0
)();
140 /* Clear the interrupt flag */
141 TMR_TMR32B0IR
= TMR_TMR32B0IR_MR0
;
143 /* If you wish to perform some action after each timer 'tick' (such as
144 incrementing a counter variable) you can do so here */
150 /**************************************************************************/
152 @brief Interrupt handler for 32-bit timer 1
154 /**************************************************************************/
155 void TIMER32_1_IRQHandler(void)
157 /* Clear the interrupt flag */
158 TMR_TMR32B1IR
= TMR_TMR32B1IR_MR0
;
160 /* If you wish to perform some action after each timer 'tick' (such as
161 incrementing a counter variable) you can do so here */
167 /**************************************************************************/
169 @brief Enables the specified timer
172 The 32-bit timer to enable (0..1)
174 /**************************************************************************/
175 void timer32Enable(uint8_t timerNum
)
179 TMR_TMR32B0TCR
= TMR_TMR32B0TCR_COUNTERENABLE_ENABLED
;
182 else if (timerNum
== 1)
184 TMR_TMR32B1TCR
= TMR_TMR32B1TCR_COUNTERENABLE_ENABLED
;
190 /**************************************************************************/
192 @brief Disables the specified timer
195 The 32-bit timer to disable (0..1)
197 /**************************************************************************/
198 void timer32Disable(uint8_t timerNum
)
202 TMR_TMR32B0TCR
= TMR_TMR32B0TCR_COUNTERENABLE_DISABLED
;
205 else if (timerNum
== 1)
207 TMR_TMR32B1TCR
= TMR_TMR32B1TCR_COUNTERENABLE_DISABLED
;
213 /**************************************************************************/
215 @brief Resets the specified timer
218 The 32-bit timer to reset (0..1)
220 /**************************************************************************/
221 void timer32Reset(uint8_t timerNum
)
227 regVal
= TMR_TMR32B0TCR
;
228 regVal
|= TMR_TMR32B0TCR_COUNTERRESET_ENABLED
;
229 TMR_TMR32B0TCR
= regVal
;
232 else if (timerNum
== 1)
234 regVal
= TMR_TMR32B1TCR
;
235 regVal
|= TMR_TMR32B1TCR_COUNTERRESET_ENABLED
;
236 TMR_TMR32B1TCR
= regVal
;
242 /**************************************************************************/
244 @brief Initialises the specified 32-bit timer, and configures the
245 timer to raise an interrupt and reset on match on MR0.
248 The 32-bit timer to initiliase (0..1)
249 @param[in] timerInterval
250 The number of clock 'ticks' between resets (0..0xFFFFFFFF)
252 @note Care needs to be taken when configuring the timers since the
253 pins are all multiplexed with other peripherals. This code is
254 provided as a starting point, but it will need to be adjusted
255 according to your own situation and pin/peripheral requirements
257 /**************************************************************************/
258 void timer32Init(uint8_t timerNum
, uint32_t timerInterval
)
260 // If timerInterval is invalid, use the default value
261 if (timerInterval
< 1)
263 timerInterval
= TIMER32_DEFAULTINTERVAL
;
268 /* Enable the clock for CT32B0 */
269 SCB_SYSAHBCLKCTRL
|= (SCB_SYSAHBCLKCTRL_CT32B0
);
271 /* The physical pins associated with CT32B0 are not enabled by
272 default in order to avoid conflicts with other peripherals. If
273 you wish to use any of the pin-dependant functionality, simply
274 uncomment the appropriate lines below. */
276 /* Configure PIO1.5 as Timer0_32 CAP0 */
277 // IOCON_PIO1_5 &= ~IOCON_PIO1_5_FUNC_MASK;
278 // IOCON_PIO1_5 |= IOCON_PIO1_5_FUNC_CT32B0_CAP0;
280 /* Configure PIO1.6 as Timer0_32 MAT0 */
281 // IOCON_PIO1_6 &= ~IOCON_PIO1_6_FUNC_MASK;
282 // IOCON_PIO1_6 |= IOCON_PIO1_6_FUNC_CT32B0_MAT0;
284 /* Configure PIO1.7 as Timer0_32 MAT1 */
285 // IOCON_PIO1_7 &= ~IOCON_PIO1_7_FUNC_MASK;
286 // IOCON_PIO1_7 |= IOCON_PIO1_7_FUNC_CT32B0_MAT1;
288 /* Configure PIO0.1 as Timer0_32 MAT2 */
289 // IOCON_PIO0_1 &= ~IOCON_PIO0_1_FUNC_MASK;
290 // IOCON_PIO0_1 |= IOCON_PIO0_1_FUNC_CT32B0_MAT2;
292 /* Configure PIO0.11 as Timer0_32 MAT3 */
293 /* Note: This pint can not be used with JTAG/SWD */
294 // IOCON_JTAG_TDI_PIO0_11 &= ~IOCON_JTAG_TDI_PIO0_11_FUNC_MASK;
295 // IOCON_JTAG_TDI_PIO0_11 |= IOCON_JTAG_TDI_PIO0_11_FUNC_CT32B0_MAT3;
297 timer32_0_counter
= 0;
298 TMR_TMR32B0MR0
= timerInterval
;
300 /* Configure match control register to raise an interrupt and reset on MR0 */
301 TMR_TMR32B0MCR
= (TMR_TMR32B0MCR_MR0_INT_ENABLED
| TMR_TMR32B0MCR_MR0_RESET_ENABLED
);
303 /* Enable the TIMER0 interrupt */
304 NVIC_EnableIRQ(TIMER_32_0_IRQn
);
307 else if ( timerNum
== 1 )
309 /* Enable the clock for CT32B1 */
310 SCB_SYSAHBCLKCTRL
|= (SCB_SYSAHBCLKCTRL_CT32B1
);
312 /* The physical pins associated with CT32B0 are not enabled by
313 default in order to avoid conflicts with other peripherals. */
315 /* Configure PIO1.0 as Timer1_32 CAP0 */
316 /* Note: This pint can not be used with JTAG/SWD */
317 // IOCON_JTAG_TMS_PIO1_0 &= ~IOCON_JTAG_TMS_PIO1_0_FUNC_MASK;
318 // IOCON_JTAG_TMS_PIO1_0 |= IOCON_JTAG_TMS_PIO1_0_FUNC_CT32B1_CAP0;
320 /* Configure PIO1.1 as Timer1_32 MAT0 */
321 /* Note: This pint can not be used with JTAG/SWD */
322 // IOCON_JTAG_TDO_PIO1_1 &= ~IOCON_JTAG_TDO_PIO1_1_FUNC_MASK;
323 // IOCON_JTAG_TDO_PIO1_1 |= IOCON_JTAG_TDO_PIO1_1_FUNC_CT32B1_MAT0;
325 /* Configure PIO1.2 as Timer1_32 MAT1 */
326 /* Note: This pint can not be used with JTAG/SWD */
327 // IOCON_JTAG_nTRST_PIO1_2 &= ~IOCON_JTAG_nTRST_PIO1_2_FUNC_MASK;
328 // IOCON_JTAG_nTRST_PIO1_2 |= IOCON_JTAG_nTRST_PIO1_2_FUNC_CT32B1_MAT1;
330 /* Configure PIO1.3 as Timer1_32 MAT2 */
331 /* Note: This pint can not be used with JTAG/SWD */
332 // IOCON_SWDIO_PIO1_3 &= ~IOCON_SWDIO_PIO1_3_FUNC_MASK;
333 // IOCON_SWDIO_PIO1_3 |= IOCON_SWDIO_PIO1_3_FUNC_CT32B1_MAT2;
335 /* Configure PIO1.4 as Timer1_32 MAT3 */
336 // IOCON_PIO1_4 &= ~IOCON_PIO1_4_FUNC_MASK;
337 // IOCON_PIO1_4 |= IOCON_PIO1_4_FUNC_CT32B1_MAT3;
339 timer32_1_counter
= 0;
340 TMR_TMR32B1MR0
= timerInterval
;
342 /* Configure match control register to raise an interrupt and reset on MR0 */
343 TMR_TMR32B1MCR
= (TMR_TMR32B1MCR_MR0_INT_ENABLED
| TMR_TMR32B1MCR_MR0_RESET_ENABLED
);
345 /* Enable the TIMER1 Interrupt */
346 NVIC_EnableIRQ(TIMER_32_1_IRQn
);
351 /**************************************************************************/
353 @brief Sets the optional callback function for 32-bit timer 0
358 #include "core/timer32/timer32.h"
360 static volatile int32_t timerCounter;
362 // Callback function for 32-bit timer 0
363 void ct32b0Callback(void)
370 // Configure cpu and mandatory peripherals
373 // Init timer 0 with 1ms delay
374 timer32Init(0, TIMER32_CCLK_1MS);
376 // Setup the interrupt callback
377 timer32SetIntHandler(ct32b0Callback);
388 /**************************************************************************/
389 void timer32SetIntHandler(void (*handler
)(void))
391 interruptHandler0
= handler
;
394 /**************************************************************************/
396 @brief Returns the value of the auto-incrementing timer counter(s)
398 /**************************************************************************/
399 uint32_t timer32GetCount(uint8_t timerNum
)
403 return timer32_0_counter
;
407 return timer32_1_counter
;
411 /**************************************************************************/
413 @brief Resets the auto-incrementing timer counter(s)
415 /**************************************************************************/
416 void timer32ResetCounter(uint8_t timerNum
)
420 timer32_0_counter
= 0;
422 else if (1 == timerNum
)
424 timer32_1_counter
= 0;