1 /*
2  * @brief LPC15xx RITimer driver
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 #ifndef __RITIMER_15XX_H_
33 #define __RITIMER_15XX_H_
34 
35 #ifdef __cplusplus
36 extern "C" {
37 #endif
38 
39 /** @defgroup RIT_15XX CHIP: LPC15xx Repetitive Interrupt Timer driver
40  * @ingroup CHIP_15XX_Drivers
41  * @{
42  */
43 
44 /**
45  * @brief Repetitive Interrupt Timer register block structure
46  */
47 typedef struct {				/*!< RITIMER Structure      */
48 	__IO uint32_t  COMPVAL;		/*!< Compare register       */
49 	__IO uint32_t  MASK;		/*!< Mask register. This register holds the 32-bit mask value. A 1 written to any bit will force a compare on the corresponding bit of the counter and compare register. */
50 	__IO uint32_t  CTRL;		/*!< Control register       */
51 	__IO uint32_t  COUNTER;		/*!< 32-bit counter         */
52 	__IO uint32_t  COMPVAL_H;	/*!< Compare upper register */
53 	__IO uint32_t  MASK_H;		/*!< Mask upper register    */
54 	__I  uint32_t  RESERVED0[1];
55 	__IO uint32_t  COUNTER_H;	/*!< Counter upper register */
56 } LPC_RITIMER_T;
57 
58 /*
59  * RIT control register
60  */
61 /**	Set by H/W when the counter value equals the masked compare value */
62 #define RIT_CTRL_INT    (1 << 0)
63 /** Set timer enable clear to 0 when the counter value equals the masked compare value  */
64 #define RIT_CTRL_ENCLR  (1 << 1)
65 /** Set timer enable on debug */
66 #define RIT_CTRL_ENBR   (1 << 2)
67 /** Set timer enable */
68 #define RIT_CTRL_TEN    (1 << 3)
69 
70 /**
71  * @brief	Initialize the RIT
72  * @param	pRITimer	: RITimer peripheral selected
73  * @return	None
74  * @note	The timer will not br running after this call. Use
75  *			Chip_RIT_Enable() to start the timer running.
76  */
77 void Chip_RIT_Init(LPC_RITIMER_T *pRITimer);
78 
79 /**
80  * @brief	Shutdown the RIT
81  * @param	pRITimer	: RITimer peripheral selected
82  * @return	None
83  */
84 void Chip_RIT_DeInit(LPC_RITIMER_T *pRITimer);
85 
86 /**
87  * @brief	Safely sets CTRL register bits
88  * @param	pRITimer	: RITimer peripheral selected
89  * @param	val			: RIT bits to be set, one or more RIT_CTRL_* values
90  * @return	None
91  */
92 void Chip_RIT_SetCTRL(LPC_RITIMER_T *pRITimer, uint32_t val);
93 
94 /**
95  * @brief	Safely clears CTRL register bits
96  * @param	pRITimer	: RITimer peripheral selected
97  * @param	val			: RIT bits to be cleared, one or more RIT_CTRL_* values
98  * @return	None
99  */
100 void Chip_RIT_ClearCTRL(LPC_RITIMER_T *pRITimer, uint32_t val);
101 
102 /**
103  * @brief	Enable Timer
104  * @param	pRITimer		: RITimer peripheral selected
105  * @return	None
106  */
Chip_RIT_Enable(LPC_RITIMER_T * pRITimer)107 STATIC INLINE void Chip_RIT_Enable(LPC_RITIMER_T *pRITimer)
108 {
109 	Chip_RIT_SetCTRL(pRITimer, RIT_CTRL_TEN);
110 }
111 
112 /**
113  * @brief	Disable Timer
114  * @param	pRITimer	: RITimer peripheral selected
115  * @return	None
116  */
Chip_RIT_Disable(LPC_RITIMER_T * pRITimer)117 STATIC INLINE void Chip_RIT_Disable(LPC_RITIMER_T *pRITimer)
118 {
119 	Chip_RIT_ClearCTRL(pRITimer, RIT_CTRL_TEN);
120 }
121 
122 /**
123  * @brief	Enable timer debug mode
124  * @param	pRITimer	: RITimer peripheral selected
125  * @return	None
126  * @note	This function halts the repetitive timer when
127  *			the processor is halted for debugging.
128  */
Chip_RIT_EnableDebug(LPC_RITIMER_T * pRITimer)129 STATIC INLINE void Chip_RIT_EnableDebug(LPC_RITIMER_T *pRITimer)
130 {
131 	Chip_RIT_SetCTRL(pRITimer, RIT_CTRL_ENBR);
132 }
133 
134 /**
135  * @brief	Disable timer debug mode
136  * @param	pRITimer	: RITimer peripheral selected
137  * @return	None
138  * @note	This function allows the repetitive timer to continue running
139  *			when the processor is halted for debugging.
140  */
Chip_RIT_DisableDebug(LPC_RITIMER_T * pRITimer)141 STATIC INLINE void Chip_RIT_DisableDebug(LPC_RITIMER_T *pRITimer)
142 {
143 	Chip_RIT_ClearCTRL(pRITimer, RIT_CTRL_ENBR);
144 }
145 
146 /**
147  * @brief	Enables automatic counter clear on compare
148  * @param	pRITimer	: RITimer peripheral selected
149  * @return	None
150  */
Chip_RIT_EnableCompClear(LPC_RITIMER_T * pRITimer)151 STATIC INLINE void Chip_RIT_EnableCompClear(LPC_RITIMER_T *pRITimer)
152 {
153 	Chip_RIT_SetCTRL(pRITimer, RIT_CTRL_ENCLR);
154 }
155 
156 /**
157  * @brief	Disables automatic counter clear on compare
158  * @param	pRITimer	: RITimer peripheral selected
159  * @return	None
160  */
Chip_RIT_DisableCompClear(LPC_RITIMER_T * pRITimer)161 STATIC INLINE void Chip_RIT_DisableCompClear(LPC_RITIMER_T *pRITimer)
162 {
163 	Chip_RIT_ClearCTRL(pRITimer, RIT_CTRL_ENCLR);
164 }
165 
166 /**
167  * @brief	Check whether timer interrupt is pending
168  * @param	pRITimer	: RITimer peripheral selected
169  * @return	true if the interrupt is pending, otherwise false
170  */
Chip_RIT_GetIntStatus(LPC_RITIMER_T * pRITimer)171 STATIC INLINE bool Chip_RIT_GetIntStatus(LPC_RITIMER_T *pRITimer)
172 {
173 	return (bool) ((pRITimer->CTRL & RIT_CTRL_INT) != 0);
174 }
175 
176 /**
177  * @brief	Clears the timer interrupt pending state
178  * @param	pRITimer	: RITimer peripheral selected
179  * @return	None
180  */
Chip_RIT_ClearIntStatus(LPC_RITIMER_T * pRITimer)181 STATIC INLINE void Chip_RIT_ClearIntStatus(LPC_RITIMER_T *pRITimer)
182 {
183 	Chip_RIT_SetCTRL(pRITimer, RIT_CTRL_INT);
184 }
185 
186 /**
187  * @brief	Set a tick value for the interrupt to time out
188  * @param	pRITimer	: RITimer peripheral selected
189  * @param	val			: value (in ticks) for the coounter compare value
190  * @return	None
191  * @note	The base timer tick rate can be determined by calling
192  *			Chip_Clock_GetSystemClockRate().
193  */
194 void Chip_RIT_SetCompareValue(LPC_RITIMER_T *pRITimer, uint64_t val);
195 
196 /**
197  * @brief	Returns the current timer compare value
198  * @param	pRITimer	: RITimer peripheral selected
199  * @return	the current timer compare value
200  */
201 uint64_t Chip_RIT_GetCompareValue(LPC_RITIMER_T *pRITimer);
202 
203 /**
204  * @brief	Sets a mask value used for bit based compare
205  * @param	pRITimer	: RITimer peripheral selected
206  * @param	mask		: Mask value for timer (see user manual)
207  * @return	None
208  */
209 void Chip_RIT_SetMaskValue(LPC_RITIMER_T *pRITimer, uint64_t mask);
210 
211 /**
212  * @brief	Returns the mask value used for bit based compare
213  * @param	pRITimer	: RITimer peripheral selected
214  * @return	the current mask value
215  */
216 uint64_t Chip_RIT_GetMaskValue(LPC_RITIMER_T *pRITimer);
217 
218 /**
219  * @brief	Sets the current timer Counter value
220  * @param	pRITimer	: RITimer peripheral selected
221  * @param	count		: Count value to set timer to
222  * @return	Nothing
223  */
224 void Chip_RIT_SetCounter(LPC_RITIMER_T *pRITimer, uint64_t count);
225 
226 /**
227  * @brief	Returns the current timer Counter value
228  * @param	pRITimer	: RITimer peripheral selected
229  * @return	the current timer counter value
230  */
231 uint64_t Chip_RIT_GetCounter(LPC_RITIMER_T *pRITimer);
232 
233 /**
234  * @brief	Set timer interval value in Hz (frequency)
235  * @param	pRITimer	: RITimer peripheral selected
236  * @param	freq		: timer interval value in Hz
237  * @return	None
238  * @note	Will not alter current counter value. Accuracy depends on
239  *			base clock rate. Timer enable/disable state is not changed.
240  */
241 void Chip_RIT_SetTimerIntervalHz(LPC_RITIMER_T *pRITimer, uint32_t freq);
242 
243 /**
244  * @brief	Returns base clock rate for timer
245  * @param	pRITimer	: RITimer peripheral selected
246  * @return	Value in Hz the timer is running at
247  * @note	This returned value contains the base clock the timer uses.
248  *			If you set the tick count to this return value with the
249  *			Chip_RIT_SetCompareValue() function, you will get a 1Hz
250  *			interval rate.
251  */
Chip_RIT_GetBaseClock(LPC_RITIMER_T * pRITimer)252 STATIC INLINE uint32_t Chip_RIT_GetBaseClock(LPC_RITIMER_T *pRITimer)
253 {
254 	return Chip_Clock_GetSystemClockRate();
255 }
256 
257 /**
258  * @}
259  */
260 
261 #ifdef __cplusplus
262 }
263 #endif
264 
265 #endif /* __RITIMER_15XX_H_ */
266