Kinetis SDK v.1.3 API Reference Manual  Rev. 0
Freescale Semiconductor, Inc.
 All Data Structures Functions Variables Typedefs Enumerations Enumerator Groups Pages
PIT Peripheral driver

Overview

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

  1. To initialize the PIT module, call the PIT_DRV_Init function. This function enables the PIT module and clock automatically.
  2. 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:

// Define device configuration.
const pit_config_t pitInit = {
isInterruptEnabled = false,// Disable timer interrupt.
isTimerChained = false, // Meaningless for timer 0.
periodUs = 20U // Set timer period to 20 us.
};
// Initialize PIT instance 0. Timers will stop running in debug mode.
PIT_DRV_Init(0, stop);
// Initialize PIT instance 0, timer 0.
PIT_DRV_InitChannel(0, 0, &pitInit);

PIT Timer Period

The PIT driver provides four ways to set the timer period.

  1. The PIT_DRV_InitChannel function sets the timer period in microseconds. It is only applicable when initializing the channel.
  2. 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.
  3. 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.
  4. 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.

Data Structures

struct  pit_user_config_t
 PIT timer configuration structure. More...
 

Variables

PIT_Type *const g_pitBase []
 Table of base addresses for pit instances. More...
 

Initialization and Shutdown

pit_status_t PIT_DRV_Init (uint32_t instance, bool isRunInDebug)
 Initializes the PIT module. More...
 
pit_status_t PIT_DRV_Deinit (uint32_t instance)
 Disables the PIT module and gate control. More...
 
void PIT_DRV_InitChannel (uint32_t instance, uint32_t channel, const pit_user_config_t *config)
 Initializes the PIT channel. More...
 

Timer Start and Stop

void PIT_DRV_StartTimer (uint32_t instance, uint32_t channel)
 Starts the timer counting. More...
 
void PIT_DRV_StopTimer (uint32_t instance, uint32_t channel)
 Stops the timer counting. More...
 

Timer Period

void PIT_DRV_SetTimerPeriodByUs (uint32_t instance, uint32_t channel, uint32_t us)
 Sets the timer period in microseconds. More...
 
uint32_t PIT_DRV_GetTimerPeriodByUs (uint32_t instance, uint32_t channel)
 Gets the timer period in microseconds for a single channel. More...
 
uint32_t PIT_DRV_ReadTimerUs (uint32_t instance, uint32_t channel)
 Reads the current timer value in microseconds. More...
 
void PIT_DRV_SetTimerPeriodByCount (uint32_t instance, uint32_t channel, uint32_t count)
 Sets the timer period in units of count. More...
 
uint32_t PIT_DRV_GetTimerPeriodByCount (uint32_t instance, uint32_t channel)
 Returns the current timer period in units of count. More...
 
uint32_t PIT_DRV_ReadTimerCount (uint32_t instance, uint32_t channel)
 Reads the current timer counting value. More...
 

Interrupt

void PIT_DRV_ClearIntFlag (uint32_t instance, uint32_t channel)
 Clears the timer interrupt flag. More...
 
bool PIT_DRV_IsIntPending (uint32_t instance, uint32_t channel)
 Reads the current timer timeout flag. More...
 

Data Structure Documentation

struct pit_user_config_t

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.

Data Fields

bool isInterruptEnabled
 Timer interrupt 0-disable/1-enable.
 
uint32_t periodUs
 Timer period in unit of microseconds.
 

Function Documentation

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
instancePIT module instance number.
isRunInDebugTimers 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.
pit_status_t PIT_DRV_Deinit ( uint32_t  instance)

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
instancePIT 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:

pit_user_config_t pitTestInit = {
// In unit of microseconds.
.periodUs = 1000,
};
Parameters
instancePIT module instance number.
channelTimer channel number.
configPIT 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
instancePIT module instance number.
channelTimer 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
instancePIT module instance number.
channelTimer 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
instancePIT module instance number.
channelTimer channel number.
usTimer period in microseconds.
uint32_t PIT_DRV_GetTimerPeriodByUs ( uint32_t  instance,
uint32_t  channel 
)
Parameters
instancePIT module instance number.
channelTimer 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
instancePIT module instance number.
channelTimer 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
instancePIT module instance number.
channelTimer channel number
countTimer period in units of count
uint32_t PIT_DRV_GetTimerPeriodByCount ( uint32_t  instance,
uint32_t  channel 
)
Parameters
instancePIT module instance number.
channelTimer 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
instancePIT module instance number.
channelTimer 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
instancePIT module instance number.
channelTimer channel number
bool PIT_DRV_IsIntPending ( uint32_t  instance,
uint32_t  channel 
)

This flag is set every time the timer counts to 0.

Parameters
instancePIT module instance number.
channelTimer channel number
Returns
Current status of the timeout flag
  • true: Timeout has occurred.
  • false: Timeout has not yet occurred.

Variable Documentation

PIT_Type* const g_pitBase[]