1 Microsoft's Azure RTOS ThreadX for Win32 2 3 Using the Visual Studio Tools 4 5 61. Open the ThreadX Project Workspace 7 8In order to build the ThreadX library and the ThreadX demonstration first load 9the Azure RTOS Workspace azure_rtos.sln, which is located inside the "example_build" 10directory. 11 12 132. Building the ThreadX run-time Library 14 15Building the ThreadX library is easy; simply make the "tx" project active and 16then select the build button. You should now observe the compilation of the 17ThreadX library source. This project build produces the ThreadX library file 18tx.lib. 19 20 213. Building the Demonstration System 22 23You are now ready to run the ThreadX Win32 demonstration. Simply make the 24"sample_thread" project active and then select the build button. When the build 25is finished, select the run button from Visual Studio and observe various 26demonstration statistics being printed to the console window. You may also set 27breakpoints, single step, perform data watches, etc. 28 29 304. System Initialization 31 32The system entry point is at main(), which is defined in the application. 33Once the application calls tx_kernel_enter, ThreadX starts running and 34performs various initialization duties prior to starting the scheduler. The 35Win32-specific initialization is done in the function _tx_initialize_low_level, 36which is located in the file tx_initialize_low_level.c. This function is 37responsible for setting up various system data structures and simulated 38interrupts - including the periodic timer interrupt source for ThreadX. 39 40In addition, _tx_initialize_low_level determines the first available 41address for use by the application. In Win32, this is basically done 42by using malloc to get a big block of memory from Windows. 43 44 455. Win32 Implementation 46 47ThreadX for Win32 is implemented using Win32 threads. Each application 48thread in ThreadX actually runs as a Win32 thread. The determination of 49which application thread to run is made by the ThreadX scheduler, which 50itself is a Win32 thread. The ThreadX scheduler is the highest priority 51thread in the system. 52 53Interrupts in ThreadX/Win32 are also simulated by threads. A good example 54is the ThreadX system timer interrupt, which can be found in 55tx_initialize_low_level.c. 56 575.1 ThreadX Limitations 58 59ThreadX for Win32 behaves in the same manner as ThreadX in an embedded 60environment EXCEPT in the case of thread termination. Unfortunately, the 61Win32 API does not have a good mechanism to terminate threads and instead 62must rely on the thread itself terminating. Hence, threads in the ThreadX 63Win32 implementation must have some ThreadX call periodically in their 64processing if they can be terminated by another ThreadX thread. 65 66 676. Improving Performance 68 69The distribution version of ThreadX is built without any compiler 70optimizations. This makes it easy to debug because you can trace or set 71breakpoints inside of ThreadX itself. Of course, this costs some 72performance. To make it run faster, you can change the tx project file to 73enable all compiler optimizations. In addition, you can eliminate the 74ThreadX basic API error checking by compiling your application code with the 75symbol TX_DISABLE_ERROR_CHECKING defined. 76 77 787. Interrupt Handling 79 80ThreadX provides simulated interrupt handling with Win32 threads. Simulated 81interrupt threads may be created by the application or may be added to the 82simulated timer interrupt defined in tx_initialize_low_level.c. The following 83format for creating simulated interrupts should be used: 84 857.1 Data structures 86 87Here is an example of how to define the Win32 data structures and prototypes 88necessary to create a simulated interrupt thread: 89 90HANDLE _sample_win32_interrupt_handle; 91DWORD _sample_win32_interrupt_id; 92DWORD WINAPI _sample_win32_interrupt(LPVOID); 93 94 957.2 Creating a Simulated Interrupt Thread 96 97Here is an example of how to create a simulated interrupt thread in Win32. 98This may be done inside of tx_initialize_low_level.c or from your application code 99 100 _sample_win32_interrupt_handle = 101 CreateThread(NULL, 0, _sample_win32_interrupt, (LPVOID) &_sample_win32_interrupt_handle, 102 CREATE_SUSPENDED, &_sample_win32_interrupt_id); 103 104 SetThreadPriority(_sample_win32_interrupt_handle, THREAD_PRIORITY_BELOW_NORMAL); 105 106 1077.3 Activating the Simulated Interrupt Thread 108 109Simulated interrupt threads should not be activated until initialization is complete, i.e. until 110ThreadX is ready to schedule threads. The following activation code may be added to the routine 111in tx_initialize_low_level.c called _tx_initialize_start_interrupts or into the application code directly: 112 113 ResumeThread(_sample_win32_interrupt_handle); 114 115 1167.4 Simulated Interrupt Thread Template 117 118The following is a template for the simulated interrupt thread. This interrupt will occur on 119a periodic basis. 120 121DWORD WINAPI _sample_win32_interrupt(LPVOID *ptr) 122{ 123 124 125 while(1) 126 { 127 128 /* Sleep for the desired time. */ 129 Sleep(18); 130 131 /* Call ThreadX context save for interrupt preparation. */ 132 _tx_thread_context_save(); 133 134 /* Call application ISR here! */ 135 136 /* Call ThreadX context restore for interrupt completion. */ 137 _tx_thread_context_restore(); 138 } 139} 140 141 1428. Revision History 143 144For generic code revision information, please refer to the readme_threadx_generic.txt 145file, which is included in your distribution. The following details the revision 146information associated with this specific port of ThreadX: 147 14809-30-2020 Initial ThreadX version for Win32 using Microsoft Visual C/C++. 149 150 151Copyright(c) 1996-2020 Microsoft Corporation 152 153 154https://azure.com/rtos 155 156