1 Microsoft's Azure RTOS ThreadX for Linux 2 3 Using the GNU GCC Tools 4 51. Building the ThreadX run-time Library 6 7First make sure you are in the "example_build" directory. Also, make sure that 8you have setup your path and other environment variables necessary for the GNU 9development environment. The following command retrieves and installs GCC 10multilib on a Ubuntu system: 11 12sudo apt-get install gcc-multilib 13 14At this point you may run the GNU make command to build the ThreadX core 15library. This will build the ThreadX run-time environment in the 16"example_build" directory. 17 18 make tx.a 19 20you should now observe the compilation of the ThreadX library source. At the 21end of the make, they are all combined into the run-time library file: tx.a. 22This file must be linked with your application in order to use ThreadX. 23 24 252. Demonstration System 26 27Building the demonstration is easy; simply execute the GNU make command while 28inside the "example_build" directory. 29 30 make sample_threadx 31 32You should observe the compilation of sample_threadx.c (which is the demonstration 33application) and linking with tx.a. The resulting file DEMO is a binary file 34that can be executed. 35 36 373. System Initialization 38 39The system entry point is at main(), which is defined in the application. 40Once the application calls tx_kernel_enter, ThreadX starts running and 41performs various initialization duties prior to starting the scheduler. The 42Linux-specific initialization is done in the function _tx_initialize_low_level, 43which is located in the file tx_initialize_low_level.c. This function is 44responsible for setting up various system data structures and simulated 45interrupts - including the periodic timer interrupt source for ThreadX. 46 47In addition, _tx_initialize_low_level determines the first available 48address for use by the application. In Linux, this is basically done 49by using malloc to get a big block of memory from Linux. 50 51 524. Linux Implementation 53 54ThreadX for Linux is implemented using POSIX pthreads. Each application 55thread in ThreadX actually runs as a Linux pthread. The determination of 56which application thread to run is made by the ThreadX scheduler, which 57itself is a Linux pthread. The ThreadX scheduler is the highest priority 58thread in the system. 59 60Interrupts in ThreadX/Linux are also simulated by pthreads. A good example 61is the ThreadX system timer interrupt, which can be found in 62tx_initialize_low_level.c. 63 64ThreadX for linux utilizes the API pthread_setschedparam() which requires 65the ThreadX application running with privilege. The following command is used 66to run a ThreadX application: 67 68./sample_threadx 69 705. Improving Performance 71 72The distribution version of ThreadX is built without any compiler 73optimizations. This makes it easy to debug because you can trace or set 74breakpoints inside of ThreadX itself. Of course, this costs some 75performance. To make it run faster, you can change the makefile to 76enable all compiler optimizations. In addition, you can eliminate the 77ThreadX basic API error checking by compiling your application code with the 78symbol TX_DISABLE_ERROR_CHECKING defined. 79 80 816. Interrupt Handling 82 83ThreadX provides simulated interrupt handling with Linux pthreads. Simulated 84interrupt threads may be created by the application or may be added to the 85simulated timer interrupt defined in tx_initialize_low_level.c. The following 86format for creating simulated interrupts should be used: 87 886.1 Data structures 89 90Here is an example of how to define the Linux data structures and prototypes 91necessary to create a simulated interrupt thread: 92 93pthread_t _sample_linux_interrupt_thread; 94void *_sample_linux_interrupt_entry(void *p); 95 966.2 Creating a Simulated Interrupt Thread 97 98Here is an example of how to create a simulated interrupt thread in Linux. 99This may be done inside of tx_initialize_low_level.c or from your application code 100 101 102struct sched_param sp; 103 104 /* Create the ISR thread */ 105 pthread_create(&_sample_linux_interrupt_thread, NULL, _sample_linux_interrupt_entry, &_sample_linux_interrupt_thread); 106 107 /* Set up the ISR priority */ 108 sp.sched_priority = TX_LINUX_PRIORITY_ISR; 109 pthread_setschedparam(_sample_linux_interrupt_thread, SCHED_FIFO, &sp); 110 111 112 1136.3 Simulated Interrupt Thread Template 114 115The following is a template for the simulated interrupt thread. This interrupt will occur on 116a periodic basis. 117 118void *_sample_linux_interrupt_entry(void *p) 119{ 120struct timespec ts; 121 122 while(1) 123 { 124 125 ts.tv_sec = 0; 126 ts.tv_nsec = 10000; 127 while(nanosleep(&ts, &ts)); 128 129 /* Call ThreadX context save for interrupt preparation. */ 130 _tx_thread_context_save(); 131 132 /* Call the real ISR routine */ 133 _sample_linux_interrupt_isr(); 134 135 /* Call ThreadX context restore for interrupt completion. */ 136 _tx_thread_context_restore(); 137 } 138} 139 140 141 1427. 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 6.1 version for Linux using GNU GCC tools. 149 150 151Copyright(c) 1996-2020 Microsoft Corporation 152 153 154https://azure.com/rtos 155 156