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