1 /**************************************************************************/
2 /*                                                                        */
3 /*       Copyright (c) Microsoft Corporation. All rights reserved.        */
4 /*                                                                        */
5 /*       This software is licensed under the Microsoft Software License   */
6 /*       Terms for Microsoft Azure RTOS. Full text of the license can be  */
7 /*       found in the LICENSE file at https://aka.ms/AzureRTOS_EULA       */
8 /*       and in the root directory of this software.                      */
9 /*                                                                        */
10 /**************************************************************************/
11 
12 
13 /**************************************************************************/
14 /**************************************************************************/
15 /**                                                                       */
16 /** ThreadX Component                                                     */
17 /**                                                                       */
18 /**   Timer                                                               */
19 /**                                                                       */
20 /**************************************************************************/
21 /**************************************************************************/
22 
23 #define TX_SOURCE_CODE
24 #define TX_THREAD_SMP_SOURCE_CODE
25 
26 
27 /* Include necessary system files.  */
28 
29 #include "tx_api.h"
30 #include "tx_trace.h"
31 #include "tx_timer.h"
32 
33 
34 /**************************************************************************/
35 /*                                                                        */
36 /*  FUNCTION                                               RELEASE        */
37 /*                                                                        */
38 /*    _tx_timer_create                                   PORTABLE SMP     */
39 /*                                                           6.1          */
40 /*  AUTHOR                                                                */
41 /*                                                                        */
42 /*    William E. Lamie, Microsoft Corporation                             */
43 /*                                                                        */
44 /*  DESCRIPTION                                                           */
45 /*                                                                        */
46 /*    This function creates an application timer from the specified       */
47 /*    input.                                                              */
48 /*                                                                        */
49 /*  INPUT                                                                 */
50 /*                                                                        */
51 /*    timer_ptr                         Pointer to timer control block    */
52 /*    name_ptr                          Pointer to timer name             */
53 /*    expiration_function               Application expiration function   */
54 /*    initial_ticks                     Initial expiration ticks          */
55 /*    reschedule_ticks                  Reschedule ticks                  */
56 /*    auto_activate                     Automatic activation flag         */
57 /*                                                                        */
58 /*  OUTPUT                                                                */
59 /*                                                                        */
60 /*    TX_SUCCESS                        Successful completion status      */
61 /*                                                                        */
62 /*  CALLS                                                                 */
63 /*                                                                        */
64 /*    _tx_timer_system_activate         Timer activation function         */
65 /*                                                                        */
66 /*  CALLED BY                                                             */
67 /*                                                                        */
68 /*    Application Code                                                    */
69 /*                                                                        */
70 /*  RELEASE HISTORY                                                       */
71 /*                                                                        */
72 /*    DATE              NAME                      DESCRIPTION             */
73 /*                                                                        */
74 /*  09-30-2020     William E. Lamie         Initial 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 #ifdef TX_THREAD_SMP_ONLY_CORE_0_DEFAULT
99 
100     /* Default the timers to run on core 0.  */
101     timer_ptr -> tx_timer_internal.tx_timer_internal_smp_cores_excluded =  (TX_THREAD_SMP_CORE_MASK & 0xFFFFFFFE);
102 #endif
103 
104     /* Disable interrupts to put the timer on the created list.  */
105     TX_DISABLE
106 
107     /* Setup the timer ID to make it valid.  */
108     timer_ptr -> tx_timer_id =  TX_TIMER_ID;
109 
110     /* Place the timer on the list of created application timers.  First,
111        check for an empty list.  */
112     if (_tx_timer_created_count == TX_EMPTY)
113     {
114 
115         /* The created timer list is empty.  Add timer to empty list.  */
116         _tx_timer_created_ptr =                   timer_ptr;
117         timer_ptr -> tx_timer_created_next =      timer_ptr;
118         timer_ptr -> tx_timer_created_previous =  timer_ptr;
119     }
120     else
121     {
122 
123         /* This list is not NULL, add to the end of the list.  */
124         next_timer =  _tx_timer_created_ptr;
125         previous_timer =  next_timer -> tx_timer_created_previous;
126 
127         /* Place the new timer in the list.  */
128         next_timer -> tx_timer_created_previous =  timer_ptr;
129         previous_timer -> tx_timer_created_next =    timer_ptr;
130 
131         /* Setup this timer's created links.  */
132         timer_ptr -> tx_timer_created_previous =  previous_timer;
133         timer_ptr -> tx_timer_created_next =      next_timer;
134     }
135 
136     /* Increment the number of created timers.  */
137     _tx_timer_created_count++;
138 
139     /* Optional timer create extended processing.  */
140     TX_TIMER_CREATE_EXTENSION(timer_ptr)
141 
142     /* If trace is enabled, register this object.  */
143     TX_TRACE_OBJECT_REGISTER(TX_TRACE_OBJECT_TYPE_TIMER, timer_ptr, name_ptr, initial_ticks, reschedule_ticks)
144 
145     /* If trace is enabled, insert this call in the trace buffer.  */
146     TX_TRACE_IN_LINE_INSERT(TX_TRACE_TIMER_CREATE, timer_ptr, initial_ticks, reschedule_ticks, auto_activate, TX_TRACE_TIMER_EVENTS)
147 
148     /* Log this kernel call.  */
149     TX_EL_TIMER_CREATE_INSERT
150 
151     /* Determine if this timer needs to be activated.  */
152     if (auto_activate == TX_AUTO_ACTIVATE)
153     {
154 
155 #ifdef TX_TIMER_ENABLE_PERFORMANCE_INFO
156 
157         /* Increment the total activations counter.  */
158         _tx_timer_performance_activate_count++;
159 
160         /* Increment the number of activations on this timer.  */
161         timer_ptr -> tx_timer_performance_activate_count++;
162 #endif
163 
164         /* Call actual activation function.  */
165         _tx_timer_system_activate(&(timer_ptr -> tx_timer_internal));
166     }
167 
168     /* Restore interrupts.  */
169     TX_RESTORE
170 
171     /* Return TX_SUCCESS.  */
172     return(TX_SUCCESS);
173 }
174 
175