LETIMER - Low Energy Timer#


Example#

The following example demonstrates how to configure and use the LETIMER for PWM generation with a configurable duty cycle.

// Global flag for COMP0 interrupt
volatile bool comp0_occurred = false;

// LETIMER0 IRQ Handler
void LETIMER0_IRQHandler(void)
{
  // Get pending interrupts
  uint32_t flags = sl_hal_letimer_get_pending_interrupts(LETIMER0);

  // Check for COMP0 interrupt
  if (flags & LETIMER_IF_COMP0) {
    // Clear the interrupt flag
    sl_hal_letimer_clear_interrupts(LETIMER0, LETIMER_IF_COMP0);

    // Set our flag to indicate the event occurred
    comp0_occurred = true;

    // Additional processing can be done here
  }
}

// Example of configuring LETIMER for PWM generation
void letimer_pwm_example(void)
{
  // Initialize with custom configuration for PWM generation
  sl_hal_letimer_init_t init = SL_HAL_LETIMER_INIT_DEFAULT;

  // Set prescaler to divide clock by 4
  init.prescaler = SL_HAL_LETIMER_PRESCALER_DIV4;

  // Free-running mode
  init.repeat_mode = SL_HAL_LETIMER_REPEAT_MODE_FREE;

  // Configure PWM on output 0
  init.underflow_output0_action = SL_HAL_LETIMER_UNDERFLOW_OUTPUT_ACTION_PWM;

  // Reload TOP on underflow
  init.enable_top = true;

  // Initialize LETIMER with our configuration
  sl_hal_letimer_init(LETIMER0, &init);

  // Enable LETIMER
  sl_hal_letimer_enable(LETIMER0);

  // Set TOP value for desired frequency
  // For example, with 32.768 kHz clock, div4 prescaler, and TOP=1000:
  // Frequency = 32768 / 4 / 1000 = 8.192 Hz
  sl_hal_letimer_set_top(LETIMER0, 1000);

  // Set compare values to control PWM duty cycle
  // COMP0 = 0 (active at start of period)
  sl_hal_letimer_set_compare(LETIMER0, 0, 0);

  // COMP1 = 500 (50% duty cycle)
  sl_hal_letimer_set_compare(LETIMER0, 1, 500);

  // Clear any pending interrupts
  sl_hal_letimer_clear_interrupts(LETIMER0, _LETIMER_IF_MASK);

  // Enable COMP0 interrupt to trigger at the start of each period
  sl_hal_letimer_enable_interrupts(LETIMER0, LETIMER_IEN_COMP0);

  // Enable NVIC interrupt for LETIMER
  sl_interrupt_manager_clear_irq_pending(LETIMER0_IRQn);
  sl_interrupt_manager_enable_irq(LETIMER0_IRQn);

  // Reset the event flag
  comp0_occurred = false;

  // Start LETIMER
  sl_hal_letimer_start(LETIMER0);

  // Wait for first COMP0 interrupt to occur
  while (!comp0_occurred) {
    // Could enter energy mode here to save power while waiting
  }

  // Change duty cycle to 25%
  sl_hal_letimer_set_compare(LETIMER0, 1, 750);  // 25% = (1000-750)/1000

  // Application code would continue here
  // ...

  // When done, clean up
  sl_hal_letimer_stop(LETIMER0);
  sl_hal_letimer_disable_interrupts(LETIMER0, _LETIMER_IEN_MASK);
  sl_interrupt_manager_disable_irq(LETIMER0_IRQn);
  sl_hal_letimer_disable(LETIMER0);
  sl_hal_letimer_wait_ready(LETIMER0);
}

Modules#

sl_hal_letimer_init_t

Enumerations#

