S32 SDK

Detailed Description

Low Power Interrupt Timer Peripheral Driver.

Hardware background

Each LPIT timer channel can be configured to run in one of 4 modes:
32-bit Periodic Counter: In this mode the counter will load and then decrement down to zero. It will then set the timer interrupt flag and assert the output pre-trigger.
Dual 16-bit Periodic Counter: In this mode, the counter will load and then the lower 16-bits will decrement down to zero, which will assert the output pre-trigger. The upper 16-bits will then decrement down to zero, which will negate the output pre-trigger and set the timer interrupt flag.
32-bit Trigger Accumulator: In this mode, the counter will load on the first trigger rising edge and then decrement down to zero on each trigger rising edge. It will then set the timer interrupt flag and assert the output pre-trigger.
32-bit Trigger Input Capture: In this mode, the counter will load with 0xFFFF_FFFF and then decrement down to zero. If a trigger rising edge is detected, it will store the inverse of the current counter value in the load value register, set the timer interrupt flag and assert the output pre-trigger.
In these modes, the timer channel operation is further controlled by Trigger Control bits (TSOT, TSOI, TROT) which control the load, reload, start and restart of the timer channels.

Driver consideration

The Driver uses structures for configuration. Each structure contains members that are specific to its respective functionality. There are lpit_user_config_t and lpit_user_channel_config_t.

Interrupt handling

Each LPIT timer channel has a corresponding interrupt handler. The LPIT Driver does not define interrupt handler internally. These interrupt handler methods can be defined by the user application. There are two ways to add an LPIT interrupt handler:

  1. Using the weak symbols defined by start-up code. if the methods LPITx_Handler(void) (x denotes instance number) are not defined, the linker use a default ISR. An error will be generated if methods with the same name are defined multiple times. This method works regardless of the placement of the interrupt vector table (Flash or RAM).
  2. Using the Interrupt Manager's INT_SYS_InstallHandler() method. This can be used to dynamically change the ISR at run-time. This method works only if the interrupt vector table is located in RAM.

Clocking configuration

The LPIT Driver does not handle clock setup (from PCC) configuration. This is handled by the Clock Manager. The driver assumes that clock configurations have been made, so it is the user's responsibility to set up clocking and pin configurations correctly.

