2 * FreeRTOS Kernel <DEVELOPMENT BRANCH>
3 * Copyright (C) 2020 Synopsys, Inc. or its affiliates. All Rights Reserved.
5 * SPDX-License-Identifier: MIT
7 * Permission is hereby granted, free of charge, to any person obtaining a copy of
8 * this software and associated documentation files (the "Software"), to deal in
9 * the Software without restriction, including without limitation the rights to
10 * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
11 * the Software, and to permit persons to whom the Software is furnished to do so,
12 * subject to the following conditions:
14 * The above copyright notice and this permission notice shall be included in all
15 * copies or substantial portions of the Software.
17 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
19 * FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
20 * COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
21 * IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
22 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
24 * https://www.FreeRTOS.org
25 * https://github.com/FreeRTOS
30 * Implementation of functions defined in portable.h
35 #include "FreeRTOSConfig.h"
37 #include "arc/arc_exception.h"
38 #include "arc/arc_timer.h"
41 #include "arc_freertos_exceptions.h"
43 volatile unsigned int ulCriticalNesting = 999UL;
44 volatile unsigned int context_switch_reqflg; /* task context switch request flag in exceptions and interrupts handling */
48 * \brief the counter for exc/int processing, =0 no int/exc
49 * >1 in int/exc processing
52 uint32_t exc_nest_count;
53 /* --------------------------------------------------------------------------*/
56 * @brief kernel tick interrupt handler of freertos
58 /* ----------------------------------------------------------------------------*/
59 static void vKernelTick( void * ptr )
61 /* clear timer interrupt */
62 arc_timer_int_clear( BOARD_OS_TIMER_ID );
63 board_timer_update( configTICK_RATE_HZ );
65 if( xTaskIncrementTick() )
67 portYIELD_FROM_ISR(); /* need to make task switch */
71 /* --------------------------------------------------------------------------*/
74 * @brief setup freertos kernel tick
76 /* ----------------------------------------------------------------------------*/
77 static void prvSetupTimerInterrupt( void )
79 unsigned int cyc = configCPU_CLOCK_HZ / configTICK_RATE_HZ;
81 int_disable( BOARD_OS_TIMER_INTNO ); /* disable os timer interrupt */
82 arc_timer_stop( BOARD_OS_TIMER_ID );
83 arc_timer_start( BOARD_OS_TIMER_ID, TIMER_CTRL_IE | TIMER_CTRL_NH, cyc );
85 int_handler_install( BOARD_OS_TIMER_INTNO, ( INT_HANDLER_T ) vKernelTick );
86 int_pri_set( BOARD_OS_TIMER_INTNO, INT_PRI_MIN );
87 int_enable( BOARD_OS_TIMER_INTNO );
91 * Setup the stack of a new task so it is ready to be placed under the
92 * scheduler control. The registers have to be placed on the stack in
93 * the order that the port expects to find them.
95 * For ARC, task context switch is implemented with the help of SWI exception
96 * It's not efficient but simple.
99 StackType_t * pxPortInitialiseStack( StackType_t * pxTopOfStack,
100 TaskFunction_t pxCode,
101 void * pvParameters )
103 /* To ensure asserts in tasks.c don't fail, although in this case the assert
104 * is not really required. */
107 /* Setup the initial stack of the task. The stack is set exactly as
108 * expected by the portRESTORE_CONTEXT() macro. */
110 /* When the task starts is will expect to find the function parameter in
112 *pxTopOfStack = ( StackType_t ) pvParameters; /* R0 */
115 *pxTopOfStack = ( StackType_t ) pxCode; /* function body */
119 *pxTopOfStack = ( StackType_t ) start_r; /* dispatch return address */
122 *pxTopOfStack = ( StackType_t ) portNO_CRITICAL_NESTING;
126 /* --------------------------------------------------------------------------*/
129 * @brief start the freertos scheduler, go to the first task
133 /* ----------------------------------------------------------------------------*/
134 BaseType_t xPortStartScheduler( void )
136 /* Start the timer that generates the tick ISR. */
137 prvSetupTimerInterrupt();
140 /* Should not get here! */
144 /* --------------------------------------------------------------------------*/
149 /* ----------------------------------------------------------------------------*/
150 void vPortEndScheduler( void )
154 /* --------------------------------------------------------------------------*/
157 * @brief generate a task switch request in ISR
159 /* ----------------------------------------------------------------------------*/
160 void vPortYieldFromIsr( void )
162 unsigned int status32;
164 status32 = cpu_lock_save();
165 context_switch_reqflg = true;
166 cpu_unlock_restore( status32 );
169 /* --------------------------------------------------------------------------*/
174 /* ----------------------------------------------------------------------------*/
175 void vPortYield( void )
177 unsigned int status32;
179 status32 = cpu_lock_save();
181 cpu_unlock_restore( status32 );
184 /* --------------------------------------------------------------------------*/
189 /* ----------------------------------------------------------------------------*/
190 void vPortEndTask( void )
192 #if ( INCLUDE_vTaskDelete == 1 )
193 vTaskDelete( NULL ); /* Delete task itself */
196 while( 1 ) /* Yield to other task */
202 #if ARC_FEATURE_STACK_CHECK
207 * It's a copy from task.c. We need to know the definition of TCB for the purpose of hardware
208 * stack check. Pls don't forget to update it when FreeRTOS is updated.
210 typedef struct tskTaskControlBlock /* The old naming convention is used to prevent breaking kernel aware debuggers. */
212 volatile StackType_t * pxTopOfStack; /*< Points to the location of the last item placed on the tasks stack. THIS MUST BE THE FIRST MEMBER OF THE TCB STRUCT. */
214 #if ( portUSING_MPU_WRAPPERS == 1 )
215 xMPU_SETTINGS xMPUSettings; /*< The MPU settings are defined as part of the port layer. THIS MUST BE THE SECOND MEMBER OF THE TCB STRUCT. */
218 ListItem_t xStateListItem; /*< The list that the state list item of a task is reference from denotes the state of that task (Ready, Blocked, Suspended ). */
219 ListItem_t xEventListItem; /*< Used to reference a task from an event list. */
220 UBaseType_t uxPriority; /*< The priority of the task. 0 is the lowest priority. */
221 StackType_t * pxStack; /*< Points to the start of the stack. */
222 char pcTaskName[ configMAX_TASK_NAME_LEN ]; /*< Descriptive name given to the task when created. Facilitates debugging only. */ /*lint !e971 Unqualified char types are allowed for strings and single characters only. */
224 #if ( ( portSTACK_GROWTH > 0 ) || ( configRECORD_STACK_HIGH_ADDRESS == 1 ) )
225 StackType_t * pxEndOfStack; /*< Points to the highest valid address for the stack. */
228 #if ( portCRITICAL_NESTING_IN_TCB == 1 )
229 UBaseType_t uxCriticalNesting; /*< Holds the critical section nesting depth for ports that do not maintain their own count in the port layer. */
232 #if ( configUSE_TRACE_FACILITY == 1 )
233 UBaseType_t uxTCBNumber; /*< Stores a number that increments each time a TCB is created. It allows debuggers to determine when a task has been deleted and then recreated. */
234 UBaseType_t uxTaskNumber; /*< Stores a number specifically for use by third party trace code. */
237 #if ( configUSE_MUTEXES == 1 )
238 UBaseType_t uxBasePriority; /*< The priority last assigned to the task - used by the priority inheritance mechanism. */
239 UBaseType_t uxMutexesHeld;
242 #if ( configUSE_APPLICATION_TASK_TAG == 1 )
243 TaskHookFunction_t pxTaskTag;
246 #if ( configNUM_THREAD_LOCAL_STORAGE_POINTERS > 0 )
247 void * pvThreadLocalStoragePointers[ configNUM_THREAD_LOCAL_STORAGE_POINTERS ];
250 #if ( configGENERATE_RUN_TIME_STATS == 1 )
251 uint32_t ulRunTimeCounter; /*< Stores the amount of time the task has spent in the Running state. */
254 #if ( configUSE_C_RUNTIME_TLS_SUPPORT == 1 )
255 configTLS_BLOCK_TYPE xTLSBlock; /*< Memory block used as Thread Local Storage (TLS) Block for the task. */
258 #if ( configUSE_TASK_NOTIFICATIONS == 1 )
259 volatile uint32_t ulNotifiedValue;
260 volatile uint8_t ucNotifyState;
263 /* See the comments above the definition of
264 * tskSTATIC_AND_DYNAMIC_ALLOCATION_POSSIBLE. */
265 #if ( tskSTATIC_AND_DYNAMIC_ALLOCATION_POSSIBLE != 0 ) /*lint !e731 !e9029 Macro has been consolidated for readability reasons. */
266 uint8_t ucStaticallyAllocated; /*< Set to pdTRUE if the task is a statically allocated to ensure no attempt is made to free the memory. */
269 #if ( INCLUDE_xTaskAbortDelay == 1 )
270 uint8_t ucDelayAborted;
273 #if ( configUSE_POSIX_ERRNO == 1 )
279 void vPortSetStackCheck( TaskHandle_t old,
284 #if ARC_FEATURE_SEC_PRESENT
285 arc_aux_write( AUX_S_KSTACK_BASE, ( uint32_t ) ( new->pxEndOfStack ) );
286 arc_aux_write( AUX_S_KSTACK_TOP, ( uint32_t ) ( new->pxStack ) );
288 arc_aux_write( AUX_KSTACK_BASE, ( uint32_t ) ( new->pxEndOfStack ) );
289 arc_aux_write( AUX_KSTACK_TOP, ( uint32_t ) ( new->pxStack ) );
293 #endif /* if ARC_FEATURE_STACK_CHECK */