S32 SDK

Detailed Description

Low Power Timer Peripheral Driver.

The LPTMR is a configurable general-purpose 16-bit counter that has two operational modes: Timer and Pulse-Counter.

Depending on the configured operational mode, the counter in the LPTMR can be incremented using a clock input (Timer mode) or an event counter (external events like button presses or internal events from different trigger souces).

Timer Mode

In Timer mode, the LPTMR increments the internal counter from a selectable clock source. An optional 16-bit prescaler can be configured.

Pulse-Counter Mode

In Pulse-Counter Mode, the LPTMR counter increments from a selectable trigger source, input pin, which can be an external event (like a button press) or internal events (like triggers from TRGMUX).

An optional 16-bit glitch-fiter can be configured to reject events that have a duration below a set period.

Initialization prerequisites

Before configuring the LPTMR, the peripheral clock must be enabled from the PCC module.

The peripheral clock must not be confused with the counter clock, which is selectable within the LPTMR.

Driver configuration

The LPTMR driver allows configuring the LPTMR for Pulse-Counter Mode or Timer Mode via the general configuration structure.

Configurable options:

API

Some of the features exposed by the API are targeted specifically for Timer Mode or Pulse-Counter Mode. For example, configuring the Compare Value in microseconds makes sense only for Timer Mode, so therefor it is restricted for use in Pulse-Counter mode.

For any invalid configuration the functions will either return an error code or trigger DEV_ASSERT (if enabled). For more details, please refer to each function description.

Data Structures

struct  lptmr_config_t
 Defines the configuration structure for LPTMR. More...
 

Enumerations

enum  lptmr_pinselect_t { LPTMR_PINSELECT_TRGMUX = 0x00u, LPTMR_PINSELECT_ALT1 = 0x01u, LPTMR_PINSELECT_ALT2 = 0x02u, LPTMR_PINSELECT_ALT3 = 0x03u }
 Pulse Counter Input selection Implements : lptmr_pinselect_t_Class. More...
 
enum  lptmr_pinpolarity_t { LPTMR_PINPOLARITY_RISING = 0u, LPTMR_PINPOLARITY_FALLING = 1u }
 Pulse Counter input polarity Implements : lptmr_pinpolarity_t_Class. More...
 
enum  lptmr_workmode_t { LPTMR_WORKMODE_TIMER = 0u, LPTMR_WORKMODE_PULSECOUNTER = 1u }
 Work Mode Implements : lptmr_workmode_t_Class. More...
 
enum  lptmr_prescaler_t {
  LPTMR_PRESCALE_2 = 0x00u, LPTMR_PRESCALE_4_GLITCHFILTER_2 = 0x01u, LPTMR_PRESCALE_8_GLITCHFILTER_4 = 0x02u, LPTMR_PRESCALE_16_GLITCHFILTER_8 = 0x03u,
  LPTMR_PRESCALE_32_GLITCHFILTER_16 = 0x04u, LPTMR_PRESCALE_64_GLITCHFILTER_32 = 0x05u, LPTMR_PRESCALE_128_GLITCHFILTER_64 = 0x06u, LPTMR_PRESCALE_256_GLITCHFILTER_128 = 0x07u,
  LPTMR_PRESCALE_512_GLITCHFILTER_256 = 0x08u, LPTMR_PRESCALE_1024_GLITCHFILTER_512 = 0x09u, LPTMR_PRESCALE_2048_GLITCHFILTER_1024 = 0x0Au, LPTMR_PRESCALE_4096_GLITCHFILTER_2048 = 0x0Bu,
  LPTMR_PRESCALE_8192_GLITCHFILTER_4096 = 0x0Cu, LPTMR_PRESCALE_16384_GLITCHFILTER_8192 = 0x0Du, LPTMR_PRESCALE_32768_GLITCHFILTER_16384 = 0x0Eu, LPTMR_PRESCALE_65536_GLITCHFILTER_32768 = 0x0Fu
}
 Prescaler Selection Implements : lptmr_prescaler_t_Class. More...
 
enum  lptmr_clocksource_t { LPTMR_CLOCKSOURCE_SIRCDIV2 = 0x00u, LPTMR_CLOCKSOURCE_1KHZ_LPO = 0x01u, LPTMR_CLOCKSOURCE_RTC = 0x02u, LPTMR_CLOCKSOURCE_PCC = 0x03u }
 Clock Source selection Implements : lptmr_clocksource_t_Class. More...
 
