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 /**   Initialize                                                          */
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_initialize.h"
30 #include "tx_thread.h"
31 #include "tx_timer.h"
32 
33 #if defined(TX_ENABLE_EXECUTION_CHANGE_NOTIFY) || defined(TX_EXECUTION_PROFILE_ENABLE)
34 extern VOID _tx_execution_initialize(VOID);
35 #endif
36 
37 /* Define any port-specific scheduling data structures.  */
38 
39 TX_PORT_SPECIFIC_DATA
40 
41 
42 #ifdef TX_SAFETY_CRITICAL
43 TX_SAFETY_CRITICAL_EXCEPTION_HANDLER
44 #endif
45 
46 
47 /**************************************************************************/
48 /*                                                                        */
49 /*  FUNCTION                                               RELEASE        */
50 /*                                                                        */
51 /*    _tx_initialize_kernel_enter                         PORTABLE C      */
52 /*                                                           6.3.0        */
53 /*  AUTHOR                                                                */
54 /*                                                                        */
55 /*    William E. Lamie, Microsoft Corporation                             */
56 /*                                                                        */
57 /*  DESCRIPTION                                                           */
58 /*                                                                        */
59 /*    This function is the first ThreadX function called during           */
60 /*    initialization.  It is called from the application's "main()"       */
61 /*    function.  It is important to note that this routine never          */
62 /*    returns.  The processing of this function is relatively simple:     */
63 /*    it calls several ThreadX initialization functions (if needed),      */
64 /*    calls the application define function, and then invokes the         */
65 /*    scheduler.                                                          */
66 /*                                                                        */
67 /*  INPUT                                                                 */
68 /*                                                                        */
69 /*    None                                                                */
70 /*                                                                        */
71 /*  OUTPUT                                                                */
72 /*                                                                        */
73 /*    None                                                                */
74 /*                                                                        */
75 /*  CALLS                                                                 */
76 /*                                                                        */
77 /*    _tx_initialize_low_level          Low-level initialization          */
78 /*    _tx_initialize_high_level         High-level initialization         */
79 /*    tx_application_define             Application define function       */
80 /*    _tx_thread_scheduler              ThreadX scheduling loop           */
81 /*                                                                        */
82 /*  CALLED BY                                                             */
83 /*                                                                        */
84 /*    main                              Application main program          */
85 /*                                                                        */
86 /*  RELEASE HISTORY                                                       */
87 /*                                                                        */
88 /*    DATE              NAME                      DESCRIPTION             */
89 /*                                                                        */
90 /*  05-19-2020      William E. Lamie        Initial Version 6.0           */
91 /*  09-30-2020      Yuxin Zhou              Modified comment(s),          */
92 /*                                            resulting in version 6.1    */
93 /*  04-25-2022      Scott Larson            Modified comment(s),          */
94 /*                                            added EPK initialization,   */
95 /*                                            resulting in version 6.1.11 */
96 /*  10-31-2023      Xiuwen Cai              Modified comment(s),          */
97 /*                                            added random generator      */
98 /*                                            initialization,             */
99 /*                                            resulting in version 6.3.0  */
100 /*                                                                        */
101 /**************************************************************************/
_tx_initialize_kernel_enter(VOID)102 VOID  _tx_initialize_kernel_enter(VOID)
103 {
104 
105     /* Determine if the compiler has pre-initialized ThreadX.  */
106     if (_tx_thread_system_state != TX_INITIALIZE_ALMOST_DONE)
107     {
108 
109         /* No, the initialization still needs to take place.  */
110 
111         /* Ensure that the system state variable is set to indicate
112            initialization is in progress.  Note that this variable is
113            later used to represent interrupt nesting.  */
114         _tx_thread_system_state =  TX_INITIALIZE_IN_PROGRESS;
115 
116         /* Call any port specific preprocessing.  */
117         TX_PORT_SPECIFIC_PRE_INITIALIZATION
118 
119         /* Invoke the low-level initialization to handle all processor specific
120            initialization issues.  */
121         _tx_initialize_low_level();
122 
123         /* Invoke the high-level initialization to exercise all of the
124            ThreadX components and the application's initialization
125            function.  */
126         _tx_initialize_high_level();
127 
128         /* Call any port specific post-processing.  */
129         TX_PORT_SPECIFIC_POST_INITIALIZATION
130     }
131 
132     /* Optional processing extension.  */
133     TX_INITIALIZE_KERNEL_ENTER_EXTENSION
134 
135     /* Ensure that the system state variable is set to indicate
136        initialization is in progress.  Note that this variable is
137        later used to represent interrupt nesting.  */
138     _tx_thread_system_state =  TX_INITIALIZE_IN_PROGRESS;
139 
140     /* Optional random number generator initialization.  */
141     TX_INITIALIZE_RANDOM_GENERATOR_INITIALIZATION
142 
143     /* Call the application provided initialization function.  Pass the
144        first available memory address to it.  */
145     tx_application_define(_tx_initialize_unused_memory);
146 
147     /* Set the system state in preparation for entering the thread
148        scheduler.  */
149     _tx_thread_system_state =  TX_INITIALIZE_IS_FINISHED;
150 
151     /* Call any port specific pre-scheduler processing.  */
152     TX_PORT_SPECIFIC_PRE_SCHEDULER_INITIALIZATION
153 
154 #if defined(TX_ENABLE_EXECUTION_CHANGE_NOTIFY) || defined(TX_EXECUTION_PROFILE_ENABLE)
155     /* Initialize Execution Profile Kit.  */
156     _tx_execution_initialize();
157 #endif
158 
159     /* Enter the scheduling loop to start executing threads!  */
160     _tx_thread_schedule();
161 
162 #ifdef TX_SAFETY_CRITICAL
163 
164     /* If we ever get here, raise safety critical exception.  */
165     TX_SAFETY_CRITICAL_EXCEPTION(__FILE__, __LINE__, 0);
166 #endif
167 }
168 
169