iLLD_TC27xC  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_0_1_0_10
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_TGC_CHANNEL_COUNT (8)
40 
41 /** \addtogroup IfxLld_Gtm_Std_Tom_TGC_Functions
42  * \{ */
43 
44 /******************************************************************************/
45 /*-----------------------Private Function Prototypes--------------------------*/
46 /******************************************************************************/
47 
48 /** \brief Builds the register value for the feature enable/disable
49  * \param enableMask Mask for the channel feature enable (bit 0: Channel 0, bit 1: channel 1, ...)
50  * \param disableMask Mask for the channel feature disable (bit 0: Channel 0, bit 1: channel 1, ...)
51  * \param bitfieldOffset Offset of the channel 0 bitfield in the register
52  * \return The register value
53  */
54 static uint32 IfxGtm_Tom_Tgc_buildFeature(uint16 enableMask, uint16 disableMask, uint8 bitfieldOffset);
55 
56 /** \} */
57 
58 /******************************************************************************/
59 /*-------------------------Function Implementations---------------------------*/
60 /******************************************************************************/
61 
62 void IfxGtm_Tom_Ch_clearOneNotification(Ifx_GTM_TOM *tom, IfxGtm_Tom_Ch channel)
63 {
64  Ifx_GTM_TOM_CH *tomCh = (Ifx_GTM_TOM_CH *)((uint32)&tom->CH0.CTRL.U + 0x40 * channel);
65 
66  tomCh->IRQ_NOTIFY.B.CCU1TC = 1;
67 }
68 
69 
70 void IfxGtm_Tom_Ch_clearZeroNotification(Ifx_GTM_TOM *tom, IfxGtm_Tom_Ch channel)
71 {
72  Ifx_GTM_TOM_CH *tomCh = (Ifx_GTM_TOM_CH *)((uint32)&tom->CH0.CTRL.U + 0x40 * channel);
73 
74  tomCh->IRQ_NOTIFY.B.CCU0TC = 1;
75 }
76 
77 
78 float32 IfxGtm_Tom_Ch_getClockFrequency(Ifx_GTM *gtm, Ifx_GTM_TOM *tom, IfxGtm_Tom_Ch channel)
79 {
81  IfxGtm_Cmu_Fxclk clkIndex[5] = {
82  IfxGtm_Cmu_Fxclk_0, /*IfxGtm_Tom_Ch_ClkSrc_cmuFxclk0 */
83  IfxGtm_Cmu_Fxclk_1, /*IfxGtm_Tom_Ch_ClkSrc_cmuFxclk1 */
84  IfxGtm_Cmu_Fxclk_2, /*IfxGtm_Tom_Ch_ClkSrc_cmuFxclk2 */
85  IfxGtm_Cmu_Fxclk_3, /*IfxGtm_Tom_Ch_ClkSrc_cmuFxclk3 */
86  IfxGtm_Cmu_Fxclk_4 /*IfxGtm_Tom_Ch_ClkSrc_cmuFxclk4 */
87  };
88 
89  clock = IfxGtm_Tom_Ch_getClockSource(tom, channel);
90 
91  if (clock == IfxGtm_Tom_Ch_ClkSrc_noClock)
92  {
93  return 0.0;
94  }
95  else
96  {
97  return IfxGtm_Cmu_getFxClkFrequency(gtm, clkIndex[clock], TRUE);
98  }
99 }
100 
101 
103 {
105  Ifx_GTM_TOM_CH *tomCh = (Ifx_GTM_TOM_CH *)((uint32)&tom->CH0.CTRL.U + 0x40 * channel);
106 
107  clock = tomCh->CTRL.B.CLK_SRC_SR;
108 
109  return clock;
110 }
111 
112 
113 boolean IfxGtm_Tom_Ch_getOutputLevel(Ifx_GTM_TOM *tom, IfxGtm_Tom_Ch channel)
114 {
115  boolean result;
116  Ifx_GTM_TOM_CH *tomCh = (Ifx_GTM_TOM_CH *)((uint32)&tom->CH0.CTRL.U + 0x40 * channel);
117 
118  result = tomCh->STAT.B.OL == 1;
119 
120  return result;
121 }
122 
123 
124 volatile Ifx_SRC_SRCR *IfxGtm_Tom_Ch_getSrcPointer(Ifx_GTM *gtm, IfxGtm_Tom tom, IfxGtm_Tom_Ch channel)
125 {
126  return &MODULE_SRC.GTM.GTM[0].TOM[tom][channel / 2];
127 }
128 
129 
131 {
132  Ifx_GTM_TOM_TGC *pointer;
133 
134  if (tgcIndex == 0)
135  {
136  pointer = (Ifx_GTM_TOM_TGC *)&tom->TGC0_GLB_CTRL;
137  }
138  else
139  {
140  pointer = (Ifx_GTM_TOM_TGC *)&tom->TGC1_GLB_CTRL;
141  }
142 
143  return pointer;
144 }
145 
146 
147 volatile uint32 *IfxGtm_Tom_Ch_getTimerPointer(Ifx_GTM_TOM *tom, IfxGtm_Tom_Ch channel)
148 {
149  Ifx_GTM_TOM_CH *tomCh = (Ifx_GTM_TOM_CH *)((uint32)&tom->CH0.CTRL.U + 0x40 * channel);
150  return (volatile uint32 *)&tomCh->CN0;
151 }
152 
153 
154 boolean IfxGtm_Tom_Ch_isOneNotification(Ifx_GTM_TOM *tom, IfxGtm_Tom_Ch channel)
155 {
156  boolean result;
157  Ifx_GTM_TOM_CH *tomCh = (Ifx_GTM_TOM_CH *)((uint32)&tom->CH0.CTRL.U + 0x40 * channel);
158 
159  result = tomCh->IRQ_NOTIFY.B.CCU1TC != 0;
160 
161  return result;
162 }
163 
164 
165 boolean IfxGtm_Tom_Ch_isZeroNotification(Ifx_GTM_TOM *tom, IfxGtm_Tom_Ch channel)
166 {
167  boolean result;
168  Ifx_GTM_TOM_CH *tomCh = (Ifx_GTM_TOM_CH *)((uint32)&tom->CH0.CTRL.U + 0x40 * channel);
169 
170  result = tomCh->IRQ_NOTIFY.B.CCU0TC != 0;
171 
172  return result;
173 }
174 
175 
176 void IfxGtm_Tom_Ch_raiseInterruptOne(Ifx_GTM_TOM *tom, IfxGtm_Tom_Ch channel)
177 {
178  Ifx_GTM_TOM_CH *tomCh = (Ifx_GTM_TOM_CH *)((uint32)&tom->CH0.CTRL.U + 0x40 * channel);
179 
180  tomCh->IRQ_FORCINT.B.TRG_CCU1TC0 = 1;
181 }
182 
183 
184 void IfxGtm_Tom_Ch_raiseInterruptZero(Ifx_GTM_TOM *tom, IfxGtm_Tom_Ch channel)
185 {
186  Ifx_GTM_TOM_CH *tomCh = (Ifx_GTM_TOM_CH *)((uint32)&tom->CH0.CTRL.U + 0x40 * channel);
187 
188  tomCh->IRQ_FORCINT.B.TRG_CCU0TC0 = 1;
189 }
190 
191 
193 {
194  Ifx_GTM_TOM_CH *tomCh = (Ifx_GTM_TOM_CH *)((uint32)&tom->CH0.CTRL.U + 0x40 * channel);
195 
196  tomCh->CTRL.B.CLK_SRC_SR = clock;
197 }
198 
199 
200 void IfxGtm_Tom_Ch_setCompare(Ifx_GTM_TOM *tom, IfxGtm_Tom_Ch channel, uint32 compareZero, uint32 compareOne)
201 {
202  Ifx_GTM_TOM_CH *tomCh = (Ifx_GTM_TOM_CH *)((uint32)&tom->CH0.CTRL.U + 0x40 * channel);
203 
204  tomCh->CM0.U = compareZero; // TK: replaced .B access to optimize runtime
205  tomCh->CM1.U = compareOne;
206 }
207 
208 
209 void IfxGtm_Tom_Ch_setCompareOne(Ifx_GTM_TOM *tom, IfxGtm_Tom_Ch channel, uint32 compareOne)
210 {
211  Ifx_GTM_TOM_CH *tomCh = (Ifx_GTM_TOM_CH *)((uint32)&tom->CH0.CTRL.U + 0x40 * channel);
212 
213  tomCh->CM1.U = compareOne; // TK: replaced .B access to optimize runtime
214 }
215 
216 
217 void IfxGtm_Tom_Ch_setCompareOneShadow(Ifx_GTM_TOM *tom, IfxGtm_Tom_Ch channel, uint32 shadowOne)
218 {
219  Ifx_GTM_TOM_CH *tomCh = (Ifx_GTM_TOM_CH *)((uint32)&tom->CH0.CTRL.U + 0x40 * channel);
220 
221  tomCh->SR1.U = shadowOne; // TK: replaced .B access to optimize runtime
222 }
223 
224 
225 void IfxGtm_Tom_Ch_setCompareShadow(Ifx_GTM_TOM *tom, IfxGtm_Tom_Ch channel, uint32 shadowZero, uint32 shadowOne)
226 {
227  Ifx_GTM_TOM_CH *tomCh = (Ifx_GTM_TOM_CH *)((uint32)&tom->CH0.CTRL.U + 0x40 * channel);
228 
229  tomCh->SR0.U = shadowZero; // TK: replaced .B access to optimize runtime
230  tomCh->SR1.U = shadowOne;
231 }
232 
233 
234 void IfxGtm_Tom_Ch_setCompareZero(Ifx_GTM_TOM *tom, IfxGtm_Tom_Ch channel, uint32 compareZero)
235 {
236  Ifx_GTM_TOM_CH *tomCh = (Ifx_GTM_TOM_CH *)((uint32)&tom->CH0.CTRL.U + 0x40 * channel);
237 
238  tomCh->CM0.U = compareZero; // TK: replaced .B access to optimize runtime
239 }
240 
241 
242 void IfxGtm_Tom_Ch_setCompareZeroShadow(Ifx_GTM_TOM *tom, IfxGtm_Tom_Ch channel, uint32 shadowZero)
243 {
244  Ifx_GTM_TOM_CH *tomCh = (Ifx_GTM_TOM_CH *)((uint32)&tom->CH0.CTRL.U + 0x40 * channel);
245 
246  tomCh->SR0.U = shadowZero; // TK: replaced .B access to optimize runtime
247 }
248 
249 
250 void IfxGtm_Tom_Ch_setCounterValue(Ifx_GTM_TOM *tom, IfxGtm_Tom_Ch channel, uint32 value)
251 {
252  Ifx_GTM_TOM_CH *tomCh = (Ifx_GTM_TOM_CH *)((uint32)&tom->CH0.CTRL.U + 0x40 * channel);
253 
254  tomCh->CN0.U = value; // TK: replaced .B access to optimize runtime
255 }
256 
257 
258 void IfxGtm_Tom_Ch_setGatedCounter(Ifx_GTM_TOM *tom, IfxGtm_Tom_Ch channel, boolean enabled)
259 {
260  if (channel <= IfxGtm_Tom_Ch_7)
261  {
262  Ifx_GTM_TOM_CH *tomCh = (Ifx_GTM_TOM_CH *)((uint32)&tom->CH0.CTRL.U + 0x40 * channel);
263  tomCh->CTRL.B.GCM = enabled ? 1 : 0;
264  }
265  else
266  {}
267 }
268 
269 
270 void IfxGtm_Tom_Ch_setNotification(Ifx_GTM_TOM *tom, IfxGtm_Tom_Ch channel, IfxGtm_IrqMode mode, boolean interruptOnCompareZero, boolean interruptOnCompareOne)
271 {
272  Ifx_GTM_TOM_CH *tomCh = (Ifx_GTM_TOM_CH *)((uint32)&tom->CH0.CTRL.U + 0x40 * channel);
273 
274  Ifx_GTM_TOM_CH_IRQ_EN en;
275 
276  en.U = tomCh->IRQ_EN.U;
277 
278  /* Disable all interrupts of the interrupt set to change mode */
279  tomCh->IRQ_EN.U = ZEROS;
280  tomCh->IRQ_MODE.B.IRQ_MODE = mode;
281  tomCh->IRQ_EN.U = en.U; /* Set the values back */
282 
283  en.B.CCU0TC_IRQ_EN = interruptOnCompareZero ? 1 : 0;
284  en.B.CCU1TC_IRQ_EN = interruptOnCompareOne ? 1 : 0;
285  tomCh->IRQ_EN.U = en.U;
286 }
287 
288 
289 void IfxGtm_Tom_Ch_setOneShotMode(Ifx_GTM_TOM *tom, IfxGtm_Tom_Ch channel, boolean enabled)
290 {
291  Ifx_GTM_TOM_CH *tomCh = (Ifx_GTM_TOM_CH *)((uint32)&tom->CH0.CTRL.U + 0x40 * channel);
292 
293  tomCh->CTRL.B.OSM = enabled ? 1 : 0;
294 }
295 
296 
297 void IfxGtm_Tom_Ch_setPcm(Ifx_GTM_TOM *tom, IfxGtm_Tom_Ch channel, boolean enabled)
298 {
299  if (channel == IfxGtm_Tom_Ch_15)
300  {
301  tom->CH15.CTRL.B.BITREV = enabled ? 1 : 0; /* FIXME check when datasheet updated, currently not specified */
302  }
303  else
304  {}
305 }
306 
307 
309 {
310  Ifx_GTM_TOM_CH *tomCh = (Ifx_GTM_TOM_CH *)((uint32)&tom->CH0.CTRL.U + 0x40 * channel);
311 
312  tomCh->CTRL.B.RST_CCU0 = event;
313 }
314 
315 
316 void IfxGtm_Tom_Ch_setSignalLevel(Ifx_GTM_TOM *tom, IfxGtm_Tom_Ch channel, Ifx_ActiveState activeState)
317 {
318  Ifx_GTM_TOM_CH *tomCh = (Ifx_GTM_TOM_CH *)((uint32)&tom->CH0.CTRL.U + 0x40 * channel);
319 
320  tomCh->CTRL.B.SL = activeState == Ifx_ActiveState_high ? 1 : 0;
321 }
322 
323 
324 void IfxGtm_Tom_Ch_setSpe(Ifx_GTM_TOM *tom, IfxGtm_Tom_Ch channel, boolean enabled)
325 {
326  if (channel <= IfxGtm_Tom_Ch_7)
327  {
328  Ifx_GTM_TOM_CH *tomCh = (Ifx_GTM_TOM_CH *)((uint32)&tom->CH0.CTRL.U + 0x40 * channel);
329  tomCh->CTRL.B.SPEM = enabled ? 1 : 0;
330  }
331  else
332  {}
333 }
334 
335 
337 {
338  Ifx_GTM_TOM_CH *tomCh = (Ifx_GTM_TOM_CH *)((uint32)&tom->CH0.CTRL.U + 0x40 * channel);
339 
340  tomCh->CTRL.B.TRIGOUT = trigger;
341 }
342 
343 
344 static uint32 IfxGtm_Tom_Tgc_buildFeature(uint16 enableMask, uint16 disableMask, uint8 bitfieldOffset)
345 {
346  uint8 i;
347  uint32 reg = 0;
348  uint32 mask = enableMask | (disableMask << 16);
349 
350  for (i = 0; i < IFXGTM_TOM_TGC_CHANNEL_COUNT; i++)
351  {
352  /* Bitfield length is 2 bits */
353  uint8 shift = (i * 2) + bitfieldOffset;
354 
355  if (mask & 0x1)
356  {
357  reg |= IfxGtm_FeatureControl_enable << shift;
358  }
359 
360  if (mask & 0x10000)
361  {
362  reg |= IfxGtm_FeatureControl_disable << shift;
363  }
364 
365  mask = mask >> 1;
366  }
367 
368  return reg;
369 }
370 
371 
372 void IfxGtm_Tom_Tgc_enableChannels(Ifx_GTM_TOM_TGC *tgc, uint16 enableMask, uint16 disableMask, boolean immediate)
373 {
374  uint32 value;
375 
376  /* FIXME: Alann... is it possible to optimize, not always re-calculating the value using buildFeature function ? */
377  value = IfxGtm_Tom_Tgc_buildFeature(enableMask, disableMask, IFX_GTM_TOM_TGC0_ENDIS_CTRL_ENDIS_CTRL0_OFF);
378 
379  if (immediate)
380  {
381  tgc->ENDIS_CTRL.U = value;
382  tgc->ENDIS_STAT.U = value;
383  }
384  else
385  {
386  tgc->ENDIS_CTRL.U = value;
387  }
388 }
389 
390 
391 void IfxGtm_Tom_Tgc_enableChannelsOutput(Ifx_GTM_TOM_TGC *tgc, uint16 enableMask, uint16 disableMask, boolean immediate)
392 {
393  uint32 value;
394 
395  value = IfxGtm_Tom_Tgc_buildFeature(enableMask, disableMask, IFX_GTM_TOM_TGC0_OUTEN_CTRL_OUTEN_CTRL0_OFF);
396 
397  if (immediate)
398  {
399  tgc->OUTEN_CTRL.U = value;
400  tgc->OUTEN_STAT.U = value;
401  }
402  else
403  {
404  tgc->OUTEN_CTRL.U = value;
405  }
406 }
407 
408 
410 {
411  tgc->INT_TRIG.U = IfxGtm_Tom_Tgc_buildFeature(enableMask, disableMask, IFX_GTM_TOM_TGC0_INT_TRIG_INT_TRIG0_OFF);
412 }
413 
414 
416 {
417  tgc->GLB_CTRL.U = IfxGtm_Tom_Tgc_buildFeature(enableMask, disableMask, IFX_GTM_TOM_TGC0_GLB_CTRL_UPEN_CTRL0_OFF);
418 }
419 
420 
422 {
423  tgc->ACT_TB.B.TB_TRIG = enabled ? 1 : 0;
424 }
425 
426 
428 {
429  uint8 i;
430  uint32 reg = 0;
431 
432  for (i = 0; i < IFXGTM_TOM_TGC_CHANNEL_COUNT; i++)
433  {
434  if (resetMask & 0x1)
435  {
436  reg |= 1 << i;
437  }
438 
439  resetMask = resetMask >> 1;
440  }
441 
442  tgc->GLB_CTRL.U = reg << IFX_GTM_TOM_TGC0_GLB_CTRL_RST_CH0_OFF;
443 }
444 
445 
446 void IfxGtm_Tom_Tgc_setChannelsForceUpdate(Ifx_GTM_TOM_TGC *tgc, uint16 enableMask, uint16 disableMask, uint16 resetEnableMask, uint16 resetDisableMask)
447 {
448  uint32 regEnable, regReset;
449 
450  regEnable = IfxGtm_Tom_Tgc_buildFeature(enableMask, disableMask, IFX_GTM_TOM_TGC0_FUPD_CTRL_FUPD_CTRL0_OFF);
451  regReset = IfxGtm_Tom_Tgc_buildFeature(resetEnableMask, resetDisableMask, IFX_GTM_TOM_TGC0_FUPD_CTRL_RSTCN0_CH0_OFF);
452 
453  tgc->FUPD_CTRL.U = regEnable | regReset;
454 }
455 
456 
458 {
459  Ifx_GTM_TOM_TGC0_ACT_TB act_tb;
460 
461  act_tb.U = tgc->ACT_TB.U;
462  act_tb.B.TBU_SEL = base;
463  act_tb.B.ACT_TB = value;
464  tgc->ACT_TB.U = act_tb.U;
465 }
466 
467 
469 {
470  tgc->GLB_CTRL.U = 1 << IFX_GTM_TOM_TGC0_GLB_CTRL_HOST_TRIG_OFF;
471 }