enum  lptmr_counter_units_t { LPTMR_COUNTER_UNITS_TICKS = 0x00U, LPTMR_COUNTER_UNITS_MICROSECONDS = 0x01U }
 Defines the LPTMR counter units available for configuring or reading the timer compare value. More...
 

LPTMR Driver Functions

void LPTMR_DRV_InitConfigStruct (lptmr_config_t *const config)
 Initialize a configuration structure with default values. More...
 
void LPTMR_DRV_Init (const uint32_t instance, const lptmr_config_t *const config, const bool startCounter)
 Initialize a LPTMR instance with values from an input configuration structure. More...
 
void LPTMR_DRV_SetConfig (const uint32_t instance, const lptmr_config_t *const config)
 Configure a LPTMR instance. More...
 
void LPTMR_DRV_GetConfig (const uint32_t instance, lptmr_config_t *const config)
 Get the current configuration of a LPTMR instance. More...
 
void LPTMR_DRV_Deinit (const uint32_t instance)
 De-initialize a LPTMR instance. More...
 
status_t LPTMR_DRV_SetCompareValueByCount (const uint32_t instance, const uint16_t compareValueByCount)
 Set the compare value in counter tick units, for a LPTMR instance. More...
 
void LPTMR_DRV_GetCompareValueByCount (const uint32_t instance, uint16_t *const compareValueByCount)
 Get the compare value in counter tick units, of a LPTMR instance. More...
 
status_t LPTMR_DRV_SetCompareValueByUs (const uint32_t instance, const uint32_t compareValueUs)
 Set the compare value for Timer Mode in microseconds, for a LPTMR instance. More...
 
void LPTMR_DRV_GetCompareValueByUs (const uint32_t instance, uint32_t *const compareValueUs)
 Get the compare value in microseconds, of a LPTMR instance. More...
 
bool LPTMR_DRV_GetCompareFlag (const uint32_t instance)
 Get the current state of the Compare Flag of a LPTMR instance. More...
 
void LPTMR_DRV_ClearCompareFlag (const uint32_t instance)
 Clear the Compare Flag of a LPTMR instance. More...
 
bool LPTMR_DRV_IsRunning (const uint32_t instance)
 Get the run state of a LPTMR instance. More...
 
void LPTMR_DRV_SetInterrupt (const uint32_t instance, const bool enableInterrupt)
 Enable/disable the LPTMR interrupt. More...
 
uint16_t LPTMR_DRV_GetCounterValueByCount (const uint32_t instance)
 Get the current counter value in counter tick units. More...
 
void LPTMR_DRV_StartCounter (const uint32_t instance)
 Enable the LPTMR / Start the counter. More...
 
void LPTMR_DRV_StopCounter (const uint32_t instance)
 Disable the LPTMR / Stop the counter. More...
 
void LPTMR_DRV_SetPinConfiguration (const uint32_t instance, const lptmr_pinselect_t pinSelect, const lptmr_pinpolarity_t pinPolarity)
 Set the Input Pin configuration for Pulse Counter mode. More...
 

Enumeration Type Documentation

Clock Source selection Implements : lptmr_clocksource_t_Class.

Enumerator
LPTMR_CLOCKSOURCE_SIRCDIV2 

SIRC clock

LPTMR_CLOCKSOURCE_1KHZ_LPO 

1kHz LPO clock

LPTMR_CLOCKSOURCE_RTC 

RTC clock

LPTMR_CLOCKSOURCE_PCC 

PCC configured clock

Definition at line 88 of file lptmr_driver.h.

Defines the LPTMR counter units available for configuring or reading the timer compare value.

Implements : lptmr_counter_units_t_Class

Enumerator
LPTMR_COUNTER_UNITS_TICKS 
LPTMR_COUNTER_UNITS_MICROSECONDS 

Definition at line 100 of file lptmr_driver.h.

Pulse Counter input polarity Implements : lptmr_pinpolarity_t_Class.

Enumerator
LPTMR_PINPOLARITY_RISING 

Count pulse on rising edge

LPTMR_PINPOLARITY_FALLING 

Count pulse on falling edge

Definition at line 50 of file lptmr_driver.h.

Pulse Counter Input selection Implements : lptmr_pinselect_t_Class.

Enumerator
LPTMR_PINSELECT_TRGMUX 

Count pulses from TRGMUX trigger

LPTMR_PINSELECT_ALT1 

Count pulses from pin alternative 1

LPTMR_PINSELECT_ALT2 

Count pulses from pin alternative 2

LPTMR_PINSELECT_ALT3 

Count pulses from pin alternative 3

