123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718 |
- #include "FreeRTOS.h"
- #include "task.h"
- #ifndef configKERNEL_INTERRUPT_PRIORITY
- #define configKERNEL_INTERRUPT_PRIORITY 255
- #endif
- #if configMAX_SYSCALL_INTERRUPT_PRIORITY == 0
- #error configMAX_SYSCALL_INTERRUPT_PRIORITY must not be set to 0. See http:
- #endif
- #ifndef configSYSTICK_CLOCK_HZ
- #define configSYSTICK_CLOCK_HZ configCPU_CLOCK_HZ
-
- #define portNVIC_SYSTICK_CLK_BIT ( 1UL << 2UL )
- #else
- #define portNVIC_SYSTICK_CLK_BIT ( 0 )
- #endif
- #ifndef configOVERRIDE_DEFAULT_TICK_CONFIGURATION
- #define configOVERRIDE_DEFAULT_TICK_CONFIGURATION 0
- #endif
- #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_SHPR3_REG ( *( ( volatile uint32_t * ) 0xe000ed20 ) )
- #define portNVIC_SYSTICK_INT_BIT ( 1UL << 1UL )
- #define portNVIC_SYSTICK_ENABLE_BIT ( 1UL << 0UL )
- #define portNVIC_SYSTICK_COUNT_FLAG_BIT ( 1UL << 16UL )
- #define portNVIC_PENDSVCLEAR_BIT ( 1UL << 27UL )
- #define portNVIC_PEND_SYSTICK_CLEAR_BIT ( 1UL << 25UL )
- #define portNVIC_PENDSV_PRI ( ( ( uint32_t ) configKERNEL_INTERRUPT_PRIORITY ) << 16UL )
- #define portNVIC_SYSTICK_PRI ( ( ( uint32_t ) configKERNEL_INTERRUPT_PRIORITY ) << 24UL )
- #define portFIRST_USER_INTERRUPT_NUMBER ( 16 )
- #define portNVIC_IP_REGISTERS_OFFSET_16 ( 0xE000E3F0 )
- #define portAIRCR_REG ( *( ( volatile uint32_t * ) 0xE000ED0C ) )
- #define portMAX_8_BIT_VALUE ( ( uint8_t ) 0xff )
- #define portTOP_BIT_OF_BYTE ( ( uint8_t ) 0x80 )
- #define portMAX_PRIGROUP_BITS ( ( uint8_t ) 7 )
- #define portPRIORITY_GROUP_MASK ( 0x07UL << 8UL )
- #define portPRIGROUP_SHIFT ( 8UL )
- #define portVECTACTIVE_MASK ( 0xFFUL )
- #define portINITIAL_XPSR ( 0x01000000 )
- #define portMAX_24_BIT_NUMBER ( 0xffffffUL )
- #define portMISSED_COUNTS_FACTOR ( 45UL )
- #define portSTART_ADDRESS_MASK ( ( StackType_t ) 0xfffffffeUL )
- void vPortSetupTimerInterrupt( void );
- void xPortPendSVHandler( void );
- void xPortSysTickHandler( void );
- void vPortSVCHandler( void );
- static void prvStartFirstTask( void );
- static void prvTaskExitError( void );
- static UBaseType_t uxCriticalNesting = 0xaaaaaaaa;
- #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
- #if ( configASSERT_DEFINED == 1 )
- static uint8_t ucMaxSysCallPriority = 0;
- static uint32_t ulMaxPRIGROUPValue = 0;
- static const volatile uint8_t * const pcInterruptPriorityRegisters = ( uint8_t * ) portNVIC_IP_REGISTERS_OFFSET_16;
- #endif
- StackType_t * pxPortInitialiseStack( StackType_t * pxTopOfStack,
- TaskFunction_t pxCode,
- void * pvParameters )
- {
-
- pxTopOfStack--;
- *pxTopOfStack = portINITIAL_XPSR;
- pxTopOfStack--;
- *pxTopOfStack = ( ( StackType_t ) pxCode ) & portSTART_ADDRESS_MASK;
- pxTopOfStack--;
- *pxTopOfStack = ( StackType_t ) prvTaskExitError;
- pxTopOfStack -= 5;
- *pxTopOfStack = ( StackType_t ) pvParameters;
- pxTopOfStack -= 8;
- return pxTopOfStack;
- }
- static void prvTaskExitError( void )
- {
-
- configASSERT( uxCriticalNesting == ~0UL );
- portDISABLE_INTERRUPTS();
- for( ; ; )
- {
- }
- }
- __asm void vPortSVCHandler( void )
- {
- PRESERVE8
- ldr r3, = pxCurrentTCB
- ldr r1, [ r3 ]
- ldr r0, [ r1 ]
- ldmia r0 !, { r4 - r11 }
- msr psp, r0
- isb
- mov r0, # 0
- msr basepri, r0
- orr r14, # 0xd
- bx r14
- }
- __asm void prvStartFirstTask( void )
- {
- PRESERVE8
-
- ldr r0, =0xE000ED08
- ldr r0, [ r0 ]
- ldr r0, [ r0 ]
-
- msr msp, r0
-
- cpsie i
- cpsie f
- dsb
- isb
-
- svc 0
- nop
- nop
- }
- BaseType_t xPortStartScheduler( void )
- {
- #if ( configASSERT_DEFINED == 1 )
- {
- volatile uint32_t ulOriginalPriority;
- volatile uint8_t * const pucFirstUserPriorityRegister = ( uint8_t * ) ( portNVIC_IP_REGISTERS_OFFSET_16 + portFIRST_USER_INTERRUPT_NUMBER );
- volatile uint8_t ucMaxPriorityValue;
-
- ulOriginalPriority = *pucFirstUserPriorityRegister;
-
- *pucFirstUserPriorityRegister = portMAX_8_BIT_VALUE;
-
- ucMaxPriorityValue = *pucFirstUserPriorityRegister;
-
- configASSERT( ucMaxPriorityValue == ( configKERNEL_INTERRUPT_PRIORITY & ucMaxPriorityValue ) );
-
- ucMaxSysCallPriority = configMAX_SYSCALL_INTERRUPT_PRIORITY & ucMaxPriorityValue;
-
- ulMaxPRIGROUPValue = portMAX_PRIGROUP_BITS;
- while( ( ucMaxPriorityValue & portTOP_BIT_OF_BYTE ) == portTOP_BIT_OF_BYTE )
- {
- ulMaxPRIGROUPValue--;
- ucMaxPriorityValue <<= ( uint8_t ) 0x01;
- }
- #ifdef __NVIC_PRIO_BITS
- {
-
- configASSERT( ( portMAX_PRIGROUP_BITS - ulMaxPRIGROUPValue ) == __NVIC_PRIO_BITS );
- }
- #endif
- #ifdef configPRIO_BITS
- {
-
- configASSERT( ( portMAX_PRIGROUP_BITS - ulMaxPRIGROUPValue ) == configPRIO_BITS );
- }
- #endif
-
- ulMaxPRIGROUPValue <<= portPRIGROUP_SHIFT;
- ulMaxPRIGROUPValue &= portPRIORITY_GROUP_MASK;
-
- *pucFirstUserPriorityRegister = ulOriginalPriority;
- }
- #endif
-
- portNVIC_SHPR3_REG |= portNVIC_PENDSV_PRI;
- portNVIC_SHPR3_REG |= portNVIC_SYSTICK_PRI;
-
- vPortSetupTimerInterrupt();
-
- uxCriticalNesting = 0;
-
- prvStartFirstTask();
-
- return 0;
- }
- void vPortEndScheduler( void )
- {
-
- configASSERT( uxCriticalNesting == 1000UL );
- }
- void vPortEnterCritical( void )
- {
- portDISABLE_INTERRUPTS();
- uxCriticalNesting++;
-
- if( uxCriticalNesting == 1 )
- {
- configASSERT( ( portNVIC_INT_CTRL_REG & portVECTACTIVE_MASK ) == 0 );
- }
- }
- void vPortExitCritical( void )
- {
- configASSERT( uxCriticalNesting );
- uxCriticalNesting--;
- if( uxCriticalNesting == 0 )
- {
- portENABLE_INTERRUPTS();
- }
- }
- __asm void xPortPendSVHandler( void )
- {
- extern uxCriticalNesting;
- extern pxCurrentTCB;
- extern vTaskSwitchContext;
- PRESERVE8
- mrs r0, psp
- isb
- ldr r3, =pxCurrentTCB
- ldr r2, [ r3 ]
- stmdb r0 !, { r4 - r11 }
- str r0, [ r2 ]
- stmdb sp !, { r3, r14 }
- mov r0, #configMAX_SYSCALL_INTERRUPT_PRIORITY
- msr basepri, r0
- dsb
- isb
- bl vTaskSwitchContext
- mov r0, #0
- msr basepri, r0
- ldmia sp !, { r3, r14 }
- ldr r1, [ r3 ]
- ldr r0, [ r1 ]
- ldmia r0 !, { r4 - r11 }
- msr psp, r0
- isb
- bx r14
- nop
- }
- void xPortSysTickHandler( void )
- {
-
- vPortRaiseBASEPRI();
- {
-
- if( xTaskIncrementTick() != pdFALSE )
- {
-
- portNVIC_INT_CTRL_REG = portNVIC_PENDSVSET_BIT;
- }
- }
- vPortClearBASEPRIFromISR();
- }
- #if ( configUSE_TICKLESS_IDLE == 1 )
- __weak void vPortSuppressTicksAndSleep( TickType_t xExpectedIdleTime )
- {
- uint32_t ulReloadValue, ulCompleteTickPeriods, ulCompletedSysTickDecrements;
- TickType_t xModifiableIdleTime;
-
- if( xExpectedIdleTime > xMaximumPossibleSuppressedTicks )
- {
- xExpectedIdleTime = xMaximumPossibleSuppressedTicks;
- }
-
- portNVIC_SYSTICK_CTRL_REG &= ~portNVIC_SYSTICK_ENABLE_BIT;
-
- ulReloadValue = portNVIC_SYSTICK_CURRENT_VALUE_REG + ( ulTimerCountsForOneTick * ( xExpectedIdleTime - 1UL ) );
- if( ulReloadValue > ulStoppedTimerCompensation )
- {
- ulReloadValue -= ulStoppedTimerCompensation;
- }
-
- __disable_irq();
- __dsb( portSY_FULL_READ_WRITE );
- __isb( portSY_FULL_READ_WRITE );
-
- 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;
-
- __enable_irq();
- }
- 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 )
- {
- __dsb( portSY_FULL_READ_WRITE );
- __wfi();
- __isb( portSY_FULL_READ_WRITE );
- }
- configPOST_SLEEP_PROCESSING( xExpectedIdleTime );
-
- __enable_irq();
- __dsb( portSY_FULL_READ_WRITE );
- __isb( portSY_FULL_READ_WRITE );
-
- __disable_irq();
- __dsb( portSY_FULL_READ_WRITE );
- __isb( portSY_FULL_READ_WRITE );
-
- 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 );
- }
- portNVIC_SYSTICK_LOAD_REG = ulCalculatedLoadValue;
-
- ulCompleteTickPeriods = xExpectedIdleTime - 1UL;
- }
- else
- {
-
- ulCompletedSysTickDecrements = ( xExpectedIdleTime * ulTimerCountsForOneTick ) - portNVIC_SYSTICK_CURRENT_VALUE_REG;
-
- ulCompleteTickPeriods = ulCompletedSysTickDecrements / ulTimerCountsForOneTick;
-
- portNVIC_SYSTICK_LOAD_REG = ( ( ulCompleteTickPeriods + 1UL ) * ulTimerCountsForOneTick ) - ulCompletedSysTickDecrements;
- }
-
- portNVIC_SYSTICK_CURRENT_VALUE_REG = 0UL;
- portNVIC_SYSTICK_CTRL_REG |= portNVIC_SYSTICK_ENABLE_BIT;
- vTaskStepTick( ulCompleteTickPeriods );
- portNVIC_SYSTICK_LOAD_REG = ulTimerCountsForOneTick - 1UL;
-
- __enable_irq();
- }
- }
- #endif
- #if ( configOVERRIDE_DEFAULT_TICK_CONFIGURATION == 0 )
- __weak void vPortSetupTimerInterrupt( void )
- {
-
- #if ( configUSE_TICKLESS_IDLE == 1 )
- {
- ulTimerCountsForOneTick = ( configSYSTICK_CLOCK_HZ / configTICK_RATE_HZ );
- xMaximumPossibleSuppressedTicks = portMAX_24_BIT_NUMBER / ulTimerCountsForOneTick;
- ulStoppedTimerCompensation = portMISSED_COUNTS_FACTOR / ( configCPU_CLOCK_HZ / configSYSTICK_CLOCK_HZ );
- }
- #endif
-
- portNVIC_SYSTICK_CTRL_REG = 0UL;
- portNVIC_SYSTICK_CURRENT_VALUE_REG = 0UL;
-
- portNVIC_SYSTICK_LOAD_REG = ( configSYSTICK_CLOCK_HZ / configTICK_RATE_HZ ) - 1UL;
- portNVIC_SYSTICK_CTRL_REG = ( portNVIC_SYSTICK_CLK_BIT | portNVIC_SYSTICK_INT_BIT | portNVIC_SYSTICK_ENABLE_BIT );
- }
- #endif
- __asm uint32_t vPortGetIPSR( void )
- {
- PRESERVE8
- mrs r0, ipsr
- bx r14
- }
- #if ( configASSERT_DEFINED == 1 )
- void vPortValidateInterruptPriority( void )
- {
- uint32_t ulCurrentInterrupt;
- uint8_t ucCurrentPriority;
-
- ulCurrentInterrupt = vPortGetIPSR();
-
- if( ulCurrentInterrupt >= portFIRST_USER_INTERRUPT_NUMBER )
- {
-
- ucCurrentPriority = pcInterruptPriorityRegisters[ ulCurrentInterrupt ];
-
- configASSERT( ucCurrentPriority >= ucMaxSysCallPriority );
- }
-
- configASSERT( ( portAIRCR_REG & portPRIORITY_GROUP_MASK ) <= ulMaxPRIGROUPValue );
- }
- #endif
|