iLLD_TC27xD  1.0
IfxGtm_Tom.c
Go to the documentation of this file.
1 /**
2  * \file IfxGtm_Tom.c
3  * \brief GTM basic functionality
4  *
5  * \version iLLD_1_0_0_11_0
6  * \copyright Copyright (c) 2013 Infineon Technologies AG. All rights reserved.
7  *
8  *
9  * IMPORTANT NOTICE
10  *
11  *
12  * Infineon Technologies AG (Infineon) is supplying this file for use
13  * exclusively with Infineon's microcontroller products. This file can be freely
14  * distributed within development tools that are supporting such microcontroller
15  * products.
16  *
17  * THIS SOFTWARE IS PROVIDED "AS IS". NO WARRANTIES, WHETHER EXPRESS, IMPLIED
18  * OR STATUTORY, INCLUDING, BUT NOT LIMITED TO, IMPLIED WARRANTIES OF
19  * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE APPLY TO THIS SOFTWARE.
20  * INFINEON SHALL NOT, IN ANY CIRCUMSTANCES, BE LIABLE FOR SPECIAL, INCIDENTAL,
21  * OR CONSEQUENTIAL DAMAGES, FOR ANY REASON WHATSOEVER.
22  *
23  */
24 
25 /******************************************************************************/
26 /*----------------------------------Includes----------------------------------*/
27 /******************************************************************************/
28 
29 #include "IfxGtm_Tom.h"
30 #include "IfxGtm_bf.h"
31 #include "IfxGtm_Cmu.h"
32 
33 /******************************************************************************/
34 /*----------------------------------Macros------------------------------------*/
35 /******************************************************************************/
36 
37 /** \brief Number of channels per TOM TGC
38  */
39 #define IFXGTM_TOM_NUM_TGC_CHANNELS (8)
40 
41 /******************************************************************************/
42 /*-------------------------Function Implementations---------------------------*/
43 /******************************************************************************/
44 
45 void IfxGtm_Tom_Ch_clearOneNotification(Ifx_GTM_TOM *tom, IfxGtm_Tom_Ch channel)
46 {
47  Ifx_GTM_TOM_CH *tomCh = IfxGtm_Tom_Ch_getChannelPointer(tom, channel);
48  tomCh->IRQ_NOTIFY.B.CCU1TC = 1;
49 }
50 
51 
52 void IfxGtm_Tom_Ch_clearZeroNotification(Ifx_GTM_TOM *tom, IfxGtm_Tom_Ch channel)
53 {
54  Ifx_GTM_TOM_CH *tomCh = IfxGtm_Tom_Ch_getChannelPointer(tom, channel);
55 
56  tomCh->IRQ_NOTIFY.B.CCU0TC = 1;
57 }
58 
59 
60 float32 IfxGtm_Tom_Ch_getClockFrequency(Ifx_GTM *gtm, Ifx_GTM_TOM *tom, IfxGtm_Tom_Ch channel)
61 {
63  IfxGtm_Cmu_Fxclk clkIndex[5] = {
64  IfxGtm_Cmu_Fxclk_0, /*IfxGtm_Tom_Ch_ClkSrc_cmuFxclk0 */
65  IfxGtm_Cmu_Fxclk_1, /*IfxGtm_Tom_Ch_ClkSrc_cmuFxclk1 */
66  IfxGtm_Cmu_Fxclk_2, /*IfxGtm_Tom_Ch_ClkSrc_cmuFxclk2 */
67  IfxGtm_Cmu_Fxclk_3, /*IfxGtm_Tom_Ch_ClkSrc_cmuFxclk3 */
68  IfxGtm_Cmu_Fxclk_4 /*IfxGtm_Tom_Ch_ClkSrc_cmuFxclk4 */
69  };
70 
71  clock = IfxGtm_Tom_Ch_getClockSource(tom, channel);
72 
73  if (clock == IfxGtm_Tom_Ch_ClkSrc_noClock)
74  {
75  return 0.0;
76  }
77  else
78  {
79  return IfxGtm_Cmu_getFxClkFrequency(gtm, clkIndex[clock], TRUE);
80  }
81 }
82 
83 
85 {
87 
88  Ifx_GTM_TOM_CH *tomCh = IfxGtm_Tom_Ch_getChannelPointer(tom, channel);
89 
90  clock = tomCh->CTRL.B.CLK_SRC_SR;
91 
92  return clock;
93 }
94 
95 
97 {
98  Ifx_GTM_TOM_CH *tomCh = IfxGtm_Tom_Ch_getChannelPointer(tom, channel);
99 
100  return tomCh->CM1.U;
101 }
102 
103 
104 volatile uint32 *IfxGtm_Tom_Ch_getCompareOnePointer(Ifx_GTM_TOM *tom, IfxGtm_Tom_Ch channel)
105 {
106  Ifx_GTM_TOM_CH *tomCh = IfxGtm_Tom_Ch_getChannelPointer(tom, channel);
107 
108  return (volatile uint32 *)&tomCh->CM1.U;
109 }
110 
111 
113 {
114  Ifx_GTM_TOM_CH *tomCh = IfxGtm_Tom_Ch_getChannelPointer(tom, channel);
115 
116  return tomCh->CM0.U;
117 }
118 
119 
120 volatile uint32 *IfxGtm_Tom_Ch_getCompareZeroPointer(Ifx_GTM_TOM *tom, IfxGtm_Tom_Ch channel)
121 {
122  Ifx_GTM_TOM_CH *tomCh = IfxGtm_Tom_Ch_getChannelPointer(tom, channel);
123  return (volatile uint32 *)&tomCh->CM0.U;
124 }
125 
126 
127 boolean IfxGtm_Tom_Ch_getOutputLevel(Ifx_GTM_TOM *tom, IfxGtm_Tom_Ch channel)
128 {
129  boolean result;
130  Ifx_GTM_TOM_CH *tomCh = IfxGtm_Tom_Ch_getChannelPointer(tom, channel);
131 
132  result = tomCh->STAT.B.OL == 1;
133 
134  return result;
135 }
136 
137 
138 volatile Ifx_SRC_SRCR *IfxGtm_Tom_Ch_getSrcPointer(Ifx_GTM *gtm, IfxGtm_Tom tom, IfxGtm_Tom_Ch channel)
139 {
140  return &MODULE_SRC.GTM.GTM[0].TOM[tom][channel / 2];
141 }
142 
143 
145 {
146  Ifx_GTM_TOM_TGC *pointer;
147 
148  if (tgcIndex == 0)
149  {
150  pointer = (Ifx_GTM_TOM_TGC *)&tom->TGC0_GLB_CTRL;
151  }
152  else
153  {
154  pointer = (Ifx_GTM_TOM_TGC *)&tom->TGC1_GLB_CTRL;
155  }
156 
157  return pointer;
158 }
159 
160 
161 volatile uint32 *IfxGtm_Tom_Ch_getTimerPointer(Ifx_GTM_TOM *tom, IfxGtm_Tom_Ch channel)
162 {
163  Ifx_GTM_TOM_CH *tomCh = IfxGtm_Tom_Ch_getChannelPointer(tom, channel);
164  return (volatile uint32 *)&tomCh->CN0.U;
165 }
166 
167 
168 boolean IfxGtm_Tom_Ch_isOneNotification(Ifx_GTM_TOM *tom, IfxGtm_Tom_Ch channel)
169 {
170  boolean result;
171  Ifx_GTM_TOM_CH *tomCh = IfxGtm_Tom_Ch_getChannelPointer(tom, channel);
172 
173  result = tomCh->IRQ_NOTIFY.B.CCU1TC != 0;
174 
175  return result;
176 }
177 
178 
179 boolean IfxGtm_Tom_Ch_isZeroNotification(Ifx_GTM_TOM *tom, IfxGtm_Tom_Ch channel)
180 {
181  boolean result;
182  Ifx_GTM_TOM_CH *tomCh = IfxGtm_Tom_Ch_getChannelPointer(tom, channel);
183 
184  result = tomCh->IRQ_NOTIFY.B.CCU0TC != 0;
185 
186  return result;
187 }
188 
189 
190 void IfxGtm_Tom_Ch_raiseInterruptOne(Ifx_GTM_TOM *tom, IfxGtm_Tom_Ch channel)
191 {
192  Ifx_GTM_TOM_CH *tomCh = IfxGtm_Tom_Ch_getChannelPointer(tom, channel);
193 
194  tomCh->IRQ_FORCINT.B.TRG_CCU1TC0 = 1;
195 }
196 
197 
198 void IfxGtm_Tom_Ch_raiseInterruptZero(Ifx_GTM_TOM *tom, IfxGtm_Tom_Ch channel)
199 {
200  Ifx_GTM_TOM_CH *tomCh = IfxGtm_Tom_Ch_getChannelPointer(tom, channel);
201 
202  tomCh->IRQ_FORCINT.B.TRG_CCU0TC0 = 1;
203 }
204 
205 
207 {
208  Ifx_GTM_TOM_CH *tomCh = IfxGtm_Tom_Ch_getChannelPointer(tom, channel);
209 
210  tomCh->CTRL.B.CLK_SRC_SR = clock;
211 }
212 
213 
214 void IfxGtm_Tom_Ch_setCompare(Ifx_GTM_TOM *tom, IfxGtm_Tom_Ch channel, uint32 compareZero, uint32 compareOne)
215 {
216  Ifx_GTM_TOM_CH *tomCh = IfxGtm_Tom_Ch_getChannelPointer(tom, channel);
217 
218  tomCh->CM0.U = compareZero; // TK: replaced .B access to optimize runtime
219  tomCh->CM1.U = compareOne;
220 }
221 
222 
223 void IfxGtm_Tom_Ch_setCompareOne(Ifx_GTM_TOM *tom, IfxGtm_Tom_Ch channel, uint32 compareOne)
224 {
225  Ifx_GTM_TOM_CH *tomCh = IfxGtm_Tom_Ch_getChannelPointer(tom, channel);
226 
227  tomCh->CM1.U = compareOne; // TK: replaced .B access to optimize runtime
228 }
229 
230 
231 void IfxGtm_Tom_Ch_setCompareOneShadow(Ifx_GTM_TOM *tom, IfxGtm_Tom_Ch channel, uint32 shadowOne)
232 {
233  Ifx_GTM_TOM_CH *tomCh = IfxGtm_Tom_Ch_getChannelPointer(tom, channel);
234 
235  tomCh->SR1.U = shadowOne; // TK: replaced .B access to optimize runtime
236 }
237 
238 
239 void IfxGtm_Tom_Ch_setCompareShadow(Ifx_GTM_TOM *tom, IfxGtm_Tom_Ch channel, uint32 shadowZero, uint32 shadowOne)
240 {
241  Ifx_GTM_TOM_CH *tomCh = IfxGtm_Tom_Ch_getChannelPointer(tom, channel);
242 
243  tomCh->SR0.U = shadowZero; // TK: replaced .B access to optimize runtime
244  tomCh->SR1.U = shadowOne;
245 }
246 
247 
248 void IfxGtm_Tom_Ch_setCompareZero(Ifx_GTM_TOM *tom, IfxGtm_Tom_Ch channel, uint32 compareZero)
249 {
250  Ifx_GTM_TOM_CH *tomCh = IfxGtm_Tom_Ch_getChannelPointer(tom, channel);
251 
252  tomCh->CM0.U = compareZero; // TK: replaced .B access to optimize runtime
253 }
254 
255 
256 void IfxGtm_Tom_Ch_setCompareZeroShadow(Ifx_GTM_TOM *tom, IfxGtm_Tom_Ch channel, uint32 shadowZero)
257 {
258  Ifx_GTM_TOM_CH *tomCh = IfxGtm_Tom_Ch_getChannelPointer(tom, channel);
259 
260  tomCh->SR0.U = shadowZero; // TK: replaced .B access to optimize runtime
261 }
262 
263 
264 void IfxGtm_Tom_Ch_setCounterValue(Ifx_GTM_TOM *tom, IfxGtm_Tom_Ch channel, uint32 value)
265 {
266  Ifx_GTM_TOM_CH *tomCh = IfxGtm_Tom_Ch_getChannelPointer(tom, channel);
267 
268  tomCh->CN0.U = value; // TK: replaced .B access to optimize runtime
269 }
270 
271 
272 void IfxGtm_Tom_Ch_setGatedCounter(Ifx_GTM_TOM *tom, IfxGtm_Tom_Ch channel, boolean enabled)
273 {
274  if (channel <= IfxGtm_Tom_Ch_7)
275  {
276  Ifx_GTM_TOM_CH *tomCh = IfxGtm_Tom_Ch_getChannelPointer(tom, channel);
277 
278  tomCh->CTRL.B.GCM = enabled ? 1 : 0;
279  }
280 }
281 
282 
283 void IfxGtm_Tom_Ch_setNotification(Ifx_GTM_TOM *tom, IfxGtm_Tom_Ch channel, IfxGtm_IrqMode mode, boolean interruptOnCompareZero, boolean interruptOnCompareOne)
284 {
285  Ifx_GTM_TOM_CH *tomCh = IfxGtm_Tom_Ch_getChannelPointer(tom, channel);
286 
287  Ifx_GTM_TOM_CH_IRQ_EN en;
288  en.U = tomCh->IRQ_EN.U;
289 
290  /* Disable all interrupts of the interrupt set to change mode */
291  tomCh->IRQ_EN.U = IFX_ZEROS;
292  tomCh->IRQ_MODE.B.IRQ_MODE = mode;
293  tomCh->IRQ_EN.U = en.U; /* Set the values back */
294 
295  en.B.CCU0TC_IRQ_EN = interruptOnCompareZero ? 1 : 0;
296  en.B.CCU1TC_IRQ_EN = interruptOnCompareOne ? 1 : 0;
297  tomCh->IRQ_EN.U = en.U;
298 }
299 
300 
301 void IfxGtm_Tom_Ch_setOneShotMode(Ifx_GTM_TOM *tom, IfxGtm_Tom_Ch channel, boolean enabled)
302 {
303  Ifx_GTM_TOM_CH *tomCh = IfxGtm_Tom_Ch_getChannelPointer(tom, channel);
304 
305  tomCh->CTRL.B.OSM = enabled ? 1 : 0;
306 }
307 
308 
309 void IfxGtm_Tom_Ch_setPcm(Ifx_GTM_TOM *tom, IfxGtm_Tom_Ch channel, boolean enabled)
310 {
311  if (channel == IfxGtm_Tom_Ch_15)
312  {
313  tom->CH15.CTRL.B.BITREV = enabled ? 1 : 0; /* FIXME check when datasheet updated, currently not specified */
314  }
315 }
316 
317 
319 {
320  Ifx_GTM_TOM_CH *tomCh = IfxGtm_Tom_Ch_getChannelPointer(tom, channel);
321 
322  tomCh->CTRL.B.RST_CCU0 = event;
323 }
324 
325 
326 void IfxGtm_Tom_Ch_setSignalLevel(Ifx_GTM_TOM *tom, IfxGtm_Tom_Ch channel, Ifx_ActiveState activeState)
327 {
328  Ifx_GTM_TOM_CH *tomCh = IfxGtm_Tom_Ch_getChannelPointer(tom, channel);
329 
330  tomCh->CTRL.B.SL = activeState == Ifx_ActiveState_high ? 1 : 0;
331 }
332 
333 
334 void IfxGtm_Tom_Ch_setSpe(Ifx_GTM_TOM *tom, IfxGtm_Tom_Ch channel, boolean enabled)
335 {
336  if (channel <= IfxGtm_Tom_Ch_7)
337  {
338  Ifx_GTM_TOM_CH *tomCh = IfxGtm_Tom_Ch_getChannelPointer(tom, channel);
339 
340  tomCh->CTRL.B.SPEM = enabled ? 1 : 0;
341  }
342 }
343 
344 
346 {
347  Ifx_GTM_TOM_CH *tomCh = IfxGtm_Tom_Ch_getChannelPointer(tom, channel);
348 
349  tomCh->CTRL.B.TRIGOUT = trigger;
350 }
351 
352 
353 uint32 IfxGtm_Tom_Tgc_buildFeature(uint16 enableMask, uint16 disableMask, uint8 bitfieldOffset)
354 {
355  uint8 i;
356  uint32 reg = 0;
357  uint32 mask = enableMask | (disableMask << 16);
358 
359  for (i = 0; i < IFXGTM_TOM_NUM_TGC_CHANNELS; i++)
360  {
361  /* Bitfield length is 2 bits */
362  uint8 shift = (i * 2) + bitfieldOffset;
363 
364  if (mask & 0x1)
365  {
366  reg |= IfxGtm_FeatureControl_enable << shift;
367  }
368 
369  if (mask & 0x10000)
370  {
371  reg |= IfxGtm_FeatureControl_disable << shift;
372  }
373 
374  mask = mask >> 1;
375  }
376 
377  return reg;
378 }
379 
380 
381 uint32 IfxGtm_Tom_Tgc_buildFeatureForChannel(IfxGtm_Tom_Ch channel, boolean enabled, uint8 bitfieldOffset)
382 {
383  uint32 reg = 0;
384 
385  /* Bitfield length is 2 bits */
386  uint8 shift = ((channel % 8) * 2) + bitfieldOffset;
387 
388  if (enabled == 1)
389  {
390  reg |= IfxGtm_FeatureControl_enable << shift;
391  }
392  else
393  {
394  reg |= IfxGtm_FeatureControl_disable << shift;
395  }
396 
397  return reg;
398 }
399 
400 
401 void IfxGtm_Tom_Tgc_enableChannel(Ifx_GTM_TOM_TGC *tgc, IfxGtm_Tom_Ch channel, boolean enabled, boolean immediate)
402 {
403  uint32 value;
404 
405  value = IfxGtm_Tom_Tgc_buildFeatureForChannel(channel, enabled, IFX_GTM_TOM_TGC0_ENDIS_CTRL_ENDIS_CTRL0_OFF);
406 
407  if (immediate)
408  {
409  tgc->ENDIS_CTRL.U |= value;
410  tgc->ENDIS_STAT.U |= value;
411  }
412  else
413  {
414  tgc->ENDIS_CTRL.U |= value;
415  }
416 }
417 
418 
419 void IfxGtm_Tom_Tgc_enableChannelOutput(Ifx_GTM_TOM_TGC *tgc, IfxGtm_Tom_Ch channel, boolean enabled, boolean immediate)
420 {
421  uint32 value;
422 
423  value = IfxGtm_Tom_Tgc_buildFeatureForChannel(channel, enabled, IFX_GTM_TOM_TGC0_OUTEN_CTRL_OUTEN_CTRL0_OFF);
424 
425  if (immediate)
426  {
427  tgc->OUTEN_CTRL.U |= value;
428  tgc->OUTEN_STAT.U |= value;
429  }
430  else
431  {
432  tgc->OUTEN_CTRL.U |= value;
433  }
434 }
435 
436 
438 {
439  tgc->GLB_CTRL.U |= IfxGtm_Tom_Tgc_buildFeatureForChannel(channel, enabled, IFX_GTM_TOM_TGC0_GLB_CTRL_UPEN_CTRL0_OFF);
440 }
441 
442 
443 void IfxGtm_Tom_Tgc_enableChannels(Ifx_GTM_TOM_TGC *tgc, uint16 enableMask, uint16 disableMask, boolean immediate)
444 {
445  uint32 value;
446 
447  /* FIXME: Alann... is it possible to optimize, not always re-calculating the value using buildFeature function ? */
448  value = IfxGtm_Tom_Tgc_buildFeature(enableMask, disableMask, IFX_GTM_TOM_TGC0_ENDIS_CTRL_ENDIS_CTRL0_OFF);
449 
450  if (immediate)
451  {
452  tgc->ENDIS_CTRL.U = value;
453  tgc->ENDIS_STAT.U = value;
454  }
455  else
456  {
457  tgc->ENDIS_CTRL.U = value;
458  }
459 }
460 
461 
462 void IfxGtm_Tom_Tgc_enableChannelsOutput(Ifx_GTM_TOM_TGC *tgc, uint16 enableMask, uint16 disableMask, boolean immediate)
463 {
464  uint32 value;
465 
466  value = IfxGtm_Tom_Tgc_buildFeature(enableMask, disableMask, IFX_GTM_TOM_TGC0_OUTEN_CTRL_OUTEN_CTRL0_OFF);
467 
468  if (immediate)
469  {
470  tgc->OUTEN_CTRL.U = value;
471  tgc->OUTEN_STAT.U = value;
472  }
473  else
474  {
475  tgc->OUTEN_CTRL.U = value;
476  }
477 }
478 
479 
481 {
482  tgc->INT_TRIG.U = IfxGtm_Tom_Tgc_buildFeature(enableMask, disableMask, IFX_GTM_TOM_TGC0_INT_TRIG_INT_TRIG0_OFF);
483 }
484 
485 
487 {
488  tgc->GLB_CTRL.U = IfxGtm_Tom_Tgc_buildFeature(enableMask, disableMask, IFX_GTM_TOM_TGC0_GLB_CTRL_UPEN_CTRL0_OFF);
489 }
490 
491 
493 {
494  tgc->ACT_TB.B.TB_TRIG = enabled ? 1 : 0;
495 }
496 
497 
499 {
500  uint8 i;
501  uint32 reg = 0;
502 
503  for (i = 0; i < IFXGTM_TOM_NUM_TGC_CHANNELS; i++)
504  {
505  if (resetMask & 0x1)
506  {
507  reg |= 1 << i;
508  }
509 
510  resetMask = resetMask >> 1;
511  }
512 
513  tgc->GLB_CTRL.U = reg << IFX_GTM_TOM_TGC0_GLB_CTRL_RST_CH0_OFF;
514 }
515 
516 
517 void IfxGtm_Tom_Tgc_setChannelForceUpdate(Ifx_GTM_TOM_TGC *tgc, IfxGtm_Tom_Ch channel, boolean enabled, boolean resetEnabled)
518 {
519  uint32 regEnable, regReset;
520 
521  regEnable = IfxGtm_Tom_Tgc_buildFeatureForChannel(channel, enabled, IFX_GTM_TOM_TGC0_FUPD_CTRL_FUPD_CTRL0_OFF);
522  regReset = IfxGtm_Tom_Tgc_buildFeatureForChannel(channel, resetEnabled, IFX_GTM_TOM_TGC0_FUPD_CTRL_RSTCN0_CH0_OFF);
523 
524  tgc->FUPD_CTRL.U |= regEnable | (regReset << 16);
525 }
526 
527 
528 void IfxGtm_Tom_Tgc_setChannelsForceUpdate(Ifx_GTM_TOM_TGC *tgc, uint16 enableMask, uint16 disableMask, uint16 resetEnableMask, uint16 resetDisableMask)
529 {
530  uint32 regEnable, regReset;
531 
532  regEnable = IfxGtm_Tom_Tgc_buildFeature(enableMask, disableMask, IFX_GTM_TOM_TGC0_FUPD_CTRL_FUPD_CTRL0_OFF);
533  regReset = IfxGtm_Tom_Tgc_buildFeature(resetEnableMask, resetDisableMask, IFX_GTM_TOM_TGC0_FUPD_CTRL_RSTCN0_CH0_OFF);
534 
535  tgc->FUPD_CTRL.U = regEnable | regReset;
536 }
537 
538 
540 {
541  Ifx_GTM_TOM_TGC0_ACT_TB act_tb;
542 
543  act_tb.U = tgc->ACT_TB.U;
544  act_tb.B.TBU_SEL = base;
545  act_tb.B.ACT_TB = value;
546  tgc->ACT_TB.U = act_tb.U;
547 }
548 
549 
551 {
552  tgc->GLB_CTRL.U = 1 << IFX_GTM_TOM_TGC0_GLB_CTRL_HOST_TRIG_OFF;
553 }