1 /* *****************************************************************************
2  * Copyright (C) 2016 Maxim Integrated Products, Inc., All Rights Reserved.
3  *
4  * Permission is hereby granted, free of charge, to any person obtaining a
5  * copy of this software and associated documentation files (the "Software"),
6  * to deal in the Software without restriction, including without limitation
7  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8  * and/or sell copies of the Software, and to permit persons to whom the
9  * Software is furnished to do so, subject to the following conditions:
10  *
11  * The above copyright notice and this permission notice shall be included
12  * in all copies or substantial portions of the Software.
13  *
14  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
15  * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
17  * IN NO EVENT SHALL MAXIM INTEGRATED BE LIABLE FOR ANY CLAIM, DAMAGES
18  * OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
19  * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
20  * OTHER DEALINGS IN THE SOFTWARE.
21  *
22  * Except as contained in this notice, the name of Maxim Integrated
23  * Products, Inc. shall not be used except as stated in the Maxim Integrated
24  * Products, Inc. Branding Policy.
25  *
26  * The mere transfer of this software does not imply any licenses
27  * of trade secrets, proprietary technology, copyrights, patents,
28  * trademarks, maskwork rights, or any other form of intellectual
29  * property whatsoever. Maxim Integrated Products, Inc. retains all
30  * ownership rights.
31  *
32  * $Id: mxc_delay.c 36202 2018-07-16 21:06:02Z michael.bayern $
33  *
34  *************************************************************************** */
35 
36 /* **** Includes **** */
37 #include <stdint.h>
38 #include "mxc_config.h"
39 #include "mxc_delay.h"
40 
41 /* **** File Scope Variables **** */
42 static uint32_t ctrl_save;
43 static volatile uint64_t compare_value = 0;
44 static volatile uint64_t curr_value;
45 static volatile uint32_t reload;
46 
47 static void mxc_delay_init(unsigned long us);
48 extern void SysTick_Handler(void);
49 
50 /* ************************************************************************** */
SysTick_Handler(void)51 __weak void SysTick_Handler(void)
52 {
53     mxc_delay_handler();
54 }
55 
56 /* ************************************************************************** */
mxc_delay_handler(void)57 void mxc_delay_handler(void)
58 {
59     // Check and clear overflow flag
60     if (SysTick->CTRL & SysTick_CTRL_COUNTFLAG_Msk) {
61     	// Is a delay in progress?
62     	if(compare_value != 0) {
63     		curr_value += reload;
64     		if(curr_value >= compare_value)	{
65             	mxc_delay_stop();
66         	}
67         }
68     }
69 }
70 
71 /* ************************************************************************** */
mxc_delay_init(unsigned long us)72 static void mxc_delay_init(unsigned long us)
73 {
74     uint32_t starttick, ticks;
75 
76     // Record the current tick value and clear the overflow flag
77     starttick = SysTick->VAL;
78 
79     // Save the state of control register (and clear the overflow flag)
80     ctrl_save = SysTick->CTRL & ~SysTick_CTRL_COUNTFLAG_Msk;
81 
82     // If the SysTick is not running, configure and start it
83     if (!(SysTick->CTRL & SysTick_CTRL_ENABLE_Msk)) {
84         SysTick->LOAD = SysTick_LOAD_RELOAD_Msk;
85         SysTick->VAL = SysTick_VAL_CURRENT_Msk;
86         SysTick->CTRL = SysTick_CTRL_CLKSOURCE_Msk;
87         starttick = SysTick_VAL_CURRENT_Msk;
88         reload = SysTick_LOAD_RELOAD_Msk + 1;
89     } else {
90         reload = SysTick->LOAD + 1; // get the current reload value
91     }
92 
93     // Calculate the total number of ticks to delay
94     ticks = (uint32_t)(((uint64_t)us * (uint64_t)SystemCoreClock) / 1000000);
95 
96     compare_value = ticks + (reload - starttick);
97     curr_value = 0;
98 
99     if (!(SysTick->CTRL & SysTick_CTRL_ENABLE_Msk)) {
100         SysTick->CTRL |= SysTick_CTRL_ENABLE_Msk;
101     }
102 }
103 
104 /* ************************************************************************** */
mxc_delay_start(unsigned long us)105 int mxc_delay_start(unsigned long us)
106 {
107     // Check if timeout currently ongoing
108     if (compare_value != 0) {
109         return E_BUSY;
110     }
111 
112     // Check if there is nothing to do
113     if (us == 0) {
114         return E_NO_ERROR;
115     }
116 
117     // Calculate the necessary delay and start the timer
118     mxc_delay_init(us);
119 
120     // Enable SysTick interrupt if necessary
121     if (compare_value != 0) {
122         SysTick->CTRL |= SysTick_CTRL_TICKINT_Msk;
123     }
124 
125     return E_NO_ERROR;
126 }
127 
128 /* ************************************************************************** */
mxc_delay_check(void)129 int mxc_delay_check(void)
130 {
131     // Check if timeout currently ongoing
132     if (compare_value == 0) {
133         return E_NO_ERROR;
134     }
135 
136     if((curr_value + (reload - SysTick->VAL)) >= compare_value) {
137         mxc_delay_stop();
138         return E_NO_ERROR;
139     }
140 
141     return E_BUSY;
142 }
143 
144 /* ************************************************************************** */
mxc_delay_stop(void)145 void mxc_delay_stop(void)
146 {
147     SysTick->CTRL = ctrl_save;
148     compare_value = 0;
149 }
150 
151 /* ************************************************************************** */
mxc_delay(unsigned long us)152 int mxc_delay(unsigned long us)
153 {
154     // Check if timeout currently ongoing
155     if (compare_value != 0) {
156         return E_BUSY;
157     }
158 
159     // Check if there is nothing to do
160     if (us == 0) {
161         return E_NO_ERROR;
162     }
163 
164     // Calculate the necessary delay and start the timer
165     mxc_delay_init(us);
166 
167     // Wait until the total number of ticks exceeds the compare value.
168     while ((curr_value + (reload - SysTick->VAL)) < compare_value) {
169         // If SysTick interrupts are enabled, COUNTFLAG will never be set here and
170         // curr_value will be incremented in the ISR. If SysTick interrupts are
171         // disabled, curr_value is incremented here.
172         if (SysTick->CTRL & SysTick_CTRL_COUNTFLAG_Msk) {
173 	    	curr_value += reload;
174         }
175     }
176 
177     mxc_delay_stop();
178     return E_NO_ERROR;
179 }
180