2 * FreeRTOS SMP Kernel V202110.00
3 * Copyright (C) 2020 Synopsys, Inc. or its affiliates. All Rights Reserved.
5 * Permission is hereby granted, free of charge, to any person obtaining a copy of
6 * this software and associated documentation files (the "Software"), to deal in
7 * the Software without restriction, including without limitation the rights to
8 * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
9 * the Software, and to permit persons to whom the Software is furnished to do so,
10 * subject to the following conditions:
12 * The above copyright notice and this permission notice shall be included in all
13 * copies or substantial portions of the Software.
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
17 * FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
18 * COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
19 * IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
20 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
22 * https://www.FreeRTOS.org
23 * https://github.com/FreeRTOS
29 * Implementation of functions defined in portable.h
34 #include "FreeRTOSConfig.h"
36 #include "arc/arc_exception.h"
37 #include "arc/arc_timer.h"
40 #include "arc_freertos_exceptions.h"
42 volatile unsigned int ulCriticalNesting = 999UL;
43 volatile unsigned int context_switch_reqflg; /* task context switch request flag in exceptions and interrupts handling */
47 * \brief the counter for exc/int processing, =0 no int/exc
48 * >1 in int/exc processing
51 uint32_t exc_nest_count;
52 /* --------------------------------------------------------------------------*/
55 * @brief kernel tick interrupt handler of freertos
57 /* ----------------------------------------------------------------------------*/
58 static void vKernelTick( void * ptr )
60 /* clear timer interrupt */
61 arc_timer_int_clear( BOARD_OS_TIMER_ID );
62 board_timer_update( configTICK_RATE_HZ );
64 if( xTaskIncrementTick() )
66 portYIELD_FROM_ISR(); /* need to make task switch */
70 /* --------------------------------------------------------------------------*/
73 * @brief setup freertos kernel tick
75 /* ----------------------------------------------------------------------------*/
76 static void prvSetupTimerInterrupt( void )
78 unsigned int cyc = configCPU_CLOCK_HZ / configTICK_RATE_HZ;
80 int_disable( BOARD_OS_TIMER_INTNO ); /* disable os timer interrupt */
81 arc_timer_stop( BOARD_OS_TIMER_ID );
82 arc_timer_start( BOARD_OS_TIMER_ID, TIMER_CTRL_IE | TIMER_CTRL_NH, cyc );
84 int_handler_install( BOARD_OS_TIMER_INTNO, ( INT_HANDLER_T ) vKernelTick );
85 int_pri_set( BOARD_OS_TIMER_INTNO, INT_PRI_MIN );
86 int_enable( BOARD_OS_TIMER_INTNO );
90 * Setup the stack of a new task so it is ready to be placed under the
91 * scheduler control. The registers have to be placed on the stack in
92 * the order that the port expects to find them.
94 * For ARC, task context switch is implemented with the help of SWI exception
95 * It's not efficient but simple.
98 StackType_t * pxPortInitialiseStack( StackType_t * pxTopOfStack,
99 TaskFunction_t pxCode,
100 void * pvParameters )
102 /* To ensure asserts in tasks.c don't fail, although in this case the assert
103 * is not really required. */
106 /* Setup the initial stack of the task. The stack is set exactly as
107 * expected by the portRESTORE_CONTEXT() macro. */
109 /* When the task starts is will expect to find the function parameter in
111 *pxTopOfStack = ( StackType_t ) pvParameters; /* R0 */
114 *pxTopOfStack = ( StackType_t ) pxCode; /* function body */
118 *pxTopOfStack = ( StackType_t ) start_r; /* dispatch return address */
121 *pxTopOfStack = ( StackType_t ) portNO_CRITICAL_NESTING;
125 /* --------------------------------------------------------------------------*/
128 * @brief start the freertos scheduler, go to the first task
132 /* ----------------------------------------------------------------------------*/
133 BaseType_t xPortStartScheduler( void )
135 /* Start the timer that generates the tick ISR. */
136 prvSetupTimerInterrupt();
139 /* Should not get here! */
143 /* --------------------------------------------------------------------------*/
148 /* ----------------------------------------------------------------------------*/
149 void vPortEndScheduler( void )
153 /* --------------------------------------------------------------------------*/
156 * @brief generate a task switch request in ISR
158 /* ----------------------------------------------------------------------------*/
159 void vPortYieldFromIsr( void )
161 unsigned int status32;
163 status32 = cpu_lock_save();
164 context_switch_reqflg = true;
165 cpu_unlock_restore( status32 );
168 /* --------------------------------------------------------------------------*/
173 /* ----------------------------------------------------------------------------*/
174 void vPortYield( void )
176 unsigned int status32;
178 status32 = cpu_lock_save();
180 cpu_unlock_restore( status32 );
183 /* --------------------------------------------------------------------------*/
188 /* ----------------------------------------------------------------------------*/
189 void vPortEndTask( void )
191 #if ( INCLUDE_vTaskDelete == 1 )
192 vTaskDelete( NULL ); /* Delete task itself */
195 while( 1 ) /* Yield to other task */
201 #if ARC_FEATURE_STACK_CHECK
206 * It's a copy from task.c. We need to konw the definition of TCB for the purpose of hardware
207 * stack check. Pls don't forget to update it when FreeRTOS is updated.
209 typedef struct tskTaskControlBlock /* The old naming convention is used to prevent breaking kernel aware debuggers. */
211 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. */
213 #if ( portUSING_MPU_WRAPPERS == 1 )
214 xMPU_SETTINGS xMPUSettings; /*< The MPU settings are defined as part of the port layer. THIS MUST BE THE SECOND MEMBER OF THE TCB STRUCT. */
217 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 ). */
218 ListItem_t xEventListItem; /*< Used to reference a task from an event list. */
219 UBaseType_t uxPriority; /*< The priority of the task. 0 is the lowest priority. */
220 StackType_t * pxStack; /*< Points to the start of the stack. */
221 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. */
223 #if ( ( portSTACK_GROWTH > 0 ) || ( configRECORD_STACK_HIGH_ADDRESS == 1 ) )
224 StackType_t * pxEndOfStack; /*< Points to the highest valid address for the stack. */
227 #if ( portCRITICAL_NESTING_IN_TCB == 1 )
228 UBaseType_t uxCriticalNesting; /*< Holds the critical section nesting depth for ports that do not maintain their own count in the port layer. */
231 #if ( configUSE_TRACE_FACILITY == 1 )
232 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. */
233 UBaseType_t uxTaskNumber; /*< Stores a number specifically for use by third party trace code. */
236 #if ( configUSE_MUTEXES == 1 )
237 UBaseType_t uxBasePriority; /*< The priority last assigned to the task - used by the priority inheritance mechanism. */
238 UBaseType_t uxMutexesHeld;
241 #if ( configUSE_APPLICATION_TASK_TAG == 1 )
242 TaskHookFunction_t pxTaskTag;
245 #if ( configNUM_THREAD_LOCAL_STORAGE_POINTERS > 0 )
246 void * pvThreadLocalStoragePointers[ configNUM_THREAD_LOCAL_STORAGE_POINTERS ];
249 #if ( configGENERATE_RUN_TIME_STATS == 1 )
250 uint32_t ulRunTimeCounter; /*< Stores the amount of time the task has spent in the Running state. */
253 #if ( configUSE_NEWLIB_REENTRANT == 1 )
255 /* Allocate a Newlib reent structure that is specific to this task.
256 * Note Newlib support has been included by popular demand, but is not
257 * used by the FreeRTOS maintainers themselves. FreeRTOS is not
258 * responsible for resulting newlib operation. User must be familiar with
259 * newlib and must provide system-wide implementations of the necessary
260 * stubs. Be warned that (at the time of writing) the current newlib design
261 * implements a system-wide malloc() that must be provided with locks. */
262 struct _reent xNewLib_reent;
265 #if ( configUSE_TASK_NOTIFICATIONS == 1 )
266 volatile uint32_t ulNotifiedValue;
267 volatile uint8_t ucNotifyState;
270 /* See the comments above the definition of
271 * tskSTATIC_AND_DYNAMIC_ALLOCATION_POSSIBLE. */
272 #if ( tskSTATIC_AND_DYNAMIC_ALLOCATION_POSSIBLE != 0 ) /*lint !e731 !e9029 Macro has been consolidated for readability reasons. */
273 uint8_t ucStaticallyAllocated; /*< Set to pdTRUE if the task is a statically allocated to ensure no attempt is made to free the memory. */
276 #if ( INCLUDE_xTaskAbortDelay == 1 )
277 uint8_t ucDelayAborted;
280 #if ( configUSE_POSIX_ERRNO == 1 )
286 void vPortSetStackCheck( TaskHandle_t old,
291 arc_aux_write( AUX_USTACK_BASE, ( uint32_t ) ( new->pxEndOfStack ) );
292 arc_aux_write( AUX_USTACK_TOP, ( uint32_t ) ( new->pxStack ) );
295 #endif /* if ARC_FEATURE_STACK_CHECK */