Select one of the symbols to view example projects that use it.
 
Outline
Private includes
#include "app_threadx.h"
Private typedef
ProgressState
Private define
#define DEFAULT_STACK_SIZE
#define MODULE_DATA_SIZE
#define OBJECT_MEM_SIZE
#define READONLY_REGION
#define READWRITE_REGION
#define SHARED_MEM_SIZE
#define MODULE_FLASH_ADDRESS
Private variables
ModuleManager
my_module
ResidentQueue
module_data_area
object_memory
memory_faults
tx_byte_pool_buffer
ModuleManagerBytePool
tx_application_define(void *)
MX_AZURE_RTOS_Init()
module_manager_entry(ULONG)
module_fault_handler(TX_THREAD *, TXM_MODULE_INSTANCE *)
pretty_msg(char *, ULONG)
Files
loading...
CodeScopeSTM32 Libraries and SamplesTx_ModuleManagerSrc/app_threadx.c
 
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
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
/** ****************************************************************************** * @file app_threadx.c * @author MCD Application Team * @brief ThreadX applicative file ****************************************************************************** * @attention * * Copyright (c) 2021 STMicroelectronics. * All rights reserved. * * This software is licensed under terms that can be found in the LICENSE file * in the root directory of this software component. * If no LICENSE file comes with this software, it is provided AS-IS. * ****************************************************************************** *//* ... */ /* Includes ------------------------------------------------------------------*/ /* Private includes ----------------------------------------------------------*/ #include "app_threadx.h" Private includes /* Private typedef -----------------------------------------------------------*/ typedef enum { PROCESSING_NOT_STARTED = 99, WRITING_TO_READWRITE = 88, WRITING_TO_READONLY = 77, READING_FROM_READWRITE = 66, READING_FROM_READONLY = 55, PROCESSING_FINISHED = 44 ...} ProgressState; Private typedef /* Private define ------------------------------------------------------------*/ #define DEFAULT_STACK_SIZE 1024 #define MODULE_DATA_SIZE 32*1024 #define OBJECT_MEM_SIZE 16*1024 #define READONLY_REGION 0x20010000 #define READWRITE_REGION 0x20010100 #define SHARED_MEM_SIZE 0xFF #define MODULE_FLASH_ADDRESS 0x08020000 7 defines Private define /* Private macro -------------------------------------------------------------*/ /* Private variables ---------------------------------------------------------*/ /* Define the ThreadX object control blocks */ TX_THREAD ModuleManager; TXM_MODULE_INSTANCE my_module; TX_QUEUE ResidentQueue; /* Define the module data pool area. */ UCHAR module_data_area[MODULE_DATA_SIZE]; /* Define the object pool area. */ UCHAR object_memory[OBJECT_MEM_SIZE]; /* Define the count of memory faults. */ ULONG memory_faults = 0; static UCHAR tx_byte_pool_buffer[TX_APP_MEM_POOL_SIZE]; static TX_BYTE_POOL ModuleManagerBytePool; Private variables /* Private function prototypes -----------------------------------------------*/ void Error_Handler(void); VOID pretty_msg(char *p_msg, ULONG r_msg); VOID module_manager_entry(ULONG thread_input); VOID module_fault_handler(TX_THREAD *thread, TXM_MODULE_INSTANCE *module); /** * @brief Application ThreadX Initialization. * @param memory_ptr: memory pointer * @retval none *//* ... */ VOID tx_application_define(VOID *first_unused_memory) { CHAR *pointer; if (tx_byte_pool_create(&ModuleManagerBytePool, "Module Manager Byte Pool", tx_byte_pool_buffer, TX_APP_MEM_POOL_SIZE) != TX_SUCCESS) { /* USER CODE BEGIN TX_Byte_Pool_Error */ Error_Handler(); /* USER CODE END TX_Byte_Pool_Error */ }if (tx_byte_pool_create(&ModuleManagerBytePool, "Module Manager Byte Pool", tx_byte_pool_buffer, TX_APP_MEM_POOL_SIZE) != TX_SUCCESS) { ... } else { /* Allocate the stack for Module Manager Thread. */ if (tx_byte_allocate(&ModuleManagerBytePool, (VOID **) &pointer, DEFAULT_STACK_SIZE, TX_NO_WAIT) != TX_SUCCESS) { Error_Handler(); }if (tx_byte_allocate(&ModuleManagerBytePool, (VOID **) &pointer, DEFAULT_STACK_SIZE, TX_NO_WAIT) != TX_SUCCESS) { ... } /* Create Module Manager Thread. */ if (tx_thread_create(&ModuleManager, "Module Manager Thread", module_manager_entry, 0, pointer, DEFAULT_STACK_SIZE, MODULE_MANAGER_THREAD_PRIO, MODULE_MANAGER_THREAD_PREEMPTION_THRESHOLD, TX_NO_TIME_SLICE, TX_AUTO_START) != TX_SUCCESS) { Error_Handler(); }if (tx_thread_create(&ModuleManager, "Module Manager Thread", module_manager_entry, 0, pointer, DEFAULT_STACK_SIZE, MODULE_MANAGER_THREAD_PRIO, MODULE_MANAGER_THREAD_PREEMPTION_THRESHOLD, TX_NO_TIME_SLICE, TX_AUTO_START) != TX_SUCCESS) { ... } /* Allocate the stack for resident_queue. */ if (tx_byte_allocate(&ModuleManagerBytePool, (VOID **) &pointer, 16 * sizeof(ULONG), TX_NO_WAIT) != TX_SUCCESS) { Error_Handler(); }if (tx_byte_allocate(&ModuleManagerBytePool, (VOID **) &pointer, 16 * sizeof(ULONG), TX_NO_WAIT) != TX_SUCCESS) { ... } /* Create the resident_queue */ if (tx_queue_create(&ResidentQueue, "Resident Queue", TX_1_ULONG, pointer, 16 * sizeof(ULONG)) != TX_SUCCESS) { Error_Handler(); }if (tx_queue_create(&ResidentQueue, "Resident Queue", TX_1_ULONG, pointer, 16 * sizeof(ULONG)) != TX_SUCCESS) { ... } }else { ... } }{ ... } /** * @brief MX_AZURE_RTOS_Init * @param None * @retval None *//* ... */ void MX_AZURE_RTOS_Init(void) { /* USER CODE BEGIN Before_Kernel_Start */ /* USER CODE END Before_Kernel_Start */ tx_kernel_enter(); /* USER CODE BEGIN Kernel_Start_Error */ /* USER CODE END Kernel_Start_Error */ }{ ... } /** * @brief Module Manager main thread. * @param thread_input: thread id * @retval none *//* ... */ VOID module_manager_entry(ULONG thread_input) { UINT status; CHAR p_msg[64]; ULONG r_msg = PROCESSING_NOT_STARTED; ULONG module_properties; /* Initialize the module manager. */ status = txm_module_manager_initialize((VOID *) module_data_area, MODULE_DATA_SIZE); if(status != TX_SUCCESS) { Error_Handler(); }if (status != TX_SUCCESS) { ... } /* Create a pool for module objects. */ status = txm_module_manager_object_pool_create(object_memory, OBJECT_MEM_SIZE); if(status != TX_SUCCESS) { Error_Handler(); }if (status != TX_SUCCESS) { ... } /* Register a fault handler. */ status = txm_module_manager_memory_fault_notify(module_fault_handler); if(status != TX_SUCCESS) { Error_Handler(); }if (status != TX_SUCCESS) { ... } /* Load the module from the specified address */ status = txm_module_manager_in_place_load(&my_module, "my module", (VOID *) MODULE_FLASH_ADDRESS); if(status != TX_SUCCESS) { Error_Handler(); }if (status != TX_SUCCESS) { ... } /* Enable shared memory region for module with read-only access permission. */ status = txm_module_manager_external_memory_enable(&my_module, (void*)READONLY_REGION, SHARED_MEM_SIZE, 0); if(status != TX_SUCCESS) { Error_Handler(); }if (status != TX_SUCCESS) { ... } /* Enable shared memory region for module with read and write access permission. */ status = txm_module_manager_external_memory_enable(&my_module, (void*)READWRITE_REGION, SHARED_MEM_SIZE, TXM_MODULE_MANAGER_SHARED_ATTRIBUTE_WRITE); if(status != TX_SUCCESS) { Error_Handler(); }if (status != TX_SUCCESS) { ... } /* Get module properties. */ status = txm_module_manager_properties_get(&my_module, &module_properties); if(status != TX_SUCCESS) { Error_Handler(); }if (status != TX_SUCCESS) { ... } /* Print loaded module info */ printf("Module <%s> is loaded from address 0x%08X\n", my_module.txm_module_instance_name, MODULE_FLASH_ADDRESS); printf("Module code section size: %i bytes, data section size: %i\n", (int)my_module.txm_module_instance_code_size, (int)my_module.txm_module_instance_data_size); printf("Module Attributes:\n"); printf(" - Compiled for %s compiler\n", ((module_properties >> 25) == 1)? "CubeIDE (GNU)" : ((module_properties >> 24) == 1)? "ARM KEIL" : "IAR EW"); printf(" - Shared/external memory access is %s\n", ((module_properties & 0x04) == 0)? "Disabled" : "Enabled"); printf(" - MPU protection is %s\n", ((module_properties & 0x02) == 0)? "Disabled" : "Enabled"); printf(" - %s mode execution is enabled for the module\n\n", ((module_properties & 0x01) == 0)? "Privileged" : "User"); /* Start the modules. */ status = txm_module_manager_start(&my_module); if(status != TX_SUCCESS) { Error_Handler(); }if (status != TX_SUCCESS) { ... } printf("Module execution is started\n"); /* Get Module's progress messages */ while(r_msg != PROCESSING_FINISHED) { if(tx_queue_receive(&ResidentQueue, &r_msg, TX_TIMER_TICKS_PER_SECOND) == TX_SUCCESS) { /* Convert the message to a user friendly string */ pretty_msg(p_msg, r_msg); printf("Module is executing: %s\n", p_msg); /* Check if the last executed operation resulted in memory violation */ if(memory_faults) { /* A memory access fault just occurred */ printf("A memory fault occurred while module executed: %s\n", p_msg); break; }if (memory_faults) { ... } }if (tx_queue_receive(&ResidentQueue, &r_msg, TX_TIMER_TICKS_PER_SECOND) == TX_SUCCESS) { ... } }while (r_msg != PROCESSING_FINISHED) { ... } /* Stop the modules. */ status = txm_module_manager_stop(&my_module); if(status != TX_SUCCESS) { Error_Handler(); }if (status != TX_SUCCESS) { ... } /* Unload the modules. */ status = txm_module_manager_unload(&my_module); if(status != TX_SUCCESS) { Error_Handler(); }if (status != TX_SUCCESS) { ... } /* Toggle green LED to indicated success of operations */ while(1) { BSP_LED_Toggle(LED_GREEN); tx_thread_sleep(TX_TIMER_TICKS_PER_SECOND / 2); }while (1) { ... } }{ ... } VOID module_fault_handler(TX_THREAD *thread, TXM_MODULE_INSTANCE *module) { /* Just increment the fault counter. */ memory_faults++; }{ ... } VOID pretty_msg(char *p_msg, ULONG r_msg) { memset(p_msg, 0, 64); switch(r_msg) { case WRITING_TO_READWRITE: memcpy(p_msg, "Writing to ReadWrite Region", 27); break;case WRITING_TO_READWRITE: case WRITING_TO_READONLY: memcpy(p_msg, "Writing to ReadOnly Region", 26); break;case WRITING_TO_READONLY: case READING_FROM_READWRITE: memcpy(p_msg, "Reading from ReadWrite Region", 29); break;case READING_FROM_READWRITE: case READING_FROM_READONLY: memcpy(p_msg, "Reading from ReadOnly Region", 28); break;case READING_FROM_READONLY: case PROCESSING_FINISHED: memcpy(p_msg, "All operations were done", 24); break;case PROCESSING_FINISHED: default: memcpy(p_msg, "Invalid option", 14); break;default }switch (r_msg) { ... } }{ ... }