Basic operations

  1. Pre-Initialization information of LPIT module
    • Before using the LPIT driver, the protocol clock of the module must be configured by the application using PCC module.
    • Configures Trigger MUX Control (TRGMUX) if want to use external trigger for LPIT module.
    • Configures different peripherals if want to use them in LPIT interrupt routine.
    • Provides configuration data structure to LPIT initialization API.
  2. To initialize the LPIT module, just call the LPIT_DRV_Init() function with the user configuration data structure. This function configures LPIT module operation when MCU enters DEBUG and DOZE (Low power mode) modes and enables LPIT module. This function must be called firstly.
    In the following code, LPIT module is initialized to continue to run when MCU enters both Debug and DOZE modes.
    #define BOARD_LPIT_INSTANCE 0U
    /* LPIT module configuration stucture */
    lpit_user_config_t lpitconfig =
    {
    .enableRunInDoze = true
    };
    /* Initializes the LPIT module. */
    LPIT_DRV_Init(BOARD_LPIT_INSTANCE, &lpitconfig);
  3. After calling the LPIT_DRV_Init() function, call LPIT_DRV_InitChannel() function with user channel configuration structure to initialize timer channel.
    This function configures timer channel chaining, timer channel mode, timer channel period, interrupt generation, trigger source, trigger select, reload on trigger, stop on interrupt and start on trigger. In the following code, timer channel is initialized with the channel chaining is disabled, interrupt generation is enabled, operation mode is 32 bit periodic counter mode, trigger source is external, reload on trigger is disabled, stop on interrupt is disabled, start on trigger is disabled and timer period is 0.5 second. Note that:
    • Trigger select is not effective if trigger source is external.
    • Timer channel period must be suitable for operation mode.
    • The timer channel 0 can not be chained.
      /* Channel 0 configuration structure */
      {
      .periodUnits = LPTMR_PERIOD_UNITS_MICROSECONDS,
      .period = 1000000U,
      .triggerSource = LPIT_TRIGGER_SOURCE_INTERNAL,
      .triggerSelect = 1U,
      .enableReloadOnTrigger = false,
      .enableStopOnInterrupt = false,
      .enableStartOnTrigger = false,
      .chainChannel = false,
      .isInterruptEnabled = true
      };
      /* Initializes the channel 0 */
      LPIT_DRV_InitChannel(BOARD_LPIT_INSTANCE, 0, &chnlconfig);
  4. To reconfigure timer channel period , just call LPIT_DRV_SetTimerPeriodByUs() or LPIT_DRV_SetTimerPeriodByCount() with corresponding new period. In the following code, the timer channel period is reconfigured with new period in count unit.
    /* Reconfigures timer channel period with new period of 10000 count*/
    LPIT_DRV_SetTimerPeriodByCount(BOARD_LPIT_INSTANCE, 0, 10000);
  5. To start timer channel counting, just call LPIT_DRV_StartTimerChannels() with timer channels starting mask. In the following code, the timer channel 0 is started with the mask of 0x1U.
    /* Starts channel 0 counting*/
    LPIT_DRV_StartTimerChannels(BOARD_LPIT_INSTANCE, 0x1U);
  6. To get interrupt flag of timer channel, just call LPIT_DRV_GetInterruptFlagTimerChannels() function with interrupt flag getting mask. In the following code, the interrupt flag of channel 0 is got with the mask of 0x1U.
    /* Gets interrupt flag of channel 0 */
    LPIT_DRV_GetInterruptFlagTimerChannels(BOARD_LPIT_INSTANCE, 0x1U);
  7. To clear interrupt flag of timer channel, just call LPIT_DRV_ClearInterruptFlagTimerChannels() function with interrupt flag clearing mask. In the following code, the interrupt flag of channel 0 is cleared with the mask of 0x1U.
    /* Clears interrupt flag of channel 0 */
    LPIT_DRV_ClearInterruptFlagTimerChannels(BOARD_LPIT_INSTANCE, 0x1U);
  8. To stop timer channel counting, just call LPIT_DRV_StopTimerChannels() with timer channels stopping mask. In the following code, the timer channel 0 is stopped with the mask of 0x1U.
    /* Stops channel 0 counting*/
    LPIT_DRV_StopTimerChannels(BOARD_LPIT_INSTANCE, 0x1U);
  9. To disable LPIT module, just call LPIT_DRV_Deinit().
    /* Disables LPIT module*/
    LPIT_DRV_Deinit(BOARD_LPIT_INSTANCE);

Data Structures

struct  lpit_user_config_t
 LPIT configuration structure. More...
 
struct  lpit_user_channel_config_t
 Structure to configure the channel timer. More...
 

Macros

#define MAX_PERIOD_COUNT   (0xFFFFFFFFU)
 Max period in count of all operation mode except for dual 16 bit periodic counter mode. More...
 
#define MAX_PERIOD_COUNT_IN_DUAL_16BIT_MODE   (0x1FFFEU)
 Max period in count of dual 16 bit periodic counter mode. More...
 
#define MAX_PERIOD_COUNT_16_BIT   (0xFFFFU)
 Max count of 16 bit. More...
 

Enumerations

enum  lpit_period_units_t { LPIT_PERIOD_UNITS_COUNTS = 0x00U, LPIT_PERIOD_UNITS_MICROSECONDS = 0x01U }
 Unit options for LPIT period. More...
 

Initialization and De-initialization

void LPIT_DRV_Init (uint32_t instance, const lpit_user_config_t *userConfig)
 Initializes the LPIT module. More...
 
void LPIT_DRV_Deinit (uint32_t instance)
 De-Initializes the LPIT module. More...
 
status_t LPIT_DRV_InitChannel (uint32_t instance, uint32_t channel, const lpit_user_channel_config_t *userChannelConfig)
 Initializes the LPIT channel. More...
 

Timer Start and Stop

void LPIT_DRV_StartTimerChannels (uint32_t instance, uint32_t mask)
 Starts the timer channel counting. More...
 
void LPIT_DRV_StopTimerChannels (uint32_t instance, uint32_t mask)
 Stops the timer channel counting. More...
 

Timer Period

status_t LPIT_DRV_SetTimerPeriodByUs (uint32_t instance, uint32_t channel, uint32_t periodUs)
 Sets the timer channel period in microseconds. More...
 
status_t LPIT_DRV_SetTimerPeriodInDual16ModeByUs (uint32_t instance, uint32_t channel, uint16_t periodHigh, uint16_t periodLow)
 Sets the timer channel period in microseconds. More...
 
