1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
23
24
25
30
40
46
47
48
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
106
107
108
109
110
111
112
113
120
125
126
130
131
132
137
142
143
144
145
146
147
148
149
150
151
152
157
158
159
160
161
162
163
164
169
190
197
198
199
204
205
207
208
209
210
211
212
217
218
219
220
221
222
223
224
227
228
229
230
235
236
237
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
255
260
261
262
267
268
269
270
271
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
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
329
330
332
333
334
335
336
337
338
342
343
344
345
/* ... */
#include "main.h"
#include "cmsis_os.h"
Includes
#define mutexSHORT_DELAY ((uint32_t) 20)
#define mutexNO_DELAY ((uint32_t) 0)
#define mutexTWO_TICK_DELAY ((uint32_t) 2)
Private macro
static osMutexId osMutex;
__IO uint32_t HighPriorityThreadCycles = 0, MediumPriorityThreadCycles = 0, LowPriorityThreadCycles = 0;
/* ... */
static osThreadId osHighPriorityThreadHandle, osMediumPriorityThreadHandle;
Private variables
static void MutexHighPriorityThread(void const *argument);
static void MutexMediumPriorityThread(void const *argument);
static void MutexLowPriorityThread(void const *argument);
static void SystemClock_Config(void);
Private function prototypes
/* ... */
int main(void)
{
/* ... */
HAL_Init();
SystemClock_Config();
BSP_LED_Init(LED1);
BSP_LED_Init(LED2);
BSP_LED_Init(LED3);
osMutexDef(osMutex);
osMutex = osMutexCreate(osMutex(osMutex));
if (osMutex != NULL)
{
osThreadDef(MutHigh, MutexHighPriorityThread, osPriorityBelowNormal, 0, configMINIMAL_STACK_SIZE);
osHighPriorityThreadHandle = osThreadCreate(osThread(MutHigh), NULL);
osThreadDef(MutMedium, MutexMediumPriorityThread, osPriorityLow, 0, configMINIMAL_STACK_SIZE);
osMediumPriorityThreadHandle = osThreadCreate(osThread(MutMedium), NULL);
osThreadDef(MutLow, MutexLowPriorityThread, osPriorityIdle, 0, configMINIMAL_STACK_SIZE);
osThreadCreate(osThread(MutLow), NULL);
}if (osMutex != NULL) { ... }
osKernelStart();
for (;;);
}{ ... }
/* ... */
static void MutexHighPriorityThread(void const *argument)
{
(void) argument;
for (;;)
{
/* ... */
if (osMutexWait(osMutex, mutexTWO_TICK_DELAY) != osOK)
{
BSP_LED_Toggle(LED3);
}if (osMutexWait(osMutex, mutexTWO_TICK_DELAY) != osOK) { ... }
/* ... */
osDelay(mutexSHORT_DELAY);
/* ... */
if (osMutexRelease(osMutex) != osOK)
{
BSP_LED_Toggle(LED3);
}if (osMutexRelease(osMutex) != osOK) { ... }
HighPriorityThreadCycles++;
BSP_LED_Toggle(LED1);
osThreadSuspend(NULL);
}for (;;) { ... }
}{ ... }
/* ... */
static void MutexMediumPriorityThread(void const *argument)
{
(void) argument;
for (;;)
{
/* ... */
if (osMutexWait(osMutex, osWaitForever) == osOK)
{
if (osThreadGetState(osHighPriorityThreadHandle) != osThreadSuspended)
{
/* ... */
BSP_LED_Toggle(LED3);
}if (osThreadGetState(osHighPriorityThreadHandle) != osThreadSuspended) { ... }
else
{
/* ... */
if (osMutexRelease(osMutex) != osOK)
{
BSP_LED_Toggle(LED3);
}if (osMutexRelease(osMutex) != osOK) { ... }
osThreadSuspend(NULL);
}else { ... }
}if (osMutexWait(osMutex, osWaitForever) == osOK) { ... }
else
{
/* ... */
BSP_LED_Toggle(LED3);
}else { ... }
if (HighPriorityThreadCycles != (MediumPriorityThreadCycles + 1))
{
BSP_LED_Toggle(LED3);
}if (HighPriorityThreadCycles != (MediumPriorityThreadCycles + 1)) { ... }
/* ... */
MediumPriorityThreadCycles++;
BSP_LED_Toggle(LED2);
}for (;;) { ... }
}{ ... }
/* ... */
static void MutexLowPriorityThread(void const *argument)
{
(void) argument;
for (;;)
{
/* ... */
if (osMutexWait(osMutex, mutexNO_DELAY) == osOK)
{
if ((osThreadGetState(osHighPriorityThreadHandle) != osThreadSuspended) || (osThreadGetState(osMediumPriorityThreadHandle) != osThreadSuspended))
{
BSP_LED_Toggle(LED3);
}if ((osThreadGetState(osHighPriorityThreadHandle) != osThreadSuspended) || (osThreadGetState(osMediumPriorityThreadHandle) != osThreadSuspended)) { ... }
else
{
/* ... */
LowPriorityThreadCycles++;
/* ... */
osThreadResume(osMediumPriorityThreadHandle);
osThreadResume(osHighPriorityThreadHandle);
/* ... */
if ((osThreadGetState(osHighPriorityThreadHandle) == osThreadSuspended) || (osThreadGetState(osMediumPriorityThreadHandle) == osThreadSuspended))
{
BSP_LED_Toggle(LED3);
}if ((osThreadGetState(osHighPriorityThreadHandle) == osThreadSuspended) || (osThreadGetState(osMediumPriorityThreadHandle) == osThreadSuspended)) { ... }
if (osMutexRelease(osMutex) != osOK)
{
BSP_LED_Toggle(LED3);
}if (osMutexRelease(osMutex) != osOK) { ... }
}else { ... }
}if (osMutexWait(osMutex, mutexNO_DELAY) == osOK) { ... }
#if configUSE_PREEMPTION == 0
{
taskYIELD();
...}/* ... */
#endif
}for (;;) { ... }
}{ ... }
/* ... */
static void SystemClock_Config(void)
{
RCC_ClkInitTypeDef RCC_ClkInitStruct;
RCC_OscInitTypeDef RCC_OscInitStruct;
HAL_StatusTypeDef ret = HAL_OK;
__HAL_RCC_PWR_CLK_ENABLE();
/* ... */
__HAL_PWR_VOLTAGESCALING_CONFIG(PWR_REGULATOR_VOLTAGE_SCALE1);
RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSE;
RCC_OscInitStruct.HSEState = RCC_HSE_BYPASS;
RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON;
RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSE;
RCC_OscInitStruct.PLL.PLLM = 8;
RCC_OscInitStruct.PLL.PLLN = 200;
RCC_OscInitStruct.PLL.PLLP = RCC_PLLP_DIV2;
RCC_OscInitStruct.PLL.PLLQ = 7;
RCC_OscInitStruct.PLL.PLLR = 2;
ret = HAL_RCC_OscConfig(&RCC_OscInitStruct);
if(ret != HAL_OK)
{
while(1) { ; }
}if (ret != HAL_OK) { ... }
/* ... */
RCC_ClkInitStruct.ClockType = (RCC_CLOCKTYPE_SYSCLK | RCC_CLOCKTYPE_HCLK | RCC_CLOCKTYPE_PCLK1 | RCC_CLOCKTYPE_PCLK2);
RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_PLLCLK;
RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1;
RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV2;
RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV1;
ret = HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_3);
if(ret != HAL_OK)
{
while(1) { ; }
}if (ret != HAL_OK) { ... }
}{ ... }
#ifdef USE_FULL_ASSERT
/* ... */
void assert_failed(uint8_t *file, uint32_t line)
{
/* ... */
while (1)
{}while (1) { ... }
}assert_failed (uint8_t *file, uint32_t line) { ... }
/* ... */#endifPrivate functions