libopencm3
A free/libre/open-source firmware library for various ARM Cortex-M3 microcontrollers.
systick.c
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_file SysTick
21 *
22 * @ingroup CM3_files
23 *
24 * @brief <b>libopencm3 Cortex System Tick Timer</b>
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 * This library supports the System Tick timer in ARM Cortex Microcontrollers.
33 *
34 * The System Tick timer is part of the ARM Cortex core. It is a 24 bit
35 * down counter that can be configured with an automatical reload value.
36 *
37 * LGPL License Terms @ref lgpl_license
38 */
39
40/**@{*/
42
43/*---------------------------------------------------------------------------*/
44/** @brief SysTick Set the Automatic Reload Value.
45 *
46 * The counter is set to the reload value when the counter starts and after it
47 * reaches zero.
48 *
49 * @note The systick counter value might be undefined upon startup. To get
50 * predictable behavior, it is a good idea to set or clear the counter after
51 * set reload. @sa systick_clear
52 *
53 * @param[in] value uint32_t. 24 bit reload value.
54 */
55void systick_set_reload(uint32_t value)
56{
57 STK_RVR = (value & STK_RVR_RELOAD);
58}
59
60/*---------------------------------------------------------------------------*/
61/** @brief SysTick Read the Automatic Reload Value.
62 *
63 * @returns 24 bit reload value as uint32_t.
64 */
65
66uint32_t systick_get_reload(void)
67{
68 return STK_RVR & STK_RVR_RELOAD;
69}
70
71/** @brief SysTick Set clock and frequency of overflow
72 *
73 * This function sets the systick to AHB clock source, and the prescaler to
74 * generate interrupts with the desired frequency. The function fails, if
75 * the frequency is too low.
76 *
77 * @param[in] freq uint32_t The desired frequency in Hz
78 * @param[in] ahb uint32_t The current AHB frequency in Hz
79 * @returns true, if success, false if the desired frequency cannot be set.
80 */
81bool systick_set_frequency(uint32_t freq, uint32_t ahb)
82{
83 uint32_t ratio = ahb / freq;
84
85#if defined(__ARM_ARCH_6M__)
87#else
88 if (ratio >= (STK_RVR_RELOAD * 8)) {
89 /* This frequency is too slow */
90 return false;
91 } else if (ratio >= STK_RVR_RELOAD) {
92 ratio /= 8;
93 systick_set_clocksource(STK_CSR_CLKSOURCE_AHB_DIV8);
94 } else {
96 }
97#endif
98 systick_set_reload(ratio - 1);
99 return true;
100}
101
102/*---------------------------------------------------------------------------*/
103/** @brief Get the current SysTick counter value.
104 *
105 * @returns 24 bit current value as uint32_t.
106 */
107
108uint32_t systick_get_value(void)
109{
110 return STK_CVR & STK_CVR_CURRENT;
111}
112
113/*---------------------------------------------------------------------------*/
114/** @brief Set the SysTick Clock Source.
115 *
116 * The clock source can be either the AHB clock or the same clock divided by 8.
117 *
118 * @param[in] clocksource uint8_t. Clock source from @ref systick_clksource.
119 */
120
121void systick_set_clocksource(uint8_t clocksource)
122{
123 STK_CSR = (STK_CSR & ~STK_CSR_CLKSOURCE) |
124 (clocksource & STK_CSR_CLKSOURCE);
125}
126
127/*---------------------------------------------------------------------------*/
128/** @brief Enable SysTick Interrupt.
129 *
130 */
131
133{
135}
136
137/*---------------------------------------------------------------------------*/
138/** @brief Disable SysTick Interrupt.
139 *
140 */
141
143{
144 STK_CSR &= ~STK_CSR_TICKINT;
145}
146
147/*---------------------------------------------------------------------------*/
148/** @brief Enable SysTick Counter.
149 *
150 */
151
153{
155}
156
157/*---------------------------------------------------------------------------*/
158/** @brief Disable SysTick Counter.
159 *
160 */
161
163{
164 STK_CSR &= ~STK_CSR_ENABLE;
165}
166
167/*---------------------------------------------------------------------------*/
168/** @brief SysTick Read the Counter Flag.
169 *
170 * The count flag is set when the timer count becomes zero, and is cleared when
171 * the flag is read.
172 *
173 * @returns Boolean if flag set.
174 */
175
177{
178 return (STK_CSR & STK_CSR_COUNTFLAG) ? 1 : 0;
179}
180
181/*---------------------------------------------------------------------------*/
182/** @brief SysTick Clear counter Value.
183 *
184 * The counter value is cleared. Useful for well defined startup.
185 */
186
188{
189 STK_CVR = 0;
190}
191
192/*---------------------------------------------------------------------------*/
193/** @brief SysTick Get Calibration Value
194 *
195 * @returns Current calibration value
196 */
197uint32_t systick_get_calib(void)
198{
199 return STK_CALIB & STK_CALIB_TENMS;
200}
201/**@}*/
202
#define STK_CALIB
SysTick Calibration Value Register(Read Only) (CALIB) Reads the calibration value and parameters for ...
Definition: systick.h:90
#define STK_RVR
SysTick Reload Value Register (RVR).
Definition: systick.h:76
#define STK_CSR
SysTick Control and Status Register (CSR).
Definition: systick.h:66
#define STK_CVR
SysTick Current Value Register (CVR).
Definition: systick.h:83
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
#define STK_CALIB_TENMS
TENMS Calibration value for 10ms.
Definition: systick.h:173
#define STK_CSR_COUNTFLAG
COUNTFLAG Indicates whether the counter has counted to 0 since the last read of this register: 0 = Ti...
Definition: systick.h:101
#define STK_CSR_ENABLE
ENABLE: Counter enable.
Definition: systick.h:127
#define STK_CSR_TICKINT
TICKINT: SysTick exception request enable.
Definition: systick.h:125
#define STK_CSR_CLKSOURCE
CLKSOURCE: Clock source selection for 0, SysTick uses the IMPLEMENTATION DEFINED external reference c...
Definition: systick.h:109
#define STK_RVR_RELOAD
RELOAD[23:0]: RELOAD value.
Definition: systick.h:134
#define STK_CVR_CURRENT
CURRENT[23:0]: Current counter value.
Definition: systick.h:143
#define STK_CSR_CLKSOURCE_AHB
Definition: systick.h:117