1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
27
28
36
43
44
45
46
47
48
49
50
51
52
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
83
84
85
90
91
92
99
100
101
102
103
104
105
106
107
108
109
110
111
112
117
118
119
120
121
122
123
124
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
154
155
156
157
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
200
201
202
203
204
205
206
207
208
214
215
216
217
218
220
221
222
223
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
252
253
254
255
256
257
258
259
260
261
262
263
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
297
298
299
301
302
303
304
305
306
307
308
309
310
311
312
314
315
316
317
318
319
320
324
325
326
327
328
329
330
332
333
334
335
336
337
338
340
341
342
343
344
345
346
347
350
353
354
357
363
364
365
366
367
368
369
370
371
372
373
374
375
380
381
382
383
384
385
386
387
388
389
390
393
394
400
401
404
405
406
407
408
409
411
412
413
414
424
425
428
429
430
431
432
436
437
438
439
440
444
446
447
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
465
466
467
468
469
470
475
476
477
478
479
480
483
484
485
486
487
488
490
491
495
496
500
501
503
504
506
507
509
510
512
513
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
536
537
538
539
540
541
544
545
546
547
548
549
551
552
553
554
555
556
557
558
559
560
562
563
564
565
568
569
570
571
572
573
574
575
582
583
584
585
586
587
588
589
590
595
596
597
598
599
600
601
602
603
604
606
607
608
609
610
611
613
614
616
617
619
620
621
622
623
624
625
626
627
628
629
630
631
634
635
636
637
638
650
651
652
653
654
655
656
660
661
662
663
666
667
668
669
675
676
683
684
685
686
687
688
691
692
693
694
702
703
704
705
706
707
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
766
767
768
769
773
774
775
776
777
778
779
780
781
782
786
/* ... */
#include "FreeRTOS.h"
#include "task.h"
#include "queue.h"
#include "main.h"
Includes
#define TEST_1_STILL_EXECUTING ( 0 )
#define TEST_2_STILL_EXECUTING ( 1 )
#define CYCLE_RESET ( 2 )
#define TEST_TASK_1_PARAMETER ( ( void * ) 0x11112222 )
Private define
#if defined(__ICCARM__)
#define mainALIGN_TO( x )
#else
#define mainALIGN_TO( x ) __attribute__((aligned(x)))
#endif
Private macro
static void SystemClock_Config(void);
/* ... */
static void Test1Task( void *pvParameters );
static void Test2Task( void *pvParameters );
/* ... */
static void CheckTask( void *pvParameters );
/* ... */
static void UserModeTask( void *pvParameters );
/* ... */
static void PrivilegedModeTask( void *pvParameters );
/* ... */
static void SendImAlive( QueueHandle_t xHandle, uint32_t ulTaskNumber );
/* ... */
static void TestMemoryRegions( void );
Private function prototypes
/* ... */
QueueHandle_t xGlobalScopeCheckQueue = NULL;
#if defined(__CC_ARM)
extern uint32_t Image$$ER_IROM_FREERTOS_SYSTEM_CALLS$$Base;
extern uint32_t Image$$ER_IROM_FREERTOS_SYSTEM_CALLS$$Limit;
/* ... */
const uint32_t * __FLASH_segment_start__ = ( uint32_t * ) 0x08000000;
const uint32_t * __FLASH_segment_end__ = ( uint32_t * ) 0x080FFFFF;
const uint32_t * __SRAM_segment_start__ = ( uint32_t * ) 0x20000000;
const uint32_t * __SRAM_segment_end__ = ( uint32_t * ) 0x20017FFF;
const uint32_t * __privileged_functions_start__ = ( uint32_t * ) 0x08000000;
const uint32_t * __privileged_functions_end__ = ( uint32_t * ) 0x08007FFF;
const uint32_t * __privileged_data_start__ = ( uint32_t * ) 0x20000000;
const uint32_t * __privileged_data_end__ = ( uint32_t * ) 0x20007FFF;
const uint32_t * __syscalls_flash_start__ = ( uint32_t * ) &( Image$$ER_IROM_FREERTOS_SYSTEM_CALLS$$Base );
const uint32_t * __syscalls_flash_end__ = ( uint32_t * ) &( Image$$ER_IROM_FREERTOS_SYSTEM_CALLS$$Limit );/* ... */
#else
extern uint32_t __FLASH_segment_start__[];
extern uint32_t __FLASH_segment_end__[];
extern uint32_t __SRAM_segment_end__[];
extern uint32_t __privileged_functions_start__[];
extern uint32_t __privileged_functions_end__[];
extern uint32_t __privileged_data_start__[];
extern uint32_t __privileged_data_end__[];
extern uint32_t __syscalls_flash_start__[];
extern uint32_t __syscalls_flash_end__ [];/* ... */
#endif
Private variables
/* ... */
#define CHECK_TASK_STACK_SIZE_WORDS 256
#define CHECK_TASK_STACK_ALIGNMENT ( CHECK_TASK_STACK_SIZE_WORDS * sizeof( portSTACK_TYPE ) )
/* ... */
#if defined(__ICCARM__)
#pragma data_alignment= CHECK_TASK_STACK_ALIGNMENT
#endif
static portSTACK_TYPE xCheckTaskStack[ CHECK_TASK_STACK_SIZE_WORDS ] mainALIGN_TO( CHECK_TASK_STACK_ALIGNMENT );
/* ... */
#define READ_WRITE_ARRAY_SIZE 130
#define READ_WRITE_ALIGN_SIZE 128
#if defined(__ICCARM__)
#pragma data_alignment= READ_WRITE_ALIGN_SIZE
#endif
char ReadWriteArray[ READ_WRITE_ARRAY_SIZE ] mainALIGN_TO( READ_WRITE_ALIGN_SIZE );
#define READ_ONLY_ARRAY_SIZE 260
#define READ_ONLY_ALIGN_SIZE 256
#if defined(__ICCARM__)
#pragma data_alignment= READ_ONLY_ALIGN_SIZE
#endif
char ReadOnlyArray[ READ_ONLY_ARRAY_SIZE ] mainALIGN_TO( READ_ONLY_ALIGN_SIZE );
#define PRIVILEGED_ONLY_ACCESS_ARRAY_SIZE 130
#define PRIVILEGED_ONLY_ACCESS_ALIGN_SIZE 128
#if defined(__ICCARM__)
#pragma data_alignment= PRIVILEGED_ONLY_ACCESS_ALIGN_SIZE
#endif
char PrivilegedOnlyAccessArray[ PRIVILEGED_ONLY_ACCESS_ALIGN_SIZE ] mainALIGN_TO( PRIVILEGED_ONLY_ACCESS_ALIGN_SIZE );
/* ... */
static const TaskParameters_t xCheckTaskParameters =
{
CheckTask,
"CheckTask",
CHECK_TASK_STACK_SIZE_WORDS,
( void * ) 0x12121212,
( tskIDLE_PRIORITY + 1 ) | portPRIVILEGE_BIT,
xCheckTaskStack,
{
{ ReadWriteArray, READ_WRITE_ALIGN_SIZE, portMPU_REGION_READ_WRITE },
{ ReadOnlyArray, READ_ONLY_ALIGN_SIZE, portMPU_REGION_READ_ONLY },
{ PrivilegedOnlyAccessArray, PRIVILEGED_ONLY_ACCESS_ALIGN_SIZE, portMPU_REGION_PRIVILEGED_READ_WRITE }
...}
...};
Data used by the 'Check' task.
/* ... */
#define TEST_STACK_SIZE_WORDS 128
#define TEST_STACK_ALIGNMENT ( TEST_STACK_SIZE_WORDS * sizeof( portSTACK_TYPE ) )
/* ... */
#if defined(__ICCARM__)
#pragma data_alignment= TEST_STACK_ALIGNMENT
#endif
static portSTACK_TYPE xTest1Stack[ TEST_STACK_SIZE_WORDS ] mainALIGN_TO( TEST_STACK_ALIGNMENT );
#if defined(__ICCARM__)
#pragma data_alignment= TEST_STACK_ALIGNMENT
#endif
static portSTACK_TYPE xTest2Stack[ TEST_STACK_SIZE_WORDS ] mainALIGN_TO( TEST_STACK_ALIGNMENT );
static const TaskParameters_t xTest1Parameters =
{
Test1Task,
"Test1Task",
TEST_STACK_SIZE_WORDS,
( void * ) TEST_TASK_1_PARAMETER,
tskIDLE_PRIORITY | portPRIVILEGE_BIT,
xTest1Stack,
{
{ 0x00, 0x00, 0x00 },
{ 0x00, 0x00, 0x00 },
{ 0x00, 0x00, 0x00 }
...}
...};
static TaskParameters_t xTest2Parameters =
{
Test2Task,
"Test2Task",
TEST_STACK_SIZE_WORDS,
( void * ) NULL,
tskIDLE_PRIORITY,
xTest2Stack,
{
{ 0x00, 0x00, 0x00 },
{ 0x00, 0x00, 0x00 },
{ 0x00, 0x00, 0x00 }
...}
...};
Data used by the 'test' tasks.
int main( void )
{
/* ... */
HAL_Init();
SystemClock_Config();
xGlobalScopeCheckQueue = xQueueCreate( 1, sizeof( uint32_t ) );
/* ... */
xTest2Parameters.pvParameters = xGlobalScopeCheckQueue;
/* ... */
xTaskCreateRestricted( &xTest1Parameters, NULL );
xTaskCreateRestricted( &xTest2Parameters, NULL );
xTaskCreateRestricted( &xCheckTaskParameters, NULL );
xTaskCreate( UserModeTask, "Task1", 100, NULL, 3, NULL );
xTaskCreate( PrivilegedModeTask, "Task2", 100, NULL,( 3 | portPRIVILEGE_BIT ), NULL );
vTaskStartScheduler();
/* ... */
for( ;; );
}{ ... }
static void CheckTask( void *pvParameters )
{
/* ... */
QueueHandle_t xQueue = xGlobalScopeCheckQueue;
int32_t lMessage;
( void ) pvParameters;
/* ... */
TestMemoryRegions();
ReadWriteArray[0] = 0;
ReadWriteArray[1] = 0;
ReadWriteArray[2] = 0;
/* ... */
for( ;; )
{
xQueueReceive( xQueue, &lMessage, portMAX_DELAY );
switch( lMessage )
{
case TEST_1_STILL_EXECUTING :
(ReadWriteArray[0])++;
break;case TEST_1_STILL_EXECUTING :
case TEST_2_STILL_EXECUTING:
(ReadWriteArray[1])++;
break;case TEST_2_STILL_EXECUTING:
case CYCLE_RESET:
/* ... */
if( ( ReadWriteArray[ 0 ] == 0 ) || ( ReadWriteArray[ 1 ] == 0 ) )
{
/* ... */
for(;;);
}if (( ReadWriteArray[ 0 ] == 0 ) || ( ReadWriteArray[ 1 ] == 0 )) { ... }
else
{
ReadWriteArray[0] = 0;
ReadWriteArray[1] = 0;
(ReadWriteArray[2])++;
}else { ... }
break;
case CYCLE_RESET:
default :
/* ... */
vTaskDelete( NULL );
break;default
}switch (lMessage) { ... }
}for (;;) { ... }
}{ ... }
static void TestMemoryRegions( void )
{
int32_t x;
char cTemp;
/* ... */
PrivilegedOnlyAccessArray[ 0 ] = 'a';
if( PrivilegedOnlyAccessArray[ 0 ] != 'a' )
{
/* ... */
vTaskDelete( NULL );
}if (PrivilegedOnlyAccessArray[ 0 ] != 'a') { ... }
/* ... */
portSWITCH_TO_USER_MODE();
/* ... */
for( x = 0; x < READ_WRITE_ALIGN_SIZE; x++ )
{
ReadWriteArray[ x ] = 'a';
if( ReadWriteArray[ x ] != 'a' )
{
/* ... */
vTaskDelete( NULL );
}if (ReadWriteArray[ x ] != 'a') { ... }
}for (x = 0; x < READ_WRITE_ALIGN_SIZE; x++) { ... }
/* ... */
for( x = 0; x < READ_ONLY_ALIGN_SIZE; x++ )
{
cTemp = ReadOnlyArray[ x ];
}for (x = 0; x < READ_ONLY_ALIGN_SIZE; x++) { ... }
/* ... */
/* ... */
/* ... */
( void ) cTemp;
}{ ... }
static void UserModeTask( void *pvParameters )
{
volatile const uint32_t *pul;
volatile uint32_t ulReadData;
/* ... */
( void ) pvParameters;
/* ... */
pul = __privileged_data_end__ + 1;
ulReadData = *pul;
pul = __SRAM_segment_end__ - 1;
ulReadData = *pul;
/* ... */
pul = __privileged_functions_end__ + 1;
ulReadData = *pul;
pul = __FLASH_segment_end__ - 1;
ulReadData = *pul;
/* ... */
/* ... */
/* ... */
/* ... */
/* ... */
/* ... */
/* ... */
/* ... */
vTaskDelete( NULL );
( void ) ulReadData;
}{ ... }
static void PrivilegedModeTask( void *pvParameters )
{
volatile const uint32_t *pul;
volatile uint32_t ulReadData;
const volatile uint32_t *pulSystemPeripheralRegister = ( volatile uint32_t * ) 0xe000e014;
const volatile uint32_t *pulStandardPeripheralRegister = ( volatile uint32_t * ) 0x40000000;
( void ) pvParameters;
/* ... */
pul = __privileged_data_end__ + 1;
ulReadData = *pul;
pul = __SRAM_segment_end__ - 1;
ulReadData = *pul;
/* ... */
pul = __privileged_functions_end__ + 1;
ulReadData = *pul;
pul = __FLASH_segment_end__ - 1;
ulReadData = *pul;
/* ... */
pul = __privileged_functions_start__;
ulReadData = *pul;
pul = __privileged_functions_end__ - 1;
ulReadData = *pul;
pul = __privileged_data_start__;
ulReadData = *pul;
pul = __privileged_data_end__ - 1;
ulReadData = *pul;
/* ... */
ulReadData = *pulSystemPeripheralRegister;
ulReadData = *pulStandardPeripheralRegister;
/* ... */
vTaskDelete( NULL );
( void ) ulReadData;
}{ ... }
static void SendImAlive( QueueHandle_t xHandle, uint32_t ulTaskNumber )
{
if( xHandle != NULL )
{
xQueueSend( xHandle, &ulTaskNumber, 0 );
}if (xHandle != NULL) { ... }
}{ ... }
void vApplicationIdleHook( void )
{
volatile const uint32_t *pul;
volatile uint32_t ulReadData;
/* ... */
pul = __privileged_data_start__;
ulReadData = *pul;
pul = __privileged_data_end__ - 1;
ulReadData = *pul;
pul = __SRAM_segment_end__ - 1;
ulReadData = *pul;
/* ... */
pul = __FLASH_segment_start__;
ulReadData = *pul;
pul = __FLASH_segment_end__ - 1;
ulReadData = *pul;
/* ... */
/* ... */
/* ... */
( void ) ulReadData;
}{ ... }
void vApplicationTickHook( void )
{
static uint32_t ulCallCount = 0;
const uint32_t ulCallsBetweenSends = pdMS_TO_TICKS( 5000 );
const uint32_t ulMessage = CYCLE_RESET;
portBASE_TYPE xDummy;
/* ... */
ulCallCount++;
if( ulCallCount >= ulCallsBetweenSends )
{
ulCallCount = 0;
/* ... */
xQueueSendFromISR( xGlobalScopeCheckQueue, &ulMessage, &xDummy );
}if (ulCallCount >= ulCallsBetweenSends) { ... }
}{ ... }
static void Test1Task( void *pvParameters )
{
/* ... */
QueueHandle_t xQueue = xGlobalScopeCheckQueue;
const TickType_t xDelayTime = pdMS_TO_TICKS( 100UL );
/* ... */
portSWITCH_TO_USER_MODE();
if( pvParameters != ( void * ) TEST_TASK_1_PARAMETER )
{
/* ... */
vTaskDelete(NULL);
}if (pvParameters != ( void * ) TEST_TASK_1_PARAMETER) { ... }
for( ;; )
{
/* ... */
SendImAlive( xQueue, TEST_1_STILL_EXECUTING );
vTaskDelay( xDelayTime );
}for (;;) { ... }
}{ ... }
static void Test2Task( void *pvParameters )
{
/* ... */
QueueHandle_t xQueue = ( QueueHandle_t ) pvParameters;
const TickType_t xDelayTime = pdMS_TO_TICKS( 100UL );
for( ;; )
{
/* ... */
SendImAlive( xQueue, TEST_2_STILL_EXECUTING );
vTaskDelay( xDelayTime );
}for (;;) { ... }
}{ ... }
#if configCHECK_FOR_STACK_OVERFLOW
void vApplicationStackOverflowHook( TaskHandle_t pxTask, char *pcTaskName )
{
/* ... */
( void ) pxTask;
( void ) pcTaskName;
for( ;; );
}{ ... }
/* ... */#endif
/* ... */
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)
{
while(1) { ; }
}if (ret != HAL_OK) { ... }
ret = HAL_PWREx_EnableOverDrive();
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_DIV4;
RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV2;
ret = HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_5);
if(ret != HAL_OK)
{
while(1) { ; }
}if (ret != HAL_OK) { ... }
}{ ... }