Select one of the symbols to view example projects that use it.
 
Outline
Includes
#include "app_threadx.h"
Private includes
Private typedef
Private define
Private macro
Private variables
MainThread
ThreadOne
ThreadTwo
EventFlag
Private function prototypes
App_ThreadX_Init(void *)
MX_ThreadX_Init()
MainThread_Entry(ULONG)
ThreadOne_Entry(ULONG)
ThreadTwo_Entry(ULONG)
App_Delay(uint32_t)
Files
loading...
SourceVuSTM32 Libraries and SamplesTx_Thread_CreationSrc/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
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
/* USER CODE BEGIN Header */ /** ****************************************************************************** * @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. * ****************************************************************************** *//* ... */ /* USER CODE END Header */ /* Includes ------------------------------------------------------------------*/ #include "app_threadx.h" Includes /* Private includes ----------------------------------------------------------*/ /* USER CODE BEGIN Includes */ /* USER CODE END Includes */ Private includes /* Private typedef -----------------------------------------------------------*/ /* USER CODE BEGIN PTD */ /* USER CODE END PTD */ Private typedef /* Private define ------------------------------------------------------------*/ /* USER CODE BEGIN PD */ /* USER CODE END PD */ Private define /* Private macro -------------------------------------------------------------*/ /* USER CODE BEGIN PM */ /* USER CODE END PM */ Private macro /* Private variables ---------------------------------------------------------*/ /* USER CODE BEGIN PV */ TX_THREAD MainThread; TX_THREAD ThreadOne; TX_THREAD ThreadTwo; TX_EVENT_FLAGS_GROUP EventFlag; /* USER CODE END PV */ Private variables /* Private function prototypes -----------------------------------------------*/ /* USER CODE BEGIN PFP */ void ThreadOne_Entry(ULONG thread_input); void ThreadTwo_Entry(ULONG thread_input); void MainThread_Entry(ULONG thread_input); void App_Delay(uint32_t Delay); /* USER CODE END PFP */ /** * @brief Application ThreadX Initialization. * @param memory_ptr: memory pointer * @retval int *//* ... */ UINT App_ThreadX_Init(VOID *memory_ptr) { UINT ret = TX_SUCCESS; TX_BYTE_POOL *byte_pool = (TX_BYTE_POOL*)memory_ptr; /* USER CODE BEGIN App_ThreadX_Init */ CHAR *pointer; /* Allocate the stack for MainThread. */ if (tx_byte_allocate(byte_pool, (VOID **) &pointer, APP_STACK_SIZE, TX_NO_WAIT) != TX_SUCCESS) { ret = TX_POOL_ERROR; }if (tx_byte_allocate(byte_pool, (VOID **) &pointer, APP_STACK_SIZE, TX_NO_WAIT) != TX_SUCCESS) { ... } /* Create MainThread. */ if (tx_thread_create(&MainThread, "Main Thread", MainThread_Entry, 0, pointer, APP_STACK_SIZE, MAIN_THREAD_PRIO, MAIN_THREAD_PREEMPTION_THRESHOLD, TX_NO_TIME_SLICE, TX_AUTO_START) != TX_SUCCESS) { ret = TX_THREAD_ERROR; }if (tx_thread_create(&MainThread, "Main Thread", MainThread_Entry, 0, pointer, APP_STACK_SIZE, MAIN_THREAD_PRIO, MAIN_THREAD_PREEMPTION_THRESHOLD, TX_NO_TIME_SLICE, TX_AUTO_START) != TX_SUCCESS) { ... } /* Allocate the stack for ThreadOne. */ if (tx_byte_allocate(byte_pool, (VOID **) &pointer, APP_STACK_SIZE, TX_NO_WAIT) != TX_SUCCESS) { ret = TX_POOL_ERROR; }if (tx_byte_allocate(byte_pool, (VOID **) &pointer, APP_STACK_SIZE, TX_NO_WAIT) != TX_SUCCESS) { ... } /* Create ThreadOne. */ if (tx_thread_create(&ThreadOne, "Thread One", ThreadOne_Entry, 0, pointer, APP_STACK_SIZE, THREAD_ONE_PRIO, THREAD_ONE_PREEMPTION_THRESHOLD, TX_NO_TIME_SLICE, TX_AUTO_START) != TX_SUCCESS) { ret = TX_THREAD_ERROR; }if (tx_thread_create(&ThreadOne, "Thread One", ThreadOne_Entry, 0, pointer, APP_STACK_SIZE, THREAD_ONE_PRIO, THREAD_ONE_PREEMPTION_THRESHOLD, TX_NO_TIME_SLICE, TX_AUTO_START) != TX_SUCCESS) { ... } /* Allocate the stack for ThreadTwo. */ if (tx_byte_allocate(byte_pool, (VOID **) &pointer, APP_STACK_SIZE, TX_NO_WAIT) != TX_SUCCESS) { ret = TX_POOL_ERROR; }if (tx_byte_allocate(byte_pool, (VOID **) &pointer, APP_STACK_SIZE, TX_NO_WAIT) != TX_SUCCESS) { ... } /* Create ThreadTwo. */ if (tx_thread_create(&ThreadTwo, "Thread Two", ThreadTwo_Entry, 0, pointer, APP_STACK_SIZE, THREAD_TWO_PRIO, THREAD_TWO_PREEMPTION_THRESHOLD, TX_NO_TIME_SLICE, TX_AUTO_START) != TX_SUCCESS) { ret = TX_THREAD_ERROR; }if (tx_thread_create(&ThreadTwo, "Thread Two", ThreadTwo_Entry, 0, pointer, APP_STACK_SIZE, THREAD_TWO_PRIO, THREAD_TWO_PREEMPTION_THRESHOLD, TX_NO_TIME_SLICE, TX_AUTO_START) != TX_SUCCESS) { ... } /* Create the event flags group. */ if (tx_event_flags_create(&EventFlag, "Event Flag") != TX_SUCCESS) { ret = TX_GROUP_ERROR; }if (tx_event_flags_create(&EventFlag, "Event Flag") != TX_SUCCESS) { ... } /* USER CODE END App_ThreadX_Init */ return ret; }{ ... } /** * @brief MX_ThreadX_Init * @param None * @retval None *//* ... */ void MX_ThreadX_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 */ }{ ... } /* USER CODE BEGIN 1 */ /** * @brief Function implementing the MainThread thread. * @param thread_input: Not used * @retval None *//* ... */ void MainThread_Entry(ULONG thread_input) { UINT old_prio = 0; UINT old_pre_threshold = 0; ULONG actual_flags = 0; uint8_t count = 0; (void) thread_input; while (count < 3) { count++; if (tx_event_flags_get(&EventFlag, THREAD_ONE_EVT, TX_OR_CLEAR, &actual_flags, TX_WAIT_FOREVER) != TX_SUCCESS) { Error_Handler(); }if (tx_event_flags_get(&EventFlag, THREAD_ONE_EVT, TX_OR_CLEAR, &actual_flags, TX_WAIT_FOREVER) != TX_SUCCESS) { ... } else { /* Update the priority and preemption threshold of ThreadTwo to allow the preemption of ThreadOne *//* ... */ tx_thread_priority_change(&ThreadTwo, NEW_THREAD_TWO_PRIO, &old_prio); tx_thread_preemption_change(&ThreadTwo, NEW_THREAD_TWO_PREEMPTION_THRESHOLD, &old_pre_threshold); if (tx_event_flags_get(&EventFlag, THREAD_TWO_EVT, TX_OR_CLEAR, &actual_flags, TX_WAIT_FOREVER) != TX_SUCCESS) { Error_Handler(); }if (tx_event_flags_get(&EventFlag, THREAD_TWO_EVT, TX_OR_CLEAR, &actual_flags, TX_WAIT_FOREVER) != TX_SUCCESS) { ... } else { /* Reset the priority and preemption threshold of ThreadTwo */ tx_thread_priority_change(&ThreadTwo, THREAD_TWO_PRIO, &old_prio); tx_thread_preemption_change(&ThreadTwo, THREAD_TWO_PREEMPTION_THRESHOLD, &old_pre_threshold); }else { ... } }else { ... } }while (count < 3) { ... } /* Destroy ThreadOne and ThreadTwo */ tx_thread_terminate(&ThreadOne); tx_thread_terminate(&ThreadTwo); /* Infinite loop */ while(1) { BSP_LED_Toggle(LED_GREEN); /* Thread sleep for 1s */ tx_thread_sleep(100); }while (1) { ... } }{ ... } /** * @brief Function implementing the ThreadOne thread. * @param thread_input: Not used * @retval None *//* ... */ void ThreadOne_Entry(ULONG thread_input) { (void) thread_input; uint8_t count = 0; /* Infinite loop */ while(1) { BSP_LED_Toggle(LED_GREEN); /* Delay for 500ms (App_Delay is used to avoid context change). */ App_Delay(50); count ++; if (count == 10) { count = 0; if (tx_event_flags_set(&EventFlag, THREAD_ONE_EVT, TX_OR) != TX_SUCCESS) { Error_Handler(); }if (tx_event_flags_set(&EventFlag, THREAD_ONE_EVT, TX_OR) != TX_SUCCESS) { ... } }if (count == 10) { ... } }while (1) { ... } }{ ... } /** * @brief Function implementing the ThreadTwo thread. * @param thread_input: Not used * @retval None *//* ... */ void ThreadTwo_Entry(ULONG thread_input) { (void) thread_input; uint8_t count = 0; /* Infinite loop */ while (1) { BSP_LED_Toggle(LED_GREEN); /* Delay for 200ms (App_Delay is used to avoid context change). */ App_Delay(20); count ++; if (count == 25) { count = 0; if (tx_event_flags_set(&EventFlag, THREAD_TWO_EVT, TX_OR) != TX_SUCCESS) { Error_Handler(); }if (tx_event_flags_set(&EventFlag, THREAD_TWO_EVT, TX_OR) != TX_SUCCESS) { ... } }if (count == 25) { ... } }while (1) { ... } }{ ... } /** * @brief Application Delay function. * @param Delay : number of ticks to wait * @retval None *//* ... */ void App_Delay(uint32_t Delay) { UINT initial_time = tx_time_get(); while ((tx_time_get() - initial_time) < Delay); }{ ... } /* USER CODE END 1 */Private function prototypes
Details