libopencm3
A free/libre/open-source firmware library for various ARM Cortex-M3 microcontrollers.
Timer peripheral API

SWM050 Timer API. More...

Collaboration diagram for Timer peripheral API:

Functions

static void timer_setup_internal (uint32_t timer, bool timer_int_en, enum timer_operation_modes op_mode, enum timer_edge_modes edge_mode, enum timer_loop_modes loop_mode, enum timer_clk_src clk_src, enum timer_output_modes output_mode, enum timer_level output_level)
 Internal function for timer setup. More...
 
void timer_counter_setup (uint32_t timer, bool timer_int_en, enum timer_edge_modes edge_mode, enum timer_loop_modes loop_mode, enum timer_clk_src clk_src, enum timer_output_modes output_mode, enum timer_level output_level, uint32_t target)
 Setup the timer in counter mode. More...
 
void timer_pwm_setup (uint32_t timer, bool timer_int_en, enum timer_edge_modes edge_mode, enum timer_clk_src clk_src, enum timer_level output_level, uint16_t period0, uint16_t period1)
 Setup the timer in PWM mode. More...
 
void timer_pulse_capture_setup (uint32_t timer, bool timer_int_en, enum timer_edge_modes edge_mode, enum timer_loop_modes loop_mode)
 Setup the timer in pulse capture mode. More...
 
void timer_duty_cycle_capture_setup (uint32_t timer, bool timer_int_en, enum timer_edge_modes edge_mode, enum timer_loop_modes loop_mode)
 Setup the timer in duty cycle capture mode. More...
 
void timer_clock_div (uint8_t div)
 Set the timer clock divider, based off of the 18MHz oscillator. More...
 
void timer_enable (uint32_t timer, bool en)
 Enables or disables the timer. More...
 
void timer_clock_enable (uint32_t timer, bool en)
 Enables or disables the timer's internal clock. More...
 
void timer_operation_mode (uint32_t timer, enum timer_operation_modes mode)
 Selects the mode of operation. More...
 
void timer_output_mode (uint32_t timer, enum timer_output_modes mode)
 Selects the output mode. More...
 
void timer_output_level (uint32_t timer, enum timer_level level)
 Selects the initial output level. More...
 
void timer_edge_mode (uint32_t timer, enum timer_edge_modes mode)
 Selects the edge mode. More...
 
void timer_loop_mode (uint32_t timer, enum timer_loop_modes mode)
 Selects the loop mode. More...
 
void timer_clock_source (uint32_t timer, enum timer_clk_src src)
 Selects the clock source for the timer. More...
 
void timer_counter_target_value (uint32_t timer, uint32_t target)
 Sets the target values for counter mode. More...
 
void timer_pwm_target_value (uint32_t timer, uint16_t period0, uint16_t period1)
 Sets the target values for PWM mode. More...
 
void timer_int_enable (uint32_t timer, bool en)
 Enable or disable the interrupt. More...
 
void timer_int_mask (uint32_t timer, enum timer_int_masked masked)
 Sets the interrupt mask. More...
 
uint32_t timer_get_current_value (uint32_t timer)
 Gets the current counter value, and clears the interrupt/interrupt overflow. More...
 
uint32_t timer_get_cycle_width (uint32_t timer)
 Gets the cycle width. More...
 
uint32_t timer_get_pulse_width (uint32_t timer)
 Gets the pulse width in pulse capture mode, or gets the period width in duty cycle capture mode. More...
 
enum timer_pwm_period timer_get_pwm_period (uint32_t timer)
 Gets the current output period in PWM mode. More...
 
bool timer_int_status (uint32_t timer)
 Gets the interrupt status after masking. More...
 
bool timer_int_raw_status (uint32_t timer)
 Gets the interrupt status before masking. More...
 
bool timer_int_overflow_status (uint32_t timer)
 Gets the interrupt overflow status. More...
 

Detailed Description

SWM050 Timer API.

LGPL License Terms libopencm3 License

Author
© 2020 Caleb Szalacinski conta.nosp@m.ct@s.nosp@m.kiboy.nosp@m..net

Function Documentation

◆ timer_clock_div()

void timer_clock_div ( uint8_t  div)

Set the timer clock divider, based off of the 18MHz oscillator.

Parameters
divTimer clock divider. Only the 6 least-significant bits are used, Takes values from 0 to 63 (in reality the possible values are the even numbers from 2 to 62, as well as the number 1). Anything after the 6 least-significant bits are stripped off of the value. If the value is 0, it will be treated as a 1. All odd values other than 1 are rounded down to the closest even value, due to the fact that all odd values are treated by the register as a 1, which would likely be unexpected. A value of 0 would also normally be treated as a 2, which would also be unexpected behavior.

Definition at line 185 of file timer.c.

