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
102
103
108
109
110
111
112
113
114
115
122
127
128
132
133
134
135
140
145
146
147
148
149
150
151
152
153
154
155
160
161
162
163
164
165
166
167
172
193
200
201
202
207
208
210
211
212
213
214
215
220
221
222
223
224
225
226
227
230
231
232
233
238
239
240
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
259
264
265
266
271
272
273
274
275
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
310
311
312
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
334
335
336
337
341
342
344
345
346
347
348
349
350
354
355
356
361
369
370
371
/* ... */
#include "main.h"
#include "cmsis_os.h"
#include "stm32469i_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();
#if defined(USE_IOEXPANDER)
BSP_IO_Init();
#endif
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 = 25;
RCC_OscInitStruct.PLL.PLLN = 360;
RCC_OscInitStruct.PLL.PLLP = RCC_PLLP_DIV2;
RCC_OscInitStruct.PLL.PLLQ = 7;
RCC_OscInitStruct.PLL.PLLR = 6;
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