uint64_t LPIT_DRV_GetTimerPeriodByUs (uint32_t instance, uint32_t channel)
 Gets the timer channel period in microseconds. More...
 
uint64_t LPIT_DRV_GetCurrentTimerUs (uint32_t instance, uint32_t channel)
 Gets the current timer channel counting value in microseconds. More...
 
void LPIT_DRV_SetTimerPeriodByCount (uint32_t instance, uint32_t channel, uint32_t count)
 Sets the timer channel period in count unit. More...
 
void LPIT_DRV_SetTimerPeriodInDual16ModeByCount (uint32_t instance, uint32_t channel, uint16_t periodHigh, uint16_t periodLow)
 Sets the timer channel period in count unit. More...
 
uint32_t LPIT_DRV_GetTimerPeriodByCount (uint32_t instance, uint32_t channel)
 Gets the current timer channel period in count unit. More...
 
uint32_t LPIT_DRV_GetCurrentTimerCount (uint32_t instance, uint32_t channel)
 Gets the current timer channel counting value in count. More...
 

Interrupt

uint32_t LPIT_DRV_GetInterruptFlagTimerChannels (uint32_t instance, uint32_t mask)
 Gets the current interrupt flag of timer channels. More...
 
void LPIT_DRV_ClearInterruptFlagTimerChannels (uint32_t instance, uint32_t mask)
 Clears the interrupt flag of timer channels. More...
 

Macro Definition Documentation

#define MAX_PERIOD_COUNT   (0xFFFFFFFFU)

Max period in count of all operation mode except for dual 16 bit periodic counter mode.

Definition at line 57 of file lpit_driver.h.

#define MAX_PERIOD_COUNT_16_BIT   (0xFFFFU)

Max count of 16 bit.

Definition at line 61 of file lpit_driver.h.

#define MAX_PERIOD_COUNT_IN_DUAL_16BIT_MODE   (0x1FFFEU)

Max period in count of dual 16 bit periodic counter mode.

Definition at line 59 of file lpit_driver.h.

Enumeration Type Documentation

Unit options for LPIT period.

This is used to determine unit of timer period Implements : lpit_period_units_t_Class

Enumerator
LPIT_PERIOD_UNITS_COUNTS 

Period value unit is count

LPIT_PERIOD_UNITS_MICROSECONDS 

Period value unit is microsecond

Definition at line 69 of file lpit_driver.h.

Function Documentation

void LPIT_DRV_ClearInterruptFlagTimerChannels ( uint32_t  instance,
uint32_t  mask 
)

Clears the interrupt flag of timer channels.

This function clears the interrupt flag of timer channels after their interrupt event occurred.

Parameters
[in]instanceLPIT module instance number
[in]maskThe interrupt flag clearing mask that decides which channels will be cleared interrupt flag
  • For example:
    • with mask = 0x01u then the interrupt flag of channel 0 only will be cleared
    • with mask = 0x02u then the interrupt flag of channel 1 only will be cleared
    • with mask = 0x03u then the interrupt flags of channel 0 and channel 1 will be cleared

Definition at line 642 of file lpit_driver.c.

void LPIT_DRV_Deinit ( uint32_t  instance)

De-Initializes the LPIT module.

This function disables LPIT module. In order to use the LPIT module again, LPIT_DRV_Init must be called.

Parameters
[in]instanceLPIT module instance number

Definition at line 121 of file lpit_driver.c.

uint32_t LPIT_DRV_GetCurrentTimerCount ( uint32_t  instance,
uint32_t  channel 
)

Gets the current timer channel counting value in count.

This function returns the real-time timer channel counting value, the value in a range from 0 to timer channel period. Need to make sure the running time does not exceed the timer channel period.

Parameters
[in]instanceLPIT module instance number
[in]channelTimer channel number
Returns
Current timer channel counting value in count

Definition at line 588 of file lpit_driver.c.

uint64_t LPIT_DRV_GetCurrentTimerUs ( uint32_t  instance,
uint32_t  channel 
)

Gets the current timer channel counting value in microseconds.

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. The return counting value here makes sense if the operation mode of timer channel is 32 bit periodic counter or dual 16 bit periodic counter or 32-bit trigger input capture. Need to make sure the running time will not exceed the timer channel period.

Parameters
[in]instanceLPIT module instance number
[in]channelTimer channel number
Returns
Current timer channel counting value in microseconds

