1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
33
34
40
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
76
77
78
79
80
83
84
85
86
87
88
89
94
95
96
99
100
101
102
103
106
107
108
111
112
113
114
115
118
119
120
121
122
123
124
125
126
127
128
129
130
131
134
135
136
138
139
140
141
142
143
144
147
148
149
150
151
155
156
157
158
159
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
183
184
185
186
187
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
210
211
212
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
249
250
251
256
257
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
278
279
280
281
282
283
284
285
286
287
289
290
291
292
293
294
295
296
297
304
305
306
307
308
309
310
313
314
315
316
317
319
320
321
322
323
324
325
326
327
328
329
330
331
333
334
335
336
337
338
339
340
341
342
343
344
345
346
352
353
354
355
356
357
358
359
360
366
367
370
371
372
373
374
375
376
377
378
379
382
383
384
385
386
387
388
389
390
391
392
393
396
397
398
399
400
401
402
403
406
407
408
410
411
412
413
414
415
416
417
425
426
427
428
429
430
436
437
438
439
440
441
442
443
444
445
446
451
452
453
454
455
456
460
461
462
463
464
465
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
494
495
496
497
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
540
541
542
543
544
545
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
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
758
759
760
761
762
766
767
768
769
770
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
799
800
804
805
806
809
810
811
815
816
818
819
820
821
822
824
825
826
828
829
830
831
832
833
835
836
837
839
840
841
842
843
844
845
846
848
849
850
851
852
853
858
859
860
861
867
868
869
870
873
874
875
876
877
881
882
883
884
885
892
893
894
899
900
901
902
903
907
908
909
912
916
917
918
919
922
923
924
925
926
930
931
932
934
935
936
938
939
940
941
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
968
969
970
971
972
973
974
975
976
977
978
979
980
984
985
987
988
989
990
991
993
994
995
996
997
998
999
1000
1001
1002
1003
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1057
1058
1059
1060
1064
1070
1071
1072
1073
1074
1075
1076
1079
1085
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1121
1122
1123
1124
1125
1126
1127
1128
1129
1133
1134
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1154
1155
1156
1157
1158
1160
1161
1162
1163
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1183
1184
/* ... */
/* ... */
#include "FreeRTOS.h"
#include "task.h"
#include "rp2040_config.h"
#include "hardware/clocks.h"
#include "hardware/exception.h"
5 includes
/* ... */
#if ( LIB_PICO_MULTICORE == 1 )
#include "pico/multicore.h"
#endif
#define portRUNNING_ON_BOTH_CORES ( configNUMBER_OF_CORES == portMAX_CORE_COUNT )
#define portNVIC_SYSTICK_CTRL_REG ( *( ( volatile uint32_t * ) 0xe000e010 ) )
#define portNVIC_SYSTICK_LOAD_REG ( *( ( volatile uint32_t * ) 0xe000e014 ) )
#define portNVIC_SYSTICK_CURRENT_VALUE_REG ( *( ( volatile uint32_t * ) 0xe000e018 ) )
#define portNVIC_INT_CTRL_REG ( *( ( volatile uint32_t * ) 0xe000ed04 ) )
#define portNVIC_SHPR3_REG ( *( ( volatile uint32_t * ) 0xe000ed20 ) )
#define portNVIC_SYSTICK_CLK_BIT ( 1UL << 2UL )
#define portNVIC_SYSTICK_INT_BIT ( 1UL << 1UL )
#define portNVIC_SYSTICK_ENABLE_BIT ( 1UL << 0UL )
#define portNVIC_SYSTICK_COUNT_FLAG_BIT ( 1UL << 16UL )
#define portNVIC_PENDSVSET_BIT ( 1UL << 28UL )
#define portMIN_INTERRUPT_PRIORITY ( 255UL )
#define portNVIC_PENDSV_PRI ( portMIN_INTERRUPT_PRIORITY << 16UL )
#define portNVIC_SYSTICK_PRI ( portMIN_INTERRUPT_PRIORITY << 24UL )
#define portINITIAL_XPSR ( 0x01000000 )
#define portMAX_24_BIT_NUMBER ( 0xffffffUL )
16 defines
/* ... */
#ifndef portMISSED_COUNTS_FACTOR
#define portMISSED_COUNTS_FACTOR ( 45UL )
#endif
/* ... */
#ifdef configTASK_RETURN_ADDRESS
#define portTASK_RETURN_ADDRESS configTASK_RETURN_ADDRESS
#else
#define portTASK_RETURN_ADDRESS prvTaskExitError
#endif
/* ... */
void vPortSetupTimerInterrupt( void );
/* ... */
void xPortPendSVHandler( void ) __attribute__( ( naked ) );
void xPortSysTickHandler( void );
void vPortSVCHandler( void );
/* ... */
static void vPortStartFirstTask( void ) __attribute__( ( naked ) );
/* ... */
static void prvTaskExitError( void );
/* ... */
#if ( configNUMBER_OF_CORES == 1 )
static UBaseType_t uxCriticalNesting;
#else
UBaseType_t uxCriticalNestings[ configNUMBER_OF_CORES ] = { 0 };
#endif
#if ( configSUPPORT_PICO_SYNC_INTEROP == 1 )
#include "pico/lock_core.h"
#include "hardware/irq.h"
#include "event_groups.h"
#if configSUPPORT_STATIC_ALLOCATION
static StaticEventGroup_t xStaticEventGroup;
#define pEventGroup ( &xStaticEventGroup )
/* ... */ #endif
static EventGroupHandle_t xEventGroup;
#if ( portRUNNING_ON_BOTH_CORES == 0 )
static EventBits_t uxCrossCoreEventBits;
static spin_lock_t * pxCrossCoreSpinLock;/* ... */
#endif
static spin_lock_t * pxYieldSpinLock[ configNUMBER_OF_CORES ];
static uint32_t ulYieldSpinLockSaveValue[ configNUMBER_OF_CORES ];/* ... */
#endif
/* ... */
#if ( configUSE_TICKLESS_IDLE == 1 )
static uint32_t ulTimerCountsForOneTick = 0;
#endif
/* ... */
#if ( configUSE_TICKLESS_IDLE == 1 )
static uint32_t xMaximumPossibleSuppressedTicks = 0;
#endif
/* ... */
#if ( configUSE_TICKLESS_IDLE == 1 )
static uint32_t ulStoppedTimerCompensation = 0;
#endif
#define INVALID_PRIMARY_CORE_NUM 0xffu
static uint8_t ucPrimaryCoreNum = INVALID_PRIMARY_CORE_NUM;
#if ( portRUNNING_ON_BOTH_CORES == 1 )
#define portIS_FREE_RTOS_CORE() ( ucPrimaryCoreNum != INVALID_PRIMARY_CORE_NUM )
#else
#define portIS_FREE_RTOS_CORE() ( ucPrimaryCoreNum == get_core_num() )
#endif
/* ... */
StackType_t * pxPortInitialiseStack( StackType_t * pxTopOfStack,
TaskFunction_t pxCode,
void * pvParameters )
{
/* ... */
pxTopOfStack--;
*pxTopOfStack = portINITIAL_XPSR;
pxTopOfStack--;
*pxTopOfStack = ( StackType_t ) pxCode;
pxTopOfStack--;
*pxTopOfStack = ( StackType_t ) portTASK_RETURN_ADDRESS;
pxTopOfStack -= 5;
*pxTopOfStack = ( StackType_t ) pvParameters;
pxTopOfStack -= 8;
return pxTopOfStack;
}{ ... }
static void prvTaskExitError( void )
{
/* ... */
panic_unsupported();
}{ ... }
void vPortSVCHandler( void )
{
/* ... */
}{ ... }
void vPortStartFirstTask( void )
{
#if ( configNUMBER_OF_CORES == 1 )
__asm volatile (
" .syntax unified \n"
" ldr r2, pxCurrentTCBConst1 \n"
" ldr r3, [r2] \n"
" ldr r0, [r3] \n"
" adds r0, #32 \n"
" msr psp, r0 \n"
" movs r0, #2 \n"
" msr CONTROL, r0 \n"
" isb \n"
" pop {r0-r5} \n"
" mov lr, r5 \n"
" pop {r3} \n"
" pop {r2} \n"
" cpsie i \n"
" bx r3 \n"
" .align 4 \n"
"pxCurrentTCBConst1: .word pxCurrentTCB\n"
);/* ... */
#else
__asm volatile (
" .syntax unified \n"
#if configRESET_STACK_POINTER
" ldr r0, =0xE000ED08 \n"
" ldr r0, [r0] \n"
" ldr r0, [r0] \n"
" msr msp, r0 \n" /* ... */
#endif
#if portRUNNING_ON_BOTH_CORES
" adr r1, ulAsmLocals \n"
" ldmia r1!, {r2, r3} \n"
" ldr r2, [r2] \n"
" lsls r2, #2 \n"
" ldr r3, [r3, r2] \n" /* ... */
#else
" ldr r3, =pxCurrentTCBs \n"
" ldr r3, [r3] \n" /* ... */
#endif
" ldr r0, [r3] \n"
" adds r0, #32 \n"
" msr psp, r0 \n"
" movs r0, #2 \n"
" msr CONTROL, r0 \n"
" isb \n"
" pop {r0-r5} \n"
" mov lr, r5 \n"
" pop {r3} \n"
" pop {r2} \n"
" cpsie i \n"
" bx r3 \n"
#if portRUNNING_ON_BOTH_CORES
" \n"
" .align 4 \n"
"ulAsmLocals: \n"
" .word 0xD0000000 \n"
" .word pxCurrentTCBs \n"/* ... */
#endif
);/* ... */
#endif
}{ ... }
#if ( LIB_PICO_MULTICORE == 1 ) && ( configSUPPORT_PICO_SYNC_INTEROP == 1 )
static void prvFIFOInterruptHandler()
{
/* ... */
multicore_fifo_drain();
multicore_fifo_clear_irq();
#if ( portRUNNING_ON_BOTH_CORES == 1 )
portYIELD_FROM_ISR( pdTRUE );
#elif ( configSUPPORT_PICO_SYNC_INTEROP == 1 )
BaseType_t xHigherPriorityTaskWoken = pdFALSE;
uint32_t ulSave = spin_lock_blocking( pxCrossCoreSpinLock );
EventBits_t ulBits = uxCrossCoreEventBits;
uxCrossCoreEventBits &= ~ulBits;
spin_unlock( pxCrossCoreSpinLock, ulSave );
xEventGroupSetBitsFromISR( xEventGroup, ulBits, &xHigherPriorityTaskWoken );
portYIELD_FROM_ISR( xHigherPriorityTaskWoken );/* ... */
#endif
}{ ... }
#endif/* ... */
#if ( configNUMBER_OF_CORES > 1 )
/* ... */
static BaseType_t xPortStartSchedulerOnCore()
{
if( ucPrimaryCoreNum == get_core_num() )
{
/* ... */
vPortSetupTimerInterrupt();
portNVIC_SHPR3_REG |= portNVIC_SYSTICK_PRI;
#if ( configUSE_DYNAMIC_EXCEPTION_HANDLERS == 1 )
exception_set_exclusive_handler( SYSTICK_EXCEPTION, xPortSysTickHandler );
#endif
}if (ucPrimaryCoreNum == get_core_num()) { ... }
portNVIC_SHPR3_REG |= portNVIC_PENDSV_PRI;
#if ( configUSE_DYNAMIC_EXCEPTION_HANDLERS == 1 )
exception_set_exclusive_handler( PENDSV_EXCEPTION, xPortPendSVHandler );
exception_set_exclusive_handler( SVCALL_EXCEPTION, vPortSVCHandler );/* ... */
#endif
multicore_fifo_clear_irq();
multicore_fifo_drain();
uint32_t ulIRQNum = SIO_IRQ_PROC0 + get_core_num();
irq_set_priority( ulIRQNum, portMIN_INTERRUPT_PRIORITY );
irq_set_exclusive_handler( ulIRQNum, prvFIFOInterruptHandler );
irq_set_enabled( ulIRQNum, 1 );
vPortStartFirstTask();
/* ... */
vTaskSwitchContext( portGET_CORE_ID() );
prvTaskExitError();
return 0;
}xPortStartSchedulerOnCore () { ... }
#if portRUNNING_ON_BOTH_CORES
static void prvDisableInterruptsAndPortStartSchedulerOnCore( void )
{
portDISABLE_INTERRUPTS();
xPortStartSchedulerOnCore();
}prvDisableInterruptsAndPortStartSchedulerOnCore (void) { ... }
/* ... */ #endif
/* ... */
BaseType_t xPortStartScheduler( void )
{
configASSERT( ucPrimaryCoreNum == INVALID_PRIMARY_CORE_NUM );
spin_lock_claim( configSMP_SPINLOCK_0 );
spin_lock_claim( configSMP_SPINLOCK_1 );
#if portRUNNING_ON_BOTH_CORES
ucPrimaryCoreNum = configTICK_CORE;
configASSERT( get_core_num() == 0 );
multicore_launch_core1( prvDisableInterruptsAndPortStartSchedulerOnCore );/* ... */
#else
ucPrimaryCoreNum = get_core_num();
#endif
xPortStartSchedulerOnCore();
return 0;
}xPortStartScheduler (void) { ... }
/* ... */
#else
/* ... */
BaseType_t xPortStartScheduler( void )
{
portNVIC_SHPR3_REG |= portNVIC_PENDSV_PRI;
portNVIC_SHPR3_REG |= portNVIC_SYSTICK_PRI;
#if ( configUSE_DYNAMIC_EXCEPTION_HANDLERS == 1 )
exception_set_exclusive_handler( PENDSV_EXCEPTION, xPortPendSVHandler );
exception_set_exclusive_handler( SYSTICK_EXCEPTION, xPortSysTickHandler );
exception_set_exclusive_handler( SVCALL_EXCEPTION, vPortSVCHandler );/* ... */
#endif
/* ... */
vPortSetupTimerInterrupt();
uxCriticalNesting = 0;
ucPrimaryCoreNum = get_core_num();
#if ( LIB_PICO_MULTICORE == 1 )
#if ( configSUPPORT_PICO_SYNC_INTEROP == 1 )
multicore_fifo_clear_irq();
multicore_fifo_drain();
uint32_t irq_num = 15 + get_core_num();
irq_set_priority( irq_num, portMIN_INTERRUPT_PRIORITY );
irq_set_exclusive_handler( irq_num, prvFIFOInterruptHandler );
irq_set_enabled( irq_num, 1 );/* ... */
#endif/* ... */
#endif
vPortStartFirstTask();
/* ... */
vTaskSwitchContext();
prvTaskExitError();
return 0;
}{ ... }
#endif/* ... */
void vPortEndScheduler( void )
{
panic_unsupported();
}{ ... }
void vPortYield( void )
{
#if ( configSUPPORT_PICO_SYNC_INTEROP == 1 )
/* ... */
configASSERT( pxYieldSpinLock[ portGET_CORE_ID() ] == NULL );/* ... */
#endif
portNVIC_INT_CTRL_REG = portNVIC_PENDSVSET_BIT;
/* ... */
__asm volatile ( "dsb" ::: "memory" );
__asm volatile ( "isb" );
}{ ... }
#if ( configNUMBER_OF_CORES == 1 )
void vPortEnterCritical( void )
{
portDISABLE_INTERRUPTS();
uxCriticalNesting++;
__asm volatile ( "dsb" ::: "memory" );
__asm volatile ( "isb" );
}{ ... }
#endif/* ... */
#if ( configNUMBER_OF_CORES == 1 )
void vPortExitCritical( void )
{
configASSERT( uxCriticalNesting );
uxCriticalNesting--;
if( uxCriticalNesting == 0 )
{
portENABLE_INTERRUPTS();
}if (uxCriticalNesting == 0) { ... }
}{ ... }
#endif/* ... */
void vPortEnableInterrupts( void )
{
#if ( configSUPPORT_PICO_SYNC_INTEROP == 1 )
int xCoreID = ( int ) portGET_CORE_ID();
if( pxYieldSpinLock[ xCoreID ] )
{
spin_lock_t * const pxTmpLock = pxYieldSpinLock[ xCoreID ];
pxYieldSpinLock[ xCoreID ] = NULL;
spin_unlock( pxTmpLock, ulYieldSpinLockSaveValue[ xCoreID ] );
}if (pxYieldSpinLock[ xCoreID ]) { ... }
/* ... */ #endif
__asm volatile ( " cpsie i " ::: "memory" );
}{ ... }
uint32_t ulSetInterruptMaskFromISR( void )
{
__asm volatile (
" mrs r0, PRIMASK \n"
" cpsid i \n"
" bx lr "
::: "memory"
);
}{ ... }
void vClearInterruptMaskFromISR( __attribute__( ( unused ) ) uint32_t ulMask )
{
__asm volatile (
" msr PRIMASK, r0 \n"
" bx lr "
::: "memory"
);
}{ ... }
void vYieldCore( int xCoreID )
{
/* ... */
( void ) xCoreID;
configASSERT( xCoreID != ( int ) portGET_CORE_ID() );
#if portRUNNING_ON_BOTH_CORES
/* ... */
sio_hw->fifo_wr = 0;/* ... */
#endif
}{ ... }
void xPortPendSVHandler( void )
{
#if ( configNUMBER_OF_CORES == 1 )
__asm volatile
(
" .syntax unified \n"
" mrs r0, psp \n"
" \n"
" ldr r3, pxCurrentTCBConst2 \n"
" ldr r2, [r3] \n"
" \n"
" subs r0, r0, #32 \n"
" str r0, [r2] \n"
" stmia r0!, {r4-r7} \n"
" mov r4, r8 \n"
" mov r5, r9 \n"
" mov r6, r10 \n"
" mov r7, r11 \n"
" stmia r0!, {r4-r7} \n"
#if portUSE_DIVIDER_SAVE_RESTORE
" movs r2, #0xd \n"
" lsls r2, #28 \n"
/* ... */
" ldr r4, [r2, #0x60] \n"
" ldr r5, [r2, #0x64] \n"
" ldr r6, [r2, #0x74] \n"
" ldr r7, [r2, #0x70] \n"
/* ... */
" subs r0, r0, #48 \n"
" stmia r0!, {r4-r7} \n"/* ... */
#endif
" push {r3, r14} \n"
" cpsid i \n"
" bl vTaskSwitchContext \n"
" cpsie i \n"
" pop {r2, r3} \n"
" \n"
" ldr r1, [r2] \n"
" ldr r0, [r1] \n"
" adds r0, r0, #16 \n"
" ldmia r0!, {r4-r7} \n"
" mov r8, r4 \n"
" mov r9, r5 \n"
" mov r10, r6 \n"
" mov r11, r7 \n"
" \n"
" msr psp, r0 \n"
" \n"
#if portUSE_DIVIDER_SAVE_RESTORE
" movs r2, #0xd \n"
" lsls r2, #28 \n"
" subs r0, r0, #48 \n"
" ldmia r0!, {r4-r7} \n"
/* ... */
" str r4, [r2, #0x60] \n"
" str r5, [r2, #0x64] \n"
" str r6, [r2, #0x74] \n"
" str r7, [r2, #0x70] \n" /* ... */
#else
" subs r0, r0, #32 \n"
#endif
" ldmia r0!, {r4-r7} \n"
" \n"
" bx r3 \n"
" .align 4 \n"
"pxCurrentTCBConst2: .word pxCurrentTCB \n"
);/* ... */
#else
__asm volatile
(
" .syntax unified \n"
" mrs r1, psp \n"
" \n"
" adr r0, ulAsmLocals2 \n"
" ldmia r0!, {r2, r3} \n"
#if portRUNNING_ON_BOTH_CORES
" ldr r0, [r2] \n"
" lsls r0, r0, #2 \n"
" adds r3, r0 \n" /* ... */
#else
" \n"
#endif
" ldr r0, [r3] \n"
" \n"
" subs r1, r1, #32 \n"
" str r1, [r0] \n"
" stmia r1!, {r4-r7} \n"
" mov r4, r8 \n"
" mov r5, r9 \n"
" mov r6, r10 \n"
" mov r7, r11 \n"
" stmia r1!, {r4-r7} \n"
#if portUSE_DIVIDER_SAVE_RESTORE
/* ... */
" ldr r4, [r2, #0x60] \n"
" ldr r5, [r2, #0x64] \n"
" ldr r6, [r2, #0x74] \n"
" ldr r7, [r2, #0x70] \n"
/* ... */
" subs r1, r1, #48 \n"
" stmia r1!, {r4-r7} \n"/* ... */
#endif
#if portRUNNING_ON_BOTH_CORES
" ldr r0, [r2] \n"
#else
" movs r0, #0 \n"
#endif
" push {r3, r14} \n"
" cpsid i \n"
" bl vTaskSwitchContext \n"
" cpsie i \n"
" pop {r2, r3} \n"
" \n"
" ldr r1, [r2] \n"
" ldr r0, [r1] \n"
" adds r0, r0, #16 \n"
" ldmia r0!, {r4-r7} \n"
" mov r8, r4 \n"
" mov r9, r5 \n"
" mov r10, r6 \n"
" mov r11, r7 \n"
" \n"
" msr psp, r0 \n"
" \n"
#if portUSE_DIVIDER_SAVE_RESTORE
" movs r2, #0xd \n"
" lsls r2, #28 \n"
" subs r0, r0, #48 \n"
" ldmia r0!, {r4-r7} \n"
/* ... */
" str r4, [r2, #0x60] \n"
" str r5, [r2, #0x64] \n"
" str r6, [r2, #0x74] \n"
" str r7, [r2, #0x70] \n" /* ... */
#else
" subs r0, r0, #32 \n"
#endif
" ldmia r0!, {r4-r7} \n"
" \n"
" bx r3 \n"
" \n"
" .align 4 \n"
"ulAsmLocals2: \n"
" .word 0xD0000000 \n"
" .word pxCurrentTCBs \n"
);/* ... */
#endif
}{ ... }
void xPortSysTickHandler( void )
{
uint32_t ulPreviousMask;
ulPreviousMask = taskENTER_CRITICAL_FROM_ISR();
traceISR_ENTER();
{
if( xTaskIncrementTick() != pdFALSE )
{
traceISR_EXIT_TO_SCHEDULER();
portNVIC_INT_CTRL_REG = portNVIC_PENDSVSET_BIT;
}if (xTaskIncrementTick() != pdFALSE) { ... }
else
{
traceISR_EXIT();
}else { ... }
...}
taskEXIT_CRITICAL_FROM_ISR( ulPreviousMask );
}{ ... }
/* ... */
__attribute__( ( weak ) ) void vPortSetupTimerInterrupt( void )
{
#if ( configUSE_TICKLESS_IDLE == 1 )
{
ulTimerCountsForOneTick = ( clock_get_hz( clk_sys ) / configTICK_RATE_HZ );
xMaximumPossibleSuppressedTicks = portMAX_24_BIT_NUMBER / ulTimerCountsForOneTick;
ulStoppedTimerCompensation = portMISSED_COUNTS_FACTOR;
...}/* ... */
#endif
portNVIC_SYSTICK_CTRL_REG = 0UL;
portNVIC_SYSTICK_CURRENT_VALUE_REG = 0UL;
portNVIC_SYSTICK_LOAD_REG = ( clock_get_hz( clk_sys ) / configTICK_RATE_HZ ) - 1UL;
portNVIC_SYSTICK_CTRL_REG = portNVIC_SYSTICK_CLK_BIT | portNVIC_SYSTICK_INT_BIT | portNVIC_SYSTICK_ENABLE_BIT;
}{ ... }
#if ( configUSE_TICKLESS_IDLE == 1 )
__attribute__( ( weak ) ) void vPortSuppressTicksAndSleep( TickType_t xExpectedIdleTime )
{
uint32_t ulReloadValue, ulCompleteTickPeriods, ulCompletedSysTickDecrements;
TickType_t xModifiableIdleTime;
if( xExpectedIdleTime > xMaximumPossibleSuppressedTicks )
{
xExpectedIdleTime = xMaximumPossibleSuppressedTicks;
}if (xExpectedIdleTime > xMaximumPossibleSuppressedTicks) { ... }
/* ... */
portNVIC_SYSTICK_CTRL_REG &= ~portNVIC_SYSTICK_ENABLE_BIT;
/* ... */
ulReloadValue = portNVIC_SYSTICK_CURRENT_VALUE_REG + ( ulTimerCountsForOneTick * ( xExpectedIdleTime - 1UL ) );
if( ulReloadValue > ulStoppedTimerCompensation )
{
ulReloadValue -= ulStoppedTimerCompensation;
}if (ulReloadValue > ulStoppedTimerCompensation) { ... }
/* ... */
__asm volatile ( "cpsid i" ::: "memory" );
__asm volatile ( "dsb" );
__asm volatile ( "isb" );
/* ... */
if( eTaskConfirmSleepModeStatus() == eAbortSleep )
{
/* ... */
portNVIC_SYSTICK_LOAD_REG = portNVIC_SYSTICK_CURRENT_VALUE_REG;
portNVIC_SYSTICK_CTRL_REG |= portNVIC_SYSTICK_ENABLE_BIT;
/* ... */
portNVIC_SYSTICK_LOAD_REG = ulTimerCountsForOneTick - 1UL;
/* ... */
__asm volatile ( "cpsie i" ::: "memory" );
}if (eTaskConfirmSleepModeStatus() == eAbortSleep) { ... }
else
{
portNVIC_SYSTICK_LOAD_REG = ulReloadValue;
/* ... */
portNVIC_SYSTICK_CURRENT_VALUE_REG = 0UL;
portNVIC_SYSTICK_CTRL_REG |= portNVIC_SYSTICK_ENABLE_BIT;
/* ... */
xModifiableIdleTime = xExpectedIdleTime;
configPRE_SLEEP_PROCESSING( xModifiableIdleTime );
if( xModifiableIdleTime > 0 )
{
__asm volatile ( "dsb" ::: "memory" );
__asm volatile ( "wfi" );
__asm volatile ( "isb" );
}if (xModifiableIdleTime > 0) { ... }
configPOST_SLEEP_PROCESSING( xExpectedIdleTime );
/* ... */
__asm volatile ( "cpsie i" ::: "memory" );
__asm volatile ( "dsb" );
__asm volatile ( "isb" );
/* ... */
__asm volatile ( "cpsid i" ::: "memory" );
__asm volatile ( "dsb" );
__asm volatile ( "isb" );
/* ... */
portNVIC_SYSTICK_CTRL_REG = ( portNVIC_SYSTICK_CLK_BIT | portNVIC_SYSTICK_INT_BIT );
/* ... */
if( ( portNVIC_SYSTICK_CTRL_REG & portNVIC_SYSTICK_COUNT_FLAG_BIT ) != 0 )
{
uint32_t ulCalculatedLoadValue;
/* ... */
ulCalculatedLoadValue = ( ulTimerCountsForOneTick - 1UL ) - ( ulReloadValue - portNVIC_SYSTICK_CURRENT_VALUE_REG );
/* ... */
if( ( ulCalculatedLoadValue < ulStoppedTimerCompensation ) || ( ulCalculatedLoadValue > ulTimerCountsForOneTick ) )
{
ulCalculatedLoadValue = ( ulTimerCountsForOneTick - 1UL );
}if (( ulCalculatedLoadValue < ulStoppedTimerCompensation ) || ( ulCalculatedLoadValue > ulTimerCountsForOneTick )) { ... }
portNVIC_SYSTICK_LOAD_REG = ulCalculatedLoadValue;
/* ... */
ulCompleteTickPeriods = xExpectedIdleTime - 1UL;
}if (( portNVIC_SYSTICK_CTRL_REG & portNVIC_SYSTICK_COUNT_FLAG_BIT ) != 0) { ... }
else
{
/* ... */
ulCompletedSysTickDecrements = ( xExpectedIdleTime * ulTimerCountsForOneTick ) - portNVIC_SYSTICK_CURRENT_VALUE_REG;
/* ... */
ulCompleteTickPeriods = ulCompletedSysTickDecrements / ulTimerCountsForOneTick;
/* ... */
portNVIC_SYSTICK_LOAD_REG = ( ( ulCompleteTickPeriods + 1UL ) * ulTimerCountsForOneTick ) - ulCompletedSysTickDecrements;
}else { ... }
/* ... */
portNVIC_SYSTICK_CURRENT_VALUE_REG = 0UL;
portNVIC_SYSTICK_CTRL_REG |= portNVIC_SYSTICK_ENABLE_BIT;
vTaskStepTick( ulCompleteTickPeriods );
portNVIC_SYSTICK_LOAD_REG = ulTimerCountsForOneTick - 1UL;
__asm volatile ( "cpsie i" ::: "memory" );
}else { ... }
}vPortSuppressTicksAndSleep (TickType_t xExpectedIdleTime) { ... }
/* ... */
#endif
#if ( configSUPPORT_PICO_SYNC_INTEROP == 1 ) || ( configSUPPORT_PICO_TIME_INTEROP == 1 )
static TickType_t prvGetTicksToWaitBefore( absolute_time_t t )
{
int64_t xDelay = absolute_time_diff_us( get_absolute_time(), t );
const uint32_t ulTickPeriod = 1000000 / configTICK_RATE_HZ;
xDelay -= ulTickPeriod;
if( xDelay >= ulTickPeriod )
{
return xDelay / ulTickPeriod;
}if (xDelay >= ulTickPeriod) { ... }
return 0;
}{ ... }
#endif/* ... */
#if ( configSUPPORT_PICO_SYNC_INTEROP == 1 )
uint32_t ulPortLockGetCurrentOwnerId()
{
if( portIS_FREE_RTOS_CORE() )
{
uint32_t exception = __get_current_exception();
if( !exception )
{
return ( uintptr_t ) xTaskGetCurrentTaskHandle();
}if (!exception) { ... }
/* ... */
return get_core_num() + exception * 2;
}if (portIS_FREE_RTOS_CORE()) { ... }
/* ... */
return get_core_num();
}{ ... }
static inline EventBits_t prvGetEventGroupBit( spin_lock_t * spinLock )
{
uint32_t ulBit;
#if ( configTICK_TYPE_WIDTH_IN_BITS == TICK_TYPE_WIDTH_16_BITS )
ulBit = 1u << ( spin_lock_get_num( spinLock ) & 0x7u );
#elif ( configTICK_TYPE_WIDTH_IN_BITS == TICK_TYPE_WIDTH_32_BITS )
ulBit = 1u << spin_lock_get_num( spinLock );
ulBit |= ulBit << 8u;
ulBit >>= 8u;/* ... */
#endif
return ( EventBits_t ) ulBit;
}{ ... }
static inline EventBits_t prvGetAllEventGroupBits()
{
#if ( configTICK_TYPE_WIDTH_IN_BITS == TICK_TYPE_WIDTH_16_BITS )
return ( EventBits_t ) 0xffu;
#elif ( configTICK_TYPE_WIDTH_IN_BITS == TICK_TYPE_WIDTH_32_BITS )
return ( EventBits_t ) 0xffffffu;
#endif
}{ ... }
void vPortLockInternalSpinUnlockWithWait( struct lock_core * pxLock,
uint32_t ulSave )
{
configASSERT( !portCHECK_IF_IN_ISR() );
if( !portIS_FREE_RTOS_CORE() )
{
spin_unlock( pxLock->spin_lock, ulSave );
__wfe();
}if (!portIS_FREE_RTOS_CORE()) { ... }
else
{
configASSERT( pxYieldSpinLock[ portGET_CORE_ID() ] == NULL );
configASSERT( pxLock->spin_lock );
int xCoreID = ( int ) portGET_CORE_ID();
pxYieldSpinLock[ xCoreID ] = pxLock->spin_lock;
ulYieldSpinLockSaveValue[ xCoreID ] = ulSave;
xEventGroupWaitBits( xEventGroup, prvGetEventGroupBit( pxLock->spin_lock ),
pdTRUE, pdFALSE, portMAX_DELAY );
}else { ... }
}{ ... }
void vPortLockInternalSpinUnlockWithNotify( struct lock_core * pxLock,
uint32_t ulSave )
{
EventBits_t uxBits = prvGetEventGroupBit( pxLock->spin_lock );
if( portIS_FREE_RTOS_CORE() )
{
#if LIB_PICO_MULTICORE
__sev();/* ... */
#endif
spin_unlock( pxLock->spin_lock, ulSave );
if( !portCHECK_IF_IN_ISR() )
{
xEventGroupSetBits( xEventGroup, uxBits );
}if (!portCHECK_IF_IN_ISR()) { ... }
else
{
BaseType_t xHigherPriorityTaskWoken = pdFALSE;
xEventGroupSetBitsFromISR( xEventGroup, uxBits, &xHigherPriorityTaskWoken );
portYIELD_FROM_ISR( xHigherPriorityTaskWoken );
}else { ... }
}if (portIS_FREE_RTOS_CORE()) { ... }
else
{
__sev();
#if ( portRUNNING_ON_BOTH_CORES == 0 )
/* ... */
if( pxCrossCoreSpinLock != pxLock->spin_lock )
{
spin_lock_unsafe_blocking( pxCrossCoreSpinLock );
uxCrossCoreEventBits |= uxBits;
spin_unlock_unsafe( pxCrossCoreSpinLock );
}if (pxCrossCoreSpinLock != pxLock->spin_lock) { ... }
else
{
uxCrossCoreEventBits |= uxBits;
}else { ... }
sio_hw->fifo_wr = 0;/* ... */
#endif
spin_unlock( pxLock->spin_lock, ulSave );
}else { ... }
}{ ... }
bool xPortLockInternalSpinUnlockWithBestEffortWaitOrTimeout( struct lock_core * pxLock,
uint32_t ulSave,
absolute_time_t uxUntil )
{
configASSERT( !portCHECK_IF_IN_ISR() );
if( !portIS_FREE_RTOS_CORE() )
{
spin_unlock( pxLock->spin_lock, ulSave );
return best_effort_wfe_or_timeout( uxUntil );
}if (!portIS_FREE_RTOS_CORE()) { ... }
else
{
configASSERT( portIS_FREE_RTOS_CORE() );
configASSERT( pxYieldSpinLock[ portGET_CORE_ID() ] == NULL );
TickType_t uxTicksToWait = prvGetTicksToWaitBefore( uxUntil );
if( uxTicksToWait )
{
/* ... */
configASSERT( pxLock->spin_lock );
int xCoreID = ( int ) portGET_CORE_ID();
pxYieldSpinLock[ xCoreID ] = pxLock->spin_lock;
ulYieldSpinLockSaveValue[ xCoreID ] = ulSave;
xEventGroupWaitBits( xEventGroup,
prvGetEventGroupBit( pxLock->spin_lock ), pdTRUE,
pdFALSE, uxTicksToWait );
}if (uxTicksToWait) { ... }
else
{
spin_unlock( pxLock->spin_lock, ulSave );
}else { ... }
if( time_reached( uxUntil ) )
{
return true;
}if (time_reached( uxUntil )) { ... }
else
{
portYIELD();
return false;
}else { ... }
}else { ... }
}{ ... }
#if ( configSUPPORT_PICO_SYNC_INTEROP == 1 )
static void __attribute__( ( constructor ) ) prvRuntimeInitializer( void )
{
/* ... */
#if ( portRUNNING_ON_BOTH_CORES == 0 )
pxCrossCoreSpinLock = spin_lock_instance( next_striped_spin_lock_num() );
#endif
/* ... */
#if ( configSUPPORT_STATIC_ALLOCATION == 1 )
xEventGroup = xEventGroupCreateStatic( &xStaticEventGroup );
#else
/* ... */
xEventGroup = xEventGroupCreate();/* ... */
#endif
}{ ... }
/* ... */#endif /* ... */
#endif
#if ( configSUPPORT_PICO_TIME_INTEROP == 1 )
void xPortSyncInternalYieldUntilBefore( absolute_time_t t )
{
TickType_t uxTicksToWait = prvGetTicksToWaitBefore( t );
if( uxTicksToWait )
{
vTaskDelay( uxTicksToWait );
}if (uxTicksToWait) { ... }
}{ ... }
#endif/* ... */