[2503] | 1 | /** @file wlan_mac_schedule.c |
---|
| 2 | * @brief Scheduler |
---|
| 3 | * |
---|
| 4 | * This set of functions allows upper-level MAC implementations |
---|
| 5 | * to schedule the execution of a provided callback for some point |
---|
| 6 | * in the future. |
---|
| 7 | * |
---|
[5689] | 8 | * @copyright Copyright 2014-2017, Mango Communications. All rights reserved. |
---|
[2503] | 9 | * Distributed under the Mango Communications Reference Design License |
---|
[5402] | 10 | * See LICENSE.txt included in the design archive or |
---|
| 11 | * at http://mangocomm.com/802.11/license |
---|
[2503] | 12 | * |
---|
[5525] | 13 | * This file is part of the Mango 802.11 Reference Design (https://mangocomm.com/802.11) |
---|
[2503] | 14 | */ |
---|
[2427] | 15 | |
---|
| 16 | /***************************** Include Files *********************************/ |
---|
[5525] | 17 | #include "wlan_mac_high_sw_config.h" |
---|
| 18 | |
---|
[5103] | 19 | #include "xil_types.h" |
---|
| 20 | #include "xil_exception.h" |
---|
| 21 | |
---|
[2427] | 22 | #include "xparameters.h" |
---|
| 23 | #include "xtmrctr.h" |
---|
| 24 | #include "xintc.h" |
---|
| 25 | |
---|
[5153] | 26 | #include "wlan_mac_time_util.h" |
---|
[5103] | 27 | #include "wlan_mac_high.h" |
---|
| 28 | #include "wlan_mac_dl_list.h" |
---|
| 29 | #include "wlan_mac_schedule.h" |
---|
| 30 | |
---|
| 31 | |
---|
| 32 | /*************************** Constant Definitions *****************************/ |
---|
| 33 | |
---|
| 34 | #define WLAN_SCHED_EXEC_MONITOR 0 |
---|
| 35 | #define WLAN_SCHED_EXEC_MONITOR_DEFAULT_THRESHOLD 100000 // 100 ms |
---|
| 36 | |
---|
| 37 | |
---|
[2427] | 38 | /*************************** Variable Definitions ****************************/ |
---|
[5103] | 39 | static XTmrCtr timer_instance; |
---|
[2427] | 40 | |
---|
[4012] | 41 | volatile static u32 schedule_count; |
---|
[3020] | 42 | |
---|
[4012] | 43 | |
---|
[5240] | 44 | static wlan_sched_state_t wlan_sched_coarse; |
---|
| 45 | static wlan_sched_state_t wlan_sched_fine; |
---|
[5514] | 46 | static dl_list disabled_list; |
---|
[5240] | 47 | |
---|
[5103] | 48 | #if WLAN_SCHED_EXEC_MONITOR |
---|
| 49 | static u64 last_exec_timestamp; |
---|
| 50 | static u64 schedule_exec_monitor; |
---|
| 51 | static u32 monitor_threshold; |
---|
| 52 | #endif |
---|
| 53 | |
---|
| 54 | |
---|
| 55 | /*************************** Function Prototypes *****************************/ |
---|
| 56 | |
---|
| 57 | void timer_interrupt_handler(void * instancePtr); |
---|
| 58 | void schedule_handler(void * callback_ref, u8 timer_number); |
---|
| 59 | |
---|
| 60 | |
---|
[2427] | 61 | /******************************** Functions **********************************/ |
---|
| 62 | |
---|
[2435] | 63 | /*****************************************************************************/ |
---|
| 64 | /** |
---|
[5103] | 65 | * Initializes the schedulers. |
---|
| 66 | * |
---|
| 67 | * @param None |
---|
| 68 | * |
---|
| 69 | * @return None |
---|
| 70 | * |
---|
| 71 | *****************************************************************************/ |
---|
[2427] | 72 | int wlan_mac_schedule_init(){ |
---|
[5103] | 73 | int status; |
---|
[2427] | 74 | |
---|
[5103] | 75 | // Initialize internal variables |
---|
[5515] | 76 | schedule_count = 1; |
---|
[5103] | 77 | |
---|
| 78 | #if WLAN_SCHED_EXEC_MONITOR |
---|
| 79 | last_exec_timestamp = 0; |
---|
| 80 | schedule_exec_monitor = 0; |
---|
| 81 | monitor_threshold = WLAN_SCHED_EXEC_MONITOR_DEFAULT_THRESHOLD; |
---|
| 82 | #endif |
---|
| 83 | |
---|
[5514] | 84 | dl_list_init(&(wlan_sched_coarse.enabled_list)); |
---|
[5240] | 85 | wlan_sched_coarse.next = NULL; |
---|
[5514] | 86 | dl_list_init(&(wlan_sched_fine.enabled_list)); |
---|
[5240] | 87 | wlan_sched_fine.next = NULL; |
---|
[2427] | 88 | |
---|
[5514] | 89 | dl_list_init(&disabled_list); |
---|
| 90 | |
---|
[5103] | 91 | // Initialize the timer |
---|
[5422] | 92 | // The driver for the timer does not handle a reboot of CPU_HIGH |
---|
| 93 | // gracefully. Before initializing it, we should stop any running |
---|
| 94 | // timers. Note: we cannot use the official XTmrCtr_Stop() function |
---|
| 95 | // for this since it requires that the timer instance be initialized. |
---|
| 96 | u32 ControlStatusReg; |
---|
| 97 | u8 i; |
---|
| 98 | XTmrCtr_Config *TmrCtrConfigPtr = XTmrCtr_LookupConfig(TMRCTR_DEVICE_ID); |
---|
[2427] | 99 | |
---|
[5422] | 100 | if(TmrCtrConfigPtr != NULL){ |
---|
| 101 | for(i=0; i<2; i++){ |
---|
| 102 | //Loop over timer instances |
---|
| 103 | /* |
---|
| 104 | * Read the current register contents |
---|
| 105 | */ |
---|
| 106 | ControlStatusReg = XTmrCtr_ReadReg(TmrCtrConfigPtr->BaseAddress, |
---|
| 107 | i, XTC_TCSR_OFFSET); |
---|
[3563] | 108 | |
---|
[5422] | 109 | /* |
---|
| 110 | * Disable the timer counter such that it's not running |
---|
| 111 | */ |
---|
| 112 | ControlStatusReg &= ~(XTC_CSR_ENABLE_TMR_MASK); |
---|
[2427] | 113 | |
---|
[5422] | 114 | /* |
---|
| 115 | * Write out the updated value to the actual register. |
---|
| 116 | */ |
---|
| 117 | XTmrCtr_WriteReg(TmrCtrConfigPtr->BaseAddress, i, |
---|
| 118 | XTC_TCSR_OFFSET, ControlStatusReg); |
---|
| 119 | |
---|
| 120 | } |
---|
| 121 | |
---|
| 122 | |
---|
| 123 | |
---|
| 124 | status = XTmrCtr_Initialize(&timer_instance, TMRCTR_DEVICE_ID); |
---|
| 125 | if ( (status != XST_SUCCESS) ) { |
---|
| 126 | xil_printf("ERROR: XTmrCtr failed to initialize\n"); |
---|
| 127 | return -1; |
---|
| 128 | } |
---|
| 129 | |
---|
| 130 | // Set the handler for Timer |
---|
| 131 | XTmrCtr_SetHandler(&timer_instance, schedule_handler, &timer_instance); |
---|
| 132 | |
---|
| 133 | // Enable interrupt of timer and auto-reload so it continues repeatedly |
---|
| 134 | XTmrCtr_SetOptions(&timer_instance, TIMER_CNTR_FAST, XTC_DOWN_COUNT_OPTION | XTC_INT_MODE_OPTION | XTC_AUTO_RELOAD_OPTION); |
---|
| 135 | XTmrCtr_SetOptions(&timer_instance, TIMER_CNTR_SLOW, XTC_DOWN_COUNT_OPTION | XTC_INT_MODE_OPTION | XTC_AUTO_RELOAD_OPTION); |
---|
| 136 | |
---|
| 137 | return 0; |
---|
| 138 | } else { |
---|
| 139 | //Could not find XTmrCtr_Config for the timer |
---|
| 140 | return -1; |
---|
| 141 | } |
---|
[2427] | 142 | } |
---|
| 143 | |
---|
[5103] | 144 | |
---|
| 145 | |
---|
[2435] | 146 | /*****************************************************************************/ |
---|
| 147 | /** |
---|
[5103] | 148 | * Set up scheduler interrupt |
---|
| 149 | * |
---|
| 150 | * @param intc - pointer to instance of interrupt controller |
---|
| 151 | * |
---|
| 152 | * @return status - success or failure of attempt to connect |
---|
| 153 | * |
---|
| 154 | *****************************************************************************/ |
---|
[2427] | 155 | int wlan_mac_schedule_setup_interrupt(XIntc* intc){ |
---|
[2459] | 156 | int status; |
---|
| 157 | |
---|
[5103] | 158 | status = XIntc_Connect(intc, TMRCTR_INTERRUPT_ID, (XInterruptHandler)timer_interrupt_handler, &timer_instance); |
---|
| 159 | XIntc_Enable(intc, TMRCTR_INTERRUPT_ID); |
---|
| 160 | |
---|
[2459] | 161 | return status; |
---|
[2427] | 162 | } |
---|
| 163 | |
---|
[5103] | 164 | |
---|
| 165 | |
---|
[2435] | 166 | /*****************************************************************************/ |
---|
| 167 | /** |
---|
[5103] | 168 | * Schedules the execution of a callback for some time in the future |
---|
| 169 | * |
---|
| 170 | * @param scheduler_sel - SCHEDULE_COARSE or SCHEDULE_FINE |
---|
| 171 | * @param delay - Interval (in microseconds) until callback should be called |
---|
| 172 | * @param num_calls - Number of repetitions or SCHEDULE_REPEAT_FOREVER for permanent periodic |
---|
| 173 | * @param callback - Function pointer to callback |
---|
| 174 | * |
---|
| 175 | * @return id - ID of scheduled event or SCHEDULE_FAILURE if error |
---|
| 176 | * |
---|
| 177 | *****************************************************************************/ |
---|
[2450] | 178 | u32 wlan_mac_schedule_event_repeated(u8 scheduler_sel, u32 delay, u32 num_calls, void(*callback)()){ |
---|
[5103] | 179 | u32 id; |
---|
| 180 | dl_entry * entry_ptr; |
---|
| 181 | wlan_sched * sched_ptr; |
---|
| 182 | u64 curr_system_time; |
---|
[2427] | 183 | |
---|
[5103] | 184 | // Allocate memory for data structures |
---|
| 185 | entry_ptr = wlan_mac_high_malloc(sizeof(dl_entry)); |
---|
| 186 | if (entry_ptr == NULL) { return SCHEDULE_FAILURE; } |
---|
[3034] | 187 | |
---|
[5103] | 188 | sched_ptr = wlan_mac_high_malloc(sizeof(wlan_sched)); |
---|
| 189 | if (sched_ptr == NULL) { wlan_mac_high_free(entry_ptr); return SCHEDULE_FAILURE; } |
---|
[4972] | 190 | |
---|
[5103] | 191 | // Attach the schedule struct to this dl_entry |
---|
[3034] | 192 | entry_ptr->data = sched_ptr; |
---|
| 193 | |
---|
[5515] | 194 | do{ |
---|
| 195 | // Get Schedule ID from global counter |
---|
| 196 | id = (schedule_count++); |
---|
[3888] | 197 | |
---|
[5515] | 198 | // Check if we hit the section of reserved IDs; Wrap back to 0 and start again |
---|
| 199 | if ((id >= SCHEDULE_ID_RESERVED_MIN) && (id <= SCHEDULE_ID_RESERVED_MAX)) { id = 1; } |
---|
[3888] | 200 | |
---|
[5515] | 201 | // Check to make sure that this id has not been issued to any currently |
---|
| 202 | // scheduled events |
---|
| 203 | } while( (wlan_mac_schedule_find(&wlan_sched_coarse.enabled_list, id) != NULL) || |
---|
| 204 | (wlan_mac_schedule_find(&wlan_sched_fine.enabled_list, id) != NULL) || |
---|
| 205 | (wlan_mac_schedule_find(&disabled_list, id) != NULL)); |
---|
| 206 | |
---|
[5103] | 207 | // Get the current system time |
---|
| 208 | curr_system_time = get_system_time_usec(); |
---|
| 209 | |
---|
| 210 | // Initialize the schedule struct |
---|
[5514] | 211 | sched_ptr->enabled = 1; |
---|
[3888] | 212 | sched_ptr->id = id; |
---|
[5103] | 213 | sched_ptr->delay_us = delay; |
---|
[2450] | 214 | sched_ptr->num_calls = num_calls; |
---|
[5103] | 215 | sched_ptr->target_us = curr_system_time + (u64)(delay); |
---|
[3888] | 216 | sched_ptr->callback = (function_ptr_t)callback; |
---|
[2427] | 217 | |
---|
[5103] | 218 | // Add schedule struct to the appropriate schedule list |
---|
[2427] | 219 | switch(scheduler_sel){ |
---|
[5103] | 220 | // ------------------------------------------------ |
---|
[2427] | 221 | case SCHEDULE_COARSE: |
---|
[5103] | 222 | // Start timer if the list goes from 0 -> 1 event |
---|
[5514] | 223 | if (wlan_sched_coarse.enabled_list.length == 0) { |
---|
[5103] | 224 | XTmrCtr_SetResetValue(&timer_instance, TIMER_CNTR_SLOW, (SLOW_TIMER_DUR_US * TIMER_CLKS_PER_US)); |
---|
| 225 | XTmrCtr_Start(&timer_instance, TIMER_CNTR_SLOW); |
---|
| 226 | } |
---|
[3020] | 227 | |
---|
[5514] | 228 | dl_entry_insertBeginning(&(wlan_sched_coarse.enabled_list), entry_ptr); |
---|
[2427] | 229 | break; |
---|
[5103] | 230 | |
---|
| 231 | // ------------------------------------------------ |
---|
[2427] | 232 | case SCHEDULE_FINE: |
---|
[5103] | 233 | // Start timer if the list goes from 0 -> 1 event |
---|
[5514] | 234 | if(wlan_sched_fine.enabled_list.length == 0){ |
---|
[5103] | 235 | XTmrCtr_SetResetValue(&timer_instance, TIMER_CNTR_FAST, (FAST_TIMER_DUR_US * TIMER_CLKS_PER_US)); |
---|
| 236 | XTmrCtr_Start(&timer_instance, TIMER_CNTR_FAST); |
---|
[2427] | 237 | } |
---|
[5103] | 238 | |
---|
[5514] | 239 | dl_entry_insertBeginning(&(wlan_sched_fine.enabled_list), entry_ptr); |
---|
[2427] | 240 | break; |
---|
[5103] | 241 | |
---|
| 242 | // ------------------------------------------------ |
---|
[3888] | 243 | default: |
---|
| 244 | xil_printf("Unknown scheduler selection. No event scheduled.\n"); |
---|
| 245 | return SCHEDULE_FAILURE; |
---|
| 246 | break; |
---|
[2427] | 247 | } |
---|
| 248 | |
---|
| 249 | return id; |
---|
| 250 | } |
---|
| 251 | |
---|
[5103] | 252 | |
---|
[5514] | 253 | dl_entry* wlan_mac_schedule_disable_id(u8 scheduler_sel, u32 sched_id){ |
---|
| 254 | dl_entry* sched_entry; |
---|
| 255 | dl_list* enabled_list; |
---|
| 256 | u8 timer_sel; |
---|
[5103] | 257 | |
---|
[5514] | 258 | switch(scheduler_sel){ |
---|
| 259 | case SCHEDULE_COARSE: |
---|
| 260 | enabled_list = &(wlan_sched_coarse.enabled_list); |
---|
| 261 | timer_sel = TIMER_CNTR_SLOW; |
---|
| 262 | break; |
---|
| 263 | |
---|
| 264 | case SCHEDULE_FINE: |
---|
| 265 | enabled_list = &(wlan_sched_fine.enabled_list); |
---|
| 266 | timer_sel = TIMER_CNTR_FAST; |
---|
| 267 | break; |
---|
| 268 | |
---|
| 269 | default: |
---|
| 270 | return NULL; |
---|
| 271 | break; |
---|
| 272 | } |
---|
| 273 | |
---|
[5515] | 274 | sched_entry = wlan_mac_schedule_find(enabled_list, sched_id); |
---|
[5514] | 275 | |
---|
| 276 | if(sched_entry != NULL){ |
---|
| 277 | if( ((wlan_sched*)(sched_entry->data))->num_calls != 0 ){ |
---|
| 278 | dl_entry_remove(enabled_list, sched_entry); |
---|
| 279 | dl_entry_insertEnd(&disabled_list, sched_entry); |
---|
| 280 | ((wlan_sched*)(sched_entry->data))->enabled = 0; |
---|
| 281 | } else { |
---|
| 282 | // wlan_mac_schedule_disable_id() is not supported for a schedule whose num_calls |
---|
| 283 | // is 0. The most common occurence of this scenario is trying to disable a schedule |
---|
| 284 | // from the final execution of a callback of that schedule. |
---|
| 285 | return NULL; |
---|
| 286 | } |
---|
| 287 | } |
---|
| 288 | |
---|
| 289 | // Stop the timer if there are no more events |
---|
| 290 | // NOTE: Will be restarted when an event is added or re-enabled |
---|
| 291 | if (enabled_list->length == 0) { |
---|
| 292 | XTmrCtr_Stop(&timer_instance, timer_sel); |
---|
| 293 | } |
---|
| 294 | |
---|
| 295 | return sched_entry; |
---|
| 296 | } |
---|
| 297 | |
---|
| 298 | int wlan_mac_schedule_enable(u8 scheduler_sel, dl_entry* sched_entry){ |
---|
| 299 | |
---|
| 300 | dl_list* enabled_list; |
---|
| 301 | u8 timer_sel; |
---|
| 302 | u32 reset_value; |
---|
| 303 | |
---|
| 304 | if(sched_entry == NULL){ |
---|
| 305 | return -1; |
---|
| 306 | } |
---|
| 307 | switch(scheduler_sel){ |
---|
| 308 | case SCHEDULE_COARSE: |
---|
| 309 | enabled_list = &(wlan_sched_coarse.enabled_list); |
---|
| 310 | timer_sel = TIMER_CNTR_SLOW; |
---|
| 311 | reset_value = (SLOW_TIMER_DUR_US * TIMER_CLKS_PER_US); |
---|
| 312 | break; |
---|
| 313 | |
---|
| 314 | case SCHEDULE_FINE: |
---|
| 315 | enabled_list = &(wlan_sched_fine.enabled_list); |
---|
| 316 | timer_sel = TIMER_CNTR_FAST; |
---|
| 317 | reset_value = (FAST_TIMER_DUR_US * TIMER_CLKS_PER_US); |
---|
| 318 | break; |
---|
| 319 | |
---|
| 320 | default: |
---|
| 321 | return -1; |
---|
| 322 | break; |
---|
| 323 | } |
---|
| 324 | |
---|
| 325 | dl_entry_remove(&disabled_list, sched_entry); |
---|
| 326 | dl_entry_insertEnd(enabled_list, sched_entry); |
---|
| 327 | |
---|
| 328 | ((wlan_sched*)(sched_entry->data))->enabled = 1; |
---|
| 329 | ((wlan_sched*)(sched_entry->data))->target_us = get_system_time_usec() + ((wlan_sched*)(sched_entry->data))->delay_us; |
---|
| 330 | |
---|
| 331 | // Start timer if the list goes from 0 -> 1 event |
---|
| 332 | if(wlan_sched_fine.enabled_list.length == 1){ |
---|
| 333 | XTmrCtr_SetResetValue(&timer_instance, timer_sel, reset_value); |
---|
| 334 | XTmrCtr_Start(&timer_instance, timer_sel); |
---|
| 335 | } |
---|
| 336 | |
---|
| 337 | return 0; |
---|
| 338 | |
---|
| 339 | } |
---|
| 340 | |
---|
| 341 | |
---|
[2435] | 342 | /*****************************************************************************/ |
---|
| 343 | /** |
---|
[5103] | 344 | * @brief Cancels the execution of a scheduled callback |
---|
| 345 | * |
---|
| 346 | * @param scheduler_sel - SCHEDULE_COARSE or SCHEDULE_FINE |
---|
| 347 | * @param id - ID of schedule that should be removed |
---|
| 348 | * |
---|
| 349 | * @return None |
---|
| 350 | * |
---|
| 351 | * @note This function will fail silently if the ID parameter does not match |
---|
| 352 | * any currently running schedule event IDs. |
---|
| 353 | * |
---|
| 354 | *****************************************************************************/ |
---|
[2431] | 355 | void wlan_mac_remove_schedule(u8 scheduler_sel, u32 id){ |
---|
[5103] | 356 | dl_entry * curr_entry_ptr; |
---|
| 357 | wlan_sched * curr_sched_ptr; |
---|
[2427] | 358 | |
---|
[5515] | 359 | dl_list* enabled_list; |
---|
[3034] | 360 | |
---|
[5515] | 361 | switch(scheduler_sel){ |
---|
| 362 | case SCHEDULE_COARSE: |
---|
| 363 | enabled_list = &(wlan_sched_coarse.enabled_list); |
---|
| 364 | break; |
---|
[5240] | 365 | |
---|
[5515] | 366 | case SCHEDULE_FINE: |
---|
| 367 | enabled_list = &(wlan_sched_fine.enabled_list); |
---|
| 368 | break; |
---|
[5240] | 369 | |
---|
[5515] | 370 | default: |
---|
| 371 | return; |
---|
| 372 | break; |
---|
| 373 | } |
---|
| 374 | |
---|
| 375 | curr_entry_ptr = wlan_mac_schedule_find(enabled_list, id); |
---|
| 376 | |
---|
[5103] | 377 | if (curr_entry_ptr != NULL) { |
---|
[5240] | 378 | |
---|
[5103] | 379 | // Extract the schedule struct from dl_entry |
---|
[3875] | 380 | curr_sched_ptr = (wlan_sched*)(curr_entry_ptr->data); |
---|
[2454] | 381 | |
---|
[5103] | 382 | switch (scheduler_sel) { |
---|
| 383 | // ------------------------------------------------ |
---|
[3875] | 384 | case SCHEDULE_COARSE: |
---|
| 385 | if(curr_sched_ptr != NULL){ |
---|
[5240] | 386 | if(wlan_sched_coarse.next == curr_entry_ptr){ |
---|
| 387 | // The next dl_entry in the schedule_handler iteration |
---|
| 388 | // is the schedule we are about to remove. We need to advance |
---|
| 389 | // this pointer prior to removing so that we do not break |
---|
| 390 | // the execution of schedule_handler. |
---|
| 391 | wlan_sched_coarse.next = dl_entry_next(curr_entry_ptr); |
---|
| 392 | } |
---|
[5514] | 393 | dl_entry_remove(&(wlan_sched_coarse.enabled_list), curr_entry_ptr); |
---|
[3875] | 394 | wlan_mac_high_free(curr_entry_ptr); |
---|
| 395 | wlan_mac_high_free(curr_sched_ptr); |
---|
| 396 | } |
---|
[2432] | 397 | |
---|
[5103] | 398 | // Stop the timer if there are no more events |
---|
| 399 | // NOTE: Will be restarted when new event is added |
---|
[5514] | 400 | if (wlan_sched_coarse.enabled_list.length == 0) { |
---|
[5103] | 401 | XTmrCtr_Stop(&timer_instance, TIMER_CNTR_SLOW); |
---|
[3875] | 402 | } |
---|
[3888] | 403 | break; |
---|
[2454] | 404 | |
---|
[5103] | 405 | // ------------------------------------------------ |
---|
[3875] | 406 | case SCHEDULE_FINE: |
---|
| 407 | if(curr_sched_ptr != NULL){ |
---|
[5240] | 408 | if(wlan_sched_fine.next == curr_entry_ptr){ |
---|
| 409 | // The next dl_entry in the schedule_handler iteration |
---|
| 410 | // is the schedule we are about to remove. We need to advance |
---|
| 411 | // this pointer prior to removing so that we do not break |
---|
| 412 | // the execution of schedule_handler. |
---|
| 413 | wlan_sched_fine.next = dl_entry_next(curr_entry_ptr); |
---|
| 414 | } |
---|
[5514] | 415 | dl_entry_remove(&(wlan_sched_fine.enabled_list), curr_entry_ptr); |
---|
[3875] | 416 | wlan_mac_high_free(curr_entry_ptr); |
---|
| 417 | wlan_mac_high_free(curr_sched_ptr); |
---|
| 418 | } |
---|
[2432] | 419 | |
---|
[5103] | 420 | // Stop the timer if there are no more events |
---|
| 421 | // NOTE: Will be restarted when new event is added |
---|
[5514] | 422 | if (wlan_sched_fine.enabled_list.length == 0){ |
---|
[5103] | 423 | XTmrCtr_Stop(&timer_instance, TIMER_CNTR_FAST); |
---|
[3875] | 424 | } |
---|
[3888] | 425 | break; |
---|
[3875] | 426 | |
---|
[5103] | 427 | // ------------------------------------------------ |
---|
[3888] | 428 | default: |
---|
| 429 | xil_printf("Unknown scheduler selection. No event removed.\n"); |
---|
[3875] | 430 | break; |
---|
| 431 | } |
---|
[5103] | 432 | |
---|
| 433 | #if WLAN_SCHED_EXEC_MONITOR |
---|
| 434 | // If both timers are stopped, then we need to reset the last_exec_timestamp |
---|
| 435 | // NOTE: This is so we do not get erroneous results when the timer is off |
---|
| 436 | // for extended periods of time. |
---|
| 437 | if ((wlan_sched_coarse.length == 0) && (wlan_sched_fine.length == 0)) { |
---|
| 438 | last_exec_timestamp = 0; |
---|
| 439 | } |
---|
| 440 | #endif |
---|
[3875] | 441 | } |
---|
[2431] | 442 | } |
---|
| 443 | |
---|
[5103] | 444 | |
---|
| 445 | |
---|
[2435] | 446 | /*****************************************************************************/ |
---|
| 447 | /** |
---|
[5103] | 448 | * Timer interrupt handler |
---|
| 449 | * |
---|
| 450 | * @param instance_ptr - Pointer to the timer instance |
---|
| 451 | * |
---|
| 452 | * @return None |
---|
| 453 | * |
---|
| 454 | * @note This function is a modified implementation of XTmrCtr_InterruptHandler |
---|
| 455 | * in the xtmrctr_intr.c driver. It has been modified to remove an explicit |
---|
| 456 | * reset of the timer. |
---|
| 457 | * |
---|
| 458 | *****************************************************************************/ |
---|
| 459 | void timer_interrupt_handler(void * instance_ptr){ |
---|
[3020] | 460 | |
---|
[5103] | 461 | XTmrCtr * timer_ptr; |
---|
| 462 | u8 timer_number; |
---|
| 463 | u32 base_addr; |
---|
| 464 | u32 csr_reg; |
---|
[2427] | 465 | |
---|
[5103] | 466 | #ifdef _ISR_PERF_MON_EN_ |
---|
[5543] | 467 | wlan_mac_set_dbg_hdr_out(ISR_PERF_MON_GPIO_MASK); |
---|
[5103] | 468 | #endif |
---|
[3875] | 469 | |
---|
[5103] | 470 | // Verify inputs are valid |
---|
| 471 | if (instance_ptr == NULL) { return; } |
---|
[3875] | 472 | |
---|
[5103] | 473 | timer_ptr = (XTmrCtr *) instance_ptr; |
---|
| 474 | base_addr = timer_ptr->BaseAddress; |
---|
[3875] | 475 | |
---|
[5103] | 476 | // Loop thru each timer counter in the device and call the callback |
---|
| 477 | // function for each timer which has caused an interrupt |
---|
| 478 | for (timer_number = 0; timer_number < XTC_DEVICE_TIMER_COUNT; timer_number++) { |
---|
[4972] | 479 | |
---|
[5103] | 480 | // Read Control / Status register |
---|
| 481 | csr_reg = XTmrCtr_ReadReg(base_addr, timer_number, XTC_TCSR_OFFSET); |
---|
[2427] | 482 | |
---|
[5103] | 483 | // Check if interrupt is enabled |
---|
| 484 | if (csr_reg & XTC_CSR_ENABLE_INT_MASK) { |
---|
[2459] | 485 | |
---|
[5103] | 486 | // Check if timer expired and interrupt occured |
---|
| 487 | if (csr_reg & XTC_CSR_INT_OCCURED_MASK) { |
---|
| 488 | // Increment statistics for the number of interrupts |
---|
| 489 | timer_ptr->Stats.Interrupts++; |
---|
[3034] | 490 | |
---|
[5103] | 491 | // Call callback to handle timer processing |
---|
| 492 | timer_ptr->Handler(timer_ptr->CallBackRef, timer_number); |
---|
[3875] | 493 | |
---|
[5103] | 494 | // Read Control / Status register |
---|
| 495 | csr_reg = XTmrCtr_ReadReg(base_addr, timer_number, XTC_TCSR_OFFSET); |
---|
[3875] | 496 | |
---|
[5103] | 497 | // Acknowledge the interrupt |
---|
| 498 | // NOTE: It's very important that this ack occurs after the handler call. This |
---|
| 499 | // will prevent a scenario where a fine schedule callback can "crash" a |
---|
| 500 | // node by taking longer than whatever the interval is of the fast timer. |
---|
| 501 | XTmrCtr_WriteReg(base_addr, timer_number, XTC_TCSR_OFFSET, (csr_reg | XTC_CSR_INT_OCCURED_MASK)); |
---|
[2427] | 502 | } |
---|
[5103] | 503 | } |
---|
| 504 | } |
---|
[2427] | 505 | |
---|
[5103] | 506 | #ifdef _ISR_PERF_MON_EN_ |
---|
[5543] | 507 | wlan_mac_clear_dbg_hdr_out(ISR_PERF_MON_GPIO_MASK); |
---|
[5103] | 508 | #endif |
---|
| 509 | } |
---|
[2427] | 510 | |
---|
| 511 | |
---|
[3034] | 512 | |
---|
[5103] | 513 | /*****************************************************************************/ |
---|
| 514 | /** |
---|
| 515 | * Internal callback used by the timer interrupt handler. This function should |
---|
| 516 | * not be called by the upper-level MAC. |
---|
| 517 | * |
---|
| 518 | * @param callback_ref - Reserved (Not currently used) |
---|
| 519 | * @param timer_number - ID of which timer has expired |
---|
| 520 | * |
---|
| 521 | * @return None |
---|
| 522 | * |
---|
| 523 | *****************************************************************************/ |
---|
| 524 | void schedule_handler(void * callback_ref, u8 timer_number){ |
---|
[5240] | 525 | dl_entry* curr_entry_ptr; |
---|
| 526 | wlan_sched* curr_sched_ptr; |
---|
[3034] | 527 | |
---|
[5240] | 528 | u8 scheduler; |
---|
| 529 | volatile wlan_sched_state_t* wlan_sched_state; |
---|
| 530 | u32 sched_id; |
---|
| 531 | function_ptr_t sched_callback; |
---|
[3180] | 532 | |
---|
[5103] | 533 | u64 curr_system_time; |
---|
[2427] | 534 | |
---|
[5240] | 535 | volatile static u8 debug_print = 0; |
---|
| 536 | |
---|
[5103] | 537 | // Get current system time |
---|
| 538 | curr_system_time = get_system_time_usec(); |
---|
| 539 | |
---|
| 540 | #if WLAN_SCHED_EXEC_MONITOR |
---|
| 541 | // ---------------------------------------------------- |
---|
| 542 | // Scheduler Monitor Implementation |
---|
| 543 | // |
---|
| 544 | // Collect the "time between executions" of the schedule_handler function. |
---|
| 545 | // |
---|
| 546 | // The first time through the handler after the timer has been started, |
---|
| 547 | // the last_exec_timestamp will be zero. Therefore, we want to wait until |
---|
| 548 | // subsequent loops to get the "time between executions" (ie the value |
---|
| 549 | // recorded by the schedule_exec_monitor variable). |
---|
| 550 | // |
---|
| 551 | if (last_exec_timestamp != 0) { |
---|
| 552 | schedule_exec_monitor = curr_system_time - last_exec_timestamp; |
---|
[2427] | 553 | } |
---|
[3233] | 554 | |
---|
[5103] | 555 | // If the monitor threshold is exceeded, print a warning. |
---|
| 556 | // NOTE: Since prints take a long time, update the system time so the |
---|
| 557 | // print does not affect the collection of "time between executions" |
---|
| 558 | if (schedule_exec_monitor > monitor_threshold) { |
---|
| 559 | xil_printf("WARNING: %d us between scheduler executions.\n", schedule_exec_monitor); |
---|
| 560 | curr_system_time = get_system_time_usec(); |
---|
[3233] | 561 | } |
---|
| 562 | |
---|
[5103] | 563 | // Store the current time in the last_timer_timestamp |
---|
| 564 | last_exec_timestamp = curr_system_time; |
---|
| 565 | #endif |
---|
[2427] | 566 | |
---|
[5103] | 567 | // Set any timer specific variables |
---|
| 568 | if (timer_number == TIMER_CNTR_FAST) { |
---|
| 569 | scheduler = SCHEDULE_FINE; |
---|
[5240] | 570 | wlan_sched_state = &(wlan_sched_fine); |
---|
[5103] | 571 | } else { |
---|
| 572 | // All other timers default to coarse scheduler |
---|
| 573 | scheduler = SCHEDULE_COARSE; |
---|
[5240] | 574 | wlan_sched_state = &(wlan_sched_coarse); |
---|
[5103] | 575 | } |
---|
[2450] | 576 | |
---|
[5103] | 577 | // Get the first entry of the schedule dl_list |
---|
[5514] | 578 | wlan_sched_state->next = wlan_sched_state->enabled_list.first; |
---|
[3875] | 579 | |
---|
[5103] | 580 | // Update the appropriate scheduler |
---|
[5240] | 581 | while (wlan_sched_state->next != NULL) { |
---|
| 582 | curr_entry_ptr = wlan_sched_state->next; |
---|
| 583 | wlan_sched_state->next = dl_entry_next(wlan_sched_state->next); |
---|
[5103] | 584 | curr_sched_ptr = (wlan_sched*)(curr_entry_ptr->data); |
---|
[3875] | 585 | |
---|
[5240] | 586 | if(debug_print){ |
---|
| 587 | xil_printf("curr_sched_ptr = 0x%08x\n", curr_sched_ptr); |
---|
| 588 | xil_printf("curr_sched_ptr->callback = 0x%08x\n", curr_sched_ptr->callback); |
---|
| 589 | xil_printf("curr_sched_ptr->id = %d\n", curr_sched_ptr->id); |
---|
| 590 | } |
---|
| 591 | |
---|
[5103] | 592 | // If the target time has passed, then process the event |
---|
| 593 | if (curr_system_time >= (curr_sched_ptr->target_us)) { |
---|
| 594 | sched_id = curr_sched_ptr->id; |
---|
| 595 | sched_callback = curr_sched_ptr->callback; |
---|
[3875] | 596 | |
---|
[5103] | 597 | // Update the number of scheduled event calls |
---|
| 598 | if ((curr_sched_ptr->num_calls != SCHEDULE_REPEAT_FOREVER) && |
---|
| 599 | (curr_sched_ptr->num_calls != 0)) { |
---|
| 600 | (curr_sched_ptr->num_calls)--; |
---|
[2450] | 601 | } |
---|
[4942] | 602 | |
---|
[5514] | 603 | sched_callback(sched_id); |
---|
| 604 | |
---|
| 605 | // The update of the target or removal of the schedule occurs after the callback so that the callback |
---|
| 606 | // has the opportunity to "save" the schedule. It can do this by updating "num_calls" or by calling |
---|
| 607 | // wlan_mac_schedule_disable |
---|
| 608 | if(curr_sched_ptr->enabled == 1){ |
---|
| 609 | if(curr_sched_ptr->num_calls == 0){ |
---|
| 610 | // Remove event (stops timer if necessary) |
---|
| 611 | wlan_mac_remove_schedule(scheduler, sched_id); |
---|
| 612 | } else { |
---|
| 613 | // Update scheduled event for next execution |
---|
| 614 | curr_sched_ptr->target_us = curr_system_time + (u64)(curr_sched_ptr->delay_us); |
---|
| 615 | } |
---|
[5103] | 616 | } |
---|
| 617 | } |
---|
| 618 | } |
---|
| 619 | } |
---|
[3875] | 620 | |
---|
| 621 | |
---|
[4942] | 622 | |
---|
[2450] | 623 | /*****************************************************************************/ |
---|
| 624 | /** |
---|
[5103] | 625 | * Find schedule that corresponds to a given ID |
---|
| 626 | * |
---|
[5515] | 627 | * @param dl_list* - List to search through; typically the fine, coarse, and deactivated lists |
---|
[5103] | 628 | * @param id - ID of the scheduler that should be returned |
---|
| 629 | * |
---|
| 630 | * @return dl_entry* - Pointer to the list entry that contains the schedule |
---|
| 631 | * |
---|
| 632 | *****************************************************************************/ |
---|
[5515] | 633 | dl_entry* wlan_mac_schedule_find(dl_list* sched_list, u32 id){ |
---|
[5103] | 634 | int iter; |
---|
[2427] | 635 | |
---|
[5103] | 636 | dl_entry * curr_dl_entry; |
---|
| 637 | dl_entry * next_dl_entry; |
---|
| 638 | wlan_sched * curr_wlan_sched; |
---|
[2427] | 639 | |
---|
[5103] | 640 | // Initialize the loop variables |
---|
| 641 | iter = sched_list->length; |
---|
| 642 | next_dl_entry = sched_list->first; |
---|
[2427] | 643 | |
---|
[5103] | 644 | // Process the list |
---|
| 645 | while ((next_dl_entry != NULL) && (iter-- > 0)) { |
---|
| 646 | curr_dl_entry = next_dl_entry; |
---|
| 647 | next_dl_entry = dl_entry_next(next_dl_entry); |
---|
| 648 | curr_wlan_sched = (wlan_sched*)(curr_dl_entry->data); |
---|
[2427] | 649 | |
---|
[5103] | 650 | if(curr_wlan_sched->id == id){ |
---|
| 651 | return curr_dl_entry; |
---|
[2427] | 652 | } |
---|
| 653 | } |
---|
[5103] | 654 | |
---|
| 655 | return NULL; |
---|
[2427] | 656 | } |
---|
[5103] | 657 | |
---|