1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
23
24
25
30
40
46
47
48
53
54
55
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
133
138
143
144
145
146
147
148
149
150
151
152
153
158
159
160
161
162
163
164
165
170
191
198
199
200
205
206
208
209
210
211
212
213
218
219
220
221
222
223
224
225
228
229
230
231
236
237
238
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
257
262
263
264
269
270
271
272
273
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
309
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
330
331
332
336
337
339
340
341
342
343
344
345
349
350
351
356
364
365
366
367
368
369
370
371
372
373
383
/* ... */
#include "main.h"
#include "cmsis_os.h"
#include "stm32446e_eval_io.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);
static void Error_Handler(void);Private function prototypes
/* ... */
int main(void)
{
/* ... */
HAL_Init();
SystemClock_Config();
BSP_IO_Init();
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_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_ON;
RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON;
RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSE;
RCC_OscInitStruct.PLL.PLLM = 8;
RCC_OscInitStruct.PLL.PLLN = 360;
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)
{
Error_Handler();
}if (ret != HAL_OK) { ... }
ret = HAL_PWREx_EnableOverDrive();
if(ret != HAL_OK)
{
Error_Handler();
}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_DIV4;
RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV2;
ret = HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_5);
if(ret != HAL_OK)
{
Error_Handler();
}if (ret != HAL_OK) { ... }
}{ ... }
/* ... */
static void Error_Handler(void)
{
BSP_LED_On(LED3);
while (1)
{
}while (1) { ... }
}{ ... }
#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