1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
24
29
34
39
50
59
60
61
62
63
64
65
66
67
72
73
74
75
76
77
78
79
80
81
85
86
87
92
93
94
98
99
100
105
106
107
111
112
113
114
115
116
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
171
172
173
174
175
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
211
212
213
214
215
221
222
223
224
225
226
227
233
234
235
236
237
242
247
248
253
254
261
262
/* ... */
#include "app_threadx.h"
Includes
#include "stdio.h"
Private includes
Private typedef
Private define
#if defined ( __GNUC__) && !defined(__clang__)
/* ... */
#define PUTCHAR_PROTOTYPE int __io_putchar(int ch)
/* ... */#else
#define PUTCHAR_PROTOTYPE int fputc(int ch, FILE *f)
#endif
Private macro
TX_THREAD ThreadOne;
TX_THREAD ThreadTwo;
APP_SYNC_TYPE SyncObject;
extern UART_HandleTypeDef huart3;
Private variables
VOID ThreadOne_Entry(ULONG thread_input);
VOID ThreadTwo_Entry(ULONG thread_input);
static VOID Led_Toggle(Led_TypeDef led, UINT iter);
static VOID App_Delay(ULONG Delay);
/* ... */
UINT App_ThreadX_Init(VOID *memory_ptr)
{
UINT ret = TX_SUCCESS;
TX_BYTE_POOL *byte_pool = (TX_BYTE_POOL*)memory_ptr;
CHAR *pointer;
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) { ... }
if (tx_thread_create(&ThreadOne, "Thread One", ThreadOne_Entry, 0, pointer, APP_STACK_SIZE, THREAD_ONE_PRIO,
THREAD_ONE_PREEMPTION_THRESHOLD, DEFAULT_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, DEFAULT_TIME_SLICE, TX_AUTO_START) != TX_SUCCESS) { ... }
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) { ... }
if (tx_thread_create(&ThreadTwo, "Thread Two", ThreadTwo_Entry, 0, pointer, APP_STACK_SIZE, THREAD_TWO_PRIO,
THREAD_TWO_PREEMPTION_THRESHOLD, DEFAULT_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, DEFAULT_TIME_SLICE, TX_AUTO_START) != TX_SUCCESS) { ... }
if (APP_SYNC_CREATE(&SyncObject) != TX_SUCCESS)
{
ret = TX_SYNC_ERROR;
}if (APP_SYNC_CREATE(&SyncObject) != TX_SUCCESS) { ... }
return ret;
}{ ... }
/* ... */
void MX_ThreadX_Init(void)
{
tx_kernel_enter();
}{ ... }
/* ... */
void ThreadOne_Entry(ULONG thread_input)
{
UNUSED(thread_input);
ULONG iteration = 0;
while(1)
{
if (APP_SYNC_GET(&SyncObject, TX_NO_WAIT) == TX_SUCCESS)
{
printf("** ThreadOne : SyncObject acquired ** \n");
Led_Toggle(LED_GREEN, 10);
APP_SYNC_PUT(&SyncObject);
printf("** ThreadOne : SyncObject released ** \n");
tx_thread_sleep(1);
}if (APP_SYNC_GET(&SyncObject, TX_NO_WAIT) == TX_SUCCESS) { ... }
else
{
if ((iteration % 2000000) == 0)
{
printf("** ThreadOne : waiting for SyncObject !! **\n");
}if ((iteration % 2000000) == 0) { ... }
}else { ... }
iteration++;
}while (1) { ... }
}{ ... }
/* ... */
void ThreadTwo_Entry(ULONG thread_input)
{
UNUSED(thread_input);
ULONG iteration = 0;
while(1)
{
if (APP_SYNC_GET(&SyncObject, TX_NO_WAIT) == TX_SUCCESS)
{
printf("** ThreadTwo : SyncObject acquired ** \n");
Led_Toggle(LED_RED, 10);
APP_SYNC_PUT(&SyncObject);
printf("** ThreadTwo : SyncObject released ** \n");
tx_thread_sleep(1);
}if (APP_SYNC_GET(&SyncObject, TX_NO_WAIT) == TX_SUCCESS) { ... }
else
{
if ((iteration % 2000000) == 0)
{
printf("** ThreadTwo : waiting for SyncObject !! **\n");
}if ((iteration % 2000000) == 0) { ... }
}else { ... }
iteration++;
}while (1) { ... }
}{ ... }
/* ... */
static VOID Led_Toggle(Led_TypeDef led, UINT iter)
{
UINT i;
BSP_LED_Off(led);
for (i =0; i < iter; i++)
{
BSP_LED_Toggle(led);
App_Delay(50);
}for (i =0; i < iter; i++) { ... }
BSP_LED_Off(led);
}{ ... }
/* ... */
void App_Delay(ULONG Delay)
{
ULONG initial_time = tx_time_get();
while ((tx_time_get() - initial_time) < Delay);
}{ ... }
/* ... */
PUTCHAR_PROTOTYPE
{
HAL_UART_Transmit(&huart3, (uint8_t *)&ch, 1, 0xFFFF);
return ch;
...}
Private function prototypes