rjw | 6c1fd8f | 2022-11-30 14:33:01 +0800 | [diff] [blame] | 1 | /***************************************************************************** |
| 2 | * Copyright Statement: |
| 3 | * -------------------- |
| 4 | * This software is protected by Copyright and the information contained |
| 5 | * herein is confidential. The software may not be copied and the information |
| 6 | * contained herein may not be used or disclosed except with the written |
| 7 | * permission of MediaTek Inc. (C) 2018 |
| 8 | * |
| 9 | * BY OPENING THIS FILE, BUYER HEREBY UNEQUIVOCALLY ACKNOWLEDGES AND AGREES |
| 10 | * THAT THE SOFTWARE/FIRMWARE AND ITS DOCUMENTATIONS ("MEDIATEK SOFTWARE") |
| 11 | * RECEIVED FROM MEDIATEK AND/OR ITS REPRESENTATIVES ARE PROVIDED TO BUYER ON |
| 12 | * AN "AS-IS" BASIS ONLY. MEDIATEK EXPRESSLY DISCLAIMS ANY AND ALL WARRANTIES, |
| 13 | * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE IMPLIED WARRANTIES OF |
| 14 | * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE OR NONINFRINGEMENT. |
| 15 | * NEITHER DOES MEDIATEK PROVIDE ANY WARRANTY WHATSOEVER WITH RESPECT TO THE |
| 16 | * SOFTWARE OF ANY THIRD PARTY WHICH MAY BE USED BY, INCORPORATED IN, OR |
| 17 | * SUPPLIED WITH THE MEDIATEK SOFTWARE, AND BUYER AGREES TO LOOK ONLY TO SUCH |
| 18 | * THIRD PARTY FOR ANY WARRANTY CLAIM RELATING THERETO. MEDIATEK SHALL ALSO |
| 19 | * NOT BE RESPONSIBLE FOR ANY MEDIATEK SOFTWARE RELEASES MADE TO BUYER'S |
| 20 | * SPECIFICATION OR TO CONFORM TO A PARTICULAR STANDARD OR OPEN FORUM. |
| 21 | * |
| 22 | * BUYER'S SOLE AND EXCLUSIVE REMEDY AND MEDIATEK'S ENTIRE AND CUMULATIVE |
| 23 | * LIABILITY WITH RESPECT TO THE MEDIATEK SOFTWARE RELEASED HEREUNDER WILL BE, |
| 24 | * AT MEDIATEK'S OPTION, TO REVISE OR REPLACE THE MEDIATEK SOFTWARE AT ISSUE, |
| 25 | * OR REFUND ANY SOFTWARE LICENSE FEES OR SERVICE CHARGE PAID BY BUYER TO |
| 26 | * MEDIATEK FOR SUCH MEDIATEK SOFTWARE AT ISSUE. |
| 27 | * |
| 28 | * THE TRANSACTION CONTEMPLATED HEREUNDER SHALL BE CONSTRUED IN ACCORDANCE |
| 29 | * WITH THE LAWS OF THE STATE OF CALIFORNIA, USA, EXCLUDING ITS CONFLICT OF |
| 30 | * LAWS PRINCIPLES. ANY DISPUTES, CONTROVERSIES OR CLAIMS ARISING THEREOF AND |
| 31 | * RELATED THERETO SHALL BE SETTLED BY ARBITRATION IN SAN FRANCISCO, CA, UNDER |
| 32 | * THE RULES OF THE INTERNATIONAL CHAMBER OF COMMERCE (ICC). |
| 33 | * |
| 34 | *****************************************************************************/ |
| 35 | |
| 36 | /***************************************************************************** |
| 37 | * |
| 38 | * Filename: |
| 39 | * --------- |
| 40 | * mer_service_timer.h |
| 41 | * |
| 42 | * Project: |
| 43 | * -------- |
| 44 | * MERTOS |
| 45 | * |
| 46 | * Description: |
| 47 | * ------------ |
| 48 | * Declare data structures and functions for timer. |
| 49 | * |
| 50 | * Author: |
| 51 | * ------- |
| 52 | * ------- |
| 53 | * |
| 54 | *****************************************************************************/ |
| 55 | |
| 56 | #ifndef _MER_SERVICE_TIMER_H_ |
| 57 | #define _MER_SERVICE_TIMER_H_ |
| 58 | |
| 59 | #include "mer_service_types.h" |
| 60 | #include "mer_service_task.h" |
| 61 | #include "mer_kernel_timer.h" |
| 62 | #include "mer_kernel_lock_target_mips.h" |
| 63 | |
| 64 | /////////////////////////////////////////////////////////////////////////////// |
| 65 | // enum |
| 66 | /////////////////////////////////////////////////////////////////////////////// |
| 67 | typedef enum { |
| 68 | MER_SERVICE_TIMER_ENABLE = 0, |
| 69 | MER_SERVICE_TIMER_DISABLE = 1 |
| 70 | } mer_service_timer_state; |
| 71 | |
| 72 | typedef enum{ |
| 73 | MER_SERVICE_TIMER_TYPE_CB = 0, |
| 74 | MER_SERVICE_TIMER_TYPE_SUSPEND = 1 |
| 75 | } mer_service_timer_type; |
| 76 | |
| 77 | #define MER_SERVICE_TIMER_MAX (0xFFFFFFFF) |
| 78 | // the following pattern is just for debug, and can be stripted in the future for better performance |
| 79 | #define MER_SERVICE_ALIGNED_TIMER (0xcbcb) |
| 80 | #define MER_SERVICE_UNALIGNED_TIMER (0xbcbc) |
| 81 | |
| 82 | /////////////////////////////////////////////////////////////////////////////// |
| 83 | // Structure |
| 84 | /////////////////////////////////////////////////////////////////////////////// |
| 85 | typedef struct _mer_service_timer |
| 86 | { |
| 87 | mer_kernel_timer_tick_info kernel_timer_cb; |
| 88 | mer_service_timer_type type; |
| 89 | mer_service_timer_state state; // for easy debugging |
| 90 | mer_uint32 expiration; |
| 91 | mer_char name[16]; // for easy debugging |
| 92 | void (*cb)(void *); |
| 93 | void *param; |
| 94 | mer_uint32 reschedule; |
| 95 | mer_uint32 delay; |
| 96 | mer_uint32 max_delay; |
| 97 | } mer_service_timer; |
| 98 | typedef mer_service_timer *mer_service_timer_id; |
| 99 | |
| 100 | /////////////////////////////////////////////////////////////////////////////// |
| 101 | /** |
| 102 | * Create an call back timer |
| 103 | * -1 will be returned if creation fails |
| 104 | * |
| 105 | * @param[out] MER_STATUS Success = 0, Fail = -1. |
| 106 | * @param[in] tid suspend timer control block address |
| 107 | * @param[in] name call back timer name, for debugging |
| 108 | * |
| 109 | */ |
| 110 | MER_STATUS mer_service_timer_create( |
| 111 | mer_service_timer_id tid, |
| 112 | const mer_char *name |
| 113 | ); |
| 114 | |
| 115 | /** |
| 116 | * start an call back timer with given parameters |
| 117 | * -1 will be returned if creation fails |
| 118 | * |
| 119 | * @param[out] MER_STATUS Success = 0, Fail = -1. |
| 120 | * @param[in] tid call back timer control block address |
| 121 | * @param[in] name call back timer name, for debugging |
| 122 | * |
| 123 | */ |
| 124 | MER_STATUS mer_service_timer_set( |
| 125 | mer_service_timer_id tid, |
| 126 | void (*cb)(void *), |
| 127 | void *param, |
| 128 | mer_uint32 init_delay, |
| 129 | mer_uint32 reschedule |
| 130 | ); |
| 131 | |
| 132 | /** |
| 133 | * let suspend suspend with timeout |
| 134 | * note that if it is used for getting resource, this function may early return |
| 135 | * upper layer users must check by themself if the return is due to timeout or |
| 136 | * the resource is seccessifully gotten by the suspend |
| 137 | * -1 will be returned if creation fails |
| 138 | * |
| 139 | * @param[out] MER_STATUS Success = 0, Fail = -1. |
| 140 | * @param[in] task_id timer control block address |
| 141 | * @param[in] timeout timeout of the suspend |
| 142 | * @param[in] state reason of calling this function |
| 143 | * @param[in] lock_to_release the lock to release if any |
| 144 | * |
| 145 | */ |
| 146 | MER_STATUS mer_service_timer_suspend_task_with_timeout( |
| 147 | mer_service_task_id task_id, |
| 148 | mer_uint32 timeout, |
| 149 | mer_service_task_state state, |
| 150 | mer_kernel_lock_id *lock_to_release |
| 151 | ); |
| 152 | |
| 153 | |
| 154 | /** |
| 155 | * cancel a timer |
| 156 | * -1 will be returned if creation fails |
| 157 | * |
| 158 | * @param[out] MER_STATUS Success = 0, Fail = -1. |
| 159 | * @param[in] tid timer control block address |
| 160 | * |
| 161 | */ |
| 162 | MER_STATUS mer_service_timer_stop(mer_service_timer_id); |
| 163 | |
| 164 | /** |
| 165 | * set the max delay of a timer, only valid for call back timer |
| 166 | * note that this function will not start a timer |
| 167 | * -1 will be returned if creation fails |
| 168 | * |
| 169 | * @param[out] MER_STATUS Success = 0, Fail = -1. |
| 170 | * @param[in] tid timer control block address |
| 171 | * |
| 172 | */ |
| 173 | MER_STATUS mer_service_timer_set_max_delay(mer_service_timer_id tid, mer_uint16 delay); |
| 174 | |
| 175 | /** |
| 176 | * cancel the max delay of a timer |
| 177 | * note that this function will not start a timer |
| 178 | * -1 will be returned if creation fails |
| 179 | * |
| 180 | * @param[out] MER_STATUS Success = 0, Fail = -1. |
| 181 | * @param[in] tid timer control block address |
| 182 | * @param[in] dealy maximum allowable delay for this timer |
| 183 | * |
| 184 | */ |
| 185 | MER_STATUS mer_service_timer_cancel_max_delay(mer_service_timer_id tid); |
| 186 | |
| 187 | /** |
| 188 | * set the max delay for task suspend timer, |
| 189 | * note that this function will not start a timer |
| 190 | * -1 will be returned if creation fails |
| 191 | * |
| 192 | * @param[out] MER_STATUS Success = 0, Fail = -1. |
| 193 | * @param[in] tid timer control block address |
| 194 | * |
| 195 | */ |
| 196 | |
| 197 | MER_STATUS mer_service_timer_set_suspend_max_delay(mer_uint16 delay); |
| 198 | /** |
| 199 | * cancel the max delay for task suspend timer, |
| 200 | * note that this function will not start a timer |
| 201 | * -1 will be returned if creation fails |
| 202 | * |
| 203 | * @param[out] MER_STATUS Success = 0, Fail = -1. |
| 204 | * @param[in] task_id task to set |
| 205 | * @param[in] dealy maximum allowable delay for this timer |
| 206 | * |
| 207 | */ |
| 208 | MER_STATUS mer_service_timer_cancel_suspend_max_delay(); |
| 209 | |
| 210 | /** |
| 211 | * get the remaining time of the timer |
| 212 | * if the timer is not running or expired, it will return 0 |
| 213 | * |
| 214 | * @param[out] MER_UINT32 remaining time, 0 if the timer is not running or expired |
| 215 | * @param[in] task_id task to cancel |
| 216 | * |
| 217 | */ |
| 218 | |
| 219 | mer_uint32 mer_service_timer_get_remaining_time(mer_service_timer_id tid); |
| 220 | |
| 221 | /** |
| 222 | * handle the timers in dpc |
| 223 | * -1 will be returned if creation fails |
| 224 | * |
| 225 | * @param[out] MER_STATUS Success = 0, Fail = -1. |
| 226 | * |
| 227 | */ |
| 228 | MER_STATUS mer_service_timer_expiration(); |
| 229 | |
| 230 | static inline mer_bool mer_service_timer_is_expired(mer_service_timer_id tid) |
| 231 | { |
| 232 | mer_uint32 now = mer_kernel_timer_get_current_tick(); |
| 233 | mer_uint32 start_time = tid->expiration - tid->delay; |
| 234 | mer_bool is_wrap = (tid->expiration < start_time); |
| 235 | |
| 236 | if((!is_wrap && !(now >= start_time && now < tid->expiration)) || |
| 237 | (is_wrap && now >= tid->expiration && now < start_time)) { |
| 238 | return MER_TRUE; |
| 239 | } |
| 240 | else { |
| 241 | return MER_FALSE; |
| 242 | } |
| 243 | } |
| 244 | |
| 245 | /*#define MER_SERVICE_TIMER_DEBUG*/ |
| 246 | |
| 247 | // we do not have many tasks, 256 should be enough |
| 248 | #ifdef MER_SERVICE_TIMER_DEBUG |
| 249 | |
| 250 | #define MER_SERVICE_TIMER_GLOBAL_COUNT (256) |
| 251 | #define MER_SERVICE_TIMER_GLOBAL_GUARD_PATTERN (0x5566dead) |
| 252 | /*global timer*/ |
| 253 | /*global timer*/ |
| 254 | /*global timer*/ |
| 255 | typedef struct _mer_service_timer_global |
| 256 | { |
| 257 | mer_uint32 guard_front; |
| 258 | mer_service_task_id owner; |
| 259 | mer_uint32 working; |
| 260 | mer_service_timer timer; |
| 261 | mer_uint32 guard_back; |
| 262 | } mer_service_timer_global; |
| 263 | |
| 264 | typedef mer_service_timer_global *mer_service_timer_global_id; |
| 265 | |
| 266 | mer_service_timer_id get_mer_service_timer_cb_global(mer_service_task_id task_id); |
| 267 | void check_mer_service_timer_cb_global(mer_service_timer_id tid); |
| 268 | void release_mer_service_timer_cb_global(mer_service_timer_id tid); |
| 269 | |
| 270 | typedef enum { |
| 271 | MER_SERVICE_TIMER_TRACE_OP_SET = 0, |
| 272 | MER_SERVICE_TIMER_TRACE_OP_STOP = 1, |
| 273 | MER_SERVICE_TIMER_TRACE_OP_SUSPEND = 2, |
| 274 | } mer_service_timer_trace_op; |
| 275 | |
| 276 | typedef struct _mer_service_timer_trace { |
| 277 | mer_service_timer_id tid; |
| 278 | kal_uint32 now; |
| 279 | kal_uint32 expiration; |
| 280 | kal_uint32 reschedule; |
| 281 | kal_uint32 max_delay; |
| 282 | kal_uint32 lr; |
| 283 | mer_service_timer_trace_op op; |
| 284 | } mer_service_timer_trace; |
| 285 | |
| 286 | void mer_service_timer_set_trace(mer_service_timer_id tid, |
| 287 | kal_uint32 delay, |
| 288 | kal_uint32 reschedule, |
| 289 | kal_uint32 lr, mer_service_timer_trace_op op); |
| 290 | |
| 291 | #endif /* MER_SERVICE_TIMER_DEBUG */ |
| 292 | |
| 293 | #endif /* _MER_SERVICE_TIMER_H */ |
| 294 | |