iLLD_TC27xC  1.0
IfxStm.h
Go to the documentation of this file.
1 /**
2  * \file IfxStm.h
3  * \brief STM basic functionality
4  * \ingroup IfxLld_Stm
5  *
6  * \version iLLD_0_1_0_10
7  * \copyright Copyright (c) 2013 Infineon Technologies AG. All rights reserved.
8  *
9  *
10  * IMPORTANT NOTICE
11  *
12  *
13  * Infineon Technologies AG (Infineon) is supplying this file for use
14  * exclusively with Infineon's microcontroller products. This file can be freely
15  * distributed within development tools that are supporting such microcontroller
16  * products.
17  *
18  * THIS SOFTWARE IS PROVIDED "AS IS". NO WARRANTIES, WHETHER EXPRESS, IMPLIED
19  * OR STATUTORY, INCLUDING, BUT NOT LIMITED TO, IMPLIED WARRANTIES OF
20  * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE APPLY TO THIS SOFTWARE.
21  * INFINEON SHALL NOT, IN ANY CIRCUMSTANCES, BE LIABLE FOR SPECIAL, INCIDENTAL,
22  * OR CONSEQUENTIAL DAMAGES, FOR ANY REASON WHATSOEVER.
23  *
24  * \defgroup IfxLld_Stm_Usage How to use the Stm driver?
25  * \ingroup IfxLld_Stm
26  *
27  * The Stm Standard driver provides APIs to initialize, configure and control the Stm.
28  *
29  * In the following sections it will be described, how to integrate the driver into the application framework.
30  *
31  * \section IfxLld_Stm_Preparation Preparation
32  * \subsection IfxLld_Stm_Include Include Files
33  *
34  * Include following header file into your C code:
35  * \code
36  * #include <Stm/Std/IfxStm.h>
37  * \endcode
38  *
39  * \subsection IfxLld_Stm_Variables Variables
40  *
41  * Declare STM variables :
42  * \code
43  * Ifx_STM *stmSfr;
44  * IfxStm_CompareConfig stmConfig;
45  * \endcode
46  *
47  * \subsection IfxLld_Stm_Interrupt Interrupt Handler Installation
48  *
49  * See also \ref IfxLld_Cpu_Interrupt_Usage
50  *
51  * Define priorities for the Interrrupt handlers. This is normally done in the Ifx_IntPrioDef.h file:
52  * \code
53  * // priorities are normally defined in Ifx_IntPrioDef.h
54  * #define IFX_INTPRIO_STM0_SR0 100
55  * \endcode
56  *
57  * Add the interrupt service routines to your C code. They have to call the Stm interrupt handlers:
58  * \code
59  * IFX_INTERRUPT(stm0Sr0ISR, 0, IFX_INTPRIO_STM0_SR0)
60  * {
61  * IfxStm_clearCompareFlag(stmSfr, stmConfig.comparator);
62  * IfxStm_updateCompare(stmSfr, stmConfig.comparator, IfxStm_getLower(stmSfr) + stmConfig.ticks);
63  * }
64  * \endcode
65  *
66  * Finally install the interrupt handlers in your initialisation function:
67  * \code
68  * // install interrupt handlers
69  * IfxCpu_Irq_installInterruptHandler(&stm0Sr0ISR, IFX_INTPRIO_STM0_SR0);
70  * IfxCpu_enableInterrupts();
71  * \endcode
72  *
73  * \subsection IfxLld_Stm_Init Module Initialisation
74  *
75  * The module initialisation can be done in the same function.
76  *
77  * \code
78  * stmSfr = &MODULE_STM0;
79  *
80  * IfxStm_initCompareConfig(&stmConfig);
81  *
82  * stmConfig.triggerInterruptEnabled = IFX_INTPRIO_STM0_SR0;
83  * stmConfig.servProvider = IfxSrc_Tos_cpu0;
84  * sint32 Fsys = IfxStm_getFrequency(stmSfr);
85  * TimeConst_10ms = Fsys / (1000 / 10);
86  * // configure to generate interrupt every 10 ms
87  * stmConfig.ticks = IfxStm_getLower(stmSfr) + TimeConst_10ms;
88  * IfxStm_initCompare(stmSfr, &stmConfig);
89  *
90  * \endcode
91  *
92  * Now the Stm shall generate interrupts regularly based on the configured time !
93  *
94  * \defgroup IfxLld_Stm STM
95  * \ingroup IfxLld
96  * \defgroup IfxLld_Stm_Std Standard Driver
97  * \ingroup IfxLld_Stm
98  * \defgroup IfxLld_Stm_Std_Enumerations Enumerations
99  * \ingroup IfxLld_Stm_Std
100  * \defgroup IfxLld_Stm_Std_Structures Data Structures
101  * \ingroup IfxLld_Stm_Std
102  * \defgroup IfxLld_Stm_Std_Module Module Functions
103  * \ingroup IfxLld_Stm_Std
104  * \defgroup IfxLld_Stm_Std_Timer Timer Functions
105  * \ingroup IfxLld_Stm_Std
106  * \defgroup IfxLld_Stm_Std_Comparator Comparator Functions
107  * \ingroup IfxLld_Stm_Std
108  */
109 
110 #ifndef IFXSTM_H
111 #define IFXSTM_H 1
112 
113 /******************************************************************************/
114 /*----------------------------------Includes----------------------------------*/
115 /******************************************************************************/
116 
117 #include "_Impl/IfxStm_cfg.h"
118 
119 /******************************************************************************/
120 /*--------------------------------Enumerations--------------------------------*/
121 /******************************************************************************/
122 
123 /** \addtogroup IfxLld_Stm_Std_Enumerations
124  * \{ */
125 /** \brief Comparator Id defined in MODULE_STMx.ISCR.B.CMP0IRR(x = 0, 1, 2)
126  */
127 typedef enum
128 {
129  IfxStm_Comparator_0 = 0, /**< \brief Comparator Id 0 */
130  IfxStm_Comparator_1, /**< \brief Comparator Id 1 */
132 
133 /** \brief Comparator Interrupt request source defined in MODULE_SRC.STM.STM[index].SRx (x =0, 1)
134  */
135 typedef enum
136 {
137  IfxStm_ComparatorInterrupt_ir0 = 0, /**< \brief Select STMIR0 */
138  IfxStm_ComparatorInterrupt_ir1 = 1 /**< \brief Select STMIR1 */
140 
141 /** \brief Comparator start bit position defined in MODULE_STMx.CMCON.B.MSTART0(x = 0,1,2)
142  */
143 typedef enum
144 {
145  IfxStm_ComparatorOffset_0 = 0, /**< \brief Comparator start bit position 0 with 64 bit timer */
146  IfxStm_ComparatorOffset_1, /**< \brief Comparator start bit position 1 with 64 bit timer */
147  IfxStm_ComparatorOffset_2, /**< \brief Comparator start bit position 2 with 64 bit timer */
148  IfxStm_ComparatorOffset_3, /**< \brief Comparator start bit position 3 with 64 bit timer */
149  IfxStm_ComparatorOffset_4, /**< \brief Comparator start bit position 4 with 64 bit timer */
150  IfxStm_ComparatorOffset_5, /**< \brief Comparator start bit position 5 with 64 bit timer */
151  IfxStm_ComparatorOffset_6, /**< \brief Comparator start bit position 6 with 64 bit timer */
152  IfxStm_ComparatorOffset_7, /**< \brief Comparator start bit position 7 with 64 bit timer */
153  IfxStm_ComparatorOffset_8, /**< \brief Comparator start bit position 8 with 64 bit timer */
154  IfxStm_ComparatorOffset_9, /**< \brief Comparator start bit position 9 with 64 bit timer */
155  IfxStm_ComparatorOffset_10, /**< \brief Comparator start bit position 10 with 64 bit timer */
156  IfxStm_ComparatorOffset_11, /**< \brief Comparator start bit position 11 with 64 bit timer */
157  IfxStm_ComparatorOffset_12, /**< \brief Comparator start bit position 12 with 64 bit timer */
158  IfxStm_ComparatorOffset_13, /**< \brief Comparator start bit position 13 with 64 bit timer */
159  IfxStm_ComparatorOffset_14, /**< \brief Comparator start bit position 14 with 64 bit timer */
160  IfxStm_ComparatorOffset_15, /**< \brief Comparator start bit position 15 with 64 bit timer */
161  IfxStm_ComparatorOffset_16, /**< \brief Comparator start bit position 16 with 64 bit timer */
162  IfxStm_ComparatorOffset_17, /**< \brief Comparator start bit position 17 with 64 bit timer */
163  IfxStm_ComparatorOffset_18, /**< \brief Comparator start bit position 18 with 64 bit timer */
164  IfxStm_ComparatorOffset_19, /**< \brief Comparator start bit position 19 with 64 bit timer */
165  IfxStm_ComparatorOffset_20, /**< \brief Comparator start bit position 20 with 64 bit timer */
166  IfxStm_ComparatorOffset_21, /**< \brief Comparator start bit position 21 with 64 bit timer */
167  IfxStm_ComparatorOffset_22, /**< \brief Comparator start bit position 22 with 64 bit timer */
168  IfxStm_ComparatorOffset_23, /**< \brief Comparator start bit position 23 with 64 bit timer */
169  IfxStm_ComparatorOffset_24, /**< \brief Comparator start bit position 24 with 64 bit timer */
170  IfxStm_ComparatorOffset_25, /**< \brief Comparator start bit position 25 with 64 bit timer */
171  IfxStm_ComparatorOffset_26, /**< \brief Comparator start bit position 26 with 64 bit timer */
172  IfxStm_ComparatorOffset_27, /**< \brief Comparator start bit position 27 with 64 bit timer */
173  IfxStm_ComparatorOffset_28, /**< \brief Comparator start bit position 28 with 64 bit timer */
174  IfxStm_ComparatorOffset_29, /**< \brief Comparator start bit position 29 with 64 bit timer */
175  IfxStm_ComparatorOffset_30, /**< \brief Comparator start bit position 30 with 64 bit timer */
176  IfxStm_ComparatorOffset_31, /**< \brief Comparator start bit position 31 with 64 bit timer */
178 
179 /** \brief Size of compare value to compare with timer defined in MODULE_STMx.CMCON.B.MSIZE0(x = 0,1,2)
180  */
181 typedef enum
182 {
183  IfxStm_ComparatorSize_1Bit = 0, /**< \brief Size of compare value to compare with timer: 1 bit */
184  IfxStm_ComparatorSize_2Bits = 1, /**< \brief Size of compare value to compare with timer: 2 bits */
185  IfxStm_ComparatorSize_3Bits = 2, /**< \brief Size of compare value to compare with timer: 3 bits */
186  IfxStm_ComparatorSize_4Bits = 3, /**< \brief Size of compare value to compare with timer: 4 bits */
187  IfxStm_ComparatorSize_5Bits = 4, /**< \brief Size of compare value to compare with timer: 5 bits */
188  IfxStm_ComparatorSize_6Bits = 5, /**< \brief Size of compare value to compare with timer: 6 bits */
189  IfxStm_ComparatorSize_7Bits = 6, /**< \brief Size of compare value to compare with timer: 7 bits */
190  IfxStm_ComparatorSize_8Bits = 7, /**< \brief Size of compare value to compare with timer: 8 bits */
191  IfxStm_ComparatorSize_9Bits = 8, /**< \brief Size of compare value to compare with timer: 9 bits */
192  IfxStm_ComparatorSize_10Bits = 9, /**< \brief Size of compare value to compare with timer: 10 bits */
193  IfxStm_ComparatorSize_11Bits = 10, /**< \brief Size of compare value to compare with timer: 11 bits */
194  IfxStm_ComparatorSize_12Bits = 11, /**< \brief Size of compare value to compare with timer: 12 bits */
195  IfxStm_ComparatorSize_13Bits = 12, /**< \brief Size of compare value to compare with timer: 13 bits */
196  IfxStm_ComparatorSize_14Bits = 13, /**< \brief Size of compare value to compare with timer: 14 bits */
197  IfxStm_ComparatorSize_15Bits = 14, /**< \brief Size of compare value to compare with timer: 15 bits */
198  IfxStm_ComparatorSize_16Bits = 15, /**< \brief Size of compare value to compare with timer: 16 bits */
199  IfxStm_ComparatorSize_17Bits = 16, /**< \brief Size of compare value to compare with timer: 17 bits */
200  IfxStm_ComparatorSize_18Bits = 17, /**< \brief Size of compare value to compare with timer: 18 bits */
201  IfxStm_ComparatorSize_19Bits = 18, /**< \brief Size of compare value to compare with timer: 19 bits */
202  IfxStm_ComparatorSize_20Bits = 19, /**< \brief Size of compare value to compare with timer: 20 bits */
203  IfxStm_ComparatorSize_21Bits = 20, /**< \brief Size of compare value to compare with timer: 21 bits */
204  IfxStm_ComparatorSize_22Bits = 21, /**< \brief Size of compare value to compare with timer: 22 bits */
205  IfxStm_ComparatorSize_23Bits = 22, /**< \brief Size of compare value to compare with timer: 23 bits */
206  IfxStm_ComparatorSize_24Bits = 23, /**< \brief Size of compare value to compare with timer: 24 bits */
207  IfxStm_ComparatorSize_25Bits = 24, /**< \brief Size of compare value to compare with timer: 25 bits */
208  IfxStm_ComparatorSize_26Bits = 25, /**< \brief Size of compare value to compare with timer: 26 bits */
209  IfxStm_ComparatorSize_27Bits = 26, /**< \brief Size of compare value to compare with timer: 27 bits */
210  IfxStm_ComparatorSize_28Bits = 27, /**< \brief Size of compare value to compare with timer: 28 bits */
211  IfxStm_ComparatorSize_29Bits = 28, /**< \brief Size of compare value to compare with timer: 29 bits */
212  IfxStm_ComparatorSize_30Bits = 29, /**< \brief Size of compare value to compare with timer: 30 bits */
213  IfxStm_ComparatorSize_31Bits = 30, /**< \brief Size of compare value to compare with timer: 31 bits */
214  IfxStm_ComparatorSize_32Bits = 31 /**< \brief Size of compare value to compare with timer: 32 bits */
216 
217 /** \} */
218 
219 /******************************************************************************/
220 /*-----------------------------Data Structures--------------------------------*/
221 /******************************************************************************/
222 
223 /** \addtogroup IfxLld_Stm_Std_Structures
224  * \{ */
225 /** \brief Comparator Configuration Structure
226  */
227 typedef struct
228 {
229  Ifx_Priority triggerInterruptEnabled; /**< \brief Interrupt priority. Range = 0 .. 255. 0 = interrupt is disabled. */
230  IfxStm_ComparatorInterrupt comparatorInterrupt; /**< \brief Comparator Interrupt request source defined in MODULE_SRC.STM.STM[index].SRx (x =0, 1). */
231  IfxStm_Comparator comparator; /**< \brief Comparator Id defined in MODULE_STMx.ISCR.B.CMP0IRR(x = 0, 1, 2). */
232  IfxStm_ComparatorOffset compareOffset; /**< \brief Comparator start bit position defined in MODULE_STMx.CMCON.B.MSTART0(x = 0,1,2). */
233  IfxStm_ComparatorSize compareSize; /**< \brief Size of compare value to compare with timer defined in MODULE_STMx.CMCON.B.MSIZE0(x = 0,1,2). */
234  uint32 ticks; /**< \brief count for next comparison from current timer count. */
235  IfxSrc_Tos servProvider; /**< \brief Type of service. */
237 
238 /** \} */
239 
240 /** \addtogroup IfxLld_Stm_Std_Module
241  * \{ */
242 
243 /******************************************************************************/
244 /*-------------------------Inline Function Prototypes-------------------------*/
245 /******************************************************************************/
246 
247 /** \brief Returns system timer value.
248  * \param stm pointer to System timer module registers.
249  * \return system timer value.
250  */
251 IFX_INLINE uint64 IfxStm_get(Ifx_STM *stm);
252 
253 /** \brief Returns the system timer frequency.
254  * \param stm pointer to System timer module registers.
255  * \return the system timer frequency in Hz.
256  */
258 
259 /******************************************************************************/
260 /*-------------------------Global Function Prototypes-------------------------*/
261 /******************************************************************************/
262 
263 /** \brief enable suspend by debugger.
264  * \param stm pointer to System timer module registers.
265  * \return None
266  */
267 IFX_EXTERN void IfxStm_enableOcdsSuspend(Ifx_STM *stm);
268 
269 /** \brief API to get the resource index of the STM specified.
270  * \param stm pointer to System timer module registers.
271  * \return system timer resource index.
272  */
274 
275 /** \} */
276 
277 /** \addtogroup IfxLld_Stm_Std_Timer
278  * \{ */
279 
280 /******************************************************************************/
281 /*-------------------------Inline Function Prototypes-------------------------*/
282 /******************************************************************************/
283 
284 /** \brief Returns the lower system timer value.
285  * \param stm pointer to System timer module registers.
286  * \return the lower system timer value.
287  */
288 IFX_INLINE uint32 IfxStm_getLower(Ifx_STM *stm);
289 
290 /** \brief Gets the TIM3 couter value.
291  * \param stm pointer to System timer module registers.
292  * \return TIM3 counter value.
293  */
295 
296 /** \brief Gets the TIM4 couter value.
297  * \param stm pointer to System timer module registers.
298  * \return TIM4 counter value.
299  */
301 
302 /** \brief Gets the TIM5 couter value.
303  * \param stm pointer to System timer module registers.
304  * \return TIM5 counter value.
305  */
307 
308 /** \brief Gets the TIM6 couter value.
309  * \param stm pointer to System timer module registers.
310  * \return TIM6 counter value.
311  */
313 
314 /** \brief Gets the TIM1 couter value.
315  * \param stm pointer to System timer module registers.
316  * \return TIM1 counter value.
317  */
319 
320 /** \brief Gets the TIM2 couter value.
321  * \param stm pointer to System timer module registers.
322  * \return TIM2 counter value.
323  */
325 
326 /** \brief Returns the timer value shifted right by offset.
327  * \param stm pointer to System timer module registers.
328  * \param offset offset value.
329  * \return the lower system timer value shifted by offset.
330  */
331 IFX_INLINE uint32 IfxStm_getOffsetTimer(Ifx_STM *stm, uint8 offset);
332 
333 /** \brief Wait for requested time.
334  * The macro waits in while loop for the specified time in system timer ticks.
335  * \param stm pointer to System timer module registers.
336  * \param ticks ticks Wait time in system timer ticks.
337  * \return None
338  */
339 IFX_INLINE void IfxStm_waitTicks(Ifx_STM *stm, uint32 ticks);
340 
341 /** \} */
342 
343 /** \addtogroup IfxLld_Stm_Std_Comparator
344  * \{ */
345 
346 /******************************************************************************/
347 /*-------------------------Inline Function Prototypes-------------------------*/
348 /******************************************************************************/
349 
350 /** \brief Returns the updated compare register value.
351  * \param stm pointer to System timer module registers.
352  * \param comparator comparator selection comparator.
353  * \return The compare value
354  */
355 IFX_INLINE uint32 IfxStm_getCompare(Ifx_STM *stm, IfxStm_Comparator comparator);
356 
357 /** \brief Update the compare register value increased with given ticks.
358  * \param stm pointer to System timer module registers.
359  * \param comparator comparator selection comparator.
360  * \param ticks count for next comparison from current timer count.
361  * \return None
362  */
363 IFX_INLINE void IfxStm_increaseCompare(Ifx_STM *stm, IfxStm_Comparator comparator, uint32 ticks);
364 
365 /** \brief Update the compare register value.
366  * \param stm pointer to System timer module registers.
367  * \param comparator comparator selection comparator.
368  * \param ticks count for next comparison.
369  * \return None
370  */
371 IFX_INLINE void IfxStm_updateCompare(Ifx_STM *stm, IfxStm_Comparator comparator, uint32 ticks);
372 
373 /******************************************************************************/
374 /*-------------------------Global Function Prototypes-------------------------*/
375 /******************************************************************************/
376 
377 /** \brief Clear the compare interrupt flag.
378  * \param stm pointer to System timer module registers.
379  * \param comparator comparator selection comparator.
380  * \return None
381  */
382 IFX_EXTERN void IfxStm_clearCompareFlag(Ifx_STM *stm, IfxStm_Comparator comparator);
383 
384 /** \brief Initialise stm compare register.
385  * \param stm pointer to System timer module registers.
386  * \param config pointer to configuration structure.
387  * \return TRUE if Comparator successful otherwise FLASE.
388  */
389 IFX_EXTERN boolean IfxStm_initCompare(Ifx_STM *stm, IfxStm_CompareConfig *config);
390 
391 /** \brief Initialise compare configuration with default values.
392  * \param config pointer to configuration structure.
393  * \return None
394  */
396 
397 /** \} */
398 
399 /******************************************************************************/
400 /*---------------------Inline Function Implementations------------------------*/
401 /******************************************************************************/
402 
404 {
405  uint64 result;
406 
407  result = stm->TIM0.U;
408  result |= ((uint64)stm->CAP.U) << 32;
409 
410  return result;
411 }
412 
413 
415 {
416  float32 result;
417 
418  result = IfxScuCcu_getStmFrequency();
419 
420  return result;
421 }
422 
423 
425 {
426  return stm->TIM0.U;
427 }
428 
429 
431 {
432  return stm->TIM3.U;
433 }
434 
435 
437 {
438  return stm->TIM4.U;
439 }
440 
441 
443 {
444  return stm->TIM5.U;
445 }
446 
447 
449 {
450  return stm->TIM6.U;
451 }
452 
453 
455 {
456  return stm->TIM1.U;
457 }
458 
459 
461 {
462  return stm->TIM2.U;
463 }
464 
465 
467 {
468  uint64 now;
469 
470  now = IfxStm_get(stm);
471 
472  return (uint32)(now >> offset);
473 }
474 
475 
476 IFX_INLINE void IfxStm_waitTicks(Ifx_STM *stm, uint32 ticks)
477 {
478  uint32 beginTime;
479 
480  beginTime = IfxStm_getLower(stm);
481 
482  /*below code will work because of unsigned 32 bit calculation even at timer wrapping condition
483  * As an example if beginTime = 0xFFFFFFFE and current time = 2 (after overflow), unsigned calculation
484  * 2 - 0xFFFFFFFE will be 4*/
485  while ((IfxStm_getLower(stm) - beginTime) < ticks)
486  {}
487 }
488 
489 
491 {
492  return stm->CMP[comparator].B.CMPVAL;
493 }
494 
495 
496 IFX_INLINE void IfxStm_increaseCompare(Ifx_STM *stm, IfxStm_Comparator comparator, uint32 ticks)
497 {
498  stm->CMP[comparator].B.CMPVAL = stm->CMP[comparator].B.CMPVAL + ticks;
499 }
500 
501 
502 IFX_INLINE void IfxStm_updateCompare(Ifx_STM *stm, IfxStm_Comparator comparator, uint32 ticks)
503 {
504  stm->CMP[comparator].B.CMPVAL = ticks;
505 }
506 
507 
508 #endif /* IFXSTM_H */