Select one of the symbols to view example projects that use it.
 
Outline
...
...
...
...
#define TX_SOURCE_CODE
#include "tx_api.h"
#include "tx_timer.h"
#include "tx_low_power.h"
tx_low_power_entered
...
...
tx_low_power_enter()
...
...
tx_low_power_exit()
...
...
tx_timer_get_next(ULONG *)
...
tx_time_increment(ULONG)
Files
loading...
CodeScopeSTM32 Libraries and Samplesthreadxutility/low_power/tx_low_power.c
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
/**************************************************************************/ /* */ /* Copyright (c) Microsoft Corporation. All rights reserved. */ /* */ /* This software is licensed under the Microsoft Software License */ /* Terms for Microsoft Azure RTOS. Full text of the license can be */ /* found in the LICENSE file at https://aka.ms/AzureRTOS_EULA */ /* and in the root directory of this software. */ /* */... /**************************************************************************/ ... /**************************************************************************/ /**************************************************************************/ /** */ /** ThreadX Component */ /** */ /** Low Power Timer Management */ /** */... /**************************************************************************/ /**************************************************************************/ #define TX_SOURCE_CODE /* Include necessary system files. */ #include "tx_api.h" #include "tx_timer.h" #include "tx_low_power.h" /* Define low power global variables. */ /* Flag to determine if we've entered low power mode or not. */ UINT tx_low_power_entered; ... /**************************************************************************/ /* */ /* FUNCTION RELEASE */ /* */ /* tx_low_power_enter PORTABLE C */ /* 6.1.6 */ /* AUTHOR */ /* */ /* William E. Lamie, Microsoft Corporation */ /* */ /* DESCRIPTION */ /* */ /* This function is the low power entry function. This function is */ /* assumed to be called from the idle loop of tx_thread_schedule. It */ /* is important to note that if an interrupt managed by ThreadX occurs */ /* anywhere where interrupts are enabled in this function, the entire */ /* processing of this function is discarded and the function won't be */ /* re-entered until the idle loop in tx_thread_schedule is executed */ /* again. */ /* */ /* INPUT */ /* */ /* None */ /* */ /* OUTPUT */ /* */ /* None */ /* */ /* CALLS */ /* */ /* tx_timer_get_next Get next timer expiration */ /* */ /* CALLED BY */ /* */ /* _tx_thread_schedule Thread scheduling loop */ /* */ /* RELEASE HISTORY */ /* */ /* DATE NAME DESCRIPTION */ /* */ /* 03-02-2021 William E. Lamie Initial Version 6.1.5 */ /* 04-02-2021 Scott Larson Modified comments and fixed */ /* compiler warning, */ /* resulting in version 6.1.6 */ /* */... /**************************************************************************/ VOID tx_low_power_enter(VOID) { TX_INTERRUPT_SAVE_AREA #ifdef TX_LOW_POWER_TIMER_SETUP ULONG tx_low_power_next_expiration; /* The next timer experation (units of ThreadX timer ticks). */ ULONG timers_active;/* ... */ #endif /* Disable interrupts while we prepare for low power mode. */ TX_DISABLE /* TX_LOW_POWER_TIMER_SETUP is a macro to a routine that sets up a low power clock. If such routine does not exist, we can skip the logic that computes the next expiration time. *//* ... */ #ifdef TX_LOW_POWER_TIMER_SETUP /* At this point, we want to enter low power mode, since nothing meaningful is going on in the system. However, in order to keep the ThreadX timer services accurate, we must first determine the next ThreadX timer expiration in terms of ticks. This is accomplished via the tx_timer_get_next API. *//* ... */ timers_active = tx_timer_get_next(&tx_low_power_next_expiration); /* There are two possibilities: 1: A ThreadX timer is active. tx_timer_get_next returns TX_TRUE. Program the hardware timer source such that the next timer interrupt is equal to: tx_low_power_next_expiration*tick_frequency. In most applications, the tick_frequency is 10ms, but this is completely application specific in ThreadX, typically set up in tx_low_level_initialize. Note that in this situation, a low power clock must be used in order to wake up the CPU for the next timeout event. Therefore an alternative clock must be programmed. 2: There are no ThreadX timers active. tx_timer_get_next returns TX_FALSE. 2.a: application may choose not to keep the ThreadX internal tick count updated (define TX_LOW_POWER_TICKLESS), therefore no need to set up a low power clock. 2.b: Application still needs to keep ThreadX tick up-to-date. In this case a low power clock needs to be set up. *//* ... */ #ifndef TX_LOW_POWER_TICKLESS /* We still want to keep track of time in low power mode. */ if (timers_active == TX_FALSE) { /* Set the next expiration to 0xFFFFFFF, an indication that the timer sleeps for maximum amount of time the HW supports.*//* ... */ tx_low_power_next_expiration = 0xFFFFFFFF; timers_active = TX_TRUE; }if (timers_active == TX_FALSE) { ... } /* ... */#endif /* TX_LOW_POWER_TICKLESS */ if (timers_active == TX_TRUE) { /* A ThreadX timer is active or we simply want to keep track of time. */ TX_LOW_POWER_TIMER_SETUP(tx_low_power_next_expiration); }if (timers_active == TX_TRUE) { ... } /* ... */#endif /* TX_LOW_POWER_TIMER_SETUP */ /* Set the flag indicating that low power has been entered. This flag is checked in tx_low_power_exit to determine if the logic used to adjust the ThreadX time is required. *//* ... */ tx_low_power_entered = TX_TRUE; /* Re-enable interrupts before low power mode is entered. */ TX_RESTORE /* User code to enter low power mode. This allows the application to power down peripherals and put the processor in sleep mode. *//* ... */ #ifdef TX_LOW_POWER_USER_ENTER TX_LOW_POWER_USER_ENTER; #endif /* If the low power code returns, this routine returns to the tx_thread_schedule loop. */ }{ ... } ... /**************************************************************************/ /* */ /* FUNCTION RELEASE */ /* */ /* tx_low_power_exit PORTABLE C */ /* 6.1.5 */ /* AUTHOR */ /* */ /* William E. Lamie, Microsoft Corporation */ /* */ /* DESCRIPTION */ /* */ /* This function is the low power exit function. This function must */ /* be called from any interrupt that can wakeup the processor from */ /* low power mode. If nothing needs to be done, this function simply */ /* returns. */ /* */ /* INPUT */ /* */ /* None */ /* */ /* OUTPUT */ /* */ /* None */ /* */ /* CALLS */ /* */ /* tx_time_increment Update the ThreadX timer */ /* */ /* CALLED BY */ /* */ /* ISRs Front-end of Interrupt */ /* Service Routines */ /* */ /* RELEASE HISTORY */ /* */ /* DATE NAME DESCRIPTION */ /* */ /* 03-02-2021 William E. Lamie Initial Version 6.1.5 */ /* */... /**************************************************************************/ VOID tx_low_power_exit(VOID) { /* How many ticks to adjust ThreadX timers after exiting low power mode. */ ULONG tx_low_power_adjust_ticks; /* Determine if the interrupt occurred in low power mode. */ if (tx_low_power_entered) { /* Yes, low power mode was interrupted. */ /* Clear the low power entered flag. */ tx_low_power_entered = TX_FALSE; /* User code to exit low power mode and reprogram the timer to the desired interrupt frequency. *//* ... */ #ifdef TX_LOW_POWER_USER_EXIT TX_LOW_POWER_USER_EXIT; #endif #ifdef TX_LOW_POWER_USER_TIMER_ADJUST /* Call the user's low-power timer code to obtain the amount of time (in ticks) the system has been in low power mode. *//* ... */ tx_low_power_adjust_ticks = TX_LOW_POWER_USER_TIMER_ADJUST;/* ... */ #else tx_low_power_adjust_ticks = (ULONG) 0; #endif /* Determine if the ThreadX timer(s) needs incrementing. */ if (tx_low_power_adjust_ticks) { /* Yes, the ThreadX timer(s) must be incremented. */ tx_time_increment(tx_low_power_adjust_ticks); }if (tx_low_power_adjust_ticks) { ... } }if (tx_low_power_entered) { ... } }{ ... } ... /**************************************************************************/ /* */ /* FUNCTION RELEASE */ /* */ /* tx_timer_get_next PORTABLE C */ /* 6.1.5 */ /* AUTHOR */ /* */ /* William E. Lamie, Microsoft Corporation */ /* */ /* DESCRIPTION */ /* */ /* This function calculates the next expiration time minus 1 tick for */ /* the currently active ThreadX timers. If no timer is active, this */ /* routine will return a value of TX_FALSE and the next ticks value */ /* will be set to zero. */ /* */ /* INPUT */ /* */ /* next_timer_tick_ptr Pointer to destination for next */ /* timer expiration value */ /* */ /* OUTPUT */ /* */ /* TX_TRUE (1) At least one timer is active */ /* TX_FALSE (0) No timers are currently active */ /* */ /* CALLS */ /* */ /* None */ /* */ /* CALLED BY */ /* */ /* tx_low_power_enter */ /* */ /* RELEASE HISTORY */ /* */ /* DATE NAME DESCRIPTION */ /* */ /* 03-02-2021 William E. Lamie Initial Version 6.1.5 */ /* */... /**************************************************************************/ ULONG tx_timer_get_next(ULONG *next_timer_tick_ptr) { TX_INTERRUPT_SAVE_AREA TX_TIMER_INTERNAL **timer_list_head; TX_TIMER_INTERNAL *next_timer; UINT i; ULONG calculated_time; ULONG expiration_time = (ULONG) 0xFFFFFFFF; /* Disable interrupts. */ TX_DISABLE /* Look at the next timer entry. */ timer_list_head = _tx_timer_current_ptr; /* Loop through the timer list, looking for the first non-NULL value to signal an active timer. *//* ... */ for (i = (UINT)0; i < TX_TIMER_ENTRIES; i++) { /* Now determine if there is an active timer in this slot. */ if (*timer_list_head) { /* Setup the pointer to the expiration list. */ next_timer = *timer_list_head; /* Loop through the timers active for this relative time slot (determined by i). */ do { /* Determine if the remaining time is larger than the list. */ if (next_timer -> tx_timer_internal_remaining_ticks > TX_TIMER_ENTRIES) { /* Calculate the expiration time. */ calculated_time = next_timer -> tx_timer_internal_remaining_ticks - (TX_TIMER_ENTRIES - i); }if (next_timer -> tx_timer_internal_remaining_ticks > TX_TIMER_ENTRIES) { ... } else { /* Calculate the expiration time, which is simply the number of entries in this case. */ calculated_time = i; }else { ... } /* Determine if a new minimum expiration time is present. */ if (expiration_time > calculated_time) { /* Yes, a new minimum expiration time is present - remember it! */ expiration_time = calculated_time; }if (expiration_time > calculated_time) { ... } /* Move to the next entry in the timer list. */ next_timer = next_timer -> tx_timer_internal_active_next; ...} while (next_timer != *timer_list_head); }if (*timer_list_head) { ... } /* This timer entry is NULL, so just move to the next one. */ timer_list_head++; /* Check for timer list wrap condition. */ if (timer_list_head >= _tx_timer_list_end) { /* Wrap to the beginning of the list. */ timer_list_head = _tx_timer_list_start; }if (timer_list_head >= _tx_timer_list_end) { ... } }for (i = (UINT)0; i < TX_TIMER_ENTRIES; i++) { ... } /* Restore interrupts. */ TX_RESTORE /* Determine if an active timer was found. */ if (expiration_time != (ULONG) 0xFFFFFFFF) { /* Yes, an active timer was found. */ *next_timer_tick_ptr = expiration_time; return(TX_TRUE); }if (expiration_time != (ULONG) 0xFFFFFFFF) { ... } else { /* No active timer was found. */ *next_timer_tick_ptr = 0; return(TX_FALSE); }else { ... } }{ ... } ... /**************************************************************************/ /* */ /* FUNCTION RELEASE */ /* */ /* tx_time_increment PORTABLE C */ /* 6.1.5 */ /* AUTHOR */ /* */ /* William E. Lamie, Microsoft Corporation */ /* */ /* DESCRIPTION */ /* */ /* This function increments the current time by a specified value. */ /* The value was derived by the application by calling the */ /* tx_timer_get_next function prior to this call, which was right */ /* before the processor was put in sleep mode. */ /* */ /* INPUT */ /* */ /* time_increment The amount of time to catch up on */ /* */ /* OUTPUT */ /* */ /* None */ /* */ /* CALLS */ /* */ /* _tx_timer_system_activate Timer activate service */ /* */ /* CALLED BY */ /* */ /* tx_low_power_exit */ /* */ /* RELEASE HISTORY */ /* */ /* DATE NAME DESCRIPTION */ /* */ /* 03-02-2021 William E. Lamie Initial Version 6.1.5 */ /* */... /**************************************************************************/ VOID tx_time_increment(ULONG time_increment) { TX_INTERRUPT_SAVE_AREA UINT i; TX_TIMER_INTERNAL **timer_list_head; TX_TIMER_INTERNAL *next_timer; TX_TIMER_INTERNAL *temp_list_head; /* Determine if there is any time increment. */ if (time_increment == 0) { /* Nothing to do, just return. */ return; }if (time_increment == 0) { ... } /* Disable interrupts. */ TX_DISABLE /* Adjust the system clock. */ _tx_timer_system_clock = _tx_timer_system_clock + time_increment; /* Adjust the time slice variable. */ if (_tx_timer_time_slice) { /* Decrement the time-slice variable. */ if (_tx_timer_time_slice > time_increment) { _tx_timer_time_slice = _tx_timer_time_slice - time_increment; }if (_tx_timer_time_slice > time_increment) { ... } else { _tx_timer_time_slice = 1; }else { ... } }if (_tx_timer_time_slice) { ... } /* Calculate the proper place to position the timer. */ timer_list_head = _tx_timer_current_ptr; /* Setup the temporary list pointer. */ temp_list_head = TX_NULL; /* Loop to pull all timers off the timer structure and put on the temporary list head. */ for (i = 0; i < TX_TIMER_ENTRIES; i++) { /* Determine if there is a timer list in this entry. */ if (*timer_list_head) { /* Walk the list and update all the relative times to actual times. */ /* Setup the pointer to the expiration list. */ next_timer = *timer_list_head; /* Loop through the timers active for this relative time slot (determined by i). */ do { /* Determine if the remaining time is larger than the list. */ if (next_timer -> tx_timer_internal_remaining_ticks > TX_TIMER_ENTRIES) { /* Calculate the actual expiration time. */ next_timer -> tx_timer_internal_remaining_ticks = next_timer -> tx_timer_internal_remaining_ticks - (TX_TIMER_ENTRIES - i) + 1; }if (next_timer -> tx_timer_internal_remaining_ticks > TX_TIMER_ENTRIES) { ... } else { /* Calculate the expiration time, which is simply the number of entries in this case. */ next_timer -> tx_timer_internal_remaining_ticks = i + 1; }else { ... } /* Move to the next entry in the timer list. */ next_timer = next_timer -> tx_timer_internal_active_next; ...} while (next_timer != *timer_list_head); /* NULL terminate the current timer list. */ ((*timer_list_head) -> tx_timer_internal_active_previous) -> tx_timer_internal_active_next = TX_NULL; /* Yes, determine if the temporary list is NULL. */ if (temp_list_head == TX_NULL) { /* First item on the list. Move the entire linked list. */ temp_list_head = *timer_list_head; }if (temp_list_head == TX_NULL) { ... } else { /* No, the temp list already has timers on it. Link the next timer list to the end. */ (temp_list_head -> tx_timer_internal_active_previous) -> tx_timer_internal_active_next = *timer_list_head; /* Now update the previous to the new list's previous timer pointer. */ temp_list_head -> tx_timer_internal_active_previous = (*timer_list_head) -> tx_timer_internal_active_previous; }else { ... } /* Now clear the current timer head pointer. */ *timer_list_head = TX_NULL; }if (*timer_list_head) { ... } /* Move to next timer entry. */ timer_list_head++; /* Determine if a wrap around condition has occurred. */ if (timer_list_head >= _tx_timer_list_end) { /* Wrap from the beginning of the list. */ timer_list_head = _tx_timer_list_start; }if (timer_list_head >= _tx_timer_list_end) { ... } }for (i = 0; i < TX_TIMER_ENTRIES; i++) { ... } /* Set the current timer pointer to the beginning of the list. */ _tx_timer_current_ptr = _tx_timer_list_start; /* Loop to update and reinsert all the timers in the list. */ while (temp_list_head) { /* Pickup the next timer to update and reinsert. */ next_timer = temp_list_head; /* Move the temp list head pointer to the next pointer. */ temp_list_head = next_timer -> tx_timer_internal_active_next; /* Determine if the remaining time is greater than the time increment value - this is the normal case. *//* ... */ if (next_timer -> tx_timer_internal_remaining_ticks > time_increment) { /* Decrement the elapsed time. */ next_timer -> tx_timer_internal_remaining_ticks = next_timer -> tx_timer_internal_remaining_ticks - time_increment; }if (next_timer -> tx_timer_internal_remaining_ticks > time_increment) { ... } else { /* Simply set the expiration value to expire on the next tick. */ next_timer -> tx_timer_internal_remaining_ticks = 1; }else { ... } /* Now clear the timer list head pointer for the timer activate function to work properly. */ next_timer -> tx_timer_internal_list_head = TX_NULL; /* Now re-insert the timer into the list. */ _tx_timer_system_activate(next_timer); }while (temp_list_head) { ... } /* Restore interrupts. */ TX_RESTORE }{ ... }