This section describes the programming interface of the PIT Peripheral driver. The PIT driver configures PIT timers and initializes and configures timer periods.
PIT Initialization
-
To initialize the PIT module, call the PIT_DRV_Init function. This function enables the PIT module and clock automatically.
-
The parameter passed in the PIT_DRV_Init configures the timers to run or stop in debug mode. To use one timer channel, call the PIT_DRV_InitChannel initialize that channel.
This is example code to initialize and configure the driver:
const pit_config_t pitInit = {
isInterruptEnabled = false,
isTimerChained = false,
periodUs = 20U
};
PIT Timer Period
The PIT driver provides four ways to set the timer period.
- The PIT_DRV_InitChannel function sets the timer period in microseconds. It is only applicable when initializing the channel.
- The void PIT_DRV_SetTimerPeriodByUs(uint32_t instance, uint32_t timer, uint32_t us) function sets the timer period in microseconds. It is applicable at any time.
- The void PIT_DRV_SetLifetimeTimerPeriodByUs(uint32_t instance, uint64_t us) function sets the lifetime timer period in microseconds. It only supports specific MCUs. Check the appropriate reference manual before using this function.
- The void PIT_HAL_SetTimerPeriodByCount(uint32_t instance, uint32_t timer, uint32_t count) function sets the timer period in units of count. To use this function, include the fsl_pit_hal.h.
To read the current timer counting value in microseconds, call the uint32_t PIT_DRV_ReadTimerUs(uint32_t instance, uint32_t timer) function.
PIT Timer Operation
After the timer setting is complete, call the void PIT_DRV_StartTimer(uint32_t timer) function to start timer counting. Call the void PIT_DRV_StopTimer(uint32_t instance, uint32_t timer) function to stop it at any time.
To close the PIT module entirely, call the void PIT_DRV_Deinit(uint32_t instance) function. This disables both the PIT module and the clock gate.
Defines a structure PitConfig and uses the PIT_DRV_InitChannel() function to make necessary initializations. The remaining functions can be used for PIT configuration.
- Note
- The timer chain feature is not valid in all devices. Check the fsl_pit_features.h for accurate settings. If it's not valid, the value set here is bypassed inside the PIT_DRV_InitChannel() function.
pit_status_t PIT_DRV_Init |
( |
uint32_t |
instance, |
|
|
bool |
isRunInDebug |
|
) |
| |
Call this function before calling any other PIT driver functions. This function un-gates the PIT clock and enables the PIT module. The isRunInDebug passed into function affects all timer channels.
- Parameters
-
instance | PIT module instance number. |
isRunInDebug | Timers run or stop in debug mode.
- true: Timers continue to run in debug mode.
- false: Timers stop in debug mode.
|
- Returns
- Error or success status returned by API.
This function disables all PIT interrupts and PIT clock. It then gates the PIT clock control. PIT_DRV_Init must be called to use PIT again.
- Parameters
-
instance | PIT module instance number. |
- Returns
- Error or success status returned by API.
void PIT_DRV_InitChannel |
( |
uint32_t |
instance, |
|
|
uint32_t |
channel, |
|
|
const pit_user_config_t * |
config |
|
) |
| |
This function initializes the PIT timers by using a channel. Pass in the timer number and its configuration structure. Timers do not start counting by default after calling this function. The function PIT_DRV_StartTimer must be called to start the timer counting. Call the PIT_DRV_SetTimerPeriodByUs to re-set the period.
This is an example demonstrating how to define a PIT channel configuration structure:
- Parameters
-
instance | PIT module instance number. |
channel | Timer channel number. |
config | PIT channel configuration structure. |
void PIT_DRV_StartTimer |
( |
uint32_t |
instance, |
|
|
uint32_t |
channel |
|
) |
| |
After calling this function, timers load period value, count down to 0 and then load the respective start value again. Each time a timer reaches 0, it generates a trigger pulse and sets the timeout interrupt flag.
- Parameters
-
instance | PIT module instance number. |
channel | Timer channel number. |
void PIT_DRV_StopTimer |
( |
uint32_t |
instance, |
|
|
uint32_t |
channel |
|
) |
| |
This function stops every timer counting. Timers reload their periods respectively after the next time they call the PIT_DRV_StartTimer.
- Parameters
-
instance | PIT module instance number. |
channel | Timer channel number. |
void PIT_DRV_SetTimerPeriodByUs |
( |
uint32_t |
instance, |
|
|
uint32_t |
channel, |
|
|
uint32_t |
us |
|
) |
| |
The period range depends on the frequency of the PIT source clock. If the required period is out of range, use the lifetime timer if applicable. This function is only valid for one channel. If channels are chained together, the period makes no sense.
- Parameters
-
instance | PIT module instance number. |
channel | Timer channel number. |
us | Timer period in microseconds. |
uint32_t PIT_DRV_GetTimerPeriodByUs |
( |
uint32_t |
instance, |
|
|
uint32_t |
channel |
|
) |
| |
- Parameters
-
instance | PIT module instance number. |
channel | Timer channel number. |
- Returns
- Timer period in microseconds.
uint32_t PIT_DRV_ReadTimerUs |
( |
uint32_t |
instance, |
|
|
uint32_t |
channel |
|
) |
| |
This function returns an absolute time stamp in microseconds. One common use of this function is to measure the running time of a part of code. Call this function at both the beginning and end of code. The time difference between these two time stamps is the running time. Make sure the running time does not exceed the timer period. The time stamp returned is up-counting.
- Parameters
-
instance | PIT module instance number. |
channel | Timer channel number. |
- Returns
- Current timer value in microseconds.
void PIT_DRV_SetTimerPeriodByCount |
( |
uint32_t |
instance, |
|
|
uint32_t |
channel, |
|
|
uint32_t |
count |
|
) |
| |
Timers begin counting from the value set by this function. The counter period of a running timer can be modified by first stopping the timer, setting a new load value, and starting the timer again. If timers are not restarted, the new value is loaded after the next trigger event.
- Parameters
-
instance | PIT module instance number. |
channel | Timer channel number |
count | Timer period in units of count |
uint32_t PIT_DRV_GetTimerPeriodByCount |
( |
uint32_t |
instance, |
|
|
uint32_t |
channel |
|
) |
| |
- Parameters
-
instance | PIT module instance number. |
channel | Timer channel number |
- Returns
- Timer period in units of count
uint32_t PIT_DRV_ReadTimerCount |
( |
uint32_t |
instance, |
|
|
uint32_t |
channel |
|
) |
| |
This function returns the real-time timer counting value in a range from 0 to a timer period.
- Parameters
-
instance | PIT module instance number. |
channel | Timer channel number |
- Returns
- Current timer counting value
void PIT_DRV_ClearIntFlag |
( |
uint32_t |
instance, |
|
|
uint32_t |
channel |
|
) |
| |
This function clears the timer interrupt flag after a timeout event occurs.
- Parameters
-
instance | PIT module instance number. |
channel | Timer channel number |
bool PIT_DRV_IsIntPending |
( |
uint32_t |
instance, |
|
|
uint32_t |
channel |
|
) |
| |
This flag is set every time the timer counts to 0.
- Parameters
-
instance | PIT module instance number. |
channel | Timer channel number |
- Returns
- Current status of the timeout flag
- true: Timeout has occurred.
- false: Timeout has not yet occurred.
PIT_Type* const g_pitBase[] |