Definition at line 40 of file lptmr_driver.h.

Prescaler Selection Implements : lptmr_prescaler_t_Class.

Enumerator
LPTMR_PRESCALE_2 

Timer mode: prescaler 2, Glitch filter mode: invalid

LPTMR_PRESCALE_4_GLITCHFILTER_2 

Timer mode: prescaler 4, Glitch filter mode: 2 clocks

LPTMR_PRESCALE_8_GLITCHFILTER_4 

Timer mode: prescaler 8, Glitch filter mode: 4 clocks

LPTMR_PRESCALE_16_GLITCHFILTER_8 

Timer mode: prescaler 16, Glitch filter mode: 8 clocks

LPTMR_PRESCALE_32_GLITCHFILTER_16 

Timer mode: prescaler 32, Glitch filter mode: 16 clocks

LPTMR_PRESCALE_64_GLITCHFILTER_32 

Timer mode: prescaler 64, Glitch filter mode: 32 clocks

LPTMR_PRESCALE_128_GLITCHFILTER_64 

Timer mode: prescaler 128, Glitch filter mode: 64 clocks

LPTMR_PRESCALE_256_GLITCHFILTER_128 

Timer mode: prescaler 256, Glitch filter mode: 128 clocks

LPTMR_PRESCALE_512_GLITCHFILTER_256 

Timer mode: prescaler 512, Glitch filter mode: 256 clocks

LPTMR_PRESCALE_1024_GLITCHFILTER_512 

Timer mode: prescaler 1024, Glitch filter mode: 512 clocks

LPTMR_PRESCALE_2048_GLITCHFILTER_1024 

Timer mode: prescaler 2048, Glitch filter mode: 1024 clocks

LPTMR_PRESCALE_4096_GLITCHFILTER_2048 

Timer mode: prescaler 4096, Glitch filter mode: 2048 clocks

LPTMR_PRESCALE_8192_GLITCHFILTER_4096 

Timer mode: prescaler 8192, Glitch filter mode: 4096 clocks

LPTMR_PRESCALE_16384_GLITCHFILTER_8192 

Timer mode: prescaler 16384, Glitch filter mode: 8192 clocks

LPTMR_PRESCALE_32768_GLITCHFILTER_16384 

Timer mode: prescaler 32768, Glitch filter mode: 16384 clocks

LPTMR_PRESCALE_65536_GLITCHFILTER_32768 

Timer mode: prescaler 65536, Glitch filter mode: 32768 clocks

Definition at line 66 of file lptmr_driver.h.

Work Mode Implements : lptmr_workmode_t_Class.

Enumerator
LPTMR_WORKMODE_TIMER 

Timer

LPTMR_WORKMODE_PULSECOUNTER 

Pulse counter

Definition at line 58 of file lptmr_driver.h.

Function Documentation

void LPTMR_DRV_ClearCompareFlag ( const uint32_t  instance)

Clear the Compare Flag of a LPTMR instance.

Parameters
[in]instance- LPTMR instance number
void LPTMR_DRV_Deinit ( const uint32_t  instance)

De-initialize a LPTMR instance.

Parameters
[in]instance- LPTMR instance number
bool LPTMR_DRV_GetCompareFlag ( const uint32_t  instance)

Get the current state of the Compare Flag of a LPTMR instance.

Parameters
[in]instance- LPTMR instance number
Returns
The state of the Compare Flag
void LPTMR_DRV_GetCompareValueByCount ( const uint32_t  instance,
uint16_t *const  compareValueByCount 
)

Get the compare value in counter tick units, of a LPTMR instance.

Parameters
[in]instance- LPTMR instance number
[out]compareValueByCount- Pointer to current compare value, in counter ticks
void LPTMR_DRV_GetCompareValueByUs ( const uint32_t  instance,
uint32_t *const  compareValueUs 
)

Get the compare value in microseconds, of a LPTMR instance.

Parameters
[in]instance- LPTMR instance number
[out]compareValueUs- Pointer to current compare value, in microseconds
void LPTMR_DRV_GetConfig ( const uint32_t  instance,
lptmr_config_t *const  config 
)

Get the current configuration of a LPTMR instance.

Parameters
[in]instance- LPTMR instance number
[out]config- Pointer to the output configuration structure
uint16_t LPTMR_DRV_GetCounterValueByCount ( const uint32_t  instance)

Get the current counter value in counter tick units.

Parameters
[in]instance- LPTMR instance number
Returns
The current counter value
void LPTMR_DRV_Init ( const uint32_t  instance,
const lptmr_config_t *const  config,
const bool  startCounter 
)