enum
SL_HAL_LETIMER_PRESCALER_DIV1 = _LETIMER_CTRL_CNTPRESC_DIV1
SL_HAL_LETIMER_PRESCALER_DIV2 = _LETIMER_CTRL_CNTPRESC_DIV2
SL_HAL_LETIMER_PRESCALER_DIV4 = _LETIMER_CTRL_CNTPRESC_DIV4
SL_HAL_LETIMER_PRESCALER_DIV8 = _LETIMER_CTRL_CNTPRESC_DIV8
SL_HAL_LETIMER_PRESCALER_DIV16 = _LETIMER_CTRL_CNTPRESC_DIV16
SL_HAL_LETIMER_PRESCALER_DIV32 = _LETIMER_CTRL_CNTPRESC_DIV32
SL_HAL_LETIMER_PRESCALER_DIV64 = _LETIMER_CTRL_CNTPRESC_DIV64
SL_HAL_LETIMER_PRESCALER_DIV128 = _LETIMER_CTRL_CNTPRESC_DIV128
SL_HAL_LETIMER_PRESCALER_DIV256 = _LETIMER_CTRL_CNTPRESC_DIV256
}

Prescaler.

enum
SL_HAL_LETIMER_REPEAT_MODE_FREE = _LETIMER_CTRL_REPMODE_FREE
SL_HAL_LETIMER_REPEAT_MODE_ONESHOT = _LETIMER_CTRL_REPMODE_ONESHOT
SL_HAL_LETIMER_REPEAT_MODE_BUFFERED = _LETIMER_CTRL_REPMODE_BUFFERED
SL_HAL_LETIMER_REPEAT_MODE_DOUBLE = _LETIMER_CTRL_REPMODE_DOUBLE
}

Repeat mode.

enum
SL_HAL_LETIMER_UNDERFLOW_OUTPUT_ACTION_NONE = _LETIMER_CTRL_UFOA0_NONE
SL_HAL_LETIMER_UNDERFLOW_OUTPUT_ACTION_TOGGLE = _LETIMER_CTRL_UFOA0_TOGGLE
SL_HAL_LETIMER_UNDERFLOW_OUTPUT_ACTION_PULSE = _LETIMER_CTRL_UFOA0_PULSE
SL_HAL_LETIMER_UNDERFLOW_OUTPUT_ACTION_PWM = _LETIMER_CTRL_UFOA0_PWM
}

Underflow action on output.

Functions#

void
sl_hal_letimer_init(LETIMER_TypeDef *letimer, const sl_hal_letimer_init_t *init)

Initialize LETIMER.

void
sl_hal_letimer_reset(LETIMER_TypeDef *letimer)

Reset the LETIMER to the same state that it was in after a hardware reset.

void
sl_hal_letimer_enable(LETIMER_TypeDef *letimer)

Enable the LETIMER.

void
sl_hal_letimer_disable(LETIMER_TypeDef *letimer)

Disable LETIMER.

void
sl_hal_letimer_wait_ready(LETIMER_TypeDef *letimer)

Wait for resetting and disabling to finish.

void
sl_hal_letimer_wait_sync(LETIMER_TypeDef *letimer)

Wait for ongoing sync of register(s) to the low-frequency domain to complete.

uint32_t
sl_hal_letimer_get_status(LETIMER_TypeDef *letimer)

Get status values for the LETIMER.

void
sl_hal_letimer_enable_interrupts(LETIMER_TypeDef *letimer, uint32_t flags)

Enable one or more LETIMER interrupts.

void
sl_hal_letimer_disable_interrupts(LETIMER_TypeDef *letimer, uint32_t flags)

Disable one or more LETIMER interrupts.

void
sl_hal_letimer_clear_interrupts(LETIMER_TypeDef *letimer, uint32_t flags)

Clear one or more pending LETIMER interrupts.

void
sl_hal_letimer_set_interrupts(LETIMER_TypeDef *letimer, uint32_t flags)

Set one or more pending LETIMER interrupts from SW.

uint32_t
sl_hal_letimer_get_pending_interrupts(LETIMER_TypeDef *letimer)

Get pending LETIMER interrupt flags.

uint32_t