Definition at line 453 of file lpit_driver.c.

uint32_t LPIT_DRV_GetInterruptFlagTimerChannels ( uint32_t  instance,
uint32_t  mask 
)

Gets the current interrupt flag of timer channels.

This function gets the current interrupt flag of timer channels. In compare modes, the flag sets to 1 at the end of the timer period. In capture modes, the flag sets to 1 when the trigger asserts.

Parameters
[in]instanceLPIT module instance number.
[in]maskThe interrupt flag getting mask that decides which channels will be got interrupt flag.
  • For example:
    • with mask = 0x01u then the interrupt flag of channel 0 only will be got
    • with mask = 0x02u then the interrupt flag of channel 1 only will be got
    • with mask = 0x03u then the interrupt flags of channel 0 and channel 1 will be got
Returns
Current the interrupt flag of timer channels

Definition at line 621 of file lpit_driver.c.

uint32_t LPIT_DRV_GetTimerPeriodByCount ( uint32_t  instance,
uint32_t  channel 
)

Gets the current timer channel period in count unit.

This function returns current period of timer channel given as argument.

Parameters
[in]instanceLPIT module instance number
[in]channelTimer channel number
Returns
Timer channel period in count unit

Definition at line 554 of file lpit_driver.c.

uint64_t LPIT_DRV_GetTimerPeriodByUs ( uint32_t  instance,
uint32_t  channel 
)

Gets the timer channel period in microseconds.

This function gets the timer channel period in microseconds. The returned period here makes sense if the operation mode of timer channel is 32 bit periodic counter or dual 16 bit periodic counter.

Parameters
[in]instanceLPIT module instance number
[in]channelTimer channel number
Returns
Timer channel period in microseconds

Definition at line 394 of file lpit_driver.c.

void LPIT_DRV_Init ( uint32_t  instance,
const lpit_user_config_t userConfig 
)

Initializes the LPIT module.

This function resets LPIT module, enables the LPIT module, configures LPIT module operation in Debug and DOZE mode. The LPIT configuration structure shall be passed as arguments. This configuration structure affects all timer channels. This function should be called before calling any other LPIT driver function.

This is an example demonstrating how to define a LPIT configuration structure:

1 lpit_user_config_t lpitInit =
2 {
3  .enableRunInDebug = false,
4  .enableRunInDoze = true
5 };
Parameters
[in]instanceLPIT module instance number.
[in]userConfigPointer to LPIT configuration structure.

Definition at line 86 of file lpit_driver.c.

status_t LPIT_DRV_InitChannel ( uint32_t  instance,
uint32_t  channel,
const lpit_user_channel_config_t userChannelConfig 
)

Initializes the LPIT channel.

This function initializes the LPIT timers by using a channel, this function configures timer channel chaining, timer channel mode, timer channel period, interrupt generation, trigger source, trigger select, reload on trigger, stop on interrupt and start on trigger. The timer channel number and its configuration structure shall be passed as arguments. Timer channels do not start counting by default after calling this function. The function LPIT_DRV_StartTimerChannels must be called to start the timer channel counting. In order to re-configures the period, call the LPIT_DRV_SetTimerPeriodByUs or LPIT_DRV_SetTimerPeriodByCount.

This is an example demonstrating how to define a LPIT channel configuration structure:

1 lpit_user_channel_config_t lpitTestInit =
2 {
3  .timerMode = LPIT_PERIODIC_COUNTER,
4  .periodUnits = LPTMR_PERIOD_UNITS_MICROSECONDS,
5  .period = 1000000U,
6  .triggerSource = LPIT_TRIGGER_SOURCE_INTERNAL,
7  .triggerSelect = 1U,
8  .enableReloadOnTrigger = false,
9  .enableStopOnInterrupt = false,
10  .enableStartOnTrigger = false,
11  .chainChannel = false,
12  .isInterruptEnabled = true
13 };
Parameters
[in]instanceLPIT module instance number
[in]channelTimer channel number
[in]userChannelConfigPointer to LPIT channel configuration structure
Returns
Operation status
  • STATUS_SUCCESS: Operation was successful.
  • STATUS_ERROR: The channel 0 is chained.
  • STATUS_ERROR: The input period is invalid.

Definition at line 148 of file lpit_driver.c.

