1 /* 2 * Copyright (c) 2022 FuZhou Lockzhiner Electronic Co., Ltd. All rights reserved. 3 * Licensed under the Apache License, Version 2.0 (the "License"); 4 * you may not use this file except in compliance with the License. 5 * You may obtain a copy of the License at 6 * 7 * http://www.apache.org/licenses/LICENSE-2.0 8 * 9 * Unless required by applicable law or agreed to in writing, software 10 * distributed under the License is distributed on an "AS IS" BASIS, 11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 * See the License for the specific language governing permissions and 13 * limitations under the License. 14 */ 15 16 /** 17 * @addtogroup Lockzhiner 18 * 19 * @file wait.h 20 * 21 */ 22 23 #ifndef LZ_HARDWARE_WAIT_H 24 #define LZ_HARDWARE_WAIT_H 25 26 /** 27 * @brief sleep until a condition gets true or a timeout elapses. 28 * 29 * 30 * 31 * @param condition Indicates a C expression for the event to wait for. 32 * @param timeout Indicates timeout, in ticks. 33 * @return Returns: 34 * 0 if the @condition evaluated to %false after the @timeout elapsed, 35 * 1 if the @condition evaluated to %true after the @timeout elapsed, 36 * or the remaining jiffies (at least 1) if the @condition evaluated 37 * to %true before the @timeout elapsed. 38 */ 39 #define WaitEventTimeout(condition, timeout) \ 40 ( { \ 41 int __ret = timeout; \ 42 while (--__ret && !(condition)) { \ 43 ToyMsleep(1); \ 44 } \ 45 if ((__ret == 0) && (condition)) \ 46 __ret = 1; \ 47 __ret; \ 48 }) 49 50 /** 51 * @brief sleep until a condition gets true. 52 * 53 * 54 * 55 * @param condition Indicates a C expression for the event to wait for. 56 * @return Returns: 57 */ 58 #define WaitEvent(condition) \ 59 ( { \ 60 while (!(condition)) { \ 61 ToyMsleep(1); \ 62 } \ 63 }) 64 65 #endif