Initialize a LPTMR instance with values from an input configuration structure.

When (counterUnits == LPTMR_COUNTER_UNITS_MICROSECONDS) the function will automatically configure the timer for the input compareValue in microseconds. The input params for 'prescaler' and 'bypassPrescaler' will be ignored - their values will be adapted by the function, to best fit the input compareValue (in microseconds) for the operating clock frequency.

LPTMR_COUNTER_UNITS_MICROSECONDS may only be used for LPTMR_WORKMODE_TIMER mode. Otherwise the function shall not convert 'compareValue' in ticks and this is likely to cause erroneous behavior.

When (counterUnits == LPTMR_COUNTER_UNITS_TICKS) the function will use the 'prescaler' and 'bypassPrescaler' provided in the input config structure.

Parameters
[in]instance- LPTMR instance number
[in]config- Pointer to the input configuration structure
[in]startCounter- Flag for starting the counter immediately after configuration
void LPTMR_DRV_InitConfigStruct ( lptmr_config_t *const  config)

Initialize a configuration structure with default values.

Parameters
[out]config- Pointer to the configuration structure to be initialized
bool LPTMR_DRV_IsRunning ( const uint32_t  instance)

Get the run state of a LPTMR instance.

Parameters
[in]instance- LPTMR instance number
Returns
The run state of the LPTMR instance:
  • true: Timer/Counter started
  • false: Timer/Counter stopped
status_t LPTMR_DRV_SetCompareValueByCount ( const uint32_t  instance,
const uint16_t  compareValueByCount 
)

Set the compare value in counter tick units, for a LPTMR instance.

Parameters
[in]instance- LPTMR instance number
[in]compareValueByCount- The compare value in counter ticks, to be written
Returns
One of the possible status codes:
  • STATUS_SUCCESS: completed successfully
  • STATUS_ERROR: cannot reconfigure compare value (TCF not set)
  • STATUS_TIMEOUT: compare value greater then current counter value
status_t LPTMR_DRV_SetCompareValueByUs ( const uint32_t  instance,
const uint32_t  compareValueUs 
)

Set the compare value for Timer Mode in microseconds, for a LPTMR instance.

Parameters
[in]instance- LPTMR peripheral instance number
[in]compareValueUs- Compare value in microseconds
Returns
One of the possible status codes:
  • STATUS_SUCCESS: completed successfully
  • STATUS_ERROR: cannot reconfigure compare value
  • STATUS_TIMEOUT: compare value greater then current counter value
void LPTMR_DRV_SetConfig ( const uint32_t  instance,
const lptmr_config_t *const  config 
)

Configure a LPTMR instance.

When (counterUnits == LPTMR_COUNTER_UNITS_MICROSECONDS) the function will automatically configure the timer for the input compareValue in microseconds. The input params for 'prescaler' and 'bypassPrescaler' will be ignored - their values will be adapted by the function, to best fit the input compareValue (in microseconds) for the operating clock frequency.

LPTMR_COUNTER_UNITS_MICROSECONDS may only be used for LPTMR_WORKMODE_TIMER mode. Otherwise the function shall not convert 'compareValue' in ticks and this is likely to cause erroneous behavior.

When (counterUnits == LPTMR_COUNTER_UNITS_TICKS) the function will use the 'prescaler' and 'bypassPrescaler' provided in the input config structure.

Parameters
[in]instance- LPTMR instance number
[in]config- Pointer to the input configuration structure
void LPTMR_DRV_SetInterrupt ( const uint32_t  instance,
const bool  enableInterrupt 
)

Enable/disable the LPTMR interrupt.

Parameters
[in]instance- LPTMR instance number
[in]enableInterrupt- The new state of the LPTMR interrupt enable flag.
void LPTMR_DRV_SetPinConfiguration ( const uint32_t  instance,
const lptmr_pinselect_t  pinSelect,
const lptmr_pinpolarity_t  pinPolarity 
)

Set the Input Pin configuration for Pulse Counter mode.

Parameters
[in]instance- LPTMR instance number
[in]pinSelect- LPTMR pin selection
[in]pinPolarity- Polarity on which to increment counter (rising/falling edge)
void LPTMR_DRV_StartCounter ( const uint32_t  instance)

Enable the LPTMR / Start the counter.

Parameters
[in]instance- LPTMR instance number
void LPTMR_DRV_StopCounter ( const uint32_t  instance)

Disable the LPTMR / Stop the counter.

Parameters
[in]instance- LPTMR instance number