123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531 |
- #include <stdlib.h>
- #include "FreeRTOS.h"
- #include "task.h"
- #ifndef configINTERRUPT_CONTROLLER_BASE_ADDRESS
- #error configINTERRUPT_CONTROLLER_BASE_ADDRESS must be defined. Refer to Cortex-A equivalent: http:
- #endif
- #ifndef configINTERRUPT_CONTROLLER_CPU_INTERFACE_OFFSET
- #error configINTERRUPT_CONTROLLER_CPU_INTERFACE_OFFSET must be defined. Refer to Cortex-A equivalent: http:
- #endif
- #ifndef configUNIQUE_INTERRUPT_PRIORITIES
- #error configUNIQUE_INTERRUPT_PRIORITIES must be defined. Refer to Cortex-A equivalent: http:
- #endif
- #ifndef configSETUP_TICK_INTERRUPT
- #error configSETUP_TICK_INTERRUPT() must be defined. Refer to Cortex-A equivalent: http:
- #endif
- #ifndef configMAX_API_CALL_INTERRUPT_PRIORITY
- #error configMAX_API_CALL_INTERRUPT_PRIORITY must be defined. Refer to Cortex-A equivalent: http:
- #endif
- #if configMAX_API_CALL_INTERRUPT_PRIORITY == 0
- #error configMAX_API_CALL_INTERRUPT_PRIORITY must not be set to 0
- #endif
- #if configMAX_API_CALL_INTERRUPT_PRIORITY > configUNIQUE_INTERRUPT_PRIORITIES
- #error configMAX_API_CALL_INTERRUPT_PRIORITY must be less than or equal to configUNIQUE_INTERRUPT_PRIORITIES as the lower the numeric priority value the higher the logical interrupt priority
- #endif
- #if configUSE_PORT_OPTIMISED_TASK_SELECTION == 1
-
- #if ( configMAX_PRIORITIES > 32 )
- #error configUSE_PORT_OPTIMISED_TASK_SELECTION can only be set to 1 when configMAX_PRIORITIES is less than or equal to 32. It is very rare that a system requires more than 10 to 15 difference priorities as tasks that share a priority will time slice.
- #endif
- #endif
- #if configMAX_API_CALL_INTERRUPT_PRIORITY <= ( configUNIQUE_INTERRUPT_PRIORITIES / 2 )
- #error configMAX_API_CALL_INTERRUPT_PRIORITY must be greater than ( configUNIQUE_INTERRUPT_PRIORITIES / 2 )
- #endif
- #ifndef configCLEAR_TICK_INTERRUPT
- #define configCLEAR_TICK_INTERRUPT()
- #endif
- #define portNO_CRITICAL_NESTING ( ( uint32_t ) 0 )
- #define portUNMASK_VALUE ( 0xFFUL )
- #define portNO_FLOATING_POINT_CONTEXT ( ( StackType_t ) 0 )
- #define portINITIAL_SPSR ( ( StackType_t ) 0x1f )
- #define portTHUMB_MODE_BIT ( ( StackType_t ) 0x20 )
- #define portINTERRUPT_ENABLE_BIT ( 0x80UL )
- #define portTHUMB_MODE_ADDRESS ( 0x01UL )
- #define portBINARY_POINT_BITS ( ( uint8_t ) 0x03 )
- #define portAPSR_MODE_BITS_MASK ( 0x1F )
- #define portAPSR_USER_MODE ( 0x10 )
- #define portCPU_IRQ_DISABLE() \
- __asm volatile ( "CPSID i" ::: "memory" ); \
- __asm volatile ( "DSB" ); \
- __asm volatile ( "ISB" );
- #define portCPU_IRQ_ENABLE() \
- __asm volatile ( "CPSIE i" ::: "memory" ); \
- __asm volatile ( "DSB" ); \
- __asm volatile ( "ISB" );
- #define portCLEAR_INTERRUPT_MASK() \
- { \
- portCPU_IRQ_DISABLE(); \
- portICCPMR_PRIORITY_MASK_REGISTER = portUNMASK_VALUE; \
- __asm volatile ( "DSB \n" \
- "ISB \n"); \
- portCPU_IRQ_ENABLE(); \
- }
- #define portINTERRUPT_PRIORITY_REGISTER_OFFSET 0x400UL
- #define portMAX_8_BIT_VALUE ( ( uint8_t ) 0xff )
- #define portBIT_0_SET ( ( uint8_t ) 0x01 )
- #ifdef configTASK_RETURN_ADDRESS
- #define portTASK_RETURN_ADDRESS configTASK_RETURN_ADDRESS
- #else
- #define portTASK_RETURN_ADDRESS prvTaskExitError
- #endif
- extern void vPortRestoreTaskContext( void );
- static void prvTaskExitError( void );
- volatile uint32_t ulCriticalNesting = 9999UL;
- uint32_t ulPortTaskHasFPUContext = pdFALSE;
- uint32_t ulPortYieldRequired = pdFALSE;
- uint32_t ulPortInterruptNesting = 0UL;
- __attribute__( ( used ) ) const uint32_t ulICCIAR = portICCIAR_INTERRUPT_ACKNOWLEDGE_REGISTER_ADDRESS;
- __attribute__( ( used ) ) const uint32_t ulICCEOIR = portICCEOIR_END_OF_INTERRUPT_REGISTER_ADDRESS;
- __attribute__( ( used ) ) const uint32_t ulICCPMR = portICCPMR_PRIORITY_MASK_REGISTER_ADDRESS;
- __attribute__( ( used ) ) const uint32_t ulMaxAPIPriorityMask = ( configMAX_API_CALL_INTERRUPT_PRIORITY << portPRIORITY_SHIFT );
- StackType_t * pxPortInitialiseStack( StackType_t * pxTopOfStack,
- TaskFunction_t pxCode,
- void * pvParameters )
- {
-
- *pxTopOfStack = ( StackType_t ) NULL;
- pxTopOfStack--;
- *pxTopOfStack = ( StackType_t ) NULL;
- pxTopOfStack--;
- *pxTopOfStack = ( StackType_t ) NULL;
- pxTopOfStack--;
- *pxTopOfStack = ( StackType_t ) portINITIAL_SPSR;
- if( ( ( uint32_t ) pxCode & portTHUMB_MODE_ADDRESS ) != 0x00UL )
- {
-
- *pxTopOfStack |= portTHUMB_MODE_BIT;
- }
- pxTopOfStack--;
-
- *pxTopOfStack = ( StackType_t ) pxCode;
- pxTopOfStack--;
-
- *pxTopOfStack = ( StackType_t ) portTASK_RETURN_ADDRESS;
- pxTopOfStack--;
- *pxTopOfStack = ( StackType_t ) 0x12121212;
- pxTopOfStack--;
- *pxTopOfStack = ( StackType_t ) 0x11111111;
- pxTopOfStack--;
- *pxTopOfStack = ( StackType_t ) 0x10101010;
- pxTopOfStack--;
- *pxTopOfStack = ( StackType_t ) 0x09090909;
- pxTopOfStack--;
- *pxTopOfStack = ( StackType_t ) 0x08080808;
- pxTopOfStack--;
- *pxTopOfStack = ( StackType_t ) 0x07070707;
- pxTopOfStack--;
- *pxTopOfStack = ( StackType_t ) 0x06060606;
- pxTopOfStack--;
- *pxTopOfStack = ( StackType_t ) 0x05050505;
- pxTopOfStack--;
- *pxTopOfStack = ( StackType_t ) 0x04040404;
- pxTopOfStack--;
- *pxTopOfStack = ( StackType_t ) 0x03030303;
- pxTopOfStack--;
- *pxTopOfStack = ( StackType_t ) 0x02020202;
- pxTopOfStack--;
- *pxTopOfStack = ( StackType_t ) 0x01010101;
- pxTopOfStack--;
- *pxTopOfStack = ( StackType_t ) pvParameters;
- pxTopOfStack--;
-
- *pxTopOfStack = portNO_CRITICAL_NESTING;
- pxTopOfStack--;
-
- *pxTopOfStack = portNO_FLOATING_POINT_CONTEXT;
- return pxTopOfStack;
- }
- static void prvTaskExitError( void )
- {
-
- configASSERT( ulPortInterruptNesting == ~0UL );
- portDISABLE_INTERRUPTS();
- for( ; ; )
- {
- }
- }
- BaseType_t xPortStartScheduler( void )
- {
- uint32_t ulAPSR, ulCycles = 8;
- #if ( configASSERT_DEFINED == 1 )
- {
- volatile uint32_t ulOriginalPriority;
- volatile uint8_t * const pucFirstUserPriorityRegister = ( volatile uint8_t * const ) ( configINTERRUPT_CONTROLLER_BASE_ADDRESS + portINTERRUPT_PRIORITY_REGISTER_OFFSET );
- volatile uint8_t ucMaxPriorityValue;
-
- ulOriginalPriority = *pucFirstUserPriorityRegister;
-
- *pucFirstUserPriorityRegister = portMAX_8_BIT_VALUE;
-
- ucMaxPriorityValue = *pucFirstUserPriorityRegister;
-
- while( ( ucMaxPriorityValue & portBIT_0_SET ) != portBIT_0_SET )
- {
- ucMaxPriorityValue >>= ( uint8_t ) 0x01;
-
- ulCycles--;
- if( ulCycles == 0 )
- {
- break;
- }
- }
-
- configASSERT( ucMaxPriorityValue == portLOWEST_INTERRUPT_PRIORITY );
-
- *pucFirstUserPriorityRegister = ulOriginalPriority;
- }
- #endif
-
- __asm volatile ( "MRS %0, APSR" : "=r" ( ulAPSR )::"memory" );
- ulAPSR &= portAPSR_MODE_BITS_MASK;
- configASSERT( ulAPSR != portAPSR_USER_MODE );
- if( ulAPSR != portAPSR_USER_MODE )
- {
-
- configASSERT( ( portICCBPR_BINARY_POINT_REGISTER & portBINARY_POINT_BITS ) <= portMAX_BINARY_POINT_VALUE );
- if( ( portICCBPR_BINARY_POINT_REGISTER & portBINARY_POINT_BITS ) <= portMAX_BINARY_POINT_VALUE )
- {
-
- portCPU_IRQ_DISABLE();
-
- configSETUP_TICK_INTERRUPT();
-
- vPortRestoreTaskContext();
- }
- }
-
- ( void ) prvTaskExitError;
- return 0;
- }
- void vPortEndScheduler( void )
- {
-
- configASSERT( ulCriticalNesting == 1000UL );
- }
- void vPortEnterCritical( void )
- {
-
- ulPortSetInterruptMask();
-
- ulCriticalNesting++;
-
- if( ulCriticalNesting == 1 )
- {
- configASSERT( ulPortInterruptNesting == 0 );
- }
- }
- void vPortExitCritical( void )
- {
- if( ulCriticalNesting > portNO_CRITICAL_NESTING )
- {
-
- ulCriticalNesting--;
-
- if( ulCriticalNesting == portNO_CRITICAL_NESTING )
- {
-
- portCLEAR_INTERRUPT_MASK();
- }
- }
- }
- void FreeRTOS_Tick_Handler( void )
- {
-
- portCPU_IRQ_DISABLE();
- portICCPMR_PRIORITY_MASK_REGISTER = ( uint32_t ) ( configMAX_API_CALL_INTERRUPT_PRIORITY << portPRIORITY_SHIFT );
- __asm volatile ( "dsb \n"
- "isb \n"::: "memory" );
- portCPU_IRQ_ENABLE();
-
- if( xTaskIncrementTick() != pdFALSE )
- {
- ulPortYieldRequired = pdTRUE;
- }
-
- portCLEAR_INTERRUPT_MASK();
- configCLEAR_TICK_INTERRUPT();
- }
- void vPortTaskUsesFPU( void )
- {
- uint32_t ulInitialFPSCR = 0;
-
- ulPortTaskHasFPUContext = pdTRUE;
-
- __asm volatile ( "FMXR FPSCR, %0" ::"r" ( ulInitialFPSCR ) : "memory" );
- }
- void vPortClearInterruptMask( uint32_t ulNewMaskValue )
- {
- if( ulNewMaskValue == pdFALSE )
- {
- portCLEAR_INTERRUPT_MASK();
- }
- }
- uint32_t ulPortSetInterruptMask( void )
- {
- uint32_t ulReturn;
-
- portCPU_IRQ_DISABLE();
- if( portICCPMR_PRIORITY_MASK_REGISTER == ( uint32_t ) ( configMAX_API_CALL_INTERRUPT_PRIORITY << portPRIORITY_SHIFT ) )
- {
-
- ulReturn = pdTRUE;
- }
- else
- {
- ulReturn = pdFALSE;
- portICCPMR_PRIORITY_MASK_REGISTER = ( uint32_t ) ( configMAX_API_CALL_INTERRUPT_PRIORITY << portPRIORITY_SHIFT );
- __asm volatile ( "dsb \n"
- "isb \n"::: "memory" );
- }
- portCPU_IRQ_ENABLE();
- return ulReturn;
- }
- #if ( configASSERT_DEFINED == 1 )
- void vPortValidateInterruptPriority( void )
- {
-
- configASSERT( portICCRPR_RUNNING_PRIORITY_REGISTER >= ( uint32_t ) ( configMAX_API_CALL_INTERRUPT_PRIORITY << portPRIORITY_SHIFT ) );
-
- configASSERT( ( portICCBPR_BINARY_POINT_REGISTER & portBINARY_POINT_BITS ) <= portMAX_BINARY_POINT_VALUE );
- }
- #endif
|