1 /**************************************************************************/
4 @author K. Townsend (microBuilder.eu)
10 Controls the 24-bit 'system tick' clock, which can be used as a
11 generic timer or to control time sharing with an embedded real-time
12 operating system (such as FreeRTOS).
17 #include "core/cpu/cpu.h"
18 #include "core/systick/systick.h"
24 // Start systick timer with one tick every 10ms
35 Software License Agreement (BSD License)
37 Copyright (c) 2010, microBuilder SARL
40 Redistribution and use in source and binary forms, with or without
41 modification, are permitted provided that the following conditions are met:
42 1. Redistributions of source code must retain the above copyright
43 notice, this list of conditions and the following disclaimer.
44 2. Redistributions in binary form must reproduce the above copyright
45 notice, this list of conditions and the following disclaimer in the
46 documentation and/or other materials provided with the distribution.
47 3. Neither the name of the copyright holders nor the
48 names of its contributors may be used to endorse or promote products
49 derived from this software without specific prior written permission.
51 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS ''AS IS'' AND ANY
52 EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
53 WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
54 DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER BE LIABLE FOR ANY
55 DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
56 (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
57 LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
58 ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
59 (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
60 SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
62 /**************************************************************************/
67 #include "drivers/fatfs/diskio.h"
68 volatile uint32_t fatTicks
= 0;
71 volatile uint32_t systickTicks
= 0; // 1ms tick counter
72 volatile uint32_t systickRollovers
= 0;
74 /**************************************************************************/
76 @brief Systick interrupt handler
78 /**************************************************************************/
79 void SysTick_Handler (void)
83 // Increment rollover counter
84 if (systickTicks
== 0xFFFFFFFF) systickRollovers
++;
96 /**************************************************************************/
98 @brief Configures the systick timer
101 The number of clock cycles between each tick of the
102 systick timer. for example, 'CFG_CPU_CCLK / 1000' =
103 1 millisecond. This value must not exceed 0x00FFFFFF.
105 /**************************************************************************/
106 static uint32_t systickConfig(uint32_t ticks
)
108 // Check if 'ticks' is greater than maximum value
109 if (ticks
> SYSTICK_STRELOAD_MASK
)
117 // Set reload register
118 SYSTICK_STRELOAD
= (ticks
& SYSTICK_STRELOAD_MASK
) - 1;
120 // Load the systick counter value
123 // Enable systick IRQ and timer
124 SYSTICK_STCTRL
= SYSTICK_STCTRL_CLKSOURCE
|
125 SYSTICK_STCTRL_TICKINT
|
126 SYSTICK_STCTRL_ENABLE
;
131 /**************************************************************************/
133 @brief Initialises the systick timer
136 The number of milliseconds between each tick of the systick
139 @note The shortest possible delay is 1 millisecond, which will
140 allow fine grained delays, but will cause more load on the
141 system than a 10mS delay. The resolution of the systick
142 timer needs to be balanced with the amount of processing
143 time you can spare. The delay should really only be set
144 to 1 mS if you genuinely have a need for 1mS delays,
145 otherwise a higher value like 5 or 10 mS is probably
148 /**************************************************************************/
149 void systickInit (uint32_t delayMs
)
151 systickConfig ((CFG_CPU_CCLK
/ 1000) * delayMs
);
154 /**************************************************************************/
156 @brief Causes a blocking delay for 'delayTicks' ticks on the
157 systick timer. For example: systickDelay(100) would cause
158 a blocking delay for 100 ticks of the systick timer.
160 @param[in] delayTicks
161 The number of systick ticks to cause a blocking delay for
163 @Note This function takes into account the fact that the tick
164 counter may eventually roll over to 0 once it reaches
167 /**************************************************************************/
168 void systickDelay (uint32_t delayTicks
)
171 curTicks
= systickTicks
;
173 // Make sure delay is at least 1 tick in case of division, etc.
174 if (delayTicks
== 0) delayTicks
= 1;
176 if (curTicks
> 0xFFFFFFFF - delayTicks
)
178 // Rollover will occur during delay
179 while (systickTicks
>= curTicks
)
181 while (systickTicks
< (delayTicks
- (0xFFFFFFFF - curTicks
)));
186 while ((systickTicks
- curTicks
) < delayTicks
);
190 /**************************************************************************/
192 @brief Returns the current value of the systick timer counter.
193 This value is incremented by one every time an interrupt
194 fires for the systick timer.
196 /**************************************************************************/
197 uint32_t systickGetTicks(void)
202 /**************************************************************************/
204 @brief Returns the current value of the systick timer rollover
205 counter. This value is incremented by one every time the
206 tick counter rolls over from 0xFFFFFFFF to 0.
208 /**************************************************************************/
209 uint32_t systickGetRollovers(void)
211 return systickRollovers
;
214 /**************************************************************************/
216 @brief Returns the approximate number of seconds that the
217 systick timer has been running.
219 /**************************************************************************/
220 uint32_t systickGetSecondsActive(void)
222 uint32_t currentTick
= systickTicks
;
223 uint32_t rollovers
= systickRollovers
;
224 uint32_t secsActive
= currentTick
/ (1000 / CFG_SYSTICK_DELAY_IN_MS
);
225 secsActive
+= rollovers
* (0xFFFFFFFF / (1000 / CFG_SYSTICK_DELAY_IN_MS
));
This page took 0.077224 seconds and 5 git commands to generate.