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