void LPIT_DRV_SetTimerPeriodByCount ( uint32_t  instance,
uint32_t  channel,
uint32_t  count 
)

Sets the timer channel period in count unit.

This function sets the timer channel period in count unit. The counter period of a running timer channel can be modified by first setting a new load value, the value will be loaded after the timer channel expires. To abort the current cycle and start a timer channel period with the new value, the timer channel must be disabled and enabled again.

Parameters
[in]instanceLPIT module instance number
[in]channelTimer channel number
[in]countTimer channel period in count unit

Definition at line 500 of file lpit_driver.c.

status_t LPIT_DRV_SetTimerPeriodByUs ( uint32_t  instance,
uint32_t  channel,
uint32_t  periodUs 
)

Sets the timer channel period in microseconds.

This function sets the timer channel period in microseconds when timer channel mode is 32 bit periodic or dual 16 bit counter mode. The period range depends on the frequency of the LPIT functional clock and operation mode of timer channel. If the required period is out of range, use the suitable mode if applicable. This function is only valid for one single channel.

Parameters
[in]instanceLPIT module instance number
[in]channelTimer channel number
[in]periodUsTimer channel period in microseconds
Returns
Operation status
  • STATUS_SUCCESS: Input period of timer channel is valid.
  • STATUS_ERROR: Input period of timer channel is invalid.

Definition at line 270 of file lpit_driver.c.

void LPIT_DRV_SetTimerPeriodInDual16ModeByCount ( uint32_t  instance,
uint32_t  channel,
uint16_t  periodHigh,
uint16_t  periodLow 
)

Sets the timer channel period in count unit.

This function sets the timer channel period in count unit when timer channel mode is dual 16 periodic counter mode. The counter period of a running timer channel can be modified by first setting a new load value, the value will be loaded after the timer channel expires. To abort the current cycle and start a timer channel period with the new value, the timer channel must be disabled and enabled again.

Parameters
[in]instanceLPIT module instance number
[in]channelTimer channel number
[in]periodHighPeriod of higher 16 bit in count unit
[in]periodLowPeriod of lower 16 bit in count unit

Definition at line 528 of file lpit_driver.c.

status_t LPIT_DRV_SetTimerPeriodInDual16ModeByUs ( uint32_t  instance,
uint32_t  channel,
uint16_t  periodHigh,
uint16_t  periodLow 
)

Sets the timer channel period in microseconds.

This function sets the timer channel period in microseconds when timer channel mode is dual 16 bit periodic counter mode. The period range depends on the frequency of the LPIT functional clock and operation mode of timer channel. If the required period is out of range, use the suitable mode if applicable. This function is only valid for one single channel.

Parameters
[in]instanceLPIT module instance number
[in]channelTimer channel number
[in]periodHighPeriod of higher 16 bit in microseconds
[in]periodLowPeriod of lower 16 bit in microseconds
Returns
Operation status
  • STATUS_SUCCESS: Input period of timer channel is valid.
  • STATUS_ERROR: Input period of timer channel is invalid.

Definition at line 341 of file lpit_driver.c.

void LPIT_DRV_StartTimerChannels ( uint32_t  instance,
uint32_t  mask 
)

Starts the timer channel counting.

This function allows starting timer channels simultaneously . After calling this function, timer channels are going operate depend on mode and control bits which controls timer channel start, reload and restart.

Parameters
[in]instanceLPIT module instance number
[in]maskTimer channels starting mask that decides which channels will be started
  • For example:
    • with mask = 0x01U then channel 0 will be started
    • with mask = 0x02U then channel 1 will be started
    • with mask = 0x03U then channel 0 and channel 1 will be started

Definition at line 220 of file lpit_driver.c.

void LPIT_DRV_StopTimerChannels ( uint32_t  instance,
uint32_t  mask 
)

Stops the timer channel counting.

This function allows stop timer channels simultaneously from counting. Timer channels reload their periods respectively after the next time they call the LPIT_DRV_StartTimerChannels. Note that: In 32-bit Trigger Accumulator mode, the counter will load on the first trigger rising edge.

Parameters
[in]instanceLPIT module instance number
[in]maskTimer channels stopping mask that decides which channels will be stopped
  • For example:
    • with mask = 0x01U then channel 0 will be stopped
    • with mask = 0x02U then channel 1 will be stopped
    • with mask = 0x03U then channel 0 and channel 1 will be stopped

Definition at line 244 of file lpit_driver.c.