1 /** 2 ***************************************************************************************** 3 * 4 * @file app_timer.h 5 * 6 * @brief app timer API. 7 * 8 ***************************************************************************************** 9 * @attention 10 #####Copyright (c) 2019 GOODIX 11 All rights reserved. 12 13 Redistribution and use in source and binary forms, with or without 14 modification, are permitted provided that the following conditions are met: 15 * Redistributions of source code must retain the above copyright 16 notice, this list of conditions and the following disclaimer. 17 * Redistributions in binary form must reproduce the above copyright 18 notice, this list of conditions and the following disclaimer in the 19 documentation and/or other materials provided with the distribution. 20 * Neither the name of GOODIX nor the names of its contributors may be used 21 to endorse or promote products derived from this software without 22 specific prior written permission. 23 24 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 25 AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 26 IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 27 ARE DISCLAIMED. IN NO EVENT SHALL COPYRIGHT HOLDERS AND CONTRIBUTORS BE 28 LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 29 CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 30 SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 31 INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 32 CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 33 ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 34 POSSIBILITY OF SUCH DAMAGE. 35 ***************************************************************************************** 36 */ 37 38 #ifndef __APP_TIMER_H__ 39 #define __APP_TIMER_H__ 40 41 #include <stdint.h> 42 #include <stdbool.h> 43 #include "gr55xx_sys.h" 44 45 /** 46 * @defgroup APP_TIMER_MAROC Defines 47 * @{ 48 */ 49 #ifndef APP_TIMER_USE_SCHEDULER 50 #define APP_TIMER_USE_SCHEDULER 0 /**< Enable scheduling app_timer events to app_scheduler. */ 51 #endif 52 /** @} */ 53 54 /** 55 * @defgroup APP_TIMER_ENUM Enumerations 56 * @{ 57 */ 58 /**@brief App timer trigger types. */ 59 typedef enum { 60 ATIMER_ONE_SHOT = 0x0, /**< The timer will expire only once. */ 61 ATIMER_REPEAT /**< The timer will restart each time it expires. */ 62 } app_timer_type_t; 63 /** @} */ 64 65 /** 66 * @defgroup APP_TIMER_TYPEDEF Typedefs 67 * @{ 68 */ 69 /**@brief The timer node trigger function. */ 70 typedef void (*app_timer_fun_t)(uint8_t* p_ctx); 71 /** @} */ 72 73 /** 74 * @defgroup APP_TIMER_STRUCT Structures 75 * @{ 76 */ 77 /**@brief App timer global variable. */ 78 typedef struct { 79 uint8_t timer_node_used; /**< Timer node is used or not. */ 80 uint8_t timer_node_status; /**< Timer node status. */ 81 uint8_t next_trigger_mode; /**< Next trigger mode. */ 82 uint32_t original_delay; /**< Original delay (us). */ 83 uint32_t next_trigger_time; /**< Next trigger time. */ 84 uint8_t* next_trigger_callback_var; /**< Timer trigger callback argument. */ 85 app_timer_fun_t next_trigger_callback; /**< Timer trigger callback . */ 86 } app_timer_t; 87 88 #if APP_TIMER_USE_SCHEDULER 89 /**@brief Structure passed to app_scheduler. */ 90 typedef struct { 91 app_timer_fun_t timeout_handler; /**< Timer timeout handler. */ 92 void *p_ctx; /**< Pointer to callback argument. */ 93 } app_timer_evt_t; 94 #endif 95 /** @} */ 96 97 /** 98 * @defgroup APP_TIMER_TYPEDEF Typedefs 99 * @{ 100 */ 101 /**@brief The timer node id. */ 102 typedef app_timer_t* app_timer_id_t; 103 /** @} */ 104 105 /** 106 * @defgroup APP_TIMER_FUNCTION Functions 107 * @{ 108 */ 109 /** 110 ***************************************************************************************** 111 * @brief create a software timer. 112 * 113 * @param[in] p_timer_id: the id of timer node 114 * @param[in] mode: timer trigger mode. 115 * @param[in] callback: Pointer to timer expire callback function 116 * 117 * @return the error code of this funciton 118 119 * @note After the function is executed, a new soft timer is added to the list and waits 120 * for execution. At this point, the user can operate the app_timer_start/app_timer_stop 121 * function to pause and restore the timer. These two functions do not delete the 122 * timer node. If the user wants to delete a soft timer node completely, user can call 123 * the app_timer_remove function, in which the create/remove function needs to pass in 124 * the pointer, because these functions update the value of the pointer. 125 * @note It should be noted that the minimum delay time of this function is 10 ms. The purpose 126 * is to enhance the execution delay of soft timer and improve the real-time performance 127 * of soft timer. 128 * @note If the return value of this function is SDK_ERR_LIST_FULL, please find the macro 129 * TIMER_NODE_CNT in app_timer.h and modify the value. 130 ***************************************************************************************** 131 */ 132 sdk_err_t app_timer_create(app_timer_id_t *p_timer_id, app_timer_type_t mode, app_timer_fun_t callback); 133 134 /** 135 ***************************************************************************************** 136 * @brief Gets the current app timer's switching status 137 * @return state 0 means stop 1 means open 138 ***************************************************************************************** 139 */ 140 uint8_t app_timer_get_status(void); 141 142 /** 143 ***************************************************************************************** 144 * @brief To stop a existed timer in node list 145 * @param[in] p_timer_id: the id of timer node 146 ***************************************************************************************** 147 */ 148 void app_timer_stop(app_timer_id_t p_timer_id); 149 150 /** 151 ***************************************************************************************** 152 * @brief To start a existed timer in node list with old parameters 153 * @param[in] app_timer_id_t: the id of timer node 154 * @param[in] delay : the delay value of timer node, note this value should not 155 * exceed 4000 seconds. Unit (ms). 156 * @param[in] p_ctx : the pointer of context 157 ***************************************************************************************** 158 */ 159 sdk_err_t app_timer_start(app_timer_id_t p_timer_id, uint32_t delay, uint8_t *p_ctx); 160 161 /** 162 ***************************************************************************************** 163 * @brief Delete a timer node from list . 164 * @param[in] p_timer_id: the timer of id will be removed from timer list, and parameter 165 * will be set to NULL 166 ***************************************************************************************** 167 */ 168 sdk_err_t app_timer_delete(app_timer_id_t *p_timer_id); 169 170 /** 171 ***************************************************************************************** 172 * @brief Stop the currently running timer and return the running time 173 * 174 * @param[in] p_timer_handle: Pointer to the timer handle 175 * 176 * @return The time that the current timer has run 177 ***************************************************************************************** 178 */ 179 uint32_t app_timer_stop_and_ret(app_timer_id_t p_timer_id); 180 /** @} */ 181 182 #endif 183