References SYSCTL_SYS_CFG_0.

◆ timer_clock_enable()

void timer_clock_enable ( uint32_t  timer,
bool  en 
)

Enables or disables the timer's internal clock.

Parameters
timerSelect timer Timer Select
enEnable or disable the internal clock

Definition at line 212 of file timer.c.

References SYSCTL_SYS_CFG_1, SYSCTL_SYS_CFG_1_TIMERSE0, SYSCTL_SYS_CFG_1_TIMERSE1, and TIMER_SE1.

Referenced by timer_setup_internal().

Here is the caller graph for this function:

◆ timer_clock_source()

void timer_clock_source ( uint32_t  timer,
enum timer_clk_src  src 
)

Selects the clock source for the timer.

Note
Be sure to set the alternate functions of the timer pins with syscon_sel_af() and disable SWD on those pins with syscon_sel_swd() as needed.
If not using the internal clock, you can disable it with timer_clock_enable() for power savings.
Parameters
timerSelect timer Timer Select
srcSelect the internal or external clock source timer_clk_src

Definition at line 311 of file timer.c.

References TIMER_CTRL, and TIMER_CTRL_OSCMOD.

Referenced by timer_setup_internal().

Here is the caller graph for this function:

◆ timer_counter_setup()

void timer_counter_setup ( uint32_t  timer,
bool  timer_int_en,
enum timer_edge_modes  edge_mode,
enum timer_loop_modes  loop_mode,
enum timer_clk_src  clk_src,
enum timer_output_modes  output_mode,
enum timer_level  output_level,
uint32_t  target 
)

Setup the timer in counter mode.

Note
Call timer_enable() when you are ready to start the timer.
Be sure to set the alternate functions of the timer pins with syscon_sel_af() and disable SWD on those pins with syscon_sel_swd() as needed.
If interrupts are enabled here, the interrupt should also be enabled using the NVIC before enabling the timer.
Parameters
timerSelect timer Timer Select
timer_int_enPassed to timer_int_enable()
edge_modePassed to timer_edge_mode()
loop_modePassed to timer_loop_mode()
clk_srcPassed to timer_clock_source()
output_modePassed to timer_output_mode()
output_levelPassed to timer_output_level()
targetPassed to timer_counter_target_value()

Definition at line 83 of file timer.c.

References timer_counter_target_value(), TIMER_MODE_COUNTER, and timer_setup_internal().

Here is the call graph for this function:

◆ timer_counter_target_value()

void timer_counter_target_value ( uint32_t  timer,
uint32_t  target 
)

Sets the target values for counter mode.

Parameters
timerSelect timer Timer Select
targetThe value to count up to

Definition at line 325 of file timer.c.

References TIMER_TARVAL.

Referenced by timer_counter_setup(), and timer_pwm_target_value().

Here is the caller graph for this function:

◆ timer_duty_cycle_capture_setup()

void timer_duty_cycle_capture_setup ( uint32_t  timer,
bool  timer_int_en,
enum timer_edge_modes  edge_mode,
enum timer_loop_modes  loop_mode 
)

Setup the timer in duty cycle capture mode.

Note
Call timer_enable() when you are ready to start the timer.
Be sure to set the alternate functions of the timer pins with syscon_sel_af() and disable SWD on those pins with syscon_sel_swd() as needed.
If interrupts are enabled here, the interrupt should also be enabled using the NVIC before enabling the timer.
Parameters
timerSelect timer Timer Select
timer_int_enPassed to timer_int_enable()
edge_modePassed to timer_edge_mode()
loop_modePassed to timer_loop_mode()

Definition at line 163 of file timer.c.

References TIMER_CLK_INTERNAL, TIMER_LEVEL_LOW, TIMER_MODE_DUTY_CYCLE_CAPTURE, TIMER_OUTPUT_NONE, and timer_setup_internal().

Here is the call graph for this function:

◆ timer_edge_mode()

void timer_edge_mode ( uint32_t  timer,
enum timer_edge_modes  mode 
)

Selects the edge mode.

Parameters
timerSelect timer Timer Select
modeThe edge mode timer_edge_modes

Definition at line 274 of file timer.c.

References TIMER_CTRL, and TIMER_CTRL_TMOD.

Referenced by timer_setup_internal().

Here is the caller graph for this function:

◆ timer_enable()

void timer_enable ( uint32_t  timer,
bool  en 
)

Enables or disables the timer.

Parameters
timerSelect timer Timer Select
enEnable or disable the timer

Definition at line 198 of file timer.c.

References TIMER_CTRL, and TIMER_CTRL_EN.

Referenced by timer_setup_internal().

Here is the caller graph for this function:

◆ timer_get_current_value()

uint32_t timer_get_current_value ( uint32_t  timer)