Get enabled and pending LETIMER interrupt flags.

void
sl_hal_letimer_start(LETIMER_TypeDef *letimer)

Start LETIMER counter.

void
sl_hal_letimer_stop(LETIMER_TypeDef *letimer)

Stop LETIMER counter.

void
sl_hal_letimer_set_counter(LETIMER_TypeDef *letimer, uint32_t value)

Set the LETIMER counter register value.

uint32_t
sl_hal_letimer_get_counter(LETIMER_TypeDef *letimer)

Get the LETIMER counter register value.

void
sl_hal_letimer_set_compare(LETIMER_TypeDef *letimer, uint8_t channel, uint32_t value)

Set the LETIMER compare register value.

uint32_t
sl_hal_letimer_get_compare(LETIMER_TypeDef *letimer, uint8_t channel)

Get the LETIMER compare register value.

void
sl_hal_letimer_set_repeat(LETIMER_TypeDef *letimer, uint8_t channel, uint32_t value)

Set the LETIMER repeat register value.

uint32_t
sl_hal_letimer_get_repeat(LETIMER_TypeDef *letimer, uint8_t channel)

Get the LETIMER repeat register value.

void
sl_hal_letimer_set_top(LETIMER_TypeDef *letimer, uint32_t value)

Set the LETIMER top register value.

uint32_t
sl_hal_letimer_get_top(LETIMER_TypeDef *letimer)

Get the LETIMER top register value.

void
sl_hal_letimer_set_top_buffer(LETIMER_TypeDef *letimer, uint32_t value)

Set the LETIMER top register value buffer.

uint32_t
sl_hal_letimer_get_top_buffer(LETIMER_TypeDef *letimer)

Get the LETIMER top register value buffer.

Macros#

#define
SL_HAL_LETIMER_REF_VALID (letimer_ref)

Check if LETIMER module is valid.

#define
SL_HAL_LETIMER_MAX_COUNT (letimer_ref)

Return LETIMER module max count.

#define
SL_HAL_LETIMER_INIT_DEFAULT undefined

Default configuration for LETIMER initialization structure.

Enumeration Documentation#

sl_hal_letimer_prescaler_t#

sl_hal_letimer_prescaler_t

Prescaler.

Enumerator
SL_HAL_LETIMER_PRESCALER_DIV1
SL_HAL_LETIMER_PRESCALER_DIV2

Divide by 1.

SL_HAL_LETIMER_PRESCALER_DIV4

Divide by 2.

SL_HAL_LETIMER_PRESCALER_DIV8

Divide by 4.

SL_HAL_LETIMER_PRESCALER_DIV16

Divide by 8.

SL_HAL_LETIMER_PRESCALER_DIV32

Divide by 16.

SL_HAL_LETIMER_PRESCALER_DIV64

Divide by 32.

SL_HAL_LETIMER_PRESCALER_DIV128

Divide by 64.

SL_HAL_LETIMER_PRESCALER_DIV256

Divide by 128.


sl_hal_letimer_repeat_mode_t#

sl_hal_letimer_repeat_mode_t

Repeat mode.

Enumerator
SL_HAL_LETIMER_REPEAT_MODE_FREE

Count until stopped by SW.

SL_HAL_LETIMER_REPEAT_MODE_ONESHOT

Count REP0 times.

SL_HAL_LETIMER_REPEAT_MODE_BUFFERED

Count REP0 times, if REP1 has been written to, it is loaded into REP0 when REP0 is about to be decremented to 0.

SL_HAL_LETIMER_REPEAT_MODE_DOUBLE

Run as long as both REP0 and REP1 are not 0.


sl_hal_letimer_underflow_output_action_t#

sl_hal_letimer_underflow_output_action_t

Underflow action on output.

Enumerator
SL_HAL_LETIMER_UNDERFLOW_OUTPUT_ACTION_NONE

No output action.

SL_HAL_LETIMER_UNDERFLOW_OUTPUT_ACTION_TOGGLE

