libopencm3
A free/libre/open-source firmware library for various ARM Cortex-M3 microcontrollers.
timer.h
Go to the documentation of this file.
1/** @defgroup timer_defines Timer Defines
2 *
3 * @brief <b>Defined Constants and Types for the SWM050 Timer</b>
4 *
5 * @ingroup SWM050_defines
6 *
7 * LGPL License Terms @ref lgpl_license
8 */
9/*
10 * This file is part of the libopencm3 project.
11 *
12 * Copyright (C) 2020 Caleb Szalacinski <contact@skiboy.net>
13 *
14 * This library is free software: you can redistribute it and/or modify
15 * it under the terms of the GNU Lesser General Public License as published by
16 * the Free Software Foundation, either version 3 of the License, or
17 * (at your option) any later version.
18 *
19 * This library is distributed in the hope that it will be useful,
20 * but WITHOUT ANY WARRANTY; without even the implied warranty of
21 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22 * GNU Lesser General Public License for more details.
23 *
24 * You should have received a copy of the GNU Lesser General Public License
25 * along with this library. If not, see <http://www.gnu.org/licenses/>.
26 */
27/**@{*/
28#ifndef LIBOPENCM3_TIMER_H
29#define LIBOPENCM3_TIMER_H
32
33/** @defgroup timer_select Timer Select
34@{*/
35#define TIMER_SE0 TIMER_SE0_BASE
36#define TIMER_SE1 TIMER_SE1_BASE
37/*@}*/
38
39/** Timer Level */
43};
44
45/** Timer Edge Modes */
47 /** Trigger on rising edge */
49 /** Trigger on falling edge */
51};
52
53/** Timer Operation Modes */
59};
60
61/** Timer Clock Source */
65};
66
67/** Timer Interrupt Mask */
71};
72
73/** Timer Loop Modes */
77};
78
79/** Timer Output Modes */
85};
86
87/** Timer PWM Periods */
91};
92
93/** Timer Clock Divider Mask */
94#define TIMER_DIV_MASK (0x3F << 16)
95
96/** @defgroup timer_registers Timer Registers
97@{*/
98/** Timer control register */
99#define TIMER_CTRL(x) MMIO32(x + 0x0)
100/** The target value(s). Treated as uint32_t in counter mode (0), and as 2
101 uint16_t values in PWM mode (1) */
102#define TIMER_TARVAL(x) MMIO32(x + 0x4)
103/** Current count value in modes 0, 2, and 3 */
104#define TIMER_CURVAL(x) MMIO32(x + 0x8)
105/** Cycle width in mode 3 */
106#define TIMER_CAPW(x) MMIO32(x + 0xC)
107/** Pulse width in modes 2 and 3 */
108#define TIMER_CAPLH(x) MMIO32(x + 0x10)
109/** PWM state in mode 1 */
110#define TIMER_MOD2LF(x) MMIO32(x + 0x14)
111/** Timer output pin value */
112#define TIMER_OUTPVAL(x) MMIO32(x + 0x80)
113/** Interrupt enable and mask */
114#define TIMER_INTCTL(x) MMIO32(x + 0x84)
115/** Interrupt status before masking */
116#define TIMER_INTSTAT(x) MMIO32(x + 0x88)
117/** Interrupt status after masking */
118#define TIMER_INTMSKSTAT(x) MMIO32(x + 0x8C)
119/** Interrupt overflow; 1 if interrupt occurs again without being cleared */
120#define TIMER_INTFLAG(x) MMIO32(x + 0x90)
121/*@}*/
122
123/** @defgroup timer_reg_values Timer Register Values
124@{*/
125#define TIMER_CTRL_EN 1
126/** Clock source selection */
127#define TIMER_CTRL_OSCMOD (1 << 8)
128/** Valid edge selection */
129#define TIMER_CTRL_TMOD (1 << 16)
130/** Loop mode selection */
131#define TIMER_CTRL_LMOD (1 << 28)
132/** Timer Output Mode Mask */
133#define TIMER_CTRL_OUTMOD_MASK 0x3
134#define TIMER_CTRL_OUTMOD_SHIFT 12
135/** Timer Operation Mode Mask */
136#define TIMER_CTRL_WMOD_MASK 0x3
137#define TIMER_CTRL_WMOD_SHIFT 4
138/** Interrupt mask */
139#define TIMER_INTCTL_INTMSK (1 << 1)
140/** Interrupt enable */
141#define TIMER_INTCTL_INTEN 1
142/*@}*/
143
145
146void timer_counter_setup(uint32_t timer,
147 bool timer_int_en,
148 enum timer_edge_modes edge_mode,
149 enum timer_loop_modes loop_mode,
150 enum timer_clk_src clk_src,
151 enum timer_output_modes output_mode,
152 enum timer_level output_level,
153 uint32_t target);
154
155void timer_pwm_setup(uint32_t timer,
156 bool timer_int_en,
157 enum timer_edge_modes edge_mode,
158 enum timer_clk_src clk_src,
159 enum timer_level output_level,
160 uint16_t target1,
161 uint16_t target2);
162
163void timer_pulse_capture_setup(uint32_t timer,
164 bool timer_int_en,
165 enum timer_edge_modes edge_mode,
166 enum timer_loop_modes loop_mode);
167
168void timer_duty_cycle_capture_setup(uint32_t timer,
169 bool timer_int_en,
170 enum timer_edge_modes edge_mode,
171 enum timer_loop_modes loop_mode);
172
173void timer_clock_div(uint8_t div);
174void timer_enable(uint32_t timer, bool en);
175void timer_clock_enable(uint32_t timer, bool en);
176void timer_operation_mode(uint32_t timer, enum timer_operation_modes mode);
177void timer_output_mode(uint32_t timer, enum timer_output_modes mode);
178void timer_output_level(uint32_t timer, enum timer_level level);
179void timer_edge_mode(uint32_t timer, enum timer_edge_modes mode);
180void timer_loop_mode(uint32_t timer, enum timer_loop_modes mode);
181void timer_clock_source(uint32_t timer, enum timer_clk_src src);
182void timer_counter_target_value(uint32_t timer, uint32_t target);
183void timer_pwm_target_value(uint32_t timer, uint16_t period0, uint16_t period1);
184void timer_int_enable(uint32_t timer, bool en);
185void timer_int_mask(uint32_t timer, enum timer_int_masked masked);
186uint32_t timer_get_current_value(uint32_t timer);
187uint32_t timer_get_cycle_width(uint32_t timer);
188uint32_t timer_get_pulse_width(uint32_t timer);
189enum timer_pwm_period timer_get_pwm_period(uint32_t timer);
190bool timer_int_status(uint32_t timer);
191bool timer_int_raw_status(uint32_t timer);
192bool timer_int_overflow_status(uint32_t timer);
193
195
196#endif
197/**@}*/
#define END_DECLS
Definition: common.h:34
#define BEGIN_DECLS
Definition: common.h:33
uint32_t timer_get_cycle_width(uint32_t timer)
Gets the cycle width.
Definition: timer.c:395
bool timer_int_overflow_status(uint32_t timer)
Gets the interrupt overflow status.
Definition: timer.c:449
void timer_pwm_target_value(uint32_t timer, uint16_t period0, uint16_t period1)
Sets the target values for PWM mode.
Definition: timer.c:337
void timer_output_level(uint32_t timer, enum timer_level level)
Selects the initial output level.
Definition: timer.c:264
void timer_int_mask(uint32_t timer, enum timer_int_masked masked)
Sets the interrupt mask.
Definition: timer.c:368
void timer_operation_mode(uint32_t timer, enum timer_operation_modes mode)
Selects the mode of operation.
Definition: timer.c:234
bool timer_int_status(uint32_t timer)
Gets the interrupt status after masking.
Definition: timer.c:427
void timer_clock_div(uint8_t div)
Set the timer clock divider, based off of the 18MHz oscillator.
Definition: timer.c:185
enum timer_pwm_period timer_get_pwm_period(uint32_t timer)
Gets the current output period in PWM mode.
Definition: timer.c:417
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 target1, uint16_t target2)
Setup the timer in PWM mode.
Definition: timer.c:113
void timer_edge_mode(uint32_t timer, enum timer_edge_modes mode)
Selects the edge mode.
Definition: timer.c:274
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.
Definition: timer.c:83
void timer_clock_enable(uint32_t timer, bool en)
Enables or disables the timer's internal clock.
Definition: timer.c:212
void timer_counter_target_value(uint32_t timer, uint32_t target)
Sets the target values for counter mode.
Definition: timer.c:325
uint32_t timer_get_current_value(uint32_t timer)
Gets the current counter value, and clears the interrupt/interrupt overflow.
Definition: timer.c:383
void timer_clock_source(uint32_t timer, enum timer_clk_src src)
Selects the clock source for the timer.
Definition: timer.c:311
void timer_output_mode(uint32_t timer, enum timer_output_modes mode)
Selects the output mode.
Definition: timer.c:252
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.
Definition: timer.c:163
void timer_enable(uint32_t timer, bool en)
Enables or disables the timer.
Definition: timer.c:198
void timer_loop_mode(uint32_t timer, enum timer_loop_modes mode)
Selects the loop mode.
Definition: timer.c:292
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.
Definition: timer.c:140
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.
Definition: timer.c:407
bool timer_int_raw_status(uint32_t timer)
Gets the interrupt status before masking.
Definition: timer.c:437
void timer_int_enable(uint32_t timer, bool en)
Enable or disable the interrupt.
Definition: timer.c:354
timer_output_modes
Timer Output Modes.
Definition: timer.h:80
timer_level
Timer Level.
Definition: timer.h:40
timer_pwm_period
Timer PWM Periods.
Definition: timer.h:88
timer_loop_modes
Timer Loop Modes.
Definition: timer.h:74
timer_edge_modes
Timer Edge Modes.
Definition: timer.h:46
timer_clk_src
Timer Clock Source.
Definition: timer.h:62
timer_int_masked
Timer Interrupt Mask.
Definition: timer.h:68
timer_operation_modes
Timer Operation Modes.
Definition: timer.h:54
@ TIMER_OUTPUT_NONE
Definition: timer.h:81
@ TIMER_OUTPUT_HIGH
Definition: timer.h:83
@ TIMER_OUTPUT_INVERT
Definition: timer.h:82
@ TIMER_OUTPUT_LOW
Definition: timer.h:84
@ TIMER_LEVEL_HIGH
Definition: timer.h:42
@ TIMER_LEVEL_LOW
Definition: timer.h:41
@ TIMER_PERIOD_0
Definition: timer.h:89
@ TIMER_PERIOD_1
Definition: timer.h:90
@ TIMER_SINGLE_MODE
Definition: timer.h:76
@ TIMER_LOOP_MODE
Definition: timer.h:75
@ TIMER_EDGE_FALLING
Trigger on falling edge.
Definition: timer.h:50
@ TIMER_EDGE_RISING
Trigger on rising edge.
Definition: timer.h:48
@ TIMER_CLK_EXTERNAL
Definition: timer.h:64
@ TIMER_CLK_INTERNAL
Definition: timer.h:63
@ TIMER_MASKED
Definition: timer.h:70
@ TIMER_UNMASKED
Definition: timer.h:69
@ TIMER_MODE_DUTY_CYCLE_CAPTURE
Definition: timer.h:58
@ TIMER_MODE_COUNTER
Definition: timer.h:55
@ TIMER_MODE_PULSE_CAPTURE
Definition: timer.h:57
@ TIMER_MODE_PWM
Definition: timer.h:56