1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
|
/***************************************************************************
* Copyright (c) 2024 Microsoft Corporation
* Copyright (c) 2026-present Eclipse ThreadX contributors
*
* This program and the accompanying materials are made available under the
* terms of the MIT License which is available at
* https://opensource.org/licenses/MIT.
*
* SPDX-License-Identifier: MIT
**************************************************************************/
/**************************************************************************/
/**************************************************************************/
/** */
/** Thread-Metric Component */
/** */
/** Porting Layer (ThreadX Example) */
/** */
/**************************************************************************/
/**************************************************************************/
/* Turn off ThreadX error checking. */
#ifndef TX_DISABLE_ERROR_CHECKING
#define TX_DISABLE_ERROR_CHECKING
#endif
/* For smallest size, the ThreadX library and application code should be built
with the following options defined (easiest to add in tx_port.h):
#define TX_ENABLE_EXECUTION_CHANGE_NOTIFY
#define TX_DISABLE_PREEMPTION_THRESHOLD
#define TX_DISABLE_NOTIFY_CALLBACKS
#define TX_DISABLE_REDUNDANT_CLEARING
#define TX_DISABLE_STACK_FILLING
#define TX_NOT_INTERRUPTABLE
#define TX_TIMER_PROCESS_IN_ISR
For the fastest performance, these additional options should also be used:
#define TX_REACTIVATE_INLINE
#define TX_INLINE_THREAD_RESUME_SUSPEND
*/
/* Include necessary files. */
#include "tx_api.h"
#include "tm_api.h"
/* Define ThreadX mapping constants. */
#define TM_THREADX_MAX_THREADS 10
#define TM_THREADX_MAX_QUEUES 1
#define TM_THREADX_MAX_SEMAPHORES 1
#define TM_THREADX_MAX_MEMORY_POOLS 1
/* Define the default ThreadX stack size. */
#define TM_THREADX_THREAD_STACK_SIZE 2096
/* Define the default ThreadX queue size. */
#define TM_THREADX_QUEUE_SIZE 200
/* Define the default ThreadX memory pool size. */
#define TM_THREADX_MEMORY_POOL_SIZE 2048
/* Define the number of timer interrupt ticks per second. */
#define TM_THREADX_TICKS_PER_SECOND 100
/* Define ThreadX data structures. */
TX_THREAD tm_thread_array[TM_THREADX_MAX_THREADS];
TX_QUEUE tm_queue_array[TM_THREADX_MAX_QUEUES];
TX_SEMAPHORE tm_semaphore_array[TM_THREADX_MAX_SEMAPHORES];
TX_BLOCK_POOL tm_block_pool_array[TM_THREADX_MAX_MEMORY_POOLS];
/* Define ThreadX object data areas. */
unsigned char tm_thread_stack_area[TM_THREADX_MAX_THREADS*TM_THREADX_THREAD_STACK_SIZE];
unsigned char tm_queue_memory_area[TM_THREADX_MAX_QUEUES*TM_THREADX_QUEUE_SIZE];
unsigned char tm_pool_memory_area[TM_THREADX_MAX_MEMORY_POOLS*TM_THREADX_MEMORY_POOL_SIZE];
/* Define array to remember the test entry function. */
void *tm_thread_entry_functions[TM_THREADX_MAX_THREADS];
/* Remember the test initialization function. */
void (*tm_initialization_function)(void);
/* Define our shell entry function to match ThreadX. */
VOID tm_thread_entry(ULONG thread_input);
/* This function called from main performs basic RTOS initialization,
calls the test initialization function, and then starts the RTOS function. */
void tm_initialize(void (*test_initialization_function)(void))
{
/* Save the test initialization function. */
tm_initialization_function = test_initialization_function;
/* Call the previously defined initialization function. */
(tm_initialization_function)();
}
/* This function takes a thread ID and priority and attempts to create the
file in the underlying RTOS. Valid priorities range from 1 through 31,
where 1 is the highest priority and 31 is the lowest. If successful,
the function should return TM_SUCCESS. Otherwise, TM_ERROR should be returned. */
int tm_thread_create(int thread_id, int priority, void (*entry_function)(void))
{
UINT status;
/* Remember the actual thread entry. */
tm_thread_entry_functions[thread_id] = (void *) entry_function;
/* Create the thread under ThreadX. */
status = tx_thread_create(&tm_thread_array[thread_id], "Thread-Metric test", tm_thread_entry, (ULONG) thread_id,
&tm_thread_stack_area[thread_id*TM_THREADX_THREAD_STACK_SIZE], TM_THREADX_THREAD_STACK_SIZE,
(UINT) priority, (UINT) priority, TX_NO_TIME_SLICE, TX_DONT_START);
/* Determine if the thread create was successful. */
if (status == TX_SUCCESS)
return(TM_SUCCESS);
else
return(TM_ERROR);
}
/* This function resumes the specified thread. If successful, the function should
return TM_SUCCESS. Otherwise, TM_ERROR should be returned. */
int tm_thread_resume(int thread_id)
{
UINT status;
/* Attempt to resume the thread. */
status = tx_thread_resume(&tm_thread_array[thread_id]);
/* Determine if the thread resume was successful. */
if (status == TX_SUCCESS)
return(TM_SUCCESS);
else
return(TM_ERROR);
}
/* This function suspends the specified thread. If successful, the function should
return TM_SUCCESS. Otherwise, TM_ERROR should be returned. */
int tm_thread_suspend(int thread_id)
{
UINT status;
/* Attempt to suspend the thread. */
status = tx_thread_suspend(&tm_thread_array[thread_id]);
/* Determine if the thread suspend was successful. */
if (status == TX_SUCCESS)
return(TM_SUCCESS);
else
return(TM_ERROR);
}
/* This function relinquishes to other ready threads at the same
priority. */
void tm_thread_relinquish(void)
{
/* Relinquish to other threads at the same priority. */
tx_thread_relinquish();
}
/* This function suspends the specified thread for the specified number
of seconds. If successful, the function should return TM_SUCCESS.
Otherwise, TM_ERROR should be returned. */
void tm_thread_sleep(int seconds)
{
/* Attempt to sleep. */
tx_thread_sleep(((UINT) seconds)*TM_THREADX_TICKS_PER_SECOND);
}
/* This function creates the specified queue. If successful, the function should
return TM_SUCCESS. Otherwise, TM_ERROR should be returned. */
int tm_queue_create(int queue_id)
{
UINT status;
/* Create the specified queue with 16-byte messages. */
status = tx_queue_create(&tm_queue_array[queue_id], "Thread-Metric test", TX_4_ULONG,
&tm_queue_memory_area[queue_id*TM_THREADX_QUEUE_SIZE], TM_THREADX_QUEUE_SIZE);
/* Determine if the queue create was successful. */
if (status == TX_SUCCESS)
return(TM_SUCCESS);
else
return(TM_ERROR);
}
/* This function sends a 16-byte message to the specified queue. If successful,
the function should return TM_SUCCESS. Otherwise, TM_ERROR should be returned. */
int tm_queue_send(int queue_id, unsigned long *message_ptr)
{
UINT status;
/* Send the 16-byte message to the specified queue. */
status = tx_queue_send(&tm_queue_array[queue_id], message_ptr, TX_NO_WAIT);
/* Determine if the queue send was successful. */
if (status == TX_SUCCESS)
return(TM_SUCCESS);
else
return(TM_ERROR);
}
/* This function receives a 16-byte message from the specified queue. If successful,
the function should return TM_SUCCESS. Otherwise, TM_ERROR should be returned. */
int tm_queue_receive(int queue_id, unsigned long *message_ptr)
{
UINT status;
/* Receive a 16-byte message from the specified queue. */
status = tx_queue_receive(&tm_queue_array[queue_id], message_ptr, TX_NO_WAIT);
/* Determine if the queue receive was successful. */
if (status == TX_SUCCESS)
return(TM_SUCCESS);
else
return(TM_ERROR);
}
/* This function creates the specified semaphore. If successful, the function should
return TM_SUCCESS. Otherwise, TM_ERROR should be returned. */
int tm_semaphore_create(int semaphore_id)
{
UINT status;
/* Create semaphore. */
status = tx_semaphore_create(&tm_semaphore_array[semaphore_id], "Thread-Metric test", 1);
/* Determine if the semaphore create was successful. */
if (status == TX_SUCCESS)
return(TM_SUCCESS);
else
return(TM_ERROR);
}
/* This function gets the specified semaphore. If successful, the function should
return TM_SUCCESS. Otherwise, TM_ERROR should be returned. */
int tm_semaphore_get(int semaphore_id)
{
UINT status;
/* Get the semaphore. */
status = tx_semaphore_get(&tm_semaphore_array[semaphore_id], TX_NO_WAIT);
/* Determine if the semaphore get was successful. */
if (status == TX_SUCCESS)
return(TM_SUCCESS);
else
return(TM_ERROR);
}
/* This function puts the specified semaphore. If successful, the function should
return TM_SUCCESS. Otherwise, TM_ERROR should be returned. */
int tm_semaphore_put(int semaphore_id)
{
UINT status;
/* Put the semaphore. */
status = tx_semaphore_put(&tm_semaphore_array[semaphore_id]);
/* Determine if the semaphore put was successful. */
if (status == TX_SUCCESS)
return(TM_SUCCESS);
else
return(TM_ERROR);
}
/* This function creates the specified memory pool that can support one or more
allocations of 128 bytes. If successful, the function should
return TM_SUCCESS. Otherwise, TM_ERROR should be returned. */
int tm_memory_pool_create(int pool_id)
{
UINT status;
/* Create the memory pool. */
status = tx_block_pool_create(&tm_block_pool_array[pool_id], "Thread-Metric test", 128, &tm_pool_memory_area[pool_id*TM_THREADX_MEMORY_POOL_SIZE], TM_THREADX_MEMORY_POOL_SIZE);
/* Determine if the block pool memory was successful. */
if (status == TX_SUCCESS)
return(TM_SUCCESS);
else
return(TM_ERROR);
}
/* This function allocates a 128 byte block from the specified memory pool.
If successful, the function should return TM_SUCCESS. Otherwise, TM_ERROR
should be returned. */
int tm_memory_pool_allocate(int pool_id, unsigned char **memory_ptr)
{
UINT status;
/* Allocate a 128-byte block from the specified memory pool. */
status = tx_block_allocate(&tm_block_pool_array[pool_id], (void **) memory_ptr, TX_NO_WAIT);
/* Determine if the block pool allocate was successful. */
if (status == TX_SUCCESS)
return(TM_SUCCESS);
else
return(TM_ERROR);
}
/* This function releases a previously allocated 128 byte block from the specified
memory pool. If successful, the function should return TM_SUCCESS. Otherwise, TM_ERROR
should be returned. */
int tm_memory_pool_deallocate(int pool_id, unsigned char *memory_ptr)
{
UINT status;
/* Release the 128-byte block back to the specified memory pool. */
status = tx_block_release((void *) memory_ptr);
/* Determine if the block pool release was successful. */
if (status == TX_SUCCESS)
return(TM_SUCCESS);
else
return(TM_ERROR);
}
/* This is the ThreadX thread entry. It is going to call the Thread-Metric
entry function saved earlier. */
VOID tm_thread_entry(ULONG thread_input)
{
void (*entry_function)(void);
/* Pickup the entry function from the saved array. */
entry_function = (void (*)(void)) tm_thread_entry_functions[thread_input];
/* Call the entry function. */
(entry_function)();
}
|