Toggle output when counter underflows.

SL_HAL_LETIMER_UNDERFLOW_OUTPUT_ACTION_PULSE

Hold output one LETIMER clock cycle when counter underflows.

SL_HAL_LETIMER_UNDERFLOW_OUTPUT_ACTION_PWM

Set output idle when counter underflows, and active when matching COMP1.


Function Documentation#

sl_hal_letimer_init#

void sl_hal_letimer_init (LETIMER_TypeDef * letimer, const sl_hal_letimer_init_t * init)

Initialize LETIMER.

Parameters
TypeDirectionArgument NameDescription
LETIMER_TypeDef *[in]letimer

A pointer to the LETIMER peripheral register block.

const sl_hal_letimer_init_t *[in]init

A pointer to the LETIMER initialization structure.


sl_hal_letimer_reset#

void sl_hal_letimer_reset (LETIMER_TypeDef * letimer)

Reset the LETIMER to the same state that it was in after a hardware reset.

Parameters
TypeDirectionArgument NameDescription
LETIMER_TypeDef *[in]letimer

Pointer to the LETIMER peripheral register block.


sl_hal_letimer_enable#

void sl_hal_letimer_enable (LETIMER_TypeDef * letimer)

Enable the LETIMER.

Parameters
TypeDirectionArgument NameDescription
LETIMER_TypeDef *[in]letimer

Pointer to the LETIMER peripheral register block.


sl_hal_letimer_disable#

void sl_hal_letimer_disable (LETIMER_TypeDef * letimer)

Disable LETIMER.

Parameters
TypeDirectionArgument NameDescription
LETIMER_TypeDef *[in]letimer

Pointer to the LETIMER peripheral register block.


sl_hal_letimer_wait_ready#

void sl_hal_letimer_wait_ready (LETIMER_TypeDef * letimer)

Wait for resetting and disabling to finish.

Parameters
TypeDirectionArgument NameDescription
LETIMER_TypeDef *[in]letimer

Pointer to the LETIMER peripheral register block.


sl_hal_letimer_wait_sync#

void sl_hal_letimer_wait_sync (LETIMER_TypeDef * letimer)

Wait for ongoing sync of register(s) to the low-frequency domain to complete.

Parameters
TypeDirectionArgument NameDescription
LETIMER_TypeDef *[in]letimer

Pointer to the LETIMER peripheral register block.


sl_hal_letimer_get_status#

uint32_t sl_hal_letimer_get_status (LETIMER_TypeDef * letimer)

Get status values for the LETIMER.

Parameters
TypeDirectionArgument NameDescription
LETIMER_TypeDef *[in]letimer

Pointer to the LETIMER peripheral register block.

Returns

  • Current status values.


sl_hal_letimer_enable_interrupts#

void sl_hal_letimer_enable_interrupts (LETIMER_TypeDef * letimer, uint32_t flags)

Enable one or more LETIMER interrupts.

Parameters
TypeDirectionArgument NameDescription
LETIMER_TypeDef *[in]letimer

Pointer to the LETIMER peripheral register block.

uint32_t[in]flags

LETIMER interrupt source(s) to enable. Use one or more valid interrupt flags for the LETIMER module (LETIMER_IF_nnn) OR'ed together.


sl_hal_letimer_disable_interrupts#

void sl_hal_letimer_disable_interrupts (LETIMER_TypeDef * letimer, uint32_t flags)

Disable one or more LETIMER interrupts.

Parameters
TypeDirectionArgument NameDescription
LETIMER_TypeDef *[in]letimer

Pointer to the LETIMER peripheral register block.

uint32_t[in]flags

LETIMER interrupt source(s) to disable. Use one or more valid interrupt flags for the LETIMER module (LETIMER_IF_nnn) OR'ed together.


sl_hal_letimer_clear_interrupts#

void sl_hal_letimer_clear_interrupts (LETIMER_TypeDef * letimer, uint32_t flags)

