1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
24
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
78
79
80
81
82
88
89
90
91
92
97
98
99
106
107
108
113
114
115
120
121
122
123
128
129
130
131
132
133
134
135
136
137
138
139
140
141
146
147
148
149
150
151
152
153
154
155
156
160
161
162
163
164
168
169
170
171
172
176
177
178
179
180
184
185
186
187
188
192
193
194
195
196
200
201
202
203
204
208
209
210
211
212
213
214
215
216
217
218
219
220
221
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
245
246
247
248
249
250
251
255
256
257
258
259
263
264
265
269
270
271
276
277
278
279
280
281
282
283
286
289
292
295
298
301
302
/* ... */
#include "app_threadx.h"
Private includes
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
#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
TX_THREAD ModuleManager;
TXM_MODULE_INSTANCE my_module;
TX_QUEUE ResidentQueue;
UCHAR module_data_area[MODULE_DATA_SIZE];
UCHAR object_memory[OBJECT_MEM_SIZE];
ULONG memory_faults = 0;
static UCHAR tx_byte_pool_buffer[TX_APP_MEM_POOL_SIZE];
static TX_BYTE_POOL ModuleManagerBytePool;
Private variables
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);
/* ... */
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)
{
Error_Handler();
}if (tx_byte_pool_create(&ModuleManagerBytePool, "Module Manager Byte Pool", tx_byte_pool_buffer, TX_APP_MEM_POOL_SIZE) != TX_SUCCESS) { ... }
else
{
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) { ... }
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) { ... }
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) { ... }
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 { ... }
}{ ... }
/* ... */
void MX_AZURE_RTOS_Init(void)
{
tx_kernel_enter();
}{ ... }
/* ... */
VOID module_manager_entry(ULONG thread_input)
{
UINT status;
CHAR p_msg[64];
ULONG r_msg = PROCESSING_NOT_STARTED;
ULONG module_properties;
status = txm_module_manager_initialize((VOID *) module_data_area, MODULE_DATA_SIZE);
if(status != TX_SUCCESS)
{
Error_Handler();
}if (status != TX_SUCCESS) { ... }
status = txm_module_manager_object_pool_create(object_memory, OBJECT_MEM_SIZE);
if(status != TX_SUCCESS)
{
Error_Handler();
}if (status != TX_SUCCESS) { ... }
status = txm_module_manager_memory_fault_notify(module_fault_handler);
if(status != TX_SUCCESS)
{
Error_Handler();
}if (status != TX_SUCCESS) { ... }
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) { ... }
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) { ... }
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) { ... }
status = txm_module_manager_properties_get(&my_module, &module_properties);
if(status != TX_SUCCESS)
{
Error_Handler();
}if (status != TX_SUCCESS) { ... }
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");
status = txm_module_manager_start(&my_module);
if(status != TX_SUCCESS)
{
Error_Handler();
}if (status != TX_SUCCESS) { ... }
printf("Module execution is started\n");
while(r_msg != PROCESSING_FINISHED)
{
if(tx_queue_receive(&ResidentQueue, &r_msg, TX_TIMER_TICKS_PER_SECOND) == TX_SUCCESS)
{
pretty_msg(p_msg, r_msg);
printf("Module is executing: %s\n", p_msg);
if(memory_faults)
{
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) { ... }
status = txm_module_manager_stop(&my_module);
if(status != TX_SUCCESS)
{
Error_Handler();
}if (status != TX_SUCCESS) { ... }
status = txm_module_manager_unload(&my_module);
if(status != TX_SUCCESS)
{
Error_Handler();
}if (status != TX_SUCCESS) { ... }
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)
{
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) { ... }
}{ ... }