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_MAX32650_MXC_DELAY_H_
28 #define LIBRARIES_PERIPHDRIVERS_INCLUDE_MAX32650_MXC_DELAY_H_
29 
30 #include <stdint.h>
31 #include "mxc_errors.h"
32 
33 #ifdef __cplusplus
34 extern "C" {
35 #endif
36 
37 /**
38  * @ingroup devicelibs
39  * @defgroup    MXC_delay Delay Utility Functions
40  * @brief       Asynchronous delay routines based on the SysTick Timer
41  * @{
42  */
43 
44 /***** Definitions *****/
45 /**
46  * Macro used to specify a microsecond timing parameter in seconds.
47  * \code
48  * x = SEC(3) // 3 seconds -> x = 3,000,000
49  * \endcode
50  */
51 #define MXC_DELAY_SEC(s) (((uint32_t)s) * 1000000UL)
52 /**
53  * Macro used to specify a microsecond timing parameter in milliseconds.
54  * \code
55  * x = MSEC(3) // 3ms -> x = 3,000
56  * \endcode
57  */
58 #define MXC_DELAY_MSEC(ms) (ms * 1000UL)
59 /**
60  * Macro used to specify a microsecond timing parameter.
61  * \code
62  * x = USEC(3) // 3us -> x = 3
63  * \endcode
64  */
65 #define MXC_DELAY_USEC(us) (us)
66 
67 /**
68  * @brief   The callback routine used by MXC_DelayAsync() when the delay is complete
69  *          or aborted early.
70  *
71  * @param   result      See \ref MXC_Error_Codes for the list of error codes.
72  */
73 typedef void (*mxc_delay_complete_t)(int result);
74 
75 /***** Function Prototypes *****/
76 
77 /**
78  * @brief      Blocks and delays for the specified number of microseconds.
79  * @details    Uses the SysTick to create the requested delay. If the SysTick is
80  *             running, the current settings will be used. If the SysTick is not
81  *             running, it will be started.
82  * @param      us    microseconds to delay
83  * @return     #E_NO_ERROR if no errors, @ref MXC_Error_Codes "error" if unsuccessful.
84  */
85 int MXC_Delay(uint32_t us);
86 
87 /**
88  * @brief      Starts a non-blocking delay for the specified number of
89  *             microseconds.
90  * @details    Uses the SysTick to time the requested delay. If the SysTick is
91  *             running, the current settings will be used. If the SysTick is not
92  *             running, it will be started.
93  * @note       MXC_Delay_handler() must be called from the SysTick interrupt service
94  *             routine or at a rate greater than the SysTick overflow rate.
95  * @param      us           microseconds to delay
96  * @param      callback     callback function to check for any error
97  * @return     #E_NO_ERROR if no errors, #E_BUSY if currently servicing another
98  *             delay request.
99  */
100 int MXC_DelayAsync(uint32_t us, mxc_delay_complete_t callback);
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_DelayCheck(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_DelayAbort(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_DelayHandler(void);
121 
122 /**@} end of group MXC_delay */
123 
124 #ifdef __cplusplus
125 }
126 #endif
127 
128 #endif // LIBRARIES_PERIPHDRIVERS_INCLUDE_MAX32650_MXC_DELAY_H_
129