Clear one or more pending LETIMER interrupts.

Parameters
TypeDirectionArgument NameDescription
LETIMER_TypeDef *[in]letimer

Pointer to the LETIMER peripheral register block.

uint32_t[in]flags

Pending LETIMER interrupt source(s) to clear. Use one or more valid interrupt flags for the LETIMER module (LETIMER_IF_nnn) OR'ed together.


sl_hal_letimer_set_interrupts#

void sl_hal_letimer_set_interrupts (LETIMER_TypeDef * letimer, uint32_t flags)

Set one or more pending LETIMER interrupts from SW.

Parameters
TypeDirectionArgument NameDescription
LETIMER_TypeDef *[in]letimer

Pointer to the LETIMER peripheral register block.

uint32_t[in]flags

LETIMER interrupt source(s) to set to pending. Use one or more valid interrupt flags for the LETIMER module (LETIMER_IF_nnn) OR'ed together.


sl_hal_letimer_get_pending_interrupts#

uint32_t sl_hal_letimer_get_pending_interrupts (LETIMER_TypeDef * letimer)

Get pending LETIMER interrupt flags.

Parameters
TypeDirectionArgument NameDescription
LETIMER_TypeDef *[in]letimer

Pointer to the LETIMER peripheral register block.

Note

  • Event bits are not cleared by this function.

Returns

  • LETIMER interrupt source(s) pending. Returns one or more valid interrupt flags for the LETIMER module (LETIMER_IF_nnn) OR'ed together.


sl_hal_letimer_get_enabled_pending_interrupts#

uint32_t sl_hal_letimer_get_enabled_pending_interrupts (LETIMER_TypeDef * letimer)

Get enabled and pending LETIMER interrupt flags.

Parameters
TypeDirectionArgument NameDescription
LETIMER_TypeDef *[in]letimer

Pointer to the LETIMER peripheral register block.

Useful for handling more interrupt sources in the same interrupt handler.

Note

  • Interrupt flags are not cleared by this function.

Returns

  • Pending and enabled LETIMER interrupt sources. The return value is the bitwise AND combination of

    • the OR combination of enabled interrupt sources in LETIMERx_IEN_nnn register (LETIMERx_IEN_nnn) and

    • the OR combination of valid interrupt flags of the LETIMER module (LETIMERx_IF_nnn).


sl_hal_letimer_start#

void sl_hal_letimer_start (LETIMER_TypeDef * letimer)

Start LETIMER counter.

Parameters
TypeDirectionArgument NameDescription
LETIMER_TypeDef *[in]letimer

Pointer to the LETIMER peripheral register block.

This function will send a start command to the LETIMER peripheral. The LETIMER peripheral will use some LF clock ticks before the command is executed. The sl_hal_letimer_wait_sync() function can be used to wait for the start command to be executed.


sl_hal_letimer_stop#

void sl_hal_letimer_stop (LETIMER_TypeDef * letimer)

Stop LETIMER counter.

Parameters
TypeDirectionArgument NameDescription
LETIMER_TypeDef *[in]letimer

Pointer to the LETIMER peripheral register block.

This function will send a stop command to the LETIMER peripheral. The LETIMER peripheral will use some LF clock ticks before the command is executed. The sl_hal_letimer_wait_sync() function can be used to wait for the stop command to be executed.


sl_hal_letimer_set_counter#

void sl_hal_letimer_set_counter (LETIMER_TypeDef * letimer, uint32_t value)

Set the LETIMER counter register value.

Parameters
TypeDirectionArgument NameDescription
LETIMER_TypeDef *[in]letimer

Pointer to the LETIMER peripheral register block.

uint32_t[in]value

Value to set in counter register.


sl_hal_letimer_get_counter#

uint32_t sl_hal_letimer_get_counter (LETIMER_TypeDef * letimer)

Get the LETIMER counter register value.

Parameters
TypeDirectionArgument NameDescription
LETIMER_TypeDef *[in]letimer