Gets the current counter value, and clears the interrupt/interrupt overflow.

If in PWM mode, this is only used for clearing the interrupt.

Parameters
timerSelect timer Timer Select
Returns
The current counter value

Definition at line 383 of file timer.c.

References TIMER_CURVAL.

◆ timer_get_cycle_width()

uint32_t timer_get_cycle_width ( uint32_t  timer)

Gets the cycle width.

Only used in duty cycle capture mode.

Note
See the datasheet for more concise diagrams.
Parameters
timerSelect timer Timer Select
Returns
The cycle width

Definition at line 395 of file timer.c.

References TIMER_CAPW.

◆ timer_get_pulse_width()

uint32_t timer_get_pulse_width ( uint32_t  timer)

Gets the pulse width in pulse capture mode, or gets the period width in duty cycle capture mode.

Note
See the datasheet for more concise diagrams.
Parameters
timerSelect timer Timer Select
Returns
The pulse width

Definition at line 407 of file timer.c.

References TIMER_CAPLH.

◆ timer_get_pwm_period()

enum timer_pwm_period timer_get_pwm_period ( uint32_t  timer)

Gets the current output period in PWM mode.

Parameters
timerSelect timer Timer Select
Returns
The current output period timer_pwm_period

Definition at line 417 of file timer.c.

References TIMER_MOD2LF.

◆ timer_int_enable()

void timer_int_enable ( uint32_t  timer,
bool  en 
)

Enable or disable the interrupt.

In counter mode, when the count has been completed, an interrupt is generated. In PWM mode, on a level change, an interupt is generated. In either capture mode, when a capture is complete, an interrupt is generated.

Note
If interrupts are enabled here, the interrupt should also be enabled using the NVIC before enabling the timer.
Parameters
timerSelect timer Timer Select
enEnable or disable the interrupt

Definition at line 354 of file timer.c.

References TIMER_INTCTL, and TIMER_INTCTL_INTEN.

Referenced by timer_setup_internal().

Here is the caller graph for this function:

◆ timer_int_mask()

void timer_int_mask ( uint32_t  timer,
enum timer_int_masked  masked 
)

Sets the interrupt mask.

Parameters
timerSelect timer Timer Select
maskedWhether or not to mask the interrupt timer_int_masked

Definition at line 368 of file timer.c.

References TIMER_INTCTL, and TIMER_INTCTL_INTMSK.

Referenced by timer_setup_internal().

Here is the caller graph for this function:

◆ timer_int_overflow_status()

bool timer_int_overflow_status ( uint32_t  timer)

Gets the interrupt overflow status.

Overflow will occur if the interrupt has not been cleared when a second interrupt happens.

Parameters
timerSelect timer Timer Select
Returns
The interrupt overflow status

Definition at line 449 of file timer.c.

References TIMER_INTFLAG.

◆ timer_int_raw_status()

bool timer_int_raw_status ( uint32_t  timer)

Gets the interrupt status before masking.

Parameters
timerSelect timer Timer Select
Returns
The interrupt status before masking

Definition at line 437 of file timer.c.

References TIMER_INTSTAT.

◆ timer_int_status()

bool timer_int_status ( uint32_t  timer)

Gets the interrupt status after masking.

Parameters
timerSelect timer Timer Select
Returns
The interrupt status after masking

Definition at line 427 of file timer.c.

References TIMER_INTMSKSTAT.

◆ timer_loop_mode()

void timer_loop_mode ( uint32_t  timer,
enum timer_loop_modes  mode 
)

Selects the loop mode.

This has no use in PWM mode. In loop mode with counter mode, the counter will constantly loop. In loop mode with the capture modes, the values will be captured again and again. In single mode, these operations happen only once.

Parameters
timerSelect timer Timer Select
modeThe loop mode timer_loop_modes

Definition at line 292 of file timer.c.

References TIMER_CTRL, and TIMER_CTRL_LMOD.

Referenced by timer_setup_internal().

Here is the caller graph for this function:

◆ timer_operation_mode()

void timer_operation_mode ( uint32_t  timer,
enum timer_operation_modes  mode 
)

Selects the mode of operation.

Parameters
timerSelect timer Timer Select
modeThe mode of operation timer_operation_modes

Definition at line 234 of file timer.c.

References TIMER_CTRL, TIMER_CTRL_OUTMOD_MASK, and TIMER_CTRL_OUTMOD_SHIFT.

Referenced by timer_setup_internal().

Here is the caller graph for this function:

◆ timer_output_level()

void timer_output_level ( uint32_t  timer,
enum timer_level  level 
)

Selects the initial output level.

Only used in counter and PWM modes.

Parameters
timerSelect timer Timer Select
levelThe initial output level timer_level

Definition at line 264 of file timer.c.

