lpit_driver.c
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2016, Freescale Semiconductor, Inc.
3  * Copyright 2016-2017 NXP
4  * All rights reserved.
5  *
6  * THIS SOFTWARE IS PROVIDED BY NXP "AS IS" AND ANY EXPRESSED OR
7  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
8  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
9  * IN NO EVENT SHALL NXP OR ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
10  * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
11  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
12  * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
13  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
14  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
15  * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
16  * THE POSSIBILITY OF SUCH DAMAGE.
17  */
18 
57 #include <stddef.h>
58 #include "lpit_driver.h"
59 #include "lpit_hw_access.h"
60 #include "interrupt_manager.h"
61 #include "clock_manager.h"
62 
63 /*******************************************************************************
64  * Variables
65  ******************************************************************************/
66 
67 /* Table of base addresses for LPIT instances */
68 static LPIT_Type * const s_lpitBase[] = LPIT_BASE_PTRS;
69 /* Table to save LPIT indexes in PCC register map for clock configuration */
71 /* LPIT functional clock variable which will be updated in some driver functions */
73 
74 /******************************************************************************
75  * Code
76  *****************************************************************************/
77 
78 /*FUNCTION**********************************************************************
79  *
80  * Function Name : LPIT_DRV_Init
81  * Description : Initializes LPIT module.
82  * This function resets LPIT module, enables the LPIT module, configures LPIT
83  * module operation in Debug and DOZE mode. The LPIT configuration structure shall
84  * be passed as arguments.
85  * This configuration structure affects all timer channels.
86  * This function should be called before calling any other LPIT driver function.
87  *
88  * Implements : LPIT_DRV_Init_Activity
89  *END**************************************************************************/
90 void LPIT_DRV_Init(uint32_t instance,
91  const lpit_user_config_t *userConfig)
92 {
93  LPIT_Type * base;
94  status_t clkErr;
95 
96  DEV_ASSERT(instance < LPIT_INSTANCE_COUNT);
97  DEV_ASSERT(userConfig != NULL);
98 
99  /* Gets current functional clock frequency of LPIT instance */
100  clkErr = CLOCK_SYS_GetFreq(s_lpitClkNames[instance], &s_lpitSourceClockFrequency[instance]);
101  /* Checks the functional clock of LPIT module */
102  (void)clkErr;
103  DEV_ASSERT(clkErr == STATUS_SUCCESS);
104  DEV_ASSERT(s_lpitSourceClockFrequency[instance] > 0U);
105 
106  base = s_lpitBase[instance];
107  /* Resets LPIT module */
108  LPIT_Reset(base);
109  /* Enables functional clock of LPIT module*/
110  LPIT_Enable(base);
111  /* Sets LPIT operation in Debug and DOZE mode*/
112  LPIT_SetTimerRunInDebugCmd(base, userConfig->enableRunInDebug);
113  LPIT_SetTimerRunInDozeCmd(base, userConfig->enableRunInDoze);
114 }
115 
116 /*FUNCTION**********************************************************************
117  *
118  * Function Name : LPIT_DRV_Deinit
119  * Description : De-initializes LPIT module.
120  * This function disables LPIT module.
121  * In order to use the LPIT module again, LPIT_DRV_Init must be called.
122  *
123  * Implements : LPIT_DRV_Deinit_Activity
124  *END**************************************************************************/
125 void LPIT_DRV_Deinit(uint32_t instance)
126 {
127  LPIT_Type * base;
128 
129  DEV_ASSERT(instance < LPIT_INSTANCE_COUNT);
130 
131  base = s_lpitBase[instance];
132  /* Disables LPIT module functional clock*/
133  LPIT_Disable(base);
134 }
135 
136 /*FUNCTION**********************************************************************
137  *
138  * Function Name : LPIT_DRV_InitChannel
139  * Description : Initializes LPIT channel.
140  * This function initializes the LPIT timers by using a channel, this function
141  * configures timer channel chaining, timer channel mode, timer channel period,
142  * interrupt generation, trigger source, trigger select, reload on trigger,
143  * stop on interrupt and start on trigger.
144  * The timer channel number and its configuration structure shall be passed as arguments.
145  * Timer channels do not start counting by default after calling this function.
146  * The function LPIT_DRV_StartTimerChannels must be called to start the timer channel counting.
147  * In order to re-configures the period, call the LPIT_DRV_SetTimerPeriodByUs or
148  * LPIT_DRV_SetTimerPeriodByCount.
149  *
150  * Implements : LPIT_DRV_InitChannel_Activity
151  *END**************************************************************************/
152 status_t LPIT_DRV_InitChannel(uint32_t instance,
153  uint32_t channel,
154  const lpit_user_channel_config_t * userChannelConfig)
155 {
156  LPIT_Type * base;
157  status_t reVal = STATUS_SUCCESS;
158  const IRQn_Type lpitIrqId[] = LPIT_IRQS;
159 
160  DEV_ASSERT(instance < LPIT_INSTANCE_COUNT);
161  DEV_ASSERT(userChannelConfig != NULL);
162  DEV_ASSERT(channel < LPIT_TMR_COUNT);
163 
164  base = s_lpitBase[instance];
165 
166  if ((channel == 0U) && (userChannelConfig->chainChannel))
167  {
168  reVal = STATUS_ERROR;
169  }
170  else
171  {
172  /* Setups the timer channel chaining */
173  LPIT_SetTimerChannelChainCmd(base, channel, userChannelConfig->chainChannel);
174  /* Setups the timer channel operation mode */
175  LPIT_SetTimerChannelModeCmd(base, channel, userChannelConfig->timerMode);
176  if (userChannelConfig->periodUnits == LPIT_PERIOD_UNITS_MICROSECONDS)
177  {
178  /* Setups timer channel period in microsecond unit */
179  reVal = LPIT_DRV_SetTimerPeriodByUs(instance, channel, userChannelConfig->period);
180  }
181  else
182  {
183  /* Setups timer channel period in count unit */
184  LPIT_DRV_SetTimerPeriodByCount(instance, channel, userChannelConfig->period);
185  }
186 
187  if (reVal == STATUS_SUCCESS)
188  {
189  /* Setups the timer channel trigger source, trigger select, reload on trigger,
190  stop on timeout, start on trigger and channel chaining */
191  LPIT_SetTriggerSourceCmd(base, channel, userChannelConfig->triggerSource);
192  LPIT_SetTriggerSelectCmd(base, channel, userChannelConfig->triggerSelect);
193  LPIT_SetReloadOnTriggerCmd(base, channel, userChannelConfig->enableReloadOnTrigger);
194  LPIT_SetStopOnInterruptCmd(base, channel, userChannelConfig->enableStopOnInterrupt);
195  LPIT_SetStartOnTriggerCmd(base, channel, userChannelConfig->enableStartOnTrigger);
196  /* Setups interrupt generation for timer channel */
197  if (userChannelConfig->isInterruptEnabled)
198  {
199  /* Enables interrupt generation */
200  LPIT_EnableInterruptTimerChannels(base, (uint32_t)1U << channel);
201  INT_SYS_EnableIRQ(lpitIrqId[channel]);
202  }
203  else
204  {
205  /* Disables interrupt generation */
206  LPIT_DisableInterruptTimerChannels(base, (uint32_t)1U << channel);
207  INT_SYS_DisableIRQ(lpitIrqId[channel]);
208  }
209  }
210  }
211  return reVal;
212 }
213 
214 /*FUNCTION**********************************************************************
215  *
216  * Function Name : LPIT_DRV_StartTimerChannels
217  * Description : Starts timer channel counting.
218  * This function allows starting timer channels simultaneously .
219  * After calling this function, timer channels are going operate depend on mode and
220  * control bits which controls timer channel start, reload and restart.
221  *
222  * Implements : LPIT_DRV_StartTimerChannels_Activity
223  *END**************************************************************************/
224 void LPIT_DRV_StartTimerChannels(uint32_t instance,
225  uint32_t mask)
226 {
227  LPIT_Type * base;
228 
229  DEV_ASSERT(instance < LPIT_INSTANCE_COUNT);
230  DEV_ASSERT(mask < (1UL << LPIT_TMR_COUNT));
231 
232  base = s_lpitBase[instance];
233  /* Starts timer channel counting */
234  LPIT_StartTimerChannels(base, mask);
235 }
236 
237 /*FUNCTION**********************************************************************
238  *
239  * Function Name : LPIT_DRV_StopTimerChannels
240  * Description : Stop timer channel from counting.
241  * This function allows stop timer channels simultaneously from counting.
242  * Timer channels reload their periods respectively after the next time
243  * they call the LPIT_DRV_StartTimerChannels. Note that: In 32-bit Trigger Accumulator
244  * mode, the counter will load on the first trigger rising edge.
245  *
246  * Implements : LPIT_DRV_StopTimerChannels_Activity
247  *END**************************************************************************/
248 void LPIT_DRV_StopTimerChannels(uint32_t instance,
249  uint32_t mask)
250 {
251  LPIT_Type * base;
252 
253  DEV_ASSERT(instance < LPIT_INSTANCE_COUNT);
254  DEV_ASSERT(mask < (1UL << LPIT_TMR_COUNT));
255 
256  base = s_lpitBase[instance];
257  /* Stops timer channel from counting */
258  LPIT_StopTimerChannels(base, mask);
259 }
260 
261 /*FUNCTION**********************************************************************
262  *
263  * Function Name : LPIT_DRV_SetTimerPeriodByUs
264  * Description : Sets timer channel period in microseconds unit.
265  * This function sets the timer channel period in microseconds
266  * when timer channel mode is 32 bit periodic or dual 16 bit counter mode.
267  * The period range depends on the frequency of the LPIT functional clock and
268  * operation mode of timer channel.
269  * If the required period is out of range, use the suitable mode if applicable.
270  * This function is only valid for one single channel.
271  *
272  * Implements : LPIT_DRV_SetTimerPeriodByUs_Activity
273  *END**************************************************************************/
275  uint32_t channel,
276  uint32_t periodUs)
277 {
278  LPIT_Type * base;
279  lpit_timer_modes_t timerMode;
280  status_t clkErr;
281  status_t reVal = STATUS_SUCCESS;
282  uint64_t count;
283 
284  DEV_ASSERT(instance < LPIT_INSTANCE_COUNT);
285  DEV_ASSERT(channel < LPIT_TMR_COUNT);
286 
287  /* Gets current functional clock frequency of LPIT instance */
288  clkErr = CLOCK_SYS_GetFreq(s_lpitClkNames[instance], &s_lpitSourceClockFrequency[instance]);
289  /* Checks the functional clock of LPIT module */
290  (void)clkErr;
291  DEV_ASSERT(clkErr == STATUS_SUCCESS);
292  DEV_ASSERT(s_lpitSourceClockFrequency[instance] > 0U);
293 
294  base = s_lpitBase[instance];
295  /* Calculates the count value, assign it to timer channel counter register.*/
296  count = ((uint64_t)periodUs) * s_lpitSourceClockFrequency[instance];
297  count = (count / 1000000U) - 1U;
298  /* Gets current timer channel operation mode */
299  timerMode = LPIT_GetTimerChannelModeCmd(base, channel);
300  /* Checks whether the count is valid with timer channel operation mode */
301  if (count <= MAX_PERIOD_COUNT)
302  {
303  if (timerMode == LPIT_DUAL_PERIODIC_COUNTER)
304  {
306  {
307  reVal = STATUS_ERROR;
308  }
309  else
310  {
311  if (count > MAX_PERIOD_COUNT_16_BIT)
312  {
313  /* Calculates the count value for dual 16 bit periodic counter mode */
314  count = ((count - (MAX_PERIOD_COUNT_16_BIT + 1U)) << 16U)
316  }
317  }
318  }
319  }
320  else
321  {
322  reVal = STATUS_ERROR;
323  }
324  if (reVal == STATUS_SUCCESS)
325  {
326  /* Sets the timer channel period in count unit */
327  LPIT_SetTimerPeriodByCount(base, channel, (uint32_t)count);
328  }
329  return reVal;
330 }
331 
332 /*FUNCTION**********************************************************************
333  *
334  * Function Name : LPIT_DRV_SetTimerPeriodInDual16ModeByUs
335  * Description : Sets timer channel period in microseconds unit.
336  * This function sets the timer channel period in microseconds
337  * when timer channel mode is dual 16 bit periodic counter mode.
338  * The period range depends on the frequency of the LPIT functional clock and
339  * operation mode of timer channel.
340  * If the required period is out of range, use the suitable mode if applicable.
341  * This function is only valid for one single channel.
342  *
343  * Implements : LPIT_DRV_SetTimerPeriodInDual16ModeByUs_Activity
344  *END**************************************************************************/
346  uint32_t channel,
347  uint16_t periodHigh,
348  uint16_t periodLow)
349 {
350  LPIT_Type * base;
351  status_t reVal = STATUS_SUCCESS;
352  status_t clkErr;
353  uint64_t periodHighCount;
354  uint64_t periodLowCount;
355  uint64_t periodCount;
356 
357  DEV_ASSERT(instance < LPIT_INSTANCE_COUNT);
358  DEV_ASSERT(channel < LPIT_TMR_COUNT);
359 
360  /* Gets current functional clock frequency of LPIT instance */
361  clkErr = CLOCK_SYS_GetFreq(s_lpitClkNames[instance], &s_lpitSourceClockFrequency[instance]);
362  /* Checks the functional clock of LPIT module */
363  (void)clkErr;
364  DEV_ASSERT(clkErr == STATUS_SUCCESS);
365  DEV_ASSERT(s_lpitSourceClockFrequency[instance] > 0U);
366 
367  base = s_lpitBase[instance];
368  /* Calculates the count value of 16 bit higher period.*/
369  periodHighCount = ((uint64_t)periodHigh) * s_lpitSourceClockFrequency[instance];
370  periodHighCount = (periodHighCount / 1000000U) - 1U;
371 
372  /* Calculates the count value of 16 bit lower period.*/
373  periodLowCount = ((uint64_t)periodLow) * s_lpitSourceClockFrequency[instance];
374  periodLowCount = (periodLowCount / 1000000U) - 1U;
375  /* Checks whether the count is valid */
376  if ((periodHighCount > MAX_PERIOD_COUNT_16_BIT) || (periodLowCount > MAX_PERIOD_COUNT_16_BIT))
377  {
378  reVal = STATUS_ERROR;
379  }
380  else
381  {
382  periodCount = (periodHighCount << 16U) | periodLowCount;
383  LPIT_SetTimerPeriodByCount(base, channel, (uint32_t)periodCount);
384  }
385 
386  return reVal;
387 }
388 
389 /*FUNCTION**********************************************************************
390  *
391  * Function Name : LPIT_DRV_GetTimerPeriodByUs
392  * Description : Gets the timer channel period in microseconds.
393  * The returned period here makes sense if the operation mode of timer channel
394  * is 32 bit periodic counter or dual 16 bit periodic counter.
395  *
396  * Implements : LPIT_DRV_GetTimerPeriodByUs_Activity
397  *END**************************************************************************/
398 uint64_t LPIT_DRV_GetTimerPeriodByUs(uint32_t instance,
399  uint32_t channel)
400 {
401  const LPIT_Type * base;
402  status_t clkErr;
403  lpit_timer_modes_t timerMode;
404  uint64_t currentPeriod;
405 
406  DEV_ASSERT(instance < LPIT_INSTANCE_COUNT);
407  DEV_ASSERT(channel < LPIT_TMR_COUNT);
408 
409  /* Gets current functional clock frequency of LPIT instance */
410  clkErr = CLOCK_SYS_GetFreq(s_lpitClkNames[instance], &s_lpitSourceClockFrequency[instance]);
411  /* Checks the functional clock of LPIT module */
412  (void)clkErr;
413  DEV_ASSERT(clkErr == STATUS_SUCCESS);
414  DEV_ASSERT(s_lpitSourceClockFrequency[instance] > 0U);
415 
416  base = s_lpitBase[instance];
417  /* Gets current timer channel period in count.*/
418  currentPeriod = LPIT_GetTimerPeriodByCount(base, channel);
419  /* Gets current timer channel operation mode */
420  timerMode = LPIT_GetTimerChannelModeCmd(base, channel);
421 
422  if (timerMode == LPIT_DUAL_PERIODIC_COUNTER)
423  {
424  if (currentPeriod > MAX_PERIOD_COUNT_16_BIT)
425  {
426  currentPeriod = (((currentPeriod >> 16U) + (currentPeriod & MAX_PERIOD_COUNT_16_BIT) + 2U) * 1000000U)
427  / s_lpitSourceClockFrequency[instance];
428  }
429  else
430  {
431  /* Converts period from count unit to microseconds unit for other modes */
432  currentPeriod = ((currentPeriod + 1U) * 1000000U) / s_lpitSourceClockFrequency[instance];
433  }
434  }
435  else
436  {
437  /* Converts period from count unit to microseconds unit for other modes */
438  currentPeriod = ((currentPeriod + 1U) * 1000000U) / s_lpitSourceClockFrequency[instance];
439  }
440  return currentPeriod;
441 }
442 
443 /*FUNCTION**********************************************************************
444  *
445  * Function Name : LPIT_DRV_GetCurrentTimerUs
446  * Description : Gets current timer channel counting value in microseconds unit.
447  * This function returns an absolute time stamp in microseconds.
448  * One common use of this function is to measure the running time of a part of
449  * code. Call this function at both the beginning and end of code. The time
450  * difference between these two time stamps is the running time.
451  * The return counting value here makes sense if the operation mode of timer channel
452  * is 32 bit periodic counter or dual 16 bit periodic counter or 32-bit trigger input capture.
453  * Need to make sure the running time will not exceed the timer channel period.
454  *
455  * Implements : LPIT_DRV_GetCurrentTimerUs_Activity
456  *END**************************************************************************/
457 uint64_t LPIT_DRV_GetCurrentTimerUs(uint32_t instance,
458  uint32_t channel)
459 {
460  const LPIT_Type * base;
461  status_t clkErr;
462  lpit_timer_modes_t timerMode;
463  uint64_t currentTime = 0U;
464 
465  DEV_ASSERT(instance < LPIT_INSTANCE_COUNT);
466  DEV_ASSERT(channel < LPIT_TMR_COUNT);
467 
468  base = s_lpitBase[instance];
469  /* Gets current timer channel counting value */
470  currentTime = LPIT_GetCurrentTimerCount(base, channel);
471  /* Gets current timer channel operation mode */
472  timerMode = LPIT_GetTimerChannelModeCmd(base, channel);
473  /* Gets current functional clock frequency of LPIT instance */
474  clkErr = CLOCK_SYS_GetFreq(s_lpitClkNames[instance], &s_lpitSourceClockFrequency[instance]);
475  (void)clkErr;
476  DEV_ASSERT(s_lpitSourceClockFrequency[instance] > 0U);
477 
478  if (timerMode == LPIT_DUAL_PERIODIC_COUNTER)
479  {
480  currentTime = (((currentTime >> 16U) + (currentTime & MAX_PERIOD_COUNT_16_BIT)) * 1000000U)
481  / s_lpitSourceClockFrequency[instance];
482  }
483  else
484  {
485  /* Converts counting value to microseconds unit for other modes */
486  currentTime = (currentTime * 1000000U) / s_lpitSourceClockFrequency[instance];
487  }
488 
489  return currentTime;
490 }
491 
492 /*FUNCTION**********************************************************************
493  *
494  * Function Name : LPIT_DRV_SetTimerPeriodByCount
495  * Description : Sets the timer channel period in count unit.
496  * This function sets the timer channel period in count unit.
497  * The counter period of a running timer channel can be modified by first setting
498  * a new load value, the value will be loaded after the timer channel expires.
499  * To abort the current cycle and start a timer channel period with the new value,
500  * the timer channel must be disabled and enabled again.
501  *
502  * Implements : LPIT_DRV_SetTimerPeriodByCount_Activity
503  *END**************************************************************************/
504 void LPIT_DRV_SetTimerPeriodByCount(uint32_t instance,
505  uint32_t channel,
506  uint32_t count)
507 {
508  LPIT_Type * base;
509 
510  DEV_ASSERT(instance < LPIT_INSTANCE_COUNT);
511  DEV_ASSERT(channel < LPIT_TMR_COUNT);
512 
513  base = s_lpitBase[instance];
514  /* Sets the timer channel period in count unit */
515  LPIT_SetTimerPeriodByCount(base, channel, count);
516 
517 }
518 
519 /*FUNCTION**********************************************************************
520  *
521  * Function Name : LPIT_DRV_SetTimerPeriodInDual16ModeByCount
522  * Description : Sets the timer channel period in count unit.
523  * This function sets the timer channel period in count unit when timer channel
524  * mode is dual 16 periodic counter mode.
525  * The counter period of a running timer channel can be modified by first setting
526  * a new load value, the value will be loaded after the timer channel expires.
527  * To abort the current cycle and start a timer channel period with the new value,
528  * the timer channel must be disabled and enabled again.
529  *
530  * Implements : LPIT_DRV_SetTimerPeriodInDual16ModeByCount_Activity
531  *END**************************************************************************/
533  uint32_t channel,
534  uint16_t periodHigh,
535  uint16_t periodLow)
536 {
537  LPIT_Type * base;
538  uint32_t period;
539 
540  DEV_ASSERT(instance < LPIT_INSTANCE_COUNT);
541  DEV_ASSERT(channel < LPIT_TMR_COUNT);
542 
543  base = s_lpitBase[instance];
544 
545  period = ((uint32_t)periodHigh << 16U) | periodLow;
546  /* Sets the timer channel period in count unit */
547  LPIT_SetTimerPeriodByCount(base, channel, period);
548 
549 }
550 
551 /*FUNCTION**********************************************************************
552  *
553  * Function Name : LPIT_DRV_GetTimerPeriodByCount
554  * Description : Gets the current timer channel period in count unit.
555  *
556  * Implements : LPIT_DRV_GetTimerPeriodByCount_Activity
557  *END**************************************************************************/
558 uint32_t LPIT_DRV_GetTimerPeriodByCount(uint32_t instance,
559  uint32_t channel)
560 {
561  const LPIT_Type * base;
562  lpit_timer_modes_t timerMode;
563  uint32_t currentPeriod;
564 
565  DEV_ASSERT(instance < LPIT_INSTANCE_COUNT);
566  DEV_ASSERT(channel < LPIT_TMR_COUNT);
567 
568  base = s_lpitBase[instance];
569  /* Gets current timer channel period by count.*/
570  currentPeriod = LPIT_GetTimerPeriodByCount(base, channel);
571  /* Gets current timer channel operation mode */
572  timerMode = LPIT_GetTimerChannelModeCmd(base, channel);
573 
574  if (timerMode == LPIT_DUAL_PERIODIC_COUNTER)
575  {
576  /* Calculates the period for dual 16 bit periodic counter mode */
577  currentPeriod = (currentPeriod >> 16U) + (currentPeriod & MAX_PERIOD_COUNT_16_BIT);
578  }
579  return currentPeriod;
580 }
581 
582 /*FUNCTION**********************************************************************
583  *
584  * Function Name : LPIT_DRV_GetCurrentTimerCount
585  * Description : Gets the current timer channel counting value in count.
586  * This function returns the real-time timer channel counting value, the value in
587  * a range from 0 to timer channel period.
588  * Need to make sure the running time does not exceed the timer channel period.
589  *
590  * Implements : LPIT_DRV_GetCurrentTimerCount_Activity
591  *END**************************************************************************/
592 uint32_t LPIT_DRV_GetCurrentTimerCount(uint32_t instance,
593  uint32_t channel)
594 {
595  const LPIT_Type * base;
596  lpit_timer_modes_t timerMode;
597  uint32_t currentTime;
598 
599  DEV_ASSERT(instance < LPIT_INSTANCE_COUNT);
600  DEV_ASSERT(channel < LPIT_TMR_COUNT);
601 
602  base = s_lpitBase[instance];
603  /* Gets current timer channel counting value */
604  currentTime = LPIT_GetCurrentTimerCount(base, channel);
605  /* Gets current timer channel operation mode */
606  timerMode = LPIT_GetTimerChannelModeCmd(base, channel);
607 
608  if (timerMode == LPIT_DUAL_PERIODIC_COUNTER)
609  {
610  /* Calculates the current counting value for dual 16 bit periodic counter mode */
611  currentTime = (currentTime >> 16U) + (currentTime & MAX_PERIOD_COUNT_16_BIT);
612  }
613  return currentTime;
614 }
615 
616 /*FUNCTION**********************************************************************
617  *
618  * Function Name : LPIT_DRV_EnableTimerChannelInterrupt
619  * Description : This function allows enabling interrupt generation of timer channel
620  * when timeout occurs or input trigger occurs.
621  *
622  * Implements : LPIT_DRV_EnableTimerChannelInterrupt_Activity
623  *END**************************************************************************/
625  uint32_t mask)
626 {
627  LPIT_Type * base;
628 
629  DEV_ASSERT(instance < LPIT_INSTANCE_COUNT);
630  DEV_ASSERT(mask < (1UL << LPIT_TMR_COUNT));
631 
632  base = s_lpitBase[instance];
633  /* Enable interrupt of timer channels */
634  LPIT_EnableInterruptTimerChannels(base, mask);
635 }
636 
637 /*FUNCTION**********************************************************************
638  *
639  * Function Name : LPIT_DRV_DisableTimerChannelInterrupt
640  * Description : This function allows disabling interrupt generation of timer channel
641  * when timeout occurs or input trigger occurs.
642  *
643  * Implements : LPIT_DRV_DisableTimerChannelInterrupt_Activity
644  *END**************************************************************************/
646  uint32_t mask)
647 {
648  LPIT_Type * base;
649 
650  DEV_ASSERT(instance < LPIT_INSTANCE_COUNT);
651  DEV_ASSERT(mask < (1UL << LPIT_TMR_COUNT));
652 
653  base = s_lpitBase[instance];
654  /* Disable interrupt of timer channels */
655  LPIT_DisableInterruptTimerChannels(base, mask);
656 }
657 
658 /*FUNCTION**********************************************************************
659  *
660  * Function Name : LPIT_DRV_GetInterruptFlagTimerChannels
661  * Description : Gets the current interrupt flag of timer channels.
662  * In compare modes, the flag sets to 1 at the end of the timer period.
663  * In capture modes, the flag sets to 1 when the trigger asserts.
664  *
665  * Implements : LPIT_DRV_GetInterruptFlagTimerChannels_Activity
666  *END**************************************************************************/
667 uint32_t LPIT_DRV_GetInterruptFlagTimerChannels(uint32_t instance,
668  uint32_t mask)
669 {
670  const LPIT_Type * base;
671 
672  DEV_ASSERT(instance < LPIT_INSTANCE_COUNT);
673  DEV_ASSERT(mask < (1UL << LPIT_TMR_COUNT));
674 
675  base = s_lpitBase[instance];
676  /* Gets the interrupt flag for timer channels */
677  return LPIT_GetInterruptFlagTimerChannels(base, mask);
678 }
679 
680 /*FUNCTION**********************************************************************
681  *
682  * Function Name : LPIT_DRV_ClearInterruptFlagTimerChannels
683  * Description : Clears the interrupt flag of timer channels.
684  * This function clears the interrupt flag of timer channels after
685  * their interrupt event occurred.
686  * Implements : LPIT_DRV_ClearInterruptFlagTimerChannels_Activity
687  *END**************************************************************************/
689  uint32_t mask)
690 {
691  LPIT_Type * base;
692 
693  DEV_ASSERT(instance < LPIT_INSTANCE_COUNT);
694  DEV_ASSERT(mask < (1UL << LPIT_TMR_COUNT));
695 
696  base = s_lpitBase[instance];
697  /* Clears the interrupt flag for timer channels */
698  LPIT_ClearInterruptFlagTimerChannels(base, mask);
699 }
700 
701 /*******************************************************************************
702  * EOF
703  ******************************************************************************/
#define MAX_PERIOD_COUNT_IN_DUAL_16BIT_MODE
Max period in count of dual 16 bit periodic counter mode.
Definition: lpit_driver.h:63
#define LPIT_BASE_PTRS
Definition: S32K118.h:5832
lpit_timer_modes_t timerMode
Definition: lpit_driver.h:126
void LPIT_DRV_SetTimerPeriodByCount(uint32_t instance, uint32_t channel, uint32_t count)
Sets the timer channel period in count unit.
Definition: lpit_driver.c:504
void LPIT_DRV_StopTimerChannels(uint32_t instance, uint32_t mask)
Stops the timer channel counting.
Definition: lpit_driver.c:248
uint64_t LPIT_DRV_GetTimerPeriodByUs(uint32_t instance, uint32_t channel)
Gets the timer channel period in microseconds.
Definition: lpit_driver.c:398
lpit_trigger_source_t triggerSource
Definition: lpit_driver.h:129
uint64_t LPIT_DRV_GetCurrentTimerUs(uint32_t instance, uint32_t channel)
Gets the current timer channel counting value in microseconds.
Definition: lpit_driver.c:457
static LPIT_Type *const s_lpitBase[]
Definition: lpit_driver.c:68
void INT_SYS_DisableIRQ(IRQn_Type irqNumber)
Disables an interrupt for a given IRQ number.
#define LPIT_TMR_COUNT
Definition: S32K118.h:5800
#define DEV_ASSERT(x)
Definition: devassert.h:77
status_t LPIT_DRV_SetTimerPeriodInDual16ModeByUs(uint32_t instance, uint32_t channel, uint16_t periodHigh, uint16_t periodLow)
Sets the timer channel period in microseconds.
Definition: lpit_driver.c:345
#define LPIT_INSTANCE_COUNT
Definition: S32K118.h:5821
status_t CLOCK_SYS_GetFreq(clock_names_t clockName, uint32_t *frequency)
Gets the clock frequency for a specific clock name.
void LPIT_DRV_Init(uint32_t instance, const lpit_user_config_t *userConfig)
Initializes the LPIT module.
Definition: lpit_driver.c:90
void LPIT_DRV_SetTimerPeriodInDual16ModeByCount(uint32_t instance, uint32_t channel, uint16_t periodHigh, uint16_t periodLow)
Sets the timer channel period in count unit.
Definition: lpit_driver.c:532
lpit_period_units_t periodUnits
Definition: lpit_driver.h:127
status_t LPIT_DRV_InitChannel(uint32_t instance, uint32_t channel, const lpit_user_channel_config_t *userChannelConfig)
Initializes the LPIT channel.
Definition: lpit_driver.c:152
void LPIT_DRV_ClearInterruptFlagTimerChannels(uint32_t instance, uint32_t mask)
Clears the interrupt flag of timer channels.
Definition: lpit_driver.c:688
status_t
Status return codes. Common error codes will be a unified enumeration (C enum) that will contain all ...
Definition: status.h:44
IRQn_Type
Defines the Interrupt Numbers definitions.
Definition: S32K118.h:188
LPIT configuration structure.
Definition: lpit_driver.h:111
static const clock_names_t s_lpitClkNames[LPIT_INSTANCE_COUNT]
Definition: lpit_driver.c:70
#define MAX_PERIOD_COUNT
Max period in count of all operation mode except for dual 16 bit periodic counter mode...
Definition: lpit_driver.h:61
Structure to configure the channel timer.
Definition: lpit_driver.h:124
#define LPIT_CLOCK_NAMES
Clock names for LPIT.
lpit_timer_modes_t
Mode options available for the LPIT timer Implements : lpit_timer_modes_t_Class.
Definition: lpit_driver.h:71
static uint32_t s_lpitSourceClockFrequency[LPIT_INSTANCE_COUNT]
Definition: lpit_driver.c:72
void INT_SYS_EnableIRQ(IRQn_Type irqNumber)
Enables an interrupt for a given IRQ number.
uint32_t LPIT_DRV_GetTimerPeriodByCount(uint32_t instance, uint32_t channel)
Gets the current timer channel period in count unit.
Definition: lpit_driver.c:558
void LPIT_DRV_EnableTimerChannelInterrupt(uint32_t instance, uint32_t mask)
Enables the interrupt generation of timer channel.
Definition: lpit_driver.c:624
#define MAX_PERIOD_COUNT_16_BIT
Max count of 16 bit.
Definition: lpit_driver.h:65
uint32_t LPIT_DRV_GetInterruptFlagTimerChannels(uint32_t instance, uint32_t mask)
Gets the current interrupt flag of timer channels.
Definition: lpit_driver.c:667
status_t LPIT_DRV_SetTimerPeriodByUs(uint32_t instance, uint32_t channel, uint32_t periodUs)
Sets the timer channel period in microseconds.
Definition: lpit_driver.c:274
void LPIT_DRV_Deinit(uint32_t instance)
De-Initializes the LPIT module.
Definition: lpit_driver.c:125
clock_names_t
Clock names.
void LPIT_DRV_DisableTimerChannelInterrupt(uint32_t instance, uint32_t mask)
Disables the interrupt generation of timer channel.
Definition: lpit_driver.c:645
void LPIT_DRV_StartTimerChannels(uint32_t instance, uint32_t mask)
Starts the timer channel counting.
Definition: lpit_driver.c:224
#define LPIT_IRQS
Definition: S32K118.h:5838
uint32_t LPIT_DRV_GetCurrentTimerCount(uint32_t instance, uint32_t channel)
Gets the current timer channel counting value in count.
Definition: lpit_driver.c:592