Pointer to the LETIMER peripheral register block.

Returns

  • Current counter.


sl_hal_letimer_set_compare#

void sl_hal_letimer_set_compare (LETIMER_TypeDef * letimer, uint8_t channel, uint32_t value)

Set the LETIMER compare register value.

Parameters
TypeDirectionArgument NameDescription
LETIMER_TypeDef *[in]letimer

Pointer to the LETIMER peripheral register block.

uint8_t[in]channel

A compare register to set, either 0 or 1.

uint32_t[in]value

Value to set in the compare register.


sl_hal_letimer_get_compare#

uint32_t sl_hal_letimer_get_compare (LETIMER_TypeDef * letimer, uint8_t channel)

Get the LETIMER compare register value.

Parameters
TypeDirectionArgument NameDescription
LETIMER_TypeDef *[in]letimer

A pointer to the LETIMER peripheral register block.

uint8_t[in]channel

A compare register to get, either 0 or 1.

Returns

  • A compare register value, 0 if invalid register selected.


sl_hal_letimer_set_repeat#

void sl_hal_letimer_set_repeat (LETIMER_TypeDef * letimer, uint8_t channel, uint32_t value)

Set the LETIMER repeat register value.

Parameters
TypeDirectionArgument NameDescription
LETIMER_TypeDef *[in]letimer

Pointer to the LETIMER peripheral register block.

uint8_t[in]channel

A repeat register to set, either 0 or 1.

uint32_t[in]value

Value to set in the repeat register.


sl_hal_letimer_get_repeat#

uint32_t sl_hal_letimer_get_repeat (LETIMER_TypeDef * letimer, uint8_t channel)

Get the LETIMER repeat register value.

Parameters
TypeDirectionArgument NameDescription
LETIMER_TypeDef *[in]letimer

A pointer to the LETIMER peripheral register block.

uint8_t[in]channel

A repeat register to get, either 0 or 1.

Returns

  • A repeat register value, 0 if invalid register selected.


sl_hal_letimer_set_top#

void sl_hal_letimer_set_top (LETIMER_TypeDef * letimer, uint32_t value)

Set the LETIMER top register value.

Parameters
TypeDirectionArgument NameDescription
LETIMER_TypeDef *[in]letimer

Pointer to the LETIMER peripheral register block.

uint32_t[in]value

Value to set in top value register.

The LETIMER is a down-counter, so when the counter reaches 0 then the top value will be loaded into the counter. This function can be used to set the top value. The LETIMER needs to be configured with enable_top to use a top value.


sl_hal_letimer_get_top#

uint32_t sl_hal_letimer_get_top (LETIMER_TypeDef * letimer)

Get the LETIMER top register value.

Parameters
TypeDirectionArgument NameDescription
LETIMER_TypeDef *[in]letimer

Pointer to the LETIMER peripheral register block.

Returns

  • Current top value.


sl_hal_letimer_set_top_buffer#

void sl_hal_letimer_set_top_buffer (LETIMER_TypeDef * letimer, uint32_t value)

Set the LETIMER top register value buffer.

Parameters
TypeDirectionArgument NameDescription
LETIMER_TypeDef *[in]letimer

Pointer to the LETIMER peripheral register block.

uint32_t[in]value

Value to set in top value buffer register.

When top value buffer register is updated, value is loaded into top value register the next time the counter reaches 0. This feature is useful to update top value safely when LETIMER is running. This won't happen if buffered_top is not configured to true and the mode is not SL_HAL_LETIMER_REPEAT_MODE_BUFFERED.


sl_hal_letimer_get_top_buffer#

uint32_t sl_hal_letimer_get_top_buffer (LETIMER_TypeDef * letimer)

Get the LETIMER top register value buffer.

Parameters
TypeDirectionArgument NameDescription
LETIMER_TypeDef *[in]letimer

Pointer to the LETIMER peripheral register block.

Returns

  • Current top value buffer.