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_MAX78002_MXC_DELAY_H_
28 #define LIBRARIES_PERIPHDRIVERS_INCLUDE_MAX78002_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 #ifdef __riscv
75 
76 /**
77  * @brief      Blocks and delays for the specified number of microseconds.
78  * @details    Uses the Performance Counter to create the requested delay. The current
79  *             and settings of the performance counter registers will be destroyed.
80  * @param      us    microseconds to delay
81  * @return     #E_NO_ERROR if no errors, @ref MXC_Error_Codes "error" if unsuccessful.
82  */
83 int MXC_Delay(uint32_t us);
84 
85 // AI87-TODO: Merged from Github, need to add comments.
86 int MXC_DelayAsync(uint32_t us, mxc_delay_complete_t callback);
87 int MXC_DelayCheck(void);
88 void MXC_DelayAbort(void);
89 
90 #else
91 
92 /***** Function Prototypes *****/
93 
94 /**
95  * @brief      Blocks and delays for the specified number of microseconds.
96  * @details    Uses the SysTick to create the requested delay. If the SysTick is
97  *             running, the current settings will be used. If the SysTick is not
98  *             running, it will be started.
99  * @param      us    microseconds to delay
100  * @return     #E_NO_ERROR if no errors, @ref MXC_Error_Codes "error" if unsuccessful.
101  */
102 int MXC_Delay(uint32_t us);
103 
104 /**
105  * @brief      Starts a non-blocking delay for the specified number of
106  *             microseconds.
107  * @details    Uses the SysTick to time the requested delay. If the SysTick is
108  *             running, the current settings will be used. If the SysTick is not
109  *             running, it will be started.
110  * @note       MXC_Delay_handler() must be called from the SysTick interrupt service
111  *             routine or at a rate greater than the SysTick overflow rate.
112  * @param      us    microseconds to delay
113  * @param      callback Function pointer to the function to call after the delay has expired.
114  * @return     #E_NO_ERROR if no errors, #E_BUSY if currently servicing another
115  *             delay request.
116  */
117 int MXC_DelayAsync(uint32_t us, mxc_delay_complete_t callback);
118 
119 /**
120  * @brief      Returns the status of a non-blocking delay request
121  * @pre        Start the asynchronous delay by calling MXC_Delay_start().
122  * @return     #E_BUSY until the requested delay time has expired.
123  */
124 int MXC_DelayCheck(void);
125 
126 /**
127  * @brief      Stops an asynchronous delay previously started.
128  * @pre        Start the asynchronous delay by calling MXC_Delay_start().
129  */
130 void MXC_DelayAbort(void);
131 
132 /**
133  * @brief      Processes the delay interrupt.
134  * @details    This function must be called from the SysTick IRQ or polled at a
135  *             rate greater than the SysTick overflow rate.
136  */
137 void MXC_DelayHandler(void);
138 
139 /**@} end of group MXC_delay */
140 
141 #endif /* __riscv */
142 
143 #ifdef __cplusplus
144 }
145 #endif
146 
147 #endif // LIBRARIES_PERIPHDRIVERS_INCLUDE_MAX78002_MXC_DELAY_H_
148