References TIMER_OUTPVAL.

Referenced by timer_setup_internal().

Here is the caller graph for this function:

◆ timer_output_mode()

void timer_output_mode ( uint32_t  timer,
enum timer_output_modes  mode 
)

Selects the output mode.

Only used in counter mode. When done counting, the pin can be set to no output, to invert the current pin level, to set the pin high, or to set the pin low.

Note
Be sure to set the alternate functions of the timer pins with syscon_sel_af() and disable SWD on those pins with syscon_sel_swd() as needed.
Parameters
timerSelect timer Timer Select
modeThe output mode timer_output_modes

Definition at line 252 of file timer.c.

References TIMER_CTRL, TIMER_CTRL_WMOD_MASK, and TIMER_CTRL_WMOD_SHIFT.

Referenced by timer_setup_internal().

Here is the caller graph for this function:

◆ timer_pulse_capture_setup()

void timer_pulse_capture_setup ( uint32_t  timer,
bool  timer_int_en,
enum timer_edge_modes  edge_mode,
enum timer_loop_modes  loop_mode 
)

Setup the timer in pulse capture mode.

Note
Call timer_enable() when you are ready to start the timer.
Be sure to set the alternate functions of the timer pins with syscon_sel_af() and disable SWD on those pins with syscon_sel_swd() as needed.
If interrupts are enabled here, the interrupt should also be enabled using the NVIC before enabling the timer.
Parameters
timerSelect timer Timer Select
timer_int_enPassed to timer_int_enable()
edge_modePassed to timer_edge_mode()
loop_modePassed to timer_loop_mode()

Definition at line 140 of file timer.c.

References TIMER_CLK_INTERNAL, TIMER_LEVEL_LOW, TIMER_MODE_PULSE_CAPTURE, TIMER_OUTPUT_NONE, and timer_setup_internal().

Here is the call graph for this function:

◆ timer_pwm_setup()

void timer_pwm_setup ( uint32_t  timer,
bool  timer_int_en,
enum timer_edge_modes  edge_mode,
enum timer_clk_src  clk_src,
enum timer_level  output_level,
uint16_t  period0,
uint16_t  period1 
)

Setup the timer in PWM mode.

Note
Call timer_enable() when you are ready to start the timer.
Be sure to set the alternate functions of the timer pins with syscon_sel_af() and disable SWD on those pins with syscon_sel_swd() as needed.
If interrupts are enabled here, the interrupt should also be enabled using the NVIC before enabling the timer.
Parameters
timerSelect timer Timer Select
timer_int_enPassed to timer_int_enable()
edge_modePassed to timer_edge_mode()
clk_srcPassed to timer_clock_source()
output_levelPassed to timer_output_level()
period0Passed to timer_pwm_target_value()
period1Passed to timer_pwm_target_value()

Definition at line 113 of file timer.c.

References TIMER_LOOP_MODE, TIMER_MODE_PWM, TIMER_OUTPUT_NONE, timer_pwm_target_value(), and timer_setup_internal().

Here is the call graph for this function:

◆ timer_pwm_target_value()

void timer_pwm_target_value ( uint32_t  timer,
uint16_t  period0,
uint16_t  period1 
)

Sets the target values for PWM mode.

Parameters
timerSelect timer Timer Select
period0length of period 0 in clock cycles. Whether it is high or low is set in timer_output_level()
period1length of period 1

Definition at line 337 of file timer.c.

References timer_counter_target_value().

Referenced by timer_pwm_setup().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ timer_setup_internal()

static void timer_setup_internal ( uint32_t  timer,
bool  timer_int_en,
enum timer_operation_modes  op_mode,
enum timer_edge_modes  edge_mode,
enum timer_loop_modes  loop_mode,
enum timer_clk_src  clk_src,
enum timer_output_modes  output_mode,
enum timer_level  output_level 
)
static

Internal function for timer setup.

Parameters
timerSelect timer Timer Select
timer_int_enPassed to timer_int_enable()
op_modePassed to timer_operation_mode()
edge_modePassed to timer_edge_mode()
loop_modePassed to timer_loop_mode()
clk_srcPassed to timer_clock_source()
output_modePassed to timer_output_mode()
output_levelPassed to timer_output_level()

Definition at line 42 of file timer.c.

References TIMER_CLK_INTERNAL, timer_clock_enable(), timer_clock_source(), timer_edge_mode(), timer_enable(), timer_int_enable(), timer_int_mask(), timer_loop_mode(), timer_operation_mode(), timer_output_level(), timer_output_mode(), and TIMER_UNMASKED.

Referenced by timer_counter_setup(), timer_duty_cycle_capture_setup(), timer_pulse_capture_setup(), and timer_pwm_setup().

Here is the call graph for this function:
Here is the caller graph for this function: