1 /**
2  * @file
3  * @brief    Asynchronous delay routines based on the SysTick Timer.
4 */
5 
6 /* ****************************************************************************
7  * Copyright (C) 2016 Maxim Integrated Products, Inc., All Rights Reserved.
8  *
9  * Permission is hereby granted, free of charge, to any person obtaining a
10  * copy of this software and associated documentation files (the "Software"),
11  * to deal in the Software without restriction, including without limitation
12  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
13  * and/or sell copies of the Software, and to permit persons to whom the
14  * Software is furnished to do so, subject to the following conditions:
15  *
16  * The above copyright notice and this permission notice shall be included
17  * in all copies or substantial portions of the Software.
18  *
19  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
20  * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
21  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
22  * IN NO EVENT SHALL MAXIM INTEGRATED BE LIABLE FOR ANY CLAIM, DAMAGES
23  * OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
24  * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
25  * OTHER DEALINGS IN THE SOFTWARE.
26  *
27  * Except as contained in this notice, the name of Maxim Integrated
28  * Products, Inc. shall not be used except as stated in the Maxim Integrated
29  * Products, Inc. Branding Policy.
30  *
31  * The mere transfer of this software does not imply any licenses
32  * of trade secrets, proprietary technology, copyrights, patents,
33  * trademarks, maskwork rights, or any other form of intellectual
34  * property whatsoever. Maxim Integrated Products, Inc. retains all
35  * ownership rights.
36  *
37  * $Date: 2018-11-05 09:52:05 -0600 (Mon, 05 Nov 2018) $
38  * $Revision: 38934 $
39  *
40  *************************************************************************** */
41 
42 /* Define to prevent redundant inclusion */
43 #ifndef _DELAY_H_
44 #define _DELAY_H_
45 
46 /**
47  * @defgroup    MXC_delay Delay Utility Functions
48  * @ingroup     devicelibs
49  * @brief       Asynchronous delay routines based on the SysTick Timer
50  * @{
51  */
52 
53 /***** Definitions *****/
54 /**
55  * Macro used to specify a microsecond timing parameter in seconds.
56  * \code
57  * x = SEC(3) // 3 seconds -> x = 3,000,000
58  * \endcode
59  */
60 #define MXC_DELAY_SEC(s)            (((unsigned long)s) * 1000000UL)
61 /**
62  * Macro used to specify a microsecond timing parameter in milliseconds.
63  * \code
64  * x = MSEC(3) // 3ms -> x = 3,000
65  * \endcode
66  */
67 #define MXC_DELAY_MSEC(ms)          (ms * 1000UL)
68 /**
69  * Macro used to specify a microsecond timing parameter.
70  * \code
71  * x = USEC(3) // 3us -> x = 3
72  * \endcode
73  */
74 #define MXC_DELAY_USEC(us)          (us)
75 
76 /***** Function Prototypes *****/
77 
78 /**
79  * @brief      Blocks and delays for the specified number of microseconds.
80  * @details    Uses the SysTick to create the requested delay. If the SysTick is
81  *             running, the current settings will be used. If the SysTick is not
82  *             running, it will be started.
83  * @param      us    microseconds to delay
84  * @return     #E_NO_ERROR if no errors, @ref MXC_Error_Codes "error" if unsuccessful.
85  */
86 int mxc_delay(unsigned long us);
87 
88 /**
89  * @brief      Starts a non-blocking delay for the specified number of
90  *             microseconds.
91  * @details    Uses the SysTick to time the requested delay. If the SysTick is
92  *             running, the current settings will be used. If the SysTick is not
93  *             running, it will be started.
94  * @note       mxc_delay_handler() must be called from the SysTick interrupt service
95  *             routine or at a rate greater than the SysTick overflow rate.
96  * @param      us    microseconds to delay
97  * @return     #E_NO_ERROR if no errors, #E_BUSY if currently servicing another
98  *             delay request.
99  */
100 int mxc_delay_start(unsigned long us);
101 
102 /**
103  * @brief      Returns the status of a non-blocking delay request
104  * @pre        Start the asynchronous delay by calling mxc_delay_start().
105  * @return     #E_BUSY until the requested delay time has expired.
106  */
107 int mxc_delay_check(void);
108 
109 /**
110  * @brief      Stops an asynchronous delay previously started.
111  * @pre        Start the asynchronous delay by calling mxc_delay_start().
112  */
113 void mxc_delay_stop(void);
114 
115 /**
116  * @brief      Processes the delay interrupt.
117  * @details    This function must be called from the SysTick IRQ or polled at a
118  *             rate greater than the SysTick overflow rate.
119  */
120 void mxc_delay_handler(void);
121 
122 /**@} end of group MXC_delay */
123 
124 #endif /* _DELAY_H_ */
125