1 /* 2 * Copyright (c) 2022 Huawei Device Co., Ltd. 3 * 4 * HDF is dual licensed: you can use it either under the terms of 5 * the GPL, or the BSD license, at your option. 6 * See the LICENSE file in the root of this repository for complete details. 7 */ 8 9 10 #ifndef TIMER_IF_H 11 #define TIMER_IF_H 12 13 #include "platform_if.h" 14 15 #ifdef __cplusplus 16 #if __cplusplus 17 extern "C" { 18 #endif 19 #endif /* __cplusplus */ 20 21 /** 22 * @brief Defines a callback that will be invoked when a timer's interrupt involved. 23 */ 24 typedef int32_t (*TimerHandleCb)(void); 25 26 /** 27 * @brief Gets a hardware timer. 28 * This function must be called to get its device handle before operating the timer. 29 * @param number Indicates a timer id. 30 * @return If the operation is successful, a pointer to the timer device handle is returned. 31 * @since 1.0 32 */ 33 DevHandle HwTimerOpen(const uint32_t number); 34 35 /** 36 * @brief Close a hardware timer. 37 * If you no longer need the timer, call this function to close it 38 * @param handle Represents a pointer to the timer device handle. 39 * @since 1.0 40 */ 41 void HwTimerClose(DevHandle handle); 42 43 /** 44 * @brief Start a timer. 45 * If you need the timer run, call this function to start it 46 * @param handle Represents a pointer to the timer device handle. 47 * @return success or fail 48 * @since 1.0 49 */ 50 int32_t HwTimerStart(DevHandle handle); 51 52 /** 53 * @brief Stop a timer. 54 * If you no longer need the timer run, call this function to stop it 55 * @param handle Represents a pointer to the timer device handle. 56 * @return success or fail 57 * @since 1.0 58 */ 59 int32_t HwTimerStop(DevHandle handle); 60 61 /** 62 * @brief Set a period timer. 63 * If you need the timer run, call this function to set timer info 64 * @param handle Represents a pointer to the timer device handle. 65 * @param useconds Represents the timer interval. 66 * @param cb Represents the timer callback function. 67 * @return success or fail 68 * @since 1.0 69 */ 70 int32_t HwTimerSet(DevHandle handle, uint32_t useconds, TimerHandleCb cb); 71 72 /** 73 * @brief Set a oneshot timer. 74 * If you need the timer run, call this function to set timer info 75 * @param useconds Represents the timer interval. 76 * @param cb Represents the timer callback function. 77 * @return success or fail 78 * @since 1.0 79 */ 80 int32_t HwTimerSetOnce(DevHandle handle, uint32_t useconds, TimerHandleCb cb); 81 82 /** 83 * @brief Get the timer info. 84 * If you need the timer info, call this function get 85 * @param handle Represents a pointer to the timer device handle. 86 * @param useconds Represents the timer interval. 87 * @param isPeriod Represents whether the timer call once 88 * @return success or fail 89 * @since 1.0 90 */ 91 int32_t HwTimerGet(DevHandle handle, uint32_t *useconds, bool *isPeriod); 92 93 /** 94 * @brief Enumerates TIMER I/O commands. 95 * 96 * @since 1.0 97 */ 98 enum TimerIoCmd { 99 TIMER_IO_OPEN = 0, /**< Open the TIMER device. */ 100 TIMER_IO_CLOSE, /**< Close the TIMER device. */ 101 TIMER_IO_START, /**< Start the TIMER. */ 102 TIMER_IO_STOP, /**< Stop the TIMER. */ 103 TIMER_IO_SET, /**< Set the period TIMER info. */ 104 TIMER_IO_SETONCE, /**< Set the once TIMER info. */ 105 TIMER_IO_GET, /**< Get the TIMER info. */ 106 }; 107 108 struct TimerConfig { 109 uint32_t number; 110 uint32_t useconds; 111 bool isPeriod; 112 }; 113 114 #ifdef __cplusplus 115 #if __cplusplus 116 } 117 #endif 118 #endif /* __cplusplus */ 119 120 #endif /* TIMER_IF_H */ 121 /** @} */ 122