1 /*
2 * FreeRTOS Kernel V10.6.2
3 * Copyright (C) 2020 Synopsys, Inc. or its affiliates. All Rights Reserved.
4 *
5 * SPDX-License-Identifier: MIT
6 *
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:
13 *
14 * The above copyright notice and this permission notice shall be included in all
15 * copies or substantial portions of the Software.
16 *
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.
23 *
24 * https://www.FreeRTOS.org
25 * https://github.com/FreeRTOS
26 *
27 */
28
29 /*
30 * Implementation of functions defined in portable.h
31 */
32
33 #include "FreeRTOS.h"
34 #include "task.h"
35 #include "FreeRTOSConfig.h"
36
37 #include "arc/arc_exception.h"
38 #include "arc/arc_timer.h"
39 #include "board.h"
40
41 #include "arc_freertos_exceptions.h"
42
43 volatile unsigned int ulCriticalNesting = 999UL;
44 volatile unsigned int context_switch_reqflg; /* task context switch request flag in exceptions and interrupts handling */
45
46 /**
47 * \var exc_nest_count
48 * \brief the counter for exc/int processing, =0 no int/exc
49 * >1 in int/exc processing
50 * @}
51 */
52 uint32_t exc_nest_count;
53 /* --------------------------------------------------------------------------*/
54
55 /**
56 * @brief kernel tick interrupt handler of freertos
57 */
58 /* ----------------------------------------------------------------------------*/
vKernelTick(void * ptr)59 static void vKernelTick( void * ptr )
60 {
61 /* clear timer interrupt */
62 arc_timer_int_clear( BOARD_OS_TIMER_ID );
63 board_timer_update( configTICK_RATE_HZ );
64
65 if( xTaskIncrementTick() )
66 {
67 portYIELD_FROM_ISR(); /* need to make task switch */
68 }
69 }
70
71 /* --------------------------------------------------------------------------*/
72
73 /**
74 * @brief setup freertos kernel tick
75 */
76 /* ----------------------------------------------------------------------------*/
prvSetupTimerInterrupt(void)77 static void prvSetupTimerInterrupt( void )
78 {
79 unsigned int cyc = configCPU_CLOCK_HZ / configTICK_RATE_HZ;
80
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 );
84
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 );
88 }
89
90 /*
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.
94 *
95 * For ARC, task context switch is implemented with the help of SWI exception
96 * It's not efficient but simple.
97 *
98 */
pxPortInitialiseStack(StackType_t * pxTopOfStack,TaskFunction_t pxCode,void * pvParameters)99 StackType_t * pxPortInitialiseStack( StackType_t * pxTopOfStack,
100 TaskFunction_t pxCode,
101 void * pvParameters )
102 {
103 /* To ensure asserts in tasks.c don't fail, although in this case the assert
104 * is not really required. */
105 pxTopOfStack--;
106
107 /* Setup the initial stack of the task. The stack is set exactly as
108 * expected by the portRESTORE_CONTEXT() macro. */
109
110 /* When the task starts is will expect to find the function parameter in
111 * R0. */
112 *pxTopOfStack = ( StackType_t ) pvParameters; /* R0 */
113
114 pxTopOfStack--;
115 *pxTopOfStack = ( StackType_t ) pxCode; /* function body */
116
117 /* PC */
118 pxTopOfStack--;
119 *pxTopOfStack = ( StackType_t ) start_r; /* dispatch return address */
120
121 pxTopOfStack--;
122 *pxTopOfStack = ( StackType_t ) portNO_CRITICAL_NESTING;
123 return pxTopOfStack;
124 }
125
126 /* --------------------------------------------------------------------------*/
127
128 /**
129 * @brief start the freertos scheduler, go to the first task
130 *
131 * @returns
132 */
133 /* ----------------------------------------------------------------------------*/
xPortStartScheduler(void)134 BaseType_t xPortStartScheduler( void )
135 {
136 /* Start the timer that generates the tick ISR. */
137 prvSetupTimerInterrupt();
138 start_dispatch();
139
140 /* Should not get here! */
141 return 0;
142 }
143
144 /* --------------------------------------------------------------------------*/
145
146 /**
147 * @brief
148 */
149 /* ----------------------------------------------------------------------------*/
vPortEndScheduler(void)150 void vPortEndScheduler( void )
151 {
152 }
153
154 /* --------------------------------------------------------------------------*/
155
156 /**
157 * @brief generate a task switch request in ISR
158 */
159 /* ----------------------------------------------------------------------------*/
vPortYieldFromIsr(void)160 void vPortYieldFromIsr( void )
161 {
162 unsigned int status32;
163
164 status32 = cpu_lock_save();
165 context_switch_reqflg = true;
166 cpu_unlock_restore( status32 );
167 }
168
169 /* --------------------------------------------------------------------------*/
170
171 /**
172 * @brief
173 */
174 /* ----------------------------------------------------------------------------*/
vPortYield(void)175 void vPortYield( void )
176 {
177 unsigned int status32;
178
179 status32 = cpu_lock_save();
180 dispatch();
181 cpu_unlock_restore( status32 );
182 }
183
184 /* --------------------------------------------------------------------------*/
185
186 /**
187 * @brief
188 */
189 /* ----------------------------------------------------------------------------*/
vPortEndTask(void)190 void vPortEndTask( void )
191 {
192 #if ( INCLUDE_vTaskDelete == 1 )
193 vTaskDelete( NULL ); /* Delete task itself */
194 #endif
195
196 while( 1 ) /* Yield to other task */
197 {
198 vPortYield();
199 }
200 }
201
202 #if ARC_FEATURE_STACK_CHECK
203
204 /*
205 * !!! Note !!!
206 * This a trick!!!
207 * It's a copy from task.c. We need to konw the definition of TCB for the purpose of hardware
208 * stack check. Pls don't forget to update it when FreeRTOS is updated.
209 */
210 typedef struct tskTaskControlBlock /* The old naming convention is used to prevent breaking kernel aware debuggers. */
211 {
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. */
213
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. */
216 #endif
217
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. */
223
224 #if ( ( portSTACK_GROWTH > 0 ) || ( configRECORD_STACK_HIGH_ADDRESS == 1 ) )
225 StackType_t * pxEndOfStack; /*< Points to the highest valid address for the stack. */
226 #endif
227
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. */
230 #endif
231
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. */
235 #endif
236
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;
240 #endif
241
242 #if ( configUSE_APPLICATION_TASK_TAG == 1 )
243 TaskHookFunction_t pxTaskTag;
244 #endif
245
246 #if ( configNUM_THREAD_LOCAL_STORAGE_POINTERS > 0 )
247 void * pvThreadLocalStoragePointers[ configNUM_THREAD_LOCAL_STORAGE_POINTERS ];
248 #endif
249
250 #if ( configGENERATE_RUN_TIME_STATS == 1 )
251 uint32_t ulRunTimeCounter; /*< Stores the amount of time the task has spent in the Running state. */
252 #endif
253
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. */
256 #endif
257
258 #if ( configUSE_TASK_NOTIFICATIONS == 1 )
259 volatile uint32_t ulNotifiedValue;
260 volatile uint8_t ucNotifyState;
261 #endif
262
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. */
267 #endif
268
269 #if ( INCLUDE_xTaskAbortDelay == 1 )
270 uint8_t ucDelayAborted;
271 #endif
272
273 #if ( configUSE_POSIX_ERRNO == 1 )
274 int iTaskErrno;
275 #endif
276 } tskTCB;
277
278
vPortSetStackCheck(TaskHandle_t old,TaskHandle_t new)279 void vPortSetStackCheck( TaskHandle_t old,
280 TaskHandle_t new )
281 {
282 if( new != NULL )
283 {
284 arc_aux_write( AUX_USTACK_BASE, ( uint32_t ) ( new->pxEndOfStack ) );
285 arc_aux_write( AUX_USTACK_TOP, ( uint32_t ) ( new->pxStack ) );
286 }
287 }
288 #endif /* if ARC_FEATURE_STACK_CHECK */
289