1 /*
2  * @brief Multi-Rate Timer (MRT) example
3  *
4  * @note
5  * Copyright(C) NXP Semiconductors, 2013
6  * All rights reserved.
7  *
8  * @par
9  * Software that is described herein is for illustrative purposes only
10  * which provides customers with programming information regarding the
11  * LPC products.  This software is supplied "AS IS" without any warranties of
12  * any kind, and NXP Semiconductors and its licensor disclaim any and
13  * all warranties, express or implied, including all implied warranties of
14  * merchantability, fitness for a particular purpose and non-infringement of
15  * intellectual property rights.  NXP Semiconductors assumes no responsibility
16  * or liability for the use of the software, conveys no license or rights under any
17  * patent, copyright, mask work right, or any other intellectual property rights in
18  * or to any products. NXP Semiconductors reserves the right to make changes
19  * in the software without notification. NXP Semiconductors also makes no
20  * representation or warranty that such application will be suitable for the
21  * specified use without further testing or modification.
22  *
23  * @par
24  * Permission to use, copy, modify, and distribute this software and its
25  * documentation is hereby granted, under NXP Semiconductors' and its
26  * licensor's relevant copyrights in the software, without fee, provided that it
27  * is used in conjunction with NXP Semiconductors microcontrollers.  This
28  * copyright, permission, and disclaimer notice must appear in all copies of
29  * this code.
30  */
31 
32 #include "board.h"
33 
34 /*****************************************************************************
35  * Private types/enumerations/variables
36  ****************************************************************************/
37 
38 static volatile bool t3Fired;
39 
40 /*****************************************************************************
41  * Public types/enumerations/variables
42  ****************************************************************************/
43 
44 /*****************************************************************************
45  * Private functions
46  ****************************************************************************/
47 
48 /* Setup a timer for a periodic (repeat mode) rate */
setupMRT(uint8_t ch,MRT_MODE_T mode,uint32_t rate)49 static void setupMRT(uint8_t ch, MRT_MODE_T mode, uint32_t rate)
50 {
51 	LPC_MRT_CH_T *pMRT;
52 
53 	/* Get pointer to timer selected by ch */
54 	pMRT = Chip_MRT_GetRegPtr(ch);
55 
56 	/* Setup timer with rate based on MRT clock */
57 	Chip_MRT_SetInterval(pMRT, (Chip_Clock_GetSystemClockRate() / rate) |
58 						 MRT_INTVAL_LOAD);
59 
60 	/* Timer mode */
61 	Chip_MRT_SetMode(pMRT, mode);
62 
63 	/* Clear pending interrupt and enable timer */
64 	Chip_MRT_IntClear(pMRT);
65 	Chip_MRT_SetEnabled(pMRT);
66 }
67 
68 /*****************************************************************************
69  * Public functions
70  ****************************************************************************/
71 
72 /**
73  * @brief	Handle interrupt from MRT
74  * @return	Nothing
75  */
MRT_IRQHandler(void)76 void MRT_IRQHandler(void)
77 {
78 	uint32_t int_pend;
79 
80 	/* Get and clear interrupt pending status for all timers */
81 	int_pend = Chip_MRT_GetIntPending();
82 	Chip_MRT_ClearIntPending(int_pend);
83 
84 	/* Channel 0 */
85 	if (int_pend & MRTn_INTFLAG(0)) {
86 		Board_LED_Toggle(0);
87 	}
88 
89 	/* Channel 1 */
90 	if (int_pend & MRTn_INTFLAG(1)) {
91 		Board_LED_Toggle(1);
92 	}
93 
94 	/* Channel 2 is single shot, reset it here */
95 	if (int_pend & (MRTn_INTFLAG(2))) {
96 		setupMRT(2, MRT_MODE_ONESHOT, 2);	/* Will fire in 0.5 seconds */
97 		Board_LED_Toggle(2);
98 	}
99 }
100 
101 /**
102  * @brief	MRT example main function
103  * @return	Status (This function will not return)
104  */
main(void)105 int main(void)
106 {
107 	int mrtch;
108 
109 	/* Generic Initialization */
110 	SystemCoreClockUpdate();
111 	Board_Init();
112 
113 	DEBUGSTR("LPC15xx MRT Example \r\n");
114 
115 	/* MRT Initialization and disable all timers */
116 	Chip_MRT_Init();
117 	for (mrtch = 0; mrtch < MRT_CHANNELS_NUM; mrtch++) {
118 		Chip_MRT_SetDisabled(Chip_MRT_GetRegPtr(mrtch));
119 	}
120 
121 	/* Enable the interrupt for the MRT */
122 	NVIC_EnableIRQ(MRT_IRQn);
123 
124 	/* Enable timers 0 and 1 in repeat mode with different rates */
125 	setupMRT(0, MRT_MODE_REPEAT, 2);/* 2Hz rate */
126 	setupMRT(1, MRT_MODE_REPEAT, 5);/* 5Hz rate */
127 
128 	/* Enable timer 2 in single one mode with the interrupt restarting the
129 	   timer */
130 	setupMRT(2, MRT_MODE_ONESHOT, 7);	/* Will fire in 1/7 seconds */
131 
132 	/* All processing and MRT reset in the interrupt handler */
133 	while (1) {
134 		__WFI();
135 	}
136 
137 	return 0;
138 }
139