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.1.11       */
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 /*                                                                        */
97 /**************************************************************************/
_tx_initialize_kernel_enter(VOID)98 VOID  _tx_initialize_kernel_enter(VOID)
99 {
100 
101     /* Determine if the compiler has pre-initialized ThreadX.  */
102     if (_tx_thread_system_state != TX_INITIALIZE_ALMOST_DONE)
103     {
104 
105         /* No, the initialization still needs to take place.  */
106 
107         /* Ensure that the system state variable is set to indicate
108            initialization is in progress.  Note that this variable is
109            later used to represent interrupt nesting.  */
110         _tx_thread_system_state =  TX_INITIALIZE_IN_PROGRESS;
111 
112         /* Call any port specific preprocessing.  */
113         TX_PORT_SPECIFIC_PRE_INITIALIZATION
114 
115         /* Invoke the low-level initialization to handle all processor specific
116            initialization issues.  */
117         _tx_initialize_low_level();
118 
119         /* Invoke the high-level initialization to exercise all of the
120            ThreadX components and the application's initialization
121            function.  */
122         _tx_initialize_high_level();
123 
124         /* Call any port specific post-processing.  */
125         TX_PORT_SPECIFIC_POST_INITIALIZATION
126     }
127 
128     /* Optional processing extension.  */
129     TX_INITIALIZE_KERNEL_ENTER_EXTENSION
130 
131     /* Ensure that the system state variable is set to indicate
132        initialization is in progress.  Note that this variable is
133        later used to represent interrupt nesting.  */
134     _tx_thread_system_state =  TX_INITIALIZE_IN_PROGRESS;
135 
136     /* Call the application provided initialization function.  Pass the
137        first available memory address to it.  */
138     tx_application_define(_tx_initialize_unused_memory);
139 
140     /* Set the system state in preparation for entering the thread
141        scheduler.  */
142     _tx_thread_system_state =  TX_INITIALIZE_IS_FINISHED;
143 
144     /* Call any port specific pre-scheduler processing.  */
145     TX_PORT_SPECIFIC_PRE_SCHEDULER_INITIALIZATION
146 
147 #if defined(TX_ENABLE_EXECUTION_CHANGE_NOTIFY) || defined(TX_EXECUTION_PROFILE_ENABLE)
148     /* Initialize Execution Profile Kit.  */
149     _tx_execution_initialize();
150 #endif
151 
152     /* Enter the scheduling loop to start executing threads!  */
153     _tx_thread_schedule();
154 
155 #ifdef TX_SAFETY_CRITICAL
156 
157     /* If we ever get here, raise safety critical exception.  */
158     TX_SAFETY_CRITICAL_EXCEPTION(__FILE__, __LINE__, 0);
159 #endif
160 }
161 
162