138138#define portBIT_0_SET ( ( uint8_t ) 0x01 )
139139
140140/* The space on the stack required to hold the FPU registers.
141- * There are 32 128-bit plus 2 64-bit status registers.*/
141+ * There are 32 128-bit plus 2 64-bit status registers. */
142142#define portFPU_REGISTER_WORDS ( ( 32 * 2 ) + 2 )
143143
144144/*-----------------------------------------------------------*/
@@ -173,7 +173,7 @@ extern void vGIC_EnableCPUInterface( void );
173173 volatile uint64_t ullCriticalNestings [ configNUMBER_OF_CORES ] = { 0 };
174174
175175 /* Saved as part of the task context. If ullPortTaskHasFPUContext is non-zero
176- * then floating point context must be saved and restored for the task. */
176+ * then floating point context must be saved and restored for the task. */
177177 uint64_t ullPortTaskHasFPUContext [ configNUMBER_OF_CORES ] = { pdFALSE };
178178 uint64_t ullPortYieldRequired [ configNUMBER_OF_CORES ] = { pdFALSE };
179179 uint64_t ullPortInterruptNestings [ configNUMBER_OF_CORES ] = { 0 };
@@ -190,12 +190,12 @@ __attribute__( ( used ) ) const uint64_t ullMaxAPIPriorityMask = ( configMAX_API
190190/*
191191 * See header file for description.
192192 */
193- StackType_t * pxPortInitialiseStack ( StackType_t * pxTopOfStack ,
194- TaskFunction_t pxCode ,
195- void * pvParameters )
193+ StackType_t * pxPortInitialiseStack ( StackType_t * pxTopOfStack ,
194+ TaskFunction_t pxCode ,
195+ void * pvParameters )
196196{
197197 /* Setup the initial stack of the task. The stack is set exactly as
198- * expected by the portRESTORE_CONTEXT() macro. */
198+ * expected by the portRESTORE_CONTEXT() macro. */
199199
200200 /* First all the general purpose registers. */
201201 pxTopOfStack -- ;
@@ -272,25 +272,25 @@ __attribute__( ( used ) ) const uint64_t ullMaxAPIPriorityMask = ( configMAX_API
272272 #if ( configUSE_TASK_FPU_SUPPORT == portTASK_NO_FPU_CONTEXT_BY_DEFAULT )
273273 {
274274 /* The task will start with a critical nesting count of 0 as interrupts are
275- * enabled. */
275+ * enabled. */
276276 pxTopOfStack -- ;
277277 * pxTopOfStack = portNO_CRITICAL_NESTING ;
278278
279279 /* The task will start without a floating point context. A task that
280- * uses the floating point hardware must call vPortTaskUsesFPU() before
281- * executing any floating point instructions. */
280+ * uses the floating point hardware must call vPortTaskUsesFPU() before
281+ * executing any floating point instructions. */
282282 pxTopOfStack -- ;
283283 * pxTopOfStack = portNO_FLOATING_POINT_CONTEXT ;
284284 }
285285 #elif ( configUSE_TASK_FPU_SUPPORT == portTASK_HAVE_FPU_CONTEXT_BY_DEFAULT )
286286 {
287287 /* The task will start with a floating point context. Leave enough
288- * space for the registers - and ensure they are initialised to 0. */
288+ * space for the registers - and ensure they are initialised to 0. */
289289 pxTopOfStack -= portFPU_REGISTER_WORDS ;
290290 memset ( pxTopOfStack , 0x00 , portFPU_REGISTER_WORDS * sizeof ( StackType_t ) );
291291
292292 /* The task will start with a critical nesting count of 0 as interrupts are
293- * enabled. */
293+ * enabled. */
294294 pxTopOfStack -- ;
295295 * pxTopOfStack = portNO_CRITICAL_NESTING ;
296296
@@ -321,18 +321,18 @@ BaseType_t xPortStartScheduler( void )
321321 #if ( configASSERT_DEFINED == 1 )
322322 {
323323 if ( portGET_CORE_ID () == 0 )
324- {
324+ {
325325 volatile uint8_t ucOriginalPriority ;
326326 volatile uint8_t * const pucFirstUserPriorityRegister = ( volatile uint8_t * const ) ( configINTERRUPT_CONTROLLER_BASE_ADDRESS + portINTERRUPT_PRIORITY_REGISTER_OFFSET );
327327 volatile uint8_t ucMaxPriorityValue ;
328328
329329 /* Determine how many priority bits are implemented in the GIC.
330- *
331- * Save the interrupt priority value that is about to be clobbered. */
330+ *
331+ * Save the interrupt priority value that is about to be clobbered. */
332332 ucOriginalPriority = * pucFirstUserPriorityRegister ;
333333
334334 /* Determine the number of priority bits available. First write to
335- * all possible bits. */
335+ * all possible bits. */
336336 * pucFirstUserPriorityRegister = portMAX_8_BIT_VALUE ;
337337
338338 /* Read the value back to see how many bits stuck. */
@@ -345,13 +345,12 @@ BaseType_t xPortStartScheduler( void )
345345 }
346346
347347 /* Sanity check configUNIQUE_INTERRUPT_PRIORITIES matches the read
348- * value. */
349-
348+ * value. */
350349 configASSERT ( ucMaxPriorityValue >= portLOWEST_INTERRUPT_PRIORITY );
351350
352351
353352 /* Restore the clobbered interrupt priority register to its original
354- * value. */
353+ * value. */
355354 * pucFirstUserPriorityRegister = ucOriginalPriority ;
356355 }
357356 }
@@ -368,14 +367,14 @@ BaseType_t xPortStartScheduler( void )
368367 * executing. */
369368 portDISABLE_INTERRUPTS ();
370369 #if ( configNUMBER_OF_CORES > 1 )
371- if ( 0 == portGET_CORE_ID () )
370+ if ( portGET_CORE_ID () == 0 )
372371 {
373372 /* Start the timer that generates the tick ISR. */
374373 configSETUP_TICK_INTERRUPT ();
375374 ucPortSchedulerRunning = pdTRUE ;
376- __asm__ volatile ("dsb sy" );
377- /* Start all other Cores and let them execute vPortRestoreTaskContext()*/
378- __asm__ volatile ("sev" );
375+ __asm__ volatile ( "dsb sy" );
376+ /* Start all other Cores and let them execute vPortRestoreTaskContext(). */
377+ __asm__ volatile ( "sev" );
379378 }
380379 else
381380 {
@@ -385,6 +384,7 @@ BaseType_t xPortStartScheduler( void )
385384 /* Start the timer that generates the tick ISR. */
386385 configSETUP_TICK_INTERRUPT ();
387386 #endif /* if ( configNUMBER_OF_CORES > 1 ) */
387+
388388 /* Start the first task executing. */
389389 vPortRestoreTaskContext ();
390390
@@ -455,12 +455,13 @@ void FreeRTOS_Tick_Handler( void )
455455 __asm volatile ( "MRS %0, ICC_RPR_EL1" : "=r" ( ullRunningInterruptPriority ) );
456456
457457 configASSERT ( ullRunningInterruptPriority == ( portLOWEST_USABLE_INTERRUPT_PRIORITY << portPRIORITY_SHIFT ) );
458+
458459 /* Interrupts should not be enabled before this point. */
459460 #if ( configASSERT_DEFINED == 1 )
460461 {
461462 uint64_t ullMaskBits ;
462463
463- __asm volatile ( "mrs %0, DAIF" : "=r" ( ullMaskBits )::"memory" );
464+ __asm volatile ( "MRS %0, DAIF" : "=r" ( ullMaskBits )::"memory" );
464465 configASSERT ( ( ullMaskBits & portDAIF_I ) != 0 );
465466 }
466467 #endif /* configASSERT_DEFINED */
@@ -482,6 +483,7 @@ void FreeRTOS_Tick_Handler( void )
482483 #if ( configNUMBER_OF_CORES > 1 )
483484 UBaseType_t x = portENTER_CRITICAL_FROM_ISR ();
484485 #endif /* if ( configNUMBER_OF_CORES > 1 ) */
486+
485487 /* Increment the RTOS tick. */
486488 if ( xTaskIncrementTick () != pdFALSE )
487489 {
@@ -494,6 +496,7 @@ void FreeRTOS_Tick_Handler( void )
494496 #if ( configNUMBER_OF_CORES > 1 )
495497 portEXIT_CRITICAL_FROM_ISR (x );
496498 #endif /* if ( configNUMBER_OF_CORES > 1 ) */
499+
497500 /* Ensure all interrupt priorities are active again. */
498501 portCLEAR_INTERRUPT_PRIORITIES_MASK ();
499502}
@@ -613,25 +616,29 @@ UBaseType_t uxPortSetInterruptMask( void )
613616 configASSERT ( ( volatile void * ) NULL );
614617}
615618
619+ /*-----------------------------------------------------------*/
620+
616621#if ( configNUMBER_OF_CORES > 1 )
617622
618- /* Which core owns the lock */
623+ /* Which core owns the lock? */
619624 volatile uint64_t ucOwnedByCore [ portMAX_CORE_COUNT ];
620- /* Lock count a core owns */
625+ /* Lock count a core owns. */
621626 volatile uint64_t ucRecursionCountByLock [ eLockCount ];
622- /* Index 0 is used for ISR lock and Index 1 is used for task lock */
627+ /* Index 0 is used for ISR lock and Index 1 is used for task lock. */
623628 uint32_t ulGateWord [ eLockCount ];
624629
625- void vInterruptCore (uint32_t ulInterruptID , uint32_t ulCoreID )
630+ void vInterruptCore ( uint32_t ulInterruptID , uint32_t ulCoreID )
626631 {
627632 uint64_t ulRegVal = 0 ;
628- uint32_t ulCoreMask = (1UL << ulCoreID );
633+ uint32_t ulCoreMask = ( 1UL << ulCoreID );
629634 ulRegVal |= ( (ulCoreMask & 0xFFFF ) | ( ( ulInterruptID & 0xF ) << 24U ) );
630635 __asm__ volatile ( "msr ICC_SGI1R_EL1, %0" : : "r" ( ulRegVal ) );
631636 __asm__ volatile ( "dsb sy" );
632637 __asm__ volatile ( "isb sy" );
633638 }
634639
640+ /*-----------------------------------------------------------*/
641+
635642 static inline void prvSpinUnlock ( uint32_t * ulLock )
636643 {
637644 __asm volatile (
@@ -646,6 +653,8 @@ UBaseType_t uxPortSetInterruptMask( void )
646653 );
647654 }
648655
656+ /*-----------------------------------------------------------*/
657+
649658 static inline uint32_t prvSpinTrylock ( uint32_t * ulLock )
650659 {
651660 register uint32_t ulRet ;
@@ -669,26 +678,32 @@ UBaseType_t uxPortSetInterruptMask( void )
669678 return ulRet ;
670679 }
671680
672- /* Read 64b value shared between cores */
681+ /*-----------------------------------------------------------*/
682+
683+ /* Read 64b value shared between cores. */
673684 static inline uint64_t prvGet64 ( volatile uint64_t * x )
674685 {
675686 __asm( "dsb sy" );
676687 return * x ;
677688 }
678689
679- /* Write 64b value shared between cores */
690+ /*-----------------------------------------------------------*/
691+
692+ /* Write 64b value shared between cores. */
680693 static inline void prvSet64 ( volatile uint64_t * x ,
681694 uint64_t value )
682695 {
683696 * x = value ;
684697 __asm( "dsb sy" );
685698 }
686699
700+ /*-----------------------------------------------------------*/
701+
687702 void vPortRecursiveLock ( BaseType_t xCoreID ,
688703 ePortRTOSLock eLockNum ,
689704 BaseType_t uxAcquire )
690705 {
691- /* Validate the core ID and lock number */
706+ /* Validate the core ID and lock number. */
692707 configASSERT ( xCoreID < portMAX_CORE_COUNT );
693708 configASSERT ( eLockNum < eLockCount );
694709
@@ -697,21 +712,21 @@ UBaseType_t uxPortSetInterruptMask( void )
697712 /* Lock acquire */
698713 if ( uxAcquire )
699714 {
700- /* Check if spinlock is available */
701- /* If spinlock is not available check if the core owns the lock */
702- /* If the core owns the lock wait increment the lock count by the core */
703- /* If core does not own the lock wait for the spinlock */
715+ /* Check if spinlock is available. */
716+ /* If spinlock is not available check if the core owns the lock. */
717+ /* If the core owns the lock wait increment the lock count by the core. */
718+ /* If core does not own the lock wait for the spinlock. */
704719 if ( prvSpinTrylock ( & ulGateWord [ eLockNum ] ) != 0 )
705720 {
706- /* Check if the core owns the spinlock */
721+ /* Check if the core owns the spinlock. */
707722 if ( prvGet64 ( & ucOwnedByCore [ xCoreID ] ) & ulLockBit )
708723 {
709724 configASSERT ( prvGet64 ( & ucRecursionCountByLock [ eLockNum ] ) != 255u );
710725 prvSet64 ( & ucRecursionCountByLock [ eLockNum ], ( prvGet64 ( & ucRecursionCountByLock [ eLockNum ] ) + 1 ) );
711726 return ;
712727 }
713728
714- /* Preload the gate word into the cache */
729+ /* Preload the gate word into the cache. */
715730 uint32_t dummy = ulGateWord [ eLockNum ];
716731 dummy ++ ;
717732
@@ -721,37 +736,39 @@ UBaseType_t uxPortSetInterruptMask( void )
721736 }
722737 }
723738
724- /* Add barrier to ensure lock is taken before we proceed */
739+ /* Add barrier to ensure lock is taken before we proceed. */
725740 __asm__ __volatile__ ( "dmb sy" ::: "memory" );
726741
727- /* Assert the lock count is 0 when the spinlock is free and is acquired */
742+ /* Assert the lock count is 0 when the spinlock is free and is acquired. */
728743 configASSERT ( prvGet64 ( & ucRecursionCountByLock [ eLockNum ] ) == 0 );
729744
730- /* Set lock count as 1 */
745+ /* Set lock count as 1. */
731746 prvSet64 ( & ucRecursionCountByLock [ eLockNum ], 1 );
732- /* Set ucOwnedByCore */
747+ /* Set ucOwnedByCore. */
733748 prvSet64 ( & ucOwnedByCore [ xCoreID ], ( prvGet64 ( & ucOwnedByCore [ xCoreID ] ) | ulLockBit ) );
734749 }
735- /* Lock release */
750+ /* Lock release. */
736751 else
737752 {
738- /* Assert the lock is not free already */
753+ /* Assert the lock is not free already. */
739754 configASSERT ( ( prvGet64 ( & ucOwnedByCore [ xCoreID ] ) & ulLockBit ) != 0 );
740755 configASSERT ( prvGet64 ( & ucRecursionCountByLock [ eLockNum ] ) != 0 );
741756
742- /* Reduce ucRecursionCountByLock by 1 */
757+ /* Reduce ucRecursionCountByLock by 1. */
743758 prvSet64 ( & ucRecursionCountByLock [ eLockNum ], ( prvGet64 ( & ucRecursionCountByLock [ eLockNum ] ) - 1 ) );
744759
745760 if ( !prvGet64 ( & ucRecursionCountByLock [ eLockNum ] ) )
746761 {
747762 prvSet64 ( & ucOwnedByCore [ xCoreID ], ( prvGet64 ( & ucOwnedByCore [ xCoreID ] ) & ~ulLockBit ) );
748763 prvSpinUnlock ( & ulGateWord [ eLockNum ] );
749- /* Add barrier to ensure lock status is reflected before we proceed */
764+ /* Add barrier to ensure lock status is reflected before we proceed. */
750765 __asm__ __volatile__ ( "dmb sy" ::: "memory" );
751766 }
752767 }
753768 }
754769
770+ /*-----------------------------------------------------------*/
771+
755772 BaseType_t xPortGetCoreID ( void )
756773 {
757774 register BaseType_t xCoreID ;
@@ -767,6 +784,8 @@ UBaseType_t uxPortSetInterruptMask( void )
767784 return xCoreID ;
768785 }
769786
787+ /*-----------------------------------------------------------*/
788+
770789 void FreeRTOS_SGI_Handler ( void )
771790 {
772791 /* Must be the lowest possible priority. */
@@ -785,10 +804,10 @@ UBaseType_t uxPortSetInterruptMask( void )
785804 #endif /* configASSERT_DEFINED */
786805
787806 /* Set interrupt mask before altering scheduler structures. The SGI
788- * handler runs at the lowest priority, so interrupts cannot already be masked,
789- * so there is no need to save and restore the current mask value. It is
790- * necessary to turn off interrupts in the CPU itself while the ICCPMR is being
791- * updated. */
807+ * handler runs at the lowest priority, so interrupts cannot already be masked,
808+ * so there is no need to save and restore the current mask value. It is
809+ * necessary to turn off interrupts in the CPU itself while the ICCPMR is being
810+ * updated. */
792811 __asm volatile ( "MSR ICC_PMR_EL1, %0 \n"
793812 "DSB SY \n"
794813 "ISB SY \n"
@@ -807,4 +826,6 @@ UBaseType_t uxPortSetInterruptMask( void )
807826 portCLEAR_INTERRUPT_PRIORITIES_MASK ();
808827 }
809828
829+ /*-----------------------------------------------------------*/
830+
810831#endif /* if( configNUMBER_OF_CORES > 1 ) */
0 commit comments