1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
22
23
24
29
39
44
45
46
51
52
53
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
101
102
103
104
105
106
107
108
115
120
121
125
126
127
128
133
138
139
140
141
142
143
144
145
146
147
148
153
154
155
156
157
158
159
160
165
186
193
194
195
200
201
203
204
205
206
207
208
213
214
215
216
217
218
219
220
223
224
225
226
231
232
233
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
252
257
258
259
264
265
266
267
268
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
303
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
350
/* ... */
#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;
static __IO uint32_t HighPriorityThreadCycles = 0, MediumPriorityThreadCycles = 0, LowPriorityThreadCycles = 0;
/* ... */
static osThreadId osHighPriorityThreadHandle, osMediumPriorityThreadHandle;
Private variables
static void MutexHighPriorityThread(void const *argument);
static void MutexMeduimPriorityThread(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);
BSP_LED_Init(LED4);
osMutexDef(osMutex);
osMutex = osMutexCreate(osMutex(osMutex));
if(osMutex != NULL)
{
osThreadDef(MutHigh, MutexHighPriorityThread, osPriorityBelowNormal, 0, configMINIMAL_STACK_SIZE);
osHighPriorityThreadHandle = osThreadCreate(osThread(MutHigh), NULL);
osThreadDef(MutMedium, MutexMeduimPriorityThread, 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 MutexMeduimPriorityThread(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++;
BSP_LED_Toggle(LED4);
/* ... */
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_RCC_PWR_CLK_ENABLE();
/* ... */
__HAL_PWR_VOLTAGESCALING_CONFIG(PWR_REGULATOR_VOLTAGE_SCALE1);
RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSE;
RCC_OscInitStruct.HSEState = RCC_HSE_ON;
RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON;
RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSE;
RCC_OscInitStruct.PLL.PLLM = 25;
RCC_OscInitStruct.PLL.PLLN = 360;
RCC_OscInitStruct.PLL.PLLP = RCC_PLLP_DIV2;
RCC_OscInitStruct.PLL.PLLQ = 7;
HAL_RCC_OscConfig(&RCC_OscInitStruct);
HAL_PWREx_EnableOverDrive();
/* ... */
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_DIV4;
RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV2;
HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_5);
}{ ... }
#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