S32 SDK
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 "lpit_driver.h"
58 
59 /*******************************************************************************
60  * Variables
61  ******************************************************************************/
62 
63 /* Table of base addresses for LPIT instances */
64 static LPIT_Type * const s_lpitBase[] = LPIT_BASE_PTRS;
65 /* Table to save LPIT indexes in PCC register map for clock configuration */
67 /* LPIT functional clock variable which will be updated in some driver functions */
69 
70 /******************************************************************************
71  * Code
72  *****************************************************************************/
73 
74 /*FUNCTION**********************************************************************
75  *
76  * Function Name : LPIT_DRV_Init
77  * Description : Initializes LPIT module.
78  * This function resets LPIT module, enables the LPIT module, configures LPIT
79  * module operation in Debug and DOZE mode. The LPIT configuration structure shall
80  * be passed as arguments.
81  * This configuration structure affects all timer channels.
82  * This function should be called before calling any other LPIT driver function.
83  *
84  * Implements : LPIT_DRV_Init_Activity
85  *END**************************************************************************/
86 void LPIT_DRV_Init(uint32_t instance,
87  const lpit_user_config_t *userConfig)
88 {
89  LPIT_Type * base;
90  status_t clkErr;
91 
92  DEV_ASSERT(instance < LPIT_INSTANCE_COUNT);
93  DEV_ASSERT(userConfig != NULL);
94 
95  /* Gets current functional clock frequency of LPIT instance */
96  clkErr = CLOCK_SYS_GetFreq(s_lpitClkNames[instance], &s_lpitSourceClockFrequency[instance]);
97  /* Checks the functional clock of LPIT module */
98  (void)clkErr;
99  DEV_ASSERT(clkErr == STATUS_SUCCESS);
100  DEV_ASSERT(s_lpitSourceClockFrequency[instance] > 0U);
101 
102  base = s_lpitBase[instance];
103  /* Resets LPIT module */
104  LPIT_HAL_Reset(base);
105  /* Enables functional clock of LPIT module*/
106  LPIT_HAL_Enable(base);
107  /* Sets LPIT operation in Debug and DOZE mode*/
110 }
111 
112 /*FUNCTION**********************************************************************
113  *
114  * Function Name : LPIT_DRV_Deinit
115  * Description : De-initializes LPIT module.
116  * This function disables LPIT module.
117  * In order to use the LPIT module again, LPIT_DRV_Init must be called.
118  *
119  * Implements : LPIT_DRV_Deinit_Activity
120  *END**************************************************************************/
121 void LPIT_DRV_Deinit(uint32_t instance)
122 {
123  LPIT_Type * base;
124 
125  DEV_ASSERT(instance < LPIT_INSTANCE_COUNT);
126 
127  base = s_lpitBase[instance];
128  /* Disables LPIT module functional clock*/
129  LPIT_HAL_Disable(base);
130 }
131 
132 /*FUNCTION**********************************************************************
133  *
134  * Function Name : LPIT_DRV_InitChannel
135  * Description : Initializes LPIT channel.
136  * This function initializes the LPIT timers by using a channel, this function
137  * configures timer channel chaining, timer channel mode, timer channel period,
138  * interrupt generation, trigger source, trigger select, reload on trigger,
139  * stop on interrupt and start on trigger.
140  * The timer channel number and its configuration structure shall be passed as arguments.
141  * Timer channels do not start counting by default after calling this function.
142  * The function LPIT_DRV_StartTimerChannels must be called to start the timer channel counting.
143  * In order to re-configures the period, call the LPIT_DRV_SetTimerPeriodByUs or
144  * LPIT_DRV_SetTimerPeriodByCount.
145  *
146  * Implements : LPIT_DRV_InitChannel_Activity
147  *END**************************************************************************/
148 status_t LPIT_DRV_InitChannel(uint32_t instance,
149  uint32_t channel,
150  const lpit_user_channel_config_t * userChannelConfig)
151 {
152  LPIT_Type * base;
153  status_t reVal = STATUS_SUCCESS;
154  const IRQn_Type lpitIrqId[] = LPIT_IRQS;
155 
156  DEV_ASSERT(instance < LPIT_INSTANCE_COUNT);
157  DEV_ASSERT(userChannelConfig != NULL);
158  DEV_ASSERT(channel < LPIT_TMR_COUNT);
159 
160  base = s_lpitBase[instance];
161 
162  if ((channel == 0U) && (userChannelConfig->chainChannel))
163  {
164  reVal = STATUS_ERROR;
165  }
166  else
167  {
168  /* Setups the timer channel chaining */
169  LPIT_HAL_SetTimerChannelChainCmd(base, channel, userChannelConfig->chainChannel);
170  /* Setups the timer channel operation mode */
171  LPIT_HAL_SetTimerChannelModeCmd(base, channel, userChannelConfig->timerMode);
172  if (userChannelConfig->periodUnits == LPIT_PERIOD_UNITS_MICROSECONDS)
173  {
174  /* Setups timer channel period in microsecond unit */
175  reVal = LPIT_DRV_SetTimerPeriodByUs(instance, channel, userChannelConfig->period);
176  }
177  else
178  {
179  /* Setups timer channel period in count unit */
180  LPIT_DRV_SetTimerPeriodByCount(instance, channel, userChannelConfig->period);
181  }
182 
183  if (reVal == STATUS_SUCCESS)
184  {
185  /* Setups the timer channel trigger source, trigger select, reload on trigger,
186  stop on timeout, start on trigger and channel chaining */
187  LPIT_HAL_SetTriggerSourceCmd(base, channel, userChannelConfig->triggerSource);
188  LPIT_HAL_SetTriggerSelectCmd(base, channel, userChannelConfig->triggerSelect);
189  LPIT_HAL_SetReloadOnTriggerCmd(base, channel, userChannelConfig->enableReloadOnTrigger);
190  LPIT_HAL_SetStopOnInterruptCmd(base, channel, userChannelConfig->enableStopOnInterrupt);
191  LPIT_HAL_SetStartOnTriggerCmd(base, channel, userChannelConfig->enableStartOnTrigger);
192  /* Setups interrupt generation for timer channel */
193  if (userChannelConfig->isInterruptEnabled)
194  {
195  /* Enables interrupt generation */
196  LPIT_HAL_EnableInterruptTimerChannels(base, (uint32_t)1U << channel);
197  INT_SYS_EnableIRQ(lpitIrqId[channel]);
198  }
199  else
200  {
201  /* Disables interrupt generation */
202  LPIT_HAL_DisableInterruptTimerChannels(base, (uint32_t)1U << channel);
203  INT_SYS_DisableIRQ(lpitIrqId[channel]);
204  }
205  }
206  }
207  return reVal;
208 }
209 
210 /*FUNCTION**********************************************************************
211  *
212  * Function Name : LPIT_DRV_StartTimerChannels
213  * Description : Starts timer channel counting.
214  * This function allows starting timer channels simultaneously .
215  * After calling this function, timer channels are going operate depend on mode and
216  * control bits which controls timer channel start, reload and restart.
217  *
218  * Implements : LPIT_DRV_StartTimerChannels_Activity
219  *END**************************************************************************/
220 void LPIT_DRV_StartTimerChannels(uint32_t instance,
221  uint32_t mask)
222 {
223  LPIT_Type * base;
224 
225  DEV_ASSERT(instance < LPIT_INSTANCE_COUNT);
226  DEV_ASSERT(mask < (1UL << LPIT_TMR_COUNT));
227 
228  base = s_lpitBase[instance];
229  /* Starts timer channel counting */
230  LPIT_HAL_StartTimerChannels(base, mask);
231 }
232 
233 /*FUNCTION**********************************************************************
234  *
235  * Function Name : LPIT_DRV_StopTimerChannels
236  * Description : Stop timer channel from counting.
237  * This function allows stop timer channels simultaneously from counting.
238  * Timer channels reload their periods respectively after the next time
239  * they call the LPIT_DRV_StartTimerChannels. Note that: In 32-bit Trigger Accumulator
240  * mode, the counter will load on the first trigger rising edge.
241  *
242  * Implements : LPIT_DRV_StopTimerChannels_Activity
243  *END**************************************************************************/
244 void LPIT_DRV_StopTimerChannels(uint32_t instance,
245  uint32_t mask)
246 {
247  LPIT_Type * base;
248 
249  DEV_ASSERT(instance < LPIT_INSTANCE_COUNT);
250  DEV_ASSERT(mask < (1UL << LPIT_TMR_COUNT));
251 
252  base = s_lpitBase[instance];
253  /* Stops timer channel from counting */
254  LPIT_HAL_StopTimerChannels(base, mask);
255 }
256 
257 /*FUNCTION**********************************************************************
258  *
259  * Function Name : LPIT_DRV_SetTimerPeriodByUs
260  * Description : Sets timer channel period in microseconds unit.
261  * This function sets the timer channel period in microseconds
262  * when timer channel mode is 32 bit periodic or dual 16 bit counter mode.
263  * The period range depends on the frequency of the LPIT functional clock and
264  * operation mode of timer channel.
265  * If the required period is out of range, use the suitable mode if applicable.
266  * This function is only valid for one single channel.
267  *
268  * Implements : LPIT_DRV_SetTimerPeriodByUs_Activity
269  *END**************************************************************************/
271  uint32_t channel,
272  uint32_t periodUs)
273 {
274  LPIT_Type * base;
275  lpit_timer_modes_t timerMode;
276  status_t clkErr;
277  status_t reVal = STATUS_SUCCESS;
278  uint64_t count;
279 
280  DEV_ASSERT(instance < LPIT_INSTANCE_COUNT);
281  DEV_ASSERT(channel < LPIT_TMR_COUNT);
282 
283  /* Gets current functional clock frequency of LPIT instance */
284  clkErr = CLOCK_SYS_GetFreq(s_lpitClkNames[instance], &s_lpitSourceClockFrequency[instance]);
285  /* Checks the functional clock of LPIT module */
286  (void)clkErr;
287  DEV_ASSERT(clkErr == STATUS_SUCCESS);
288  DEV_ASSERT(s_lpitSourceClockFrequency[instance] > 0U);
289 
290  base = s_lpitBase[instance];
291  /* Calculates the count value, assign it to timer channel counter register.*/
292  count = ((uint64_t)periodUs) * s_lpitSourceClockFrequency[instance];
293  count = (count / 1000000U) - 1U;
294  /* Gets current timer channel operation mode */
295  timerMode = LPIT_HAL_GetTimerChannelModeCmd(base, channel);
296  /* Checks whether the count is valid with timer channel operation mode */
297  if (count <= MAX_PERIOD_COUNT)
298  {
299  if (timerMode == LPIT_DUAL_PERIODIC_COUNTER)
300  {
302  {
303  reVal = STATUS_ERROR;
304  }
305  else
306  {
307  if (count > MAX_PERIOD_COUNT_16_BIT)
308  {
309  /* Calculates the count value for dual 16 bit periodic counter mode */
310  count = ((count - (MAX_PERIOD_COUNT_16_BIT + 1U)) << 16U)
312  }
313  }
314  }
315  }
316  else
317  {
318  reVal = STATUS_ERROR;
319  }
320  if (reVal == STATUS_SUCCESS)
321  {
322  /* Sets the timer channel period in count unit */
323  LPIT_HAL_SetTimerPeriodByCount(base, channel, (uint32_t)count);
324  }
325  return reVal;
326 }
327 
328 /*FUNCTION**********************************************************************
329  *
330  * Function Name : LPIT_DRV_SetTimerPeriodInDual16ModeByUs
331  * Description : Sets timer channel period in microseconds unit.
332  * This function sets the timer channel period in microseconds
333  * when timer channel mode is dual 16 bit periodic counter mode.
334  * The period range depends on the frequency of the LPIT functional clock and
335  * operation mode of timer channel.
336  * If the required period is out of range, use the suitable mode if applicable.
337  * This function is only valid for one single channel.
338  *
339  * Implements : LPIT_DRV_SetTimerPeriodInDual16ModeByUs_Activity
340  *END**************************************************************************/
342  uint32_t channel,
343  uint16_t periodHigh,
344  uint16_t periodLow)
345 {
346  LPIT_Type * base;
347  status_t reVal = STATUS_SUCCESS;
348  status_t clkErr;
349  uint64_t periodHighCount;
350  uint64_t periodLowCount;
351  uint64_t periodCount;
352 
353  DEV_ASSERT(instance < LPIT_INSTANCE_COUNT);
354  DEV_ASSERT(channel < LPIT_TMR_COUNT);
355 
356  /* Gets current functional clock frequency of LPIT instance */
357  clkErr = CLOCK_SYS_GetFreq(s_lpitClkNames[instance], &s_lpitSourceClockFrequency[instance]);
358  /* Checks the functional clock of LPIT module */
359  (void)clkErr;
360  DEV_ASSERT(clkErr == STATUS_SUCCESS);
361  DEV_ASSERT(s_lpitSourceClockFrequency[instance] > 0U);
362 
363  base = s_lpitBase[instance];
364  /* Calculates the count value of 16 bit higher period.*/
365  periodHighCount = ((uint64_t)periodHigh) * s_lpitSourceClockFrequency[instance];
366  periodHighCount = (periodHighCount / 1000000U) - 1U;
367 
368  /* Calculates the count value of 16 bit lower period.*/
369  periodLowCount = ((uint64_t)periodLow) * s_lpitSourceClockFrequency[instance];
370  periodLowCount = (periodLowCount / 1000000U) - 1U;
371  /* Checks whether the count is valid */
372  if ((periodHighCount > MAX_PERIOD_COUNT_16_BIT) || (periodLowCount > MAX_PERIOD_COUNT_16_BIT))
373  {
374  reVal = STATUS_ERROR;
375  }
376  else
377  {
378  periodCount = (periodHighCount << 16U) | periodLowCount;
379  LPIT_HAL_SetTimerPeriodByCount(base, channel, (uint32_t)periodCount);
380  }
381 
382  return reVal;
383 }
384 
385 /*FUNCTION**********************************************************************
386  *
387  * Function Name : LPIT_DRV_GetTimerPeriodByUs
388  * Description : Gets the timer channel period in microseconds.
389  * The returned period here makes sense if the operation mode of timer channel
390  * is 32 bit periodic counter or dual 16 bit periodic counter.
391  *
392  * Implements : LPIT_DRV_GetTimerPeriodByUs_Activity
393  *END**************************************************************************/
394 uint64_t LPIT_DRV_GetTimerPeriodByUs(uint32_t instance,
395  uint32_t channel)
396 {
397  const LPIT_Type * base;
398  status_t clkErr;
399  lpit_timer_modes_t timerMode;
400  uint64_t currentPeriod;
401 
402  DEV_ASSERT(instance < LPIT_INSTANCE_COUNT);
403  DEV_ASSERT(channel < LPIT_TMR_COUNT);
404 
405  /* Gets current functional clock frequency of LPIT instance */
406  clkErr = CLOCK_SYS_GetFreq(s_lpitClkNames[instance], &s_lpitSourceClockFrequency[instance]);
407  /* Checks the functional clock of LPIT module */
408  (void)clkErr;
409  DEV_ASSERT(clkErr == STATUS_SUCCESS);
410  DEV_ASSERT(s_lpitSourceClockFrequency[instance] > 0U);
411 
412  base = s_lpitBase[instance];
413  /* Gets current timer channel period in count.*/
414  currentPeriod = LPIT_HAL_GetTimerPeriodByCount(base, channel);
415  /* Gets current timer channel operation mode */
416  timerMode = LPIT_HAL_GetTimerChannelModeCmd(base, channel);
417 
418  if (timerMode == LPIT_DUAL_PERIODIC_COUNTER)
419  {
420  if (currentPeriod > MAX_PERIOD_COUNT_16_BIT)
421  {
422  currentPeriod = (((currentPeriod >> 16U) + (currentPeriod & MAX_PERIOD_COUNT_16_BIT) + 2U) * 1000000U)
423  / s_lpitSourceClockFrequency[instance];
424  }
425  else
426  {
427  /* Converts period from count unit to microseconds unit for other modes */
428  currentPeriod = ((currentPeriod + 1U) * 1000000U) / s_lpitSourceClockFrequency[instance];
429  }
430  }
431  else
432  {
433  /* Converts period from count unit to microseconds unit for other modes */
434  currentPeriod = ((currentPeriod + 1U) * 1000000U) / s_lpitSourceClockFrequency[instance];
435  }
436  return currentPeriod;
437 }
438 
439 /*FUNCTION**********************************************************************
440  *
441  * Function Name : LPIT_DRV_GetCurrentTimerUs
442  * Description : Gets current timer channel counting value in microseconds unit.
443  * This function returns an absolute time stamp in microseconds.
444  * One common use of this function is to measure the running time of a part of
445  * code. Call this function at both the beginning and end of code. The time
446  * difference between these two time stamps is the running time.
447  * The return counting value here makes sense if the operation mode of timer channel
448  * is 32 bit periodic counter or dual 16 bit periodic counter or 32-bit trigger input capture.
449  * Need to make sure the running time will not exceed the timer channel period.
450  *
451  * Implements : LPIT_DRV_GetCurrentTimerUs_Activity
452  *END**************************************************************************/
453 uint64_t LPIT_DRV_GetCurrentTimerUs(uint32_t instance,
454  uint32_t channel)
455 {
456  const LPIT_Type * base;
457  status_t clkErr;
458  lpit_timer_modes_t timerMode;
459  uint64_t currentTime = 0U;
460 
461  DEV_ASSERT(instance < LPIT_INSTANCE_COUNT);
462  DEV_ASSERT(channel < LPIT_TMR_COUNT);
463 
464  base = s_lpitBase[instance];
465  /* Gets current timer channel counting value */
466  currentTime = LPIT_HAL_GetCurrentTimerCount(base, channel);
467  /* Gets current timer channel operation mode */
468  timerMode = LPIT_HAL_GetTimerChannelModeCmd(base, channel);
469  /* Gets current functional clock frequency of LPIT instance */
470  clkErr = CLOCK_SYS_GetFreq(s_lpitClkNames[instance], &s_lpitSourceClockFrequency[instance]);
471  (void)clkErr;
472  DEV_ASSERT(s_lpitSourceClockFrequency[instance] > 0U);
473 
474  if (timerMode == LPIT_DUAL_PERIODIC_COUNTER)
475  {
476  currentTime = (((currentTime >> 16U) + (currentTime & MAX_PERIOD_COUNT_16_BIT)) * 1000000U)
477  / s_lpitSourceClockFrequency[instance];
478  }
479  else
480  {
481  /* Converts counting value to microseconds unit for other modes */
482  currentTime = (currentTime * 1000000U) / s_lpitSourceClockFrequency[instance];
483  }
484 
485  return currentTime;
486 }
487 
488 /*FUNCTION**********************************************************************
489  *
490  * Function Name : LPIT_DRV_SetTimerPeriodByCount
491  * Description : Sets the timer channel period in count unit.
492  * This function sets the timer channel period in count unit.
493  * The counter period of a running timer channel can be modified by first setting
494  * a new load value, the value will be loaded after the timer channel expires.
495  * To abort the current cycle and start a timer channel period with the new value,
496  * the timer channel must be disabled and enabled again.
497  *
498  * Implements : LPIT_DRV_SetTimerPeriodByCount_Activity
499  *END**************************************************************************/
500 void LPIT_DRV_SetTimerPeriodByCount(uint32_t instance,
501  uint32_t channel,
502  uint32_t count)
503 {
504  LPIT_Type * base;
505 
506  DEV_ASSERT(instance < LPIT_INSTANCE_COUNT);
507  DEV_ASSERT(channel < LPIT_TMR_COUNT);
508 
509  base = s_lpitBase[instance];
510  /* Sets the timer channel period in count unit */
511  LPIT_HAL_SetTimerPeriodByCount(base, channel, count);
512 
513 }
514 
515 /*FUNCTION**********************************************************************
516  *
517  * Function Name : LPIT_DRV_SetTimerPeriodInDual16ModeByCount
518  * Description : Sets the timer channel period in count unit.
519  * This function sets the timer channel period in count unit when timer channel
520  * mode is dual 16 periodic counter mode.
521  * The counter period of a running timer channel can be modified by first setting
522  * a new load value, the value will be loaded after the timer channel expires.
523  * To abort the current cycle and start a timer channel period with the new value,
524  * the timer channel must be disabled and enabled again.
525  *
526  * Implements : LPIT_DRV_SetTimerPeriodInDual16ModeByCount_Activity
527  *END**************************************************************************/
529  uint32_t channel,
530  uint16_t periodHigh,
531  uint16_t periodLow)
532 {
533  LPIT_Type * base;
534  uint32_t period;
535 
536  DEV_ASSERT(instance < LPIT_INSTANCE_COUNT);
537  DEV_ASSERT(channel < LPIT_TMR_COUNT);
538 
539  base = s_lpitBase[instance];
540 
541  period = ((uint32_t)periodHigh << 16U) | periodLow;
542  /* Sets the timer channel period in count unit */
543  LPIT_HAL_SetTimerPeriodByCount(base, channel, period);
544 
545 }
546 
547 /*FUNCTION**********************************************************************
548  *
549  * Function Name : LPIT_DRV_GetTimerPeriodByCount
550  * Description : Gets the current timer channel period in count unit.
551  *
552  * Implements : LPIT_DRV_GetTimerPeriodByCount_Activity
553  *END**************************************************************************/
554 uint32_t LPIT_DRV_GetTimerPeriodByCount(uint32_t instance,
555  uint32_t channel)
556 {
557  const LPIT_Type * base;
558  lpit_timer_modes_t timerMode;
559  uint32_t currentPeriod;
560 
561  DEV_ASSERT(instance < LPIT_INSTANCE_COUNT);
562  DEV_ASSERT(channel < LPIT_TMR_COUNT);
563 
564  base = s_lpitBase[instance];
565  /* Gets current timer channel period by count.*/
566  currentPeriod = LPIT_HAL_GetTimerPeriodByCount(base, channel);
567  /* Gets current timer channel operation mode */
568  timerMode = LPIT_HAL_GetTimerChannelModeCmd(base, channel);
569 
570  if (timerMode == LPIT_DUAL_PERIODIC_COUNTER)
571  {
572  /* Calculates the period for dual 16 bit periodic counter mode */
573  currentPeriod = (currentPeriod >> 16U) + (currentPeriod & MAX_PERIOD_COUNT_16_BIT);
574  }
575  return currentPeriod;
576 }
577 
578 /*FUNCTION**********************************************************************
579  *
580  * Function Name : LPIT_DRV_GetCurrentTimerCount
581  * Description : Gets the current timer channel counting value in count.
582  * This function returns the real-time timer channel counting value, the value in
583  * a range from 0 to timer channel period.
584  * Need to make sure the running time does not exceed the timer channel period.
585  *
586  * Implements : LPIT_DRV_GetCurrentTimerCount_Activity
587  *END**************************************************************************/
588 uint32_t LPIT_DRV_GetCurrentTimerCount(uint32_t instance,
589  uint32_t channel)
590 {
591  const LPIT_Type * base;
592  lpit_timer_modes_t timerMode;
593  uint32_t currentTime;
594 
595  DEV_ASSERT(instance < LPIT_INSTANCE_COUNT);
596  DEV_ASSERT(channel < LPIT_TMR_COUNT);
597 
598  base = s_lpitBase[instance];
599  /* Gets current timer channel counting value */
600  currentTime = LPIT_HAL_GetCurrentTimerCount(base, channel);
601  /* Gets current timer channel operation mode */
602  timerMode = LPIT_HAL_GetTimerChannelModeCmd(base, channel);
603 
604  if (timerMode == LPIT_DUAL_PERIODIC_COUNTER)
605  {
606  /* Calculates the current counting value for dual 16 bit periodic counter mode */
607  currentTime = (currentTime >> 16U) + (currentTime & MAX_PERIOD_COUNT_16_BIT);
608  }
609  return currentTime;
610 }
611 
612 /*FUNCTION**********************************************************************
613  *
614  * Function Name : LPIT_DRV_GetInterruptFlagTimerChannels
615  * Description : Gets the current interrupt flag of timer channels.
616  * In compare modes, the flag sets to 1 at the end of the timer period.
617  * In capture modes, the flag sets to 1 when the trigger asserts.
618  *
619  * Implements : LPIT_DRV_GetInterruptFlagTimerChannels_Activity
620  *END**************************************************************************/
621 uint32_t LPIT_DRV_GetInterruptFlagTimerChannels(uint32_t instance,
622  uint32_t mask)
623 {
624  const LPIT_Type * base;
625 
626  DEV_ASSERT(instance < LPIT_INSTANCE_COUNT);
627  DEV_ASSERT(mask < (1UL << LPIT_TMR_COUNT));
628 
629  base = s_lpitBase[instance];
630  /* Gets the interrupt flag for timer channels */
631  return LPIT_HAL_GetInterruptFlagTimerChannels(base, mask);
632 }
633 
634 /*FUNCTION**********************************************************************
635  *
636  * Function Name : LPIT_DRV_ClearInterruptFlagTimerChannels
637  * Description : Clears the interrupt flag of timer channels.
638  * This function clears the interrupt flag of timer channels after
639  * their interrupt event occurred.
640  * Implements : LPIT_DRV_ClearInterruptFlagTimerChannels_Activity
641  *END**************************************************************************/
643  uint32_t mask)
644 {
645  LPIT_Type * base;
646 
647  DEV_ASSERT(instance < LPIT_INSTANCE_COUNT);
648  DEV_ASSERT(mask < (1UL << LPIT_TMR_COUNT));
649 
650  base = s_lpitBase[instance];
651  /* Clears the interrupt flag for timer channels */
653 }
654 
655 /*******************************************************************************
656  * EOF
657  ******************************************************************************/
static void LPIT_HAL_SetStartOnTriggerCmd(LPIT_Type *const base, uint32_t channel, bool isStartOnTrigger)
Sets timer channel start on trigger.
Definition: lpit_hal.h:538
static void LPIT_HAL_SetStopOnInterruptCmd(LPIT_Type *const base, uint32_t channel, bool isStopOnInterrupt)
Sets timer channel stop on interrupt.
Definition: lpit_hal.h:517
#define MAX_PERIOD_COUNT_IN_DUAL_16BIT_MODE
Max period in count of dual 16 bit periodic counter mode.
Definition: lpit_driver.h:59
#define LPIT_BASE_PTRS
Definition: S32K144.h:5977
lpit_timer_modes_t timerMode
Definition: lpit_driver.h:97
lpit_timer_modes_t
Mode options available for the LPIT timer Implements : lpit_timer_modes_t_Class.
Definition: lpit_hal.h:60
static void LPIT_HAL_SetReloadOnTriggerCmd(LPIT_Type *const base, uint32_t channel, bool isReloadOnTrigger)
Sets timer channel reload on trigger.
Definition: lpit_hal.h:497
static void LPIT_HAL_SetTimerChannelModeCmd(LPIT_Type *const base, uint32_t channel, lpit_timer_modes_t mode)
Sets operation mode of timer channel.
Definition: lpit_hal.h:400
static uint32_t LPIT_HAL_GetInterruptFlagTimerChannels(const LPIT_Type *base, uint32_t mask)
Gets the interrupt flag of timer channels.
Definition: lpit_hal.h:351
static void LPIT_HAL_SetTriggerSourceCmd(LPIT_Type *const base, uint32_t channel, lpit_trigger_source_t triggerSource)
Sets trigger source of timer channel.
Definition: lpit_hal.h:477
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:500
static void LPIT_HAL_EnableInterruptTimerChannels(LPIT_Type *const base, uint32_t mask)
Enables the interrupt generation for timer channels.
Definition: lpit_hal.h:310
void LPIT_DRV_StopTimerChannels(uint32_t instance, uint32_t mask)
Stops the timer channel counting.
Definition: lpit_driver.c:244
static void LPIT_HAL_SetTimerRunInDebugCmd(LPIT_Type *const base, bool isRunInDebug)
Sets operation of LPIT in debug mode.
Definition: lpit_hal.h:580
uint64_t LPIT_DRV_GetTimerPeriodByUs(uint32_t instance, uint32_t channel)
Gets the timer channel period in microseconds.
Definition: lpit_driver.c:394
IRQn_Type
Defines the Interrupt Numbers definitions.
Definition: S32K144.h:269
static void LPIT_HAL_DisableInterruptTimerChannels(LPIT_Type *const base, uint32_t mask)
Disables the interrupt generation for timer channels.
Definition: lpit_hal.h:330
lpit_trigger_source_t triggerSource
Definition: lpit_driver.h:100
uint64_t LPIT_DRV_GetCurrentTimerUs(uint32_t instance, uint32_t channel)
Gets the current timer channel counting value in microseconds.
Definition: lpit_driver.c:453
static uint32_t LPIT_HAL_GetCurrentTimerCount(const LPIT_Type *base, uint32_t channel)
Gets the current timer channel counting value.
Definition: lpit_hal.h:283
static LPIT_Type *const s_lpitBase[]
Definition: lpit_driver.c:64
static void LPIT_HAL_StopTimerChannels(LPIT_Type *const base, uint32_t mask)
Stops the timer channel from counting.
Definition: lpit_hal.h:198
void INT_SYS_DisableIRQ(IRQn_Type irqNumber)
Disables an interrupt for a given IRQ number.
#define LPIT_TMR_COUNT
Definition: S32K144.h:5945
#define DEV_ASSERT(x)
Definition: devassert.h:78
static void LPIT_HAL_SetTriggerSelectCmd(LPIT_Type *const base, uint32_t channel, uint32_t triggerChannelSelect)
Sets internal trigger source for timer channel.
Definition: lpit_hal.h:459
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:341
#define LPIT_INSTANCE_COUNT
Definition: S32K144.h:5966
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:86
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:528
lpit_period_units_t periodUnits
Definition: lpit_driver.h:98
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:148
void LPIT_DRV_ClearInterruptFlagTimerChannels(uint32_t instance, uint32_t mask)
Clears the interrupt flag of timer channels.
Definition: lpit_driver.c:642
status_t
Status return codes. Common error codes will be a unified enumeration (C enum) that will contain all ...
Definition: status.h:31
static void LPIT_HAL_SetTimerChannelChainCmd(LPIT_Type *const base, uint32_t channel, bool isChannelChained)
Sets timer channel chaining.
Definition: lpit_hal.h:558
static void LPIT_HAL_Enable(LPIT_Type *const base)
Enables the LPIT module.
Definition: lpit_hal.h:118
LPIT configuration structure.
Definition: lpit_driver.h:82
static void LPIT_HAL_Disable(LPIT_Type *const base)
Disables the LPIT module.
Definition: lpit_hal.h:132
static const clock_names_t s_lpitClkNames[LPIT_INSTANCE_COUNT]
Definition: lpit_driver.c:66
#define MAX_PERIOD_COUNT
Max period in count of all operation mode except for dual 16 bit periodic counter mode...
Definition: lpit_driver.h:57
static uint32_t LPIT_HAL_GetTimerPeriodByCount(const LPIT_Type *base, uint32_t channel)
Gets the timer channel period in count unit.
Definition: lpit_hal.h:265
Structure to configure the channel timer.
Definition: lpit_driver.h:95
clock_names_t
Clock names.
static void LPIT_HAL_SetTimerRunInDozeCmd(LPIT_Type *const base, bool isRunInDoze)
Sets operation of LPIT in DOZE mode.
Definition: lpit_hal.h:600
static uint32_t s_lpitSourceClockFrequency[LPIT_INSTANCE_COUNT]
Definition: lpit_driver.c:68
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:554
#define MAX_PERIOD_COUNT_16_BIT
Max count of 16 bit.
Definition: lpit_driver.h:61
uint32_t LPIT_DRV_GetInterruptFlagTimerChannels(uint32_t instance, uint32_t mask)
Gets the current interrupt flag of timer channels.
Definition: lpit_driver.c:621
static void LPIT_HAL_ClearInterruptFlagTimerChannels(LPIT_Type *const base, uint32_t mask)
Clears the interrupt flag of timer channels.
Definition: lpit_hal.h:371
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:270
void LPIT_DRV_Deinit(uint32_t instance)
De-Initializes the LPIT module.
Definition: lpit_driver.c:121
static void LPIT_HAL_StartTimerChannels(LPIT_Type *const base, uint32_t mask)
Starts the timer channel counting.
Definition: lpit_hal.h:175
static void LPIT_HAL_Reset(LPIT_Type *const base)
Resets the LPIT module.
Definition: lpit_hal.h:146
void LPIT_DRV_StartTimerChannels(uint32_t instance, uint32_t mask)
Starts the timer channel counting.
Definition: lpit_driver.c:220
static lpit_timer_modes_t LPIT_HAL_GetTimerChannelModeCmd(const LPIT_Type *base, uint32_t channel)
Gets current operation mode of timer channel.
Definition: lpit_hal.h:418
static void LPIT_HAL_SetTimerPeriodByCount(LPIT_Type *const base, uint32_t channel, uint32_t count)
Sets the timer channel period in count unit.
Definition: lpit_hal.h:248
#define LPIT_IRQS
Definition: S32K144.h:5983
uint32_t LPIT_DRV_GetCurrentTimerCount(uint32_t instance, uint32_t channel)
Gets the current timer channel counting value in count.
Definition: lpit_driver.c:588