1 /***************************************************************************
2 * Copyright (c) 2024 Microsoft Corporation
3 *
4 * This program and the accompanying materials are made available under the
5 * terms of the MIT License which is available at
6 * https://opensource.org/licenses/MIT.
7 *
8 * SPDX-License-Identifier: MIT
9 **************************************************************************/
10
11
12 /**************************************************************************/
13 /**************************************************************************/
14 /** */
15 /** ThreadX Component */
16 /** */
17 /** Timer */
18 /** */
19 /**************************************************************************/
20 /**************************************************************************/
21
22 #define TX_SOURCE_CODE
23
24
25 /* Include necessary system files. */
26
27 #include "tx_api.h"
28 #include "tx_trace.h"
29 #include "tx_timer.h"
30
31
32 /**************************************************************************/
33 /* */
34 /* FUNCTION RELEASE */
35 /* */
36 /* _tx_timer_create PORTABLE C */
37 /* 6.1 */
38 /* AUTHOR */
39 /* */
40 /* William E. Lamie, Microsoft Corporation */
41 /* */
42 /* DESCRIPTION */
43 /* */
44 /* This function creates an application timer from the specified */
45 /* input. */
46 /* */
47 /* INPUT */
48 /* */
49 /* timer_ptr Pointer to timer control block */
50 /* name_ptr Pointer to timer name */
51 /* expiration_function Application expiration function */
52 /* initial_ticks Initial expiration ticks */
53 /* reschedule_ticks Reschedule ticks */
54 /* auto_activate Automatic activation flag */
55 /* */
56 /* OUTPUT */
57 /* */
58 /* TX_SUCCESS Successful completion status */
59 /* */
60 /* CALLS */
61 /* */
62 /* _tx_timer_system_activate Timer activation function */
63 /* */
64 /* CALLED BY */
65 /* */
66 /* Application Code */
67 /* */
68 /* RELEASE HISTORY */
69 /* */
70 /* DATE NAME DESCRIPTION */
71 /* */
72 /* 05-19-2020 William E. Lamie Initial Version 6.0 */
73 /* 09-30-2020 Yuxin Zhou Modified comment(s), */
74 /* resulting in version 6.1 */
75 /* */
76 /**************************************************************************/
_tx_timer_create(TX_TIMER * timer_ptr,CHAR * name_ptr,VOID (* expiration_function)(ULONG id),ULONG expiration_input,ULONG initial_ticks,ULONG reschedule_ticks,UINT auto_activate)77 UINT _tx_timer_create(TX_TIMER *timer_ptr, CHAR *name_ptr,
78 VOID (*expiration_function)(ULONG id), ULONG expiration_input,
79 ULONG initial_ticks, ULONG reschedule_ticks, UINT auto_activate)
80 {
81
82 TX_INTERRUPT_SAVE_AREA
83
84 TX_TIMER *next_timer;
85 TX_TIMER *previous_timer;
86
87
88 /* Initialize timer control block to all zeros. */
89 TX_MEMSET(timer_ptr, 0, (sizeof(TX_TIMER)));
90
91 /* Setup the basic timer fields. */
92 timer_ptr -> tx_timer_name = name_ptr;
93 timer_ptr -> tx_timer_internal.tx_timer_internal_remaining_ticks = initial_ticks;
94 timer_ptr -> tx_timer_internal.tx_timer_internal_re_initialize_ticks = reschedule_ticks;
95 timer_ptr -> tx_timer_internal.tx_timer_internal_timeout_function = expiration_function;
96 timer_ptr -> tx_timer_internal.tx_timer_internal_timeout_param = expiration_input;
97
98 /* Disable interrupts to put the timer on the created list. */
99 TX_DISABLE
100
101 /* Setup the timer ID to make it valid. */
102 timer_ptr -> tx_timer_id = TX_TIMER_ID;
103
104 /* Place the timer on the list of created application timers. First,
105 check for an empty list. */
106 if (_tx_timer_created_count == TX_EMPTY)
107 {
108
109 /* The created timer list is empty. Add timer to empty list. */
110 _tx_timer_created_ptr = timer_ptr;
111 timer_ptr -> tx_timer_created_next = timer_ptr;
112 timer_ptr -> tx_timer_created_previous = timer_ptr;
113 }
114 else
115 {
116
117 /* This list is not NULL, add to the end of the list. */
118 next_timer = _tx_timer_created_ptr;
119 previous_timer = next_timer -> tx_timer_created_previous;
120
121 /* Place the new timer in the list. */
122 next_timer -> tx_timer_created_previous = timer_ptr;
123 previous_timer -> tx_timer_created_next = timer_ptr;
124
125 /* Setup this timer's created links. */
126 timer_ptr -> tx_timer_created_previous = previous_timer;
127 timer_ptr -> tx_timer_created_next = next_timer;
128 }
129
130 /* Increment the number of created timers. */
131 _tx_timer_created_count++;
132
133 /* Optional timer create extended processing. */
134 TX_TIMER_CREATE_EXTENSION(timer_ptr)
135
136 /* If trace is enabled, register this object. */
137 TX_TRACE_OBJECT_REGISTER(TX_TRACE_OBJECT_TYPE_TIMER, timer_ptr, name_ptr, initial_ticks, reschedule_ticks)
138
139 /* If trace is enabled, insert this call in the trace buffer. */
140 TX_TRACE_IN_LINE_INSERT(TX_TRACE_TIMER_CREATE, timer_ptr, initial_ticks, reschedule_ticks, auto_activate, TX_TRACE_TIMER_EVENTS)
141
142 /* Log this kernel call. */
143 TX_EL_TIMER_CREATE_INSERT
144
145 /* Determine if this timer needs to be activated. */
146 if (auto_activate == TX_AUTO_ACTIVATE)
147 {
148
149 #ifdef TX_TIMER_ENABLE_PERFORMANCE_INFO
150
151 /* Increment the total activations counter. */
152 _tx_timer_performance_activate_count++;
153
154 /* Increment the number of activations on this timer. */
155 timer_ptr -> tx_timer_performance_activate_count++;
156 #endif
157
158 /* Call actual activation function. */
159 _tx_timer_system_activate(&(timer_ptr -> tx_timer_internal));
160 }
161
162 /* Restore interrupts. */
163 TX_RESTORE
164
165 /* Return TX_SUCCESS. */
166 return(TX_SUCCESS);
167 }
168
169