libopencm3
A free/libre/open-source firmware library for various ARM Cortex-M3 microcontrollers.
systick.h
Go to the documentation of this file.
1/*
2 * This file is part of the libopencm3 project.
3 *
4 * Copyright (C) 2010 Thomas Otto <tommi@viadmin.org>
5 * Copyright (C) 2012 Benjamin Vernoux <titanmkd@gmail.com>
6 *
7 * This library is free software: you can redistribute it and/or modify
8 * it under the terms of the GNU Lesser General Public License as published by
9 * the Free Software Foundation, either version 3 of the License, or
10 * (at your option) any later version.
11 *
12 * This library is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU Lesser General Public License for more details.
16 *
17 * You should have received a copy of the GNU Lesser General Public License
18 * along with this library. If not, see <http://www.gnu.org/licenses/>.
19 */
20/** @defgroup CM3_systick_defines Cortex-M SysTick Defines
21 *
22 * @brief <b>libopencm3 Defined Constants and Types for the Cortex SysTick </b>
23 *
24 * @ingroup CM3_defines
25 *
26 * @version 1.0.0
27 *
28 * @author @htmlonly &copy; @endhtmlonly 2010 Thomas Otto <tommi@viadmin.org>
29 *
30 * @date 19 August 2012
31 *
32 * System Control Space (SCS) => System timer register support in the SCS.
33 * To configure SysTick, load the interval required between SysTick events to
34 * the SysTick Reload Value register. The timer interrupt, or COUNTFLAG bit in
35 * the SysTick Control and Status register, is activated on the transition from
36 * 1 to 0, therefore it activates every n+1 clock ticks. If you require a
37 * period of 100, write 99 to the SysTick Reload Value register. The SysTick
38 * Reload Value register supports values between 0x1 and 0x00FFFFFF.
39 *
40 * If you want to use SysTick to generate an event at a timed interval, for
41 * example 1ms, you can use the SysTick Calibration Value Register to scale
42 * your value for the Reload register. The SysTick Calibration Value Register
43 * is a read-only register that contains the number of pulses for a period of
44 * 10ms, in the TENMS field, bits[23:0].
45 *
46 * This register also has a SKEW bit. Bit[30] == 1 indicates that the
47 * calibration for 10ms in the TENMS section is not exactly 10ms due to clock
48 * frequency. Bit[31] == 1 indicates that the reference clock is not provided.
49 *
50 * LGPL License Terms @ref lgpl_license
51 */
52
53/**@{*/
54
55#ifndef LIBOPENCM3_SYSTICK_H
56#define LIBOPENCM3_SYSTICK_H
57
60
61/** SysTick Control and Status Register (CSR).
62 * Controls the system timer and provides status data.
63 * Usage constraints: There are no usage constraints.
64 * Configurations Always implemented.
65 */
66#define STK_CSR MMIO32(SYS_TICK_BASE + 0x00)
67
68/** SysTick Reload Value Register (RVR).
69 * Reads or clears the value that will be loaded to the counter.
70 * Usage constraints:
71 * - Any write to the register clears the register to zero.
72 * - The counter does not provide read-modify-write protection.
73 * - Unsupported bits are read as zero
74 * Configurations Always implemented.
75 */
76#define STK_RVR MMIO32(SYS_TICK_BASE + 0x04)
77
78/** SysTick Current Value Register (CVR).
79 * Holds the current value of the counter.
80 * Usage constraints: There are no usage constraints.
81 * Configurations Always implemented.
82 */
83#define STK_CVR MMIO32(SYS_TICK_BASE + 0x08)
84
85/** SysTick Calibration Value Register(Read Only) (CALIB)
86 * Reads the calibration value and parameters for SysTick.
87 * Usage constraints: There are no usage constraints.
88 * Configurations Always implemented.
89 */
90#define STK_CALIB MMIO32(SYS_TICK_BASE + 0x0C)
91
92/** @defgroup STK_CSR_VALUES STK_CSR Values
93 * @{
94 */
95/** COUNTFLAG
96 * Indicates whether the counter has counted to 0 since the last read of this
97 * register:
98 * 0 = Timer has not counted to 0
99 * 1 = Timer has counted to 0.
100 */
101#define STK_CSR_COUNTFLAG (1 << 16)
102
103#define STK_CSR_CLKSOURCE_LSB 2
104/** CLKSOURCE: Clock source selection
105 * for 0, SysTick uses the IMPLEMENTATION DEFINED external reference clock.
106 * for 1, SysTick uses the processor clock.
107 * If no external clock is provided, this bit reads as 1 and ignores writes.
108 */
109#define STK_CSR_CLKSOURCE (1 << STK_CSR_CLKSOURCE_LSB)
110
111/** @defgroup systick_clksource Clock source selection
112@ingroup CM3_systick_defines
113
114@{*/
115#if defined(__ARM_ARCH_6M__)
116#define STK_CSR_CLKSOURCE_EXT (0 << STK_CSR_CLKSOURCE_LSB)
117#define STK_CSR_CLKSOURCE_AHB (1 << STK_CSR_CLKSOURCE_LSB)
118#else
119#define STK_CSR_CLKSOURCE_AHB_DIV8 (0 << STK_CSR_CLKSOURCE_LSB)
120#define STK_CSR_CLKSOURCE_AHB (1 << STK_CSR_CLKSOURCE_LSB)
121#endif
122/**@}*/
123
124/** TICKINT: SysTick exception request enable */
125#define STK_CSR_TICKINT (1 << 1)
126/** ENABLE: Counter enable */
127#define STK_CSR_ENABLE (1 << 0)
128/**@}*/
129
130/** @defgroup STK_RVR_VALUES STK_RVR Values
131 * @{
132 */
133/** RELOAD[23:0]: RELOAD value */
134#define STK_RVR_RELOAD 0x00FFFFFF
135
136/**@}*/
137
138
139/** @defgroup STK_RVR_VALUES STK_RVR Values
140 * @{
141 */
142/** CURRENT[23:0]: Current counter value */
143#define STK_CVR_CURRENT 0x00FFFFFF
144/**@}*/
145
146
147/** @defgroup STK_CALIB_VALUES STK_CALIB Values
148 * @{
149 */
150/** NOREF: NOREF flag
151 * Bit31 => NOREF Indicates whether the IMPLEMENTATION DEFINED reference clock
152 * is implemented:
153 * 0 = The reference clock is implemented.
154 * 1 = The reference clock is not implemented.
155 * When this bit is 1, the CLKSOURCE bit of the SYST_CSR register is forced to
156 * 1 and cannot be cleared to 0.
157 */
158#define STK_CALIB_NOREF (1 << 31)
159
160/** SKEW: SKEW flag
161 * Bit30 => SKEW Indicates whether the 10ms calibration value is exact:
162 * 0 = 10ms calibration value is exact.
163 * 1 = 10ms calibration value is inexact, because of the clock frequency
164 */
165#define STK_CALIB_SKEW (1 << 30)
166
167/* Bits [29:24] Reserved, must be kept cleared. */
168/** TENMS Calibration value for 10ms.
169 * Bit0 to 23 => TENMS Optionally, holds a reload value to be used for 10ms
170 * (100Hz) timing, subject to system clock skew errors. If this field is zero,
171 * the calibration value is not known.
172 */
173#define STK_CALIB_TENMS 0x00FFFFFF
174/**@}*/
175
176/* --- Function Prototypes ------------------------------------------------- */
177
179
180void systick_set_reload(uint32_t value);
181bool systick_set_frequency(uint32_t freq, uint32_t ahb);
182uint32_t systick_get_reload(void);
183uint32_t systick_get_value(void);
184void systick_set_clocksource(uint8_t clocksource);
185void systick_interrupt_enable(void);
187void systick_counter_enable(void);
188void systick_counter_disable(void);
189uint8_t systick_get_countflag(void);
190void systick_clear(void);
191
192uint32_t systick_get_calib(void);
193
195
196#endif
197/**@}*/
198
#define END_DECLS
Definition: common.h:34
#define BEGIN_DECLS
Definition: common.h:33
uint8_t systick_get_countflag(void)
SysTick Read the Counter Flag.
Definition: systick.c:176
void systick_interrupt_enable(void)
Enable SysTick Interrupt.
Definition: systick.c:132
void systick_set_clocksource(uint8_t clocksource)
Set the SysTick Clock Source.
Definition: systick.c:121
void systick_clear(void)
SysTick Clear counter Value.
Definition: systick.c:187
uint32_t systick_get_calib(void)
SysTick Get Calibration Value.
Definition: systick.c:197
bool systick_set_frequency(uint32_t freq, uint32_t ahb)
SysTick Set clock and frequency of overflow.
Definition: systick.c:81
void systick_set_reload(uint32_t value)
SysTick Set the Automatic Reload Value.
Definition: systick.c:55
void systick_counter_disable(void)
Disable SysTick Counter.
Definition: systick.c:162
void systick_counter_enable(void)
Enable SysTick Counter.
Definition: systick.c:152
uint32_t systick_get_reload(void)
SysTick Read the Automatic Reload Value.
Definition: systick.c:66
uint32_t systick_get_value(void)
Get the current SysTick counter value.
Definition: systick.c:108
void systick_interrupt_disable(void)
Disable SysTick Interrupt.
Definition: systick.c:142