1 /**
2  * @file
3  * @brief    Asynchronous delay routines based on the SysTick Timer.
4 */
5 
6 /******************************************************************************
7  *
8  * Copyright (C) 2022-2023 Maxim Integrated Products, Inc. (now owned by
9  * Analog Devices, Inc.),
10  * Copyright (C) 2023-2024 Analog Devices, Inc.
11  *
12  * Licensed under the Apache License, Version 2.0 (the "License");
13  * you may not use this file except in compliance with the License.
14  * You may obtain a copy of the License at
15  *
16  *     http://www.apache.org/licenses/LICENSE-2.0
17  *
18  * Unless required by applicable law or agreed to in writing, software
19  * distributed under the License is distributed on an "AS IS" BASIS,
20  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
21  * See the License for the specific language governing permissions and
22  * limitations under the License.
23  *
24  ******************************************************************************/
25 
26 /* Define to prevent redundant inclusion */
27 #ifndef LIBRARIES_PERIPHDRIVERS_INCLUDE_MAX32662_MXC_DELAY_H_
28 #define LIBRARIES_PERIPHDRIVERS_INCLUDE_MAX32662_MXC_DELAY_H_
29 
30 #include <stdint.h>
31 
32 #ifdef __cplusplus
33 extern "C" {
34 #endif
35 
36 /**
37  * @ingroup devicelibs
38  * @defgroup    MXC_delay Delay Utility Functions
39  * @brief       Asynchronous delay routines based on the SysTick Timer
40  * @{
41  */
42 
43 /***** Definitions *****/
44 /**
45  * Macro used to specify a microsecond timing parameter in seconds.
46  * \code
47  * x = SEC(3) // 3 seconds -> x = 3,000,000
48  * \endcode
49  */
50 #define MXC_DELAY_SEC(s) (((uint32_t)s) * 1000000UL)
51 /**
52  * Macro used to specify a microsecond timing parameter in milliseconds.
53  * \code
54  * x = MSEC(3) // 3ms -> x = 3,000
55  * \endcode
56  */
57 #define MXC_DELAY_MSEC(ms) (ms * 1000UL)
58 /**
59  * Macro used to specify a microsecond timing parameter.
60  * \code
61  * x = USEC(3) // 3us -> x = 3
62  * \endcode
63  */
64 #define MXC_DELAY_USEC(us) (us)
65 
66 /**
67  * @brief   The callback routine used by MXC_DelayAsync() when the delay is complete
68  *          or aborted early.
69  *
70  * @param   result      See \ref MXC_Error_Codes for the list of error codes.
71  */
72 typedef void (*mxc_delay_complete_t)(int result);
73 
74 /***** Function Prototypes *****/
75 
76 /**
77  * @brief      Blocks and delays for the specified number of microseconds.
78  * @details    Uses the SysTick to create the requested delay. If the SysTick is
79  *             running, the current settings will be used. If the SysTick is not
80  *             running, it will be started.
81  * @param      us    microseconds to delay
82  * @return     #E_NO_ERROR if no errors, @ref MXC_Error_Codes "error" if unsuccessful.
83  */
84 int MXC_Delay(uint32_t us);
85 
86 /**
87  * @brief      Starts a non-blocking delay for the specified number of
88  *             microseconds.
89  * @details    Uses the SysTick to time the requested delay. If the SysTick is
90  *             running, the current settings will be used. If the SysTick is not
91  *             running, it will be started.
92  * @note       MXC_Delay_handler() must be called from the SysTick interrupt service
93  *             routine or at a rate greater than the SysTick overflow rate.
94  * @param      us    microseconds to delay
95  * @param      callback   Function pointer to the function to call after the delay has expired.
96  * @return     #E_NO_ERROR if no errors, #E_BUSY if currently servicing another
97  *             delay request.
98  */
99 int MXC_DelayAsync(uint32_t us, mxc_delay_complete_t callback);
100 
101 /**
102  * @brief      Returns the status of a non-blocking delay request
103  * @pre        Start the asynchronous delay by calling MXC_Delay_start().
104  * @return     #E_BUSY until the requested delay time has expired.
105  */
106 int MXC_DelayCheck(void);
107 
108 /**
109  * @brief      Stops an asynchronous delay previously started.
110  * @pre        Start the asynchronous delay by calling MXC_Delay_start().
111  */
112 void MXC_DelayAbort(void);
113 
114 /**
115  * @brief      Processes the delay interrupt.
116  * @details    This function must be called from the SysTick IRQ or polled at a
117  *             rate greater than the SysTick overflow rate.
118  */
119 void MXC_DelayHandler(void);
120 
121 /**@} end of group MXC_delay */
122 
123 #ifdef __cplusplus
124 }
125 #endif
126 
127 #endif // LIBRARIES_PERIPHDRIVERS_INCLUDE_MAX32662_MXC_DELAY_H_
128